使用 Bass 库绘制音频频谱

Drawing audio spectrum with Bass library

如何使用Bass library绘制给定音频文件的频谱?

我的意思是类似于 Audacity 生成的图表:

我知道我可以获得给定时间 t(当我播放音频时)的 FFT 数据:

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

这样我每次都能在数组中得到 1024 个值 t。该数组中的值是信号幅度 (dB),我说得对吗?如果是这样,频率 (Hz) 如何与这些值相关联?按索引?

我是一名程序员,但我对音频处理完全没有经验。所以我不知道如何利用我拥有的数据来绘制所需的光谱。

我正在使用 C++ 版本,但其他语言的示例也很好(我可以转换它们)。

根据文档,该标志将导致计算 FFT 幅度,从它的声音来看,它是线性幅度。

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

(我不确定音频文件样本是测量强度还是压力。麦克风输出与什么线性相关?)

另外,它表示输入的长度和FFT的长度匹配,但是丢弃了一半的FFT(对应负频率)。因此最高 FFT 频率将是采样频率的二分之一。这发生在 N/2。文档实际上说

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.

看起来很清楚。