5.2 KiB
Read aloud
An assistant reply has Read aloud beside Copy (same IconButton style). While
that reply reads it becomes Pause/Resume and Stop; one reply reads at a time
app-wide. The spoken word is highlighted (theme primaryContainer) in the
rendered reply and the transcript scrolls only when that word leaves the
viewport. Settings > Read aloud picks the voice (grouped by language, device
language first, a sample per voice), the rate (0.5x to 2.0x) and resets both;
they are saved on the device.
Structure
lib/features/chat/speech/speech_engine.dart: theSpeechEngineseam (speak, pause, stop, setVoice, setRate, voices, start/pause/resume/ progress/completion/cancel/error callbacks).flutter_tts_speech_engine.dart: theflutter_ttsimplementation. The rate is a multiplier (1.0 = normal); Apple platforms get half of it on their 0..1 scale. Linux reports unsupported, so the button and section are hidden.speech_controller.dart: the reader. State changes notify the controller; the spoken word is the separatewordnotifier and the reading's conversation (SpeechReading: chat id and name) thereadingnotifier. Stops onhidden,pausedordetached(notinactive, which is only a lost focus): the phone app backgrounded keeps that behaviour.app_speech.dart: the app's single reader (speechController, a fake in tests viadebugSpeechController),speechControllerIfCreatedfor code that only stops or relabels, andspeechReading, the app-level listenable the avatars and the global control follow.speech_document.dart: Markdown to sentences, with each spoken character's part and source offset. Code blocks (``` and ~~~) are "Code block.", tables "Table." then cells; rules are skipped.chat_markdown_speech.dart: highlight spans and the reveal scroll forChatMarkdownView.- Reader ids are
<sessionId>/<row id>;SpeechOrigin(put by the thread view) gives Read aloud the chat id and name when a reading starts.
Keeps reading across chats
The reader belongs to the app, not the thread view: switching chats or panes,
opening the chat list or another destination keeps the reply reading, and
reopening the chat shows its Pause/Stop again. A reading ends when it is
stopped (the reply's Stop or the global one), finishes, another reply starts,
its chat is closed (/close, the row's Close, or a refreshed row closed
elsewhere) or deleted (gone from a successful list read, which includes its
machine being removed), and when the app closes or is backgrounded.
- Indicator:
chatSessionActivityBadge(speaking:)drawsChatSpeakingGlyph(three animated bars; a still speaker under reduced motion) in the avatar's bottom-right badge, ahead of the operator wait, the running spinner and the background-task count.ChatSessionAvatar(list, quick switch, phone chat stack, pane header) andChatCompactAvatar(compact sidebar) followspeechReading. - Global stop:
ChatSpeakingControl, "Speaking · " (or "Paused") with Stop speaking, sits above the composer of the active pane (pasteTarget) of any chat, so one copy shows whatever the layout. It uses the design's panel fill, frame and corner tokens. - Servers: federated chat ids carry their machine, so moving between machines
in the combined list keeps reading. Switching the server profile (native)
remounts
PipelinesApp, whose dispose stops the reader; on the web a server switch loads another page, which ends speech. - Detached windows are separate processes (desktop) or browser windows (web), each with its own reader: speech belongs to the window that started it, shows only there, and stops when that window closes.
- Web: the utterance is held by
WebSpeechEngine, owned by the app reader, never by a widget.
Platform notes
- Resume speaks the paused text again; Android, Apple and web plugins all treat that as a resume. Android then reports progress against the remaining suffix, which the controller maps back to the whole text.
- After a stop the controller waits (at most 400 ms) for the platform cancel, so a late cancel cannot end the next reply and web speech is idle before the next utterance.
- Apple and web
flutter_ttscannot clear a chosen voice. After a voice was set in a run, "System default" uses the device language's first voice. - Platforms without word boundaries read without a highlight.
flutter_ttshas no Swift package, so macOS (and iOS) builds use CocoaPods for it:Podfileand thePods-Runnerxcconfig includes are the generated plugin integration.
Verification
Tests (fake engine): test/features/chat/speech/,
test/features/chat/ui/transcript/chat_read_aloud_button_test.dart,
chat_markdown_read_aloud_test.dart,
test/features/chat/conversation/chat_thread_read_aloud_test.dart,
test/features/chat/ui/sessions/chat_speaking_badge_test.dart,
chat_sessions_speech_test.dart,
test/features/settings/settings_read_aloud_section_test.dart.
Captures (speech=fake, Terminal and Classic, wide and phone) show the badge
on the reading chat after switching to another one, and the global control.
Not verified on a device: real speech output, word boundaries and voices on Android, macOS and browsers.