import { Meta } from '@storybook/addon-docs/blocks';

<Meta title="Docs/Recipes/Text-to-Speech" />

# Text-to-speech (TTS)

## Browser-native (zero dependencies)

Speak each reply once it finishes streaming — call `speak(answer)` right before `chat.loading = false`:

```js
function speak(text) {
  if (!('speechSynthesis' in window)) return;
  const utter = new SpeechSynthesisUtterance(text);
  utter.lang = 'en-US';
  speechSynthesis.cancel();
  speechSynthesis.speak(utter);
}
```

## Cloud TTS (OpenAI, ElevenLabs, …)

For higher-quality voices, have your backend call a TTS API and return audio (keep the provider key server-side):

```js
async function speakCloud(text) {
  const res = await fetch('/api/tts', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ text, voice: 'alloy' }),
  });
  const audio = new Audio(URL.createObjectURL(await res.blob()));
  audio.play();
}
```
