« get me outta code hell

Hide playback status - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/loop-play.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-08-25 16:36:37 -0300
committerFlorrie <towerofnix@gmail.com>2017-08-25 16:36:37 -0300
commit9a3b488fd91291e6ff848145d83fb7552b6bd398 (patch)
tree69e0a08b5ab5089eff9b91ade274476ecdd400c7 /src/loop-play.js
parent7f70ad795385503f6632198b44a41f6bc4e93e6f (diff)
Hide playback status
Diffstat (limited to 'src/loop-play.js')
-rw-r--r--src/loop-play.js37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/loop-play.js b/src/loop-play.js
index 585414b..212ee1c 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -22,6 +22,10 @@ const {
 } = require('./downloaders')
 
 class Player {
+  constructor() {
+    this.disablePlaybackStatus = false
+  }
+
   playFile(file) {}
   seekAhead(secs) {}
   seekBack(secs) {}
@@ -43,6 +47,10 @@ class MPVPlayer extends Player {
     this.process = spawn('mpv', this.getMPVOptions(file))
 
     this.process.stderr.on('data', data => {
+      if (this.disablePlaybackStatus) {
+        return
+      }
+
       const match = data.toString().match(
         /(..):(..):(..) \/ (..):(..):(..) \(([0-9]+)%\)/
       )
@@ -147,7 +155,25 @@ class SoXPlayer extends Player {
 
     this.process = spawn('play', [file])
 
-    return promisifyProcess(this.process)
+    this.process.stdout.on('data', data => {
+      process.stdout.write(data.toString())
+    })
+
+    // Most output from SoX is given to stderr, for some reason!
+    this.process.stderr.on('data', data => {
+      // The status line starts with "In:".
+      if (data.toString().trim().startsWith('In:')) {
+        if (this.disablePlaybackStatus) {
+          return
+        }
+      }
+
+      process.stdout.write(data.toString())
+    })
+
+    return new Promise(resolve => {
+      this.process.on('close', () => resolve())
+    })
   }
 
   async kill() {
@@ -425,7 +451,10 @@ class PlayController extends EventEmitter {
 }
 
 module.exports = async function startLoopPlay(
-  playlist, picker, playerCommand = 'mpv', playOpts = []
+  playlist, {
+    picker, playerCommand = 'mpv',
+    disablePlaybackStatus = false
+  }
 ) {
   // Looping play function. Takes one argument, the "picker" function,
   // which returns a track to play. Stops when the result of the picker
@@ -453,6 +482,8 @@ module.exports = async function startLoopPlay(
     return Promise.resolve()
   }
 
+  Object.assign(player, {disablePlaybackStatus})
+
   const downloadController = new DownloadController(playlist)
   await downloadController.init()
 
@@ -460,7 +491,7 @@ module.exports = async function startLoopPlay(
     picker, player, playlist, downloadController
   )
 
-  Object.assign(playController, {playerCommand, playOpts})
+  Object.assign(playController, {playerCommand})
 
   const promise = playController.loopPlay()