简体   繁体   English

iPhone应用程序拾取声音

[英]iPhone App Pick Up Sound

I am trying to do a certain action based on whether or not the user makes a loud sound. 我试图根据用户是否发出响亮的声音来做某个动作。 I'm not trying to do any voice recognition or anything. 我不是想做任何语音识别或任何事情。 Just simply do an action based on whether the iPhone picks up a loud sound. 只需根据iPhone是否发出响亮的声音来做一个动作。

Any suggestions, tutorials, I can't find anything on the apple developer site. 任何建议,教程,我在苹果开发者网站上找不到任何东西。 I'm assuming i'm not looking or searching right. 我假设我不正确或正确搜索。

The easiest thing for you do is to use the AudioQueue services. 最简单的方法是使用AudioQueue服务。 Here's the manual: Apple AQ manual 这是手册: Apple AQ手册

Basically, look for any example code that initialized things with AudioQueueNewInput(). 基本上,查找使用AudioQueueNewInput()初始化事物的任何示例代码。 Something like this: 像这样的东西:

    Status = AudioQueueNewInput(&_Description,
                                Audio_Input_Buffer_Ready,
                                self,
                                NULL,
                                NULL,
                                0,
                                &self->Queue);

Once you have that going, you can enable sound level metering with something like this: 完成后,您可以启用声级计量,如下所示:

// Turn on level metering (iOS 2.0 and later)
UInt32 on = 1;
AudioQueueSetProperty(self->Queue,kAudioQueueProperty_EnableLevelMetering,&on,sizeof(on));

You will have a callback routine that is invoked for each chunk of audio data. 您将拥有一个为每个音频数据块调用的回调例程。 In it, you can check the current meter levels with something like this: 在其中,您可以使用以下内容检查当前的仪表级别:

//
//  Check metering levels and detect silence
//
AudioQueueLevelMeterState meters[1];
UInt32 dlen = sizeof(meters);
Status = AudioQueueGetProperty(_Queue,kAudioQueueProperty_CurrentLevelMeterDB,meters,&dlen);
if (Status == 0) {
    if (meters[0].mPeakPower > _threshold) {
        silence = 0.0;     // reset silence timer
    } else {
        silence += time;                
    }
}

//
//  Notify observers of incoming data.
//
if (delegate) {
    [delegate audioMeter:meters[0].mPeakPower duration:time];
    [delegate audioData:Buffer->mAudioData size:Buffer->mAudioDataByteSize];
}

Or, in your case, instead of silence you can detect if the decibel level is over a certain value for long enough. 或者,在您的情况下,您可以检测分贝水平是否超过某个值足够长的时间而不是沉默。 Note that the decibel values you will see will range from about -70.0 for dead silence, up to 0.0db for very loud things. 请注意,您将看到的分贝值范围从大约-70.0(对于死静音)到高达0.0db(非常响亮的事物)。 On an exponential scale. 在指数范围内。 You'll have to play with it to see what values work for your particular application. 您必须使用它来查看哪些值适用于您的特定应用程序。

Apple has examples such as Speak Here which looks to have code relating to decibels. Apple有一些例子,例如Speak Here ,它看起来有与分贝有关的代码。 I would check some of the meter classes for examples. 我会检查一些仪表类的例子。 I have no audio programming experience but hopefully that will get you started while someone provides you with a better answer. 我没有音频编程经验,但希望这会让你开始,而有人为你提供更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM