diff --git a/classes/AudioBuffer.js b/classes/AudioBuffer.js index a61cdc6..ac227e5 100644 --- a/classes/AudioBuffer.js +++ b/classes/AudioBuffer.js @@ -14,8 +14,9 @@ class StreamBuffer extends EventEmitter { paused: false, hiccups: 0 }; + this.audiosettings = settings?.audio; this.#setupBuffer(settings?.threshold); - this.#setupStreams(stream, settings?.audio); + this.#setupStreams(stream); } play() { @@ -62,25 +63,32 @@ class StreamBuffer extends EventEmitter { } } - #setupStreams(stream, audioSettings) { + #setupStreams(stream) { if (stream === undefined) { return; } this.streams = { input: stream, - output: new NodeSpeaker({ - channels: audioSettings?.channels || 2, - bitDepth: audioSettings?.bitDepth || 16, - sampleRate: audioSettings?.sampleRate || 44100, - }), buffer: new Readable() }; this.streams.buffer._read = () => { }; + this.#createOutputStream(); this.#handleBufferStream(); this.#handleInputStream(); this.#handleOutputStream(); } + #createOutputStream() { + if (this.streams.output !== undefined) { + this.streams.output.destroy(); + } + this.streams.output = new NodeSpeaker({ + channels: audioSettings?.channels || 2, + bitDepth: audioSettings?.bitDepth || 16, + sampleRate: audioSettings?.sampleRate || 44100, + }); + } + #isThresholdReached() { return this.streams.buffer.readableLength >= this.threshold.value; } @@ -185,6 +193,7 @@ class StreamBuffer extends EventEmitter { } this.streams.output.on('error', (error) => { logger.error('output stream encountered an error: ' + error); + this.#createOutputStream(); }); this.streams.output.on('written', (bytes) => { this.#writeBufferedChunk(bytes);