private void button2_Click_2(object sender, EventArgs e) { if (graph2FrequencyRanges.Count > 0) { FrequencyRange frequencyRange = graph2FrequencyRanges.Pop(); long lowerIndex = (long)((frequencyRange.lower - dataLowerFrequency) / binSize); long upperIndex = (long)((frequencyRange.upper - dataLowerFrequency) / binSize); graph2LowerFrequency = frequencyRange.lower; graph2UpperFrequency = frequencyRange.upper; if (series1BinData != null) { RangeChanged(chart2, series1BinData.dataSeries, series1BinData.avgBinArray, lowerIndex, upperIndex, graph2LowerFrequency, ref graph2BinFreqInc); } if (series2BinData != null) { RangeChanged(chart2, series2BinData.dataSeries, series2BinData.avgBinArray, lowerIndex, upperIndex, graph2LowerFrequency, ref graph2BinFreqInc); } if (series1BinData != null && series2BinData != null) { GraphDifference(series1BinData, series2BinData); } } }
/// <summary> /// Grab samples out of the data container, belonging to the given freqRange. /// Time - is the desired track time we're grabbing samples from. /// </summary> /// <param name="freqRange"></param> /// <param name="time"></param> /// <returns></returns> public float[] GetSamples(FrequencyRange freqRange, float time) { if (time > clipLength) { //return empty samples if the song has ended return(new float[bufferSize]); } List <Array> samples = GetSampleArray(freqRange); float percentThroughSong = time / clipLength; int sampleIndex = (int)((samples.Count - 1) * percentThroughSong); if (percentThroughSong < 0) { sampleIndex = 0; } if (samples.Count == 0) { Debug.LogError("Error: samples in the FrequencyRange: " + freqRange + " were not recorded for this file"); return(null); } return(samples[sampleIndex].data); }
/// <summary> /// Grab the correct sample storage list based on FrequencyRange /// </summary> /// <param name="freqRange"></param> /// <returns></returns> public List <Array> GetSampleArray(FrequencyRange freqRange) { switch (freqRange) { case FrequencyRange.SubBase: return(subBassSamples); case FrequencyRange.Bass: return(bassSamples); case FrequencyRange.LowMidrange: return(lowMidSamples); case FrequencyRange.Midrange: return(midSamples); case FrequencyRange.UpperMidrange: return(upperMidSamples); case FrequencyRange.High: return(highSamples); case FrequencyRange.VeryHigh: return(veryHighSamples); default: case FrequencyRange.Decibal: return(decibalSamples); } }
//like GetAvg or GetRMS, but inside a given frequency range public float GetFrequencyVol(int audioSourceIndex, FrequencyRange freqRange) { // if (!audioSources[audioSourceIndex].mute) // if not muted // { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh for (int i = n1; i <= n2; i++) { if (i < freqData.Length) { sum += Mathf.Abs(freqData[i]); } } sum = sum * audioSources[audioSourceIndex].volume; return(sum / (n2 - n1 + 1)); // } // // return 0; }
//return the raw spectrum data i nthe given frequency range. public float[] GetFrequencyData(int audioSourceIndex, FrequencyRange freqRange) { // if (!audioSources[audioSourceIndex].mute) // if not muted // { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh List <float> validData = new List <float>(); for (int i = n1; i <= n2; i++) { validData.Add(freqData[i] * audioSources[audioSourceIndex].volume); } float[] normData = NormalizeArray(validData.ToArray()); return(normData); // } // // Debug.LogWarning("warning: Audio Source: " + audioSourceIndex + " is muted"); // return new float[samplesToTake]; }
//like GetAvg or GetRMS, but inside a given frequency range public float GetFrequencyVol(int audioSourceIndex, FrequencyRange freqRange, bool useAudioFile, bool useSilent = false) { //grab the correct audio source, from the correct list! Prebeat or normal AudioSource source = useSilent ? silentAudio.audioSources[audioSourceIndex] : audioSources[audioSourceIndex]; if (!source.mute) // if not muted { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; if (useAudioFile) { freqData = GetAudioData(audioSourceIndex).GetSamples(freqRange, audioTimer); } else { source.GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); } int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh for (int i = n1; i <= n2; i++) { if (i < freqData.Length && i >= 0) { sum += Mathf.Abs(freqData[i]); } } //multiply by volume if we're not using mic input, where volume is set to 0 to avoid feedback issues; if (usingMic) { //zero out the sum if we don't meet the min microphone volume requirement if (sum < micVolumeThreshold) { if (debug) { Debug.Log("Mic Filtered Out: " + sum + "<" + micVolumeThreshold); } sum = 0; } } else { sum = sum * source.volume; } float frequency = sum / (n2 - n1 + 1); //Debug.Log("Freq: " + frequency); return(frequency); } return(0); }
public static FrequencyRange GetIndicesForFrequencyRange(long specifiedLowerFrequency, long specifiedUpperFrequency, long dataLowerFrequency, double binFrequencySize) { long lowerIndex = (long)(Math.Round((specifiedLowerFrequency - dataLowerFrequency) / binFrequencySize)); long upperIndex = (long)(Math.Round((specifiedUpperFrequency - dataLowerFrequency) / binFrequencySize)); FrequencyRange frequencyRange = new FrequencyRange(lowerIndex, upperIndex); return(frequencyRange); }
//return the raw spectrum data i nthe given frequency range, using the specified number of bins public float[] GetFrequencyData(int audioSourceIndex, FrequencyRange freqRange, int numBins, bool abs, bool useAudioFile) { if (audioSources[audioSourceIndex].mute && !useAudioFile) { Debug.LogWarning("warning: Audio Source: " + audioSourceIndex + " is muted"); return(new float[numBins]); } Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; if (useAudioFile) { freqData = GetAudioData(audioSourceIndex).GetSamples(freqRange, audioTimer); } else { audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); } int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; //Debug.Log("Sampling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh //Debug.Log("Valid Freq Data: (" + n1 + "-" + n2 + ")/" + samplesToTake); string f = ""; List <float> validData = new List <float>(); for (int i = n1; i <= n2; i++) { float frequency = freqData[i]; if (abs) { frequency = Mathf.Abs(freqData[i]); } //multiply by volume if we're not using mic input, where volume is set to 0 to avoid feedback issues; if (!usingMic) { frequency = frequency * audioSources[audioSourceIndex].volume; } validData.Add(frequency); f += frequency.ToString() + " , "; } //Debug.Log("validData: " + f); //Debug.Log("NumBins: " + numBins); float[] binnedArray = GetBinnedArray(validData.ToArray(), numBins); float[] normalizedArray = NormalizeArray(binnedArray); return(normalizedArray); }
void Update() { var microphoneInput = MicrophoneInput.Instance; if (microphoneInput != null) { var newExcitation = FrequencyRange.Overlap(microphoneInput.Spectrum, ModeGraphic.ExcitationFrequency); var mode = ModeGraphic.VibrationalMode; var target = Mathf.Clamp01(newExcitation); mode.Excitation = Mathf.MoveTowards(mode.Excitation, target, (target > mode.Excitation ? 1f : 0.25f) * Time.deltaTime * 2f); } }
public float[] GetFrequencyRange(FrequencyRange frequencyRange) { switch (frequencyRange) { case FrequencyRange.SubBase: return(new float[2] { 0, 60 }); case FrequencyRange.Bass: return(new float[2] { 60, 250 }); case FrequencyRange.LowMidrange: return(new float[2] { 250, 500 }); case FrequencyRange.Midrange: return(new float[2] { 500, 2000 }); case FrequencyRange.UpperMidrange: return(new float[2] { 2000, 4000 }); case FrequencyRange.High: return(new float[2] { 4000, 6000 }); case FrequencyRange.VeryHigh: return(new float[2] { 6000, 20000 }); case FrequencyRange.Decibel: return(new float[2] { 0, 20000 }); default: Debug.LogError("GetFrequencyRange: value out of range"); return(new float[2] { float.NaN, float.NaN }); } }
public static float Overlap(FrequencyRange reading, FrequencyRange area) { float v = 0f; float tot = 0f; for (var i = 0; i < reading.Length; i++) { var inputValue = reading.FrequencyData [i]; var targetValue = area.FrequencyData [i]; v += Mathf.Clamp01(inputValue / targetValue) * targetValue; tot += targetValue; } return(v / tot); }
void OnSetMode(VibrationalMode mode) { this.VibrationalMode = mode; var spectrum = new float[AppManager.Instance.NumberOfFrequencies]; var maximumFrequency = AppManager.Instance.MaximumFrequency; var resonanceFrequency = AudioFrequency; for (int i = 0; i < spectrum.Length; i++) { float freq = (1f * i / spectrum.Length) * maximumFrequency; spectrum [i] = GaussianScale * Mathf.Exp(-Mathf.Pow(GaussianStrength * (freq - resonanceFrequency), 2)); } ExcitationFrequency = new FrequencyRange(spectrum, 0, maximumFrequency); }
public static Vector2 GetFreqForRange(FrequencyRange freqRange) { switch (freqRange) { case FrequencyRange.SubBase: return(new Vector2(20, 60)); break; case FrequencyRange.Bass: return(new Vector2(60, 250)); break; case FrequencyRange.LowMidrange: return(new Vector2(250, 500)); break; case FrequencyRange.Midrange: return(new Vector2(500, 2000)); break; case FrequencyRange.UpperMidrange: return(new Vector2(2000, 4000)); break; case FrequencyRange.High: return(new Vector2(4000, 6000)); break; case FrequencyRange.VeryHigh: return(new Vector2(6000, 20000)); break; case FrequencyRange.Decibal: return(new Vector2(0, 20000)); default: break; } return(Vector2.zero); }
private void chart1_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) { FrequencyRange frequencyRange = new FrequencyRange(graph1LowerFrequency, graph1UpperFrequency); graph1FrequencyRanges.Push(frequencyRange); if (series1BinData != null) { AxisViewChanged(chart1, series1BinData.dataSeries, series1BinData.binArray, ref graph1LowerFrequency, ref graph1UpperFrequency, ref graph1BinFreqInc); } if (series2BinData != null) { AxisViewChanged(chart1, series2BinData.dataSeries, series2BinData.binArray, ref graph1LowerFrequency, ref graph1UpperFrequency, ref graph1BinFreqInc); } }
void ReadSpectrum() { // Get Sound Spectrum as a Fourier Series var rawSpectrum = new float[AppManager.Instance.BinSize]; for (int i = 0; i < AppManager.Instance.BinSize; i++) { rawSpectrum [i] = 0; } AudioSource.GetSpectrumData(rawSpectrum, 0, FFTWindow.BlackmanHarris); while (!(Microphone.GetPosition(null) > 0)) { } var spectrum = new float[AppManager.Instance.NumberOfFrequencies]; Array.Copy(rawSpectrum, spectrum, AppManager.Instance.NumberOfFrequencies); Spectrum = new FrequencyRange(spectrum, 0, AppManager.Instance.MaximumFrequency); }
//return the raw spectrum data i nthe given frequency range. public float[] GetFrequencyData(int audioSourceIndex, FrequencyRange freqRange, bool useAudioFile) { if (!audioSources[audioSourceIndex].mute) // if not muted { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; if (useAudioFile) { freqData = GetAudioData(audioSourceIndex).GetSamples(freqRange, audioTimer); } else { audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); } int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh List <float> validData = new List <float>(); for (int i = n1; i <= n2; i++) { //multiply by volume if we're not using mic input, where volume is set to 0 to avoid feedback issues; if (!usingMic) { freqData[i] = freqData[i] * audioSources[audioSourceIndex].volume; } validData.Add(freqData[i]); } float[] normData = NormalizeArray(validData.ToArray()); return(normData); } Debug.LogWarning("warning: Audio Source: " + audioSourceIndex + " is muted"); return(new float[samplesToTake]); }
public static FrequencyRange GetFrequencyRangeFromFrequency(long frequency) { double lowerFrequency, upperFrequency; lowerFrequency = Math.Floor((double)frequency / 1000000) * 1000000; upperFrequency = (double)frequency / 1000000; if (upperFrequency % 1 == 0) { upperFrequency += 0.5; } upperFrequency = Math.Ceiling(upperFrequency) * 1000000; FrequencyRange frequencyRange = new FrequencyRange(lowerFrequency, upperFrequency); return(frequencyRange); }
private void chart2_AxisViewChanged(object sender, System.Windows.Forms.DataVisualization.Charting.ViewEventArgs e) { FrequencyRange frequencyRange = new FrequencyRange(graph2LowerFrequency, graph2UpperFrequency); graph2FrequencyRanges.Push(frequencyRange); if (series1BinData != null) { AxisViewChanged(chart2, series1BinData.dataSeries, series1BinData.avgBinArray, ref graph2LowerFrequency, ref graph2UpperFrequency, ref graph2BinFreqInc); } if (series2BinData != null) { AxisViewChanged(chart2, series2BinData.dataSeries, series2BinData.avgBinArray, ref graph2LowerFrequency, ref graph2UpperFrequency, ref graph2BinFreqInc); } if (series1BinData != null && series2BinData != null) { GraphDifference(series1BinData, series2BinData); } }
//return the raw spectrum data i nthe given frequency range, using the specified number of bins public float[] GetFrequencyData(int audioSourceIndex, FrequencyRange freqRange, int numBins, bool abs) { if (!audioSources[audioSourceIndex].mute) // if not muted { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x; //Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y; //Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh //Debug.Log("Valid Freq Data: (" + n1 + "-" + n2 + ")/" + samplesToTake); List <float> validData = new List <float>(); for (int i = n1; i <= n2; i++) { float frequency = freqData[i]; if (abs) { frequency = Mathf.Abs(freqData[i]); } validData.Add(frequency * audioSources[audioSourceIndex].volume); } float[] binnedArray = GetBinnedArray(validData.ToArray(), numBins); float[] normalizedArray = NormalizeArray(binnedArray); return(normalizedArray); } Debug.LogWarning("warning: Audio Source: " + audioSourceIndex + " is muted"); return(new float[numBins]); }
public override async Task Initialize() { if (ParentViewModel != null) { IEnumerable <BatCall> batCalls = ParentViewModel.BatCalls; await Task.Run(() => { FreqBins.LoadBins(batCalls); IntensityBins.LoadBins(batCalls); CallDurationBins.LoadBins(batCalls); TimeBins.LoadBins(batCalls); }); FrequencyRange.Set(FreqBins.Range); IntensityRange.Set(IntensityBins.Range); DurationRange.Set(CallDurationBins.Range); TimeRange.Set(TimeBins.Range); Freq = new ObservableCollection <KeyValuePair <DateTime, uint> >(batCalls.Select(c => new KeyValuePair <DateTime, uint>(c.StartTime, c.MaxFrequency))); OnPropertyChanged(nameof(Freq)); OnPropertyChanged(nameof(FilterText)); } }
//return the raw spectrum data i nthe given frequency range, using the specified number of bins public float[] GetFrequencyData(int audioSourceIndex, FrequencyRange freqRange, int numBins, bool abs) { if (!audioSources[audioSourceIndex].mute) // if not muted { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x;//Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y;//Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh //Debug.Log("Valid Freq Data: (" + n1 + "-" + n2 + ")/" + samplesToTake); List<float> validData = new List<float>(); for (int i = n1; i <= n2; i++) { float frequency = freqData[i]; if (abs) { frequency = Mathf.Abs(freqData[i]); } validData.Add(frequency * audioSources[audioSourceIndex].volume); } float[] binnedArray = GetBinnedArray(validData.ToArray(), numBins); float[] normalizedArray = NormalizeArray(binnedArray); return normalizedArray; } Debug.LogWarning("warning: Audio Source: " + audioSourceIndex + " is muted"); return new float[numBins]; }
public RangeViewModel(string title, FrequencyRange value, bool isChecked = false) { Title = title ?? throw new ArgumentNullException(nameof(title)); Value = value; IsChecked = isChecked; }
private void XB200AdjustFilterBank() { if (_dev == IntPtr.Zero) return; if (!_xb200_enabled) return; if (_centerFrequency >= MinFrequency) return; int error = 0; FrequencyRange filter50M_freqs = new FrequencyRange(50000000, 54000000); FrequencyRange filter144M_freqs = new FrequencyRange(149000000, 159000000); FrequencyRange filter222M_freqs = new FrequencyRange(206000000, 235000000); switch (_xb200_filter) { case bladerf_xb200_filter.BLADERF_XB200_AUTO_1DB: filter50M_freqs.Min = 37774405; filter50M_freqs.Max = 59535436; filter144M_freqs.Min = 128326173; filter144M_freqs.Max = 166711171; filter222M_freqs.Min = 187593160; filter222M_freqs.Max = 245346403; break; case bladerf_xb200_filter.BLADERF_XB200_AUTO_3DB: filter50M_freqs.Min = 34782924; filter50M_freqs.Max = 61899260; filter144M_freqs.Min = 121956957; filter144M_freqs.Max = 178444099; filter222M_freqs.Min = 177522675; filter222M_freqs.Max = 260140935; break; } if (filter50M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_50M); } else if (filter144M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_144M); } else if (filter222M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_222M); } else { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_CUSTOM); } if (error != 0) throw new ApplicationException(String.Format("bladerf_xb200_set_filterbank() error. {0}", NativeMethods.bladerf_strerror(error))); }
private void XB200AdjustFilterBank() { if (_dev == IntPtr.Zero) { return; } if (!_xb200_enabled) { return; } if (_centerFrequency >= MinFrequency) { return; } int error = 0; FrequencyRange filter50M_freqs = new FrequencyRange(50000000, 54000000); FrequencyRange filter144M_freqs = new FrequencyRange(149000000, 159000000); FrequencyRange filter222M_freqs = new FrequencyRange(206000000, 235000000); switch (_xb200_filter) { case bladerf_xb200_filter.BLADERF_XB200_AUTO_1DB: filter50M_freqs.Min = 37774405; filter50M_freqs.Max = 59535436; filter144M_freqs.Min = 128326173; filter144M_freqs.Max = 166711171; filter222M_freqs.Min = 187593160; filter222M_freqs.Max = 245346403; break; case bladerf_xb200_filter.BLADERF_XB200_AUTO_3DB: filter50M_freqs.Min = 34782924; filter50M_freqs.Max = 61899260; filter144M_freqs.Min = 121956957; filter144M_freqs.Max = 178444099; filter222M_freqs.Min = 177522675; filter222M_freqs.Max = 260140935; break; } if (filter50M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_50M); } else if (filter144M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_144M); } else if (filter222M_freqs.contains(_centerFrequency)) { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_222M); } else { error = NativeMethods.bladerf_xb200_set_filterbank(_dev, bladerf_module.BLADERF_MODULE_RX, bladerf_xb200_filter.BLADERF_XB200_CUSTOM); } if (error != 0) { throw new ApplicationException(String.Format("bladerf_xb200_set_filterbank() error. {0}", NativeMethods.bladerf_strerror(error))); } }
/// <summary> /// Record samples of a specified FrequencyRange /// </summary> /// <param name="freqRange"></param> /// <param name="data"></param> public void RecordSamples(FrequencyRange freqRange, float[] data) { List <Array> samples = GetSampleArray(freqRange); samples.Add(new Array(data, true)); }
//Get pre-recorded audio samples float[] GetAudioSamplesFromFile(FrequencyRange frequencyRange, int audioSourceIndex) { float loopTime = audioTimer % audioDatas[audioSourceIndex].clipLength; return(audioDatas[audioSourceIndex].GetSamples(frequencyRange, loopTime)); }
public static Vector2 GetFreqForRange(FrequencyRange freqRange) { switch (freqRange) { case FrequencyRange.SubBase: return new Vector2(20, 60); break; case FrequencyRange.Bass: return new Vector2(60, 250); break; case FrequencyRange.LowMidrange: return new Vector2(250, 500); break; case FrequencyRange.Midrange: return new Vector2(500, 2000); break; case FrequencyRange.UpperMidrange: return new Vector2(2000, 4000); break; case FrequencyRange.High: return new Vector2(4000, 6000); break; case FrequencyRange.VeryHigh: return new Vector2(6000, 20000); break; case FrequencyRange.Decibal: return new Vector2(0, 20000); default: break; } return Vector2.zero; }
private void Update() { if (AudioSource.mute && !Input.Use) { return; } if (Input.Use) { if (!AudioSource.isPlaying) { int microphoneSamples = Microphone.GetPosition(Input.CurrentAudioInput); if (microphoneSamples / AudioReactInput.Frequency > Input.Delay) { AudioSource.timeSamples = (int)(microphoneSamples - (Input.Delay * AudioReactInput.Frequency)); AudioSource.Play(); } } audioMultiplier = Input.AudioMultiplier; } else { audioMultiplier = AudioSourceMultiplier; } AudioSource.GetSpectrumData(frequencyData, 0, FFTWindow.BlackmanHarris); AudioSource.GetOutputData(outputData, 0); audioVolume = AudioSource.volume; for (int i = 0; i < FrequencySamples.Length; i++) { Action action; FrequencyRange frequencyRange = (FrequencyRange)i; if (frequencyRange != FrequencyRange.Decibel) { // Get Frequency Volume action = new Action(() => { float[] range = GetFrequencyRange(frequencyRange); float frequencyLow = range[0]; float frequencyHigh = range[1]; int n1 = (int)Mathf.Floor(frequencyLow * samplesAmount / frequencyMax); int n2 = (int)Mathf.Floor(frequencyHigh * samplesAmount / frequencyMax); float sum = 0; for (int j = n1; j <= n2; j++) { if (j < frequencyData.Length && j >= 0) { sum += Mathf.Abs(frequencyData[j]); } } if (Input.Use) { if (sum < Input.VolumeThreshold) { sum = 0; } } else { sum = sum * audioVolume; } lock (FrequencySamples) { FrequencySamples[i] = sum / (n2 - n1 + 1); } }); } else { // get RMS action = new Action(() => { float sum = 0; for (int j = 0; j < outputData.Length; j++) { sum += outputData[j] * outputData[j]; } float rmsValue = Mathf.Sqrt(sum / samplesAmount); if (Input.Use) { if (rmsValue < Input.VolumeThreshold) { rmsValue = 0; } } else { rmsValue = rmsValue * audioVolume; } lock (FrequencySamples) { FrequencySamples[i] = rmsValue; } }); } if (threadPool.MaxThreads > 1) { threadPool.CreateThread(action, "GetFrequencySample[" + i + "]"); } else { action(); } } if (threadPool.MaxThreads > 1) { for (int i = 0; i < FrequencySamples.Length; i++) { threadPool.JoinThread(i); } threadPool.OnUpdate(); } for (int i = 0; i < FrequencySamples.Length; i++) { FrequencySamples[i] *= audioMultiplier; } }
//like GetAvg or GetRMS, but inside a given frequency range public float GetFrequencyVol(int audioSourceIndex, FrequencyRange freqRange) { if (!audioSources[audioSourceIndex].mute) // if not muted { Vector2 range = GetFreqForRange(freqRange); float fLow = range.x;//Mathf.Clamp (range.x, 20, fMax); // limit low... float fHigh = range.y;//Mathf.Clamp (range.y, fLow, fMax); // and high frequencies // get spectrum float[] freqData = new float[samplesToTake]; audioSources[audioSourceIndex].GetSpectrumData(freqData, 0, FFTWindow.BlackmanHarris); int n1 = (int)Mathf.Floor(fLow * samplesToTake / fMax); int n2 = (int)Mathf.Floor(fHigh * samplesToTake / fMax); float sum = 0; // Debug.Log("Smapling freq: " + n1 + "-" + n2); // average the volumes of frequencies fLow to fHigh for (int i = n1; i <= n2; i++) { sum += Mathf.Abs(freqData[i]); } sum = sum * audioSources[audioSourceIndex].volume; return sum / (n2 - n1 + 1); } return 0; }