Search code examples
iosobjective-cavaudiorecorder

Get Decibels data from recorded audio in iOS and Objective C


I am trying to get decibels values from a recorded audio sample in order to make a wave chart and process its peaks. At the moment I'm using AVAudioRecorder to record an audio file and access its DB values by using [recorder averagePowerForChannel:0], called by a timer, however I am facing a limitation. Since I need to be the most precise as possible I have set the timer to repeating at 0.001s, however calling [recorder averagePowerForChannel:0] at each tick outputs the same value multiple times. It seems like the recorder is not able to update the values below 0.2s. How could I overcome this limitation?

This is the recorder setting:

NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSNumber numberWithFloat: 44000.0],                 AVSampleRateKey,
                                  [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey,
                                  [NSNumber numberWithInt: 1],                         AVNumberOfChannelsKey,
                                  [NSNumber numberWithInt: AVAudioQualityMax],         AVEncoderAudioQualityKey,
                                  nil];

Then I setup the recorder

recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
    [recorder setMeteringEnabled:YES];
    [recorder prepareToRecord];
    [recorder record];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.001f target:self selector: @selector(cllBack:) userInfo: nil repeats: YES];

And in cllBack I call

[recorder updateMeters];
[recorder averagePowerForChannel:0];

Thanks in advance for any help!


Solution

  • Solved using AVAudioEngine by installing a tap on bus and analyzing the data.