iosswifttwiliosfspeechrecognizer

iOS 10: SFSpeechRecognizer and Twilio TVILocalAudioTrack


I'm working on an iOS App in Swift that attempts to transcribe conversations. I was successfully able to transcribe a conversation using an AVAudioEngine() and SFSpeechRecognizer().

    // MARK: Speech Helper Methods
func recordAndRecognizeSpeech() {
    guard let node = audioEngine.inputNode else { return }
    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 {
        return print(error)
    }

    guard let myRecognizer = SFSpeechRecognizer() else {
        return
    }
    if !myRecognizer.isAvailable {
        return
    }

    recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in
        if let result = result {
            let bestString = result.bestTranscription.formattedString
        } else if let error = error {
            print(error)
        }
    })
}

Now I want to try and bring this to a video chat. So I followed the Twilio quickstart found here on github and set up everything as appropriate (I used the CallKit version, not that it matters). However, the audio track that I have available during a Twilio video call is not an AVAudioEngine track but instead is a TVILocalAudioTrack. This is a specific type for the Twilio SDK.

The documentation for TVILocalAudioTrack is here, but I cannot figure out how to plug that into SFSpeechRecognizer.

Possible Alternative

Start an AVAudioEngine() while someone is in a Twilio room (I don't think I can as per Twilio's SDK) - if anyone has advice on this that would be great.

Maybe Swift isn't good enough for this project? idk!


Solution

  • This can not currently be done with Twilio v1.0 beta 14. It requires having access to the audio buffer, which is not accessible through Twilio's API.

    See this Github issue for more details.