Skip to content

Commit

Permalink
better streaming buffer re-use to reduce peak memory use
Browse files Browse the repository at this point in the history
  • Loading branch information
phoddie committed Apr 18, 2023
1 parent 19cb24e commit 2e27c75
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 7 additions & 4 deletions examples/pins/audioout/http-stream/sbcstreamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SBCStreamer {
#stream;
#next;
#playing = [];
#free = [];
#pending = [];
#ready; // undefined while initializing, false if not buffered / playing, true if buffers full to play / playing
#header;
Expand Down Expand Up @@ -116,9 +117,9 @@ class SBCStreamer {
this.#bytesQueued -= bytes;
let played = this.#playing.shift();
this.#callbacks.onPlayed?.(played);
if (!this.#next && (played.byteLength === (kMAUDHeader + this.#bytesPerBlock))) {
if (played.byteLength === (kMAUDHeader + this.#bytesPerBlock)) {
played.position = kMAUDHeader;
this.#next = played;
this.#free.push(played);
}
played = undefined;

Expand All @@ -135,7 +136,7 @@ class SBCStreamer {
this.#audio.callbacks[this.#stream] = null;

this.#http.close();
this.#http = this.#audio = this.#playing = this.#pending = undefined;
this.#http = this.#audio = this.#playing = this.#pending = this.#free = undefined;
}

#fillQueue() {
Expand All @@ -144,7 +145,9 @@ class SBCStreamer {
(this.#audio.length(this.#stream) >= 2)) {
let next = this.#next;
if (!next) {
this.#next = next = new Uint8Array(new SharedArrayBuffer(kMAUDHeader + this.#bytesPerBlock));
this.#next = next = this.#free.shift();
if (!next)
this.#next = next = new Uint8Array(new SharedArrayBuffer(kMAUDHeader + this.#bytesPerBlock));
next.position = kMAUDHeader;
}

Expand Down
11 changes: 7 additions & 4 deletions examples/pins/audioout/http-stream/wavstreamer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class WavStreamer {
#http;
#request;
#playing = [];
#free = [];
#ready; // undefined while initializing, false if not buffered / playing, true if buffers full to play / playing
#next;
#bytes = Infinity; // remaining in stream
Expand Down Expand Up @@ -179,9 +180,9 @@ class WavStreamer {
this.#bytesQueued -= bytes;
let played = this.#playing.shift();
this.#callbacks.onPlayed?.(played);
if (!this.#next && (played.byteLength === (this.#bytesPerBlock * this.#channels))) {
if (played.byteLength === (this.#bytesPerBlock * this.#channels)) {
played.position = 0;
this.#next = played;
this.#free.push(played);
}
played = undefined;

Expand All @@ -198,15 +199,17 @@ class WavStreamer {
this.#audio.callbacks[this.#stream] = null;

this.#http?.close();
this.#http = this.#audio = this.#playing = this.#pending = undefined;
this.#http = this.#audio = this.#playing = this.#pending = this.#free = undefined;
}
#fillQueue() {
while ((this.#bytesQueued < this.#targetBytesQueued) &&
this.#request.readable &&
(this.#audio.length(this.#stream) >= 2)) {
let next = this.#next;
if (!next) {
this.#next = next = new Uint8Array(new SharedArrayBuffer(this.#bytesPerBlock * this.#channels));
this.#next = next = this.#free.shift();
if (!next)
this.#next = next = new Uint8Array(new SharedArrayBuffer(this.#bytesPerBlock * this.#channels));
next.position = 0;
}

Expand Down

0 comments on commit 2e27c75

Please sign in to comment.