Biranchi
Biranchi

Reputation: 16327

Getting AveragePower and PeakPower for a Channel in AVAudioRecorder

I am annoyed with this piece of code. I am trying to get the averagePowerForChannel and peakPowerForChannel while recording Audio, but every time i am getting it as 0.0

Below is my code for recording audio :

 NSMutableDictionary *recordSetting =[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithFloat: 22050.0], AVSampleRateKey,
            [NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
            [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
            [NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
            [NSNumber numberWithInt:32],AVLinearPCMBitDepthKey,
            [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
            [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
            nil];

recorder1 = [[AVAudioRecorder alloc] initWithURL:[NSURL fileURLWithPath:audioFilePath] settings:recordSetting error:&err];
 recorder1.meteringEnabled = YES;
 recorder1.delegate=self;
 [recorder1 prepareToRecord];
 [recorder1 record];
 levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.3f target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];

- (void)levelTimerCallback:(NSTimer *)timer {

 [recorder1 updateMeters];

 NSLog(@"Peak Power : %f , %f", [recorder1 peakPowerForChannel:0], [recorder1 peakPowerForChannel:1]);
 NSLog(@"Average Power : %f , %f", [recorder1 averagePowerForChannel:0], [recorder1 averagePowerForChannel:1]);

}

What is the error in the code ???

Upvotes: 2

Views: 3369

Answers (2)

Viraj
Viraj

Reputation: 1890

I think you have to use the property meteringEnabled after you prepareToRecord. And just the comment the part where you specify number of channels as 1. Let it use the default values. Hope it helps.

Upvotes: 6

Rick Brown
Rick Brown

Reputation: 21

I think if you set AVNumberOfChannelsKey to 2...it will work.

Upvotes: 2

Related Questions