10

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

Though I see the suggested algorithm, I am unclear as to how to implement it in Objective-C.

Community
  • 1
  • 1
Nathan Clark
  • 657
  • 6
  • 13
  • The algorithm is clear, it may only need tuning. What's the problem with implementing it in Objective-C? Isn't Objective-C a superset of plain C? If you can do it in plain C, you should be able to do it in Objective-C. – Alexey Frunze Jun 23 '12 at 23:12
  • 2
    Try this [ Tutorial: Detecting When A User Blows Into The Mic](http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/) for some possible approaches – spring Jun 24 '12 at 00:16
  • sinnyTOD, your link to mobileOrchard.com came up 404 for me. – john.k.doe Jun 24 '12 at 01:58
  • 1
    @john.k.doe Try this (http://mobileorchard.com/?s=blows+into+the+mic&submit=Search)[http://mobileorchard.com/?s=blows+into+the+mic&submit=Search]. – Alexey Frunze Jun 24 '12 at 04:39
  • @Nathan Clark, could you give an update as to how this was solved? The objective-c approach is tricky because there isn't a straightforward way to implement the algorithm with the AVAudioFoundation framework. – Stavash Aug 27 '12 at 11:22
  • hi Nathan Clark. i am creating one app which is working on Clapping based. but i m little confuse how to use it. so can you provide me full clapping based example or provide any example link so i can implement. thanx – Sandy Patel Feb 09 '15 at 05:11
  • link provided by @AlexeyFrunze is useful – Gajendra K Chauhan Jun 12 '15 at 07:18
  • @Nathan Clark, i need to detect the whistle, i am a bit confuse How can i achieve that. Can you please provide me some working example or useful link/path to achieve this. – pankaj asudani Aug 26 '15 at 06:36

2 Answers2

4

You didn't mention what sort of detection fidelity you are looking for? Just checking for some kind of sound "pressure" change may be entirely adequate for your needs, honestly.

Keep in mind however that bumps to the phone might end up being a very low frequency and fairly high-powered impulse such that it will trigger you detector even though it was not an actual clap. Ditto for very high frequency sound sources that are also not likely to be a clap.

Is this ok for your needs?

If not and you are hoping for something higher fidelity, I think you'd be better of doing a spectral analysis (FFT) of the input signal and then looking in a much narrower frequency band for a sharp signal spike, similar to the part you already have.

I haven't looked closely at this source, but here's some possible open source FFT code you could hopefully use as-is for your iphone app:

Edit: https://github.com/alexbw/iPhoneFFT

The nice part about graphing the spectral result is that it should make it quite easy to tune which frequency range you actually care about. In my own tests with some laptop software I have, my claps have a very strong spike around 1kHz - 2kHz.

Possibly overkill for you needs, but if you need something higher fidelity, then I suspect you will not be satisfied with simply tracking a signal spike without knowing what frequency range led to the signal spike in the first place.

Cheers

JDischler
  • 191
  • 17
  • The oscopeapp link is no longer valid. – Jetti May 01 '13 at 16:50
  • hi JDischler. i am creating one app which is working on Clapping based. but i m little confuse how to use it. so can you provide me full clapping based example or provide any example link so i can implement. thnx – – Sandy Patel Feb 20 '15 at 06:35
0

I used FFT for my App https://itunes.apple.com/us/app/clapmera/id519363613?mt=8 . Clap in the frequency domain looks like a (not perfect) constant.

Regards

Dario
  • 304
  • 2
  • 8