如何获得当前音频输出设备的当前声级?

时间:2012-08-09 23:44:11

标签: objective-c macos cocoa core-audio openal

我正在寻找一种方法来点击Mac上的当前音频输出,然后返回一个代表当前声级的值。

声级是指输出产生的噪音量。我询问如何获取输出设备的当前音量级别。

1 个答案:

答案 0 :(得分:17)

Apple's Sample AVRecorder中提取以下代码...此特定位代码从此类的movieFileOutput的连接方法获取一组连接,获取每个连接的AVCaptureAudioChannel,并根据该连接计算分贝能力。我认为如果您正在寻找输出“噪音水平”,您将能够捕获类似的信息。如果您正在寻找比这更低级别的东西,请尝试HAL(硬件抽象层)框架。

- (void)updateAudioLevels:(NSTimer *)timer
{
    NSInteger channelCount = 0;
    float decibels = 0.f;

    // Sum all of the average power levels and divide by the number of channels
    for (AVCaptureConnection *connection in [[self movieFileOutput] connections]) {
        for (AVCaptureAudioChannel *audioChannel in [connection audioChannels]) {
            decibels += [audioChannel averagePowerLevel];
            channelCount += 1;
        }
    }

    decibels /= channelCount;

    [[self audioLevelMeter] setFloatValue:(pow(10.f, 0.05f * decibels) * 20.0f)];
}
相关问题