speech: TTS fails loudly on unsupported language/voice requests
- Commit
e462ba26eb0e5f0aa70eec0360df26e3b7f7b91d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 21:36 UTC (5 days ago)
- Parents
3d8850f
Synthesis treated 'request names something we don't have' identically to 'request names nothing': a request for Thai (deliberately unbundled — onIsLanguageAvailable reports NOT SUPPORTED) silently reused the previously loaded German voice and reported success, and a bogus voice name that onIsValidVoiceName itself rejects was spoken in whatever voice was loaded. The system could hear confidently wrong-language audio marked as a successful synthesis. onSynthesizeText now validates before mutating any state: a request naming an unresolvable language/voice, or carrying a rejected -x- style, errors without touching the loaded selection. Requests naming nothing still keep it; a valid per-request language still overrides. Three regression tests (unsupported language errors, bogus voice errors, valid override intact) — the first two failed pre-fix. Whisper paths and voice resolution were audited and cleared. Verified: :speech:testDevUnitTest 106 tests pass; lint 0 errors, warnings pre-existing elsewhere.
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 ad59f920e9..276101d1f1 100644--- a/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt+++ b/speech/src/main/java/com/vayunmathur/speech/service/SupertonicTtsService.kt@@ -166,13 +166,28 @@ open class SupertonicTtsService : TextToSpeechService() { callback.error() return }- SupertonicVoices.resolve(+ // A request that names a language or voice this bundle does not have is an+ // error, not a cue to reuse the previous utterance's selection: synthesising+ // Thai text in the loaded German voice and reporting success is worse than+ // failing, because the caller has no way to tell it heard the wrong language.+ // A request that names nothing keeps the loaded selection, as before.+ val requested = SupertonicVoices.resolve( lang = request.language, country = request.country, variant = request.variant, voiceName = request.voiceName,- )?.let { language = it }- SupertonicVoices.styleIn(request.voiceName)?.let { voice = it }+ )+ val requestedStyle = SupertonicVoices.styleIn(request.voiceName)+ val namesUnsupportedLanguage = requested == null &&+ (!request.language.isNullOrEmpty() || !request.voiceName.isNullOrEmpty())+ val namesUnsupportedStyle = requestedStyle == null &&+ SupertonicVoices.namesAStyle(request.voiceName)+ if (namesUnsupportedLanguage || namesUnsupportedStyle) {+ callback.error()+ return+ }+ requested?.let { language = it }+ requestedStyle?.let { voice = it } val rate = engine.sampleRate val text = request.charSequenceText?.toString().orEmpty()
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 4a05d2d478..f705d01340 100644--- a/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt+++ b/speech/src/test/java/com/vayunmathur/speech/service/SupertonicTtsServiceTest.kt@@ -320,6 +320,73 @@ class SupertonicTtsServiceTest { assertEquals(2, cb.audio.size) } + /**+ * A framework-populated request: the public constructor leaves the language/voice+ * fields null, so set them the way the framework's parcel path does.+ */+ private fun requestWith(+ text: String,+ language: String? = null,+ country: String? = null,+ variant: String? = null,+ voiceName: String? = null,+ ): SynthesisRequest {+ val request = SynthesisRequest(text, Bundle())+ fun setField(name: String, value: String?) {+ SynthesisRequest::class.java.getDeclaredField(name)+ .apply { isAccessible = true }+ .set(request, value)+ }+ setField("mLanguage", language)+ setField("mCountry", country)+ setField("mVariant", variant)+ setField("mVoiceName", voiceName)+ return request+ }++ @Test+ fun synthesizeUnsupportedLanguageErrorsInsteadOfReusingLoadedLanguage() {+ val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+ assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, s.loadLanguage("de", "DE", null))+ val cb = RecordingCallback()+ // Thai is not in the bundle: onIsLanguageAvailable("th", …) is LANG_NOT_SUPPORTED,+ // so synthesising it must not silently speak the text in the loaded German.+ s.synthesize(requestWith("Sawasdee", language = "th", country = "TH"), cb)+ assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+ assertTrue(cb.startCalls.isEmpty())+ assertTrue(fake.synthCalls.isEmpty(), "synthCalls=${fake.synthCalls}")+ // The loaded language is untouched by the rejected request.+ assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+ }++ @Test+ fun synthesizeBogusVoiceStyleErrorsInsteadOfReusingLoadedVoice() {+ 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("en-US-x-BOGUS") is ERROR, so synthesis must not accept+ // the name silently and speak English in the previously loaded M3 voice.+ s.synthesize(+ requestWith("Hello.", language = "en", country = "US", voiceName = "en-US-x-BOGUS"),+ cb,+ )+ assertEquals(1, cb.errorCalls); assertEquals(0, cb.doneCalls)+ assertTrue(fake.synthCalls.isEmpty(), "synthCalls=${fake.synthCalls}")+ assertTrue(fake.voicesSeen.isEmpty(), "voicesSeen=${fake.voicesSeen}")+ assertEquals(arrayOf("deu", "DEU", "").toList(), s.language().toList())+ }++ @Test+ fun synthesizeRequestLanguageOverridesLoadedLanguage() {+ val (s, fake) = fakeService { chunks = listOf(floatArrayOf(0.5f)) }+ assertEquals(TextToSpeech.LANG_COUNTRY_AVAILABLE, s.loadLanguage("de", "DE", null))+ val cb = RecordingCallback()+ s.synthesize(requestWith("Bonjour.", language = "fr", country = "FR"), cb)+ assertEquals(1, cb.doneCalls); assertEquals(0, cb.errorCalls)+ assertEquals(listOf("Bonjour." to "fr"), fake.synthCalls)+ }+ @Test fun destroyShutsEngineDown() { val controller = Robolectric.buildService(Harness::class.java).create()