[GH-ISSUE #1138] TTS: stream audio per sentence as the model generates (drop time-to-first-audio) #562

Closed
opened 2026-10-20 17:30:13 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @etherious1804 on GitHub (May 24, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1138

Problem

When auto-play is enabled, Marinara dispatches the entire model reply to TTS only after isStreaming flips to false — i.e. after the full reply has been generated AND any post-processing agents have run. For local TTS providers (mlx-audio, openedai-speech, etc.) where synthesis is much slower than cloud APIs, this means the user waits the full reply time before hearing the first word. With a 30-second roleplay reply and a local Chatterbox model, time-to-first-audio is 30+ seconds.

The information needed to start TTS earlier is already available in the chat store: streamBuffers is updated token-by-token as the LLM streams. We can split on sentence boundaries and dispatch each completed sentence to /api/tts/speak immediately, playing the resulting clips sequentially as they arrive.

Proposed feature

Opt-in toggle under TTS settings: "Stream as the model generates". When enabled (with auto-play for the chat mode also enabled):

  • Each completed sentence in the streaming buffer fires a TTS request in parallel.
  • Resulting audio blobs play in order through a chained HTMLAudioElement playback queue.
  • Time-to-first-audio drops to ~1-2 seconds (time for the model to emit the first sentence + one TTS roundtrip).
  • The end-of-stream "speak the whole reply" path is suppressed when the toggle is on, so the reply isn't spoken twice.
  • Default: off (no behavior change for existing users).

UX edge cases

  • User clicks Stop mid-reply — audio must cut off immediately, not finish the queued sentences after the LLM aborts.
  • User switches chats mid-stream — the streaming session for the old chat tears down; the new chat starts fresh.
  • User joins a chat that's already streaming — prime the chunker cursor to the current buffer length so the backlog isn't spoken as one giant chunk.
  • Inline thinking blocks (<thought>, <|channel>thought) — recognized and skipped so the model's reasoning isn't spoken aloud.
  • Server content_replace events — defensive cursor snap so a shortened buffer doesn't desync the chunker.
  • Per-message play buttons must keep working — streaming TTS uses raw HTMLAudioElement rather than calling ttsService.stop(), preserving the global service for other code paths.
  • Speed slider — playbackRate is applied client-side on each audio element so the slider works regardless of whether the local TTS server respects the speed API parameter.

Implementation

Open PR follows.

Originally created by @etherious1804 on GitHub (May 24, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1138 ### Problem When auto-play is enabled, Marinara dispatches the entire model reply to TTS *only after* `isStreaming` flips to false — i.e. after the full reply has been generated AND any post-processing agents have run. For local TTS providers (mlx-audio, openedai-speech, etc.) where synthesis is much slower than cloud APIs, this means the user waits the *full* reply time before hearing the first word. With a 30-second roleplay reply and a local Chatterbox model, time-to-first-audio is 30+ seconds. The information needed to start TTS earlier is already available in the chat store: `streamBuffers` is updated token-by-token as the LLM streams. We can split on sentence boundaries and dispatch each completed sentence to `/api/tts/speak` immediately, playing the resulting clips sequentially as they arrive. ### Proposed feature Opt-in toggle under TTS settings: **"Stream as the model generates"**. When enabled (with auto-play for the chat mode also enabled): - Each completed sentence in the streaming buffer fires a TTS request in parallel. - Resulting audio blobs play in order through a chained HTMLAudioElement playback queue. - Time-to-first-audio drops to ~1-2 seconds (time for the model to emit the first sentence + one TTS roundtrip). - The end-of-stream "speak the whole reply" path is suppressed when the toggle is on, so the reply isn't spoken twice. - Default: off (no behavior change for existing users). ### UX edge cases - **User clicks Stop mid-reply** — audio must cut off immediately, not finish the queued sentences after the LLM aborts. - **User switches chats mid-stream** — the streaming session for the old chat tears down; the new chat starts fresh. - **User joins a chat that's already streaming** — prime the chunker cursor to the current buffer length so the backlog isn't spoken as one giant chunk. - **Inline thinking blocks** (`<thought>`, `<|channel>thought`) — recognized and skipped so the model's reasoning isn't spoken aloud. - **Server `content_replace` events** — defensive cursor snap so a shortened buffer doesn't desync the chunker. - **Per-message play buttons** must keep working — streaming TTS uses raw HTMLAudioElement rather than calling `ttsService.stop()`, preserving the global service for other code paths. - **Speed slider** — `playbackRate` is applied client-side on each audio element so the slider works regardless of whether the local TTS server respects the `speed` API parameter. ### Implementation Open PR follows.
Author
Owner

@cha1latte commented on GitHub (May 25, 2026):

Triage note: labeling this as normal-scope because streaming TTS is an aligned opt-in improvement for existing TTS autoplay behavior with clear user-facing edge cases and a bounded settings toggle.

The least risky next step is to review the open implementation against stop/cancel, chat-switch, and duplicate-playback boundaries.

<!-- gh-comment-id:4536671759 --> @cha1latte commented on GitHub (May 25, 2026): Triage note: labeling this as `normal-scope` because streaming TTS is an aligned opt-in improvement for existing TTS autoplay behavior with clear user-facing edge cases and a bounded settings toggle. The least risky next step is to review the open implementation against stop/cancel, chat-switch, and duplicate-playback boundaries.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#562
No description provided.