---
name: ferret-speech
description: Use for text to speech and speech to text in Ferret (@ferret/speech): characters that speak lines at their position, synthesised clips and their cache, microphone capture and recognition, by the player's session on the Camerlengo API; speech events, speech.* commands, the ?speech viewer.
---

# Speech

## When to use
An NPC or narrator speaks a line from text; the player talks to the game (a command, an answer, a chat line); a recording is turned into text.

## The model
`SpeechService` (`installSpeech(world, opts?)`) gives primitives and binds no input: `speak(entity | null, text, {voice, language, interrupt})` plays on the voice bus at the entity, lines of one speaker queue; `synthesize(text)` returns a clip (MP3) and caches it; `listen()` -> `stop()` (the text) or `cancel()` (nothing sent); `recognize(clip | bytes | pcm)`. Requests go to Camerlengo (`ai:tts`, `ai:stt`) with the signed-in player's session from `AccountService`, so the player's wallet pays; `client`, `session`, `key` or a whole `backend` override it. Events at the fixed step: `speech:start`, `speech:end`, `speech:heard`, `speech:listening`, `speech:error`.

## Steps
1. Register `AudioService` and `AccountService` (with a `CamerlengoClient` on `https://squirrelwisdom.com/` and the public key that has `ai:tts` and `ai:stt`), then `installSpeech(world)`.
2. Unlock sound on the first user action (`audio.unlock()`).
3. Speak: `speech.speak(npc, text, { voice, language })`; always give `language` (otherwise the server pays a detection call per line). Pre-synthesise known lines with `synthesize` to hide the round trip.
4. Listen: bind the game's own action to `listen()` and `stop()`; show `level` as a meter; handle `SpeechError` codes.
5. Idle people: give the micromotion person the entity's id, and spoken lines give beats over their real length.

## Check afterwards
`speech.status` shows lines and the cache; `packages/speech/test/speech.test.ts` passes; in the `?speech` viewer a signed-in player hears the figure from its side and a held talk button gives text. Headless: `startServer({ headless: true, speech: true, fakeMic: 'x.wav' })`.

## What usually goes wrong
- `signed-out`: a guest; nobody signed in and no override. Nothing was sent.
- `forbidden` (997): the key lacks `ai:tts` / `ai:stt`.
- `no-funds` (402): the player's wallet is too low.
- `audio-locked`: a line before the first user action.
- Network errors: the client must point at `https://squirrelwisdom.com/`, the proxy to the backend, never at the backend host itself.
- `empty`: the capture was silence (wrong microphone, muted) or nothing was recognised.

## Files

- `reference/speech.md` — Text to speech and speech to text on the Camerlengo API (@ferret/speech): lines spoken at an entity on the voice bus, a cache of synthesised lines, microphone capture and recognition, by the signed-in player's session; events, speech.* commands and the ?speech viewer.
- `reference/api-speech.md` — every export of `@ferret/speech` with signature and doc comment
- `reference/commands.md` — 8 commands of these packages with their arguments
