2021-10-19 04:55:58 +00:00
|
|
|
//
|
|
|
|
// SpeechForwarder.swift
|
|
|
|
// ListenerGS
|
|
|
|
//
|
|
|
|
// Created by Jeremy Rand on 2021-10-18.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-01-12 04:30:41 +00:00
|
|
|
import os
|
2021-10-19 04:55:58 +00:00
|
|
|
import Speech
|
|
|
|
|
2022-03-16 03:58:04 +00:00
|
|
|
class SpeechForwarder : SpeechForwarderProtocol {
|
2021-10-19 04:55:58 +00:00
|
|
|
|
2022-01-19 03:51:28 +00:00
|
|
|
private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: Locale.preferredLanguages[0]))!
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
|
|
|
|
|
|
|
|
private var recognitionTask: SFSpeechRecognitionTask?
|
|
|
|
|
|
|
|
private let audioEngine = AVAudioEngine()
|
|
|
|
|
2022-01-12 04:30:41 +00:00
|
|
|
private let logger = Logger()
|
2021-10-19 04:55:58 +00:00
|
|
|
|
2022-03-16 22:13:56 +00:00
|
|
|
func startListening(connection : GSConnection) -> Bool {
|
2022-03-16 03:58:04 +00:00
|
|
|
SFSpeechRecognizer.requestAuthorization { authStatus in
|
|
|
|
OperationQueue.main.addOperation {
|
|
|
|
switch authStatus {
|
|
|
|
case .authorized:
|
2021-10-19 04:55:58 +00:00
|
|
|
break
|
2022-03-16 03:58:04 +00:00
|
|
|
|
|
|
|
case .denied, .restricted, .notDetermined:
|
2022-03-16 22:13:56 +00:00
|
|
|
connection.stopListening()
|
2022-03-16 03:58:04 +00:00
|
|
|
|
|
|
|
default:
|
2022-03-16 22:13:56 +00:00
|
|
|
connection.stopListening()
|
2022-02-23 04:52:00 +00:00
|
|
|
}
|
2021-10-19 04:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 03:58:04 +00:00
|
|
|
|
|
|
|
do {
|
2022-03-16 22:13:56 +00:00
|
|
|
try startRecording(connection: connection)
|
2022-03-16 03:58:04 +00:00
|
|
|
logger.debug("Started listening")
|
2021-10-19 04:55:58 +00:00
|
|
|
}
|
2022-03-16 03:58:04 +00:00
|
|
|
catch {
|
2022-02-23 04:52:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2021-10-19 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 03:58:04 +00:00
|
|
|
func stopListening() {
|
|
|
|
logger.debug("Stopped listening")
|
|
|
|
recognitionRequest?.endAudio()
|
|
|
|
audioEngine.stop()
|
|
|
|
audioEngine.inputNode.removeTap(onBus: 0)
|
|
|
|
recognitionTask?.cancel()
|
|
|
|
|
|
|
|
recognitionRequest = nil
|
|
|
|
recognitionTask = nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:13:56 +00:00
|
|
|
private func startRecording(connection : GSConnection) throws {
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
// Cancel the previous task if it's running.
|
|
|
|
recognitionTask?.cancel()
|
|
|
|
self.recognitionTask = nil
|
|
|
|
|
|
|
|
// Configure the audio session for the app.
|
|
|
|
let audioSession = AVAudioSession.sharedInstance()
|
|
|
|
try audioSession.setCategory(.record, mode: .measurement, options: .duckOthers)
|
|
|
|
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
|
|
|
|
let inputNode = audioEngine.inputNode
|
2022-02-22 04:22:58 +00:00
|
|
|
|
|
|
|
// Configure the microphone input.
|
|
|
|
let recordingFormat = inputNode.outputFormat(forBus: 0)
|
|
|
|
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
|
|
|
|
self.recognitionRequest?.append(buffer)
|
|
|
|
}
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
// Create and configure the speech recognition request.
|
|
|
|
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
|
|
|
|
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to create a SFSpeechAudioBufferRecognitionRequest object") }
|
|
|
|
recognitionRequest.shouldReportPartialResults = true
|
2022-03-09 04:05:23 +00:00
|
|
|
recognitionRequest.requiresOnDeviceRecognition = false
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
// Create a recognition task for the speech recognition session.
|
|
|
|
// Keep a reference to the task so that it can be canceled.
|
|
|
|
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
|
|
|
|
var isFinal = false
|
|
|
|
|
|
|
|
if let result = result {
|
|
|
|
// Update the text view with the results.
|
2022-03-16 22:13:56 +00:00
|
|
|
OperationQueue.main.addOperation { connection.set(text: result.bestTranscription.formattedString) }
|
2021-10-19 04:55:58 +00:00
|
|
|
isFinal = result.isFinal
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:22:58 +00:00
|
|
|
if error != nil {
|
|
|
|
self.logger.error("Error from recognizer: \(String(describing: error))")
|
|
|
|
}
|
|
|
|
|
2021-10-19 04:55:58 +00:00
|
|
|
if error != nil || isFinal {
|
2022-02-23 04:52:00 +00:00
|
|
|
OperationQueue.main.addOperation {
|
2022-03-16 22:13:56 +00:00
|
|
|
connection.stopListening()
|
2022-01-12 04:30:41 +00:00
|
|
|
}
|
2021-10-19 04:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
audioEngine.prepare()
|
|
|
|
try audioEngine.start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|