Skip to content

Commit

Permalink
Merge pull request #169 from harmony-one/fix-tap-to-speak
Browse files Browse the repository at this point in the history
Fix the logic when a user resets the speech recognition
  • Loading branch information
theofandrich authored Nov 13, 2023
2 parents 9932bcb + e15b9d5 commit 5ad9ca0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 6 additions & 3 deletions voice/voice-ai/x/Actions/ActionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion voice/voice-ai/x/Actions/ActionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions voice/voice-ai/x/SpeechRecognition/SpeechRecognition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol SpeechRecognitionProtocol {
func pause(feedback: Bool?)
func repeate()
func speak()
func stopSpeak()
func stopSpeak(cancel: Bool?)
func sayMore()
func cancelSpeak()
}
Expand All @@ -25,6 +25,10 @@ extension SpeechRecognitionProtocol {
func pause() {
pause(feedback: true)
}

func stopSpeak() {
stopSpeak(cancel: false)
}
}

class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol {
Expand Down Expand Up @@ -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
}
Expand All @@ -418,6 +422,7 @@ class SpeechRecognition: NSObject, ObservableObject, SpeechRecognitionProtocol {
recognitionRequest?.endAudio()
audioEngine.inputNode.removeTap(onBus: 0)
audioEngine.stop()
print("stop")
}

func resumeCapturing() {
Expand Down Expand Up @@ -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]")

Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit 5ad9ca0

Please sign in to comment.