« get me outta code hell

(v), (V) - Volume controls - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-01-24 23:32:42 -0400
committerFlorrie <towerofnix@gmail.com>2019-01-24 23:32:42 -0400
commit5762a825d589704a53aaabb425d9470c726bbc0d (patch)
tree6b772bd8eccc16fd81dadca30ee65a6a56454de2
parentb4e575f4ec8b65464f9a8f109ddf3907cf30b853 (diff)
(v), (V) - Volume controls
"v" increases because it's lowercase and therefore more likely to be
accidentally pressed, hence more likely to confuse the user when it
decreases the volume (if it decreased the volume - since it increases
the volume, it generally doesn't seem to have any effect until you've
already discovered that v and V are used for volume controls).
-rw-r--r--README.md1
-rw-r--r--players.js8
-rw-r--r--todo.txt3
-rw-r--r--ui.js15
4 files changed, 25 insertions, 2 deletions
diff --git a/README.md b/README.md
index abd8183..5c95677 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,7 @@ You're also welcome to share any ideas, suggestions, and questions through there
 * <kbd>l</kbd> - toggle track loop
 * <kbd>Right</kbd> - seek ahead
 * <kbd>Left</kbd> - seek back
+* <kbd>v</kbd> and <kbd>V</kbd> - increase and decrease playback volume
 * <kbd><kbd>Ctrl</kbd>+<kbd>F</kbd></kbd> or <kbd>/</kbd> - jump to a track or group by entering (part of) its name
 * <kbd><kbd>Ctrl</kbd>+<kbd>O</kbd></kbd> - open a playlist from a source (like a /path/to/a/folder or a YouTube playlist URL) (you can also just pass this source to `mtui`)
 * <kbd>t</kbd> and <kbd>T</kbd> (shift+T) - switch between playlist tabs
diff --git a/players.js b/players.js
index fdcd038..e9cf76e 100644
--- a/players.js
+++ b/players.js
@@ -50,6 +50,7 @@ class Player extends EventEmitter {
 
     this.disablePlaybackStatus = false
     this.isLooping = false
+    this.volume = 100
   }
 
   set process(newProcess) {
@@ -98,6 +99,7 @@ module.exports.MPVPlayer = class extends Player {
     if (this.isLooping) {
       opts.unshift('--loop')
     }
+    opts.unshift('--volume', this.volume)
     return opts
   }
 
@@ -174,11 +176,13 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
   }
 
   volUp(amount) {
-    this.sendCommand(`add volume +${parseFloat(amount)}`)
+    this.volume = Math.min(100, this.volume + amount)
+    this.sendCommand(`set volume ${this.volume}`)
   }
 
   volDown(amount) {
-    this.sendCommand(`add volume -${parseFloat(amount)}`)
+    this.volume = Math.max(0, this.volume - amount)
+    this.sendCommand(`set volume ${this.volume}`)
   }
 
   togglePause() {
diff --git a/todo.txt b/todo.txt
index dfb14d6..13db542 100644
--- a/todo.txt
+++ b/todo.txt
@@ -191,3 +191,6 @@ TODO: A "play later" option for songs in the queue, identical to distributing
 
 TODO: Loop one song!
       (Done!)
+
+TODO: Volume controls!
+      (Done!)
diff --git a/ui.js b/ui.js
index 4a8f0a4..67f1bf4 100644
--- a/ui.js
+++ b/ui.js
@@ -409,6 +409,10 @@ class AppElement extends FocusElement {
       this.togglePause()
     } else if (telc.isCaselessLetter(keyBuf, 'l')) {
       this.toggleLoop()
+    } else if (telc.isCharacter(keyBuf, 'v')) {
+      this.volUp()
+    } else if (telc.isCharacter(keyBuf, 'V')) {
+      this.volDown()
     } else if (telc.isEscape(keyBuf)) {
       this.clearPlayingTrack()
     } else if (telc.isShiftUp(keyBuf) || telc.isCaselessLetter(keyBuf, 'p')) {
@@ -503,6 +507,14 @@ class AppElement extends FocusElement {
     this.player.toggleLoop()
   }
 
+  volUp(amount = 10) {
+    this.player.volUp(amount)
+  }
+
+  volDown(amount = 10) {
+    this.player.volDown(amount)
+  }
+
   stopPlaying() {
     // We emit this so playTrack doesn't immediately start a new track.
     // We aren't *actually* about to play a new track.
@@ -1664,6 +1676,9 @@ class PlaybackInfoElement extends DisplayElement {
     if (player.isLooping) {
       this.progressTextLabel.text += ' [Looping]'
     }
+    if (player.volume !== 100) {
+      this.progressTextLabel.text += ` [Volume: ${Math.round(player.volume)}%]`
+    }
     this.fixLayout()
   }