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 | 1x 1x 1x 1x 1x 1x 15x 4x 11x 11x 6x 5x 5x 5x 47x 47x 7x 7x 40x 40x 40x 40x 40x 1x 17x 26x 37x 37x 37x 37x 37x 37x 1x 36x 27x 36x 16x 11x 5x 16x 16x 1x 15x 20x 2x 20x 1x 1x 1x 19x 3x 8x 8x 8x 2x 2x 6x 4x 6x 6x 6x 8x 3x 3x 3x 1x 34x 31x 31x 34x 1x 34x 34x 31x 34x 34x 34x 34x 16x 16x 6x 16x 16x 34x 34x 34x 2x 1x 2x 1x 34x 2x 1x 1x 1x 1x 1x 1x 1x 34x 34x 3x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x | 'use strict'; // Dependencies: Built-in const cp = require('child_process'); const Emitter = require('events').EventEmitter; const path = require('path'); // Program Specific const priv = new WeakMap(); try { cp.spawnSync('kill -9 $(pgrep "madplay")'); } catch (error) {} function toSeconds(time) { if (typeof time === 'number') { return time; } const num = Number(time); if (!Number.isNaN(num)) { return num; } const sets = time.split(':'); const first = sets[0]; return { 1: parseFloat(first, 10), 2: (parseInt(first, 10) * 60) + parseFloat(sets[1], 10), 3: (parseInt(first, 10) * 3600) + (parseInt(sets[1], 10) * 60) + parseFloat(sets[2], 10) }[sets.length || 1]; } class Player extends Emitter { constructor(filename) { super(); if (typeof filename === 'string' && !filename.endsWith('.mp3')) { filename = filename || ''; throw new Error( `av.Player can only playback mp3. To convert your audio, use: ffmpeg -i ${filename} ${filename.replace(path.extname(filename), '.mp3')} ` ); } const device = '/dev/dsp'; const card = +cp.execSync('aplay --list-devices') .toString().split('\n')[1].match(/(?:\s)(\d)(?:\:)/)[1]; const state = { filename, device: `${device}${card ? card : ''}`, isPlaying: false, interval: null, process: null, time: null, pauseTime: 0, currentTime: 0, }; priv.set(this, state); Object.defineProperties(this, { file: { get: () => state.filename, }, currentTime: { get: () => Number(state.currentTime.toFixed(3)), }, isPlaying: { get: () => state.isPlaying, }, }); } play(file, time) { const state = priv.get(this); let offset = null; let args = []; let hasFile = false; let pushFile = true; if (state.isPlaying || (!state.filename && file == null)) { return this; } if (typeof time === 'undefined') { time = 0; } if (typeof file === 'string') { if (file.endsWith('.mp3')) { state.filename = file; } else { // We'll attempt to accept this as a time code time = file; } /* istanbul ignore else */ if (typeof time !== 'undefined') { if (!/^([0-9]+:){0,2}[0-9]+([.;][0-9]+)?$/.test(time)) { throw new Error( ` The play() method expects time in: hh:mm:ss, ssss, ssss.dddd ` ); } time = toSeconds(time); } } else { if (typeof file === 'number') { time = file; } if (Array.isArray(file)) { // player.play(['file.mp3', '-a', 10, '-r', 2 ]); args = file; time = null; pushFile = false; } else { // Don't need to check for null, since those are handled above if (typeof file === 'object') { // player.play({ // file: 'file.mp3', // a: 10, // r: 2, // }); // args = Object.keys(file).reduce((accum, key) => { const value = file[key]; let option = ''; // When the key is "file", we only want the value if (key === 'file') { hasFile = true; accum.push(value.trim()); } else { if (key.length === 1) { option = '-'; } option += key; accum.push(option); accum.push(value); } return accum; }, []); time = null; pushFile = false; // If no "file" property was provided, nothing to do. if (!hasFile) { return this; } } } } if (pushFile) { Iif (!state.filename) { return this; } args.push(state.filename); } if (state.pauseTime) { time = state.pauseTime; } /* istanbul ignore else */ if (state.process === null) { if (time !== null) { args.push('-s', time || 0); } state.isPlaying = true; state.currentTime = state.pauseTime || 0; state.startTime = Date.now(); state.interval = setInterval(() => { const now = Date.now(); if (offset === null) { offset = now - state.startTime; } state.currentTime = time + (now - state.startTime - offset) / 1000; this.emit('timeupdate', this.currentTime); }, 100); args.push('-o', state.device); state.process = cp.spawn('madplay', args); state.process.stderr.on('data', (data) => { const lines = data.toString().split('\n').filter(Boolean).map(line => line.trim()); lines.forEach(line => { if (line.includes('No such file or directory')) { console.error(line); } }); }); state.process.on('exit', (code, signal) => { if (code !== null && signal === null) { /* istanbul ignore else */ if (state.interval) { clearInterval(state.interval); } state.process = null; state.currentTime = 0; state.startTime = 0; state.isPlaying = false; this.emit('ended'); } }); } this.emit('play'); return this; } stop() { const state = priv.get(this); if (!state.isPlaying) { return this; } state.isPlaying = false; /* istanbul ignore else */ if (state.interval) { clearInterval(state.interval); } /* istanbul ignore else */ if (state.process) { state.process.kill('SIGTERM'); state.process = null; } this.emit('stop'); return this; } pause() { const state = priv.get(this); if (!state.isPlaying) { return this; } state.isPlaying = false; /* istanbul ignore else */ if (state.interval) { clearInterval(state.interval); } state.pauseTime = state.currentTime; /* istanbul ignore else */ if (state.process) { state.process.kill('SIGTERM'); state.process = null; } this.emit('pause'); return this; } } module.exports = Player; |