From: Kostya Shishkov Date: Sun, 21 Sep 2025 15:02:39 +0000 (+0200) Subject: videoplayer: make volume control more logarithmic X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=e2532c85230fad090c825176c91c71f29b259026;p=nihav-player.git videoplayer: make volume control more logarithmic This change trades flexibility in volume control for having more convenient pre-defined volume levels. And it is still possible to set arbitrary volume level from command line. --- diff --git a/videoplayer/src/main.rs b/videoplayer/src/main.rs index 47ca34e..8d91a9f 100644 --- a/videoplayer/src/main.rs +++ b/videoplayer/src/main.rs @@ -169,7 +169,35 @@ fn format_time(ms: u64) -> String { } const FRAME_QUEUE_LEN: usize = 25; -const MAX_VOLUME: usize = 200; +const MAX_VOLUME: usize = 400; + +const VOLUME_STEPS: &[usize] = &[5, 10, 25, 50, 75, 100, 125, 150, 175, 200, 250, 300, 350, 400]; + +fn find_volume_step_down(vol: usize) -> usize { + for vr in VOLUME_STEPS.windows(2) { + if vol > vr[0] && vol <= vr[1] { + return vr[0]; + } + } + if vol <= VOLUME_STEPS[0] { + 0 + } else { + MAX_VOLUME + } +} + +fn find_volume_step_up(vol: usize) -> usize { + for vr in VOLUME_STEPS.windows(2) { + if vol >= vr[0] && vol < vr[1] { + return vr[1]; + } + } + if vol < VOLUME_STEPS[0] { + VOLUME_STEPS[0] + } else { + MAX_VOLUME + } +} pub type FrameRecord = (NABufferType, u64); @@ -558,19 +586,19 @@ impl Player { Keycode::PageDown | Keycode::Kp3 => { self.seek(600, false, dmx, disp_queue)?; }, Keycode::Space => { self.toggle_pause(); }, Keycode::Plus | Keycode::KpPlus => { - self.volume = (self.volume + 10).min(MAX_VOLUME); + self.volume = find_volume_step_up(self.volume); if !self.mute { self.acontrol.set_volume(self.volume); } }, Keycode::Equals if keymod.contains(Mod::RSHIFTMOD) || keymod.contains(Mod::LSHIFTMOD) => { - self.volume = (self.volume + 10).min(MAX_VOLUME); + self.volume = find_volume_step_up(self.volume); if !self.mute { self.acontrol.set_volume(self.volume); } }, Keycode::Minus | Keycode::KpMinus => { - self.volume = self.volume.saturating_sub(10); + self.volume = find_volume_step_down(self.volume); if !self.mute { self.acontrol.set_volume(self.volume); }