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.

26.7 KB
Raw Blame
import { getElevenKey, setElevenKey, clearElevenKey, withElevenKey, maskElevenKey } from './voice/eleven-key.js';
import { mountVoiceBrowser, statusLine, readLaneError } from './voice/voice-browser.js';

const $ = (id) => document.getElementById(id);

// ── Reading scripts ──────────────────────────────────────────────────────────

const SCRIPTS = [
	"Welcome to three.ws, the home of agentic 3D characters. I'm recording my voice to create a digital clone that will bring my avatar to life. This voice will power real-time conversations and text-to-speech synthesis across the platform.",
	"The quick brown fox jumps over the lazy dog. She sells seashells by the seashore. Peter Piper picked a peck of pickled peppers. How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
	"Technology should serve people, not the other way around. When we build tools that feel intuitive and accessible, everyone benefits. The future of human-computer interaction lies in natural, conversational interfaces: less like commanding a machine, more like talking to a friend.",
	"Three dot ws uses WebGL and Three.js to render real-time 3D avatars directly in the browser. Combined with instant voice cloning, speech recognition, and AI-powered conversation, we create fully interactive digital characters that can see, hear, and respond to people naturally.",
];

// ── Voice library (localStorage) ─────────────────────────────────────────────

const STORAGE_KEY = 'voicelab_voices_v1';

function loadVoices() {
	try {
		return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
	} catch { return []; }
}

function saveVoices(voices) {
	try { localStorage.setItem(STORAGE_KEY, JSON.stringify(voices)); } catch {}
}

function addVoice(voice) {
	const voices = loadVoices();
	voices.unshift(voice);
	if (voices.length > 20) voices.length = 20;
	saveVoices(voices);
}

function removeVoice(voiceId) {
	const voices = loadVoices().filter((v) => v.voiceId !== voiceId);
	saveVoices(voices);
}

// ── State ────────────────────────────────────────────────────────────────────

let state = 'idle';
let mediaStream = null;
let recorder = null;
let audioCtx = null;
let analyser = null;
let recordedBlob = null;
let recordedUrl = null;
let startTs = 0;
let rafId = 0;
let scriptIdx = 0;
let lastClonedVoiceId = null;

const MAX_RECORD_S = 60;
const RECOMMENDED_S = 25;

// ── Canvas setup ─────────────────────────────────────────────────────────────

const canvas = $('waveCanvas');
const ctx = canvas.getContext('2d');
let cW = 0;
let cH = 0;

function fitCanvas() {
	const dpr = window.devicePixelRatio || 1;
	cW = canvas.clientWidth;
	cH = canvas.clientHeight;
	canvas.width = cW * dpr;
	canvas.height = cH * dpr;
	ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
fitCanvas();
window.addEventListener('resize', fitCanvas);

// ── Level meter setup ────────────────────────────────────────────────────────

const levelMeter = $('levelMeter');
const LEVEL_BARS = 8;
for (let i = 0; i < LEVEL_BARS; i++) {
	const bar = document.createElement('div');
	bar.className = 'vl-level-bar';
	bar.style.height = '3px';
	levelMeter.appendChild(bar);
}

// ── Waveform visualization ───────────────────────────────────────────────────

const BAR_COUNT = 64;
let idlePhase = 0;
let smoothBars = new Float32Array(BAR_COUNT);

function roundRect(x, y, w, h, r) {
	if (ctx.roundRect) {
		ctx.roundRect(x, y, w, h, r);
	} else {
		ctx.moveTo(x + r, y);
		ctx.arcTo(x + w, y, x + w, y + h, r);
		ctx.arcTo(x + w, y + h, x, y + h, r);
		ctx.arcTo(x, y + h, x, y, r);
		ctx.arcTo(x, y, x + w, y, r);
		ctx.closePath();
	}
}

function lerpColor(t) {
	const r = Math.round(61 + t * (167 - 61));
	const g = Math.round(193 + t * (139 - 193));
	const b = 255;
	return `rgba(${r},${g},${b},`;
}
const barColors = Array.from({ length: BAR_COUNT }, (_, i) => lerpColor(i / BAR_COUNT));

function drawIdle() {
	idlePhase += 0.015;
	ctx.clearRect(0, 0, cW, cH);

	const centerY = cH / 2;
	const totalW = cW * 0.85;
	const startX = (cW - totalW) / 2;
	const barW = (totalW / BAR_COUNT) * 0.65;
	const gap = (totalW / BAR_COUNT) * 0.35;

	for (let i = 0; i < BAR_COUNT; i++) {
		const t = i / BAR_COUNT;
		const ambient = (Math.sin(idlePhase + t * 6) * 0.5 + 0.5) * 0.08 + 0.02;
		smoothBars[i] += (ambient - smoothBars[i]) * 0.08;
		const h = smoothBars[i] * centerY;
		const x = startX + i * (barW + gap);
		const alpha = 0.4 + smoothBars[i] * 3;

		ctx.fillStyle = barColors[i] + alpha + ')';
		ctx.beginPath();
		roundRect(x, centerY - h, barW, h, 2);
		ctx.fill();
		ctx.beginPath();
		roundRect(x, centerY + 1, barW, h, 2);
		ctx.fill();
	}
}

function drawLive() {
	if (!analyser) return;
	const data = new Uint8Array(analyser.frequencyBinCount);
	analyser.getByteFrequencyData(data);

	ctx.clearRect(0, 0, cW, cH);

	const centerY = cH / 2;
	const totalW = cW * 0.85;
	const startX = (cW - totalW) / 2;
	const barW = (totalW / BAR_COUNT) * 0.65;
	const gap = (totalW / BAR_COUNT) * 0.35;
	const binsPerBar = Math.floor(data.length / BAR_COUNT);

	let rmsSum = 0;

	for (let i = 0; i < BAR_COUNT; i++) {
		let sum = 0;
		const start = i * binsPerBar;
		for (let j = start; j < start + binsPerBar; j++) sum += data[j];
		const raw = (sum / binsPerBar) / 255;
		rmsSum += raw * raw;
		smoothBars[i] += (raw - smoothBars[i]) * 0.25;
		const val = smoothBars[i];
		const h = Math.max(2, val * centerY * 0.92);
		const x = startX + i * (barW + gap);
		const alpha = 0.5 + val * 0.5;

		ctx.fillStyle = barColors[i] + alpha + ')';
		ctx.beginPath();
		roundRect(x, centerY - h, barW, h, 2);
		ctx.fill();
		ctx.beginPath();
		roundRect(x, centerY + 1, barW, h, 2);
		ctx.fill();
	}

	updateLevelMeter(Math.sqrt(rmsSum / BAR_COUNT));
}

function updateLevelMeter(rms) {
	const bars = levelMeter.children;
	for (let i = 0; i < LEVEL_BARS; i++) {
		const threshold = (i + 1) / LEVEL_BARS;
		const active = rms > threshold * 0.6;
		const h = active ? Math.min(20, 4 + rms * 20) : 3;
		bars[i].style.height = h + 'px';
		if (i >= LEVEL_BARS - 2 && active) {
			bars[i].style.background = '#ef4444';
		} else if (i >= LEVEL_BARS - 4 && active) {
			bars[i].style.background = '#fbbf24';
		} else {
			bars[i].style.background = '#3dc1ff';
		}
	}
}

function animationLoop() {
	rafId = requestAnimationFrame(animationLoop);
	if (state === 'recording' && analyser) {
		drawLive();
		updateTimer();
	} else {
		drawIdle();
	}
}
animationLoop();

// ── Timer ────────────────────────────────────────────────────────────────────

function updateTimer() {
	const elapsed = (performance.now() - startTs) / 1000;
	const m = Math.floor(elapsed / 60);
	const s = Math.floor(elapsed % 60);
	$('timer').textContent = `${m}:${s.toString().padStart(2, '0')}`;

	const pct = Math.min(100, (elapsed / RECOMMENDED_S) * 100);
	$('progress').style.width = pct + '%';

	if (elapsed >= RECOMMENDED_S) {
		$('timerSub').textContent = 'Great length! You can stop whenever you\'re ready.';
		$('timerSub').style.color = '#34d399';
	} else {
		const remaining = Math.ceil(RECOMMENDED_S - elapsed);
		$('timerSub').textContent = `${remaining}s more recommended`;
		$('timerSub').style.color = '';
	}

	if (elapsed >= MAX_RECORD_S) {
		setStatus('info', `Reached the ${MAX_RECORD_S}s limit, stopping automatically.`);
		stopRecording();
	}
}

// ── Scripts ──────────────────────────────────────────────────────────────────

function renderScript() {
	$('scriptText').textContent = `"${SCRIPTS[scriptIdx]}"`;
	const dotsEl = $('scriptDots');
	dotsEl.innerHTML = '';
	for (let i = 0; i < SCRIPTS.length; i++) {
		const dot = document.createElement('div');
		dot.className = 'vl-script-dot' + (i === scriptIdx ? ' active' : '');
		dotsEl.appendChild(dot);
	}
}
renderScript();

$('scriptNext').addEventListener('click', () => {
	scriptIdx = (scriptIdx + 1) % SCRIPTS.length;
	renderScript();
});

// ── Recording ────────────────────────────────────────────────────────────────

function pickMime() {
	const candidates = [
		'audio/webm;codecs=opus',
		'audio/webm',
		'audio/ogg;codecs=opus',
		'audio/mp4',
	];
	for (const m of candidates) {
		if (window.MediaRecorder && MediaRecorder.isTypeSupported(m)) return m;
	}
	return '';
}

async function startRecording() {
	try {
		mediaStream = await navigator.mediaDevices.getUserMedia({
			audio: {
				channelCount: 1,
				echoCancellation: true,
				noiseSuppression: true,
				autoGainControl: true,
				sampleRate: 48000,
			},
		});
	} catch (err) {
		setStatus('err', `Microphone access denied: ${err.message}`);
		return;
	}

	audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 48000 });
	const src = audioCtx.createMediaStreamSource(mediaStream);
	analyser = audioCtx.createAnalyser();
	analyser.fftSize = 2048;
	analyser.smoothingTimeConstant = 0.75;
	src.connect(analyser);

	const mime = pickMime();
	const chunks = [];
	try {
		recorder = mime
			? new MediaRecorder(mediaStream, { mimeType: mime })
			: new MediaRecorder(mediaStream);
	} catch (err) {
		setStatus('err', `Could not start recorder: ${err.message}`);
		return;
	}

	recorder.ondataavailable = (e) => {
		if (e.data?.size) chunks.push(e.data);
	};
	recorder.onstop = () => {
		const blobMime = recorder.mimeType || mime || 'audio/webm';
		recordedBlob = new Blob(chunks, { type: blobMime });
		if (recordedUrl) URL.revokeObjectURL(recordedUrl);
		recordedUrl = URL.createObjectURL(recordedBlob);
		$('reviewAudio').src = recordedUrl;

		const elapsed = (performance.now() - startTs) / 1000;
		if (elapsed < 3) {
			setStatus('err', 'Recording too short. Please record at least 3 seconds.');
			setState('idle');
			return;
		}

		setStatus('ok', `Captured ${(recordedBlob.size / 1024).toFixed(0)} KB (${Math.round(elapsed)}s). Review and clone below.`);
		setState('review');
	};

	recorder.start(250);
	startTs = performance.now();
	setState('recording');
	setStatus('info', 'Recording... read the script aloud or speak naturally.');
}

function stopRecording() {
	if (recorder?.state === 'recording') recorder.stop();
	mediaStream?.getTracks().forEach((t) => t.stop());
	audioCtx?.close().catch(() => {});
	analyser = null;
	audioCtx = null;
	mediaStream = null;
}

function resetRecording() {
	recordedBlob = null;
	if (recordedUrl) { URL.revokeObjectURL(recordedUrl); recordedUrl = null; }
	$('reviewAudio').removeAttribute('src');
	$('progress').style.width = '0%';
	$('timer').textContent = '0:00';
	$('timerSub').textContent = 'Recommended: 20-30 seconds';
	$('timerSub').style.color = '';
	smoothBars.fill(0);
	setState('idle');
	setStatus('info', 'Ready to record.');
}

// ── Clone ────────────────────────────────────────────────────────────────────

async function cloneVoice() {
	if (!recordedBlob) return;
	const name = $('voiceName').value.trim();
	if (!name) {
		setStatus('err', 'Enter a name for your voice.');
		$('voiceName').focus();
		return;
	}

	setState('cloning');
	setStatus('info', 'Uploading sample and cloning...');

	const ext = recordedBlob.type.includes('mp4') ? 'm4a'
		: recordedBlob.type.includes('ogg') ? 'ogg'
		: recordedBlob.type.includes('wav') ? 'wav'
		: 'webm';

	const fd = new FormData();
	fd.append('audio', recordedBlob, `sample.${ext}`);
	fd.append('name', name);

	let res;
	try {
		res = await fetch('/api/tts/eleven-clone', {
			method: 'POST',
			credentials: 'include',
			headers: withElevenKey(),
			body: fd,
		});
	} catch (err) {
		setStatus('err', `Network error: ${err.message}`);
		setState('review');
		return;
	}

	let body;
	try {
		body = await res.json();
	} catch {
		body = { error_description: await res.text().catch(() => '') };
	}

	if (!res.ok) {
		const msg = statusLine(body.message || body.error_description || body.error || `HTTP ${res.status}`);
		setStatus('err', `Clone failed: ${msg}`);
		setState('review');
		return;
	}

	const voiceId = body.voice_id;
	lastClonedVoiceId = voiceId;

	addVoice({
		voiceId,
		name,
		createdAt: Date.now(),
		status: body.status || 'ready',
	});

	$('doneText').textContent = `"${name}" cloned successfully`;
	setStatus('ok', `Voice cloned. ID: ${voiceId}`);
	setState('done');
	renderLibrary();
	renderPlaygroundVoices();
}

// ── State machine ────────────────────────────────────────────────────────────

function setState(newState) {
	state = newState;
	$('recorder').dataset.state = newState;
}

function setStatus(type, text) {
	const el = $('status');
	el.className = 'vl-status' + (type !== 'info' ? ` ${type}` : '');
	el.textContent = text;
}

// ── Voice library rendering ──────────────────────────────────────────────────

function renderLibrary() {
	const voices = loadVoices();
	const container = $('voiceLibrary');

	if (!voices.length) {
		container.innerHTML = `
			<div class="vl-empty">
				<div class="vl-empty-title">No cloned voices yet</div>
				Record a voice sample above to get started.
			</div>`;
		return;
	}

	container.innerHTML = `<div class="vl-voices">${
		voices.map((v) => {
			const age = timeAgo(v.createdAt);
			return `
				<div class="vl-voice-card" data-vid="${esc(v.voiceId)}">
					<div class="vl-voice-name">${esc(v.name)}</div>
					<div class="vl-voice-meta">
						<span>${age}</span>
					</div>
					<div class="vl-voice-id">${esc(v.voiceId)}</div>
					<div class="vl-voice-actions">
						<button class="vl-btn vl-btn-ghost vl-btn-sm" data-play="${esc(v.voiceId)}">Play sample</button>
						<button class="vl-btn vl-btn-danger vl-btn-sm" data-del="${esc(v.voiceId)}">Remove</button>
					</div>
				</div>`;
		}).join('')
	}</div>`;
}

function esc(s) {
	return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

function timeAgo(ts) {
	const diff = Date.now() - ts;
	if (diff < 60000) return 'Just now';
	if (diff < 3600000) return `${Math.floor(diff / 60000)}m ago`;
	if (diff < 86400000) return `${Math.floor(diff / 3600000)}h ago`;
	return `${Math.floor(diff / 86400000)}d ago`;
}

// ── Playground ───────────────────────────────────────────────────────────────
//
// One picker over every lane. Options are keyed "<provider>:<voiceId>" so the
// same select can hold a cloned ElevenLabs voice, a free Edge neural voice and
// a Gemini prebuilt without the ids ever colliding.

// Voices the browser handed over, in pick order (most recent first). Kept in
// memory only: the browser re-fetches the catalog on load, so persisting them
// would risk offering a voice the account no longer has.
let pickedVoices = [];
let providerMeta = [];

function voiceKey(provider, id) {
	return `${provider}:${id}`;
}

function providerFor(id) {
	return providerMeta.find((p) => p.id === id) || null;
}

/** Add (or promote) a voice the user selected in the browser, then select it. */
function usePlaygroundVoice(voice) {
	const key = voiceKey(voice.provider, voice.id);
	pickedVoices = [
		{ key, provider: voice.provider, voiceId: voice.id, name: voice.name },
		...pickedVoices.filter((v) => v.key !== key),
	].slice(0, 12);
	renderPlaygroundVoices(key);
	$('playgroundSection').scrollIntoView({ behavior: 'smooth', block: 'center' });
}

/** Called whenever the catalog is (re-)read, so the controls track the lanes. */
function setProviderMeta(providers) {
	providerMeta = providers;
	// A lane that has gone down since the pick cannot render the voices parked in
	// the picker, so they leave with it rather than sitting there as a selection
	// that can only fail.
	const live = new Set(providers.filter((p) => p.available).map((p) => p.id));
	const kept = pickedVoices.filter((v) => live.has(v.provider));
	if (kept.length !== pickedVoices.length) {
		pickedVoices = kept;
		renderPlaygroundVoices();
		return;
	}
	renderModelOptions();
}

function renderPlaygroundVoices(preferKey = null) {
	const cloned = loadVoices();
	const sel = $('pgVoice');
	const prev = sel.value;

	sel.innerHTML = '';
	if (!cloned.length && !pickedVoices.length) {
		sel.innerHTML = '<option value="" disabled selected>Pick a voice above</option>';
		$('pgSpeak').disabled = true;
		renderModelOptions();
		return;
	}

	const addGroup = (label, items) => {
		if (!items.length) return;
		const group = document.createElement('optgroup');
		group.label = label;
		items.forEach((v) => {
			const opt = document.createElement('option');
			opt.value = v.key;
			opt.textContent = v.name;
			group.appendChild(opt);
		});
		sel.appendChild(group);
	};

	addGroup(
		'My cloned voices',
		cloned.map((v) => ({
			key: voiceKey('elevenlabs', v.voiceId),
			name: v.name,
		})),
	);
	addGroup('Picked from the browser', pickedVoices);

	const options = [...sel.options].map((o) => o.value);
	const wanted = [
		preferKey,
		lastClonedVoiceId ? voiceKey('elevenlabs', lastClonedVoiceId) : null,
		prev,
	].find((k) => k && options.includes(k));
	sel.value = wanted || options[0];

	$('pgSpeak').disabled = false;
	renderModelOptions();
}

/** Show the model select and the direction field only where the lane has them. */
function renderModelOptions() {
	const { provider } = currentSelection();
	const meta = providerFor(provider);
	const models = meta?.models || [];
	const modelField = $('pgModelField');
	const modelSel = $('pgModel');

	if (!models.length) {
		modelField.hidden = true;
		modelSel.innerHTML = '';
	} else {
		const prev = modelSel.value;
		modelSel.innerHTML = models
			.map((m) => `<option value="${esc(m.id)}" title="${esc(m.note || '')}">${esc(m.label)}</option>`)
			.join('');
		if (prev && models.some((m) => m.id === prev)) modelSel.value = prev;
		modelField.hidden = false;
	}

	$('pgDirectionRow').hidden = !meta?.direction;
}

function currentSelection() {
	const raw = $('pgVoice').value || '';
	const idx = raw.indexOf(':');
	if (idx === -1) return { provider: '', voiceId: '' };
	return { provider: raw.slice(0, idx), voiceId: raw.slice(idx + 1) };
}

async function speakPlayground() {
	const { provider, voiceId } = currentSelection();
	const text = $('pgText').value.trim();
	if (!provider || !voiceId || !text) return;

	$('pgSpeak').disabled = true;
	$('pgHint').textContent = 'Synthesizing...';

	const payload = { provider, voiceId, text, speed: Number($('pgSpeed').value) || 1 };
	const modelSel = $('pgModel');
	if (!$('pgModelField').hidden && modelSel.value) payload.model = modelSel.value;
	if (!$('pgDirectionRow').hidden) {
		const direction = $('pgDirection').value.trim();
		if (direction) payload.direction = direction;
	}

	try {
		const r = await fetch('/api/tts/synthesize', {
			method: 'POST',
			credentials: 'include',
			headers: withElevenKey({ 'content-type': 'application/json' }),
			body: JSON.stringify(payload),
		});

		if (!r.ok) {
			const body = await r.clone().json().catch(() => ({}));
			const { message, laneDown } = await readLaneError(r);
			// The lane is gone, not busy: re-read the catalog so the picker drops
			// the voices it can no longer render.
			if (laneDown) voiceBrowser.reload({ silent: true });
			const topUp = body.top_up_url ? ` Top up at ${body.top_up_url}.` : '';
			throw new Error(message + topUp);
		}

		const cacheHit = r.headers.get('x-tts-cache') === 'hit';
		const billing = r.headers.get('x-tts-billing');
		const chargedUsd = Number(r.headers.get('x-tts-charged-usd') || 0);
		const contentType = r.headers.get('content-type') || 'audio/mpeg';
		const buf = await r.arrayBuffer();
		const blob = new Blob([buf], { type: contentType });
		const url = URL.createObjectURL(blob);

		const audio = $('pgAudio');
		if (audio.src) URL.revokeObjectURL(audio.src);
		audio.src = url;
		$('pgOutput').classList.add('visible');
		audio.play().catch(() => {});

		const billingNote =
			billing === 'byok' ? ' · your ElevenLabs key'
			: billing === 'gcp' ? ' · free on platform credits'
			: billing === 'free' ? ' · free lane'
			: chargedUsd > 0 ? ` · $${chargedUsd.toFixed(4)} credits`
			: '';
		$('pgHint').textContent = `${(buf.byteLength / 1024).toFixed(0)} KB · ${cacheHit ? 'cached' : 'generated'}${billingNote}`;
	} catch (err) {
		$('pgHint').textContent = `Error: ${err.message}`;
	} finally {
		$('pgSpeak').disabled = false;
	}
}

async function playVoiceSample(voiceId) {
	const voice = loadVoices().find((v) => v.voiceId === voiceId);
	if (!voice) return;

	const btn = document.querySelector(`[data-play="${voiceId}"]`);
	if (btn) { btn.disabled = true; btn.textContent = 'Loading...'; }

	try {
		const r = await fetch('/api/tts/synthesize', {
			method: 'POST',
			credentials: 'include',
			headers: withElevenKey({ 'content-type': 'application/json' }),
			body: JSON.stringify({
				provider: 'elevenlabs',
				voiceId,
				text: "Hello, I'm your cloned voice. How does this sound?",
			}),
		});
		if (!r.ok) {
			const body = await r.json().catch(() => ({}));
			throw new Error(statusLine(body.message || body.error_description || `HTTP ${r.status}`));
		}
		const blob = await r.blob();
		const audio = new Audio(URL.createObjectURL(blob));
		audio.onended = () => URL.revokeObjectURL(audio.src);
		audio.play();
	} catch (err) {
		setStatus('err', `Playback failed: ${err.message}`);
	} finally {
		if (btn) { btn.disabled = false; btn.textContent = 'Play sample'; }
	}
}

function deleteVoice(voiceId) {
	removeVoice(voiceId);
	renderLibrary();
	renderPlaygroundVoices();
	setStatus('info', 'Voice removed from library.');
}

// ── Event wiring ─────────────────────────────────────────────────────────────

$('btnRecord').addEventListener('click', startRecording);
$('btnStop').addEventListener('click', stopRecording);
$('btnRerecord').addEventListener('click', resetRecording);
$('btnClone').addEventListener('click', cloneVoice);

$('btnTryPlayground').addEventListener('click', () => {
	$('playgroundSection').scrollIntoView({ behavior: 'smooth', block: 'center' });
});
$('btnRecordAnother').addEventListener('click', resetRecording);

$('pgSpeak').addEventListener('click', speakPlayground);

$('pgText').addEventListener('keydown', (e) => {
	if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
		e.preventDefault();
		speakPlayground();
	}
});

document.querySelectorAll('.vl-pg-sample').forEach((btn) => {
	btn.addEventListener('click', () => {
		$('pgText').value = btn.dataset.text;
	});
});

$('voiceLibrary').addEventListener('click', (e) => {
	const playBtn = e.target.closest('[data-play]');
	if (playBtn) { playVoiceSample(playBtn.dataset.play); return; }
	const delBtn = e.target.closest('[data-del]');
	if (delBtn) { deleteVoice(delBtn.dataset.del); }
});

$('pgVoice').addEventListener('change', () => {
	$('pgSpeak').disabled = !$('pgVoice').value;
	renderModelOptions();
});

$('pgSpeed').addEventListener('input', () => {
	$('pgSpeedVal').textContent = `${Number($('pgSpeed').value).toFixed(2)}×`;
});

// Keyboard shortcut: Space toggles record/stop, but only when the page itself
// has focus. Space is also how a keyboard user presses a focused button or
// checkbox, so claiming it unconditionally silently broke every button on the
// page for anyone navigating by Tab: preventDefault swallowed the activation
// and started a recording instead.
const KEYBOARD_ACTIVATED = 'a, button, input, textarea, select, summary, [contenteditable=""], [contenteditable="true"], [tabindex]:not([tabindex="-1"])';

function ownsTheSpaceBar(target) {
	return target instanceof Element && Boolean(target.closest(KEYBOARD_ACTIVATED));
}

document.addEventListener('keydown', (e) => {
	if (e.code !== 'Space' || e.metaKey || e.ctrlKey || e.altKey) return;
	if (ownsTheSpaceBar(e.target)) return;
	e.preventDefault();
	if (state === 'idle') startRecording();
	else if (state === 'recording') stopRecording();
});

// ── BYOK: your own ElevenLabs key ────────────────────────────────────────────

function renderByokState() {
	const key = getElevenKey();
	$('byokClear').hidden = !key;
	$('byokKey').placeholder = key ? maskElevenKey(key) : 'sk_...';
	$('byokSave').textContent = key ? 'Update key' : 'Save key';
	$('byokStatus').textContent = key
		? `Using your ElevenLabs account (${maskElevenKey(key)}). Clones and speech are billed to it.`
		: 'Using the three.ws account: clones and speech are metered to your credit balance.';
}

$('byokSave').addEventListener('click', () => {
	const value = $('byokKey').value.trim();
	if (!value) {
		$('byokStatus').textContent = 'Paste your ElevenLabs API key first.';
		$('byokKey').focus();
		return;
	}
	setElevenKey(value);
	$('byokKey').value = '';
	renderByokState();
	voiceBrowser.reload();
});

$('byokKey').addEventListener('keydown', (e) => {
	if (e.key === 'Enter') {
		e.preventDefault();
		$('byokSave').click();
	}
});

$('byokClear').addEventListener('click', () => {
	clearElevenKey();
	$('byokKey').value = '';
	renderByokState();
	voiceBrowser.reload();
});

// ── Init ─────────────────────────────────────────────────────────────────────

renderLibrary();
renderPlaygroundVoices();
renderByokState();

// The browser owns discovery (every lane, every voice); the playground owns
// synthesis. They meet here: a pick in the browser becomes the playground's
// selected voice, and the catalog's provider metadata decides which playground
// controls (model, direction) are even meaningful.
const voiceBrowser = mountVoiceBrowser({
	root: $('browserSection'),
	onCatalog: setProviderMeta,
	onSelect: usePlaygroundVoice,
});

// Auto-populate voice name with a sensible default
const now = new Date();
$('voiceName').value = `My Voice ${now.toLocaleDateString('en-US', { month: 'short', day: 'numeric' })}`;