iPhone应用程序拾取声音 [英] iPhone App Pick Up Sound
问题描述
我正在尝试根据用户是否发出响亮的声音来执行某项操作。我不是想做任何语音识别或任何事情。只需根据iPhone是否发出响亮的声音做一个动作。
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.
任何建议,教程,我在苹果开发者网站上找不到任何东西。我假设我没有正确地寻找或搜索。
Any suggestions, tutorials, I can't find anything on the apple developer site. I'm assuming i'm not looking or searching right.
推荐答案
最简单的方法是使用AudioQueue服务。以下是手册:
Apple AQ手册
The easiest thing for you do is to use the AudioQueue services. Here's the manual: Apple AQ manual
基本上,查找使用AudioQueueNewInput()初始化事物的任何示例代码。这样的事情:
Basically, look for any example code that initialized things with 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];
}
或者,在您的情况下,您可以检测分贝水平,而不是沉默超过某个值足够长的时间。请注意,您将看到的分贝值范围大约为-70.0(对于死静音),对于非常响亮的事物,大约为0.0db。在指数范围内。您必须使用它来查看哪些值适用于您的特定应用程序。
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. On an exponential scale. You'll have to play with it to see what values work for your particular application.
这篇关于iPhone应用程序拾取声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!