mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #1142] [CLOSED] feat(tts): stream audio per sentence as the model generates #4942
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4942
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1142
Author: @etherious1804
Created: 5/24/2026
Status: ❌ Closed
Base:
staging← Head:feat/tts-streaming-autoplay📝 Commits (2)
0954666fix(tts): apply playback rate client-side for local OAI-compatible serversefdf859feat(tts): stream audio per sentence as the model generates📊 Changes
8 files changed (+720 additions, -26 deletions)
View changed files
📝
packages/client/src/components/chat/ChatArea.tsx(+35 -2)📝
packages/client/src/components/chat/ChatMessage.tsx(+52 -23)📝
packages/client/src/components/panels/settings/TTSConfigCard.tsx(+13 -0)➕
packages/client/src/hooks/use-streaming-tts.ts(+368 -0)➕
packages/client/src/lib/sentence-chunker.ts(+205 -0)📝
packages/client/src/lib/tts-dialogue.ts(+26 -0)📝
packages/client/src/lib/tts-service.ts(+12 -1)📝
packages/shared/src/types/tts.ts(+9 -0)📄 Description
Linked issue
Closes #1138
Why this change
When auto-play is enabled, Marinara today waits for
isStreamingto flip false before dispatching the entire reply to TTS. For local TTS providers (mlx-audio Chatterbox, openedai-speech, etc.) where synthesis is much slower than cloud APIs, this means the user waits the full generate time before hearing any audio. With a 30-second roleplay reply on a local M-series box, time-to-first-audio is 30+ seconds — the assistant has finished writing before it even starts speaking.The information needed to start TTS much earlier is already available in the chat store:
streamBuffersupdates token-by-token as the LLM streams. We can split on sentence boundaries and dispatch each completed sentence as it arrives, then play the resulting clips sequentially. Time-to-first-audio drops to ~1-2 seconds (the time for the model to produce the first sentence plus one TTS round-trip).What changed
Client — new streaming-TTS hook
packages/client/src/hooks/use-streaming-tts.ts: newuseStreamingTTShook subscribes to the chat store, runs a sentence chunker against the growing stream buffer, and dispatchesttsService.generateAudioper completed sentence. Audio fetches run in parallel; playback is serialized through a chained promise so order is preserved. Aborts cleanly on user-cancel (the existing chat-abort signal is forwarded, plus a local AbortController fires onstopSession). Per-audio AbortListener is bounded to that element's playback lifetime so long replies don't pile up 50+ listeners on a single signal.stopStreamingTTS()/isStreamingTTSActive()/subscribeStreamingTTSActive()trio so any UI surface can interrupt or observe the active session — the per-message speaker icon hooks into this.Client — sentence chunker
packages/client/src/lib/sentence-chunker.ts: new module. Maintains acursor+highWaterMarkover the growing buffer;extractNewSentencesemits each completed sentence as soon as a sentence-ending punctuation + whitespace appears. Multi-dot ellipses (..,...) are explicitly NOT treated as sentence ends (continuations, not terminators). Known abbreviations (Mr.,Dr., etc.) are skipped. The high-water mark prevents replay if the server later emits acontent_replaceSSE event that rewinds the buffer to a common prefix (used to strip character commands at end-of-stream).Client — stop affordance on speaker icons
packages/client/src/components/chat/ChatMessage.tsx: every speaker icon subscribes to the streaming-TTS active state. While a session is active, the icon flips toVolumeX, shows "Stop streaming narration" as tooltip, and clicking any icon halts playback. The chat's generation-abort signal only fires while the LLM is streaming; without this affordance there's no way to interrupt audio after natural completion.Wiring
packages/client/src/components/chat/ChatArea.tsx: invokesuseStreamingTTSwhen the chat is in a mode that supports autoplay andttsConfig.autoplayStreamingis on. The legacy "speak the whole reply at end-of-stream" path is gated onif (cfg.autoplayStreaming) return;so the reply isn't spoken twice.packages/client/src/components/panels/settings/TTSConfigCard.tsx: adds the "Stream as the model generates" toggle under TTS settings (default off — no behavior change for existing users).packages/shared/src/types/tts.ts: addsautoplayStreaming?: booleantoTTSConfig.Known limitation
The speaker icon does not visually flip back to "play" after natural stream completion — it stays in stop-state until a new stream starts. Click-to-stop continues to function (clicking a stuck stop-icon is a safe no-op since there's nothing to stop). Will track in a follow-up; not blocking.
Dependency
Includes #1141 (client-side playback rate) as a base commit so the streaming-TTS path can honor the speed slider too. Once #1141 merges, this PR's diff will shrink to just the streaming TTS changes.
Validation
pnpm checkpasses locallyContainer (Docker / Podman) built and ran without issue
Ran the app, clicked through the changes manually
Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
Above manual verification completed (describe below)
Read and followed
CONTRIBUTING.mdManual verification notes
What was verified on the branch (against a running Marinara dev server, local mlx-audio TTS, local Gemma 4 26B LLM, Roleplay mode with autoplay + streaming-TTS toggle on):
setStreaming(true) → clearStreamBuffer())."But tell me, human..."don't cause mid-sentence cuts; the chunker waits for a real terminator.content_replace(strips character commands) no longer replays the cleaned tail — the high-water mark guards against re-emission.To verify in browser:
Docs and release impact
UI evidence (if applicable)
No new UI surface beyond the toggle in TTS settings. The speaker-icon swap (Volume2 ↔ VolumeX + sky-blue tint while streaming) is in-place, no new components.
🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.