iPhone App Pick Up Sound

别来无恙 提交于 2019-12-21 22:31:49

问题


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.


回答1:


The easiest thing for you do is to use the AudioQueue services. Here's the manual: Apple AQ manual

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

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.




回答2:


Apple has examples such as Speak Here which looks to have code relating to decibels. 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.



来源:https://stackoverflow.com/questions/5436565/iphone-app-pick-up-sound

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