How to detect voice in my iPhone app? [closed]

橙三吉。 提交于 2019-12-23 06:30:29

问题


I want to fire some method on voice detection.

For Example,

Just like in PragDuck app, When user starts speaking duck starts its animation.

How can I detect users voice?


回答1:


Use AVAudioRecorder - Audio Metering - checkout out this tutorial - dettect when a user blows into mic http://mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/

Quick Example:

_audioRecorder.meteringEnabled = YES;

//1. This method will get the current mic activity and will format it to a 0 - 1 scale.

-(void)checkRecordingMeters:(NSTimer *)timer
{
      [_audioRecorder updateMeters];

      const double ALPHA = 0.2;
      float peakPower = [_audioRecorder peakPowerForChannel:0];

      double peakPowerForChannel = pow(10, (0.05 * peakPower));
      lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;

      NSLog(@"Meters: %f" , peakPower);
      NSLog(@"lowPassResults: %f \n" , lowPassResults);
}


//2. Call this method to run a loop timer to check the current mic activity
-(void)enableMettering:(BOOL)enable
{

    if(enable)
    {
        levelTimer = [[NSTimer scheduledTimerWithTimeInterval:0.03
                                                  target:self
                                                selector:@selector(checkRecordingMeters:)
                                                userInfo:nil
                                                 repeats:YES] retain];
    }
    else
    {
        [levelTimer invalidate];
        [levelTimer release];
    }
}



回答2:


You may use AudioQueue to record and add a simple threshold filter to ignore environment noise. For lower latency, you may use AudioUnit.



来源:https://stackoverflow.com/questions/8484566/how-to-detect-voice-in-my-iphone-app

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