zaun_bot/audio.rs
1//! Low-level audio output (capability `voice`): the bot writes the audio itself instead of
2//! naming tracks for [`Voice`](crate::Voice)'s player. For synthesisers, text to speech,
3//! mixers, and formats the bot decodes on its own. Output only: bots never receive members'
4//! audio.
5//!
6//! One stream at a time, in the bot's voice channel; opening it pauses the player and closing it
7//! resumes the player. When the buffer runs low the bot gets
8//! [`VoiceEvent::AudioLow`](crate::voice::VoiceEvent::AudioLow), its cue to write more.
9//!
10//! ```no_run
11//! use zaun_bot::Audio;
12//! # fn demo(samples: &[i16]) -> Result<(), zaun_bot::types::HostError> {
13//! Audio::open_pcm(48_000, 1, Some("Beep"))?; // UNSUPPORTED on hosts without an encoder
14//! Audio::write_pcm(samples)?; // returns the ms buffered; BUSY past 3 s
15//! Audio::close(true); // after the buffer has played
16//! # Ok(()) }
17//! ```
18
19use crate::bindings::zaun::bot::audio as wit;
20pub use wit::{AudioFormat, PcmFormat, StreamOptions};
21
22use crate::types::HostError;
23
24/// The bot's audio output stream.
25#[derive(Debug, Clone, Copy, Default)]
26pub struct Audio;
27
28impl Audio {
29 /// Opens the stream with every option.
30 ///
31 /// # Errors
32 /// `CONFLICT` when the bot is in no voice channel or a stream is open; `UNSUPPORTED` for a
33 /// format the host cannot take; `BAD_REQUEST` for values out of range.
34 pub fn open(options: &StreamOptions) -> Result<(), HostError> {
35 wit::open(options)
36 }
37
38 /// Opens a stream of Opus packets the bot encodes ([`Audio::write_opus`]).
39 ///
40 /// # Errors
41 /// As [`Audio::open`].
42 pub fn open_opus(title: Option<&str>) -> Result<(), HostError> {
43 Self::open(&StreamOptions {
44 format: AudioFormat::Opus,
45 low_water_ms: None,
46 title: title.map(str::to_owned),
47 })
48 }
49
50 /// Opens a stream of 16-bit PCM ([`Audio::write_pcm`]) at `sample_rate` (8000, 12000,
51 /// 16000, 24000 or 48000) with 1 or 2 interleaved channels; the host encodes it.
52 ///
53 /// # Errors
54 /// As [`Audio::open`]; `UNSUPPORTED` on hosts without an Opus encoder.
55 pub fn open_pcm(sample_rate: u32, channels: u8, title: Option<&str>) -> Result<(), HostError> {
56 Self::open(&StreamOptions {
57 format: AudioFormat::Pcm(PcmFormat {
58 sample_rate,
59 channels,
60 }),
61 low_water_ms: None,
62 title: title.map(str::to_owned),
63 })
64 }
65
66 /// Writes Opus packets (2.5–120 ms each, ≤ 1000 bytes); returns the ms buffered.
67 ///
68 /// # Errors
69 /// `BUSY` past 3 s buffered (write again on `AudioLow`); `CONFLICT` without an `opus`
70 /// stream.
71 pub fn write_opus(packets: &[Vec<u8>]) -> Result<u32, HostError> {
72 wit::write_opus(packets)
73 }
74
75 /// Writes interleaved samples, any count; returns the ms buffered.
76 ///
77 /// # Errors
78 /// `BUSY` past 3 s buffered (write again on `AudioLow`); `CONFLICT` without a `pcm`
79 /// stream.
80 pub fn write_pcm(samples: &[i16]) -> Result<u32, HostError> {
81 wit::write_pcm(samples)
82 }
83
84 /// The audio written and not yet played, in ms (0 without a stream).
85 #[must_use]
86 pub fn buffered_ms() -> u32 {
87 wit::buffered_ms()
88 }
89
90 /// Drops everything buffered at once; the stream stays open.
91 ///
92 /// # Errors
93 /// `CONFLICT` without a stream.
94 pub fn clear() -> Result<(), HostError> {
95 wit::clear()
96 }
97
98 /// Closes the stream, after the buffer has played (`drain`) or at once; the player
99 /// resumes. [`VoiceEvent::AudioClosed`](crate::voice::VoiceEvent::AudioClosed) follows.
100 pub fn close(drain: bool) {
101 wit::close(drain);
102 }
103}