# SPECTRUM ANALYSIS

Introduction

This short section reveals how easy it is to obtain the realtime spectrum graph for FMOD's DSP output as you hear it, for graphical display or even advanced signal processing!

Using it

The spectrum analysis unit is an FMOD System DSP Unit. If you are unsure of the FMOD DSP system then see the DSP tutorial.
By default it is turned off because it can consume a reasonable amount of cpu time and it doesn't want to be doing Fast Fourier Transform code when the spectrum isn't even being queried!
To turn it on is quite simple. Just call.
FSOUND_DSP_SetActive(FSOUND_DSP_GetFFTUnit, TRUE);
To turn it off again is just as simple. Just call.
FSOUND_DSP_SetActive(FSOUND_DSP_GetFFTUnit(), FALSE);
When your audio is playing, this DSP unit will every 25ms, or the rate of the DSP engine, update a snapshot of the current audible spectrum.
The range for this FFT produces a spectrum array between 1hz and Nyquist, or in other words, 1hz and the sample rate given to FSOUND_Init divided by 2.
If you had a 44khz output it would produce results up to 22khz.
To get the spectrum, use FSOUND_DSP_GetSpectrum. As the documentation for this function says, it returns a pointer to an array of 512 floating point values between 0.0 and 1.0.
Simply plot these values, scaling the 0 to 1 value to the height of your spectrum display, or process them for things like beat detection etc. Values above 11khz (when initialized with 44khz) become very small and are some times graphically not very interesting. The FMOD media player only plots the first 256 entries because of this. It is up to you how you interpret the data.
If you want a different size window beside 512 then simply interpolate, disregard or skip entries and average them to your desired range. Different window sizes for the current FFT are not planned for future releases of FMOD.
NOTE: You can change the position of the system FFT DSP unit. It is currently positioned last in the chain of DSP units by default so that the spectrum results in feedback from everything played through the FMOD software engine. See FSOUND_DSP_PRIORITIES for the relative system DSP unit priorities. You could move it to before the music unit and after the SFX unit for example, which would exclude mods from having its input into the spectrum graph. It would be achieved like this
FSOUND_DSP_SetPriority(FSOUND_DSP_GetFFTUnit(), 150);

What happens if I want to get a Spectrum but use FSOUND_FX? Aren't the 2 incompatible?.

It is possible but with some limitations, See this section for more information