I am working on a C # 4.0 / WPF real-time spectrum analyzer (as the basis of another project). I use the latest version of NAudio to receive real-time audio data on a sound card and WPFSoundVisualizationLib (http://wpfsvl.codeplex.com/) for the WPF spectrum analyzer. With these wonderful tools, the work is almost complete, but it does not work correctly: - (
I have a functional Spectrum, but the information is not right, and I donβt understand where this problem comes from ... (I compared my Spectrum with Equalify, Spectrum / Equalizer for Spotify, and I do not have the same behavior)
This is my main class:
using System; using System.Windows; using WPFSoundVisualizationLib; namespace MySpectrumAnalyser { public partial class MainWindow : Window { private RealTimePlayback _playback; private bool _record; public MainWindow() { InitializeComponent(); this.Topmost = true; this.Closing += MainWindow_Closing; this.spectrum.FFTComplexity = FFTDataSize.FFT2048; this.spectrum.RefreshInterval = 60; } private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (this._record) { this._playback.Stop(); } } private void Button_Click_1(object sender, RoutedEventArgs e) { if (this._playback == null) { this._playback = new RealTimePlayback(); this.spectrum.RegisterSoundPlayer(this._playback); } if (!this._record) { this._playback.Start(); this.Dispatcher.Invoke(new Action(delegate { this.btnRecord.Content = "Stop"; })); } else { this._playback.Stop(); this.Dispatcher.Invoke(new Action(delegate { this.btnRecord.Content = "Start"; })); } this._record = !this._record; } } }
And my loopback analyzer (which implements ISpectrumPlayer for use with the WPFSoundVisualizationLib Spectrum control).
LoopbackCapture inherits NAudio.CoreAudioApi.WasapiCapture.
The data received from Wasapi is a byte array (32 bits PCM, 44.1 kHz, 2 channels, 32 bits per sample)
using NAudio.Dsp; using NAudio.Wave; using System; using WPFSoundVisualizationLib; namespace MySpectrumAnalyser { public class RealTimePlayback : ISpectrumPlayer { private LoopbackCapture _capture; private object _lock; private int _fftPos; private int _fftLength; private Complex[] _fftBuffer; private float[] _lastFftBuffer; private bool _fftBufferAvailable; private int _m; public RealTimePlayback() { this._lock = new object(); this._capture = new LoopbackCapture(); this._capture.DataAvailable += this.DataAvailable; this._m = (int)Math.Log(this._fftLength, 2.0); this._fftLength = 2048;
GetFFTData is called by the WPF control every 60 ms to update Spectrum.
Floyd source share