aboutsummaryrefslogtreecommitdiff
path: root/src/voice/audio.rs
diff options
context:
space:
mode:
authorKyle Simpson <[email protected]>2018-01-31 19:12:56 +0000
committerZeyla Hellyer <[email protected]>2018-01-31 11:12:56 -0800
commit324a288fbb0dd7d135aa9aab876cf39dabb6a02e (patch)
tree24f13043f4ddcb5bc656b7a547170223d826aedb /src/voice/audio.rs
parentFix case insensitivity for aliases (#262) (diff)
downloadserenity-324a288fbb0dd7d135aa9aab876cf39dabb6a02e.tar.xz
serenity-324a288fbb0dd7d135aa9aab876cf39dabb6a02e.zip
Multiple audio stream playback, volume control, pausing
* Fix Speaking state, use latest voice API version * Speaking state would remain stuck on after playing particularly long stretches of audio. So far as I can tell, playing 5 frames of silence BEFORE changing the state seems to do the trick. * Added new constant to make sure the library uses v3 of the voice api, which it is written for. * Heartbeat interval adjusted by * .75 as recommended by Discord. * Initial version of new Audio wrapper. * Single audio file case, as before.. * Loop over all available audio samples. * Combine audio streams, account for volume. * Cheaper explicit Opus silence frames. As per Discord's recommendation, use a well-known 3-byte silence frame when needed. * A bit of cleanup Cleanup some of the code, rename some short-form fields to longer forms (e.g. `s/src/source`), and remove a breaking change. `Handler::play` was changed to return `LockedAudio` instead of `()`. If someone were to rely on `Handler::play` returning `()`, the return type change would break their code. Instead, this functionality has been added to a new `Handler::play_returning` function.
Diffstat (limited to 'src/voice/audio.rs')
-rw-r--r--src/voice/audio.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/voice/audio.rs b/src/voice/audio.rs
index 21f8f31..8a001d1 100644
--- a/src/voice/audio.rs
+++ b/src/voice/audio.rs
@@ -1,3 +1,6 @@
+use parking_lot::Mutex;
+use std::sync::Arc;
+
pub const HEADER_LEN: usize = 12;
pub const SAMPLE_RATE: u32 = 48_000;
@@ -29,3 +32,49 @@ pub enum AudioType {
Opus,
Pcm,
}
+
+/// Control object for audio playback.
+///
+/// Accessed by both commands and the playback code -- as such, access is
+/// always guarded.
+pub struct Audio {
+ pub playing: bool,
+ pub volume: f32,
+ pub finished: bool,
+ pub source: Box<AudioSource>,
+}
+
+impl Audio {
+ pub fn new(source: Box<AudioSource>) -> Self {
+ Self {
+ playing: true,
+ volume: 1.0,
+ finished: false,
+ source,
+ }
+ }
+
+ pub fn play(&mut self) -> &mut Self {
+ self.playing = true;
+
+ self
+ }
+
+ pub fn pause(&mut self) -> &mut Self {
+ self.playing = false;
+
+ self
+ }
+
+ pub fn volume(&mut self, volume: f32) -> &mut Self {
+ self.volume = volume;
+
+ self
+ }
+}
+
+/// Threadsafe form of an instance of the [`Audio`] struct, locked behind a
+/// Mutex.
+///
+/// [`Audio`]: struct.Audio.html
+pub type LockedAudio = Arc<Mutex<Audio>>;