有人可以解释此代码如何使用 Accelerate Framework 将音量转换为分贝吗?

Can someone explain how this code converts volume to decibels using the Accelerate Framework?

我正在使用 EZAudio 构建一个 iOS 应用程序。它委托 returns 返回一个 float** 缓冲区,其中包含指示检测到的体积的浮点值。这个委托不断被调用,它的工作是在不同的线程中完成的。

What I am trying to do is to take the float value from EZAudio and convert it into decibels.


EZAudioDelegate

这是我的简化 EZAudio Delegate for getting Microphone Data:

- (void)microphone:(EZMicrophone *)microphone hasAudioReceived:(float **)buffer withBufferSize:(UInt32)bufferSize withNumberOfChannels:(UInt32)numberOfChannels {
    /*
     *  Returns a float array called buffer that contains the stereo signal data
     *  buffer[0] is the left audio channel
     *  buffer[1] is the right audio channel
     */

    // Using a separate audio thread to not block the main UI thread
    dispatch_async(dispatch_get_main_queue(), ^{

        float decibels = [self getDecibelsFromVolume:buffer withBufferSize:bufferSize];

        NSLog(@"Decibels: %f", decibels);

    });

}

问题

The problem is that after implementing solutions from the links below, I do not understand how it works. If someone could explain how it converts volume to decibels I would be very grateful


代码

该解决方案使用Accelerate Framework中的以下方法将音量转换为分贝:

下面是从 EZAudio Delegate 调用的方法 getDecibelsFromVolume。它从委托传递 float** bufferbufferSize

- (float)getDecibelsFromVolume:(float**)buffer withBufferSize:(UInt32)bufferSize {

    // Decibel Calculation.

    float one = 1.0;
    float meanVal = 0.0;
    float tiny = 0.1;
    float lastdbValue = 0.0;

    vDSP_vsq(buffer[0], 1, buffer[0], 1, bufferSize);

    vDSP_meanv(buffer[0], 1, &meanVal, bufferSize);

    vDSP_vdbcon(&meanVal, 1, &one, &meanVal, 1, 1, 0);


    // Exponential moving average to dB level to only get continous sounds.

    float currentdb = 1.0 - (fabs(meanVal) / 100);

    if (lastdbValue == INFINITY || lastdbValue == -INFINITY || isnan(lastdbValue)) {
        lastdbValue = 0.0;
    }

    float dbValue = ((1.0 - tiny) * lastdbValue) + tiny * currentdb;

    lastdbValue = dbValue;

    return dbValue;
}

我将解释如何使用代码计算信号的 dB 值,然后展示它与 vDSP 示例的关系。

首先,计算一块数据的 RMS 总和

double sumSquared = 0;
for (int i = 0 ; i < numSamples ; i++)
{
   sumSquared += samples[i]*samples[i];
}
double rms = sumSquared/numSamples;

有关RMS

的更多信息

接下来将 RMS 值转换为 dB

double dBvalue = 20*log10(rms);

这与示例代码有什么关系

vDSP_vsq(buffer[0], 1, buffer[0], 1, bufferSize);

此行遍历缓冲区并计算缓冲区中所有元素的平方。如果缓冲区在调用之前包含值 [1,2,3,4],那么在调用之后它将包含值 [1,4,9,16]

vDSP_meanv(buffer[0], 1, &meanVal, bufferSize);

此行遍历缓冲区,对缓冲区中的值求和,然后返回总和除以元素数。因此对于输入缓冲区 [1,4,9,16] 计算总和 30,除以 4 和 returns 结果 7.5.

vDSP_vdbcon(&meanVal, 1, &one, &meanVal, 1, 1, 0);

此行将 meanVal 转换为分贝。在这里调用矢量化函数真的没有意义,因为它只对单个元素进行操作。然而,它正在做的是将参数代入以下公式:

meanVal = n*log10(meanVal/one)

其中 n1020 取决于最后一个参数。在本例中为 1010 用于功率测量,20 用于幅度。我认为 20 更适合您使用。

最后一小段代码看起来是在对结果进行一些简单的平滑处理,以降低仪表的弹性。