Trying to understand AVAudioPlayer and measuring sound level

I am trying to understand AVAudioPlayer and sound level measurement. What I have is an “AudioPlayer” object that plays a short sound. Now I want to output the power of this sound (decibels). For some reason I don’t think that I am doing it right.

        audioPlayer.meteringEnabled = YES;
        [audioPlayer play];
        int channels = audioPlayer.numberOfChannels;
        [audioPlayer updateMeters];
        for (int i=0; i<channels; i++) {
            //Log the peak and average power
            NSLog(@"%d %0.2f %0.2f", i, [audioPlayer peakPowerForChannel:0],[audioPlayer averagePowerForChannel:0]);

NSLog output of this 0 -160.00 -160.00 1 -160.00 -160.00

Now, according to Apple, “A return value of 0 dB means full scale or maximum power, a return value of -160 dB indicates minimum power." Does this mean that this sound is minimal? I do not think that this is true, because the audio fragment is quite loud sound. I think that something is missing here, any clarifications will be appreciated.

+5
source share
2

, - updateMeters, , play. , , . , updateMeters , , .

> 0, 0 , i . , :

for (int currChan = 0; currChan < channels; currChan++) {
    //Log the peak and average power
    NSLog(@"%d %0.2f %0.2f", currChan, [audioPlayer peakPowerForChannel:currChan], [audioPlayer averagePowerForChannel:currChan]);
}
+3

- Jacques .

[audioPlayer updateMeters]; . , NSTimer.

iVar NSTimer *playerTimer; @interface.

<AVAudioPlayerDelegate> , , .

:

audioPlayer.meteringEnabled = YES;
audioPlayer.delegate = self;

if (!playerTimer)
{
    playerTimer = [NSTimer scheduledTimerWithTimeInterval:0.001
                  target:self selector:@selector(monitorAudioPlayer)
                userInfo:nil
                 repeats:YES];
}

[audioPlayer play];

:

-(void) monitorAudioPlayer
{   
    [audioPlayer updateMeters];
    
    for (int i=0; i<audioPlayer.numberOfChannels; i++)
    {
        //Log the peak and average power
         NSLog(@"%d %0.2f %0.2f", i, [audioPlayer peakPowerForChannel:i],[audioPlayer averagePowerForChannel:i]);
    }
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{   
    NSLog (@"audioPlayerDidFinishPlaying:");
    [playerTimer invalidate];
    playerTimer = nil;
}

.

+9

All Articles