This project has moved. For the latest updates, please go here.

FFT lags on BeginInvoke

Apr 9, 2014 at 11:49 AM
Edited Apr 9, 2014 at 12:06 PM
I've got WPF project, main window contains calss that contains naudio "player", also im using SampleAggregator to get FFT. So thats actually all stuff. Now, when fft "ready" event fires, it goes to parent class then by next event goes to main window (so i can draw something like spectrograph), but I cant update controls directly because blah-blah-blah different thread, ok, im trying to use invoke and here goes super-duper-thing ( or im totally dumb ... this can be the reason too actually )).
public void a_FFTCalculated(object sender, FftEventArgs e)
            FFTPoly.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
                * draw our super-duper-spectrograph *
public void a_FFTCalculated(object sender, FftEventArgs e)
            FFTPoly.Dispatcher.BeginInvoke (DispatcherPriority.Render, new Action(() =>
                * draw our super-duper-spectrograph *
Almost same, except async way, but using Invoke - everything will be fine, but if im trying to use BeginInvoke some strange ( like stretched fft ) data appears about every second.

I just cant understand how anyway this could happen :)

Thanks anyway :)
Apr 10, 2014 at 7:27 AM
Ok, glad to see so many responses :)

So, ill make lil test app here

Mabye im doing something wrong or in some terrible way so the whole world could collapse?
All i need is to have fully independent ( async ) spectrograph ...

Well, thanks for watching, thats all folks :)
Apr 10, 2014 at 8:47 AM
One of the things I try to do in NAudio is avoid creating too much work for the garbage collector. So the FFT buffer that is provided in the event is reused for the next one, meaning it gets cleared out after the event handler completes. Probably SampleAggregator should be updated to have at least two buffers that it alternates between. It's part of the reason why SampleAggregator is not actually part of the NAudio library yet.
Apr 10, 2014 at 9:08 AM
Thanks for Your answer :)

You are absolutely right :)
changing to ( dirty way :) )
                    FastFourierTransform.FFT(true, m, fftBuffer);
                    FftCalculated(this, new FftEventArgs(fftBuffer));
                    fftPos = 0;
                    fftBuffer = new Complex[fftLength];
And all works just fine :)

Thanks again!