aboutsummaryrefslogtreecommitdiffstats
path: root/src/audio_player.rs
diff options
context:
space:
mode:
authorJokler <jokler@protonmail.com>2020-03-14 17:57:48 +0100
committerJokler <jokler@protonmail.com>2020-03-14 18:13:43 +0100
commit778f5216d269e7c861be706ce65b778b3db86f7c (patch)
tree561c4196b6f7dd28bc5943dcbb0c8448f90c2f27 /src/audio_player.rs
parent6f9806abdbfa61583b25fd544c41a017a574686c (diff)
downloadpokebot-778f5216d269e7c861be706ce65b778b3db86f7c.tar.gz
pokebot-778f5216d269e7c861be706ce65b778b3db86f7c.zip
Add a way of making relative volume changes
Additionally this moves seeking into the command parsing for consistency.
Diffstat (limited to 'src/audio_player.rs')
-rw-r--r--src/audio_player.rs23
1 files changed, 12 insertions, 11 deletions
diff --git a/src/audio_player.rs b/src/audio_player.rs
index d231c72..5f9c625 100644
--- a/src/audio_player.rs
+++ b/src/audio_player.rs
@@ -14,16 +14,10 @@ use std::sync::{Arc, RwLock};
use tokio02::sync::mpsc::UnboundedSender;
use crate::youtube_dl::AudioMetadata;
+use crate::command::{Seek, VolumeChange};
static GST_INIT: Once = Once::new();
-#[derive(Copy, Clone, Debug)]
-pub enum Seek {
- Positive(Duration),
- Negative(Duration),
- Absolute(Duration),
-}
-
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum PollResult {
Continue,
@@ -201,10 +195,17 @@ impl AudioPlayer {
Ok(())
}
- pub fn set_volume(&self, volume: f64) -> Result<(), AudioPlayerError> {
- *self.volume_f64.write().unwrap() = volume;
- let db = 50.0 * volume.log10();
- info!("Setting volume: {} -> {} dB", volume, db);
+ pub fn change_volume(&self, volume: VolumeChange) -> Result<(), AudioPlayerError> {
+ let new_volume = match volume {
+ VolumeChange::Positive(vol) => self.volume() + vol,
+ VolumeChange::Negative(vol) => self.volume() - vol,
+ VolumeChange::Absolute(vol) => vol,
+ };
+ let new_volume = new_volume.max(0.0).min(1.0);
+
+ *self.volume_f64.write().unwrap() = new_volume;
+ let db = 50.0 * new_volume.log10();
+ info!("Setting volume: {} -> {} dB", new_volume, db);
let linear =
StreamVolume::convert_volume(StreamVolumeFormat::Db, StreamVolumeFormat::Linear, db);