您可以使用 AVRecorder 或更低的版本,例如实时 IO 音频单元。
“体积”的概念非常模糊。您可能想查看计算峰值和 RMS 值之间的差异,并了解如何在给定时间(比如 300 毫秒,这是 VU 表使用的)内积分 RMS 值。
基本上,您将所有值的平方相加。您将取平方根并使用 10 * log10f(sqrt(sum/num_samples)) 转换为 dBFS,但您可以使用 20 * log10f(sum/num_samples) 一步完成,而无需 sqrt。
您需要对集成时间和阈值进行大量调整,以使其以您想要的方式运行。
对于音高转换,我认为 OpenAL 可以解决问题,它背后的技术称为带限插值 - https://ccrma.stanford.edu/~jos/resample/Theory_Ideal_Bandlimited_Interpolation.html
此示例将 rms 计算显示为运行平均值。循环缓冲区维护平方的历史,并且消除了每次操作求平方和的需要。我还没有运行它,所以把它当作伪代码;)
例子:
class VUMeter
{
protected:
// samples per second
float _sampleRate;
// the integration time in seconds (vu meter is 300ms)
float _integrationTime;
// these maintain a circular buffer which contains
// the 'squares' of the audio samples
int _integrationBufferLength;
float *_integrationBuffer;
float *_integrationBufferEnd;
float *_cursor;
// this is a sort of accumulator to make a running
// average more efficient
float _sum;
public:
VUMeter()
: _sampleRate(48000.0f)
, _integrationTime(0.3f)
, _sum(0.)
{
// create a buffer of values to be integrated
// e.g 300ms @ 48khz is 14400 samples
_integrationBufferLength = (int) (_integrationTime * _sampleRate);
_integrationBuffer = new float[_integrationBufferLength + 1];
bzero(_integrationBuffer, _integrationBufferLength);
// set the pointers for our ciruclar buffer
_integrationBufferEnd = _integrationBuffer + _integrationBufferLength;
_cursor = _integrationBuffer;
}
~VUMeter()
{
delete _integrationBuffer;
}
float getRms(float *audio, int samples)
{
// process the samples
// this part accumulates the 'squares'
for (int i = 0; i < samples; ++i)
{
// get the input sample
float s = audio[i];
// remove the oldest value from the sum
_sum -= *_cursor;
// calculate the square and write it into the buffer
double square = s * s;
*_cursor = square;
// add it to the sum
_sum += square;
// increment the buffer cursor and wrap
++_cursor;
if (_cursor == _integrationBufferEnd)
_cursor = _integrationBuffer;
}
// now calculate the 'root mean' value in db
return 20 * log10f(_sum / _integrationBufferLength);
}
};