]> git.nihav.org Git - nihav-player.git/commitdiff
videoplayer: make volume control more logarithmic
authorKostya Shishkov <kostya.shishkov@gmail.com>
Sun, 21 Sep 2025 15:02:39 +0000 (17:02 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Sun, 21 Sep 2025 15:02:39 +0000 (17:02 +0200)
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.

videoplayer/src/main.rs

index 47ca34ebbdd5f1a646a56547f9514c1f560042bf..8d91a9f1aa6713d9ba0982cffdbda61469da9ba9 100644 (file)
@@ -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);
                         }