# Struct Audio

> The bot's audio output stream.

`zaun_bot::Audio` · Struct

```rust
pub struct Audio
```

The bot's audio output stream.

## Methods

### `open`

```rust
pub fn open(options: &StreamOptions) -> Result<(), HostError>
```

Opens the stream with every option.

# Errors
`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 values out of range.

### `open_opus`

```rust
pub fn open_opus(title: Option<&str>) -> Result<(), HostError>
```

Opens a stream of Opus packets the bot encodes (`Audio::write_opus`).

# Errors
As `Audio::open`.

### `open_pcm`

```rust
pub fn open_pcm(sample_rate: u32, channels: u8, title: Option<&str>) -> Result<(), HostError>
```

Opens a stream of 16-bit PCM (`Audio::write_pcm`) at `sample_rate` (8000, 12000,
16000, 24000 or 48000) with 1 or 2 interleaved channels; the host encodes it.

# Errors
As `Audio::open`; `UNSUPPORTED` on hosts without an Opus encoder.

### `write_opus`

```rust
pub fn write_opus(packets: &[Vec<u8>]) -> Result<u32, HostError>
```

Writes Opus packets (2.5–120 ms each, ≤ 1000 bytes); returns the ms buffered.

# Errors
`BUSY` past 3 s buffered (write again on `AudioLow`); `CONFLICT` without an `opus`
stream.

### `write_pcm`

```rust
pub fn write_pcm(samples: &[i16]) -> Result<u32, HostError>
```

Writes interleaved samples, any count; returns the ms buffered.

# Errors
`BUSY` past 3 s buffered (write again on `AudioLow`); `CONFLICT` without a `pcm`
stream.

### `buffered_ms`

```rust
pub fn buffered_ms() -> u32
```

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

### `clear`

```rust
pub fn clear() -> Result<(), HostError>
```

Drops everything buffered at once; the stream stays open.

# Errors
`CONFLICT` without a stream.

### `close`

```rust
pub fn close(drain: bool)
```

Closes the stream, after the buffer has played (`drain`) or at once; the player
resumes. `VoiceEvent::AudioClosed` follows.

## Trait implementations

`Clone`, `Copy`, `Debug`, `Default`

[Full rustdoc page](/docs/api/rust/zaun-bot/zaun_bot/audio/struct.Audio.html)
