|
| 1 | +/** |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + * |
| 16 | + * A JS FIFO implementation for the AudioWorklet. 3 assumptions for the |
| 17 | + * simpler operation: |
| 18 | + * 1. the push and the pull operation are done by 128 frames. (Web Audio |
| 19 | + * API's render quantum size in the speficiation) |
| 20 | + * 2. the channel count of input/output cannot be changed dynamically. |
| 21 | + * The AudioWorkletNode should be configured with the `.channelCount = k` |
| 22 | + * (where k is the channel count you want) and |
| 23 | + * `.channelCountMode = explicit`. |
| 24 | + * 3. This is for the single-thread operation. (obviously) |
| 25 | + * |
| 26 | + * @class |
| 27 | + */ |
| 28 | +class RingBuffer { |
| 29 | + /** |
| 30 | + * @constructor |
| 31 | + * @param {number} length Buffer length in frames. |
| 32 | + * @param {number} channelCount Buffer channel count. |
| 33 | + */ |
| 34 | + constructor(length, channelCount) { |
| 35 | + this._readIndex = 0; |
| 36 | + this._writeIndex = 0; |
| 37 | + this._framesAvailable = 0; |
| 38 | + |
| 39 | + this._channelCount = channelCount; |
| 40 | + this._length = length; |
| 41 | + this._channelData = []; |
| 42 | + for (let i = 0; i < this._channelCount; ++i) { |
| 43 | + this._channelData[i] = new Float32Array(length); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Getter for Available frames in buffer. |
| 49 | + * |
| 50 | + * @return {number} Available frames in buffer. |
| 51 | + */ |
| 52 | + get framesAvailable() { |
| 53 | + return this._framesAvailable; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Push a sequence of Float32Arrays to buffer. |
| 58 | + * |
| 59 | + * @param {array} arraySequence A sequence of Float32Arrays. |
| 60 | + */ |
| 61 | + push(arraySequence) { |
| 62 | + // The channel count of arraySequence and the length of each channel must |
| 63 | + // match with this buffer obejct. |
| 64 | + |
| 65 | + // Transfer data from the |arraySequence| storage to the internal buffer. |
| 66 | + let sourceLength = arraySequence[0].length; |
| 67 | + for (let i = 0; i < sourceLength; ++i) { |
| 68 | + let writeIndex = (this._writeIndex + i) % this._length; |
| 69 | + for (let channel = 0; channel < this._channelCount; ++channel) { |
| 70 | + this._channelData[channel][writeIndex] = arraySequence[channel][i]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + this._writeIndex += sourceLength; |
| 75 | + if (this._writeIndex >= this._length) { |
| 76 | + this._writeIndex = 0; |
| 77 | + } |
| 78 | + |
| 79 | + // For excessive frames, the buffer will be overwritten. |
| 80 | + this._framesAvailable += sourceLength; |
| 81 | + if (this._framesAvailable > this._length) { |
| 82 | + this._framesAvailable = this._length; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Pull data out of buffer and fill a given sequence of Float32Arrays. |
| 88 | + * |
| 89 | + * @param {array} arraySequence An array of Float32Arrays. |
| 90 | + */ |
| 91 | + pull(arraySequence) { |
| 92 | + // The channel count of arraySequence and the length of each channel must |
| 93 | + // match with this buffer obejct. |
| 94 | + |
| 95 | + // If the FIFO is completely empty, do nothing. |
| 96 | + if (this._framesAvailable === 0) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + let destinationLength = arraySequence[0].length; |
| 101 | + |
| 102 | + // Transfer data from the internal buffer to the |arraySequence| storage. |
| 103 | + for (let i = 0; i < destinationLength; ++i) { |
| 104 | + let readIndex = (this._readIndex + i) % this._length; |
| 105 | + for (let channel = 0; channel < this._channelCount; ++channel) { |
| 106 | + arraySequence[channel][i] = this._channelData[channel][readIndex]; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + this._readIndex += destinationLength; |
| 111 | + if (this._readIndex >= this._length) { |
| 112 | + this._readIndex = 0; |
| 113 | + } |
| 114 | + |
| 115 | + this._framesAvailable -= destinationLength; |
| 116 | + if (this._framesAvailable < 0) { |
| 117 | + this._framesAvailable = 0; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// export an object for compatibility with preval.require() |
| 123 | +module.exports = { |
| 124 | + default: RingBuffer |
| 125 | +}; |
0 commit comments