Note: This doesn't work quite yet, but the structure is drafted out.
A stream that decodes ArrayBuffers into AudioBuffers. The decoders are made in WebAssembly so they are portable (Node.js and browser) and decent speed.
const decoder = // Obtain codec module and initialize decoder:const mod = await const decode = await // Decode ArrayBuffers:
Install
npm i -D audio-decode-wasm
Usage
decoder(mod) -> Promise<decode>
Initializes a decoder from a given [WebAssembly.Module
]. The available ones
can be seen in src/
.
const mod = ...
decode(arrayBuffer, done)
Decodes arrayBuffer
and calls done(err, audioBuffer)
when finished.
To stop or "reset" the stream send decode(null)
.
const decode = await
Using multiple modules
Promiseall
How does it work?
The decoders are wrote in C and compiled with Emscripten. The code is more restricted than a normal Emscripten runtime so it's cheap to load.
Each C modules has the functions
Context*
From JS you can create the context with _open(input, output)
, where the parameters and return values are pointers on WebAssembly's memory, which JS can access and modify.
The Context
from C looks like:
typedef struct Context;
To construct an AudioBuffer
you need numberOfChannels
and sampleRate
, so JS imports a set_params(int, int)
function which C can call.
The stream routine would look like this:
- JS copies
ArrayBuffer
into WebAssembly's input buffer. - WebAssembly decodes it to planar float values on the output buffer.
- JS copies the output buffer as
Float32Array
s into anAudioBuffer
. - Repeat until stream is done.
Building
Requires Emscripten, Binaryen, and WABT. Then, using make
:
make
to createdist/
make debug
to producedist/*.wat
make clean
to remove output