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

Pét*_*son 5 macos cocoa openal objective-c core-audio

我正在寻找一种方法来接入Mac上的当前音频输出,然后返回表示当前声级的值.

声级是指输出产生的噪音量.我不是问如何获得输出设备的当前音量级别.

joh*_*doe 17

以下代码是从Apple的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)];
}
Run Code Online (Sandbox Code Playgroud)