Modern Apps git

← Modern-Apps.git

speech: an unsupported voice or language in a request now errors instead of speaking the wrong one

Commit
7d876007b14fa53ae8341b6a63a20565a8998a13
Author
Vayun Mathur <[email protected]>
When
2026-12-01 04:26 UTC (5 days ago)
Parents
5fe7bc8
onSynthesizeText validated only the combined resolve()
result, and resolve falls through from an unresolvable field
to a valid one: a request naming an unsupported voice with a
valid language (loaded de-DE-x-M3; en/US + th-TH-x-F1) spoke
English and reported done(), though onIsValidVoiceName
returns ERROR for that voice — Thai is deliberately
unbundled. The mirror case (language=th with an en voice)
also spoke the wrong language successfully.

Language and voice are now validated independently before
any state mutation; if either names something unresolvable,
callback.error() fires with the loaded selection untouched.
Requests naming nothing still keep the loaded selection, and
a valid per-request language still overrides. Two regression
tests failed pre-fix on both directions and assert no engine
call and unchanged selection. resolve itself is untouched.
Verified: :speech:testDevUnitTest 108 tests and lint pass.

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 276101d1f1..dd3b2db6c9 100644--- a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt+++ b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt@@ -178,11 +178,22 @@ open class SupertonicTtsService : TextToSpeechService() {             voiceName = request.voiceName,         )         val requestedStyle = SupertonicVoices.styleIn(request.voiceName)-        val namesUnsupportedLanguage = requested == null &&-            (!request.language.isNullOrEmpty() || !request.voiceName.isNullOrEmpty())+        // Each named field is validated on its own, not only through the+        // combined resolve above: resolve falls through from an unresolvable+        // voice name to a valid language (and vice versa), so the combined+        // result alone would mask a request naming, say, a Thai voice+        // alongside an English language field and speak it in English.+        val namesUnsupportedLanguage = !request.language.isNullOrEmpty() &&+            SupertonicVoices.resolve(+                lang = request.language,+                country = request.country,+                variant = request.variant,+            ) == null+        val namesUnsupportedVoice = !request.voiceName.isNullOrEmpty() &&+            SupertonicVoices.resolve(voiceName = request.voiceName) == null         val namesUnsupportedStyle = requestedStyle == null &&             SupertonicVoices.namesAStyle(request.voiceName)-        if (namesUnsupportedLanguage || namesUnsupportedStyle) {+        if (namesUnsupportedLanguage || namesUnsupportedVoice || namesUnsupportedStyle) {             callback.error()             return         }
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 f705d01340..b66e82de14 100644--- a/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt+++ b/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt@@ -377,6 +377,43 @@ class SupertonicTtsServiceTest {         assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())     } +    @Test+    fun synthesizeUnsupportedVoiceNameErrorsEvenWhenLanguageIsValid() {+        val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+        assertEquals(TextToSpeech.SUCCESS, s.loadVoice("de-DE-x-M3"))+        fake.voicesSeen.clear()+        val cb = RecordingCallback()+        // onIsValidVoiceName("th-TH-x-F1") is ERROR (Thai is not bundled), so a+        // synthesis request naming that voice must not fall through to its+        // valid language field and speak English in the loaded voice.+        s.synthesize(+            requestWith("Hello.", language = "en", country = "US", voiceName = "th-TH-x-F1"),+            cb,+        )+        assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+        assertTrue(cb.startCalls.isEmpty())+        assertTrue(fake.synthCalls.isEmpty(), "synthCalls=${fake.synthCalls}")+        assertTrue(fake.voicesSeen.isEmpty(), "voicesSeen=${fake.voicesSeen}")+        assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+    }++    @Test+    fun synthesizeUnsupportedLanguageErrorsEvenWhenVoiceNameIsValid() {+        val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+        assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, s.loadLanguage("de", "DE", null))+        val cb = RecordingCallback()+        // The mirror image: a valid voice name must not mask a language field+        // naming a language this bundle does not have.+        s.synthesize(+            requestWith("Sawasdee", language = "th", country = "TH", voiceName = "en-US-x-F1"),+            cb,+        )+        assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+        assertTrue(cb.startCalls.isEmpty())+        assertTrue(fake.synthCalls.isEmpty(), "synthCalls=${fake.synthCalls}")+        assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+    }+     @Test     fun synthesizeRequestLanguageOverridesLoadedLanguage() {         val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }