# @ferret/speech: API

Text to speech and speech to text on the Camerlengo API: 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 and speech.* commands.

27 exports. Generated from the source (`packages/speech/src`), so it matches the code.

## Classes

### CamerlengoSpeech

*class* · `packages/speech/src/backend.ts:46`

```ts
class CamerlengoSpeech implements SpeechBackend
```

- `constructor(private opts: CamerlengoSpeechOptions)`
- `async synthesize(text: string, o: { voice: string; language: string }): Promise<SpeechClip>`
- `async recognize(clip: SpeechClip, o: RecognizeOptions): Promise<string>`

### MicCapture

*class* · `packages/speech/src/capture.ts:60`

```ts
class MicCapture
```

One capture: from `start` until `stop` (the samples) or `cancel` (nothing). It ends by itself at `maxSeconds`, keeping what it has.

- `level: number` — the loudness of the last chunk, 0..1 (root mean square): a meter for the game's UI
- `get capturing(): boolean` — true while samples are coming in
- `onLimit?: () => void` — called when the capture ended by itself at the limit
- `constructor(private open: OpenMicrophone = browserMicrophone, readonly maxSeconds = 60)`
- `async start(): Promise<void>`
- `stop(): PcmAudio` — Ends the capture: the samples collected (mono).
- `cancel(): void` — Ends the capture and drops what it had.

### SpeechError

*class* · `packages/speech/src/clip.ts:26`

```ts
class SpeechError extends Error
```

- `constructor(readonly code: SpeechErrorCode, message: string)`

### SpeechService

*class* · `packages/speech/src/service.ts:77`

```ts
class SpeechService implements Service
```

- `readonly name: "speech"`
- `readonly sent: { synthesize: number; recognize: number; }` — the clips that went to the backend and came back from it (a meter for tests and the status command)
- `constructor(readonly opts: SpeechOptions = {})`
- `init(world: World)`
- `fixedUpdate()` — Called before the components' fixed update, at the fixed rate.
- `dispose()`
- `backend(): SpeechBackend` — the backend the calls go to
- `synthesize(text: string, o: SynthesizeOptions & { cache?: boolean } = {}): Promise<SpeechClip>` — The line as a clip; nothing is played.
- `get cached(): number`
- `speak(entity: Entity | null, text: string, o: SpeakOptions = {}): SpeechLine` — Says a line: on the voice bus at the entity (null: not positioned). Synthesis starts at once; the line waits for the speaker's earlier lines.
- `hush(entity: Entity | null)` — Cuts every line of a speaker (null: the unpositioned ones).
- `lines(): Record<string, { text: string; playing: boolean }[]>` — lines sounding and waiting, by speaker ('' for unpositioned)
- `async listen(o: RecognizeOptions = {}): Promise<Listening>` — Opens the microphone and captures until `stop()` (recognise) or `cancel()` (nothing sent). One capture at a time.
- `get listening(): boolean` — true while the microphone captures
- `async recognize(input: SpeechClip | PcmAudio | Uint8Array, o: RecognizeOptions = {}): Promise<string>` — The text of a clip (a capture, a file, a synthesised line), or of samples.

## Functions

### browserMicrophone

*function* · `packages/speech/src/capture.ts:21`

```ts
function browserMicrophone: OpenMicrophone
```

The browser's microphone (echo cancellation and noise suppression on). Throws SpeechError `no-microphone` or `mic-denied`.

### clipFromBytes

*function* · `packages/speech/src/clip.ts:31`

```ts
function clipFromBytes(bytes: Uint8Array): SpeechClip
```

A clip from the bytes of a WAV or MP3 file (its length read from the header, nothing decoded).

### clipFromPcm

*function* · `packages/speech/src/clip.ts:60`

```ts
function clipFromPcm(pcm: PcmAudio, format: SoundFormat = 'mp3', rate = 16000): SpeechClip
```

Captured samples as a speech clip: mono at 16 kHz, MP3 (default) or WAV.

### fromBase64

*function* · `packages/speech/src/clip.ts:72`

```ts
function fromBase64(b64: string): Uint8Array
```

### installSpeech

*function* · `packages/speech/src/service.ts:280`

```ts
function installSpeech(world: World, opts: SpeechOptions = {}): SpeechService
```

Registers the service; options say who pays and with what (default: the signed-in player's session).

### languageCode

*function* · `packages/speech/src/backend.ts:40`

```ts
function languageCode(l: string): string
```

The language as the STT command takes it (a code): `en-US` -> `en`; a name is looked up.

### languageName

*function* · `packages/speech/src/backend.ts:38`

```ts
function languageName: (l: string) => string
```

The language as the TTS command takes it (a name); a name given stays as it is.

### peak

*function* · `packages/speech/src/clip.ts:53`

```ts
function peak(samples: Float32Array): number
```

The loudest sample's magnitude.

### toBase64

*function* · `packages/speech/src/clip.ts:67`

```ts
function toBase64(bytes: Uint8Array): string
```

### toSpeechPcm

*function* · `packages/speech/src/clip.ts:37`

```ts
function toSpeechPcm(pcm: PcmAudio, rate = 16000): Float32Array
```

Mono, at `rate` (linear interpolation; a box average first when going down, so the highs do not fold back).

## Interfaces

### CamerlengoSpeechOptions

*interface* · `packages/speech/src/backend.ts:25`

```ts
interface CamerlengoSpeechOptions
```

- `client: CamerlengoClient`
- `session: () => string | null` — the session that pays; read on every call (sign-in, renewal). Null: refused with `signed-out`, nothing sent
- `key?: string` — the key with the ai:tts / ai:stt scopes (default: the client's appKey)

### LineResult

*interface* · `packages/speech/src/service.ts:51`

```ts
interface LineResult
```

- `interrupted: boolean`
- `error?: SpeechError`

### Listening

*interface* · `packages/speech/src/service.ts:61`

```ts
interface Listening
```

- `readonly level: number` — the loudness now, 0..1
- `readonly capturing: boolean`
- `stop(): Promise<string>` — ends the capture and recognises it
- `cancel(): void` — ends the capture and sends nothing

### PcmSource

*interface* · `packages/speech/src/capture.ts:8`

```ts
interface PcmSource
```

A running source of mono samples. `onData` is set by the capture; `close` releases the device.

- `readonly rate: number`
- `onData: ((chunk: Float32Array) => void) | null`
- `close(): void`

### RecognizeOptions

*interface* · `packages/speech/src/backend.ts:13`

```ts
interface RecognizeOptions
```

- `language?: string` — the language spoken, a code (en, ru); without it the recognizer guesses
- `timeout?: number` — seconds the backend waits for the recognizer (Camerlengo: default 30, at most 600)

### SpeakOptions

*interface* · `packages/speech/src/service.ts:43`

```ts
interface SpeakOptions extends SynthesizeOptions
```

- `interrupt?: boolean` — cut what this speaker is saying and has queued, then say this
- `volume?: number`
- `spatial?: Spatial`
- `cache?: boolean` — false: do not take the clip from the cache or keep it there

### SpeechBackend

*interface* · `packages/speech/src/backend.ts:20`

```ts
interface SpeechBackend
```

- `synthesize(text: string, o: Required<Pick<SynthesizeOptions, 'voice' | 'language'>>): Promise<SpeechClip>`
- `recognize(clip: SpeechClip, o: RecognizeOptions): Promise<string>`

### SpeechClip

*interface* · `packages/speech/src/clip.ts:6`

```ts
interface SpeechClip
```

- `bytes: Uint8Array`
- `format: SoundFormat`
- `duration: number` — seconds

### SpeechLine

*interface* · `packages/speech/src/service.ts:52`

```ts
interface SpeechLine
```

- `readonly text: string`
- `readonly speaker: string | null`
- `readonly done: Promise<LineResult>` — resolves when the line is over: played out, interrupted, or failed (never rejects)
- `readonly playing: boolean` — true while it sounds
- `stop(): void`

### SpeechOptions

*interface* · `packages/speech/src/service.ts:17`

```ts
interface SpeechOptions
```

- `backend?: SpeechBackend` — another backend altogether (the developer's own server); the options below are then unused
- `client?: CamerlengoClient` — the Camerlengo client (default: the AccountService's)
- `session?: () => string | null` — who pays (default: the AccountService's session; a guest is refused with `signed-out`)
- `key?: string` — the key with the ai:tts / ai:stt scopes (default: the client's appKey)
- `voice?: string` — default voice and language of synthesize/speak/listen (default `alloy`, `en`)
- `language?: string`
- `microphone?: OpenMicrophone` — the microphone (default: the browser's) and the longest capture, s (default 60)
- `maxSeconds?: number`
- `silence?: number` — a capture whose loudest sample is below this is silence: not sent, `empty` (default 0.01)
- `upload?: 'mp3' | 'wav'` — what a capture is uploaded as (default mp3: smaller, and recognition is paid by size)
- `cacheSize?: number` — synthesised clips kept (default 64)
- `spatial?: Spatial` — how a positioned line fades with distance (default min 2 m, max 40 m) and its volume
- `volume?: number`

### SynthesizeOptions

*interface* · `packages/speech/src/backend.ts:7`

```ts
interface SynthesizeOptions
```

- `voice?: string` — an OpenAI voice name (alloy, echo, fable, onyx, nova, shimmer: a male or female voice of the language) or an exact Edge voice name (ru-RU-SvetlanaNeural)
- `language?: string` — the text's language: a code (en, ru) or a name (English). Always sent: without it the server spends a language-detection call on every line

## Types

### OpenMicrophone

*type* · `packages/speech/src/capture.ts:13`

```ts
type OpenMicrophone = () => Promise<PcmSource>
```

### SpeechErrorCode

*type* · `packages/speech/src/clip.ts:13`

```ts
type SpeechErrorCode = | 'signed-out' // no session and no other credentials: nothing was sent | 'no-funds' // the wallet that pays is too low (402) | 'forbidden' // the key may not do this (997) | 'backend' // any other refusal of the backend | 'network' // the backend could not be reached | 'no-microphone' // no microphone, or no way to capture in this host | 'mic-denied' // the player refused the microphone | 'busy' // a capture is already running | 'empty' // silence, or nothing was recognised | 'no-audio' // speak() without an AudioService | 'audio-locked'
```

