Android: fall back to available voice when requested language is filtered out#2387
Open
Android: fall back to available voice when requested language is filtered out#2387
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the Android TextToSpeechService implementation to avoid silent synthesis failures when assistive technologies request a language that’s not present in the user’s filtered voice set, by falling back to an available voice instead of hard-failing.
Changes:
- Treat
LANG_MISSING_DATAas a hard failure inselectVoice(). - On
LANG_NOT_SUPPORTED, fall back to an available voice (reusemMatchingVoiceif already set; otherwise pick one from the filtered set).
Comments suppressed due to low confidence (1)
android/src/com/reecedunn/espeak/TtsService.java:326
selectVoice(SynthesisRequest)is currently not called anywhere inTtsService(no references in this class), so this new fallback logic will never run and won’t address the reported “fresh start” failure case wheremMatchingVoiceis null. To make the fix effective, either invokeselectVoice(request)from the synthesis path (e.g., at the start ofonSynthesizeText) or move this fallback intoonLoadLanguage, which is part of the framework’s voice-selection API surface.
private int selectVoice(SynthesisRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final String name = request.getVoiceName();
if (name != null && !name.isEmpty()) {
return onLoadVoice(name);
}
}
final int result = onLoadLanguage(request.getLanguage(), request.getCountry(), request.getVariant());
switch (result) {
case TextToSpeech.LANG_MISSING_DATA:
return TextToSpeech.ERROR;
case TextToSpeech.LANG_NOT_SUPPORTED:
// When the requested language is not in the user's selected set,
// fall back to an available voice instead of failing. This allows
// screen readers that request the system language (e.g. Jieshuo)
// to still work when the user has selected only other languages.
synchronized (mAvailableVoices) {
if (!mAvailableVoices.isEmpty()) {
if (mMatchingVoice == null) {
mMatchingVoice = mAvailableVoices.values().iterator().next();
}
return TextToSpeech.SUCCESS;
}
}
return TextToSpeech.ERROR;
}
return TextToSpeech.SUCCESS;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ac723fa to
912ab51
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
android/eSpeakTests/src/com/reecedunn/espeak/test/TextToSpeechServiceTest.java
Show resolved
Hide resolved
912ab51 to
3957bac
Compare
…ered out Screen readers like Jieshuo request the system language for synthesis. When the system language (e.g. English) is not in the user's language selector but another language (e.g. Russian) is, synthesis would fail silently. Fall back to an available voice instead of returning an error, so the engine always synthesizes in a user-selected language regardless of what the screen reader requests. Co-Authored-By: Claude <noreply@anthropic.com>
3957bac to
2ed8e8a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem
On an English-system Android device with eSpeak's language selector set to Russian only:
onLoadLanguage("eng", ...)based on the system languageLANG_NOT_SUPPORTEDselectVoice()returnsERROR→mMatchingVoicestaysnull→ synthesis silently failsTalkBack works because it calls
onGetVoices()first and picks an available voice.Fix
In
selectVoice(), when the requested language returnsLANG_NOT_SUPPORTED:mMatchingVoiceis already set from a previous load, keep it (reuse the last working voice)mMatchingVoiceis null (fresh start), pick the first available voice from the filtered setLANG_MISSING_DATAstill fails hard (no data installed at all)This effectively "pins" the engine to the user's selected language(s) regardless of what the screen reader requests.
Test plan
./gradlew assembleDebug)ctest --test-dir build)