# Instruments endpoints

An instrument is one of the eight VSCO 2 Community Edition sampled
instruments that the studio's MIDI dry render plays. These endpoints list
them, download and delete their samples, and serve the parsed SFZ regions and
the sample files the browser plays.
[Sampled instruments](https://mirli.app/docs/studio-instruments/index.md#download-a-sampled-instrument)
describes how the studio uses them.

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

## Instrument object

The list endpoint returns this object:

| Field | Type | Description |
|---|---|---|
| `id` | string | The instrument's id |
| `label` | string | Display name |
| `family` | string | `strings`, `woodwinds` or `brass` |
| `library` | string | Always `VSCO 2 CE` |
| `license` | string | Always `CC0-1.0` |
| `size_bytes` | integer | Total size of its files |
| `installed` | boolean | Its samples are downloaded |

The eight instruments are these:

| `id` | `label` | `family` |
|---|---|---|
| `violin` | Solo violin | `strings` |
| `violins` | Violin section | `strings` |
| `cellos` | Cello section | `strings` |
| `harp` | Harp | `strings` |
| `flute` | Flute | `woodwinds` |
| `clarinet` | Clarinet | `woodwinds` |
| `trumpet` | Trumpet | `brass` |
| `horn` | French horn | `brass` |

## List instruments

`GET /api/instruments`

Returns all eight instruments, installed or not. This endpoint takes no
parameters.

This request lists the instruments:

```bash
curl "http://127.0.0.1:17494/api/instruments"
```

A successful response is `200 OK` with an array of
[instrument objects](#instrument-object). The example is abridged:

```jsonc
[
  {
    "id": "clarinet",
    "label": "Clarinet",
    "family": "woodwinds",
    "library": "VSCO 2 CE",
    "license": "CC0-1.0",
    "size_bytes": 59351881,
    "installed": true
  }
  // ...more instruments
]
```

This endpoint returns only the common errors.

## Get an instrument

`GET /api/instruments/<INSTRUMENT_ID>`

Returns an installed instrument's SFZ file parsed into regions: each region is
a sample and the keys and velocities it answers to.

| Name | Type | Required | Description |
|---|---|---|---|
| `<INSTRUMENT_ID>` | string | Yes | The instrument's id |

This request reads the clarinet's regions:

```bash
curl "http://127.0.0.1:17494/api/instruments/clarinet"
```

A successful response is `200 OK` with the regions. The example is abridged:

```jsonc
{
  "id": "clarinet",
  "files_url": "/api/instruments/clarinet/files",
  "regions": [
    {
      "sample": "Woodwinds/Clarinet/susLong/DCClar_susLong_A#2_v1_rr1_sum.wav",
      "lokey": 56,
      "hikey": 59,
      "pitchKeycenter": 58,
      "lovel": 0,
      "hivel": 41,
      "volume": 20,
      "tune": 0,
      "ampegAttack": 0.001,
      "ampegRelease": 0.7
    }
    // ...more regions
  ]
}
```

| Field | Type | Description |
|---|---|---|
| `files_url` | string | The URL the sample paths are served under |
| `regions[].sample` | string | The sample's path, relative to `files_url` |
| `lokey`, `hikey` | integer | The MIDI key range the region plays |
| `pitchKeycenter` | integer | The key the sample was recorded at |
| `lovel`, `hivel` | integer | The velocity range |
| `volume` | number | Level in dB |
| `tune` | number | Tuning in cents |
| `ampegAttack`, `ampegRelease` | number | Fade in and fade out, in seconds |

An opcode a region does not set takes the SFZ default.

| Status | Cause |
|---|---|
| `400` | No instrument has this id. The body has `detail` and `failure` |
| `404` | The instrument's samples are not installed |

## Download an instrument

`POST /api/instruments/<INSTRUMENT_ID>/download`

Queues a job that downloads the instrument's files from one pinned commit of
the VSCO 2 CE repository on GitHub into `<DATA_DIR>/models/instruments/`,
shared by every profile. Files already on disk at the right size are skipped,
so an interrupted download continues. While a download of the instrument is
running, the endpoint returns that job instead of queuing another.

| Name | Type | Required | Description |
|---|---|---|---|
| `<INSTRUMENT_ID>` | string | Yes | The instrument's id |

This request downloads the clarinet:

```bash
curl -X POST -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/instruments/clarinet/download"
```

A successful response is `202 Accepted` with the job's id. Progress streams
from [the job's events](https://mirli.app/docs/api-jobs/index.md#stream-a-jobs-events):

```json
{
  "job_id": "b7d2e5a1-4c9f-4a3b-8e6d-2f1c0a9b7e58"
}
```

| Status | Cause |
|---|---|
| `400` | No instrument has this id. The body has `detail` and `failure` |
| `403` | The server's capabilities turn `downloads` off |

## Delete an instrument

`DELETE /api/instruments/<INSTRUMENT_ID>`

Deletes the instrument's downloaded files. An instrument that is not installed
also returns `204`.

| Name | Type | Required | Description |
|---|---|---|---|
| `<INSTRUMENT_ID>` | string | Yes | The instrument's id |

This request deletes the clarinet's samples:

```bash
curl -X DELETE -H 'X-Soundbox: 1' \
  "http://127.0.0.1:17494/api/instruments/clarinet"
```

A successful response is `204 No Content`.

| Status | Cause |
|---|---|
| `400` | No instrument has this id. The body has `detail` and `failure` |
| `403` | The server's capabilities turn `downloads` off |

## Get a sample file

`GET /api/instruments/<INSTRUMENT_ID>/files/<PATH>`

Returns one of an installed instrument's files. Only paths in the
instrument's manifest are served, so nothing outside its folder can be named.

| Name | Type | Required | Description |
|---|---|---|---|
| `<INSTRUMENT_ID>` | string | Yes | The instrument's id |
| `<PATH>` | string | Yes | A region's `sample` path, each segment URL-encoded. Names contain `#` and spaces |

This request saves one clarinet sample:

```bash
curl -o a2.wav "http://127.0.0.1:17494/api/instruments/clarinet/files/\
Woodwinds/Clarinet/susLong/DCClar_susLong_A%232_v1_rr1_sum.wav"
```

A successful response is `200 OK` with `audio/wav`, or
`text/plain; charset=utf-8` for the `.sfz` file. The header
`Cache-Control: private, max-age=86400` lets a browser cache it for a day.

| Status | Cause |
|---|---|
| `404` | The instrument is unknown or not installed, or the path is not in its manifest |
