Expanding a speech to text demo, and while it works, I am still trying to learn Swift. Is .installTap the Swift version of a C callback function?
From what I interpret here, every time the buffer becomes full, the code in between the last { } runs, as well, the code below it is also run.
It almost feels like a callback combined with a GOTO line from basic.
yes, it works, but I'd like to understand that I am getting the flow of the code correctly.
func startSpeechRecognition (){
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in self.request.append(buffer) }
audioEngine.prepare()
do {
try audioEngine.start()
} catch let error {
...
}
guard let myRecognition = SFSpeechRecognizer() else {
...
return
}
if !myRecognition.isAvailable {
...
}
task = speechRecognizer?.recognitionTask(with: request, resultHandler: { (response, error) in guard let response = response else {
if error != nil {
print ("\(String(describing: error.debugDescription))")
} else {
print ("problem in repsonse")
}
return
}
let message = response.bestTranscription.formattedString
print ("\(message)")
})
}
Yes.
Doc explains:
tapBlock
A block the framework calls with audio buffers.
Which means that the closure (block) is called repeatedly as soon as the node gets an audio buffer. It plays as an observer, running permanently until you stop it. And code execution continues in sequence as soon tap is created.
Doc also reminds:
The framework may invoke the tapBlock on a thread other than the main thread.