P4
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { EventEmitter } from 'node:events';
|
||||
|
||||
export class PipeWirePlayback extends EventEmitter {
|
||||
constructor({ command = 'pw-cat', spawnImpl = spawn, sampleRate = 44_100, nodeName = 'Jarvis' } = {}) {
|
||||
super(); this.command = command; this.spawnImpl = spawnImpl; this.sampleRate = sampleRate; this.nodeName = nodeName; this.process = null;
|
||||
}
|
||||
async play(samples) {
|
||||
this.stop();
|
||||
const child = this.process = this.spawnImpl(this.command, ['--playback', '--raw', '--format', 's16', '--rate', String(this.sampleRate), '--channels', '1', '--name', this.nodeName], { stdio: ['pipe', 'ignore', 'pipe'] });
|
||||
child.stderr?.on('data', (chunk) => this.emit('diagnostic', String(chunk).trim()));
|
||||
child.on('error', (error) => this.emit('error', error));
|
||||
child.stdin.end(Buffer.from(samples.buffer, samples.byteOffset, samples.byteLength));
|
||||
await new Promise((resolve, reject) => { child.once('close', resolve); child.once('error', reject); });
|
||||
if (this.process === child) this.process = null;
|
||||
}
|
||||
stop() { if (this.process) { this.process.kill('SIGTERM'); this.process = null; } }
|
||||
}
|
||||
Reference in New Issue
Block a user