# Story tracks endpoints

A track is one lane of a [story](https://mirli.app/docs/api-stories/index.md)'s timeline: its name, mute,
solo and gain, and its device chain. The clips on a lane are
[segments](https://mirli.app/docs/api-segments/index.md).

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

## Track object

A lane gets a row when a setting first changes; until then it has the
defaults.

| Field | Type | Description |
|---|---|---|
| `story_id`, `track_index` | string, integer | The story and the lane |
| `name` | string or null | Up to 80 characters. Default `null` |
| `muted`, `soloed` | boolean | Default `false`. With any lane soloed, only soloed lanes are heard |
| `gain_db` | number | −40 to 24. Default 0 |
| `devices` | object[] | The insert chain, each `{ id, kind, bypassed, params }`. `id` is stable; a bypassed device stays in the chain, unheard; `params` holds every knob |

Each device kind has these knobs, given as range (default), with times in
seconds:

- `compressor`: `threshold` −60 to 0 dB (−24), `ratio` 1–20 (4), `attack`
  0.001–0.3 (0.006), `release` 0.01–1 (0.15)
- `eq`: `low`, `mid`, `high` −24 to 12 dB (0), `lowFrequency` 80–1,000 Hz (400),
  `highFrequency` 1,000–10,000 Hz (2,500)
- `reverb`: `decay` 0.1–10 (1.8), `preDelay` 0–0.3 (0.02), `wet` 0–1 (0.25)
- `delay`: `delayTime` 0.01–1 (0.25), `feedback` 0–0.95 (0.3), `wet` 0–1 (0.25)
- `distortion`: `distortion` 0–1 (0.3), `wet` 0–1 (0.5)
- `gate`: `threshold` −80 to 0 dB (−45), `smoothing` 0.01–0.5 (0.06)
- `limiter`: `threshold` −24 to 0 dB (−1)
- `autotune`: `tuneKey` 0–11, C to B (0), `scale` 0–2, Chromatic, Major, Minor
  (0), `retune` 0–0.4 (0.08), `amount` 0–1 (1)

## List tracks

`GET /api/stories/<STORY_ID>/tracks`

Returns the lanes with a row, in order.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |

This request lists lanes:

```bash
curl "http://127.0.0.1:17494/api/stories/<STORY_ID>/tracks"
```

A successful response is `200 OK` with an array of
[track objects](#track-object).

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

## Update a track

`PATCH /api/stories/<STORY_ID>/tracks/<TRACK_INDEX>`

Changes a lane, creating its row if needed. `devices` replaces the chain:
unknown knobs are dropped, values clamped (`tuneKey` and `scale` also rounded),
and missing knobs take defaults.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<TRACK_INDEX>` | string, integer | Yes | The story's id and the lane |
| `name`, `muted`, `soloed`, `gain_db` | mixed | No | As in the [track object](#track-object) |
| `devices` | object[] | No | Up to 16 `{ kind, id?, bypassed?, params? }`. `[]` empties it |

This request names lane 0 and adds two devices:

```bash
curl -X PATCH "http://127.0.0.1:17494/api/stories/<STORY_ID>/tracks/0" \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "Dialogue",
    "gain_db": -3,
    "devices": [
      {
        "kind": "compressor"
      },
      {
        "kind": "reverb",
        "params": {
          "wet": 0.2
        }
      }
    ]
  }'
```

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

| Status | Cause |
|---|---|
| `404` | No story has this id |
| `422` | `<TRACK_INDEX>` is not an integer |

## Delete a track

`DELETE /api/stories/<STORY_ID>/tracks/<TRACK_INDEX>`

Deletes a lane and its clips. Higher lanes move down one.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<TRACK_INDEX>` | string, integer | Yes | The story's id and the lane |

This request deletes lane 2:

```bash
curl -X DELETE -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/tracks/2"
```

A successful response is `200 OK` with the clips removed:

```json
{
  "removed_clips": 3
}
```

| Status | Cause |
|---|---|
| `404` | No story has this id |
| `422` | `<TRACK_INDEX>` is not an integer |
