2017-09-21 2 views
1

말하는 경우 예상하지만 사용자가 그 시간에 말하기를 중지하면 탐지를 중지하고 해당 데이터를 서버로 보내려는 것을 감지하려고합니다.어떻게 자동 정지 음성 인식에 대한 사용자 정지 내가 봇 응용 프로그램에서 일하고 여기에 내가 연설</li>에 모두 같이 노력하고 있습니다 </ul> <p>을</li> <li>텍스트를 텍스트로이 개 기능</p> <ul> <li>음성이

해당 사용자가 말하는 것을 방해 할 수있는 방법이 있습니까?

나는 음성 검출 코드 아래 사용하고 있습니다 :

// Starts an AVAudio Session 
    NSError *error; 
    AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; 
    [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]; 

    // Starts a recognition process, in the block it logs the input or stops the audio 
    // process if there's an error. 
    recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; 
    AVAudioInputNode *inputNode = audioEngine.inputNode; 
    recognitionRequest.shouldReportPartialResults = YES; 
    recognitionTask = [speechRecognizer recognitionTaskWithRequest:recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) { 
     BOOL isFinal = NO; 
     if (result) { 
      // Whatever you say in the microphone after pressing the button should be being logged 
      // in the console. 
      NSLog(@"RESULT:%@",result.bestTranscription.formattedString); 
      self.inputToolbar.contentView.textView.text = result.bestTranscription.formattedString; 
      self.inputToolbar.contentView.rightBarButtonItem.enabled = YES; 
      isFinal = !result.isFinal; 
     } 
     if (error) { 
      if (audioEngine != NULL) { 
       [audioEngine stop]; 
       [inputNode removeTapOnBus:0]; 
       recognitionRequest = nil; 
       recognitionTask = nil; 
      } 
     } 
    }]; 

    // Sets the recording format 
    AVAudioFormat *recordingFormat = [inputNode outputFormatForBus:0]; //[[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100 channels:1]; 
    [inputNode installTapOnBus:0 bufferSize:1024 format:recordingFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) { 
     [recognitionRequest appendAudioPCMBuffer:buffer]; 
    }]; 

    // Starts the audio engine, i.e. it starts listening. 
    [audioEngine prepare]; 
    [audioEngine startAndReturnError:&error]; 
    NSLog(@"Say Something, I'm listening"); 

어느 한이에 대한 자세한 내용을 필요에 알려주세요.

미리 감사드립니다.

답변

2

사용해보십시오이 :

AVAudioRecorder *recorder; 
NSTimer *levelTimer; 
double lowPassResults; 

-(void)configureRecorder{ 
    // AVAudioSession already set in your code, so no need for these 2 lines. 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
    [[AVAudioSession sharedInstance] setActive:YES error:nil]; 

    NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; 

    NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat: 44100.0],     AVSampleRateKey, 
          [NSNumber numberWithInt: kAudioFormatAppleLossless], AVFormatIDKey, 
          [NSNumber numberWithInt: 1],       AVNumberOfChannelsKey, 
          [NSNumber numberWithInt: AVAudioQualityMax],   AVEncoderAudioQualityKey, 
          nil]; 

    NSError *error; 

    lowPassResults = 0; 

    recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error]; 

    if (recorder) { 
     [recorder prepareToRecord]; 
     recorder.meteringEnabled = YES; 
     [recorder record]; 
     levelTimer = [NSTimer scheduledTimerWithTimeInterval: 0.05 target: self selector: @selector(levelTimerCallback:) userInfo: nil repeats: YES]; 
    } else 
     NSLog(@"%@", [error description]); 
    } 
} 


- (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(@"lowPassResults: %f",lowPassResults); 

    // Use here a threshold value to stablish if there is silence or speech 
    if (lowPassResults < 0.1) { 
     NSLog(@"Silence"); 
    } else if(lowPassResults > 0.5){ 
     NSLog(@"Speech"); 
    } 
} 

참조 : http://codedrago.com/q/200783/ios-objective-c-speech-recognition-how-to-detect-speech-start-on-ios-speech-api

+0

그것이 예상대로 작동하지만 난 우리가 API를 수 있도록 텍스트 음성을 사용하는대로 메모리 관리에 영향을 미칠 것이다 질문이 하나의 이미 네트워크 통화 및 녹화를 위해 더 많은 메모리와 CPU를 소비하고 있으므로 가능하다면 UR에 귀중한 의견을 전합니다. – CodeChanger

+0

이 코드를 사용하고 있으며 응용 프로그램에서 잘 작동합니다. 당신은 레코더를 사용하여 침묵을 감지 할 수 있으므로 더 많은 메모리를 소비하지 않을 것이라고 생각합니다. 타이머와 레코더를 관리하는 데 필요한 것은 단 하나입니다. 작업이 완료되면 타이머를 무효화하고 레코더를 중지하십시오. – Pushpendra

+0

네,이 코드를 시도해보고 라이브가 어떻게되는지 보도록하겠습니다.하지만 ur exp마다 생각하면 메모리가 더 많이 소모됩니다. 답장을 보내 주셔서 감사합니다. – CodeChanger

관련 문제