I am trying to build an IOS application that counts claps. I have been watching the WWDC videos on CoreAudio, and the topic seems so vast that I'm not quite sure where to look.

I have found similar problems here in stackoverflow. Here is one in C# for detecting a door slam: Given an audio stream, find when a door slams (sound pressure level calculation?)

It seems that I need to do this:

  1. Divide the samples up into sections
  2. Calculate the energy of each section
  3. Take the ratio of the energies between the previous window and the current window
  4. If the ratio exceeds some threshold, determine that there was a sudden loud noise.

I am not sure how to accomplish this in Objective-C. I have been able to figure out how to sample the audio with SCListener Here is my attempt:

- (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;


    if ([recorder peakPowerForChannel:0] == 0)
        totalClapsLabel.text = [NSString stringWithFormat:@"%d", total++];

    SCListener *listener = [SCListener sharedListener];
    if (![listener isListening])
        return;

    AudioQueueLevelMeterState *levels = [listener levels];
    Float32 peak = levels[0].mPeakPower;
    Float32 average = levels[0].mAveragePower;


    lowPassResultsLabel.text = [NSString stringWithFormat:@"%f", lowPassResults];
    peakInputLabel.text      = [NSString stringWithFormat:@"%f", peak];
    averageInputLabel.text   = [NSString stringWithFormat:@"%f", average];

}

enter image description here<

Edit
Report