public LineSpectrum(SpectrumProvider provider, int barCount) { SetFftSize(provider.FftSize); _spectrumProvider = provider; _scalingStrategy = ScalingStrategy.SquareRoot; UpdateFrequencyMapping(barCount); }
public WasapiAudio(WasapiCaptureType captureType, int spectrumSize, ScalingStrategy scalingStrategy, int minFrequency, int maxFrequency, WasapiAudioFilter[] filters, Action <float[]> receiveAudio) { _captureType = captureType; _spectrumSize = spectrumSize; _scalingStrategy = scalingStrategy; _minFrequency = minFrequency; _maxFrequency = maxFrequency; _filters = filters; _receiveAudio = receiveAudio; }
public LineSpectrum GetLineSpectrum(int barCount, ScalingStrategy scalingStrategy) { if (_spectrumProvider == null) { return(null); } return(new LineSpectrum(FftSize) { SpectrumProvider = _spectrumProvider, UseAverage = true, BarCount = barCount, IsXLogScale = true, ScalingStrategy = scalingStrategy }); }
private void CheckCaptureParams() { bool freqChanged = newMinFreq != minFreq || newMaxFreq != maxFreq; bool scalingChanged = labelToScaling[scalingModeSelection.Selected] != scalingMode; bool deviceChanged = labelToCapture[captureModeSelection.Selected] != captureMode; bool smoothingChanged = newSmoothing != smoothingIterations; if (freqChanged) { minFreq = newMinFreq; maxFreq = newMaxFreq; } if (scalingChanged) { scalingMode = labelToScaling[scalingModeSelection.Selected]; } if (deviceChanged) { captureMode = labelToCapture[captureModeSelection.Selected]; } if (freqChanged || scalingChanged || deviceChanged) { if (wasapiAudio != null && capturing) { wasapiAudio.StopListen(); } InitializeWasapiCapture(); if (capturing) { wasapiAudio.StartListen(); } } if (smoothingChanged) { smoothingIterations = newSmoothing; InitializeSmoother(); } }
/// <summary> /// Called after the <see cref="ScalingStrategy"/> value has changed. /// </summary> /// <param name="oldValue">The previous value of <see cref="ScalingStrategy"/></param> /// <param name="newValue">The new value of <see cref="ScalingStrategy"/></param> protected virtual void OnScalingStrategyChanged(ScalingStrategy oldValue, ScalingStrategy newValue) { }
/// <summary> /// Coerces the value of <see cref="ScalingStrategy"/> when a new value is applied. /// </summary> /// <param name="value">The value that was set on <see cref="ScalingStrategy"/></param> /// <returns>The adjusted value of <see cref="ScalingStrategy"/></returns> protected virtual ScalingStrategy OnCoerceScalingStrategy(ScalingStrategy value) { return(value); }
private void UpdateSpectrum(double maxValue, float[] fftBuffer) { ScalingStrategy spectrumScalingStrategy = ScalingStrategy.Decibel; Dispatcher.Invoke((Action) delegate { spectrumScalingStrategy = SpectrumScalingStrategy; }); var dataPoints = new List <double>(); double value0 = 0, value = 0; double lastValue = 0; double actualMaxValue = maxValue; int spectrumPointIndex = 0; for (int i = _minimumFrequencyIndex; i <= _maximumFrequencyIndex; i++) { switch (spectrumScalingStrategy) { case ScalingStrategy.Decibel: value0 = (((20 * Math.Log10(fftBuffer[i])) - MinDbValue) / DbScale) * actualMaxValue; break; case ScalingStrategy.Linear: value0 = (fftBuffer[i] * ScaleFactorLinear) * actualMaxValue; break; case ScalingStrategy.Sqrt: value0 = ((Math.Sqrt(fftBuffer[i])) * ScaleFactorSqr) * actualMaxValue; break; } bool recalc = true; value = Math.Max(0, Math.Max(value0, value)); while (spectrumPointIndex <= _spectrumIndexMax.Length - 1 && i == _spectrumIndexMax[spectrumPointIndex]) { if (!recalc) { value = lastValue; } if (value > maxValue) { value = maxValue; } /*TODO: Add Average property to the spectrum analyzer*/ if (/*_useAverage*/ true && spectrumPointIndex > 0) { value = (lastValue + value) / 2.0; } dataPoints.Add(value); lastValue = value; value = 0.0; spectrumPointIndex++; recalc = false; } } Dispatcher.Invoke((Action) delegate { var width = this.RenderSize.Width; var height = this.RenderSize.Height; var barSpacing = width / FrequencyBarCount; var barWidth = barSpacing - FrequencyBarSpacing; // freeze brushes var borderBrush = FrequencyBarBorderBrush.Clone(); var barBrush = FrequencyBarBrush.Clone(); borderBrush.Freeze(); barBrush.Freeze(); if (barWidth < .5) { barWidth = .5; } for (var i = 0; i < FrequencyBarCount; i++) { //var b = new Border(); var b = bars[i]; b.Width = barWidth; b.BorderBrush = borderBrush; b.CornerRadius = FrequencyBarCornerRadius; b.BorderThickness = FrequencyBarBorderThickness; b.Height = (dataPoints[i] / 100) * height; b.HorizontalAlignment = HorizontalAlignment.Left; b.VerticalAlignment = VerticalAlignment.Bottom; b.Margin = new Thickness(i * barSpacing, 0, 0, 0); b.Background = barBrush; } }, DispatcherPriority.ApplicationIdle); }
public RealtimeAudio(int spectrumSize, ScalingStrategy scalingStrategy, Action <float[]> receiveAudio) { _spectrumSize = spectrumSize; _scalingStrategy = scalingStrategy; _receiveAudio = receiveAudio; }