Sign in to follow this  
WuTz

FFTW - Realtime music visualization

Recommended Posts

Hello! I've serached the Web for some samples, which show how to visualize Music with FFTW. I need to get the Sound Spectrum. Any Ideas/samples?

Share this post


Link to post
Share on other sites
* you need access to the PCM buffers that are played back
* you then run a Fast Fourier Transform to transform from a time domain to a frequency domain

The more samples you transform at a time (a power of two), the higher the resolution of the spectrum. The highest detectable frequency is half the sample frequency used.

Example:

sample rate = 48000Hz (=48 samples per millisecond/channel)
max frequency = 24000Hz

If you now use an FFT with 512 samples, you receive 256 useful complex numbers that describe the frequency range 1-24000Hz every 10.666667 milliseconds.

Share this post


Link to post
Share on other sites
Thanks, I think this could be usefull, but I
meant CodeSamples (C++) with samples. :)

I've used google, but I havn't found something :(

Share this post


Link to post
Share on other sites
You asked for ideas/samples, and I gave you ideas ;)

What did you type in the Google search box?

You can download the sourcecode from Audacity (an open source audio editor) and see how they do their FFT (they have a special version that generates a power spectrum straight away last time I checked).

But looking for FFT source code should yield good results also (I tried).

Share this post


Link to post
Share on other sites
I searched for "Music visualization with FFTW C++ Sample". I've heard, that the FFTW lib should be very fast, and I want to use some Effects, triggered by the music in my game.

I'll take a look at Audacity.

EDIT:

Audacity does not shows how to calculate the Spectrum (Or I didn't find it).
I want those Bars, which you can see in winamp, for example.


# #
## # # #
## # # # # #
### ## # ## ## ## #
######## ### #######
#################### <---- This :)

Share this post


Link to post
Share on other sites
Audacity does contain code that generates a power spectrum. Did you look in the source code or within the Audacity tool itself? In the code: FFT is the keyword. In the took you should look for frequency- or spectrum analysis.

Note that when you're decoding an mp2/mp3 or any encoded file that uses a similar lossy tecnique, the subband information [what you show with your ASCII art] is there for grabs. It's generated as part of the decoding process.

Share this post


Link to post
Share on other sites
Ah, I see - you already have found a library that can do fourier transforms for you (FFTW). Here's the deal:

* Convert samples to complex numbers (this sounds hard, but simply means you put the sample in the real part and zero in the imaginary part)
* Perform a fourier transform (see my earlier post for tips about the size you should choose)
* you will receive a series of complex numbers (the same amount you put in)
* convert these to a power spectrum, by doing sum(r*r + i*i) for each of the subbands you're interested in. (Note that only half of them are useful due to Nyquist frequency.)

Share this post


Link to post
Share on other sites
Ah, thanks! :)

I think with that information, I can do it without any sample code.
Its better so, anyways.

One more question:

What do you mean with samples? How can I get them Out of the Raw
Audio-Data? I am using XAudio2 for playback. Maybe I'll find the sollution
in the Documentation.

Greets [WuTz]!

Share this post


Link to post
Share on other sites
Audio data consist out of samples. They come in many flavors, so let me assume stereo interleaved 16 bit PCM in your case.

An audio stream would look like this

signed 16 bit sample left, signed 16 bit sample right (this is repeated 48000 times for one second of audio if it was sampled @ 48kHz).

In that case you could create a mono complex number like this:

complex.real = (audio.left + audio.right) * (1.0 / 65536); // normalized to -1.0 ~ +1.0
complex.imaginary = 0.0;

(but you can use other heuristics if you like - it would change the scale of the power spectrum you end up with).

Share this post


Link to post
Share on other sites
Hello again!

Sorry, I had no time for doing that, and then I forgot about it. :(
Now I've tried, but I got stuck very early.

How to get the playing audio?

complex.real = (audio.left + audio.right) * (1.0 / 65536);

what do you mean with "audio.left" or right? Do I have to use some
Get***() functions, or can I get them with the current playbackpos and my audiobuffer, which contains the whole song?

Share this post


Link to post
Share on other sites
How do you access your audio data? I'm sure you have some sort of signed 16 bit PCM buffer with samples. Stereo samples are often interleaved, so in that case:


static const int FFT_SIZE = <some power of two>;

short *pStereo16bSigned = <retrieve a pointer to the PCM data>;

struct
{
float I, R;
} fftData[FFT_SIZE];

for (int i = 0; i < FFT_SIZE; ++i)
{
int audioLeft = (int)pStereo16bSigned[i * 2];
int audioRight = (int)pStereo16bSigned[i * 2 + 1];

fftData[i].I = 0.0f;
fftData[i].R = (audioLeft + audioRight) * (1.0f / 65536.0f);
}

// (perform a fast fourier transform)

float powerSpectrum[FFT_SIZE / 2];

for (int i = 0; i < FFT_SIZE / 2; ++i)
{
// this code assumes the FFT input was replaced by the output
powerSpectrum[i] = fftData[i].I * fftData[i].I + fftData[i].R * fftData[i].R;
}


I don't use the API's you're working with, but this is the basic idea. Dig into the API documentation to find out specifics.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this