# Sounds endpoints

A sound is one audio file in the library and the row that describes it. These
endpoints read and change sounds, add audio from disk, make edited copies, and
serve the files. Cover art is on [Artwork endpoints](https://mirli.app/docs/api-artwork/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).

## Sound object

The sound endpoints return this object:

| Field | Type | Description |
|---|---|---|
| `id` | string | The sound's id |
| `created_at` | string | ISO 8601 time it was made |
| `model` | string | The model id that made it. For an upload, the upload's `source` |
| `prompt`, `negative_prompt`, `lyrics`, `track_name` | string or null | What it was made from |
| `edit`, `edit_options` | string or null, object or null | The edit that made it from `parent_id`: `extend`, `cover` or `repaint` |
| `duration_s` | number | Length in seconds |
| `seed`, `steps`, `cfg` | number or null | Settings it was made with |
| `sample_rate`, `channels` | integer | Audio format |
| `file` | string | URL of the WAV, `/files/<ID>.wav` |
| `peaks` | number[] | 256 normalised peaks for drawing the waveform |
| `favorite` | boolean | Marked as a favourite |
| `tags` | string[] | Lower-case tags |
| `name` | string | Display name |
| `size_bytes` | integer | File size |
| `session_id`, `project_id` | string or null | Where it is filed |
| `effective_project_id` | string or null | `project_id`, or the session's project when the sound has none |
| `source` | string | Where the request came from: `app`, `api` or `mcp` |
| `client` | string or null | The MCP client name and version, when an agent made it |
| `parent_id` | string or null | The sound it was derived from (a stem, an edit, a split half) |
| `artwork` | string or null | URL of its cover art, `/artwork/<FILE>.png` |
| `voice_id` | string or null | The saved voice that read it, when exactly one did |

## List sounds

`GET /api/sounds`

Returns a page of library sounds, filtered and sorted.

| Name | Type | Required | Description |
|---|---|---|---|
| `?q` | string | No | Text search |
| `?tag` | string | No | One tag |
| `?favorite` | boolean | No | `true` for favourites only |
| `?model` | string | No | One model id |
| `?models` | string | No | Several model ids |
| `?session_id` | string | No | One session's sounds |
| `?project_id` | string | No | One project's sounds |
| `?parent_id` | string | No | Sounds derived from this one |
| `?voice_id` | string | No | Sounds one saved voice read alone: single-voice reads and conversions, not dialogues |
| `?source` | string | No | `app`, `api` or `mcp` |
| `?sort` | string | No | `newest`, `oldest`, `longest`, `shortest` or `name` |
| `?limit` | integer | No | 1 to 500. Default 100 |
| `?offset` | integer | No | Default 0 |

This request lists up to 20 sounds tagged `impact` that match "door":

```bash
curl "http://127.0.0.1:17494/api/sounds?q=door&tag=impact&limit=20"
```

A successful response is `200 OK` with the page and the total match count. The
example is abridged:

```jsonc
{
  "items": [
    {
      "id": "3f6c1b9e-2a4d-4f0e-9c1a-7b5d2e8f0a11",
      "name": "Door slam 01"
      // ...the rest of the sound object
    }
  ],
  "total": 1
}
```

| Status | Cause |
|---|---|
| `422` | A query value is out of range or not one of the allowed values |

## Get a sound

`GET /api/sounds/<SOUND_ID>`

Returns one sound.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound's id |

This request reads one sound:

```bash
curl "http://127.0.0.1:17494/api/sounds/<SOUND_ID>"
```

A successful response is `200 OK` with the [sound object](#sound-object).

| Status | Cause |
|---|---|
| `404` | No sound has this id |

## Update a sound

`PATCH /api/sounds/<SOUND_ID>`

Changes a sound's name, favourite flag, tags or project.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound's id |
| `name` | string | No | New display name |
| `favorite` | boolean | No | Favourite flag |
| `tags` | string[] | No | Replaces the tags |
| `project_id` | string or null | No | The project to file it in. `null` takes it out; leaving the field out leaves the project unchanged |

This request renames a sound and marks it as a favourite:

```bash
curl -X PATCH "http://127.0.0.1:17494/api/sounds/<SOUND_ID>" \
  -H 'Content-Type: application/json' \
  -d '{"name": "Door slam 01", "favorite": true}'
```

A successful response is `200 OK` with the updated
[sound object](#sound-object).

| Status | Cause |
|---|---|
| `404` | No sound has this id |

## Delete a sound

`DELETE /api/sounds/<SOUND_ID>`

Deletes a sound's row and its file. Sounds derived from it are kept, and their
`parent_id` becomes `null`.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound's id |

This request deletes one sound:

```bash
curl -X DELETE -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/sounds/<SOUND_ID>"
```

A successful response is `204 No Content`.

| Status | Cause |
|---|---|
| `404` | No sound has this id |
| `409` | The sound is on a story's timeline. `detail` names the story, for example: `This sound is on the timeline of the story "Gate scene". Remove it from that timeline first, then delete it.` |

## Upload a sound

`POST /api/sounds/upload`

Adds a WAV file from disk or a recording to the library. The body is the WAV
file itself, up to 256 MB, sent as `audio/wav` or `application/octet-stream`.

| Name | Type | Required | Description |
|---|---|---|---|
| `?name` | string | No | Display name. Default `Recording` |
| `?source` | string | No | `recording` (the default), `import` or `midi`. Stored as the sound's `model` |
| `?project_id` | string | No | The project to file it in |

This request uploads `footsteps.wav` as an import:

```bash
curl -X POST \
  "http://127.0.0.1:17494/api/sounds/upload?name=Footsteps&source=import" \
  -H 'Content-Type: audio/wav' \
  --data-binary @footsteps.wav
```

A successful response is `201 Created` with the new
[sound object](#sound-object).

| Status | Cause |
|---|---|
| `400` | The body is empty or is not a readable WAV |
| `413` | The body is larger than 256 MB |
| `422` | `source` is not one of the allowed values |

## Edit a sound

`POST /api/sounds/<SOUND_ID>/edit`

Writes a new sound derived from this one. The original is not changed, and the
new sound's `parent_id` points at it.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound to edit |
| `op` | string | Yes | `trim`, `normalize`, `reverse`, `gain`, `highpass`, `lowpass`, `pitch` or `stretch` |
| `start_s`, `end_s` | number | For `trim` | The range to keep, in seconds |
| `target_lufs` | number | For `normalize` | −40 to 0 |
| `gain_db` | number | For `gain` | −40 to 24 |
| `cutoff_hz` | number | For `highpass`, `lowpass` | Above 0, up to 20,000 |
| `semitones` | number | For `pitch` | −24 to 24 |
| `rate` | number | For `stretch` | Above 0.25, up to 4 |

This request keeps 0.2 s to 2.5 s of a sound:

```bash
curl -X POST "http://127.0.0.1:17494/api/sounds/<SOUND_ID>/edit" \
  -H 'Content-Type: application/json' \
  -d '{"op": "trim", "start_s": 0.2, "end_s": 2.5}'
```

A successful response is `201 Created` with the new
[sound object](#sound-object).

| Status | Cause |
|---|---|
| `404` | No sound has this id |

## Reveal a sound's file

`POST /api/sounds/<SOUND_ID>/reveal`

Opens the operating system's file manager on the server's machine, with the
sound's file selected.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound's id |

This request shows one sound's file:

```bash
curl -X POST -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/sounds/<SOUND_ID>/reveal"
```

A successful response is `204 No Content`.

| Status | Cause |
|---|---|
| `403` | The server's capabilities turn `reveal` off |
| `404` | No sound has this id |

## List tags

`GET /api/tags`

Returns every tag in the library with the number of sounds that carry it. This
endpoint takes no parameters.

This request lists the tags:

```bash
curl http://127.0.0.1:17494/api/tags
```

A successful response is `200 OK` with the tags:

```json
[
  {
    "tag": "impact",
    "count": 12
  }
]
```

This endpoint returns only the common errors.

## Get a sound's audio

`GET /files/<SOUND_ID>.wav`

Returns the audio file. The response supports byte ranges, so a player can
seek.

| Name | Type | Required | Description |
|---|---|---|---|
| `<SOUND_ID>` | string | Yes | The sound's id |
| `?format` | string | No | `mp3` transcodes the file when `ffmpeg` is installed, and caches the result |

This request saves a sound as MP3:

```bash
curl -o door.mp3 \
  "http://127.0.0.1:17494/files/<SOUND_ID>.wav?format=mp3"
```

A successful response is `200 OK` (or `206 Partial Content` for a range) with
`audio/wav` or `audio/mpeg`. When `format=mp3` is asked for and `ffmpeg` is
missing, the response is the WAV with the header
`X-Soundbox-Format-Fallback: wav`.

| Status | Cause |
|---|---|
| `400` | The file name contains a path |
| `404` | No such file |
| `416` | The requested range is outside the file |
