Skip to content

Fix TTS fast-forward delay and queue saturation on skip - #20

Closed
MrGodFavourite wants to merge 16 commits into
HnDK0:mainfrom
MrGodFavourite:main
Closed

MrGodFavourite wants to merge 16 commits into
HnDK0:mainfrom
MrGodFavourite:main

Conversation

@MrGodFavourite

@MrGodFavourite MrGodFavourite commented Nov 27, 2026 •

Copy link
Copy Markdown

What changed

Why

Type

  • Bug fix
  • Feature
  • Refactor / performance
  • Docs / build / CI

Testing

./gradlew ...
  • Regression test added for a bug fix
  • Affected modes checked (split / parallel / TTS) if the reader changed
  • User-facing strings added to values/strings.xml and values-ru/strings.xml
  • Screenshots attached for UI changes
  • No secrets, keys, tokens or cookies in the diff or logs

Notes

@MrGodFavourite

Copy link
Copy Markdown
Author

Problem

Fast-forwarding lines/paragraphs in TTS caused a multi-second "Preparing" (TtsStatus.Loading) pause or stopped playback:

  1. seekToBlock() while Playing/Loading always called playFrom(), which re-chunked the chapter and re-ran applySavedSettings() → applyEngine(), tearing down and re-initializing the Android TextToSpeech engine on every skip.
  2. speakFrom() queued every remaining chunk in the chapter at once (current until chunks.size) using QUEUE_ADD right after engine.stop(), risking Binder/TTS queue saturation on rapid skips.

Fix

  • TtsController.kt:
    • Added a fast path in seekToBlock() when already ready and playing/loading the same chapter and language to jump directly via speakFrom().
    • Replaced full-chapter queuing in speakFrom() with a bounded 4-chunk lookahead window (LOOKAHEAD_CHUNKS = 4) topped up in onStart, using flush = true (QUEUE_FLUSH) for the first utterance.
    • Prevented a failed future lookahead chunk (i > current) from stopping the active utterance.
  • TtsEngine.kt: Added flush: Boolean = false to TtsEngine.speak() and mapped it to TextToSpeech.QUEUE_FLUSH / QUEUE_ADD in AndroidTtsEngine.
  • TtsControllerTest.kt: Updated FakeEngine.speak() and added regression tests for bounded lookahead queuing, rapid skipNextChunk(), and seekToBlock() same-chapter jumps.

@HnDK0

HnDK0 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks for tracking down #19 and sharing this fix.

Limiting the lookahead to 4 chunks (QUEUE_FLUSH on the first one) and fast-seeking within the current chapter is the right call. Since this branch had unrelated commit history and workflow changes, I pulled the fix into main cleanly in dee503de with a few edge-case fixes and tests:

  • Top up and re-prime the queue inside onDone as well, so playback cannot stall if an engine drops onStart or rejects a lookahead chunk.
  • Only take the seekToBlock fast path when the translation map is unchanged and no chapter handoff / load is active (and cancel pending handoffTimeout in playFrom).
  • Stop already queued lookahead utterances on onError.
  • Added regression tests for these cases in TtsControllerTest.

Closing this PR now that the fix is in dee503de. Let me know in #19 if anything still lags on your device.

@HnDK0 HnDK0 closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants