Skip to content

Instantly share code, notes, and snippets.

@SunXiaoShan
Created February 8, 2018 05:05
Show Gist options
  • Save SunXiaoShan/71fb8bd4abd934cbf77a2d33f6e2cca2 to your computer and use it in GitHub Desktop.
Save SunXiaoShan/71fb8bd4abd934cbf77a2d33f6e2cca2 to your computer and use it in GitHub Desktop.
VoiceRecognition
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
let inputNode = self.getInputNode()
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }
// Configure request so that results are returned before audio recording is finished
recognitionRequest.shouldReportPartialResults = true
let recognizer = SFSpeechRecognizer(locale: Locale(identifier: (self.localeIdentifier)!))
recognizer?.recognitionTask(with: recognitionRequest, delegate: self)
let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
try audioEngine.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment