# Generation endpoints

These endpoints make audio from a prompt, rework a library sound, and separate
stems. `POST /api/render` answers with the WAV; the others queue a
[job](https://mirli.app/docs/api-jobs/index.md). Speech is on [Speech endpoints](https://mirli.app/docs/api-speech/index.md).

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).

## Render and wait

`POST /api/render`

Runs one generation or speech job and answers with its WAV. The job waits its
turn in the app's queue, and shows there while it runs.
If the caller disconnects first, the job is cancelled. A proxy can cut a long
wait; a GPU host's HTTP proxy often allows about 100 seconds.

The body is a [generate body](#generate-a-sound) or a speak body from
[Speech endpoints](https://mirli.app/docs/api-speech/index.md); one that fails that schema is a `422`
[validation error](https://mirli.app/docs/api-errors/index.md#validation-errors). The answer is one file,
so `variations` must be 1 and `separate` is refused. These fields belong to
this endpoint alone:

| Name | Type | Required | Description |
|---|---|---|---|
| `task` | string | No | `generate` or `speak`. Left out, a body with `text` or `lines` is speech and anything else is a generation |
| `keep` | boolean | No | `false` deletes the sound once sent, for a server that only renders for others. Default `true` |
| `text` | string | No | Speech shorthand: one line, up to 20,000 characters |
| `voice` | string | No | With `text`: a saved voice's id, or its name in any case |

This request renders a two-second door slam to `door.wav`:

```bash
curl -X POST "http://127.0.0.1:17494/api/render" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "audiocpp-stable-audio-3-sfx",
    "prompt": "door slam",
    "duration_s": 2
  }' \
  -o door.wav
```

A successful response is `200 OK` with `Content-Type: audio/wav`, the WAV as
the body, and the headers `X-Soundbox-Job-Id` and `X-Soundbox-Sound-Id`. The
sound id is empty when `keep` is `false`.

| Status | Cause |
|---|---|
| `400` | Refused before anything runs: `variations` above 1, `separate` set, both `text` and `lines` or neither on speech, or a `voice` that matches no saved voice. Also any [model error](https://mirli.app/docs/api-errors/index.md#model-errors) from the generate or speak checks |
| `409` | The job was cancelled on the server before it finished |
| `429` | One client sent more than 60 calls in a minute, or the job queue is full |
| `500` | The render failed. The body is `{ detail, failure, job_id }`, with a [failure object](https://mirli.app/docs/api-errors/index.md#failure-object). A job that finished without a sound gives `{ detail, job_id }` |

## Generate a sound

`POST /api/generate`

Queues a job that renders a prompt, or reworks a library sound. An unloaded
model is loaded first, in the `loading` phase.

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | A model id from [Models endpoints](https://mirli.app/docs/api-models/index.md) |
| `prompt` | string | Yes | 1 to 2,000 characters |
| `negative_prompt` | string | No | Ignored by models whose `supports.negative_prompt` is false |
| `duration_s` | number | No | Above 0, up to 300 and the model's `max_duration_s`. Default 5. For `extend`, the new total length |
| `seed` | integer | No | 0 to 2³¹−1. Left out or `null`, random. Only the first variation uses it |
| `steps` | integer | No | 1 to 500. Default: the model's. Ignored without `supports.steps` |
| `cfg` | number | No | 0 to 30, and at most the model's `max_cfg`. Default: the model's |
| `variations` | integer | No | 1 to 4 takes. Default 1 |
| `tags` | string[] | No | Trimmed, lower-cased and de-duplicated; first 20 kept |
| `lyrics` | string | No | Up to 4,000 characters, for models with `supports.lyrics` (ACE-Step) |
| `track_name` | string | No | Up to 200 characters |
| `source_sound_id` | string | No | A library sound to edit. Needs `edit` |
| `edit` | string | No | Needs `source_sound_id`. `extend` continues the source to `duration_s` total seconds; `cover` re-renders it in the style of `prompt` at its length; `repaint` regenerates the `edit_options` window |
| `edit_options` | object | No | Settings for the edit |
| `separate` | object | No | `{ keep, rest, model }`: splits the result into stems before the job reports done |
| `session_id` | string | No | The session to add to. Left out, or naming no session, the server makes one titled from the prompt |
| `project_id` | string | No | The project a new session is filed in |

An edit needs a model that declares `supports.<edit>`, and makes a new sound
whose `parent_id` is the source. The `edit_options` fields are:

| Name | Type | Description |
|---|---|---|
| `cover_strength` | number | `cover` only. 0 to 1: 1 follows the source faithfully, 0 loosely |
| `extend_window_s` | number | `extend` only. 1 to 300 seconds of tail fed back as context when the target runs past the model's maximum. Default half that maximum. Each pass also renders up to 6 s past what it keeps and discards it, so the model's ending never lands mid-take |
| `repaint_start_s`, `repaint_end_s` | number | `repaint` only, and required. Seconds in the source; start before end |
| `repaint_mode` | string | `balanced`, `conservative` or `aggressive` |
| `repaint_strength` | number | 0 to 1. How far the new material may drift |

`separate` takes the `keep` and `rest` of
[Split a sound into stems](#split-a-sound-into-stems), and a `model` whose
`task` is `sep`; left out, the server picks an installed one. The job's
`result.sounds` are then the stems, and the mixes stay in the library as their
parents, listed in `result.sources`.

This request queues a three-second door slam:

```bash
curl -X POST "http://127.0.0.1:17494/api/generate" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "audiocpp-stable-audio-3-sfx",
    "prompt": "heavy wooden door slam, dry room",
    "duration_s": 3,
    "tags": ["door", "impact"]
  }'
```

A successful response is `202 Accepted` with the
[job](https://mirli.app/docs/api-jobs/index.md#get-a-job) id:

```json
{
  "job_id": "5f0c2a9e-3b1d-4c6a-9e2f-8a7b6c5d4e3f"
}
```

These end the job with status `error` before the model loads: a model that
cannot do the edit, a missing source, an `extend` no longer than the source, or
a repaint window past its end. So does a `separate` without `model` when no
separator is installed, after the render.

| Status | Cause |
|---|---|
| `400` | A [model error](https://mirli.app/docs/api-errors/index.md#model-errors): an unknown `model`, `duration_s` past the model's maximum (except for `extend`), `cfg` above its `max_cfg`, or a `separate.model` that is not a separator |

## Split a sound into stems

`POST /api/separate`

Queues a job that splits a library sound into stems, each saved as its own
sound and listed in the job's `result.sounds`.

| Name | Type | Required | Description |
|---|---|---|---|
| `model` | string | Yes | A model whose `task` is `sep` |
| `source_sound_id` | string | Yes | The sound to split |
| `stems.keep` | string[] | No | Up to 16 stem names, such as `vocals`, `drums`, `bass` or `other`, matched ignoring case and punctuation. Empty, or no `stems`, keeps all |
| `stems.rest` | string | No | `discard` (the default) drops the others; `merge` sums them into one sound named `rest` |

This request keeps the vocals and merges the rest into one sound:

```bash
curl -X POST "http://127.0.0.1:17494/api/separate" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "audiocpp-htdemucs-sep",
    "source_sound_id": "<SOUND_ID>",
    "stems": {
      "keep": ["vocals"],
      "rest": "merge"
    }
  }'
```

A successful response is `202 Accepted` with the
[job](https://mirli.app/docs/api-jobs/index.md#get-a-job) id:

```json
{
  "job_id": "5f0c2a9e-3b1d-4c6a-9e2f-8a7b6c5d4e3f"
}
```

A `keep` matching none of the model's stems ends the job with status `error`.

| Status | Cause |
|---|---|
| `400` | A [model error](https://mirli.app/docs/api-errors/index.md#model-errors): an unknown `model`, a model that does not separate, or no sound with `source_sound_id` |

## Repaint a window

`POST /api/repaint`

Queues a job that regenerates `start_s` to `end_s` of a library sound and keeps
the rest sample for sample: the editor's **Repaint** tool. The length, and
unless overridden the prompt, lyrics, tags and session, are the source's. It is `POST /api/generate` with `edit: "repaint"` underneath: the
job's `kind` is `generate`, and its `request` is that generate body.

| Name | Type | Required | Description |
|---|---|---|---|
| `source_sound_id` | string | Yes | The sound to repaint |
| `start_s`, `end_s` | number | Yes | The window in seconds. `end_s` must be greater than `start_s` |
| `prompt` | string | No | Up to 2,000 characters: what should be there instead. Default: the source's prompt |
| `lyrics` | string | No | Up to 4,000 characters. Replaces the source's lyrics |
| `model` | string | No | Default: the source's model if it can repaint, otherwise a ready one that can |
| `strength` | number | No | 0 to 1. How far the new material may drift |
| `mode` | string | No | `balanced`, `conservative` or `aggressive` |
| `seed` | integer | No | 0 to 2³¹−1 |
| `variations` | integer | No | 1 to 4. Default 1 |
| `session_id`, `project_id` | string | No | As on [Generate a sound](#generate-a-sound) |

This request repaints 12 s to 18.5 s of a track:

```bash
curl -X POST "http://127.0.0.1:17494/api/repaint" \
  -H 'Content-Type: application/json' \
  -d '{
    "source_sound_id": "<SOUND_ID>",
    "start_s": 12.0,
    "end_s": 18.5,
    "prompt": "brass stabs instead of the pad",
    "mode": "balanced"
  }'
```

A successful response is `202 Accepted` with the
[job](https://mirli.app/docs/api-jobs/index.md#get-a-job) id:

```json
{
  "job_id": "5f0c2a9e-3b1d-4c6a-9e2f-8a7b6c5d4e3f"
}
```

| Status | Cause |
|---|---|
| `400` | A [model error](https://mirli.app/docs/api-errors/index.md#model-errors): no sound with `source_sound_id`, a window ending past the source, a `model` that is unknown or cannot repaint (only ACE-Step can), none installed that can, or a source longer than the model renders in one pass |
