# Stem splitting

> Split a master into vocals, drums, bass and other, by API or embed.

Split a lossless song (WAV or FLAC) into four 24-bit WAV stems — **vocals**, **drums**, **bass**, and **other** — via the REST API or inside `<wavbee-stems>` on your own site.

The stems are made with Demucs (MIT licence), an open-source model from Meta AI. Stem separation was proposed and first
prototyped for wavbee by Noel Toms (`@noeltoms`) in `MSS_WebApp`.

Requires the `stems` feature on your account.

## REST API flow

Stem separation runs on top of a mastering session so the uploaded file is measured once and expires automatically after 7 days.

### 1. Upload the song

Create a session via `POST /v1/mastering/sessions` (multipart `file`, `audio_url`, or direct-upload `upload_id` for files over 32 MiB) and poll `GET /v1/mastering/sessions/:id` until `status` is `"ready"`.

### 2. Start the split

```bash
curl -X POST "$ACTIVQC_BASE_URL/v1/mastering/sessions/41/stems" \
  -H "Authorization: Bearer $WQC_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)"
```

Returns `202 Accepted` with a `mastering.stems` object (`status: "queued"`).

### 3. Poll and download the stems

```bash
curl "$ACTIVQC_BASE_URL/v1/mastering/stems/12" \
  -H "Authorization: Bearer $WQC_API_KEY"
```

When `status` is `"succeeded"`, `stems` maps each part (`vocals`, `drums`, `bass`, `other`) to a short-lived signed 24-bit WAV download URL (15-minute TTL).

## Embeddable stem splitter (`<wavbee-stems>`)

Mount the stem splitter and synchronized 4-track mute/solo mixer directly inside your app.

### 1. Mint a stems embed session on your server

Call `POST /v1/embed/sessions` with your secret API key and `product: "stems"` (optionally `require_payment: true` to gate stem downloads behind your checkout):

```bash
curl -X POST "$ACTIVQC_BASE_URL/v1/embed/sessions" \
  -H "Authorization: Bearer $WQC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "product": "stems",
    "end_user_ref": "artist-8842",
    "require_payment": false
  }'
```

A stems embed session works even if `mastering` is disabled on the account — only `stems` is required. Each session sees only the uploads and splits it created.

### 2. Mount the element

Load `/embed/v1/embed.js` (or use `Stems` from `@wavbee/release-builder-react`):

```html
<script type="module" src="https://YOUR-ACTIVQC-HOST/embed/v1/embed.js"></script>

<wavbee-stems client-token="wqc_es_..." theme="dark"></wavbee-stems>

<script type="module">
  const el = document.querySelector('wavbee-stems')

  el.addEventListener('wavbee:stems.completed', (e) => {
    console.log('Stems ready', e.detail.stem_ids) // ["vocals", "drums", "bass", "other"]
  })

  el.addEventListener('wavbee:download.requested', async (e) => {
    // When require_payment: true, charge your user, then call POST /v1/mastering/stems/:id/release
    await fetch(`/api/stems/checkout/${e.detail.stems_id}`, { method: 'POST' })
    el.notifyDownloadReleased()
  })

  el.addEventListener('wavbee:download.ready', (e) => {
    console.log('Stem downloads unlocked', e.detail.stems_id)
  })
</script>
```

### Events (`wavbee.stems.v1`)

All messages use `{ protocol: "wavbee.stems.v1", version: 1, type, nonce, data }` posted strictly to your `allowed_origin`, and are re-emitted on `<wavbee-stems>` as `wavbee:<type>` `CustomEvent`s:

| Event | `detail` payload | When |
|---|---|---|
| `ready` | `{ session_id }` | Embed mounted and session active |
| `resize` | `{ height }` | Content height changed (handled automatically by the loader) |
| `stems.completed` | `{ stems_id, stem_ids }` | Separation finished (`stem_ids`: `["vocals", "drums", "bass", "other"]`) |
| `download.requested` | `{ stems_id, stem }` | Artist clicked Download on a payment-held split |
| `download.ready` | `{ stems_id }` | Partner released the split via `POST /v1/mastering/stems/:id/release` |
| `session.expired` | `{}` | 30-minute embed session expired or was revoked |
| `error` | `{ code }` | An API or upload call failed |

## Metering

Each live stem separation that succeeds records one `stems_track` `UsageEvent` (keyed idempotently by separation id). Failed splits and sandbox sessions cost 0.
