« get me outta code hell

Controls for pause/play, seek, and volume - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorliam4 <towerofnix@gmail.com>2017-06-26 22:16:59 -0300
committerliam4 <towerofnix@gmail.com>2017-06-26 22:16:59 -0300
commitafa6c4a7eaf9bdcc17f6ad7d948557d3e982ae9a (patch)
treeb839a40e9d1856d97bf4d0b0e5edca364fd5b495 /src
parent6d88565f91724f322b4db69d072b81ed27ed8da9 (diff)
Controls for pause/play, seek, and volume
Diffstat (limited to 'src')
-rwxr-xr-xsrc/http-music.js37
-rw-r--r--src/loop-play.js51
2 files changed, 80 insertions, 8 deletions
diff --git a/src/http-music.js b/src/http-music.js
index 48aff95..7686b45 100755
--- a/src/http-music.js
+++ b/src/http-music.js
@@ -281,6 +281,43 @@ Promise.resolve()
       process.stdin.setRawMode(true)
 
       process.stdin.on('data', data => {
+        const escModifier = Buffer.from('\x1b[')
+        const shiftModifier = Buffer.from('1;2')
+
+        const esc = num => Buffer.concat([escModifier, Buffer.from([num])])
+
+        const shiftEsc = num => (
+          Buffer.concat([escModifier, shiftModifier, Buffer.from([num])])
+        )
+
+        if (Buffer.from([0x20]).equals(data)) {
+          play.togglePause()
+        }
+
+        if (esc(0x43).equals(data)) {
+          play.seekAhead(5)
+        }
+
+        if (esc(0x44).equals(data)) {
+          play.seekBack(5)
+        }
+
+        if (shiftEsc(0x43).equals(data)) {
+          play.seekAhead(30)
+        }
+
+        if (shiftEsc(0x44).equals(data)) {
+          play.seekBack(30)
+        }
+
+        if (esc(0x41).equals(data)) {
+          play.volUp(10)
+        }
+
+        if (esc(0x42).equals(data)) {
+          play.volDown(10)
+        }
+
         if (Buffer.from('s').equals(data)) {
           // clearConsoleLine()
           // console.log(
diff --git a/src/loop-play.js b/src/loop-play.js
index 24f5303..328bb0b 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -240,7 +240,11 @@ class PlayController {
 
   playFile(file) {
     this.fifo = new FIFO()
-    this.process = spawn('mpv', ['--input-file=' + this.fifo.path, file])
+    this.process = spawn('mpv', [
+      '--input-file=' + this.fifo.path,
+      '--no-audio-display',
+      file
+    ])
 
     this.process.stderr.on('data', data => {
       const match = data.toString().match(
@@ -284,6 +288,36 @@ class PlayController {
     })
   }
 
+  skipCurrent() {
+    this.killProcess()
+  }
+
+  seekAhead(secs) {
+    this.sendCommand(`seek +${parseFloat(secs)}`)
+  }
+
+  seekBack(secs) {
+    this.sendCommand(`seek -${parseFloat(secs)}`)
+  }
+
+  volUp(amount) {
+    this.sendCommand(`add volume +${parseFloat(amount)}`)
+  }
+
+  volDown(amount) {
+    this.sendCommand(`add volume -${parseFloat(amount)}`)
+  }
+
+  togglePause() {
+    this.sendCommand('cycle pause')
+  }
+
+  sendCommand(command) {
+    if (this.fifo) {
+      this.fifo.write(command)
+    }
+  }
+
   killProcess() {
     if (this.process) {
       this.process.kill()
@@ -291,6 +325,7 @@ class PlayController {
 
     if (this.fifo) {
       this.fifo.close()
+      delete this.fifo
     }
 
     this.currentTrack = null
@@ -315,13 +350,13 @@ module.exports = function loopPlay(picker, downloader, playArgs = []) {
   return {
     promise,
 
-    skipCurrent: function() {
-      playController.killProcess()
-    },
-
-    skipUpNext: function() {
-      downloadController.skipUpNext()
-    },
+    seekBack: secs => playController.seekBack(secs),
+    seekAhead: secs => playController.seekAhead(secs),
+    skipCurrent: () => playController.skipCurrent(),
+    skipUpNext: () => downloadController.skipUpNext(),
+    volUp: amount => playController.volUp(amount),
+    volDown: amount => playController.volDown(amount),
+    togglePause: () => playController.togglePause(),
 
     kill: function() {
       playController.killProcess()