Detecting a clap in IOS

一笑奈何 提交于 2019-11-27 02:02:05

问题


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

}

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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/11173605/detecting-a-clap-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!