From 43f1a1dd1b44065663a797603012394c52a9baea Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 13 May 2023 13:31:58 -0300 Subject: use ESM module syntax & update tui-lib Exciting update! This doesn't make any substantial changes exactly but does update the most quickly-archaic parts of older Node code. --- players.js | 56 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) (limited to 'players.js') diff --git a/players.js b/players.js index 1d64061..959bf27 100644 --- a/players.js +++ b/players.js @@ -1,21 +1,22 @@ // stolen from http-music -const { +import { commandExists, killProcess, getTimeStrings, - getTimeStringsFromSec -} = require('./general-util') + getTimeStringsFromSec, +} from './general-util.js' -const { spawn } = require('child_process') -const EventEmitter = require('events') -const Socat = require('./socat') -const fs = require('fs') -const util = require('util') +import {spawn} from 'node:child_process' +import {statSync} from 'node:fs' +import {unlink} from 'node:fs/promises' +import EventEmitter from 'node:events' +import path from 'node:path' +import url from 'node:url' -const unlink = util.promisify(fs.unlink) +import Socat from './socat.js' -class Player extends EventEmitter { +export class Player extends EventEmitter { constructor(processOptions = []) { super() @@ -43,14 +44,14 @@ class Player extends EventEmitter { return this._process } - playFile(file, startTime) {} - seekAhead(secs) {} - seekBack(secs) {} - seekTo(timeInSecs) {} + playFile(_file, _startTime) {} + seekAhead(_secs) {} + seekBack(_secs) {} + seekTo(_timeInSecs) {} seekToStart() {} - volUp(amount) {} - volDown(amount) {} - setVolume(value) {} + volUp(_amount) {} + volDown(_amount) {} + setVolume(_value) {} updateVolume() {} togglePause() {} toggleLoop() {} @@ -93,7 +94,7 @@ class Player extends EventEmitter { } } -module.exports.MPVPlayer = class extends Player { +export class MPVPlayer extends Player { // 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. @@ -172,7 +173,7 @@ module.exports.MPVPlayer = class extends Player { } } -module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { +export class ControllableMPVPlayer extends MPVPlayer { getMPVOptions(...args) { return ['--input-ipc-server=' + this.socat.path, ...super.getMPVOptions(...args)] } @@ -181,8 +182,7 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { this.removeSocket(this.socketPath) do { - // this.socketPathpath = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) - this.socketPath = __dirname + '/mtui-socket-' + Math.floor(Math.random() * 10000) + this.socketPath = path.join(path.dirname(url.fileURLToPath(import.meta.url)), 'mtui-socket-' + Math.floor(Math.random() * 10000)) } while (this.existsSync(this.socketPath)) this.socat = new Socat(this.socketPath) @@ -196,7 +196,7 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { existsSync(path) { try { - fs.statSync(path) + statSync(path) return true } catch (error) { return false @@ -289,7 +289,7 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { } } -module.exports.SoXPlayer = class extends Player { +export class SoXPlayer extends Player { playFile(file, startTime) { // 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). @@ -315,14 +315,12 @@ module.exports.SoXPlayer = class extends Player { return } - const timeRegex = '([0-9]*):([0-9]*):([0-9]*)\.([0-9]*)' + const timeRegex = String.raw`([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). @@ -385,15 +383,15 @@ module.exports.SoXPlayer = class extends Player { } } -module.exports.getPlayer = async function(name = null, options = []) { +export async function getPlayer(name = null, options = []) { if (await commandExists('mpv') && (name === null || name === 'mpv')) { - return new module.exports.ControllableMPVPlayer(options) + return new ControllableMPVPlayer(options) } else if (name === 'mpv') { return null } if (await commandExists('play') && (name === null || name === 'sox')) { - return new module.exports.SoXPlayer(options) + return new SoXPlayer(options) } else if (name === 'sox') { return null } -- cgit 1.3.0-6-gf8a5