# Story segments endpoints

A segment is a line, a pause or a placed sound in a [story](https://mirli.app/docs/api-stories/index.md).
Lanes and their device chains are on
[Story tracks endpoints](https://mirli.app/docs/api-tracks/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).

## Segment object

The segment endpoints return this object. Ranges and defaults apply on
create and update:

| Field | Type | Description |
|---|---|---|
| `id`, `story_id` | string | Its id and its story's |
| `kind`, `text` | string | `kind` is `line` (default) or `pause`, a silence. `text` is up to 20,000 characters |
| `speaker`, `voice_id`, `language` | string or null | Display name (up to 80 characters), the saved voice that reads it, and language (up to 20) |
| `request_options` | object | Its delivery. Empty follows the story's |
| `track_index` | integer | Lane, −16 to 16. Default 0 |
| `start_s` | number | Seconds on the timeline. Default: after its lane's last clip |
| `position` | integer | Reading order, from 0. Default: last. Moving the clip renumbers it |
| `sound_id` | string or null | Its audio: a placed sound, or `null` until spoken |
| `sound` | object or null | The [sound object](https://mirli.app/docs/api-sounds/index.md#sound-object) for `sound_id` |
| `duration_s` | number or null | 0 to 600. A pause's length, or how much sound plays: by default all after `trim_start_s`, never more |
| `trim_start_s` | number | Where in the sound the clip starts |
| `fade_in_s`, `fade_out_s`, `gain_db` | number | Fades 0 to 60, gain −40 to 24 |
| `muted` | boolean | Left out of playback and export |
| `midi` | object or null | Its [MIDI take](#midi-take-object) |
| `created_at`, `updated_at` | string | ISO 8601 times |

## MIDI take object

A clip rendered from MIDI keeps its notes, to edit and render again in place.
The eight sampled instruments are on
[Instruments endpoints](https://mirli.app/docs/api-instruments/index.md).

| Field | Type | Description |
|---|---|---|
| `notes` | object[] | 1 to 10,000, each `note` 0–127, `velocity` 0–1, `start_s` 0–3,600 from the take's start, `duration_s` above 0, up to 600 |
| `bpm` | number | Optional, 20–240, for the bar grid |
| `instrument` | string | The dry preset it was rendered with |
| `cover` | object or null | The ACE-Step cover's `{ prompt, strength }` (`strength` 0–1), or `null` |
| `role`, `take_id` | string | `dry` (default) or `cover`; the id the dry clip and its cover share |

## List segments

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

Returns the segments in reading order.

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

This request lists segments:

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

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

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

## Create a segment

`POST /api/stories/<STORY_ID>/segments`

Adds a segment. The body takes the [segment object](#segment-object) fields
except `id`, `story_id`, `sound` and the times, all optional.

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

This request adds a line for a guard:

```bash
curl -X POST "http://127.0.0.1:17494/api/stories/<STORY_ID>/segments" \
  -H 'Content-Type: application/json' \
  -d '{
    "speaker": "Guard",
    "voice_id": "<VOICE_ID>",
    "text": "Halt! Who goes there?",
    "track_index": 0
  }'
```

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

| Status | Cause |
|---|---|
| `400` | No sound has the `sound_id` |
| `404` | No story has this id |

## Reorder segments

`PUT /api/stories/<STORY_ID>/segments/reorder`

Sets the whole reading order.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>` | string | Yes | The story's id |
| `segment_ids` | string[] | Yes | Every segment id, 1 to 500, in order |

This request orders two segments:

```bash
curl -X PUT \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/segments/reorder" \
  -H 'Content-Type: application/json' \
  -d '{"segment_ids": ["<SEGMENT_ID_1>", "<SEGMENT_ID_2>"]}'
```

A successful response is `200 OK` with the
[segment objects](#segment-object) in the new order.

| Status | Cause |
|---|---|
| `404` | No story has this id |
| `409` | `segment_ids` does not match the story's segments, for example after another client added one |

## Update a segment

`PATCH /api/stories/<STORY_ID>/segments/<SEGMENT_ID>`

Changes a segment. The body takes the [create](#create-a-segment) fields
except `kind` and `position`. A new `sound_id` keeps the clip's place and trim,
`request_options: {}` follows the story again, and `midi: null` forgets the
notes.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<SEGMENT_ID>` | string | Yes | The story's and the segment's ids |

This request moves a clip to lane 1 at 4.5 s:

```bash
curl -X PATCH \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/segments/<SEGMENT_ID>" \
  -H 'Content-Type: application/json' \
  -d '{"track_index": 1, "start_s": 4.5}'
```

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

| Status | Cause |
|---|---|
| `400` | No sound has the `sound_id` |
| `404` | No segment has this id |

## Delete a segment

`DELETE /api/stories/<STORY_ID>/segments/<SEGMENT_ID>`

Deletes a segment and closes the gap in the reading order. Its sound stays in
the library.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<SEGMENT_ID>` | string | Yes | The story's and the segment's ids |

This request deletes one segment:

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

A successful response is `204 No Content`.

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

## Speak a line

`POST /api/stories/<STORY_ID>/segments/<SEGMENT_ID>/generate`

Starts a `segment` job reading the line. A line still at 0 s then moves after
its lane's last clip. The job fails if the line has no text or the model is not
text-to-speech. For Fish Audio, every clip of the voice needs a
transcript ([Voices endpoints](https://mirli.app/docs/api-voices/index.md)).

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<SEGMENT_ID>` | string | Yes | The story's and the segment's ids |
| `model` | string | Yes | A text-to-speech model id |
| `seed`, `steps`, `cfg` | number or null | No | 0 to 2,147,483,647; 1 to 500; 0 to 30 |
| `load_options` | object | No | Names to string values |
| `request_options` | object | No | Delivery for this take only |

This request reads one line:

```bash
curl -X POST -H 'Content-Type: application/json' \
  -d '{"model": "audiocpp-supertonic-3", "seed": 7}' \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/segments/<SEGMENT_ID>/generate"
```

A successful response is `202 Accepted` with the job's id. Its `result` holds
`sounds` and `segment_id`:

```json
{
  "job_id": "e7a03b52-1f9c-4d28-b6e4-90c2a5d17f3e"
}
```

| Status | Cause |
|---|---|
| `400` | No such segment, unknown `model`, missing voice or clip file, or a Fish Audio clip without transcript |

## Split a clip

`POST /api/stories/<STORY_ID>/segments/<SEGMENT_ID>/split`

Cuts a clip in two, each half a new derived sound. The first keeps the text
and fade in, the second the fade out; both keep gain and mute, and lose the
MIDI take.

| Name | Type | Required | Description |
|---|---|---|---|
| `<STORY_ID>`, `<SEGMENT_ID>` | string | Yes | The story's and the segment's ids |
| `at_s` | number | Yes | Seconds from the clip's left edge on the timeline |

This request cuts a clip 1.5 s in:

```bash
curl -X POST \
  "http://127.0.0.1:17494/api/stories/<STORY_ID>/segments/<SEGMENT_ID>/split" \
  -H 'Content-Type: application/json' \
  -d '{"at_s": 1.5}'
```

A successful response is `200 OK` with the two halves as
[segment objects](#segment-object).

| Status | Cause |
|---|---|
| `400` | No audio, missing file, or `at_s` outside the clip |
| `404` | No segment has this id |
