Audio input FFT example

Here’s a basic Fast Fourier Transform implementation that’s kind of similar to the fmod one in the soundPlayer except that you can use it on incoming audio among other things.

I’ve made a dirty C++ wrapper for Dominic Mazzoni’s FFT code that is used in Audacity among other things. I believe this code is GPL.

As there are a bunch of general FFT related functions (including inverse FFT) this class could be used for all sorts of things…but in the example it just operates on incoming audio to create a spectrogram.

All you need to do is compile the application and send some audio into the record input of your default sound card. So a microphone is needed.

download the code (based on V0.02):


Apologies for resurecting this thread. I was looking for an fft implementation and came across this. In the post is states the assumption of a GPL license. In the same search I also found this (…-aspx?s=mp3) which seems to be based on the same code.

According to the comments at the top (which you can take with as large a pinch of salt as you see necessary), the user received a message from the original author relicensing the code under LGPL.

Hope this information proves useful & legal to others.


Interesting…I guess you should be able then to replace this file with the one I bundled in my example…if they are identical, and change the licensing to LGPL.

I have problems to run this code in Debug Version, on Release works fine. It looks like that the problem is on ofSoundStreamSetup function. Any clues?

Is it exactly the same code? What platform are you running on?
Which compiler are you using?

I am getting an STATUS_STACK_BUFFER_OVERRUN. It occours only at debug mode at Visual Studio at ofSoundStreamSetup. Any clues?

Found it! For some reason I was using rtAudio.lib instead of rtAudioD.lib at the debug version. Now works fine! Thanks!!

In case anyone gets the warning

deprecated conversion from string constant to ‘char*’

change the return type of WindowFuncName method from char* to const char*

I have ckecked your useful example, but I found it only uses left channel to draw the spectrum.

Would it be possible to draw a FFT using both channels in the powerSpectrum function call?

Thank you. :slight_smile:

Sure, of course. I was just being lazy and wanted to draw just one input.
But if you are using stereo you can use both. Just call the same functions on the other channel.

Thank you grimus!!!

However, I was thinking about how to order the samples from both channels to get a complex FFT. In the RealFFT function, says:

* Real Fast Fourier Transform
* This function was based on the code in Numerical Recipes in C.
* In Num. Rec., the inner loop is based on a single 1-based array
* of interleaved real and imaginary numbers. Because we have two
* separate zero-based arrays, our indices are quite different.
* Here is the correspondence between Num. Rec. indices and our indices:
* i1 <-> real[i]
* i2 <-> imag[i]
* i3 <-> real[n/2-i]
* i4 <-> imag[n/2-i]

which I don’t know how to pass the array composed by L and R channels to the function.

Thank you.

Already did it!!
Just using as input data array a new array composed alternating left and right channel samples, and using the FFT function (complex FFT).

The only thing is to be careful about the window size and number of samples.

Awesome fft wrapper!! :wink:

I’was looking for that kind of addon, thx…

i didn’t try your lib but i’have a question after a quick look :
what’s the freqphase array ?



First of all, It seems to be a great code exactly what i looking for. But,

If i copy the code to my compiler i get a lot of “undeclared identifier” issues. Is this expected or do I have to manually changes al these issues? If so, do you have any tips? I’m quite novice to programming. I have read some books and did some basic coding (from the book creative coding in OF), but a large code like this is a quite a different cup of thea. I use Xcode as compiler btw.

Some examples of issues:
ofSetupOpenGL(1024,768, OF_FULLSCREEN);

ofSoundStreamSetup(0,2,this, 44100,BUFFER_SIZE, 4);
left = new float[BUFFER_SIZE];
right = new float[BUFFER_SIZE];

for (int i = 0; i < NUM_WINDOWS; i++)
	for (int j = 0; j < BUFFER_SIZE/2; j++)
		freq[i][j] = 0;	

Thanks in advance.