0

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

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 1
    Have you taken a look at this doc link on their site? It might have what you need in the remarks section. http://www.un4seen.com/doc/#bass/BASS_ChannelGetData.html – E. Moffat Mar 17 '15 at 21:54
  • @E.Moffat Yes, but somehow I cannot find a way to go further. Maybe it's because of my lack of knowledge in audio processing problems :/ – PolGraphic Mar 17 '15 at 22:06

1 Answers1

2

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Thank you man :) About the question - it's the .wav file loaded and played with the human voice. One silly question: should I compute the `db` value for each of `fft[1024]` values to plot my chart? Wouldn't it produce the series of charts (one for each time `t` - each `update()` the calculated `fft` array is different if I do not pause the sound) instead of one chart I want to get for some period of time (Audacity "Frequency Analysis")? – PolGraphic Mar 17 '15 at 22:37
  • 1
    @PolGraphic: I'm not familiar enough with Audacity to know exactly what it is doing... from your screenshot it appears to be using a fixed-size FFT and averaging that over the time window you select. Another option is to use an FFT corresponding to the time window. A third option is to use a waterfall chart -- axes are time and frequency, and the power density is shown using color. – Ben Voigt Mar 17 '15 at 22:43
  • so with some simplification I could say that Audacity draws an "average" (calculated in one way or another) chart from the series of `db/Hz` charts (one chart for each time `t`)? – PolGraphic Mar 17 '15 at 22:50
  • Yes, but the averaging has to be done before conversion to dB. – Ben Voigt Mar 17 '15 at 22:52