This commit is contained in:
2026-09-11 14:09:04 -04:00
parent 68d7a40605
commit 5c6c94713a
16 changed files with 539 additions and 16 deletions
+18
View File
@@ -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; } }
}