# Speech endpoints

These endpoints read text aloud in saved voices, as a job or as a live stream,
re-voice recordings and transcribe speech. Saved voices are on
[Voices endpoints](https://mirli.app/docs/api-voices/index.md), and the
[OpenAI-compatible endpoints](https://mirli.app/docs/api-openai/index.md) reach the same models in
OpenAI's request shapes.

Each endpoint lists the errors it returns itself. Any endpoint can also return
the [common errors](https://mirli.app/docs/api-errors/index.md#common-errors). A `400` from a model error
has `detail` and a [failure object](https://mirli.app/docs/api-errors/index.md#failure-object).

## Speak a script

`POST /api/speak`

Queues a job that reads a script and saves it as one library sound. A single
read is a one-line script, and a dialogue is several lines.

Every model and voice is checked before the job is queued. Each line is read
on its own, with one seed for the whole read, and the reads are joined with
`gap_s` of silence between turns. The result is never trimmed and
is loudness-normalised to the `normalize_lufs` setting. Without a `session_id`,
or with one that no longer exists, the read starts a new session titled from
`name` or the first line. The sound's `voice_id` is set only when every line
used the same saved voice.

For Fish Audio, every clip of a line's voice needs a transcript. The server
builds `request_options.reference_text` from the voice's clip transcripts, in
clip order (`created_at`, then `id`), joined with spaces. This overrides any
other transcript, over HTTP and over MCP alike.

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | A model whose `task` is `tts` |
| `lines` | object[] | Yes | 1 to 200 lines |
| `lines[].text` | string | Yes | 1 to 20,000 characters |
| `lines[].voice_id` | string or null | No | A saved voice's id, or `builtin:<NAME>` for one of the model's own voices. Omit for the model's default voice |
| `lines[].speaker` | string or null | No | Display name, up to 80 characters, such as `Narrator`. Not sent to the model |
| `lines[].model` | string or null | No | Another `tts` model for this line, for a dialogue between voices made on different families |
| `gap_s` | number | No | Silence between lines, 0 to 5 seconds. Default 0.4 |
| `seed` | integer | No | 0 to 2,147,483,647 |
| `steps` | integer | No | 1 to 500 |
| `cfg` | number | No | 0 to 30 |
| `request_options` | object | No | How the lines are read. Keys are names from the model's `params` on the [model object](https://mirli.app/docs/api-models/index.md#model-object), up to 60 characters; values are strings up to 2,000 characters. Unknown keys are accepted |
| `load_options` | object | No | Load-time settings as string values, such as PocketTTS' `language` |
| `name` | string | No | Name for the sound, up to 200 characters. Default: from the first line |
| `tags` | string[] | No | Normalised as on [Generate a sound](https://mirli.app/docs/api-generate/index.md#generate-a-sound) |
| `session_id`, `project_id` | string | No | Where the sound is filed |

This request reads a two-line dialogue:

```bash
curl -X POST "http://127.0.0.1:17494/api/speak" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "audiocpp-supertonic-3",
    "lines": [
      {
        "speaker": "Guard",
        "voice_id": "<VOICE_ID>",
        "text": "Who goes there?"
      },
      {
        "speaker": "Ada",
        "text": "A friend."
      }
    ],
    "gap_s": 0.4,
    "name": "Gate scene"
  }'
```

A successful response is `202 Accepted` with the job's id. When the
[job](https://mirli.app/docs/api-jobs/index.md#get-a-job) is done, `result.sounds` holds the new sound's
id:

```json
{
  "job_id": "5b2e9c40-7d1a-4f3e-8a6b-0c9d2e4f1a37"
}
```

| Status | Cause |
|---|---|
| `400` | A model is unknown or not `tts`, a `voice_id` names no voice, a clone has no clip on disk, or, for Fish Audio, a clip has no transcript (`detail` names the voice and clips) |

## Stream speech

`POST /api/speak/stream`

Speaks one line and sends the audio while it is being made. It is not a job: it
never queues, holds the GPU while the client listens, and closing the
connection cancels it.

Voices resolve as on `POST /api/speak`, including the Fish Audio transcript
rule. Nothing is saved unless `save` is `true`; a saved stream is kept as
heard, not normalised. Which models stream is `supports.streaming` on
the [model object](https://mirli.app/docs/api-models/index.md#model-object). Client examples are in
[Streaming speech](https://github.com/HamSanders/soundbox/blob/main/docs/STREAMING.md#client-examples-ship-with-the-docs).

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | A `tts` model that streams |
| `text` | string | Yes | 1 to 5,000 characters |
| `voice_id` | string or null | No | As on a `POST /api/speak` line |
| `format` | string | No | `sse` (the default) or `pcm` |
| `seed` | integer | No | 0 to 2,147,483,647 |
| `steps` | integer | No | 1 to 500 |
| `cfg` | number | No | 0 to 30 |
| `request_options` | object | No | As on `POST /api/speak` |
| `save` | boolean | No | `true` also saves the audio to the library. Default `false` |
| `name` | string | No | Name for a saved sound, up to 200 characters |
| `tags` | string[] | No | Tags for a saved sound |
| `project_id` | string | No | The project to file a saved sound in |

This request streams one line as server-sent events:

```bash
curl -N -X POST "http://127.0.0.1:17494/api/speak/stream" \
  -H 'Content-Type: application/json' \
  -d '{"model": "audiocpp-supertonic-3", "text": "Who goes there?"}'
```

A successful response is `200 OK`, sent once the first audio exists. Both
formats carry the headers `X-Soundbox-Sample-Rate`, `X-Soundbox-Channels` and
`X-Soundbox-Stream-Id`.

With `format: "sse"`, the body is `text/event-stream` with these events:

| Event | Data |
|---|---|
| `start` | `stream_id`, `model`, `sample_rate`, `channels` and `encoding: "pcm_s16le"` |
| `audio` | `seq` (counting from 0), `pcm` (base64 interleaved 16-bit little-endian PCM) and `frames` |
| `done` | `duration_s`; `ttft_ms`, the time to first audio including the model load; `model_ttft_ms`, audio.cpp's own figure; `sound_id`, null unless `save` |
| `error` | `message`, for a failure after audio has started |

This is a `done` event:

```text
event: done
data: {"duration_s":1.42,"ttft_ms":380,"model_ttft_ms":210,"sound_id":null}
```

With `format: "pcm"`, the body is raw interleaved 16-bit little-endian PCM with
`Content-Type: audio/L16; rate=<RATE>; channels=<CHANNELS>`. A failure after
audio has started ends the body without an error.

| Status | Cause |
|---|---|
| `400` | Before any audio: the model is unknown or not `tts`, `voice_id` names no voice, or a Fish Audio clip has no transcript |
| `409` | `reason: "unsupported"`: the model cannot stream on this audio.cpp build |
| `409` | `reason: "busy"`: another stream or a running generation holds the GPU. `retry_after_s`, also sent as `Retry-After`, estimates the wait in seconds, or is null |

## Convert a voice

`POST /api/convert`

Queues a job that re-voices a library sound through a voice-conversion model,
as a new sound.

The new sound's `parent_id` is the source. Its name is the source's name with
the voice's name in brackets, its tags are copied from the source, and it is
filed in `session_id`, or else in the source's session. It is
loudness-normalised to the `normalize_lufs` setting. The model is checked when
the job runs: an unknown model, or one whose `task` is not `vc`, fails the job.

A trained singing voice always runs on `audiocpp-rvc`, whatever `model`
says, since no other family can read its model. A trained voice that has not
finished training is refused.

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | A model whose `task` is `vc` |
| `source_sound_id` | string | Yes | The library sound to re-voice |
| `voice_id` | string | Yes | A saved `clone` voice, or a ready [trained singing voice](https://mirli.app/docs/api-voices/index.md#trained-singing-voices). A `builtin` voice is refused |
| `semitones` | integer | No | Semitones, -24 to 24, to move the melody before it is re-voiced. Default 0 |
| `auto_octave` | boolean | No | Trained voices only: ignore `semitones` and shift by the whole number of octaves worked out from the source's and the voice's median pitch. The job's `result.pitch_shift` says which. Default false |
| `seed` | integer | No | 0 to 2,147,483,647 |
| `session_id`, `project_id` | string | No | Where the sound is filed |

This request re-voices a sound in a saved voice:

```bash
curl -X POST "http://127.0.0.1:17494/api/convert" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "audiocpp-seed-vc",
    "source_sound_id": "<SOUND_ID>",
    "voice_id": "<VOICE_ID>"
  }'
```

A successful response is `202 Accepted` with the job's id:

```json
{
  "job_id": "9e1f4b27-3c8d-4a6e-b05f-2d7a9c1e6b83"
}
```

| Status | Cause |
|---|---|
| `400` | No sound has `source_sound_id`, no voice has `voice_id`, or the voice is `builtin` |

## Transcribe speech

`POST /api/transcribe`

Turns speech into text and waits for the answer. The body is a WAV file, up to
64 MB, sent as `audio/wav` or `application/octet-stream`, or it is empty and
`?sound_id` names a library sound.

The clip is converted to 16 kHz mono first. The work runs as a queued
`transcribe` job, and closing the connection cancels it.

| Name | Type | Required | Description |
|---|---|---|---|
| `?sound_id` | string | No | A library sound to transcribe instead of the body |
| `?model` | string | No | A model whose `task` is `asr`, such as `audiocpp-qwen3-asr-0-6b`. Default: an installed one, preferring a loaded one |
| `?language` | string | No | A language hint, such as `en` |

This request transcribes `clip.wav`:

```bash
curl -X POST "http://127.0.0.1:17494/api/transcribe?language=en" \
  -H 'Content-Type: audio/wav' \
  --data-binary @clip.wav
```

A successful response is `200 OK` with the text, the language and the model
that was used. `language` is null when the model does not report one:

```json
{
  "text": "Who goes there?",
  "language": "en",
  "model": "audiocpp-qwen3-asr-0-6b"
}
```

| Status | Cause |
|---|---|
| `400` | The body is not a readable WAV, or `model` is unknown or does not transcribe |
| `404` | `sound_id` names no sound |
| `409` | No `model` was given and none is installed (`code: "no_transcription_model"`), or the job was cancelled |
| `413` | The body is larger than 64 MB |
| `500` | The transcription failed. The body has `detail` and `job_id` |
