Use two SFSpeechRecognizer with different languages at the same time in Swift 5

I am trying to run two SFSpeechRecognizer simultaneously with different languages.

So I tried the following:

var speechRecognizer1: SFSpeechRecognizer? = SFSpeechRecognizer(locale: Locale(identifier: "en-GB"))
var speechRecognizer2: SFSpeechRecognizer? = SFSpeechRecognizer(locale: Locale(identifier: "it-IT"))

var speechAudioBufferRecognitionRequest = SFSpeechAudioBufferRecognitionRequest()

var speechRecognitionTask1: SFSpeechRecognitionTask!
var speechRecognitionTask2: SFSpeechRecognitionTask!

...

let node = self.audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)

node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat, block: { (buffer, time) in
    self.speechAudioBufferRecognitionRequest.append(buffer)
})
    
self.audioEngine.prepare()
do {
    try audioEngine.start()
} catch {
    print("Error")
    return
}
    
guard let myRecognition = SFSpeechRecognizer() else {
    print("Error")
    return
}
    
if(!myRecognition.isAvailable) {
    print("Error")
    return
}
    
self.speechRecognitionTask1 = self.speechRecognizer1?.recognitionTask(with: self.speechAudioBufferRecognitionRequest, resultHandler: { (response, error) in
    if(response != nil) {
        guard let response = response else {
            if let error = error {
                print("Error")
                return
            } else {
                print("Error")
                return
            }
        }
            
        var message = response.bestTranscription.formattedString
})

self.speechRecognitionTask2 = self.speechRecognizer2?.recognitionTask(with: self.speechAudioBufferRecognitionRequest, resultHandler: { (response, error) in
    if(response != nil) {
        guard let response = response else {
            if let error = error {
                print("Error")
                return
            } else {
                print("Error")
                return
            }
        }
            
        var message = response.bestTranscription.formattedString
})

This gave me the error: SFSpeechAudioBufferRecognitionRequest cannot be re-used

So I tried to create two instances and initialized them by:

    self.speechAudioBufferRecognitionRequest1.append(buffer)
    self.speechAudioBufferRecognitionRequest2.append(buffer)
})

But this also didn't work. There was no error, but one speechRecognition just overwrote the other...

I tried some other stuff like changing the bus etc. but was not successful...

After some more trying I found out that only the speech recognition task works, whose buffer comes first.

That means if I do it like this:

self.speechAudioBufferRecognitionRequest1.append(buffer) self.speechAudioBufferRecognitionRequest2.append(buffer)

then the speechRecognitionTask1 works. If I do it like this:

self.speechAudioBufferRecognitionRequest2.append(buffer) self.speechAudioBufferRecognitionRequest1.append(buffer)

then the speechRecognitionTask2 works.

I have a similar issue. I use the SFSpeechRecognizer for 2 or 3 local files and try to recognize them in parallel. I use macOS for it but it didn't work only one of the recognizer will work and the other will stop. I don't know if it is on purpose or a hardware limitation 🤷‍♂️. I wish some Apple engineer could bring some light into this.

Use two SFSpeechRecognizer with different languages at the same time in Swift 5
 
 
Q