How can I get the current sound level of the current sound output device?

I am looking for a way to use the current audio output on a Mac, and then return a value representing the current sound level.

By sound level, I mean the amount of noise generated by the output. I DO NOT ask how to get the current volume level of the output device.

+5
source share
1 answer

The following code is pulled from an Apple SampleAVRecorder ... this particular bit of code gets the connection set from this method of the movieFileOutput class methods, gets the AVCaptureAudioChannel for each connection, and calculates the decibel power based on this. I would suggest that if you are looking for an output noise level, you can get similar information. if you are looking for something lower than this, try the Hardware Abstraction Layer (HAL) structure.

- (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)];
}
+17
source

All Articles