« get me outta code hell

loop-play.js « src - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/loop-play.js
blob: 585414b89c5b67ea17c9a19bf37b8772a593070e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// This isn't actually the code for the `play` command! That's in `play.js`.

// NOTE TO FUTURE SELF AND READERS:
// Please be careful to discern the target of methods such as waitForDownload.
// DownloadController and PlayController are messes and have lots of functions
// of the same name but completely different purposes. (Also keep an eye out
// for similarly/identically-named events between the two classes.)

'use strict'

const { spawn } = require('child_process')
const FIFO = require('fifo-js')
const EventEmitter = require('events')
const promisifyProcess = require('./promisify-process')
const killProcess = require('./kill-process')
const { getItemPathString } = require('./playlist-utils')

const { safeUnlink } = require('./playlist-utils')

const {
  getDownloaderFor, byName: downloadersByName, makeConverter
} = require('./downloaders')

class Player {
  playFile(file) {}
  seekAhead(secs) {}
  seekBack(secs) {}
  volUp(amount) {}
  volDown(amount) {}
  togglePause() {}
  kill() {}
}

class MPVPlayer extends Player {
  getMPVOptions(file) {
    return ['--no-audio-display', file]
  }

  playFile(file) {
    // The more powerful MPV player. MPV is virtually impossible for a human
    // being to install; if you're having trouble with it, try the SoX player.

    this.process = spawn('mpv', this.getMPVOptions(file))

    this.process.stderr.on('data', data => {
      const match = data.toString().match(
        /(..):(..):(..) \/ (..):(..):(..) \(([0-9]+)%\)/
      )

      if (match) {
        const [
          curHour, curMin, curSec, // ##:##:##
          lenHour, lenMin, lenSec, // ##:##:##
          percent // ###%
        ] = match.slice(1)

        let curStr, lenStr

        // We don't want to display hour counters if the total length is less
        // than an hour.
        if (parseInt(lenHour) > 0) {
          curStr = `${curHour}:${curMin}:${curSec}`
          lenStr = `${lenHour}:${lenMin}:${lenSec}`
        } else {
          curStr = `${curMin}:${curSec}`
          lenStr = `${lenMin}:${lenSec}`
        }

        // Multiplication casts to numbers; addition prioritizes strings.
        // Thanks, JavaScript!
        const curSecTotal = (3600 * curHour) + (60 * curMin) + (1 * curSec)
        const lenSecTotal = (3600 * lenHour) + (60 * lenMin) + (1 * lenSec)
        const percentVal = (100 / lenSecTotal) * curSecTotal
        const percentStr = (Math.trunc(percentVal * 100) / 100).toFixed(2)

        process.stdout.write(
          `\x1b[K~ (${percentStr}%) ${curStr} / ${lenStr}\r`
        )
      }
    })

    return new Promise(resolve => {
      this.process.once('close', resolve)
    })
  }

  async kill() {
    if (this.process) {
      await killProcess(this.process)
    }
  }
}

class ControllableMPVPlayer extends MPVPlayer {
  getMPVOptions(file) {
    return ['--input-file=' + this.fifo.path, ...super.getMPVOptions(file)]
  }

  playFile(file) {
    this.fifo = new FIFO()

    return super.playFile(file)
  }

  sendCommand(command) {
    if (this.fifo) {
      this.fifo.write(command)
    }
  }

  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')
  }

  kill() {
    if (this.fifo) {
      this.fifo.close()
      delete this.fifo
    }

    return super.kill()
  }
}

class SoXPlayer 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])

    return promisifyProcess(this.process)
  }

  async kill() {
    if (this.process) {
      await killProcess(this.process)
    }
  }
}

class DownloadController extends EventEmitter {
  constructor(playlist) {
    super()

    this.playlist = playlist
  }

  async init() {
    this.converter = await makeConverter('wav')
  }

  waitForDownload() {
    // Returns a promise that resolves when a download is
    // completed.  Note that this isn't necessarily the download
    // that was initiated immediately before a call to
    // waitForDownload (if any), since that download may have
    // been canceled (see cancel).  You can also listen for the
    // 'downloaded' event instead.

    return new Promise((resolve, reject) => {
      const onDownloaded = file => { clear(); resolve(file) }
      const onErrored = err => { clear(); reject(err) }

      const clear = () => {
        this.removeListener('downloaded', onDownloaded)
        this.removeListener('errored', onErrored)
      }

      this.once('downloaded', onDownloaded)
      this.once('errored', onErrored)
    })
  }

  async download(downloader, arg) {
    // Downloads a file.  This doesn't return anything; use
    // waitForDownload to get the result of this.
    // (The reasoning is that it's possible for a download to
    // be canceled and replaced with a new download (see cancel)
    // which would void the result of the old download.)
    // The resulting file is a WAV.

    this.cleanupListeners()

    let canceled = false

    this._handleCanceled = () => {
      canceled = true
      this.cleanupListeners()
    }

    this.once('canceled', this._handleCanceled)

    let downloadFile

    // TODO: Be more specific; 'errored' * 2 could instead be 'downloadErrored' and
    // 'convertErrored'.

    try {
      downloadFile = await downloader(arg)
    } catch(err) {
      this.emit('errored', err)
      return
    }

    // If this current download has been canceled, we should get rid of the
    // download file (and shouldn't emit a download success).
    if (canceled) {
      await safeUnlink(downloadFile, this.playlist)
      return
    }

    let convertFile

    try {
      convertFile = await this.converter(downloadFile)
    } catch(err) {
      this.emit('errored', err)
      return
    } finally {
      // Whether the convertion succeeds or not (hence 'finally'), we should
      // delete the temporary download file.
      await safeUnlink(downloadFile, this.playlist)
    }

    // Again, if canceled, we should delete temporary files and stop.
    if (canceled) {
      await safeUnlink(convertFile, this.playlist)
      return
    }

    this.emit('downloaded', convertFile)
    this.cleanupListeners()
  }

  cleanupListeners() {
    if (this._handleCanceled) {
      this.removeListener('canceled', this._handleCanceled)
    }
  }

  cancel() {
    // Cancels the current download.  This doesn't cancel any
    // waitForDownload promises, though -- you'll need to start
    // a new download to resolve those.

    this.emit('canceled')
    this.cleanupListeners()
  }
}

class PlayController extends EventEmitter {
  constructor(picker, player, playlist, downloadController) {
    super()

    this.picker = picker
    this.player = player
    this.playlist = playlist
    this.downloadController = downloadController

    this.currentTrack = null
    this.nextTrack = null
    this.nextFile = undefined // TODO: Why isn't this null?
    this.stopped = false
  }

  async loopPlay() {
    // Null would imply there's NO up-next track, but really we just haven't
    // set it yet.
    this.nextTrack = undefined

    this.startNextDownload()

    await this.waitForDownload()

    while (this.nextTrack && !this.stopped) {
      this.currentTrack = this.nextTrack

      const next = this.nextFile
      this.nextFile = undefined

      this.startNextDownload()

      if (next) {
        await this.playFile(next)

        // Now that we're done playing the file, we should delete it.. unless
        // it's the file that's coming up! (This would only happen in the case
        // that all temporary files are stored in the same folder, together;
        // indeed an unusual case, but technically possible.)
        if (next !== this.nextFile) {
          await safeUnlink(next, this.playlist)
        }
      }

      await this.waitForDownload()
    }
  }

  waitForDownload() {
    return new Promise(resolve => {
      if (this.isDownloading) {
        this.once('downloaded', () => resolve())
      } else {
        resolve()
      }
    })
  }

  startNextDownload() {
    this.isDownloading = true

    const picked = this.picker()
    this.nextTrack = picked

    if (!picked) {
      return null
    }

    let downloader

    if (picked.downloader) {
      downloader = downloadersByName[picked.downloader]()

      if (!downloader) {
        console.error(
          `Invalid downloader for track ${picked.name}:`, downloader
        )

        return false
      }
    } else {
      downloader = getDownloaderFor(picked.downloaderArg)
    }

    this.downloadController.download(downloader, picked.downloaderArg)

    this.downloadController.waitForDownload()
      .then(file => {
        this.isDownloading = false
        this.nextFile = file
        this.emit('downloaded')
      })
      .catch(() => {
        console.warn(
          "\x1b[31mFailed to download (or convert) track \x1b[1m" +
          getItemPathString(this.nextTrack) + "\x1b[0m"
        )

        this.startNextDownload()
      })

    return picked
  }

  playFile(file) {
    return this.player.playFile(file)
  }

  async skip() {
    // TODO: It would be nice if this returned the next track, but that
    // probably isn't possible with the current play/loop-setup.

    await this.player.kill()
    this.currentTrack = null
  }

  async skipUpNext() {
    if (this.nextFile) {
      await safeUnlink(this.nextFile, this.playlist)
    }

    this.downloadController.cancel()
    return this.startNextDownload()
  }

  async stop() {
    // TODO: How to bork download-controller files?? Wait for it to emit a
    // 'cleaned up' event? This whole program being split-up is a Baaaaad idea.
    this.downloadController.cancel()
    await this.player.kill()
    this.currentTrack = null
    this.stopped = true
  }

  logTrackInfo() {
    const getMessage = t => {
      let path = getItemPathString(t)

      return (
        `\x1b[1m${t.name} \x1b[0m@ ${path} \x1b[2m${t.downloaderArg}\x1b[0m`
      )
    }

    if (this.currentTrack) {
      console.log(`Playing: ${getMessage(this.currentTrack)}`)
    } else {
      console.log("No song currently playing.")
    }

    if (this.nextTrack) {
      console.log(`Up next: ${getMessage(this.nextTrack)}`)
    } else {
      console.log("No song up next.")
    }
  }
}

module.exports = async function startLoopPlay(
  playlist, picker, playerCommand = 'mpv', playOpts = []
) {
  // Looping play function. Takes one argument, the "picker" function,
  // which returns a track to play. Stops when the result of the picker
  // function is null (or similar). Optionally takes a second argument
  // used as arguments to the `play` process (before the file name).

  let player
  if (playerCommand === 'sox' || playerCommand === 'play') {
    player = new SoXPlayer()
  } else if (playerCommand === 'mpv') {
    player = new ControllableMPVPlayer()
  } else if (
    playerCommand === 'mpv-nocontrolls' ||
    playerCommand === 'mpv-windows' ||
    playerCommand === 'mpv-nofifo'
  ) {
    player = new MPVPlayer()
  } else {
    if (playerCommand) {
      console.warn('Invalid player command given?', playerCommand)
    } else {
      console.warn('No player command given?')
    }

    return Promise.resolve()
  }

  const downloadController = new DownloadController(playlist)
  await downloadController.init()

  const playController = new PlayController(
    picker, player, playlist, downloadController
  )

  Object.assign(playController, {playerCommand, playOpts})

  const promise = playController.loopPlay()

  return {
    promise,
    playController,
    downloadController,
    player
  }
}