« get me outta code hell

Play music - 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>2018-12-22 13:46:10 -0400
committerFlorrie <towerofnix@gmail.com>2018-12-22 13:57:16 -0400
commit50a3f912da28bc6a23864c0160295e219a990869 (patch)
tree9a00a61ae5747818b6ce543c5b9ba736f35fcd1d /players.js
parent72eff553df4fd4496172cf8d26b4585ac9f34b49 (diff)
Play music
Diffstat (limited to 'players.js')
-rw-r--r--players.js107
1 files changed, 15 insertions, 92 deletions
diff --git a/players.js b/players.js
index 1eacf7d..be303fd 100644
--- a/players.js
+++ b/players.js
@@ -51,21 +51,6 @@ class Player extends EventEmitter {
     this.disablePlaybackStatus = false
   }
 
-  set process(newProcess) {
-    this._process = newProcess
-    this._process.on('exit', code => {
-      if (code !== 0 && !this._killed) {
-        this.emit('crashed', code)
-      }
-
-      this._killed = false
-    })
-  }
-
-  get process() {
-    return this._process
-  }
-
   playFile(file) {}
   seekAhead(secs) {}
   seekBack(secs) {}
@@ -73,12 +58,7 @@ class Player extends EventEmitter {
   volDown(amount) {}
   togglePause() {}
 
-  async kill() {
-    if (this.process) {
-      this._killed = true
-      await killProcess(this.process)
-    }
-  }
+  async kill() {}
 
   printStatusLine(data) {
     // Quick sanity check - we don't want to print the status line if it's
@@ -90,6 +70,19 @@ class Player extends EventEmitter {
   }
 }
 
+module.exports.WebPlayer = class extends Player {
+  constructor() {
+    super()
+
+    this.audioEl = document.createElement('audio')
+  }
+
+  playFile(file) {
+    this.audioEl.src = file
+    this.audioEl.play()
+  }
+}
+
 module.exports.MPVPlayer = class extends Player {
   getMPVOptions(file) {
     return ['--no-video', file]
@@ -174,76 +167,6 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer {
   }
 }
 
-module.exports.SoXPlayer = class extends Player {
-  playFile(file) {
-    // SoX's play command is useful for systems that don't have MPV. SoX is
-    // much easier to install (and probably more commonly installed, as well).
-    // You don't get keyboard controls such as seeking or volume adjusting
-    // with SoX, though.
-
-    this.process = spawn('play', [file])
-
-    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
-        }
-
-        const timeRegex = '([0-9]*):([0-9]*):([0-9]*)\.([0-9]*)'
-        const match = data.toString().trim().match(new RegExp(
-          `^In:([0-9.]+%)\\s*${timeRegex}\\s*\\[${timeRegex}\\]`
-        ))
-
-        if (match) {
-          const percentStr = match[1]
-
-          // SoX takes a loooooot of math in order to actually figure out the
-          // duration, since it outputs the current time and the remaining time
-          // (but not the duration).
-
-          const [
-            curHour, curMin, curSec, curSecFrac, // ##:##:##.##
-            remHour, remMin, remSec, remSecFrac // ##:##:##.##
-          ] = match.slice(2).map(n => parseInt(n))
-
-          const duration = Math.round(
-            (curHour + remHour) * 3600 +
-            (curMin + remMin) * 60 +
-            (curSec + remSec) * 1 +
-            (curSecFrac + remSecFrac) / 100
-          )
-
-          const lenHour = Math.floor(duration / 3600)
-          const lenMin = Math.floor((duration - lenHour * 3600) / 60)
-          const lenSec = Math.floor(duration - lenHour * 3600 - lenMin * 60)
-
-          this.printStatusLine(getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec}))
-        }
-      }
-    })
-
-    return new Promise(resolve => {
-      this.process.on('close', () => resolve())
-    })
-  }
-}
-
 module.exports.getPlayer = async function() {
-  if (await commandExists('mpv')) {
-    if (await commandExists('mkfifo')) {
-      return new module.exports.ControllableMPVPlayer()
-    } else {
-      return new module.exports.MPVPlayer()
-    }
-  } else if (await commandExists('play')) {
-    return new module.exports.SoXPlayer()
-  } else {
-    return null
-  }
+  return new module.exports.WebPlayer()
 }