Implementation:23blocks OS Ai maestro Use TTS Hook
| Knowledge Sources | |
|---|---|
| Domains | UI, Text_To_Speech |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete tool for multi-provider text-to-speech management — Web Speech, OpenAI, ElevenLabs — with per-agent config, voice loading, mute, and audio element access, provided by AI Maestro's `hooks/useTTS.ts`.
Description
The useTTS hook manages TTS for the companion voice panel. It creates/swaps providers (Web Speech API, OpenAI TTS, ElevenLabs) based on config, loads available voices, and exposes `speak(text)`, `stop()`, `toggleMute()`, and `getAudioElement()` (returns the `<audio>` playing, for waveform analysis; null for web-speech). Config is persisted per agent in localStorage (`companion-tts-<agentId>`). Config and voices are kept in refs so `speak()` always reads the latest values without re-creating the callback. Stopping in-progress speech when swapping providers prevents overlap.
Usage
Call with `agentId`. Read `isSpeaking`, `isMuted`, `config`, `availableVoices`. Call `speak(text)` to speak, `stop()` to halt, `toggleMute()` to mute. Pass `getAudioElement` to `useSpeechLevel` for waveform-driven animation.
Code Reference
Source Location
- Repository: ai-maestro
- File: hooks/useTTS.ts
- Lines: L1-L181
Signature
export function useTTS(options: { agentId: string }): {
isSpeaking: boolean
isMuted: boolean
config: TTSConfig
availableVoices: TTSVoice[]
toggleMute: () => void
setConfig: (update: Partial<TTSConfig>) => void
speak: (text: string) => void
stop: () => void
getAudioElement: () => HTMLAudioElement | null
}
Import
import { useTTS } from '@/hooks/useTTS'
I/O Contract
| Name | Type | Description |
|---|---|---|
| isSpeaking | boolean | True while speech is in progress |
| availableVoices | TTSVoice[] | Voices for the current provider |
| speak | (text: string) => void | Speak text (chunked if long) |
| getAudioElement | null | For waveform analysis (null for web-speech) |
Usage Examples
import { useTTS } from '@/hooks/useTTS'
function VoicePanel({ agentId }) {
const { speak, stop, isMuted, toggleMute, getAudioElement } = useTTS({ agentId })
return (
<>
<button onClick={() => speak('Hello world')}>Speak</button>
<button onClick={stop}>Stop</button>
<button onClick={toggleMute}>{isMuted ? 'Unmute' : 'Mute'}</button>
</>
)
}