# FFTW - Realtime music visualization

This topic is 3194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 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 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 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 on other sites
Sorry, I want to use the FFTW library, because of its speed.
It seems that audacity does not use it :(

##### 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 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 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).

1. 1
2. 2
Rutin
29
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633647
• Total Posts
3013108
×