Skip to content
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

Consolidating the existing RingBuffer Implementation. #369

Merged
merged 5 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ module.exports = function(eleventyConfig) {
'src/robots.txt',
'src/README.md',
'src/sitemap.xml',
'src/lib/**/*.js',
].map(path => eleventyConfig.addPassthroughCopy(path));

// eleventyConfig.addPassthroughCopy('src/favicon');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import Module from './variable-buffer-kernel.wasmmodule.js';
import {HeapAudioBuffer, RingBuffer} from '../lib/wasm-audio-helper.js';
import { FreeQueue } from '../../../lib/free-queue/free-queue.js';

/**
* An example of AudioWorkletProcessor that uses RingBuffer inside. If your
Expand All @@ -40,17 +40,15 @@ class RingBufferWorkletProcessor extends AudioWorkletProcessor {
this._kernelBufferSize = options.processorOptions.kernelBufferSize;
this._channelCount = options.processorOptions.channelCount;

// RingBuffers for input and output.
this._inputRingBuffer =
new RingBuffer(this._kernelBufferSize, this._channelCount);
this._outputRingBuffer =
new RingBuffer(this._kernelBufferSize, this._channelCount);
this._inputAudioBuffer =
new FreeQueue(Module, this._kernelBufferSize, this._channelCount);
this._outputAudioBuffer =
new FreeQueue(Module, this._kernelBufferSize, this._channelCount);

// For WASM memory, also for input and output.
this._heapInputBuffer =
new HeapAudioBuffer(Module, this._kernelBufferSize, this._channelCount);
new FreeQueue(Module, this._kernelBufferSize, this._channelCount);
this._heapOutputBuffer =
new HeapAudioBuffer(Module, this._kernelBufferSize, this._channelCount);
new FreeQueue(Module, this._kernelBufferSize, this._channelCount);

// WASM audio processing kernel.
this._kernel = new Module.VariableBufferKernel(this._kernelBufferSize);
Expand All @@ -72,29 +70,29 @@ class RingBufferWorkletProcessor extends AudioWorkletProcessor {

// AudioWorkletProcessor always gets 128 frames in and 128 frames out. Here
// we push 128 frames into the ring buffer.
this._inputRingBuffer.push(input);
this._inputAudioBuffer.push(input);

// Process only if we have enough frames for the kernel.
if (this._inputRingBuffer.framesAvailable >= this._kernelBufferSize) {
if (this._inputAudioBuffer.framesAvailable >= this._kernelBufferSize) {
// Get the queued data from the input ring buffer.
this._inputRingBuffer.pull(this._heapInputBuffer.getChannelData());
this._inputAudioBuffer.pull(this._inputAudioBuffer.getChannelData());

// This WASM process function can be replaced with ScriptProcessor's
// |onaudioprocess| callback funciton. However, if the event handler
// touches DOM in the main scope, it needs to be translated with the
// async messaging via MessagePort.
this._kernel.process(
this._heapInputBuffer.getHeapAddress(),
this._heapOutputBuffer.getHeapAddress(),
this._channelCount);
this._inputAudioBuffer.getHeapAddress(),
this._outputAudioBuffer.getHeapAddress(),
this._channelCount);

// Fill the output ring buffer with the processed data.
this._outputRingBuffer.push(this._heapOutputBuffer.getChannelData());
this._outputAudioBuffer.push(this._outputAudioBuffer.getChannelData());
}

// Always pull 128 frames out. If the ring buffer does not have enough
// frames, the output will be silent.
this._outputRingBuffer.pull(output);
this._outputAudioBuffer.pull(output);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.

import Module from './synth.wasm.js';
import {HeapAudioBuffer} from '../lib/wasm-audio-helper.js';
import { FreeQueue } from '../../../lib/free-queue/free-queue.js';

/* global sampleRate */

Expand All @@ -16,7 +16,7 @@ class SynthProcessor extends AudioWorkletProcessor {
// Create an instance of Synthesizer and WASM memory helper. Then set up an
// event handler for MIDI data from the main thread.
this._synth = new Module.Synthesizer(sampleRate);
this._wasmHeapBuffer = new HeapAudioBuffer(Module, NUM_FRAMES, 1, 1);
this._wasmHeapBuffer = new FreeQueue(Module, NUM_FRAMES, 1, 1);
this.port.onmessage = this._playTone.bind(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// found in the LICENSE file.

import Module from './simple-kernel.wasmmodule.js';
import {RENDER_QUANTUM_FRAMES, MAX_CHANNEL_COUNT, HeapAudioBuffer}
from '../lib/wasm-audio-helper.js';
import {RENDER_QUANTUM_FRAMES, MAX_CHANNEL_COUNT, FreeQueue}
from '../../../lib/free-queue/free-queue.js';

/**
* A simple demonstration of WASM-powered AudioWorkletProcessor.
Expand All @@ -21,9 +21,9 @@ class WASMWorkletProcessor extends AudioWorkletProcessor {

// Allocate the buffer for the heap access. Start with stereo, but it can
// be expanded up to 32 channels.
this._heapInputBuffer = new HeapAudioBuffer(
this._heapInputBuffer = new FreeQueue(
Module, RENDER_QUANTUM_FRAMES, 2, MAX_CHANNEL_COUNT);
this._heapOutputBuffer = new HeapAudioBuffer(
this._heapOutputBuffer = new FreeQueue(
Module, RENDER_QUANTUM_FRAMES, 2, MAX_CHANNEL_COUNT);
this._kernel = new Module.SimpleKernel();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FreeQueue from "../../src/free-queue.js";
import { FreeQueueSAB } from "../../../../lib/free-queue/free-queue-sab.js";
import { FRAME_SIZE, RENDER_QUANTUM } from "./constants.js";

/**
Expand All @@ -20,8 +20,8 @@ class BasicProcessor extends AudioWorkletProcessor {
this.inputQueue = options.processorOptions.inputQueue;
this.outputQueue = options.processorOptions.outputQueue;
this.atomicState = options.processorOptions.atomicState;
Object.setPrototypeOf(this.inputQueue, FreeQueue.prototype);
Object.setPrototypeOf(this.outputQueue, FreeQueue.prototype);
Object.setPrototypeOf(this.inputQueue, FreeQueueSAB.prototype);
Object.setPrototypeOf(this.outputQueue, FreeQueueSAB.prototype);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import FreeQueue from '../../src/free-queue.js'
import { FreeQueueSAB } from '../../../../lib/free-queue/free-queue-sab.js'
import { QUEUE_SIZE } from './constants.js';

const toggleButton = document.getElementById('toggle');
toggleButton.disabled = false;

// Create 2 FreeQueue instances with 4096 buffer length and 1 channel.
const inputQueue = new FreeQueue(QUEUE_SIZE, 1);
const outputQueue = new FreeQueue(QUEUE_SIZE, 1);
const inputQueue = new FreeQueueSAB(QUEUE_SIZE, 1);
const outputQueue = new FreeQueueSAB(QUEUE_SIZE, 1);
// Create an atomic state for synchronization between worker and AudioWorklet.
const atomicState = new Int32Array(
new SharedArrayBuffer(1 * Int32Array.BYTES_PER_ELEMENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FreeQueue from "../../src/free-queue.js";
import { FreeQueueSAB } from "../../../../lib/free-queue/free-queue-sab.js";
import { FRAME_SIZE } from "./constants.js";

/**
Expand All @@ -9,8 +9,8 @@ import { FRAME_SIZE } from "./constants.js";
self.onmessage = (msg) => {
if (msg.data.type === "init") {
let { inputQueue, outputQueue, atomicState } = msg.data.data;
Object.setPrototypeOf(inputQueue, FreeQueue.prototype);
Object.setPrototypeOf(outputQueue, FreeQueue.prototype);
Object.setPrototypeOf(inputQueue, FreeQueueSAB.prototype);
Object.setPrototypeOf(outputQueue, FreeQueueSAB.prototype);

// buffer for storing data pulled out from queue.
const input = new Float32Array(FRAME_SIZE);
Expand Down
1 change: 1 addition & 0 deletions src/lib/free-queue/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Work In Progress ....
Loading
Loading