-
-
Notifications
You must be signed in to change notification settings - Fork 682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ring buffers to AudioWorklet processors to support variable buffer sizes #376
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
49ada2a
Use a ring buffer to allow for a buffer size of 1024 in p5.SoundRecor…
oshoham 515b366
Add ring buffer to p5.SoundFile AudioWorklet processor
oshoham fb5e96f
Add ring buffer to p5.Amplitude AudioWorklet processor
oshoham 226a237
Update build with new ring buffer functionality
oshoham d423aec
Make sure that polyfill-ed worketl nodes never have a smaller buffer …
oshoham 2014fbc
extract buffer size calculation into shared method
oshoham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
* A JS FIFO implementation for the AudioWorklet. 3 assumptions for the | ||
* simpler operation: | ||
* 1. the push and the pull operation are done by 128 frames. (Web Audio | ||
* API's render quantum size in the speficiation) | ||
* 2. the channel count of input/output cannot be changed dynamically. | ||
* The AudioWorkletNode should be configured with the `.channelCount = k` | ||
* (where k is the channel count you want) and | ||
* `.channelCountMode = explicit`. | ||
* 3. This is for the single-thread operation. (obviously) | ||
* | ||
* @class | ||
*/ | ||
class RingBuffer { | ||
/** | ||
* @constructor | ||
* @param {number} length Buffer length in frames. | ||
* @param {number} channelCount Buffer channel count. | ||
*/ | ||
constructor(length, channelCount) { | ||
this._readIndex = 0; | ||
this._writeIndex = 0; | ||
this._framesAvailable = 0; | ||
|
||
this._channelCount = channelCount; | ||
this._length = length; | ||
this._channelData = []; | ||
for (let i = 0; i < this._channelCount; ++i) { | ||
this._channelData[i] = new Float32Array(length); | ||
} | ||
} | ||
|
||
/** | ||
* Getter for Available frames in buffer. | ||
* | ||
* @return {number} Available frames in buffer. | ||
*/ | ||
get framesAvailable() { | ||
return this._framesAvailable; | ||
} | ||
|
||
/** | ||
* Push a sequence of Float32Arrays to buffer. | ||
* | ||
* @param {array} arraySequence A sequence of Float32Arrays. | ||
*/ | ||
push(arraySequence) { | ||
// The channel count of arraySequence and the length of each channel must | ||
// match with this buffer obejct. | ||
|
||
// Transfer data from the |arraySequence| storage to the internal buffer. | ||
let sourceLength = arraySequence[0].length; | ||
for (let i = 0; i < sourceLength; ++i) { | ||
let writeIndex = (this._writeIndex + i) % this._length; | ||
for (let channel = 0; channel < this._channelCount; ++channel) { | ||
this._channelData[channel][writeIndex] = arraySequence[channel][i]; | ||
} | ||
} | ||
|
||
this._writeIndex += sourceLength; | ||
if (this._writeIndex >= this._length) { | ||
this._writeIndex = 0; | ||
} | ||
|
||
// For excessive frames, the buffer will be overwritten. | ||
this._framesAvailable += sourceLength; | ||
if (this._framesAvailable > this._length) { | ||
this._framesAvailable = this._length; | ||
} | ||
} | ||
|
||
/** | ||
* Pull data out of buffer and fill a given sequence of Float32Arrays. | ||
* | ||
* @param {array} arraySequence An array of Float32Arrays. | ||
*/ | ||
pull(arraySequence) { | ||
// The channel count of arraySequence and the length of each channel must | ||
// match with this buffer obejct. | ||
|
||
// If the FIFO is completely empty, do nothing. | ||
if (this._framesAvailable === 0) { | ||
return; | ||
} | ||
|
||
let destinationLength = arraySequence[0].length; | ||
|
||
// Transfer data from the internal buffer to the |arraySequence| storage. | ||
for (let i = 0; i < destinationLength; ++i) { | ||
let readIndex = (this._readIndex + i) % this._length; | ||
for (let channel = 0; channel < this._channelCount; ++channel) { | ||
arraySequence[channel][i] = this._channelData[channel][readIndex]; | ||
} | ||
} | ||
|
||
this._readIndex += destinationLength; | ||
if (this._readIndex >= this._length) { | ||
this._readIndex = 0; | ||
} | ||
|
||
this._framesAvailable -= destinationLength; | ||
if (this._framesAvailable < 0) { | ||
this._framesAvailable = 0; | ||
} | ||
} | ||
} | ||
|
||
// export an object for compatibility with preval.require() | ||
module.exports = { | ||
default: RingBuffer | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the switch to Ring Buffer is causing an issue in Firefox for "/examples/record". It seems that the bufferSize for Firefox ScriptProcessorNode is 4096, regardless of what we pass in here.