Skip to content

Instantly share code, notes, and snippets.

@iosdevie
Created May 26, 2021 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iosdevie/8187ea967b6fd8b21c6c6ab5fe89088e to your computer and use it in GitHub Desktop.
Save iosdevie/8187ea967b6fd8b21c6c6ab5fe89088e to your computer and use it in GitHub Desktop.
The AVAudioEngine is responsible for receiving the audio signals from the microphone. It provides our input for speech recognition.
let audioEngine = AVAudioEngine()
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
let inputNode = audioEngine.inputNode
inputNode.removeTap(onBus: 0)
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