Skip to content

Commit

Permalink
fix speechrecognizer dictation hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Nov 12, 2024
1 parent 0b5f79c commit c76445d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
})
speechRecognizer.startListening(intent)
awaitClose {

speechRecognizer.destroy()
}
}.flowOn(Dispatchers.Main)

Expand Down Expand Up @@ -222,6 +222,7 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
Logging.d("Speech recognition results: ${status.results}")
if (status.results.firstOrNull()?.second?.isBlank() != false) {
emit(DictationServiceResponse.Transcription(emptyList()))
emit(DictationServiceResponse.Complete)
return@collect
}
emit(DictationServiceResponse.Transcription(
Expand All @@ -240,7 +241,6 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
} finally {
//audioTrack.stop()
audioJob.cancel()
speechRecognizer.destroy()
decoder.close()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,65 +54,63 @@ class VoiceSessionHandler(
.takeWhile { it !is DictationServiceResponse.Complete }
.onEach {
Logging.v("DictationServiceResponse: $it")
withTimeout(1.minutes) {
when (it) {
is DictationServiceResponse.Ready -> {
pebbleDevice.activeVoiceSession.value = voiceSession
val result = SessionSetupResult(
sessionType = SessionType.Dictation,
result = Result.Success
)
if (appInitiated) {
result.flags.set(1u)
}
pebbleDevice.voiceService.send(result)
sentReady = true
}
is DictationServiceResponse.Error -> {
val result = if (sentReady) {
DictationResult(
voiceSession.sessionId.toUShort(),
it.result,
buildList {
if (appInitiated && voiceSession.appUuid != null) {
add(VoiceAttribute.AppUuid().apply {
uuid.set(voiceSession.appUuid)
})
}
}
)
} else {
SessionSetupResult(
sessionType = SessionType.Dictation,
result = it.result
)
}
if (appInitiated) {
result.flags.set(1u)
}
pebbleDevice.voiceService.send(result)
when (it) {
is DictationServiceResponse.Ready -> {
pebbleDevice.activeVoiceSession.value = voiceSession
val result = SessionSetupResult(
sessionType = SessionType.Dictation,
result = Result.Success
)
if (appInitiated) {
result.flags.set(1u)
}
is DictationServiceResponse.Transcription -> {
val resp = DictationResult(
pebbleDevice.voiceService.send(result)
sentReady = true
}
is DictationServiceResponse.Error -> {
val result = if (sentReady) {
DictationResult(
voiceSession.sessionId.toUShort(),
Result.Success,
it.result,
buildList {
add(makeTranscription(it.sentences))
if (appInitiated && voiceSession.appUuid != null) {
add(VoiceAttribute(
id = VoiceAttributeType.AppUuid.value,
content = VoiceAttribute.AppUuid().apply {
uuid.set(voiceSession.appUuid)
}
))
add(VoiceAttribute.AppUuid().apply {
uuid.set(voiceSession.appUuid)
})
}
}
)
if (appInitiated) {
resp.flags.set(1u)
}
pebbleDevice.voiceService.send(resp)
} else {
SessionSetupResult(
sessionType = SessionType.Dictation,
result = it.result
)
}
if (appInitiated) {
result.flags.set(1u)
}
pebbleDevice.voiceService.send(result)
}
is DictationServiceResponse.Transcription -> {
val resp = DictationResult(
voiceSession.sessionId.toUShort(),
Result.Success,
buildList {
add(makeTranscription(it.sentences))
if (appInitiated && voiceSession.appUuid != null) {
add(VoiceAttribute(
id = VoiceAttributeType.AppUuid.value,
content = VoiceAttribute.AppUuid().apply {
uuid.set(voiceSession.appUuid)
}
))
}
}
)
if (appInitiated) {
resp.flags.set(1u)
}
pebbleDevice.voiceService.send(resp)
}
}
}
Expand Down Expand Up @@ -170,9 +168,7 @@ class VoiceSessionHandler(
val dictationService: DictationService by inject()
val voiceSession = VoiceSession(appUuid, message.sessionId.get().toInt(), encoderInfo, dictationService)
Logging.d("Received voice session: $voiceSession")
coroutineScope {
launch { handleSpeechStream(voiceSession) }
}
handleSpeechStream(voiceSession)
}
}

Expand Down

0 comments on commit c76445d

Please sign in to comment.