When running in the browser, Superpowered sits on top of the Web Audio API's AudioContext. The AudioContext manages the streams to and from the computers audio hardware and also schedules the audio processing callback which we use to render our audio buffers with the Superpowered WebAssembly library.
We've created some helpers which will help you get audio up and running.
For a complete guide to getting setup, you may want to take a look at our guide:
// Most common: import Superpowered and the Superpowered Web Audio helper:
We've wrapped the standard AudioContext class with some helpers to streamline some common tasks when writing audio web applications. The SuperpoweredWebAudio class contains helper functions for easier Web Audio initialization. The returned objects are standard Web Audio objects without any quirks.
Please note that to enable Web Audio features on a page, you are required to be running from a secure context: HTTPS or localhost.
let firstInputSampleLeft = inputBuffer.array;
let firstInputSampleRight = inputBuffer.array;
In the low-level memory model of WebAssembly, memory is represented as a contiguous range of untyped bytes called Linear Memory, which is a standard ArrayBuffer.
Memory can be "allocated" in the Linear Memory, returning with a pointer to the allocated region, which can be used to represent an array of data, such as an array of floating point numbers. This pointer (aka address) is just a simple integer Number, the byte index inside the Linear Memory. For example, 0 represents the first byte in the Linear Memory, however Superpowered occupies this part and 0 means "NULL".
let length =128;// We want the buffer to store 128 floating point numbers.
let pointer =Superpowered.malloc(length *4);// A floating point number is 4 bytes, therefore we allocate length * 4 bytes of memory.
// You can use "pointer" to pass audio to most Superpowered APIs.
let arrayView =newFloat32Array(
Superpowered.linearMemory,// Standard WebAssembly Module reference to the Linear Memory.
pointer,// The address of the allocated region.
length // The length of the region.
// Now this is possible:
// Deallocate the region when we don't need it anymore.
SuperpoweredGlue offers some special typed buffer APIs to make this allocation process a little bit easier:
// Total memory consumption in this example: 256 * 4 = 1024 bytes.
let someBuffer =newSuperpowered.Float32Buffer(256);
someBuffer.pointer;// Getting the linear memory index (pointer).
someBuffer.length;// Getting the length of the buffer (256).
someBuffer.array;// A Float32Array view of this buffer (standard TypedArray).
someBuffer.array=0.5;// Accessing the buffer as a Float32Array.
someBuffer.free();// Deallocate the buffer.
Creates a Superpowered.Uint8Buffer instance.
Length of buffer array.
Deallocate the buffer.
A Uint8Array view of this buffer (standard TypedArray).