mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #7547] [MERGED] feat(avatars): lip sync with text-to-speech #7787
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#7787
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/7547
Author: @SpicyMarinara
Created: 10/13/2026
Status: ✅ Merged
Merged: 10/13/2026
Merged by: @SpicyMarinara
Base:
staging← Head:feat/avatars-tts-lipsync📝 Commits (5)
15af58ffeat(avatars): shared contract for the 2D and 3D avatar agentsdf53866fix(avatars): refuse Windows device names in stored paths and point code at the developer notes1aff83bfeat(avatars): move the speaking character's mouth with text-to-speechdbf22c4fix(avatars): keep the mouth shape while the mouth closes after a soundb6e6c18fix(avatars): keep a text-to-speech reading with the chat it was requested in📊 Changes
44 files changed (+2599 additions, -6 deletions)
View changed files
📝
docs/agents/agents-overview.md(+1 -0)➕
docs/agents/avatars.md(+52 -0)📝
docs/development/optional-agent-packages.md(+19 -0)📝
packages/client/package.json(+5 -0)📝
packages/client/src/components/chat/ChatArea.tsx(+1 -0)📝
packages/client/src/components/chat/ChatMessage.tsx(+1 -0)➕
packages/client/src/features/avatars/audio/avatar-audio.ts(+105 -0)➕
packages/client/src/features/avatars/audio/voice-envelope.ts(+144 -0)➕
packages/client/src/features/avatars/contract.ts(+198 -0)➕
packages/client/src/features/avatars/director/avatar-director.ts(+24 -0)➕
packages/client/src/features/avatars/generation/AvatarGenerationPanel.tsx(+8 -0)➕
packages/client/src/features/avatars/renderers/mesh-avatar/MeshAvatarSetupPanel.tsx(+7 -0)➕
packages/client/src/features/avatars/renderers/mesh-avatar/mesh-avatar-renderer.ts(+20 -0)➕
packages/client/src/features/avatars/renderers/pngtuber/PngtuberSetupPanel.tsx(+7 -0)➕
packages/client/src/features/avatars/renderers/pngtuber/pngtuber-renderer.ts(+19 -0)➕
packages/client/src/features/avatars/renderers/vrm/VrmSetupPanel.tsx(+7 -0)➕
packages/client/src/features/avatars/renderers/vrm/vrm-renderer.ts(+20 -0)➕
packages/client/src/features/avatars/setup/AvatarAgentSetup.tsx(+13 -0)➕
packages/client/src/features/avatars/stage/AvatarStage.tsx(+15 -0)📝
packages/client/src/lib/tts-dialogue.ts(+3 -0)...and 24 more files
📄 Description
Linked issue
Refs #7537 (epic: 2D and 3D avatar agents), lane TTS lip sync.
Depends on https://github.com/Pasta-Devs/Marinara-Engine/pull/7539, the shared avatar contract (opened ready against
staging, Refs #7537, not merged yet). Its commits show in this diff until it merges; this lane's changes are the last three commits only (1aff83b61,dbf22c4af,b6e6c18b7).Why this change
What changed
features/avatars/audio/avatar-audio.ts(fills the contract stubsubscribeAvatarAudio): while TTS plays a line, it publishes anAvatarAudioLevelSourcefor that line's character, andnullwhen the voice pauses, stops, moves to narration or another speaker, or the chat changes. A reading belongs to the chat it was requested in, which the Roleplay callers pass with the request. Only that chat's avatars follow the voice: switching to another chat (even while the voice is still loading) stops the mouth there, and coming back while it still plays picks it up again. A reading with no chat, like the voice preview in settings, moves no mouth. It watches the TTS service only while the director is subscribed.features/avatars/audio/voice-envelope.ts: each clip is decoded a second time, on the side, by oneOfflineAudioContextthat is created once and kept. The decode turns the clip into a 20 ms loudness track. That track is noise-gated, normalised to the clip's loud parts and smoothed: the mouth opens fast and closes a little slower.level()reads the track at the playback element'scurrentTime.viseme()gives a coarse shape from how bright the sound is:oufor dark sounds,aafor open ones,ihfors/sh. While the mouth closes after a sound, it keeps that sound's shape. Each clip's track is cached by its Blob, so a replay does not decode again. A clip the browser cannot decode gives no level, and the voice still plays.lib/tts-service.ts(minimal): a read-onlygetPlayback()that returns the element, the Blob, the reading's chat and the line's character.speak()andspeakSequence()accept an optionalchatId, and requests an optionalcharacterId. Neither is sent to the provider or used in cache keys. No playback, gesture or autoplay code changed.components/chat/ChatArea.tsx(auto-play) andcomponents/chat/ChatMessage.tsx(Speak): each passes the chat it already knows (targetChatId,message.chatId) aschatId. One line each.lib/tts-dialogue.tsandlib/tts-roleplay-speaker-extractor.ts: each voice request now carries thecharacterIdthat the existing voice lookup already resolved, so the mouth that moves belongs to the character whose voice speaks. Cache keys do not include it, so cached clips still replay.docs/agents/avatars.md: filled the "Lip sync with text-to-speech" section.scripts/regressions/avatar-tts-lipsync.regression.ts. It checks the loudness track (silence, gating, attack and release, quieter parts, mouth shapes) and the request builders. It also drives the realttsServicewith a stubbed audio element, fetch and Web Audio through a three-speaker reading, pause and resume, a chat switch, restart, stop, an undecodable clip and unsubscribing, and asserts that no realtimeAudioContextis ever created. It covers a chat switch while the voice is still loading, a stage that mounts in another chat while a voice plays, and a reading with no chat.Decisions and deviations
AnalyserNodeon the playback element (the lane brief asked forcreateMediaElementSource+AnalyserNode).createMediaElementSourcereroutes that element's sound through the AudioContext for good, and TTS reuses one element for every clip. On iOS a context goessuspended/interruptedafter backgrounding, a call or Siri. From then on every autoplayed voice would play silently until a later tap resumed the context, while the app still showed "playing". Web Audio output also follows iOS's ring/silent switch, which a plain<audio>element does not. A decoded loudness track gives the same level and needs no gesture. It keeps the element out of Web Audio entirely, so the Safari/iOS code needs no change. The trade-off is one extra decode per clip, a few milliseconds, and only while an avatar stage is listening.tts-dialogue.tsandtts-roleplay-speaker-extractor.ts(each got one optional field and one line), and the two Roleplay speak calls inChatArea.tsxandChatMessage.tsx(onechatIdoption line each). Matching speaker names inside the audio module would have needed the chat's participants, and the audio API in the contract cannot receive them. Guessing the reading's chat from whichever chat is open when the clip starts playing gave the wrong chat when the user switched chats while a voice loaded, so the callers now say which chat it is. No contract file was changed.GameNarration.tsx), not the shared TTS player, so Game avatars follow the text. Hooking that up means editing the Game line display code, which belongs to the director lane; it can feedAvatarAudioLevelSources itself, or a follow-up can.Validation
pnpm checkpasses locallyCONTRIBUTING.mdCommands run by the agent (results as reported; the boxes above are for the human reviewer):
pnpm install --frozen-lockfile --prefer-offline --store-dir <main checkout>/.pnpm-storeandpnpm build:shared: ok.pnpm checkonb6e6c18b7(current head): passed (impeccable, localization with no new keys, Prettier, ESLint, e2e and token-estimation types, full build).node scripts/run-regressions.mjs --filter <name>:tts(7 files:avatar-tts-lipsync,roleplay-vn-tts,tts-cache-clear,tts-pcm,tts-safari-play-guard,tts-source-persistence,tts-voice-assignment-route),avatar(6 files, includingavatar-contract),roleplay-vn(2 files),agent-activation(2),spatial-context,open-issues: all passed.avatar-tts-lipsyncat "switching chats withdraws the voice" (reverted).tts-servicestays in its existing chunk.coderabbit review --agent --committed --base-commit df538660f, Pasta-Devs org, this lane's commit only): 1 minor finding, left as it is. It asked to update the translated docs or link a[docs-i18n]follow-up fordocs/agents/avatars.md. That follow-up is #7540, linked under "Docs and release impact".dbf22c4af. When a sound ended, the mouth shape dropped to "rest" while the mouth was still half open, so a renderer could snap it. The shape now holds until the mouth is shut, and the regression checks it.dbf22c4af, fixed inb6e6c18b7:chatId,TTSPlaybackcarries it in place of the sequence number that only served the guess, and a reading with no chat moves no avatar.avatar-tts-lipsyncregression passes. Against the old code it fails at "chat B's avatars stay still for chat A's voice" (switching while loading). A scratch copy without that block, kept outside the repo, fails at "a stage mounted in another chat does not take chat A's voice" (late stage). The fix was restored and the tree is clean.b6e6c18b7vianode scripts/run-regressions.mjs --filter <name>:tts7/7,avatar6/6,roleplay-vn2/2,agent-activation2/2,spatial-context1/1,open-issues1/1,roleplay-streaming,character-identity,chat-message-css-securityandfrozen-server-client-timeouts1/1 each: all passed.b6e6c18b7(coderabbit review --agent --committed --base-commit d115a4406, after two rate-limit waits): 2 minor findings, both outside this lane's files, not changed here:docs/agents/avatars.mdoutline placeholders and "staging" in the intro. These are the contract's outline and other lanes' sections; the guide is filled lane by lane, as answered on the GitHub thread below.AVATAR_OWNER_ID_PATTERNaccepts Windows device names (nul,con). It is in a frozen contract file. Real owner ids come fromnanoid(), 21 characters, so they can never be a device name, and avatar storage on this branch is still a stub that throws. Raised as a follow-up for the contract or base lane rather than patched here.1aff83b61: 1 thread, answered and resolved without a change. It asked to call lip sync "forthcoming" in the guide. The guide's intro already says the feature is still being built and fills in as parts land. Each lane writes its section for the finished behaviour, and no avatar package is in the catalog yet.Manual verification notes
Nothing about avatars is visible until the director and base lanes land. Until then, these checks confirm that TTS still plays exactly as before:
pnpm dev:client) on desktop Chrome: in DevTools runconst a = await import("/src/features/avatars/audio/avatar-audio.ts"); a.subscribeAvatarAudio((id, s) => { console.log(id, s); if (s) { const t = setInterval(() => s.isPlaying() ? console.log(s.level().toFixed(2), s.viseme?.()) : clearInterval(t), 100); } });. Then press Speak on a character's message. The character's id is logged, then levels that rise and fall with the voice. Narration logsnull.After the director and base lanes land (Mari's staging test list):
Docs and release impact
docs-i18nbranch updated to match, or a[docs-i18n]follow-up issue opened (see CONTRIBUTING.md § Translated documentation)Docs:
docs/agents/avatars.md("Lip sync with text-to-speech"). Translation follow-up already exists: #7540 coversdocs/agents/avatars.mdonce the lanes have filled it.UI evidence (if applicable)
No UI in this lane.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.