Skip to content

Commit

Permalink
empty response when text is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Oct 31, 2024
1 parent 014c8f5 commit 70f9d88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.*
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.nio.ByteBuffer
import java.nio.ShortBuffer


@RequiresApi(VERSION_CODES.TIRAMISU)
Expand All @@ -28,7 +29,7 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
private val scope = CoroutineScope(Dispatchers.IO)

sealed class SpeechRecognizerStatus {
object Ready: SpeechRecognizerStatus()
data object Ready: SpeechRecognizerStatus()
class Error(val error: Int): SpeechRecognizerStatus()
class Results(val results: List<Pair<Float, String>>): SpeechRecognizerStatus()
}
Expand Down Expand Up @@ -143,11 +144,11 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
speechRecognizer.stopListening()
}
} else if (frame is AudioStreamFrame.AudioData) {
decodedBuf.rewind()
val result = decoder.decodeFrame(frame.data, decodedBuf, hasHeaderByte = true)
if (result != SpeexDecodeResult.Success) {
Logging.e("Speex decode error: ${result.name}")
}
decodedBuf.rewind()
recognizerWritePipe.write(decodedBuf.array(), decodedBuf.arrayOffset(), decodeBufLength)
}
}
Expand All @@ -173,6 +174,10 @@ class SpeechRecognizerDictationService: DictationService, KoinComponent {
}
is SpeechRecognizerStatus.Results -> {
Logging.d("Speech recognition results: ${status.results}")
if (status.results.firstOrNull()?.second?.isBlank() != false) {
emit(DictationServiceResponse.Transcription(emptyList()))
return@collect
}
emit(DictationServiceResponse.Transcription(
listOf(
buildList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,28 @@ class VoiceSessionHandler(
sentReady = true
}
is DictationServiceResponse.Error -> {
if (sentReady) {
pebbleDevice.voiceService.send(DictationResult(
val result = if (sentReady) {
DictationResult(
voiceSession.sessionId.toUShort(),
it.result,
emptyList()
))
buildList {
if (appInitiated && voiceSession.appUuid != null) {
add(VoiceAttribute.AppUuid().apply {
uuid.set(voiceSession.appUuid)
})
}
}
)
} else {
pebbleDevice.voiceService.send(SessionSetupResult(
SessionSetupResult(
sessionType = SessionType.Dictation,
result = it.result
))
)
}
if (appInitiated) {
result.flags.set(1u)
}
pebbleDevice.voiceService.send(result)
}
is DictationServiceResponse.Transcription -> {
val resp = DictationResult(
Expand Down

0 comments on commit 70f9d88

Please sign in to comment.