# Interface: audio

> Low-level audio output (capability `voice`): the bot writes the audio itself, packet by packet or sample by sample, instead of naming tracks for the player. For synthesisers, text to speech, mixers, a

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

Low-level audio output (capability `voice`): the bot writes the audio itself, packet by
packet or sample by sample, instead of naming tracks for the player. For synthesisers, text
to speech, mixers, and bots that decode formats of their own. Output only: members' audio is
never delivered to a bot.

One stream at a time, in the bot's voice channel. Opening it pauses the player (`voice`);
closing it resumes the player. `voice-event.audio-low` asks for more, so a bot can write as
it plays without timers.

## Uses

- `use types.{host-error}`

## Functions

### `open`

```wit
open: func(options: stream-options) -> result<_, host-error>;
```

Opens the stream in the bot's voice channel, pausing the player. `CONFLICT` when the
bot is in no voice channel or a stream is open; `UNSUPPORTED` for a format the host
cannot take; `BAD_REQUEST` for a rate, channel count or low-water mark out of range.

### `write-opus`

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

Opus packets, for an `opus` stream. Returns the audio buffered, in ms; `BUSY` past 3 s.

### `write-pcm`

```wit
write-pcm: func(samples: list<s16>) -> result<u32, host-error>;
```

Interleaved samples, for a `pcm` stream; any count (the host frames them). Returns the
audio buffered, in ms; `BUSY` past 3 s.

### `buffered-ms`

```wit
buffered-ms: func() -> u32;
```

The audio written and not yet played, in ms (0 without a stream).

### `clear`

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

Drops everything buffered at once, keeping the stream open (to interrupt speech).

### `close`

```wit
close: func(drain: bool);
```

Closes the stream: once the buffer has played (`drain`), or at once. The player
resumes. Nothing happens without a stream.

## Types

### `pcm-format`

*record*

PCM the host encodes to Opus.

| Member | Description |
|---|---|
| `sample-rate: u32` | 8000, 12000, 16000, 24000 or 48000 Hz (Opus's own rates; nothing is resampled). |
| `channels: u8` | 1 (mono) or 2 (stereo, interleaved left-right). |

### `audio-format`

*variant*

How the bot's audio is encoded.

| Member | Description |
|---|---|
| `opus` | Opus packets (2.5-120 ms each, at most 1000 bytes), sent as they are. |
| `pcm(pcm-format)` | Signed 16-bit PCM, encoded to Opus by the host: `UNSUPPORTED` on hosts without an Opus encoder. |

### `stream-options`

*record*

| Member | Description |
|---|---|
| `format: audio-format` |  |
| `low-water-ms: option<u32>` | Send `audio-low` when less than this is buffered, in ms (20-2000; default 200). |
| `title: option<string>` | Shown as the current title in `voice-state` while the stream plays. |
