From bee2a271b4f854885d7f694798dabeac1d27699e Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Mon, 15 Dec 2025 00:26:05 -0500 Subject: [PATCH] Add TTS context implementation for centralized audio state management --- frontend/src/contexts/TTSContext.tsx | 259 +++++++++++++++++++++++++++ frontend/src/contexts/tts-context.ts | 17 ++ 2 files changed, 276 insertions(+) create mode 100644 frontend/src/contexts/TTSContext.tsx create mode 100644 frontend/src/contexts/tts-context.ts diff --git a/frontend/src/contexts/TTSContext.tsx b/frontend/src/contexts/TTSContext.tsx new file mode 100644 index 00000000..2b61d19d --- /dev/null +++ b/frontend/src/contexts/TTSContext.tsx @@ -0,0 +1,259 @@ +import { useState, useRef, useCallback, useEffect, type ReactNode } from 'react' +import { useSettings } from '@/hooks/useSettings' +import { API_BASE_URL } from '@/config' +import { TTSContext, type TTSState } from './tts-context' + +export { TTSContext, type TTSContextValue, type TTSState } from './tts-context' + +const SENTENCE_REGEX = /(?<=[.!?])\s+/ +const SENTENCES_PER_CHUNK = 2 + +function splitIntoChunks(text: string): string[] { + const sentences = text.split(SENTENCE_REGEX).filter(s => s.trim().length > 0) + if (sentences.length === 0) return [text] + + const chunks: string[] = [] + for (let i = 0; i < sentences.length; i += SENTENCES_PER_CHUNK) { + const chunk = sentences.slice(i, i + SENTENCES_PER_CHUNK).join(' ') + if (chunk.trim()) chunks.push(chunk.trim()) + } + + return chunks.length > 0 ? chunks : [text] +} + +interface TTSProviderProps { + children: ReactNode +} + +export function TTSProvider({ children }: TTSProviderProps) { + const { preferences } = useSettings() + const [state, setState] = useState('idle') + const [error, setError] = useState(null) + const [currentText, setCurrentText] = useState(null) + + const abortControllerRef = useRef(null) + const audioRef = useRef(null) + const stoppedRef = useRef(false) + const chunksRef = useRef([]) + const chunkIndexRef = useRef(0) + const prefetchedBlobsRef = useRef>(new Map()) + const fetchingIndexRef = useRef(-1) + + const ttsConfig = preferences?.tts + const isEnabled = !!(ttsConfig?.enabled && ttsConfig?.apiKey) + + const cleanup = useCallback(() => { + if (audioRef.current) { + audioRef.current.pause() + audioRef.current.src = '' + audioRef.current = null + } + if (abortControllerRef.current) { + abortControllerRef.current.abort() + abortControllerRef.current = null + } + prefetchedBlobsRef.current.forEach((_, key) => { + prefetchedBlobsRef.current.delete(key) + }) + prefetchedBlobsRef.current.clear() + chunksRef.current = [] + chunkIndexRef.current = 0 + fetchingIndexRef.current = -1 + }, []) + + const stop = useCallback(() => { + stoppedRef.current = true + cleanup() + setState('idle') + setCurrentText(null) + setError(null) + }, [cleanup]) + + useEffect(() => { + return () => { + stoppedRef.current = true + cleanup() + } + }, [cleanup]) + + const synthesize = useCallback(async (text: string, signal?: AbortSignal): Promise => { + if (stoppedRef.current) return null + + try { + const response = await fetch(`${API_BASE_URL}/api/tts/synthesize`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ text }), + signal, + }) + + if (stoppedRef.current) return null + + if (!response.ok) { + let errorMessage = 'TTS request failed' + try { + const errorData = await response.json() + errorMessage = errorData.error || errorData.details || errorMessage + } catch { + if (response.status === 401) errorMessage = 'Invalid API key' + else if (response.status === 429) errorMessage = 'Rate limit exceeded' + else if (response.status >= 500) errorMessage = 'Service unavailable' + } + throw new Error(errorMessage) + } + + const contentType = response.headers.get('content-type') + if (!contentType?.includes('audio')) { + throw new Error('Invalid response from TTS service') + } + + const blob = await response.blob() + if (blob.size === 0) { + throw new Error('Empty audio response') + } + + return blob + } catch (err) { + if (err instanceof Error && err.name === 'AbortError') { + return null + } + throw err + } + }, []) + + const fetchNextChunk = useCallback(async (index: number) => { + if (stoppedRef.current) return + if (index >= chunksRef.current.length) return + if (prefetchedBlobsRef.current.has(index)) { + fetchNextChunk(index + 1) + return + } + + fetchingIndexRef.current = index + + try { + const blob = await synthesize(chunksRef.current[index], abortControllerRef.current?.signal) + if (blob && !stoppedRef.current) { + prefetchedBlobsRef.current.set(index, blob) + fetchNextChunk(index + 1) + } + } catch { + if (stoppedRef.current) return + } + + fetchingIndexRef.current = -1 + }, [synthesize]) + + const playChunk = useCallback(async (index: number) => { + if (stoppedRef.current || index >= chunksRef.current.length) { + if (!stoppedRef.current) { + setState('idle') + setCurrentText(null) + } + return + } + + chunkIndexRef.current = index + + try { + let blob: Blob | undefined = prefetchedBlobsRef.current.get(index) + + if (!blob) { + setState('loading') + const fetched = await synthesize(chunksRef.current[index], abortControllerRef.current?.signal) + if (fetched && !stoppedRef.current) { + blob = fetched + } + fetchNextChunk(index + 1) + } + + if (!blob || stoppedRef.current) return + + prefetchedBlobsRef.current.delete(index) + + const url = URL.createObjectURL(blob) + const audio = new Audio(url) + audioRef.current = audio + + audio.onended = () => { + URL.revokeObjectURL(url) + audioRef.current = null + if (!stoppedRef.current) { + playChunk(index + 1) + } + } + + audio.onerror = () => { + URL.revokeObjectURL(url) + audioRef.current = null + if (!stoppedRef.current) { + setError('Audio playback failed') + setState('error') + } + } + + setState('playing') + await audio.play() + } catch (err) { + if (stoppedRef.current) return + setError(err instanceof Error ? err.message : 'TTS failed') + setState('error') + } + }, [synthesize, fetchNextChunk]) + + const speak = useCallback(async (text: string): Promise => { + if (!ttsConfig?.enabled) { + setError('TTS is not enabled') + setState('error') + return false + } + + if (!ttsConfig?.apiKey) { + setError('API key not configured') + setState('error') + return false + } + + if (!text?.trim()) { + setError('No text provided') + setState('error') + return false + } + + if (!ttsConfig.voice || !ttsConfig.model) { + setError('Voice or model not configured') + setState('error') + return false + } + + stop() + stoppedRef.current = false + setError(null) + setCurrentText(text) + + abortControllerRef.current = new AbortController() + chunksRef.current = splitIntoChunks(text) + + playChunk(0) + + return true + }, [ttsConfig, stop, playChunk]) + + const value = { + speak, + stop, + state, + error, + currentText, + isEnabled, + isPlaying: state === 'playing', + isLoading: state === 'loading', + isIdle: state === 'idle', + } + + return ( + + {children} + + ) +} diff --git a/frontend/src/contexts/tts-context.ts b/frontend/src/contexts/tts-context.ts new file mode 100644 index 00000000..fe05a101 --- /dev/null +++ b/frontend/src/contexts/tts-context.ts @@ -0,0 +1,17 @@ +import { createContext } from 'react' + +export type TTSState = 'idle' | 'loading' | 'playing' | 'error' + +export interface TTSContextValue { + speak: (text: string) => Promise + stop: () => void + state: TTSState + error: string | null + currentText: string | null + isEnabled: boolean + isPlaying: boolean + isLoading: boolean + isIdle: boolean +} + +export const TTSContext = createContext(null)