WebAssembly code used as part of the FluexGL DSP library.
This repository contains the source code for the WebAssembly module, written in Rust using the wasm-bindgen tool.
It forms a crucial part of FluexGL DSP, as the library uses WebAssembly to perform heavy audio processing without blocking the main thread.
FluexGL DSP requires two files when initializing the DSP pipeline:
- The compiled
.wasmfile - The generated
.workletfile
You can find these files packaged together in the release zip.
To use the prebuilt worklets, go to the Releases page and download the latest version (recommended, since new effects are developed and released over time).
The zipped release file contains the following files:
fluexgl-dsp-processor.workletfluexgl-dsp-wasm_bg.wasmfluexgl-dsp-wasm_bg.wasm.d.tsfluexgl-dsp-wasm.d.tsfluexgl-dsp-wasm.js
The fluexgl-dsp-processor.worklet and fluexgl-dsp-wasm_bg.wasm files are required.
Copy these into your project and initialize the DSP pipeline as shown below:
import { DspPipeline } from "@fluexgl/dsp";
const pipeline = new DspPipeline({
pathToWasm: "./assets/fluexgl-dsp/fluexgl-dsp-wasm_bg.wasm",
pathToWorklet: "./assets/fluexgl-dsp/fluexgl-dsp-processor.worklet"
});
await pipeline.InitializeDspPipeline();If you want to build the WebAssembly module yourself, you’ll need to install the required tools.
The Rust toolchain must be installed first, as it’s needed for the other dependencies.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shirm https://sh.rustup.rs | sh-
Add the WebAssembly target:
rustup target add wasm32-unknown-unknown
-
Install the
wasm-bindgenCLI:cargo install wasm-bindgen-cli
-
Install the
wasm-packtool:cargo install wasm-pack
-
(Optional) Install
cargo-watchto automatically rebuild when source files change:cargo install cargo-watch
-
(Optional) Install
binaryenfor additional optimization:sudo apt-get install -y binaryen
You can build the source code using several provided scripts.
-
Compile the WebAssembly module:
npm run sh:web-assembly:build
-
Build the Webpack configuration:
npm run sh:webpack:build
-
Build the worklets:
npm run sh:webpack:run
-
Compile the WebAssembly module:
npm run ps:web-assembly:build
-
Build the Webpack configuration:
npm run ps:webpack:build
-
Build the worklets:
npm run ps:webpack:run
These commands will build the WebAssembly and worklet files into the dist/ folder.
The generated files can be used when initializing the DSP pipeline.
You can easily create custom worklets. Navigate to the following directory:
~/worklets/src/
All worklets are stored here.
To create a custom worklet, add your file to:
~/worklets/src/worklets/
Export your custom class, and import it inside:
~/worklets/src/exports.ts
CustomProcessor.worklet.ts
export default class CustomProcessor extends AudioWorkletProcessor {
constructor(options?: AudioWorkletNodeOptions) {
super(options);
}
public process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: any): boolean {
const output = outputs[0];
output.forEach((channel) => {
for (let i = 0; i < channel.length; i++) {
channel[i] = Math.random() * 2 - 1;
}
});
// Custom processing code here
return true;
}
}exports.ts
import "../../dist/fluexgl-dsp-wasm.js";
import SoftClipProcessor from "./worklets/SoftClipProcessor.worklet";
import WhiteNoiseProcessor from "./worklets/WhiteNoiseProcessor.worklet";
// NEW: import custom processor
import CustomProcessor from "./worklets/CustomProcessor.worklet";
registerProcessor("SoftClipProcessor", SoftClipProcessor);
registerProcessor("WhiteNoiseProcessor", WhiteNoiseProcessor);
// IMPORTANT: register your custom processor
registerProcessor("CustomProcessor", CustomProcessor);- The
.wasmand.workletfiles are the core of FluexGL DSP. - You can either use prebuilt releases or build them yourself.
- Custom worklets allow you to extend the DSP functionality to your needs.