feat: stream synthesis sentence-by-sentence for low-latency playback#31
Merged
Conversation
ebeb53b to
1187fd2
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
Makes
speak()stream synthesis sentence-by-sentence so audio starts almost immediately instead of after the whole text is synthesized. Kokoro batches by phoneme count (~510), so a full paragraph synthesizes as one blob before any sound plays — that's the lead-in lag. Splitting into sentences ourselves means the first sentence (~0.2s to synthesize) plays right away while the rest synthesize in the background.Changes
_split_sentences()— breaks text into sentence-ish chunks on./!/?followed by whitespace, and on newlines. The whitespace requirement leaves decimals (0.95) and mid-token dots (file.ts) intact._stream_play()— a producer thread synthesizes ahead into a bounded queue (maxsize=8) while the main thread plays each chunk as it's ready, so later sentences are usually synthesized by the time the previous one finishes.speak(blocking=True)streams inline;blocking=Falseruns_stream_playon a background daemon thread (_play_thread).stop()now sets a_stop_eventso an in-progress stream halts between sentences (in addition tosd.stop()).voice/speed/langoverrides are threaded through to each sentence's synthesis.Testing
Ran the full CI-equivalent suite locally, all green:
New tests cover: one synth+play cycle per sentence, single-sentence playback, background-thread playback for
blocking=False, engine defaults vs per-call overrides,stop()setting the event, and_split_sentencesedge cases (decimals/mid-token dots preserved, newline splitting, blank input).Related issues