2021-07-16 12:46:54 -04:00
|
|
|
|
//
|
|
|
|
|
// ContentView.swift
|
2021-09-15 22:42:53 -04:00
|
|
|
|
// ListenerGS
|
2021-07-16 12:46:54 -04:00
|
|
|
|
//
|
|
|
|
|
// Created by Jeremy Rand on 2021-07-16.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import SwiftUI
|
2021-07-16 22:58:41 -04:00
|
|
|
|
import Speech
|
2021-07-16 12:46:54 -04:00
|
|
|
|
|
|
|
|
|
struct ContentView: View {
|
2021-07-16 22:58:41 -04:00
|
|
|
|
@State private var listening = false
|
2021-07-16 23:12:01 -04:00
|
|
|
|
@State private var listenEnabled = false
|
2021-07-16 22:58:41 -04:00
|
|
|
|
@State private var textHeard = ""
|
2021-07-20 19:42:47 -04:00
|
|
|
|
@State private var log = ""
|
2021-07-16 23:12:01 -04:00
|
|
|
|
@State private var ipAddress = ""
|
|
|
|
|
@State private var isEditing = false
|
2021-07-16 22:58:41 -04:00
|
|
|
|
|
2021-07-18 15:39:56 -04:00
|
|
|
|
let LISTEN_STATE_MSG = 1
|
|
|
|
|
let LISTEN_TEXT_MSG = 2
|
|
|
|
|
|
|
|
|
|
let port = 19026
|
|
|
|
|
@State private var client: TCPClient?
|
|
|
|
|
|
2021-07-16 22:58:41 -04:00
|
|
|
|
private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!
|
|
|
|
|
|
|
|
|
|
@State private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
|
|
|
|
|
|
|
|
|
|
@State private var recognitionTask: SFSpeechRecognitionTask?
|
|
|
|
|
|
|
|
|
|
private let audioEngine = AVAudioEngine()
|
|
|
|
|
|
2021-07-16 12:46:54 -04:00
|
|
|
|
var body: some View {
|
2021-07-16 22:58:41 -04:00
|
|
|
|
VStack {
|
2021-07-16 23:12:01 -04:00
|
|
|
|
TextField("IP Address", text: $ipAddress) { isEditing in
|
|
|
|
|
self.isEditing = isEditing
|
|
|
|
|
} onCommit: {
|
|
|
|
|
validate(destination: ipAddress)
|
|
|
|
|
}
|
|
|
|
|
.padding()
|
2021-07-16 23:20:52 -04:00
|
|
|
|
|
2021-07-20 19:42:47 -04:00
|
|
|
|
ScrollView() {
|
|
|
|
|
Text(log)
|
|
|
|
|
.multilineTextAlignment(.leading)
|
|
|
|
|
}
|
2021-07-16 23:20:52 -04:00
|
|
|
|
|
2021-07-16 22:58:41 -04:00
|
|
|
|
Button("Listen") {
|
|
|
|
|
listen()
|
|
|
|
|
}
|
2021-07-16 23:20:52 -04:00
|
|
|
|
.padding()
|
2021-07-20 19:42:47 -04:00
|
|
|
|
.background(listening ? Color.red : Color.clear)
|
2021-07-16 23:20:52 -04:00
|
|
|
|
.foregroundColor(listening ? .black : .blue)
|
|
|
|
|
.disabled(listenEnabled == false)
|
|
|
|
|
.frame(maxWidth: .infinity)
|
|
|
|
|
.buttonStyle(PlainButtonStyle())
|
2021-07-16 22:58:41 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 19:42:47 -04:00
|
|
|
|
func logError(message: String) {
|
|
|
|
|
log.append("ERROR: " + message + "\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func logEvent(message: String) {
|
|
|
|
|
log.append("EVENT: " + message + "\n")
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 23:12:01 -04:00
|
|
|
|
func validate(destination : String) {
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Attempting to connect to " + destination)
|
2021-07-18 15:39:56 -04:00
|
|
|
|
client = TCPClient(address: destination, port: Int32(port))
|
|
|
|
|
guard let client = client else { return }
|
|
|
|
|
switch client.connect(timeout: 10) {
|
|
|
|
|
case .success:
|
|
|
|
|
listenEnabled = true
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Connected to " + destination)
|
2021-07-18 15:39:56 -04:00
|
|
|
|
case .failure(let error):
|
|
|
|
|
client.close()
|
|
|
|
|
self.client = nil
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logError(message: String(describing: error))
|
2021-07-18 15:39:56 -04:00
|
|
|
|
break
|
|
|
|
|
}
|
2021-07-16 23:12:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 22:58:41 -04: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 {
|
|
|
|
|
switch authStatus {
|
|
|
|
|
case .authorized:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case .denied:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case .restricted:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case .notDetermined:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
self.listening = false
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 15:39:56 -04: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
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logError(message: String(describing: error))
|
2021-07-18 15:39:56 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 22:58:41 -04:00
|
|
|
|
if (self.listening) {
|
|
|
|
|
do {
|
|
|
|
|
try startRecording()
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Listening...")
|
2021-07-16 22:58:41 -04:00
|
|
|
|
}
|
|
|
|
|
catch {
|
2021-07-18 15:39:56 -04:00
|
|
|
|
self.listening = false
|
2021-07-16 22:58:41 -04:00
|
|
|
|
}
|
2021-07-18 15:39:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!self.listening) {
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Listening stopped")
|
2021-07-16 22:58:41 -04:00
|
|
|
|
audioEngine.stop()
|
|
|
|
|
recognitionRequest?.endAudio()
|
2021-07-18 15:39:56 -04:00
|
|
|
|
switch (client.send(data: isListening())) {
|
|
|
|
|
case .success:
|
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
self.listening = false
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logError(message: String(describing: error))
|
2021-07-18 15:39:56 -04:00
|
|
|
|
}
|
2021-07-16 22:58:41 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 15:39:56 -04:00
|
|
|
|
|
|
|
|
|
private func isListening() -> Data {
|
|
|
|
|
return pack("<hh", [LISTEN_STATE_MSG, listening ? 1 : 0])
|
|
|
|
|
}
|
2021-07-16 22:58:41 -04:00
|
|
|
|
|
2021-07-18 15:39:56 -04:00
|
|
|
|
private func send(latestText : String) {
|
2021-07-18 16:13:07 -04:00
|
|
|
|
guard let client = client else { return }
|
|
|
|
|
var commonChars = self.textHeard.count
|
|
|
|
|
while (commonChars > 0) {
|
|
|
|
|
if (latestText.prefix(commonChars) == self.textHeard.prefix(commonChars)) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
commonChars -= 1
|
|
|
|
|
}
|
|
|
|
|
var stringToSend = ""
|
|
|
|
|
if (commonChars < self.textHeard.count) {
|
2021-07-20 19:04:38 -04:00
|
|
|
|
stringToSend = String(repeating: "\u{7f}", count: self.textHeard.count - commonChars)
|
2021-07-18 16:13:07 -04:00
|
|
|
|
}
|
2021-07-20 19:59:33 -04:00
|
|
|
|
stringToSend.append(contentsOf: latestText.suffix(latestText.count - commonChars).replacingOccurrences(of: "\n", with: "\r"))
|
2021-07-18 15:39:56 -04:00
|
|
|
|
|
2021-07-18 16:13:07 -04:00
|
|
|
|
if (stringToSend.count > 0) {
|
|
|
|
|
// TODO - Handle strings to send that are longer than 64K (doubt that would happen though)
|
|
|
|
|
// TODO - Try to convert encoding from utf8 to something the GS can understand.
|
|
|
|
|
switch (client.send(data: pack("<hh\(stringToSend.count)s", [LISTEN_TEXT_MSG, stringToSend.count, stringToSend]))) {
|
|
|
|
|
case .success:
|
|
|
|
|
self.textHeard = latestText
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Sent \"" + stringToSend + "\"")
|
2021-07-18 16:13:07 -04:00
|
|
|
|
break
|
|
|
|
|
case .failure(let error):
|
|
|
|
|
self.listening = false
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logError(message: String(describing: error))
|
2021-07-18 16:13:07 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-18 15:39:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-16 22:58:41 -04: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
|
|
|
|
|
|
|
|
|
|
// Create and configure the speech recognition request.
|
|
|
|
|
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
|
|
|
|
|
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to create a SFSpeechAudioBufferRecognitionRequest object") }
|
|
|
|
|
recognitionRequest.shouldReportPartialResults = true
|
|
|
|
|
|
|
|
|
|
// Keep speech recognition data on device
|
|
|
|
|
if #available(iOS 13, *) {
|
|
|
|
|
recognitionRequest.requiresOnDeviceRecognition = false
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-18 15:39:56 -04:00
|
|
|
|
self.textHeard = ""
|
|
|
|
|
|
2021-07-16 22:58:41 -04: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.
|
2021-07-18 15:39:56 -04:00
|
|
|
|
send(latestText: result.bestTranscription.formattedString)
|
2021-07-16 22:58:41 -04:00
|
|
|
|
isFinal = result.isFinal
|
|
|
|
|
print("Text \(result.bestTranscription.formattedString)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if error != nil || isFinal {
|
|
|
|
|
// Stop recognizing speech if there is a problem.
|
|
|
|
|
self.audioEngine.stop()
|
|
|
|
|
inputNode.removeTap(onBus: 0)
|
|
|
|
|
|
|
|
|
|
self.recognitionRequest = nil
|
|
|
|
|
self.recognitionTask = nil
|
|
|
|
|
self.listening = false
|
2021-07-20 19:42:47 -04:00
|
|
|
|
logEvent(message: "Listening stopped")
|
2021-07-20 19:14:08 -04:00
|
|
|
|
guard let client = client else { return }
|
|
|
|
|
client.send(data: isListening())
|
2021-07-16 22:58:41 -04: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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
audioEngine.prepare()
|
|
|
|
|
try audioEngine.start()
|
2021-07-16 12:46:54 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
|
static var previews: some View {
|
|
|
|
|
ContentView()
|
|
|
|
|
}
|
|
|
|
|
}
|