diff --git a/voice/voice-ai/x/Actions/ActionHandler.swift b/voice/voice-ai/x/Actions/ActionHandler.swift index 502bfa245..fdbdae9ec 100644 --- a/voice/voice-ai/x/Actions/ActionHandler.swift +++ b/voice/voice-ai/x/Actions/ActionHandler.swift @@ -65,13 +65,16 @@ class ActionHandler: ObservableObject { let resetTapSpeakState = isTapToSpeakActive && !isItTapToSpeakAction if resetTapSpeakState { isTapToSpeakActive = false - speechRecognition.cancelSpeak() + lastRecordingStateChangeTime = 0 + stopRecording(cancel: true) } } func handle(actionType: ActionType) { syncTapToSpeakState(actionType) + print("Run action \(actionType)") + switch actionType { case .reset: resetThrottler.send() @@ -126,11 +129,11 @@ class ActionHandler: ObservableObject { SpeechRecognition.shared.speak() } - func stopRecording() { + func stopRecording(cancel: Bool = false) { if isRecording { isRecording = false print("Stopped Recording") - speechRecognition.stopSpeak() + speechRecognition.stopSpeak(cancel: cancel) // Simulating delay before triggering a synthesizing state change DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.isSynthesizing = true diff --git a/voice/voice-ai/x/Actions/ActionsView.swift b/voice/voice-ai/x/Actions/ActionsView.swift index d17c8739b..69055fc6c 100644 --- a/voice/voice-ai/x/Actions/ActionsView.swift +++ b/voice/voice-ai/x/Actions/ActionsView.swift @@ -224,8 +224,10 @@ struct ActionsView: View { GridButton(currentTheme: currentTheme, button: button, foregroundColor: .black, active: isSpeakButtonPressed, isPressed: isPressed) {}.simultaneousGesture( DragGesture(minimumDistance: 0) .onChanged { _ in + if (self.isSpeakButtonPressed == false) { + actionHandler.handle(actionType: ActionType.speak) + } self.isSpeakButtonPressed = true - actionHandler.handle(actionType: ActionType.speak) } .onEnded { _ in self.isSpeakButtonPressed = false diff --git a/voice/voice-ai/x/SpeechRecognition/SpeechRecognition.swift b/voice/voice-ai/x/SpeechRecognition/SpeechRecognition.swift index 74617a6b1..eabbf01fa 100644 --- a/voice/voice-ai/x/SpeechRecognition/SpeechRecognition.swift +++ b/voice/voice-ai/x/SpeechRecognition/SpeechRecognition.swift @@ -12,7 +12,7 @@ protocol SpeechRecognitionProtocol { func pause(feedback: Bool?) func repeate() func speak() - func stopSpeak() + func stopSpeak(cancel: Bool?) func sayMore() func cancelSpeak() } @@ -25,6 +25,10 @@ extension SpeechRecognitionProtocol { func pause() { pause(feedback: true) } + + func stopSpeak() { + stopSpeak(cancel: false) + } } class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol { @@ -400,7 +404,7 @@ class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol { handleQuery(retryCount: maxRetry) } - func pauseCapturing(cancel: Bool = false) { + func pauseCapturing(cancel: Bool? = false) { guard isCapturing else { return } @@ -418,6 +422,7 @@ class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol { recognitionRequest?.endAudio() audioEngine.inputNode.removeTap(onBus: 0) audioEngine.stop() + print("stop") } func resumeCapturing() { @@ -627,7 +632,7 @@ class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol { } // Refactored 'stopSpeak' function to finalize speech recognition - func stopSpeak() { + func stopSpeak(cancel: Bool? = false) { DispatchQueue.global(qos: .userInitiated).async { print("[SpeechRecognition][stopSpeak]") @@ -654,7 +659,7 @@ class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol { // Pause capturing after handling the recognized text DispatchQueue.main.async { - self.pauseCapturing() + self.pauseCapturing(cancel: cancel) } } }