zaun_bot/voice/mod.rs
1//! Voice channels and music (capability `voice`, ADR-0020).
2//!
3//! The host joins as the bot, end-to-end encrypted like any member, and plays audio for it in
4//! real time; the bot only says what to play. Audio is Opus: an `https://` Ogg Opus (or, on
5//! hosts that demux it, WebM Opus) file or stream the host fetches (under the bot's `network` grant, or an `http://` server next to
6//! the host under its `local_network` grant), Ogg Opus bytes the bot holds, or Opus packets
7//! the bot pushes. Nothing is transcoded.
8//!
9//! ```no_run
10//! use zaun_bot::Voice;
11//! # fn demo(channel: &str) -> Result<(), zaun_bot::types::HostError> {
12//! Voice::join(channel)?;
13//! Voice::play_url("https://music.example.org/intro.opus", Some("Intro"))?;
14//! Voice::enqueue_url("https://music.example.org/song.opus", Some("Song"))?;
15//! # Ok(()) }
16//! ```
17
18mod position;
19mod queue;
20
21use crate::bindings::zaun::bot::voice as wit;
22pub use wit::{LoopMode, PlayerStatus, VoiceEvent, VoiceState};
23
24use crate::types::HostError;
25
26/// The bot's voice: one stay at a time.
27#[derive(Debug, Clone, Copy, Default)]
28pub struct Voice;
29
30fn track(source: wit::Source, title: Option<&str>) -> wit::Track {
31 wit::Track {
32 source,
33 title: title.map(str::to_owned),
34 }
35}
36
37impl Voice {
38 /// Joins voice channel `channel` (leaving another one); returns once audio can flow.
39 ///
40 /// # Errors
41 /// `NOT_GRANTED` without the `voice` capability; `FORBIDDEN` without `CONNECT`/`SPEAK`.
42 pub fn join(channel: &str) -> Result<(), HostError> {
43 wit::join(channel)
44 }
45
46 /// Leaves; playback stops and the queue is cleared.
47 pub fn leave() {
48 wit::leave();
49 }
50
51 /// Plays an Ogg Opus file or stream now, replacing the current track: an `https://` URL
52 /// on a `network` host, or an `http://`/`https://` URL on a `local_network` address. The
53 /// SDK passes the URL through; the host checks it.
54 ///
55 /// # Errors
56 /// `CONFLICT` when not in a voice channel; an address outside the grants shows up as a
57 /// `TrackError` event when the track starts.
58 pub fn play_url(url: &str, title: Option<&str>) -> Result<(), HostError> {
59 wit::play(&track(wit::Source::Url(url.to_owned()), title))
60 }
61
62 /// Queues an Ogg Opus file or stream (as [`Voice::play_url`]); returns the queue's length.
63 ///
64 /// # Errors
65 /// As [`Voice::play_url`]; `CONFLICT` with `limit_reached` past 100 tracks.
66 pub fn enqueue_url(url: &str, title: Option<&str>) -> Result<u32, HostError> {
67 wit::enqueue(&track(wit::Source::Url(url.to_owned()), title))
68 }
69
70 /// Plays Ogg Opus bytes (≤ 16 MiB) now.
71 ///
72 /// # Errors
73 /// As [`Voice::play_url`].
74 pub fn play_ogg(bytes: Vec<u8>, title: Option<&str>) -> Result<(), HostError> {
75 wit::play(&track(wit::Source::Ogg(bytes), title))
76 }
77
78 /// Starts a track the bot feeds with [`Voice::push_opus`] until [`Voice::end_stream`].
79 ///
80 /// # Errors
81 /// As [`Voice::play_url`].
82 pub fn start_stream(title: Option<&str>) -> Result<(), HostError> {
83 wit::play(&track(wit::Source::Pushed, title))
84 }
85
86 /// Opus packets (2.5–120 ms each, ≤ 1000 bytes) for the current stream; returns the ms
87 /// buffered. Push about as fast as it plays.
88 ///
89 /// # Errors
90 /// `BUSY` past 3 s buffered (push again later); `CONFLICT` without a stream track.
91 pub fn push_opus(packets: &[Vec<u8>]) -> Result<u32, HostError> {
92 wit::push(packets)
93 }
94
95 /// Ends the stream track once its buffered packets have played.
96 pub fn end_stream() {
97 wit::end_stream();
98 }
99
100 /// Ends the current track; the next queued one starts.
101 pub fn skip() {
102 wit::skip();
103 }
104
105 /// Ends the current track and clears the queue.
106 pub fn stop() {
107 wit::stop();
108 }
109
110 /// Pauses.
111 pub fn pause() {
112 wit::pause();
113 }
114
115 /// Resumes.
116 pub fn resume() {
117 wit::resume();
118 }
119
120 /// Where the bot is and what it plays.
121 #[must_use]
122 pub fn state() -> VoiceState {
123 wit::state()
124 }
125}