Search code examples
c++fftspectrumbass

Drawing audio spectrum with Bass library


How can I draw an spectrum for an given audio file with Bass library?

I mean the chart similar to what Audacity generates: enter image description here

I know that I can get the FFT data for given time t (when I play the audio) with:

float fft[1024];
BASS_ChannelGetData(chan, fft, BASS_DATA_FFT2048); // get the FFT data

That way I get 1024 values in array for each time t. Am I right that the values in that array are signal amplitudes (dB)? If so, how the frequency (Hz) is associated with those values? By the index?

I am an programmer, but I am not experienced with audio processing at all. So I don't know what to do, with the data I have, to plot the needed spectrum.

I am working with C++ version, but examples in other languages are just fine (I can convert them).


Solution

  • From the documentation, that flag will cause the FFT magnitude to be computed, and from the sounds of it, it is the linear magnitude.

    dB = 10 * log10(intensity);
    dB = 20 * log10(pressure);
    

    (I'm not sure whether audio file samples are a measurement of intensity or pressure. What's a microphone output linearly related to?)

    Also, it indicates the length of the input and the length of the FFT match, but half the FFT (corresponding to negative frequencies) is discarded. Therefore the highest FFT frequency will be one-half the sampling frequency. This occurs at N/2. The docs actually say

    For example, with a 2048 sample FFT, there will be 1024 floating-point values returned. If the BASS_DATA_FIXED flag is used, then the FFT values will be in 8.24 fixed-point form rather than floating-point. Each value, or "bin", ranges from 0 to 1 (can actually go higher if the sample data is floating-point and not clipped). The 1st bin contains the DC component, the 2nd contains the amplitude at 1/2048 of the channel's sample rate, followed by the amplitude at 2/2048, 3/2048, etc.

    That seems pretty clear.