检测Mic中的打击并做一些事情

时间:2010-04-11 19:07:33

标签: iphone microphone

我发现了这个tutorial,这很好,但对我不起作用!

这是代码:

- (void)listenForBlow:(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 (lowPassResults > 0.95)
        NSLog(@"Mic blow detected");
 //change the background color e.g !
}
控制台中的

告诉我这样的nslog研究(没有任何保龄球!):

2010-04-11 23:32:27.935 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:27.965 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:27.995 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.026 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.055 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.086 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.115 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.145 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.175 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.205 MicBlow[2358:207] Mic blow detected
2010-04-11 23:32:28.236 MicBlow[2358:207] Mic blow detected

我改变了这个值:

 if (lowPassResults < 0.95)

if (lowPassResults > 0.95)

所以看起来很有效!但是,如果我把背景改变代码,我的代码改变背景,但没有任何保龄球! 有什么问题?

1 个答案:

答案 0 :(得分:3)

我也在使用它,它对我有用。您只需要使用该值将其与lowPassResults进行比较。这是我的代码:

- (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);
    if (lowPassResults > 0.55)
        NSLog(@"Mic blow detected");
}