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

View File

@ -120,8 +120,10 @@ pub async fn start(
let messages = match event.code { let messages = match event.code {
// Arrow key volume controls. // Arrow key volume controls.
KeyCode::Up | KeyCode::Right => Messages::VolumeUp, KeyCode::Up => Messages::ChangeVolume(0.1),
KeyCode::Down | KeyCode::Left => Messages::VolumeDown, KeyCode::Right => Messages::ChangeVolume(0.01),
KeyCode::Down => Messages::ChangeVolume(-0.1),
KeyCode::Left => Messages::ChangeVolume(-0.01),
KeyCode::Char(character) => match character { KeyCode::Char(character) => match character {
// Ctrl+C // Ctrl+C
'c' if event.modifiers == KeyModifiers::CONTROL => break, 'c' if event.modifiers == KeyModifiers::CONTROL => break,
@ -136,8 +138,8 @@ pub async fn start(
'p' => Messages::Pause, 'p' => Messages::Pause,
// Volume up & down // Volume up & down
'+' | '=' => Messages::VolumeUp, '+' | '=' => Messages::ChangeVolume(0.1),
'-' | '_' => Messages::VolumeDown, '-' | '_' => Messages::ChangeVolume(-0.1),
_ => continue, _ => continue,
}, },
_ => 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 // 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. // 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); 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 volume = player.sink.volume();
let audio = (player.sink.volume() * AUDIO_WIDTH as f32).round() as usize; 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!( format!(
" volume: [{}{}] {}{} ", " volume: [{}{}] {}{} ",