« get me outta code hell

Auto-DJ - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/players.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-04-06 22:03:17 -0300
committerFlorrie <towerofnix@gmail.com>2020-04-06 22:03:17 -0300
commit0890f592ecdada66e8e264d6201e43ed0c6bf7ac (patch)
treec89cb63cfd5b19d2693b89c1477da925a234dc76 /players.js
parentef66a8613ddc0584a1f08696261f62c19cd7db6e (diff)
Auto-DJ
Diffstat (limited to 'players.js')
-rw-r--r--players.js32
1 files changed, 30 insertions, 2 deletions
diff --git a/players.js b/players.js
index 868129d..5fe4714 100644
--- a/players.js
+++ b/players.js
@@ -19,6 +19,7 @@ class Player extends EventEmitter {
     this.isLooping = false
     this.isPaused = false
     this.volume = 100
+    this.volumeMultiplier = 1.0
   }
 
   set process(newProcess) {
@@ -41,6 +42,8 @@ class Player extends EventEmitter {
   seekBack(secs) {}
   volUp(amount) {}
   volDown(amount) {}
+  setVolume(value) {}
+  updateVolume() {}
   togglePause() {}
   toggleLoop() {}
   setPause() {}
@@ -61,6 +64,25 @@ class Player extends EventEmitter {
       this.emit('printStatusLine', data)
     }
   }
+
+  setVolumeMultiplier(value) {
+    this.volumeMultiplier = value
+    this.updateVolume()
+  }
+
+  fadeIn() {
+    const interval = 50
+    const duration = 1000
+    const delta = 1.0 - this.volumeMultiplier
+    const id = setInterval(() => {
+      this.volumeMultiplier += delta * interval / duration
+      if (this.volumeMultiplier >= 1.0) {
+        this.volumeMultiplier = 1.0
+        clearInterval(id)
+      }
+      this.updateVolume()
+    }, interval)
+  }
 }
 
 module.exports.MPVPlayer = class extends Player {
@@ -72,7 +94,7 @@ module.exports.MPVPlayer = class extends Player {
     if (this.isPaused) {
       opts.unshift('--pause')
     }
-    opts.unshift('--volume=' + this.volume)
+    opts.unshift('--volume=' + this.volume * this.volumeMultiplier)
     return opts
   }
 
@@ -112,6 +134,8 @@ module.exports.MPVPlayer = class extends Player {
 
         this.printStatusLine(getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}))
       }
+
+      this.updateVolume();
     })
 
     return new Promise(resolve => {
@@ -177,7 +201,11 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
     this.volume = value
     this.volume = Math.max(0, this.volume)
     this.volume = Math.min(100, this.volume)
-    this.sendCommand('set_property', 'volume', this.volume)
+    this.updateVolume()
+  }
+
+  updateVolume() {
+    this.sendCommand('set_property', 'volume', this.volume * this.volumeMultiplier)
   }
 
   togglePause() {