# Graphics in responce to music

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

## Recommended Posts

Hi, I want some graphics in my game to respond to selected music bands. For example every time the song plays a low beat something would happen on the screen. Something similar to Windows Media Player but much simpler. Does anyone have any suggestions on the easiest way I can do this? If some non directx/directaudio way exists, i would like to know of it also, Thanks.

##### Share on other sites
Is this really such a mysterious topic that no one has a clue how to go about it? I just need a place to start, so any suggestions are welcomed.

##### Share on other sites
Just blue-sky thinking here.

1. Audacity can give you a frequency analysis for a wave file. I believe you can pick windows (time-domains) for the frequency analysis. Look for a peak in the frequency of interest, note the time from the start of the file. In your app, when the sound is played, periodically (every 50ms or so?) check the play position and take the desired action at the right time.

2. There are fft routines (fast-fourier transform) around the web. If you're not familiar with them, you may have to play around to get the desired frequency spectrum. Anyway, load the wave file, do an analysis for desired frequencies and magnitudes, again noting the offset of the desired frequency peak or pattern from the beginning of the file. Check the play position.. etc. (similar to #1 above)

3. I don't think you can read soundbuffers but you can synchronize the play position with a position in a memory copy of the data and do a fft on the fly. If the fft shows the frequency pattern you want, take the desired action.

##### Share on other sites
I just read most of the stuff I could find on FFT and I sort of get the principle (im not catching on to the math part though): A number of simpler sine waves constitute the original sound wave. So the DFT finds those other simpler waves for the bands that I specify and by measuring the amplitude of the simpler waves I am able to see how loud a certain band is playing. I gave up on trying to write the algorithm myself since I only bits and pieces of math thats going on, so I decided to use code already available. What I can't figure out is what output the FFT function is supposed to produce. Most functions that I found look something like this

void ComputeFFT(float data[], unsigned long number_of_samples, unsigned int sample_rate);

Lets say I have the "data" and I'm sampling at "1024", what should the result of this functions be? Also if anyone knows any library that can do this stuff for me I'd greatly appreciate it.

##### Share on other sites
I wouldn't even begin to think about writing the algorithm myself! [smile]

There are several libraries available. I use FFTW (www.fftw.org). Unfortunately, none that I know of are simple at all. But given something like:

ComputeFFT(data[], 1024, 11000)

I would expect that the function would do an fft on the first 1024 data points and output a table of frequency vs. amplitude (or possibly wavelength vs. amplitude). The sample_rate, if I remember correctly, is just the sample frequency of the data - how many points per seconds.

The output table itself is usually pretty simple:

24 hz, amplitude at 24hz
48 hz, amplitude at 48hz
etc.

For your application, you don't need to worry about the complex forms of the functions. The complex form that I use gives me the phase for each frequency, but I don't believe that's pertinent to what you want to do.

If you're going to mess with it, at first, feed it some data you know the answer to - like a table of one or two sine waves so the result table can be easily interpreted.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 10
• ### Forum Statistics

• Total Topics
634088
• Total Posts
3015419
×