From 00170f2ef6e0771e5ccb3346f1dd9f8aac6734a2 Mon Sep 17 00:00:00 2001 From: Daksh-10 Date: Sun, 25 Feb 2024 17:07:29 +0530 Subject: [PATCH 1/4] updating index after every iteration for _readIndex and _writeIndex in class RingBuffer --- .../design-pattern/lib/wasm-audio-helper.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js index 1ea76b5fd..dc1dcacf2 100644 --- a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js +++ b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js @@ -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; + this._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 + sourceLength) % this._length; - // For excessive frames, the buffer will be overwritten. this._framesAvailable += sourceLength; if (this._framesAvailable > this._length) { @@ -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; + this._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 + destinationLength) % this._length; - this._framesAvailable -= destinationLength; if (this._framesAvailable < 0) { this._framesAvailable = 0; From ee2ca66160b3d28a9d6e40823ce7ae3c20c57962 Mon Sep 17 00:00:00 2001 From: Daksh Date: Sun, 3 Mar 2024 17:42:12 +0530 Subject: [PATCH 2/4] Addressed the comments regarding direct increment of index by 1 --- src/audio-worklet/design-pattern/lib/wasm-audio-helper.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js index dc1dcacf2..08f3c7e0d 100644 --- a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js +++ b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js @@ -216,7 +216,8 @@ class RingBuffer { // Transfer data from the |arraySequence| storage to the internal buffer. const sourceLength = arraySequence[0].length; for (let i = 0; i < sourceLength; ++i) { - this._writeIndex = (this._writeIndex + i) % this._length; + //updating index directly instead of making new temporary every iteration + this._writeIndex = (this._writeIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { this._channelData[channel][this._writeIndex] = arraySequence[channel][i]; } @@ -247,7 +248,8 @@ class RingBuffer { // Transfer data from the internal buffer to the |arraySequence| storage. for (let i = 0; i < destinationLength; ++i) { - this._readIndex = (this._readIndex + i) % this._length; + //updating index directly instead of making new temporary every iteration + this._readIndex = (this._readIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { arraySequence[channel][i] = this._channelData[channel][this._readIndex]; } From 4d3cb4160cfef4f5e113f90eec9cdff6e0772e4b Mon Sep 17 00:00:00 2001 From: Daksh-10 Date: Thu, 7 Mar 2024 14:46:30 +0530 Subject: [PATCH 3/4] removed the comments --- src/audio-worklet/design-pattern/lib/wasm-audio-helper.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js index 08f3c7e0d..a089f2be5 100644 --- a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js +++ b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js @@ -216,7 +216,6 @@ class RingBuffer { // Transfer data from the |arraySequence| storage to the internal buffer. const sourceLength = arraySequence[0].length; for (let i = 0; i < sourceLength; ++i) { - //updating index directly instead of making new temporary every iteration this._writeIndex = (this._writeIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { this._channelData[channel][this._writeIndex] = arraySequence[channel][i]; @@ -248,7 +247,6 @@ class RingBuffer { // Transfer data from the internal buffer to the |arraySequence| storage. for (let i = 0; i < destinationLength; ++i) { - //updating index directly instead of making new temporary every iteration this._readIndex = (this._readIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { arraySequence[channel][i] = this._channelData[channel][this._readIndex]; From d630cf2c3d4390b189f952b806390360bd2b342e Mon Sep 17 00:00:00 2001 From: Daksh Date: Thu, 7 Mar 2024 19:29:50 +0530 Subject: [PATCH 4/4] incrementing index after using them --- src/audio-worklet/design-pattern/lib/wasm-audio-helper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js index a089f2be5..87b2f5e2b 100644 --- a/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js +++ b/src/audio-worklet/design-pattern/lib/wasm-audio-helper.js @@ -216,10 +216,10 @@ class RingBuffer { // Transfer data from the |arraySequence| storage to the internal buffer. const sourceLength = arraySequence[0].length; for (let i = 0; i < sourceLength; ++i) { - this._writeIndex = (this._writeIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { this._channelData[channel][this._writeIndex] = arraySequence[channel][i]; } + this._writeIndex = (this._writeIndex + 1) % this._length; } // For excessive frames, the buffer will be overwritten. @@ -247,10 +247,10 @@ class RingBuffer { // Transfer data from the internal buffer to the |arraySequence| storage. for (let i = 0; i < destinationLength; ++i) { - this._readIndex = (this._readIndex + 1) % this._length; for (let channel = 0; channel < this._channelCount; ++channel) { arraySequence[channel][i] = this._channelData[channel][this._readIndex]; } + this._readIndex = (this._readIndex + 1) % this._length; } this._framesAvailable -= destinationLength;