Skip to content

feat: add synthesize_stream for per-sentence audio - #65

Merged
StuBehan merged 2 commits into
mainfrom
feat/streaming-synthesis
Oct 21, 2026
Merged

StuBehan merged 2 commits into
mainfrom
feat/streaming-synthesis

Conversation

@StuBehan

Copy link
Copy Markdown
Collaborator

Refs #61. Streaming half only, the session options/threads bit follows separately.

speak() has streamed sentence by sentence since #31 but only to the output device, so anything wanting the samples back had to call synthesize() and wait out the whole text. Pulled the producer thread and queue out of _stream_play into a _synthesize_chunks() generator - speak() and the new synthesize_stream() are both just consumers of it now. No new threading, one copy instead of two.

On a 5 sentence paragraph (20.8s of speech) time to first audio goes 3.95s to 1.29s. Total wall clock goes the other way, 3.95s to 5.95s, because per sentence batching is less efficient. That's in the docstring so callers can pick.

The stream gets its own stop event rather than sharing self._stop_event, so a stop() on the shared output device can't truncate an unrelated caller's stream (consumer cancels by abandoning the generator, which takes the synth thread with it).

Concatenated output runs ~3% shorter than synthesize() because Kokoro trims each sentence, but that's the audio speak() has always played, not new.

8 new tests, the one that matters is the leak check - verified it fails without the drain loop.

@StuBehan
StuBehan merged commit 9e8846d into main Oct 21, 2026
10 checks passed
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.

1 participant