« 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
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
parent72eff553df4fd4496172cf8d26b4585ac9f34b49 (diff)
Play music
-rw-r--r--crawlers.js2
-rw-r--r--downloaders.js2
-rw-r--r--players.js107
3 files changed, 16 insertions, 95 deletions
diff --git a/crawlers.js b/crawlers.js
index 3a1436d..9309f7d 100644
--- a/crawlers.js
+++ b/crawlers.js
@@ -55,7 +55,6 @@ function crawlHTTP(absURL, opts = {}, internals = {}) {
     .then(
       res => res.text().then(async text => {
         const links = getHTMLLinks(text)
-        console.log(links)
 
         const items = []
 
@@ -78,7 +77,6 @@ function crawlHTTP(absURL, opts = {}, internals = {}) {
           let base
           if (path.extname(absURL)) {
             base = path.dirname(absURL) + '/'
-            console.log('extname:', path.extname(absURL), 'so base:', base)
           } else {
             base = absURL
           }
diff --git a/downloaders.js b/downloaders.js
index f5df4d2..0054ca7 100644
--- a/downloaders.js
+++ b/downloaders.js
@@ -16,7 +16,7 @@ const downloaders = {
 
   getDownloaderFor: arg => {
     if (arg.startsWith('http://') || arg.startsWith('https://')) {
-      return downloaders.http
+      return downloaders.fetch
     } else {
       return null
     }
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()
 }