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
|
'use strict'
const fs = require('fs')
const tempy = require('tempy')
const { spawn } = require('child_process')
const { promisify } = require('util')
const fetch = require('node-fetch')
const path = require('path')
const promisifyProcess = require('./promisify-process')
const sanitize = require('sanitize-filename')
const writeFile = promisify(fs.writeFile)
module.exports = function loopPlay(picker, downloader, playArgs = []) {
// Looping play function. Takes one argument, the "pick" function,
// which returns a track to play. Preemptively downloads the next
// track while the current one is playing for seamless continuation
// from one song to the next. Stops when the result of the pick
// function is null (or similar). Optionally takes a second argument
// used as arguments to the `play` process (before the file name).
let playProcess, convertProcess
async function downloadNext() {
const picked = picker()
if (picked == null) {
return false
}
const [ title, downloaderArg ] = picked
console.log(`Downloading ${title}..\nDownloader arg: ${downloaderArg}`)
const downloadFile = await downloader(downloaderArg)
const tempDir = tempy.directory()
const wavFile = tempDir + `/.${sanitize(title)}.wav`
try {
const convertPromise = convert(downloadFile, wavFile)
convertProcess = convertPromise.process
await convertPromise
} catch(err) {
console.warn("Failed to convert " + title)
console.warn("Selecting a new track\n")
return await downloadNext()
}
return wavFile
}
async function main() {
let wavFile = await downloadNext()
while (wavFile) {
const nextPromise = downloadNext()
// What a mouthful!
const playPromise = playFile(wavFile, playArgs)
playProcess = playPromise.process
try {
await playPromise
} catch(err) {
console.warn(err)
}
wavFile = await nextPromise
}
}
const promise = main()
return {
promise,
skip: function() {
if (playProcess) playProcess.kill()
},
kill: function() {
if (playProcess) playProcess.kill()
if (convertProcess) convertProcess.kill()
}
}
}
function convert(fromFile, toFile) {
const avconv = spawn('avconv', ['-y', '-i', fromFile, toFile])
return promisifyProcess(avconv, false)
}
function playFile(file, opts = []) {
const play = spawn('play', [...opts, file])
return Object.assign(promisifyProcess(play), {process: play})
}
|