# Interface: voice

> Voice channels (capability `voice`, ADR-0020). The host joins as the bot, end-to-end encrypted like any member, and plays audio for it in real time: the bot only says what to play. One stay per bot; `

Generated from `zaun:bot@0.2.0` (`wit/` in [zaun-bot](https://github.com/zaunchat/zaun-bot)). Do not edit.

Voice channels (capability `voice`, ADR-0020). The host joins as the bot, end-to-end
encrypted like any member, and plays audio for it in real time: the bot only says what to
play. One stay per bot; `CONNECT` and `SPEAK` apply as for members. Nothing is transcoded,
so audio is Opus: Ogg Opus files and streams (`.opus`, `.ogg` holding Opus), WebM Opus
(`audio/webm`, YouTube's itag 251) on hosts that demux it, or Opus packets the bot makes.

## Uses

- `use types.{id, host-error}`

## Functions

### `join`

```wit
join: func(channel: id) -> result<_, host-error>;
```

Joins voice channel `channel` (leaving another one first). Returns once media can
flow (at most about 10 s). `FORBIDDEN` without `voice`, `CONNECT` or `SPEAK`.

### `leave`

```wit
leave: func();
```

Leaves; playback stops and the queue is cleared.

### `play`

```wit
play: func(track: track) -> result<_, host-error>;
```

Plays `track` now, replacing the current one; the queue stays.

### `enqueue`

```wit
enqueue: func(track: track) -> result<u32, host-error>;
```

Adds `track` to the queue (≤ 100); returns the queue's length.

### `skip`

```wit
skip: func();
```

Ends the current track; the next one starts.

### `stop`

```wit
stop: func();
```

Ends the current track and clears the queue.

### `remove`

```wit
remove: func(index: u32) -> result<option<string>, host-error>;
```

Removes the queued track at `index` (0 plays next); returns its title. `NOT_FOUND`
past the end.

### `move`

```wit
move: func(%from: u32, to: u32) -> result<_, host-error>;
```

Moves the queued track at `from` to `to` (both 0-based); the tracks between shift.
`NOT_FOUND` past the end.

### `shuffle`

```wit
shuffle: func() -> result<_, host-error>;
```

Puts the queue in a random order (the current track keeps playing).

### `clear-queue`

```wit
clear-queue: func() -> result<_, host-error>;
```

Empties the queue; the current track keeps playing.

### `set-loop`

```wit
set-loop: func(mode: loop-mode) -> result<_, host-error>;
```

Sets what happens when a track ends (`off` when the bot joins).

### `seek`

```wit
seek: func(ms: u64) -> result<_, host-error>;
```

Jumps to `ms` into the current track (past the end: the track ends). `UNSUPPORTED`
for live streams and pushed audio, and on hosts that cannot seek; `CONFLICT` when
nothing plays.

### `set-volume`

```wit
set-volume: func(percent: u16) -> result<_, host-error>;
```

Sets the playback volume, 0-200 % (100 when the bot joins; `BAD_REQUEST` above 200).
Changing it means decoding and re-encoding the Opus audio, so hosts without an Opus
encoder answer `UNSUPPORTED`: call it anyway and tell the member.

### `pause`

```wit
pause: func();
```

### `resume`

```wit
resume: func();
```

### `push`

```wit
push: func(packets: list<list<u8>>) -> result<u32, host-error>;
```

Opus packets (2.5 to 120 ms each, ≤ 1000 bytes) for the current `pushed` track.
Returns the audio buffered, in ms; `BUSY` past 3 s, so push about as fast as it plays.

### `end-stream`

```wit
end-stream: func();
```

The `pushed` track ends once its buffered packets have played.

### `state`

```wit
state: func() -> voice-state;
```

Where the bot is and what it plays.

## Types

### `source`

*variant*

Where a track's audio comes from.

| Member | Description |
|---|---|
| `url(string)` | An `https://` Ogg Opus or WebM Opus file or stream (WebM where the host demuxes it; elsewhere a `track-error`), fetched by the host under the bot's `network` grant (the server learns the host's address) and played as it arrives. `http://` and `https://` URLs to a `local_network` address are fetched the same way, without leaving the host's machine or network. |
| `ogg(list<u8>)` | Ogg Opus bytes the bot already holds (≤ 16 MiB). |
| `pushed` | Opus packets the bot pushes with `push`, until `end-stream`. |

### `track`

*record*

Something to play.

| Member | Description |
|---|---|
| `source: source` |  |
| `title: option<string>` | Shown in events and `state`. |

### `player-status`

*enum*

| Member | Description |
|---|---|
| `idle` |  |
| `playing` |  |
| `paused` |  |
| `auto-paused` |  |

### `loop-mode`

*enum*

What happens when a track ends: go on (`off`), play it again (`track`), or put it at
the end of the queue (`queue`).

| Member | Description |
|---|---|
| `off` |  |
| `track` |  |
| `queue` |  |

### `voice-state`

*record*

| Member | Description |
|---|---|
| `channel: option<id>` | The voice channel the bot is in. |
| `status: player-status` |  |
| `current: option<string>` | The current track's title. |
| `queued: list<option<string>>` | The queued tracks' titles, in order (index 0 plays next). |
| `loop-mode: loop-mode` | What happens when a track ends. |
| `position-ms: u64` | How far into the current track playback is, in ms (0 when idle). Paused time does not count. |
| `duration-ms: option<u64>` | The current track's length in ms, when known: `none` for live streams, pushed audio, and files whose length the host cannot tell yet. |
| `participants: list<id>` | The members in the bot's voice channel, the bot itself left out (empty when it is in none). Empty means the bot is alone. |

### `voice-event`

*variant*

What happened, for `on-voice-event`.

| Member | Description |
|---|---|
| `track-start(option<string>)` | A track started (its title). |
| `track-end(option<string>)` | A track ended: played out, skipped, stopped or failed. |
| `track-error(string)` | A track could not be read (the text names no URL). |
| `member-joined(id)` | A member joined the bot's voice channel. |
| `member-left(id)` | A member left it. |
| `ended(option<string>)` | The bot's stay ended (left, moved, disconnected, the host or relay went away). |
| `queue-ended` | The last track played out with nothing queued (and no loop): the player is idle. Not sent after `stop` or `leave`. |
| `audio-low(u32)` | The `audio` stream's buffer fell below its low-water mark: write more. The audio still buffered, in ms. Sent again only after a write. |
| `audio-closed` | The `audio` stream closed: drained after `close`, closed at once, or the stay ended. |
