From 20b3af5483c4694c1b5a5b040f7e2f5e5e506dab Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 16 Jun 2026 21:07:09 +0200 Subject: [PATCH] refac --- .../src/lib/components/chat/ChatInput.svelte | 2 + .../src/lib/components/chat/ChatPanel.svelte | 6 +- cptr/frontend/src/lib/stores/audio.ts | 60 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/cptr/frontend/src/lib/components/chat/ChatInput.svelte b/cptr/frontend/src/lib/components/chat/ChatInput.svelte index 1a4cb6c..7b8a1c9 100644 --- a/cptr/frontend/src/lib/components/chat/ChatInput.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatInput.svelte @@ -27,6 +27,7 @@ sttConfigured, ttsConfigured, ttsEnabled, + unlockTtsAudioPlayback, voiceModeEnabled, voiceModeSttMode } from '$lib/stores/audio'; @@ -808,6 +809,7 @@ const next = !$voiceModeEnabled; voiceModeEnabled.set(next); if (next) { + void unlockTtsAudioPlayback(); voiceWaitingForResponse = false; voiceSawStreaming = false; startVoiceRecognition(); diff --git a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte index 61250ca..ee1e9da 100644 --- a/cptr/frontend/src/lib/components/chat/ChatPanel.svelte +++ b/cptr/frontend/src/lib/components/chat/ChatPanel.svelte @@ -37,8 +37,10 @@ ttsEnabled, ttsConfigured, ttsFormat, + getTtsAudioElement, ttsPlaybackEnabled, ttsVoice, + unlockTtsAudioPlayback, voiceModeEnabled } from '$lib/stores/audio'; @@ -1078,6 +1080,7 @@ return; } stopTtsPlayback(); + void unlockTtsAudioPlayback(); speakingMessageId = messageId; ttsPlaybackEnabled.set(true); ttsErrorShown = false; @@ -1177,7 +1180,8 @@ if (generation !== ttsGeneration) break; if (ttsObjectUrl) URL.revokeObjectURL(ttsObjectUrl); ttsObjectUrl = URL.createObjectURL(blob); - ttsAudio = new Audio(ttsObjectUrl); + ttsAudio = getTtsAudioElement() ?? new Audio(); + ttsAudio.src = ttsObjectUrl; await new Promise((resolve, reject) => { const audio = ttsAudio!; audio.onended = () => resolve(); diff --git a/cptr/frontend/src/lib/stores/audio.ts b/cptr/frontend/src/lib/stores/audio.ts index 5e53376..33e1731 100644 --- a/cptr/frontend/src/lib/stores/audio.ts +++ b/cptr/frontend/src/lib/stores/audio.ts @@ -27,10 +27,70 @@ export const ttsPlaybackEnabled = writable( typeof localStorage !== 'undefined' ? localStorage.getItem('ttsPlaybackEnabled') === 'true' : false ); +const SILENT_WAV = + 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBAAAAABAAEAESsAACJWAAACABAAZGF0YQQAAAAAAA=='; + +let ttsAudioElement: HTMLAudioElement | null = null; +let ttsAudioContext: AudioContext | null = null; +let ttsAudioUnlocked = false; + ttsPlaybackEnabled.subscribe((v) => { if (typeof localStorage !== 'undefined') localStorage.setItem('ttsPlaybackEnabled', String(v)); }); +export function getTtsAudioElement(): HTMLAudioElement | null { + if (typeof Audio === 'undefined') return null; + if (!ttsAudioElement) { + ttsAudioElement = new Audio(); + ttsAudioElement.preload = 'auto'; + (ttsAudioElement as HTMLAudioElement & { playsInline?: boolean }).playsInline = true; + } + return ttsAudioElement; +} + +export async function unlockTtsAudioPlayback() { + if (typeof window === 'undefined') return; + + const unlocks: Promise[] = []; + const AudioContextCtor = + window.AudioContext || (window as Window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext; + try { + if (AudioContextCtor) { + ttsAudioContext ??= new AudioContextCtor(); + const buffer = ttsAudioContext.createBuffer(1, 1, 22050); + const source = ttsAudioContext.createBufferSource(); + source.buffer = buffer; + source.connect(ttsAudioContext.destination); + source.start(0); + if (ttsAudioContext.state !== 'running') unlocks.push(ttsAudioContext.resume()); + } + } catch {} + + const audio = getTtsAudioElement(); + if (audio && !ttsAudioUnlocked) { + try { + audio.src = SILENT_WAV; + audio.volume = 0; + const started = audio.play(); + unlocks.push( + Promise.resolve(started) + .then(() => { + ttsAudioUnlocked = true; + }) + .finally(() => { + audio.pause(); + audio.currentTime = 0; + audio.removeAttribute('src'); + audio.load(); + audio.volume = 1; + }) + ); + } catch {} + } + + await Promise.allSettled(unlocks); +} + export async function refreshAudioState() { try { const data = await fetchJSON<{