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

Fix audio worklet import #143

Merged
merged 9 commits into from
Dec 23, 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
4 changes: 4 additions & 0 deletions .scripts/build-linux-gnu-docker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ console.log(`> executing docker command in dir ${cwd}`);
// the source code directory is shared with the docker image,
// so every thing is always up to date

// @todo - rebuild docket image
// cd ./.scripts/docker_x86_64-unknown-linux-gnu/
// docker build -t bbmmaa/build-x86_64 .

execSync(`docker run --rm \
-v ~/.cargo/git:/root/.cargo/git \
-v ~/.cargo/registry:/root/.cargo/registry \
Expand Down
20 changes: 20 additions & 0 deletions examples/audio-worklet-online.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AudioContext, OscillatorNode, GainNode, AudioWorkletNode } from '../index.mjs';

// load audio worklet from online source

const plugin = 'https://googlechromelabs.github.io/web-audio-samples/audio-worklet/basic/noise-generator/noise-generator.js';
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule(plugin);

const modulatorNode = new OscillatorNode(audioContext);
const modGainNode = new GainNode(audioContext);
const noiseGeneratorNode = new AudioWorkletNode(audioContext, 'noise-generator');
noiseGeneratorNode.connect(audioContext.destination);

// Connect the oscillator to 'amplitude' AudioParam.
const paramAmp = noiseGeneratorNode.parameters.get('amplitude');
modulatorNode.connect(modGainNode).connect(paramAmp);

modulatorNode.frequency.value = 0.5;
modGainNode.gain.value = 0.75;
modulatorNode.start();
16 changes: 16 additions & 0 deletions examples/audio-worklet-webassembly.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// https://github.com/GoogleChromeLabs/web-audio-samples/tree/main/src/audio-worklet/design-pattern/wasm
//
// Copyright (c) 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import { AudioContext, OscillatorNode, AudioWorkletNode } from '../index.mjs';

const audioContext = new AudioContext();

await audioContext.audioWorklet.addModule('./worklets/wasm-worklet-processor.mjs');
const oscillator = new OscillatorNode(audioContext);
const bypasser = new AudioWorkletNode(audioContext, 'wasm-worklet-processor');
oscillator.connect(bypasser).connect(audioContext.destination);
oscillator.start();

15 changes: 15 additions & 0 deletions examples/worklets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DEPS = SimpleKernel.cc

build: $(DEPS)
@emcc --bind -O1 \
-s WASM=1 \
-s BINARYEN_ASYNC_COMPILATION=0 \
-s SINGLE_FILE=1 \
-s ENVIRONMENT=node \
-s EXPORT_ES6=1 \
-s EXPORTED_FUNCTIONS="['_malloc']" \
SimpleKernel.cc \
-o simple-kernel.wasmmodule.mjs

clean:
@rm -f simple-kernel.wasmmodule.js
65 changes: 65 additions & 0 deletions examples/worklets/SimpleKernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// adapted from https://github.com/GoogleChromeLabs/web-audio-samples/tree/main/src/audio-worklet/design-pattern/wasm

/**
* 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.
*/

#include "emscripten/bind.h"

using namespace emscripten;

const unsigned kRenderQuantumFrames = 128;
const unsigned kBytesPerChannel = kRenderQuantumFrames * sizeof(float);

// The "kernel" is an object that processes a audio stream, which contains
// one or more channels. It is supposed to obtain the frame data from an
// |input|, process and fill an |output| of the AudioWorkletProcessor.
//
// AudioWorkletProcessor Input(multi-channel, 128-frames)
// |
// V
// Kernel
// |
// V
// AudioWorkletProcessor Output(multi-channel, 128-frames)
//
// In this implementation, the kernel operates based on 128-frames, which is
// the render quantum size of Web Audio API.
class SimpleKernel {
public:
SimpleKernel() {}

void Process(uintptr_t input_ptr, uintptr_t output_ptr,
unsigned channel_count) {
float* input_buffer = reinterpret_cast<float*>(input_ptr);
float* output_buffer = reinterpret_cast<float*>(output_ptr);

// Bypasses the data. By design, the channel count will always be the same
// for |input_buffer| and |output_buffer|.
for (unsigned channel = 0; channel < channel_count; ++channel) {
float* destination = output_buffer + channel * kRenderQuantumFrames;
float* source = input_buffer + channel * kRenderQuantumFrames;
memcpy(destination, source, kBytesPerChannel);
}
}
};

EMSCRIPTEN_BINDINGS(CLASS_SimpleKernel) {
class_<SimpleKernel>("SimpleKernel")
.constructor()
.function("process",
&SimpleKernel::Process,
allow_raw_pointers());
}
254 changes: 254 additions & 0 deletions examples/worklets/free-queue.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
// ported from https://github.com/GoogleChromeLabs/web-audio-samples/tree/main/src/audio-worklet/design-pattern/wasm

/**
* 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.
*/

// Byte per audio sample. (32 bit float)
const BYTES_PER_SAMPLE = Float32Array.BYTES_PER_ELEMENT;

// Basic byte unit of WASM heap. (16 bit = 2 bytes)
const BYTES_PER_UNIT = Uint16Array.BYTES_PER_ELEMENT;

// The max audio channel on Chrome is 32.
const MAX_CHANNEL_COUNT = 32;

// WebAudio's render quantum size.
const RENDER_QUANTUM_FRAMES = 128;


/**
* A WASM HEAP wrapper for AudioBuffer class. This breaks down the AudioBuffer
* into an Array of Float32Array for the convinient WASM opearion.
*
* @class
* @dependency Module A WASM module generated by the emscripten glue code.
*/
class FreeQueue {
/**
* @constructor
* @param {object} wasmModule WASM module generated by Emscripten.
* @param {number} length Buffer frame length.
* @param {number} channelCount Number of channels.
* @param {number=} maxChannelCount Maximum number of channels.
*/
constructor(wasmModule, length, channelCount, maxChannelCount) {
// HeapAudioBuffer initialization
// The |channelCount| must be greater than 0, and less than or equal to
// the maximum channel count.
this._isInitialized = false;
this._module = wasmModule;
this._length = length;
this._maxChannelCount = maxChannelCount ?
Math.min(maxChannelCount, MAX_CHANNEL_COUNT) : channelCount;
this._channelCount = channelCount;
this._allocateHeap();
this._isInitialized = true;


// RingBuffer initialization
this._readIndex = 0;
this._writeIndex = 0;
this._framesAvailable = 0;
this._channelDataLocal = [];
for (let i = 0; i < this._channelCount; ++i) {
this._channelDataLocal[i] = new Float32Array(length);
}
}

/**
* Allocates memory in the WASM heap and set up Float32Array views for the
* channel data.
*
* @private
*/
_allocateHeap() {
const channelByteSize = this._length * BYTES_PER_SAMPLE;
const dataByteSize = this._channelCount * channelByteSize;
this._dataPtr = this._module._malloc(dataByteSize);
this._channelData = [];
for (let i = 0; i < this._channelCount; ++i) {
const startByteOffset = this._dataPtr + i * channelByteSize;
const endByteOffset = startByteOffset + channelByteSize;
// Get the actual array index by dividing the byte offset by 2 bytes.
this._channelData[i] =
this._module.HEAPF32.subarray(
startByteOffset >> BYTES_PER_UNIT,
endByteOffset >> BYTES_PER_UNIT);
}
}

/**
* Adapt the current channel count to the new input buffer.
*
* @param {number} newChannelCount The new channel count.
*/
adaptChannel(newChannelCount) {
if (newChannelCount < this._maxChannelCount) {
this._channelCount = newChannelCount;
}
}

/**
* Getter for the buffer length in frames.
*
* @return {?number} Buffer length in frames.
*/
get length() {
return this._isInitialized ? this._length : null;
}

/**
* Getter for the number of channels.
*
* @return {?number} Buffer length in frames.
*/
get numberOfChannels() {
return this._isInitialized ? this._channelCount : null;
}

/**
* Getter for the maxixmum number of channels allowed for the instance.
*
* @return {?number} Buffer length in frames.
*/
get maxChannelCount() {
return this._isInitialized ? this._maxChannelCount : null;
}

/**
* Returns a Float32Array object for a given channel index. If the channel
* index is undefined, it returns the reference to the entire array of channel
* data.
*
* @param {number|undefined} channelIndex Channel index.
* @return {?Array} a channel data array or an
* array of channel data.
*/
getChannelData(channelIndex) {
if (channelIndex >= this._channelCount) {
return null;
}

return typeof channelIndex === 'undefined' ?
this._channelData : this._channelData[channelIndex];
}

/**
* Returns the base address of the allocated memory space in the WASM heap.
*
* @return {number} WASM Heap address.
*/
getHeapAddress() {
return this._dataPtr;
}

/**
* Returns the base address of the allocated memory space in the WASM heap.
*
* @return {number} WASM Heap address.
*/
getPointer() {
return this._dataPtr;
}

/**
* Frees the allocated memory space in the WASM heap.
*/
free() {
this._isInitialized = false;
this._module._free(this._dataPtr);
this._module._free(this._pointerArrayPtr);
this._channelData = null;
}

/**
* 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.

if (arraySequence.length !== this._channelCount) {
throw new Error(`Channel count mismatch: expected ${this._channelCount}, but got ${arraySequence.length}.`);
}

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

// 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 (arraySequence.length !== this._channelCount) {
throw new Error(`Channel count mismatch: expected ${this._channelCount}, but got ${arraySequence.length}.`);
}

// If the FIFO is completely empty, do nothing.
if (this._framesAvailable === 0) {
return;
}

const destinationLength = arraySequence[0].length;

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

this._framesAvailable -= destinationLength;
if (this._framesAvailable < 0) {
this._framesAvailable = 0;
}
}
} // class FreeQueue

export {
MAX_CHANNEL_COUNT,
RENDER_QUANTUM_FRAMES,
FreeQueue
};
Loading
Loading