Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

27.0 KB
Raw Blame
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1" />
		<title data-i18n="demos_audio2face.meta_title">Audio2Face · two NVIDIA models on one face · three.ws</title>
		<meta name="description" content="Type text, hear it in NVIDIA Magpie's voice, and watch the 3D avatar's face animate in real time — driven frame-for-frame by NVIDIA Audio2Face-3D. Real ARKit blendshapes, mapped onto any rig."  data-i18n-attr="content:demos_audio2face.meta_description"/>
		<link rel="canonical" href="https://three.ws/audio2face" />
		<meta property="og:title" content="Audio2Face · two NVIDIA models on one face · three.ws"  data-i18n-attr="content:demos_audio2face.meta_og_title"/>
		<meta property="og:description" content="NVIDIA Magpie voice + NVIDIA Audio2Face-3D facial animation, in sync, in the browser. The avatar's lips track the real audio."  data-i18n-attr="content:demos_audio2face.meta_og_description"/>
		<meta property="og:url" content="https://three.ws/audio2face" />
		<meta property="og:type" content="website" />
		<meta property="og:site_name" content="three.ws" />
		<meta name="twitter:card" content="summary_large_image" />
		<meta name="twitter:title" content="Audio2Face · two NVIDIA models on one face · three.ws"  data-i18n-attr="content:demos_audio2face.meta_twitter_title"/>
		<meta name="twitter:description" content="NVIDIA Magpie voice + NVIDIA Audio2Face-3D facial animation, in sync, in the browser."  data-i18n-attr="content:demos_audio2face.meta_twitter_description"/>
		<meta name="twitter:site" content="@trythreews" />
		<link rel="stylesheet" href="/style.css" />
		<link rel="stylesheet" href="/nav.css" />
		<style>
			:root { color-scheme: dark; }
			body { background: #060606; color: #eee; font: 15px/1.5 -apple-system, system-ui, sans-serif; margin: 0; }
			.stage { max-width: 1100px; margin: 0 auto; padding: 48px 24px 90px; }
			.crumb a { color: #8a8d98; text-decoration: none; }
			.crumb a:hover { color: #76e0a1; }
			h1 { font-size: 28px; font-weight: 600; letter-spacing: -0.015em; margin: 14px 0 8px; }
			.sub { color: #8a8d98; max-width: 68ch; margin: 0 0 8px; }
			.models { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 26px; }
			.chip { display: inline-flex; align-items: center; gap: 7px; background: #0e140f; border: 1px solid #1d2a20; color: #9fe9bd; border-radius: 999px; padding: 5px 12px; font-size: 12px; font-weight: 600; }
			.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: #76e0a1; box-shadow: 0 0 8px #76e0a1; }
			.chip.voice { background: #0d1117; border-color: #1c2430; color: #8fb7ff; }
			.chip.voice .dot { background: #5b9bff; box-shadow: 0 0 8px #5b9bff; }
			.row { display: grid; grid-template-columns: 1fr 340px; gap: 22px; }
			@media (max-width: 900px) { .row { grid-template-columns: 1fr; } }
			.viewer-host { background: #0c0e0c; border: 1.5px solid #1c241d; border-radius: 14px; min-height: 500px; position: relative; overflow: hidden; }
			.viewer-host canvas { display: block; width: 100%; height: 100%; }
			.viewer-label { position: absolute; top: 12px; left: 12px; max-width: calc(100% - 24px); z-index: 1; background: rgba(0,0,0,0.6); padding: 5px 11px; border-radius: 999px; font-size: 11.5px; color: #76e0a1; font-weight: 600; letter-spacing: 0.02em; }
			/* Bottom-left, not top-right: the viewer column is narrow, and a top-right
			   badge ran into the top-left pipeline label once the rig report filled in. */
			.viewer-badge { position: absolute; bottom: 12px; left: 12px; max-width: calc(100% - 24px); z-index: 1; background: rgba(0,0,0,0.6); padding: 5px 11px; border-radius: 999px; font-size: 11.5px; color: #8a8d98; font-weight: 600; }
			.viewer-badge b { color: #cfeede; }
			.viewer-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 12px; color: #8a8d98; font-size: 13px; transition: opacity var(--duration-base) var(--ease-standard); }
			.viewer-loading[hidden] { display: none; }
			.spinner { width: 30px; height: 30px; border-radius: 50%; border: 3px solid #1c241d; border-top-color: #76e0a1; animation: spin 0.8s linear infinite; }
			@keyframes spin { to { transform: rotate(360deg); } }
			@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
			.panel { background: #0c0e0c; border: 1.5px solid #1c241d; border-radius: 14px; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
			.panel textarea { background: #070807; border: 1px solid #1c241d; color: #eee; padding: 11px 12px; border-radius: 9px; font: inherit; min-height: 88px; resize: vertical; }
			.panel textarea:focus, .panel select:focus { outline: 2px solid #2c6b46; outline-offset: 1px; border-color: #2c6b46; }
			.panel select { background: #070807; border: 1px solid #1c241d; color: #eee; padding: 8px 10px; border-radius: 9px; font: inherit; width: 100%; }
			.panel label { font-size: 11px; color: #8a8d98; text-transform: uppercase; letter-spacing: 0.08em; }
			.field { display: flex; flex-direction: column; gap: 5px; }
			.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
			.btns { display: flex; gap: 8px; }
			.panel button { background: #76e0a1; color: #06140c; border: 0; border-radius: 9px; padding: 11px 14px; font-weight: 700; cursor: pointer; font: inherit; transition: filter var(--duration-fast) var(--ease-standard), transform var(--duration-instant) var(--ease-standard); flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
			.panel button:hover:not(:disabled) { filter: brightness(1.07); }
			.panel button:active:not(:disabled) { transform: translateY(1px); }
			.panel button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
			.panel button.secondary { background: rgba(255,255,255,0.06); color: #eee; flex: 0 0 auto; }
			.panel button:disabled { opacity: 0.45; cursor: not-allowed; }
			.btn-spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid rgba(6,20,12,0.35); border-top-color: #06140c; animation: spin 0.7s linear infinite; }
			.status { font-size: 12.5px; color: #8a8d98; min-height: 18px; display: flex; align-items: center; gap: 7px; }
			.status .live-dot { width: 7px; height: 7px; border-radius: 50%; background: #76e0a1; animation: pulse 1.1s ease-in-out infinite; }
			@keyframes pulse { 0%,100% { opacity: 0.35; } 50% { opacity: 1; } }
			.pathline { font-size: 12px; color: #8a8d98; border-top: 1px solid #161c17; padding-top: 12px; }
			.pathline b { color: #cfeede; font-weight: 600; }
			.pathline.a2f b { color: #76e0a1; }
			.pathline.fallback b { color: #ffd27a; }
			.shapes { display: flex; flex-direction: column; gap: 6px; }
			.shape-row { display: grid; grid-template-columns: 96px 1fr; gap: 8px; align-items: center; font-size: 11.5px; color: #8a8d98; font-family: ui-monospace, monospace; }
			.meter { height: 7px; border-radius: 4px; background: rgba(118,224,161,0.12); overflow: hidden; }
			.meter > span { display: block; height: 100%; width: 0%; background: linear-gradient(90deg,#3aa873,#76e0a1); border-radius: 4px; transition: width var(--duration-instant) var(--ease-standard); }
			.a2f-error { background: rgba(255, 120, 86, 0.08); border: 1px solid rgba(255, 120, 86, 0.3); color: #ffc6ad; border-radius: 9px; padding: 11px 13px; font-size: 13px; line-height: 1.5; display: flex; align-items: center; gap: 10px; justify-content: space-between; }
			.a2f-error[hidden] { display: none; }
			.a2f-error span { flex: 1; }
			.a2f-error strong { color: #fff; font-weight: 600; }
			.a2f-error .retry { flex: 0 0 auto; cursor: pointer; font: inherit; font-weight: 600; color: #1a0d07; background: #ffb48a; border: 0; border-radius: 7px; padding: 7px 13px; }
			.a2f-error .retry:hover { filter: brightness(1.07); }
			.a2f-error .retry:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
			.foot { color: #6a6d75; font-size: 12px; margin-top: 28px; max-width: 70ch; }
			.foot code { color: #9fe9bd; }
			.foot a { color: #76e0a1; }
		</style>
		<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"Audio2Face","description":"NVIDIA Magpie voice + NVIDIA Audio2Face-3D facial animation, in sync, in the browser.","url":"https://three.ws/audio2face","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"}}</script>
	</head>
	<body>
		<header><div id="nav-container"></div></header>

		<main class="stage">
			<p class="crumb" data-i18n-html="demos_audio2face.three_ws_audio2face_amplitude_lipsync"><a href="/">three.ws</a> · audio2face · <a href="/lipsync">amplitude lipsync</a></p>
			<h1 data-i18n="demos_audio2face.two_nvidia_models_on_one_face">Two NVIDIA models on one face</h1>
			<p class="sub" data-i18n-html="demos_audio2face.type_a_line_it_s_spoken_in">
				Type a line. It's spoken in <strong>NVIDIA Magpie</strong>'s voice, and that exact
				audio is sent to <strong>NVIDIA Audio2Face-3D</strong>, which returns a per-frame
				ARKit blendshape track. The avatar's mouth and face animate frame-for-frame with the
				real voice — mapped onto whatever morph-target convention the loaded GLB ships
				(ARKit, common avatar-platform, VRM, Oculus visemes). If Audio2Face is unavailable, the
				avatar still speaks and falls back to amplitude lipsync — never a frozen face.
			</p>
			<div class="models">
				<span class="chip voice" data-i18n-html="demos_audio2face.magpie_tts_voice"><span class="dot"></span>Magpie TTS · voice</span>
				<span class="chip" data-i18n-html="demos_audio2face.audio2face_3d_facial_animation"><span class="dot"></span>Audio2Face-3D · facial animation</span>
			</div>

			<div class="row">
				<div class="viewer-host" id="viewer-host">
					<div class="viewer-label">live · Magpie → Audio2Face → morphs</div>
					<div class="viewer-badge" id="viewer-badge">—</div>
					<div class="viewer-loading" id="viewer-loading">
						<div class="spinner"></div>
						<div id="loading-text">Loading avatar…</div>
					</div>
				</div>
				<div class="panel">
					<div class="field">
						<label for="text" data-i18n="demos_audio2face.text_to_speak">Text to speak</label>
						<textarea id="text">Hi! I'm a three dot w s avatar. My voice is NVIDIA Magpie, and my face is animated by NVIDIA Audio2Face, frame for frame.</textarea>
					</div>

					<div class="grid2">
						<div class="field">
							<label for="avatar" data-i18n="demos_audio2face.avatar_rig">Avatar rig</label>
							<select id="avatar">
								<option value="/avatars/default.glb" data-i18n="demos_audio2face.default">Default</option>
								<option value="/avatars/realistic-female.glb" data-i18n="demos_audio2face.realistic_female">Realistic female</option>
								<option value="/avatars/realistic-male.glb" data-i18n="demos_audio2face.realistic_male">Realistic male</option>
								<option value="/avatars/selfie-girl.glb" data-i18n="demos_audio2face.selfie_girl">Selfie girl</option>
							</select>
						</div>
						<div class="field">
							<label for="voice" data-i18n="demos_audio2face.voice">Voice</label>
							<select id="voice">
								<option value="nova" selected data-i18n="demos_audio2face.nova">Nova</option>
								<option value="alloy" data-i18n="demos_audio2face.alloy">Alloy</option>
								<option value="echo" data-i18n="demos_audio2face.echo">Echo</option>
								<option value="fable" data-i18n="demos_audio2face.fable">Fable</option>
								<option value="onyx" data-i18n="demos_audio2face.onyx">Onyx</option>
								<option value="shimmer" data-i18n="demos_audio2face.shimmer">Shimmer</option>
								<option value="sage" data-i18n="demos_audio2face.sage">Sage</option>
								<option value="coral" data-i18n="demos_audio2face.coral">Coral</option>
							</select>
						</div>
					</div>

					<div class="btns">
						<button id="speak"><span id="speak-label" data-i18n="demos_audio2face.speak">Speak</span></button>
						<button id="stop" class="secondary" disabled data-i18n="demos_audio2face.stop">Stop</button>
					</div>

					<div class="status" id="status" role="status" aria-live="polite"></div>

					<div id="a2f-error" class="a2f-error" role="alert" aria-live="assertive" hidden></div>

					<div class="pathline" id="pathline">Pipeline path will appear here after you press Speak.</div>

					<div class="field">
						<label data-i18n="demos_audio2face.live_blendshapes">Live blendshapes</label>
						<div class="shapes" id="shapes"></div>
					</div>
				</div>
			</div>

			<p class="foot" data-i18n-html="demos_audio2face.powered_by_nvidia_audio2face_3d_over_nvcf">
				Powered by <code>/api/a2f</code> (NVIDIA Audio2Face-3D over NVCF gRPC) and
				<code>/api/tts/speak</code> (NVIDIA Magpie). The blendshape→morph mapping lives in
				<code>src/voice/a2f-player.js</code> — it reuses the ARKit/VRM/Oculus vocabulary in
				<code>src/voice/arkit-blendshapes.js</code>, so any humanoid rig lip-syncs without a
				per-rig allowlist. See the <a href="/lipsync">amplitude-only lipsync demo</a> for the
				fallback path on its own.
			</p>
		</main>

		<script src="/nav.js" defer></script>
		<script type="module">
			import * as THREE from 'three';
			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
			import { getDecoders } from '/src/viewer/internal.js';
			import { A2FPlayer } from '/src/voice/a2f-player.js';
			import { LipsyncDriver, tapAudioElement } from '/src/voice/lipsync-driver.js';
			import { AvatarMouthTarget } from '/src/voice/avatar-morph-target.js';

			const $ = (id) => document.getElementById(id);
			const host = $('viewer-host');
			const statusEl = $('status');
			const badgeEl = $('viewer-badge');
			const pathEl = $('pathline');
			const errEl = $('a2f-error');
			const speakBtn = $('speak');
			const speakLabel = $('speak-label');
			const stopBtn = $('stop');
			const loadingEl = $('viewer-loading');
			const loadingText = $('loading-text');

			// ── Live blendshape meters ────────────────────────────────────────────
			const METERS = [
				['jawOpen', 'JawOpen'],
				['mouthFunnel', 'MouthFunnel'],
				['mouthSmileLeft', 'MouthSmile'],
				['mouthPucker', 'MouthPucker'],
				['browInnerUp', 'BrowInnerUp'],
				['eyeBlinkLeft', 'EyeBlink'],
			];
			const meterBars = new Map();
			for (const [key, label] of METERS) {
				const row = document.createElement('div');
				row.className = 'shape-row';
				row.innerHTML = `<span>${label}</span><span class="meter"><span data-k="${key}"></span></span>`;
				$('shapes').appendChild(row);
				meterBars.set(key, row.querySelector(`[data-k="${key}"]`));
			}
			function paintMeters(getter) {
				for (const [key] of METERS) {
					const v = Math.max(0, Math.min(1, getter(key)));
					meterBars.get(key).style.width = (v * 100).toFixed(0) + '%';
				}
			}
			function clearMeters() { for (const [key] of METERS) meterBars.get(key).style.width = '0%'; }

			// ── Three.js viewer ───────────────────────────────────────────────────
			const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
			renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
			renderer.setSize(host.clientWidth, host.clientHeight);
			renderer.outputColorSpace = THREE.SRGBColorSpace;
			host.appendChild(renderer.domElement);

			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera(26, host.clientWidth / host.clientHeight, 0.01, 100);
			camera.position.set(0, 1.58, 0.7);
			const controls = new OrbitControls(camera, renderer.domElement);
			controls.target.set(0, 1.57, 0);
			controls.enableDamping = true;
			controls.minDistance = 0.35;
			controls.maxDistance = 3.0;

			scene.add(new THREE.HemisphereLight(0xffffff, 0x404050, 1.15));
			const key = new THREE.DirectionalLight(0xffffff, 1.5);
			key.position.set(2, 4, 3);
			scene.add(key);
			const rim = new THREE.DirectionalLight(0x88ccff, 0.6);
			rim.position.set(-3, 2, -2);
			scene.add(rim);

			window.addEventListener('resize', () => {
				const w = host.clientWidth, h = host.clientHeight;
				renderer.setSize(w, h);
				camera.aspect = w / h;
				camera.updateProjectionMatrix();
			});

			// Avatars baked through the three.ws pipeline are EXT_meshopt_compression
			// (and some carry Draco/KTX2), so wire the shared decoder set before load.
			const loader = new GLTFLoader();
			const decodersReady = getDecoders().then(({ dracoLoader, ktx2Loader, meshoptDecoder }) => {
				loader.setDRACOLoader(dracoLoader)
					.setKTX2Loader(ktx2Loader.detectSupport(renderer))
					.setMeshoptDecoder(meshoptDecoder);
			});
			const player = new A2FPlayer();          // A2F track-driven path
			const mouthTarget = new AvatarMouthTarget(); // amplitude fallback target
			let currentModel = null;
			let avatarReady = false;

			async function loadAvatar(url) {
				avatarReady = false;
				loadingEl.hidden = false;
				loadingText.textContent = 'Loading avatar…';
				badgeEl.textContent = '—';
				try {
					await decodersReady;
					const gltf = await loader.loadAsync(url);
					if (currentModel) { scene.remove(currentModel); disposeModel(currentModel); }
					currentModel = gltf.scene;
					// Frame the head: lift so the face sits at camera height regardless of rig origin.
					const box = new THREE.Box3().setFromObject(currentModel);
					const height = box.max.y - box.min.y;
					if (height > 0.1) currentModel.position.y = -box.min.y;
					scene.add(currentModel);

					player.attach(currentModel);
					mouthTarget.attach(currentModel);
					avatarReady = true;
					loadingEl.hidden = true;

					// Report the convention we actually DETECTED on the rig, not a guess.
					const cov = player.describe();
					const conv = cov.direct.length ? 'ARKit / RPM' :
						cov.expression.length ? 'VRM / viseme' :
						mouthTarget.hasMouthMorphs() ? 'generic morphs' :
						mouthTarget.hasJawBone() ? 'jaw bone' : 'head fallback';
					const drivers = cov.direct.length ? `${cov.direct.length} ARKit morphs (direct)` :
						cov.expression.length ? `${cov.expression.length} viseme morphs (derived)` :
						'amplitude only';
					badgeEl.innerHTML = `<b>${conv}</b> · ${drivers}`;
				} catch (err) {
					loadingText.textContent = `Avatar load failed: ${err.message}`;
				}
			}

			function disposeModel(root) {
				root.traverse((n) => {
					if (n.geometry) n.geometry.dispose?.();
					if (n.material) (Array.isArray(n.material) ? n.material : [n.material]).forEach((m) => m.dispose?.());
				});
			}

			// ── Playback state ────────────────────────────────────────────────────
			let audioEl = null;
			let currentURL = null;
			let mode = 'idle';        // 'a2f' | 'amplitude' | 'idle'
			let amplitudeDriver = null;
			let audioTap = null;

			function tick() {
				requestAnimationFrame(tick);
				controls.update();
				if (avatarReady && mode === 'a2f' && audioEl && !audioEl.paused) {
					player.update(audioEl.currentTime);
					paintMeters((k) => player.currentWeight(k));
				}
				renderer.render(scene, camera);
			}
			tick();

			// ── Error banner ──────────────────────────────────────────────────────
			function clearError() { errEl.hidden = true; errEl.innerHTML = ''; }
			function showError(title, detail) {
				errEl.innerHTML = `<span><strong>${title}</strong> ${detail}</span>`;
				const btn = document.createElement('button');
				btn.type = 'button';
				btn.className = 'retry';
				btn.textContent = 'Retry';
				btn.addEventListener('click', speak);
				errEl.appendChild(btn);
				errEl.hidden = false;
			}

			function setStatus(text, live) {
				statusEl.innerHTML = live ? `<span class="live-dot"></span>${text}` : text;
			}
			// The label is translated copy until a run starts. While busy the script
			// owns it (so a late catalog pass cannot overwrite the spinner), and when
			// the run ends it gets back the localized label it had, not English.
			let idleLabel = null;
			function busy(on, label) {
				speakBtn.disabled = on;
				stopBtn.disabled = !on;
				if (on) {
					if (idleLabel === null) idleLabel = speakLabel.textContent;
					speakLabel.setAttribute('data-i18n-owned', '1');
					speakLabel.innerHTML = `<span class="btn-spin"></span>${label || 'Working…'}`;
				} else if (idleLabel !== null) {
					speakLabel.textContent = idleLabel;
					speakLabel.removeAttribute('data-i18n-owned');
					idleLabel = null;
				}
			}

			function teardownAudio() {
				if (amplitudeDriver) { amplitudeDriver.stop(); amplitudeDriver = null; }
				if (audioTap) { audioTap.disconnect(); audioTap = null; }
				if (audioEl) { audioEl.pause(); audioEl.src = ''; audioEl = null; }
				player.reset();
				mouthTarget.setMouthShape({ open: 0, wide: 0, round: 0 });
				clearMeters();
			}

			function onEnded() {
				mode = 'idle';
				busy(false);
				setStatus('Done.');
				player.reset();
				clearMeters();
			}

			// ── The pipeline ──────────────────────────────────────────────────────
			async function speak() {
				const text = $('text').value.trim();
				if (!text) { setStatus('Enter some text first.'); return; }
				if (!avatarReady) { setStatus('Avatar still loading…'); return; }

				clearError();
				teardownAudio();
				busy(true, 'Synthesizing…');
				const voice = $('voice').value;

				// Primary path: one round-trip to /api/a2f → Magpie audio + A2F track.
				setStatus('Generating voice + facial animation…', true);
				let data = null;
				try {
					const res = await fetch('/api/a2f', {
						method: 'POST',
						headers: { 'content-type': 'application/json' },
						body: JSON.stringify({ text, voice }),
					});
					if (res.ok) {
						data = await res.json();
					} else {
						const body = await res.json().catch(() => ({}));
						// 503 not_configured (or 404 in an environment where the route
						// isn't deployed yet) → drop to the amplitude fallback cleanly.
						console.info(`[a2f] endpoint returned ${res.status} (${body.error || ''}); using amplitude fallback`);
					}
				} catch (err) {
					console.info(`[a2f] request failed (${err.message}); using amplitude fallback`);
				}

				if (data?.animation?.frames?.length && data?.audio?.base64) {
					return playA2F(data);
				}
				// Fallback: synthesize via /api/tts/speak and drive amplitude lipsync.
				return playAmplitude(text, voice);
			}

			function makeAudio(blobUrl) {
				if (currentURL) URL.revokeObjectURL(currentURL);
				currentURL = blobUrl;
				const el = new Audio();
				el.crossOrigin = 'anonymous';
				el.src = blobUrl;
				el.addEventListener('ended', onEnded);
				el.addEventListener('error', () => {
					mode = 'idle'; busy(false);
					showError('Playback failed.', 'The generated audio could not be played. Try again.');
				});
				return el;
			}

			function b64ToBlob(b64, type) {
				const bin = atob(b64);
				const bytes = new Uint8Array(bin.length);
				for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
				return new Blob([bytes], { type });
			}

			async function playA2F(data) {
				const anim = data.animation;
				player.setTrack(anim);
				if (!player.hasCoverage()) {
					// Rig has no morphs A2F can drive — honour the track but the rig
					// can't show it, so fall back to jaw/amplitude motion instead.
					console.info('[a2f] track received but rig exposes no usable morphs; amplitude fallback');
					return playAmplitude($('text').value.trim(), $('voice').value, data.audio);
				}
				const blob = b64ToBlob(data.audio.base64, data.audio.contentType || 'audio/wav');
				audioEl = makeAudio(URL.createObjectURL(blob));
				mode = 'a2f';
				pathEl.className = 'pathline a2f';
				pathEl.innerHTML = `<b>Audio2Face-3D</b> · ${anim.frameCount} frames @ ${anim.fps} fps · ${anim.blendShapeNames.length} ARKit blendshapes · ${anim.durationSec.toFixed(1)}s`;
				try {
					await audioEl.play();
					busy(true, 'Speaking…');
					setStatus('Lips tracking the real audio.', true);
				} catch (err) {
					mode = 'idle'; busy(false);
					showError('Playback was blocked.', 'Your browser needs a click to play audio — press Retry.');
				}
			}

			async function playAmplitude(text, voice, existingAudio) {
				try {
					let blobUrl;
					if (existingAudio?.base64) {
						blobUrl = URL.createObjectURL(b64ToBlob(existingAudio.base64, existingAudio.contentType || 'audio/wav'));
					} else {
						setStatus('Generating voice…', true);
						const res = await fetch('/api/tts/speak', {
							method: 'POST',
							headers: { 'content-type': 'application/json' },
							body: JSON.stringify({ text, voice, format: 'wav' }),
						});
						if (!res.ok) {
							const detail = res.status === 429 ? 'Rate limit reached — wait a moment.' :
								res.status >= 500 ? 'The speech service hit an error.' : `HTTP ${res.status}.`;
							mode = 'idle'; busy(false);
							setStatus('');
							showError('Speech generation failed.', detail);
							return;
						}
						blobUrl = URL.createObjectURL(await res.blob());
					}

					audioEl = makeAudio(blobUrl);
					mode = 'amplitude';
					pathEl.className = 'pathline fallback';
					pathEl.innerHTML = `<b>Amplitude fallback</b> · Audio2Face unavailable — driving mouth shape from the audio envelope so the avatar never freezes.`;

					// Web Audio analyser → LipsyncDriver → AvatarMouthTarget.
					audioTap = tapAudioElement(audioEl);
					amplitudeDriver = new LipsyncDriver({ analyser: audioTap.analyser, target: mouthTarget });
					// Reflect the amplitude 'open' value on the JawOpen meter for feedback.
					const origSet = mouthTarget.setMouthShape.bind(mouthTarget);
					mouthTarget.setMouthShape = (s) => { origSet(s); paintMeters((k) => k === 'jawOpen' ? (s.open || 0) : 0); };
					amplitudeDriver.start();

					await audioEl.play();
					busy(true, 'Speaking…');
					setStatus('Speaking with amplitude lipsync.', true);
				} catch (err) {
					mode = 'idle'; busy(false);
					showError('Could not play audio.', err.message || 'Try again.');
				}
			}

			function stop() {
				teardownAudio();
				mode = 'idle';
				busy(false);
				setStatus('Stopped.');
			}

			speakBtn.addEventListener('click', speak);
			stopBtn.addEventListener('click', stop);
			$('avatar').addEventListener('change', () => { stop(); loadAvatar($('avatar').value); });

			// Probe capability so the UI can hint when A2F isn't configured upstream.
			fetch('/api/a2f', { method: 'GET' })
				.then((r) => r.ok ? r.json() : null)
				.then((cap) => {
					if (cap && cap.configured === false) {
						setStatus('Audio2Face is not configured on this deployment — the avatar will speak with amplitude lipsync.');
					}
				})
				.catch(() => {});

			loadAvatar($('avatar').value);
		</script>
		<script src="/brand.js" defer></script>
		<div id="footer-container"></div>
		<script src="/footer.js"></script>
		<!-- Runtime i18n: detects locale, swaps annotated copy, mounts <lang-switcher>. -->
	<script type="module" src="/i18n.js"></script>
</body>
</html>