Skip to content

Commit

Permalink
feat: init inputNode once for performance #207
Browse files Browse the repository at this point in the history
  • Loading branch information
sowens-csd committed May 3, 2021
1 parent ef77ce3 commit 38ca3f3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
private var listening = false
private let audioSession = AVAudioSession.sharedInstance()
private let audioEngine = AVAudioEngine()
private var inputNode: AVAudioInputNode?
private let jsonEncoder = JSONEncoder()
private let busForNodeTap = 0
private let speechBufferSize: AVAudioFrameCount = 1024
Expand Down Expand Up @@ -226,6 +227,12 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
onDeviceStatus = localRecognizer.supportsOnDeviceRecognition
}
recognizer?.delegate = self
inputNode = audioEngine.inputNode
guard inputNode != nil else {
os_log("Error no input node", log: pluginLog, type: .error)
sendBoolResult( false, result );
return
}
setupListeningSound()

sendBoolResult( true, result );
Expand Down Expand Up @@ -312,8 +319,7 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
}
do {
try trap {
let inputNode = self.audioEngine.inputNode
inputNode.removeTap(onBus: self.busForNodeTap);
self.inputNode?.removeTap(onBus: self.busForNodeTap);
}
}
catch {
Expand Down Expand Up @@ -380,9 +386,8 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
}
sound.play()
}
self.audioEngine.reset();
let inputNode = self.audioEngine.inputNode
if(inputNode.inputFormat(forBus: 0).channelCount == 0){
self.audioEngine.reset();
if(inputNode?.inputFormat(forBus: 0).channelCount == 0){
throw SpeechToTextError.runtimeError("Not enough available inputs.")
}
self.currentRequest = SFSpeechAudioBufferRecognitionRequest()
Expand All @@ -408,9 +413,9 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
break
}
self.currentTask = self.recognizer?.recognitionTask(with: currentRequest, delegate: self )
let recordingFormat = inputNode.outputFormat(forBus: self.busForNodeTap)
let recordingFormat = inputNode?.outputFormat(forBus: self.busForNodeTap)
try trap {
inputNode.installTap(onBus: self.busForNodeTap, bufferSize: self.speechBufferSize, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.inputNode?.installTap(onBus: self.busForNodeTap, bufferSize: self.speechBufferSize, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
currentRequest.append(buffer)
self.updateSoundLevel( buffer: buffer )
}
Expand Down

0 comments on commit 38ca3f3

Please sign in to comment.