|
| 1 | +import { debug } from './common'; |
| 2 | + |
| 3 | +type Command = { |
| 4 | + cmd: string; |
| 5 | + argsWithPath: (path: string) => string[]; |
| 6 | + argsWithStdin: string[] | null; |
| 7 | +}; |
| 8 | + |
| 9 | +let defaultAudioPlayer: Command | null | undefined = undefined; |
| 10 | +const findDefaultAudioPlayer = (): Command | null => { |
| 11 | + if (defaultAudioPlayer === undefined) { |
| 12 | + defaultAudioPlayer = findDefaultAudioPlayer_(); |
| 13 | + } |
| 14 | + return defaultAudioPlayer; |
| 15 | +}; |
| 16 | +const findDefaultAudioPlayer_ = (): Command | null => { |
| 17 | + const isWindows = process.platform === 'win32'; |
| 18 | + |
| 19 | + const atEnd = |
| 20 | + (...arr: string[]) => |
| 21 | + (path: string) => [...arr, path]; |
| 22 | + |
| 23 | + // Ordered by preference |
| 24 | + const commonPlayers: Command[] = isWindows |
| 25 | + ? [ |
| 26 | + { |
| 27 | + cmd: 'powershell', |
| 28 | + argsWithPath: (path) => ['-c', `"(New-Object Media.SoundPlayer '${path}').PlaySync()"`], |
| 29 | + argsWithStdin: null, |
| 30 | + }, |
| 31 | + { |
| 32 | + cmd: 'ffplay', |
| 33 | + argsWithPath: atEnd('-nodisp', '-autoexit'), |
| 34 | + argsWithStdin: ['-nodisp', '-autoexit', '-i', '-'], |
| 35 | + }, |
| 36 | + { cmd: 'mpv', argsWithPath: atEnd('--no-video'), argsWithStdin: ['--no-video', '-'] }, |
| 37 | + { cmd: 'mplayer', argsWithPath: atEnd(''), argsWithStdin: ['-'] }, |
| 38 | + ] |
| 39 | + : [ |
| 40 | + { |
| 41 | + cmd: 'ffplay', |
| 42 | + argsWithPath: atEnd('-nodisp', '-autoexit'), |
| 43 | + argsWithStdin: ['-nodisp', '-autoexit', '-i', '-'], |
| 44 | + }, |
| 45 | + { cmd: 'afplay', argsWithPath: atEnd(''), argsWithStdin: null }, |
| 46 | + { cmd: 'mplayer', argsWithPath: atEnd(''), argsWithStdin: ['-'] }, |
| 47 | + { cmd: 'mpv', argsWithPath: atEnd('--no-video'), argsWithStdin: ['--no-video', '-'] }, |
| 48 | + { cmd: 'aplay', argsWithPath: atEnd(''), argsWithStdin: ['-'] }, |
| 49 | + { cmd: 'play', argsWithPath: atEnd(''), argsWithStdin: ['-'] }, |
| 50 | + ]; |
| 51 | + |
| 52 | + for (const player of commonPlayers) { |
| 53 | + const checkCmd = isWindows ? 'where' : 'which'; |
| 54 | + try { |
| 55 | + Bun.spawnSync([checkCmd, player.cmd]); |
| 56 | + return player; // found! |
| 57 | + } catch {} |
| 58 | + } |
| 59 | + |
| 60 | + return null; |
| 61 | +}; |
| 62 | + |
| 63 | +export const playAudioFile = async ( |
| 64 | + path: string, |
| 65 | + customCommand: string | null |
| 66 | +): Promise<unknown> => { |
| 67 | + const command = ensureAudioPlayer( |
| 68 | + customCommand ? parseCustomCommand(customCommand) : findDefaultAudioPlayer() |
| 69 | + ); |
| 70 | + const isWindows = process.platform === 'win32'; |
| 71 | + const sanitizedPath = isWindows ? path.replace(/\\/g, '\\\\') : path; |
| 72 | + |
| 73 | + return Bun.spawn([command.cmd, ...command.argsWithPath(sanitizedPath)], { |
| 74 | + stdout: 'ignore', |
| 75 | + stderr: 'ignore', |
| 76 | + }).exited; |
| 77 | +}; |
| 78 | + |
| 79 | +export const parseCustomCommand = (command: string): Command => { |
| 80 | + const [cmd, ...args] = command.split(' '); |
| 81 | + const argsWithPath = (path: string) => args.map((arg) => arg.replace('$AUDIO_FILE', path)); |
| 82 | + const argsWithStdin = args.some((arg) => arg.includes('$AUDIO_FILE')) ? argsWithPath('-') : args; |
| 83 | + |
| 84 | + return { |
| 85 | + cmd, |
| 86 | + argsWithPath, |
| 87 | + argsWithStdin, |
| 88 | + }; |
| 89 | +}; |
| 90 | + |
| 91 | +const ensureAudioPlayer = (command: Command | null): Command => { |
| 92 | + if (!command) { |
| 93 | + throw new Error( |
| 94 | + 'No audio player found. Please install ffplay or specify a custom player with --play-command' |
| 95 | + ); |
| 96 | + } |
| 97 | + return command; |
| 98 | +}; |
| 99 | + |
| 100 | +const ensureStdinSupport = (command: Command): Command & { argsWithStdin: string[] } => { |
| 101 | + const { argsWithStdin } = command; |
| 102 | + if (!argsWithStdin) { |
| 103 | + throw new Error( |
| 104 | + `The audio player does not support playing from stdin. Please specify a custom player with --play-command` |
| 105 | + ); |
| 106 | + } |
| 107 | + return { ...command, argsWithStdin }; |
| 108 | +}; |
| 109 | + |
| 110 | +export const withStdinAudioPlayer = async ( |
| 111 | + customCommand: string | null, |
| 112 | + f: (writeAudio: (audioBuffer: Buffer) => void) => Promise<void> |
| 113 | +): Promise<void> => { |
| 114 | + const command = ensureStdinSupport( |
| 115 | + ensureAudioPlayer(customCommand ? parseCustomCommand(customCommand) : findDefaultAudioPlayer()) |
| 116 | + ); |
| 117 | + |
| 118 | + debug([command.cmd, command.argsWithStdin]); |
| 119 | + const proc = Bun.spawn([command.cmd, ...command.argsWithStdin], { |
| 120 | + stdout: 'ignore', |
| 121 | + stderr: 'ignore', |
| 122 | + stdin: 'pipe', |
| 123 | + }); |
| 124 | + |
| 125 | + await f((audioBuffer: Buffer) => { |
| 126 | + proc.stdin.write(audioBuffer); |
| 127 | + }); |
| 128 | + proc.stdin.end(); |
| 129 | + await proc.exited; |
| 130 | +}; |
0 commit comments