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
|
|
|
|
|
|
|
|
|
|
class SpeechForwarder : ObservableObject {
|
|
|
|
|
@Published var listening = false
|
2022-01-12 04:30:41 +00:00
|
|
|
|
@Published var connected = false
|
2022-02-23 04:52:00 +00:00
|
|
|
|
@Published var connecting = false
|
2022-01-19 03:51:28 +00:00
|
|
|
|
@Published var textHeard = ""
|
2022-02-23 04:52:00 +00:00
|
|
|
|
@Published var sending = false
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
|
|
let LISTEN_STATE_MSG = 1
|
|
|
|
|
let LISTEN_TEXT_MSG = 2
|
2022-02-23 04:52:00 +00:00
|
|
|
|
let LISTEN_SEND_MORE = 3
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
|
|
|
|
let port = 19026
|
|
|
|
|
private var client: TCPClient?
|
|
|
|
|
|
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-02-23 04:52:00 +00:00
|
|
|
|
private let queue = OperationQueue()
|
|
|
|
|
|
|
|
|
|
private var condition = NSCondition()
|
|
|
|
|
private var latestText = ""
|
|
|
|
|
|
2022-01-12 04:30:41 +00:00
|
|
|
|
func connect(destination : String) {
|
2022-02-23 04:52:00 +00:00
|
|
|
|
connecting = true
|
|
|
|
|
queue.addOperation {
|
|
|
|
|
self.logger.debug("Attempting to connect to \(destination)")
|
|
|
|
|
self.client = TCPClient(address: destination, port: Int32(self.port))
|
|
|
|
|
guard let client = self.client else {
|
|
|
|
|
OperationQueue.main.addOperation { self.connecting = false }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch client.connect(timeout: 10) {
|
|
|
|
|
case .success:
|
|
|
|
|
OperationQueue.main.addOperation { self.connected = true }
|
|
|
|
|
self.logger.debug("Connected to \(destination)")
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
client.close()
|
|
|
|
|
self.client = nil
|
|
|
|
|
self.logger.error("Failed to connect to \(destination): \(String(describing: error))")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
OperationQueue.main.addOperation { self.connecting = false }
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 04:30:41 +00:00
|
|
|
|
func disconnect() {
|
|
|
|
|
if (listening) {
|
|
|
|
|
listen()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let client = client else { return }
|
2022-02-23 04:52:00 +00:00
|
|
|
|
|
|
|
|
|
condition.lock()
|
2022-01-12 04:30:41 +00:00
|
|
|
|
client.close()
|
2022-02-23 04:52:00 +00:00
|
|
|
|
self.client = nil
|
|
|
|
|
condition.broadcast()
|
|
|
|
|
condition.unlock()
|
|
|
|
|
|
2022-01-12 04:30:41 +00:00
|
|
|
|
connected = false
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 04:55:58 +00:00
|
|
|
|
func listen() {
|
|
|
|
|
self.listening.toggle()
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
SFSpeechRecognizer.requestAuthorization { authStatus in
|
|
|
|
|
// The authorization status results in changes to the
|
|
|
|
|
// app’s interface, so process the results on the app’s
|
|
|
|
|
// main queue.
|
|
|
|
|
OperationQueue.main.addOperation {
|
2022-02-23 04:52:00 +00:00
|
|
|
|
switch authStatus {
|
|
|
|
|
case .authorized:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case .denied:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
2022-02-23 04:52:00 +00:00
|
|
|
|
case .restricted:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
2021-10-19 04:55:58 +00:00
|
|
|
|
|
2022-02-23 04:52:00 +00:00
|
|
|
|
case .notDetermined:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let client = client else { return }
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
switch (client.send(data: isListening())) {
|
|
|
|
|
case .success:
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
self.listening = false
|
2022-01-12 04:30:41 +00:00
|
|
|
|
logger.error("Unable to send header: \(String(describing: error))")
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
do {
|
|
|
|
|
try startRecording()
|
2022-01-12 04:30:41 +00:00
|
|
|
|
logger.debug("Started listening")
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
self.listening = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!self.listening) {
|
2022-01-12 04:30:41 +00:00
|
|
|
|
logger.debug("Stopped listening")
|
2021-10-19 04:55:58 +00:00
|
|
|
|
recognitionRequest?.endAudio()
|
2022-02-23 04:52:00 +00:00
|
|
|
|
audioEngine.stop()
|
|
|
|
|
audioEngine.inputNode.removeTap(onBus: 0)
|
2022-02-22 04:22:58 +00:00
|
|
|
|
recognitionTask?.cancel()
|
2022-02-23 04:52:00 +00:00
|
|
|
|
|
|
|
|
|
self.recognitionRequest = nil
|
|
|
|
|
self.recognitionTask = nil
|
|
|
|
|
condition.lock()
|
|
|
|
|
self.listening = false
|
|
|
|
|
condition.broadcast()
|
|
|
|
|
condition.unlock()
|
2021-10-19 04:55:58 +00:00
|
|
|
|
switch (client.send(data: isListening())) {
|
|
|
|
|
case .success:
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
2022-01-12 04:30:41 +00:00
|
|
|
|
logger.error("Failed to send header: \(String(describing: error))")
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func isListening() -> Data {
|
|
|
|
|
return pack("<hh", [LISTEN_STATE_MSG, listening ? 1 : 0])
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 04:52:00 +00:00
|
|
|
|
private func send() {
|
|
|
|
|
var stringLastSent = ""
|
|
|
|
|
var stringToSend = ""
|
|
|
|
|
var canSend = true
|
|
|
|
|
|
|
|
|
|
while true {
|
|
|
|
|
while (!canSend) {
|
|
|
|
|
logger.debug("Cannot send")
|
|
|
|
|
guard let client = client else {
|
|
|
|
|
logger.debug("Returning because client gone")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let byteArray = client.read(2, timeout: 1) else {
|
|
|
|
|
logger.debug("Did not read data")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
let data = Data(byteArray)
|
|
|
|
|
do {
|
|
|
|
|
let unpacked = try unpack("<h", data)
|
|
|
|
|
canSend = (unpacked[0] as? Int == LISTEN_SEND_MORE)
|
|
|
|
|
logger.debug("Updated canSend")
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
logger.debug("Unpack failed")
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
logger.debug("Can send")
|
|
|
|
|
|
|
|
|
|
condition.lock()
|
|
|
|
|
while (stringLastSent == latestText) {
|
|
|
|
|
if (!self.listening) {
|
|
|
|
|
condition.unlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
condition.wait()
|
|
|
|
|
if (!self.listening) {
|
|
|
|
|
condition.unlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard client != nil else {
|
|
|
|
|
condition.unlock()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stringToSend = latestText
|
|
|
|
|
condition.unlock()
|
|
|
|
|
|
|
|
|
|
if send(latestText: stringToSend, lastSent: stringLastSent) {
|
|
|
|
|
stringLastSent = stringToSend
|
|
|
|
|
canSend = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func send(latestText : String, lastSent: String) -> Bool {
|
|
|
|
|
guard let client = client else { return false }
|
|
|
|
|
var commonChars = lastSent.count
|
2021-10-19 04:55:58 +00:00
|
|
|
|
while (commonChars > 0) {
|
2022-02-23 04:52:00 +00:00
|
|
|
|
if (latestText.prefix(commonChars) == lastSent.prefix(commonChars)) {
|
2021-10-19 04:55:58 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
commonChars -= 1
|
|
|
|
|
}
|
|
|
|
|
var stringToSend = ""
|
2022-02-23 04:52:00 +00:00
|
|
|
|
if (commonChars < lastSent.count) {
|
|
|
|
|
stringToSend = String(repeating: "\u{7f}", count: lastSent.count - commonChars)
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
stringToSend.append(contentsOf: latestText.suffix(latestText.count - commonChars).replacingOccurrences(of: "\n", with: "\r"))
|
|
|
|
|
|
2022-02-23 04:52:00 +00:00
|
|
|
|
if (stringToSend.count == 0) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JSR_TODO - Handle strings to send that are longer than 64K (doubt that would happen though)
|
|
|
|
|
let nsEnc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringBuiltInEncodings.macRoman.rawValue))
|
|
|
|
|
let encoding = String.Encoding(rawValue: nsEnc) // String.Encoding
|
|
|
|
|
if let bytes = stringToSend.data(using: encoding) {
|
|
|
|
|
switch (client.send(data: pack("<hh", [LISTEN_TEXT_MSG, bytes.count]))) {
|
|
|
|
|
case .success:
|
|
|
|
|
switch (client.send(data: bytes)) {
|
|
|
|
|
case .success:
|
|
|
|
|
logger.debug("Sent text \"\(stringToSend)\"")
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
OperationQueue.main.addOperation {
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
self.listen()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
logger.error("Failed to send text: \(String(describing: error))")
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
OperationQueue.main.addOperation {
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
self.listen()
|
2022-01-19 03:51:28 +00:00
|
|
|
|
}
|
2022-02-23 04:52:00 +00:00
|
|
|
|
}
|
|
|
|
|
logger.error("Failed to send text: \(String(describing: error))")
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-23 04:52:00 +00:00
|
|
|
|
return true
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func startRecording() throws {
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
self.textHeard = ""
|
2022-02-23 04:52:00 +00:00
|
|
|
|
self.latestText = ""
|
|
|
|
|
self.sending = true
|
|
|
|
|
|
|
|
|
|
queue.addOperation {
|
|
|
|
|
self.send()
|
|
|
|
|
OperationQueue.main.addOperation { self.sending = 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-02-23 04:52:00 +00:00
|
|
|
|
self.condition.lock()
|
|
|
|
|
self.latestText = result.bestTranscription.formattedString
|
|
|
|
|
self.condition.broadcast()
|
|
|
|
|
self.condition.unlock()
|
|
|
|
|
|
|
|
|
|
OperationQueue.main.addOperation { self.textHeard = 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 {
|
|
|
|
|
if (self.listening) {
|
|
|
|
|
self.listen()
|
|
|
|
|
}
|
2022-01-12 04:30:41 +00:00
|
|
|
|
}
|
2021-10-19 04:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audioEngine.prepare()
|
|
|
|
|
try audioEngine.start()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|