3

I have an audio recorder and I was wondering if anyone has a code to show a audio meter (the bar that most of audio recorders have which shows the level of input audio).

Monolo
  • 18,205
  • 17
  • 69
  • 103
Nomad
  • 49
  • 2
  • 4

2 Answers2

4

Apple's SpeakHere example code includes a LevelView class which seems to be exactly what you are looking for.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Jonah
  • 17,918
  • 1
  • 43
  • 70
2

Heres an example that calculates output and prints it:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; // Your audio save path

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

    NSError *error;

    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];

    if (recorder) {
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;
        [recorder record];
        levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.03 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES];
    } 
}


 - (void)levelTimerCallback:(NSTimer *)timer {
    [recorder updateMeters];

    const double ALPHA = 0.05;
    double peakPowerForChannel = pow(10, (0.05 * [recorder peakPowerForChannel:0]));
    lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
    NSLog(@"%f",(lowPassResults*100.0f));
}

Depending on the lowPassResults you can animate a view accordingly.

Ushan87
  • 1,608
  • 8
  • 15