4

I am having trouble getting an accurate meter reading from the AVAudioRecorder (testing on iPad).

It seems to work fine while the volume is rising, however a delay happens when the volume drops. For example: I speak into the mic and slowly raise my voice. The readings increment from -35.567 to -34.678 up to -10.579 as I would hope, but when I stop talking there is a delay of 1 - 2 seconds before it drops back down to -35.567 (or whatever it happens to be). The NSLog continues to update from the loop but the meter number stays the same during the delay even though the sound has long ended.

I have added the gist of the code below and would be happy to supply full code if need be.

I initialize the recorder like so:

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory()  stringByAppendingPathComponent: [NSString stringWithString: @"Recording.caf"]]];

recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder setMeteringEnabled:YES];

[recorder record];

and update the meter in a loop:

-(void) loop:(ccTime)dt 
{
    if(isRecording == YES)
    {        
        //get volume levels
        [recorder updateMeters];
        float level = [recorder peakPowerForChannel:0];
        NSLog(@"Vol: %f", level);
    }
}

edited: I should also mention that I am using the Cocos2d schedule for the loop:

[self schedule:@selector(loop:)];

Any ideas why there would be such a long delay?

edited: I have tried using the average peak power and this has no delay. So I could possibly use that as a work around. However I would rather not use and averaged peak power and it would be nice to understand what is going on.

Kangoo
  • 143
  • 3
  • 15
  • what is the loop interval of loop:(ccTime)dt function? – Raptor Jul 11 '11 at 05:55
  • The default. I'm not sure what that is. But the loop is updating the NSLog over and over during the delay. The number stays the same until it suddenly drops back down. – Kangoo Jul 11 '11 at 06:01
  • This starts the loop: [self schedule:@selector(loop:)]; – Kangoo Jul 11 '11 at 06:07
  • Any luck finding an answer for this? I am also having this problem – james Jul 18 '11 at 15:54
  • No sorry. I had to settle for a work around. I ended up using the average power for channel rather than the peak power for channel. It averages it out of course but it reacted much better and was sufficient for what I was doing. If you ever find out the answer I would still like to know. I hate having to settle for a work around! Cheers. – Kangoo Aug 09 '11 at 02:57

3 Answers3

3

I'm sure that most have figured this out but if you want less lag on your metering you need to use AudioQueue or RemoteIO. See the better explanation here:

Confusion with meters in AVAudioRecorder

Community
  • 1
  • 1
krut
  • 547
  • 4
  • 14
2

You can fix this by resetting the meteringEnabled property to YES.

yourRecorderName.meteringEnabled = YES

Call this every time you want the levels to reset to ambient levels. This takes about 0.02 seconds, and in that time the levels will briefly drop down to 0, or -120 dB before resetting to ambient.

Alternatively, you can use:

[yourRecorderName stop]
[yourRecorderName record]

This takes about 0.05 seconds, but the levels won't drop down to 0 in the wait time. In fact, nothing will happen because in this case, it actually takes the recorder object 0.05 seconds to stop and start recording again.

Matt Logan
  • 5,886
  • 5
  • 31
  • 48
1

How about using a timer ? it would be mutch quicker after

NSError* error
        if (recorder) {
            recorder.meteringEnabled = YES;
            [recorder record];
            levelTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    } else
            NSLog(@" error %@",[error description]);
        }

Where levelTimer is the NStimer that calls the function that does what you want(levelTimerCallback:), updates the meters, etc.

   -(IBAction)levelTimerCallback:(NSTimer*)timer
{
     [recorder updateMeters];
        float level = [recorder peakPowerForChannel:0];
        NSLog(@"Vol: %f", level);
}
Radu
  • 3,434
  • 4
  • 27
  • 38
  • No. It didn't work for me. Did you mange to get this to work at your end or is this untested? No malice intended - I'm just wondering if there is something wrong with my setup. The problem doesn't seem to be the timer. The timer continues to update the console but the number stays the same when it should be dropping. Thanks for your help. – Kangoo Jul 16 '11 at 21:05