feat: fine grained volume adjustment (#8)

This commit is contained in:
Samsu 2024-10-03 14:27:27 +02:00 committed by GitHub
parent a414c5e9f4
commit 49e7191369
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 22 deletions

View File

@ -39,11 +39,8 @@ pub enum Messages {
/// Pauses the [Sink]. This will also unpause it if it is paused.
Pause,
/// Increase the volume of playback
VolumeUp,
/// Decrease the volume of playback
VolumeDown,
/// Change the volume of playback
ChangeVolume(f32),
}
const TIMEOUT: Duration = Duration::from_secs(8);
@ -220,17 +217,8 @@ impl Player {
player.sink.pause();
}
}
Messages::VolumeUp => {
// Increase the volume, if possible.
if player.sink.volume() < 1.0 {
player.sink.set_volume(player.sink.volume() + 0.1);
}
}
Messages::VolumeDown => {
// Decreaes the volume, if possible.
if player.sink.volume() > 0.0 {
player.sink.set_volume(player.sink.volume() - 0.1);
}
Messages::ChangeVolume(change) => {
player.sink.set_volume((player.sink.volume() + change).clamp(0.0, 1.0));
}
}
}

View File

@ -120,8 +120,10 @@ pub async fn start(
let messages = match event.code {
// Arrow key volume controls.
KeyCode::Up | KeyCode::Right => Messages::VolumeUp,
KeyCode::Down | KeyCode::Left => Messages::VolumeDown,
KeyCode::Up => Messages::ChangeVolume(0.1),
KeyCode::Right => Messages::ChangeVolume(0.01),
KeyCode::Down => Messages::ChangeVolume(-0.1),
KeyCode::Left => Messages::ChangeVolume(-0.01),
KeyCode::Char(character) => match character {
// Ctrl+C
'c' if event.modifiers == KeyModifiers::CONTROL => break,
@ -136,8 +138,8 @@ pub async fn start(
'p' => Messages::Pause,
// Volume up & down
'+' | '=' => Messages::VolumeUp,
'-' | '_' => Messages::VolumeDown,
'+' | '=' => Messages::ChangeVolume(0.1),
'-' | '_' => Messages::ChangeVolume(-0.1),
_ => continue,
},
_ => continue,
@ -145,7 +147,7 @@ pub async fn start(
// If it's modifying the volume, then we'll set the `volume_timer` to 1
// so that the ui thread will know that it should show the audio bar.
if messages == Messages::VolumeDown || messages == Messages::VolumeUp {
if let Messages::ChangeVolume(_) = messages {
volume_timer.store(1, Ordering::Relaxed);
}

View File

@ -49,7 +49,7 @@ pub fn audio_bar(player: &Arc<Player>) -> String {
let volume = player.sink.volume();
let audio = (player.sink.volume() * AUDIO_WIDTH as f32).round() as usize;
let percentage = format!("{}%", (volume * 100.0).ceil().abs());
let percentage = format!("{}%", (volume * 100.0).round().abs());
format!(
" volume: [{}{}] {}{} ",