Skip to content

Commit

Permalink
hack(tts): treat 'speed = 1' as 'use system'
Browse files Browse the repository at this point in the history
The Anki backend sends `speed` as a non-nullable float
The default is '1'

But, using `setSpeechRate(1)` overrides the android system
speech rate

So we ignore all values of '1' coming from the backend
and treat them as 'use system'

Fixes 15598
  • Loading branch information
david-allison authored and lukstbit committed Feb 21, 2024
1 parent c17fed6 commit 9f49124
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
6 changes: 4 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/AndroidTtsPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ class AndroidTtsPlayer(private val context: Context, private val voices: List<Tt
suspendCancellableCoroutine { continuation ->
val tts = tts?.also {
it.voice = voice.voice
if (it.setSpeechRate(tag.speed) == ERROR) {
return@suspendCancellableCoroutine continuation.resume(AndroidTtsError.failure(TtsErrorCode.APP_SPEECH_RATE_FAILED))
tag.speed?.let { speed ->
if (it.setSpeechRate(speed) == ERROR) {
return@suspendCancellableCoroutine continuation.resume(AndroidTtsError.failure(TtsErrorCode.APP_SPEECH_RATE_FAILED))
}
}
// if it's already playing: stop it
it.stopPlaying()
Expand Down
4 changes: 3 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/Sound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import java.util.regex.Pattern

/**
* Records information about a text to speech tag.
*
* @param speed speed of speech, where `1.0f` is normal speed. `null`: use system
*/
data class TTSTag(
val fieldText: String,
Expand All @@ -40,7 +42,7 @@ data class TTSTag(
*/
val lang: String,
val voices: List<String>,
val speed: Float,
val speed: Float?,
/** each arg should be in the form 'foo=bar' */
val otherArgs: List<String>
) : AvTag()
Expand Down
5 changes: 4 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/TemplateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ class TemplateManager {
lang = tag.tts.lang,
voices = tag.tts.voicesList,
otherArgs = tag.tts.otherArgsList,
speed = tag.tts.speed
// The backend currently sends speed = 1, even when undefined.
// We agreed that '1' should be classed as 'use system' and ignored
// https://github.com/ankidroid/Anki-Android/issues/15598#issuecomment-1953653639
speed = tag.tts.speed.let { if (it == 1f) null else it }
)
}
}
Expand Down

0 comments on commit 9f49124

Please sign in to comment.