Modern Apps git

← Modern-Apps.git

speech: a failed utterance no longer reprograms the loaded voice

Commit
39a8c98e18a99b78c449e3fe4ac7795a9b9f8874
Author
Vayun Mathur <[email protected]>
When
2026-12-01 08:30 UTC (5 days ago)
Parents
e8d2dbd
onSynthesizeText committed the request's language/voice to
the loaded selection before checking preload, voice-apply
and callback.start. The recent per-field-validation fix made
unsupported requests leave the selection untouched, but
every other failure path still mutated it: load de-DE-x-M3,
synthesize fr/fr-FR-x-F2 with preload, voice-apply or start
failing โ€” the loaded selection was already French despite
zero audio produced, and a subsequent unnamed utterance
spoke French/F2 instead of German/M3.

The selection is now computed as locals and committed only
once the utterance has actually started (the blank-text path
commits as it completes); a started utterance that later
fails mid-audio keeps its selection, as before. Three
regression tests cover the three failure points and the
follow-up unnamed utterance (failed pre-fix). Queue/stop,
Whisper lifecycle and validation variants were audited and
cleared. Verified: :speech:testDevUnitTest 111 tests pass;
:speech:lint passes.

Diff

speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt
diff --git a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.ktindex dd3b2db6c9..084b4c0df6 100644--- a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt+++ b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt@@ -197,8 +197,17 @@ open class SupertonicTtsService : TextToSpeechService() {             callback.error()             return         }-        requested?.let { language = it }-        requestedStyle?.let { voice = it }+        // The selection this request will speak in, not yet the loaded one: a+        // request that goes on to fail โ€” the engine cannot come up, cannot+        // take the voice, or the callback refuses the start โ€” must leave the+        // loaded selection untouched, exactly as an unsupported request does+        // above, or the next request naming nothing would silently speak in+        // a language and voice whose only utterance never happened. The+        // selection is committed only once the utterance has actually+        // started, below; the engine is re-offered the loaded voice on every+        // synthesis, so a start failure here leaves nothing to undo there.+        val selectedLanguage = requested ?: language+        val selectedVoice = requestedStyle ?: voice          val rate = engine.sampleRate         val text = request.charSequenceText?.toString().orEmpty()@@ -206,19 +215,23 @@ open class SupertonicTtsService : TextToSpeechService() {             // Still start and finish: a caller that queued an empty utterance is waiting for its             // completion callback, and erroring would surface as a failure it did not cause.             callback.start(rate, AudioFormat.ENCODING_PCM_16BIT, 1)+            language = selectedLanguage+            voice = selectedVoice             callback.done()             return         }-        if (!engine.preload() || !engine.voice(voice)) {+        if (!engine.preload() || !engine.voice(selectedVoice)) {             callback.error()             return         }         if (callback.start(rate, AudioFormat.ENCODING_PCM_16BIT, 1) != TextToSpeech.SUCCESS) {             return         }+        language = selectedLanguage+        voice = selectedVoice          val maxBytes = callback.maxBufferSize-        val ok = engine.synthesize(text, language.code) { samples ->+        val ok = engine.synthesize(text, selectedLanguage.code) { samples ->             if (stopped) return@synthesize false             val pcm = floatsToPcm16(samples)             var offset = 0
speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt
diff --git a/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt b/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.ktindex b66e82de14..25bd48c724 100644--- a/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt+++ b/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt@@ -288,6 +288,70 @@ class SupertonicTtsServiceTest {         assertEquals(startsBefore, cb.startCalls.size)     } +    @Test+    fun synthesizePreloadFailureLeavesLoadedSelectionUntouched() {+        val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+        assertEquals(TextToSpeech.SUCCESS, s.loadVoice("de-DE-x-M3"))+        fake.preloadOk = false+        val cb = RecordingCallback()+        s.synthesize(+            requestWith("Bonjour.", language = "fr", country = "FR", voiceName = "fr-FR-x-F2"),+            cb,+        )+        assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+        // The failed request must not become the loaded selection: a later+        // request naming nothing still speaks German in M3, not French in F2.+        assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+        fake.preloadOk = true+        fake.synthCalls.clear(); fake.voicesSeen.clear()+        val cb2 = RecordingCallback()+        s.synthesize(SynthesisRequest("Hallo.", Bundle()), cb2)+        assertEquals(1, cb2.doneCalls); assertEquals(0, cb2.errorCalls)+        assertEquals(listOf("Hallo." to "de"), fake.synthCalls)+        assertEquals(listOf("M3"), fake.voicesSeen)+    }++    @Test+    fun synthesizeVoiceFailureLeavesLoadedSelectionUntouched() {+        val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+        assertEquals(TextToSpeech.SUCCESS, s.loadVoice("de-DE-x-M3"))+        fake.voiceOk = false+        val cb = RecordingCallback()+        s.synthesize(+            requestWith("Bonjour.", language = "fr", country = "FR", voiceName = "fr-FR-x-F2"),+            cb,+        )+        assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+        assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+        fake.voiceOk = true+        fake.synthCalls.clear(); fake.voicesSeen.clear()+        val cb2 = RecordingCallback()+        s.synthesize(SynthesisRequest("Hallo.", Bundle()), cb2)+        assertEquals(1, cb2.doneCalls); assertEquals(0, cb2.errorCalls)+        assertEquals(listOf("Hallo." to "de"), fake.synthCalls)+        assertEquals(listOf("M3"), fake.voicesSeen)+    }++    @Test+    fun synthesizeStartFailureLeavesLoadedSelectionUntouched() {+        val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+        assertEquals(TextToSpeech.SUCCESS, s.loadVoice("de-DE-x-M3"))+        val cb = RecordingCallback(startResult = TextToSpeech.ERROR)+        s.synthesize(+            requestWith("Bonjour.", language = "fr", country = "FR", voiceName = "fr-FR-x-F2"),+            cb,+        )+        assertEquals(0, cb.doneCalls); assertEquals(0, cb.errorCalls)+        assertTrue(fake.synthCalls.isEmpty())+        assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+        fake.synthCalls.clear(); fake.voicesSeen.clear()+        val cb2 = RecordingCallback()+        s.synthesize(SynthesisRequest("Hallo.", Bundle()), cb2)+        assertEquals(1, cb2.doneCalls); assertEquals(0, cb2.errorCalls)+        assertEquals(listOf("Hallo." to "de"), fake.synthCalls)+        assertEquals(listOf("M3"), fake.voicesSeen)+    }+     @Test     fun synthesizeAudioFailureErrors() {         val (s, _) = fakeService { chunks = listOf(floatArrayOf(0.5f, 0.5f)) }