Skip to content

Commit

Permalink
Merge pull request #352 from Daksh-10/Fix-#347
Browse files Browse the repository at this point in the history
Fix: #347 : Improve RingBuffer
  • Loading branch information
mjwilson-google authored Mar 7, 2024
2 parents b3b0851 + d630cf2 commit 0643084
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/audio-worklet/design-pattern/lib/wasm-audio-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,12 @@ class RingBuffer {
// Transfer data from the |arraySequence| storage to the internal buffer.
const sourceLength = arraySequence[0].length;
for (let i = 0; i < sourceLength; ++i) {
const writeIndex = (this._writeIndex + i) % this._length;
for (let channel = 0; channel < this._channelCount; ++channel) {
this._channelData[channel][writeIndex] = arraySequence[channel][i];
this._channelData[channel][this._writeIndex] = arraySequence[channel][i];
}
this._writeIndex = (this._writeIndex + 1) % this._length;
}

this._writeIndex = (this._writeIndex + sourceLength) % this._length;

// For excessive frames, the buffer will be overwritten.
this._framesAvailable += sourceLength;
if (this._framesAvailable > this._length) {
Expand All @@ -249,14 +247,12 @@ class RingBuffer {

// Transfer data from the internal buffer to the |arraySequence| storage.
for (let i = 0; i < destinationLength; ++i) {
const readIndex = (this._readIndex + i) % this._length;
for (let channel = 0; channel < this._channelCount; ++channel) {
arraySequence[channel][i] = this._channelData[channel][readIndex];
arraySequence[channel][i] = this._channelData[channel][this._readIndex];
}
this._readIndex = (this._readIndex + 1) % this._length;
}

this._readIndex = (this._readIndex + destinationLength) % this._length;

this._framesAvailable -= destinationLength;
if (this._framesAvailable < 0) {
this._framesAvailable = 0;
Expand Down

0 comments on commit 0643084

Please sign in to comment.