Class AniraWeb¶
The package’s main entry point. Owns the WebAssembly module, exposes a
factory method per wrapper class, and coordinates the inference
workers and audio worklet integration. Construct one per app via
await AniraWeb.create().
- class AniraWeb(module, memory)¶
- Arguments:
module (AniraWasmInstance)
memory (Memory)
- static async create(config, memory)¶
Instantiate the WASM module and return a ready-to-use
AniraWeb. This is the standard entry point — see Basic Usage.- Arguments:
config (AniraWasmConfig & Record<string, unknown>) – Optional Emscripten module overrides plus an
processPrePosthook for users running their own JS pre/post dispatch outside the registry. Most callers pass nothing.memory (Memory) – Optional pre-allocated shared
WebAssembly.Memory. Used when reusing memory across multipleAniraWebinstances; defaults to a fresh 8192-page shared memory.
- Returns:
Promise<AniraWeb>
- stackRestore(ptr)¶
Restore Emscripten’s stack pointer to a previously saved value. Used internally when re-entering WASM from a worker thread; rarely needed in user code.
- Arguments:
ptr (number)
- malloc(size)¶
Allocate
sizebytes in the WASM heap and return the pointer.- Arguments:
size (number)
- Returns:
number
- getMemory()¶
Return the shared
WebAssembly.Memorybacking the WASM module.- Returns:
Memory
- getWasmInstance()¶
Return the underlying Emscripten module instance. Use this to call raw WASM exports directly when the high-level wrappers don’t cover what you need.
- Returns:
AniraWasmInstance
- allocWasmString(str)¶
Encode
stras a null-terminated UTF-8 string in the WASM heap and return the pointer. The caller is responsible forfree()-ing the returned pointer when done.- Arguments:
str (string)
- Returns:
number
- async registerProcessor(backend, className)¶
Register a custom JS inference backend so the inference worker can dispatch into it. Required for any backend that uses
InferenceBackend.CUSTOM— see Custom Inference Backends. The descriptor is also forwarded to all currently-running inference workers so they can construct the backend on their side.- Arguments:
backend (JSBackendBase)
className (string)
- Returns:
Promise<void>
- async unregisterProcessor(backend)¶
Inverse of
registerProcessor(). Removes the descriptor from the main-thread registry and instructs all active inference workers to destroy and deregister the backend (releasing any resources such as ORT sessions).- Arguments:
backend (JSBackendBase)
- Returns:
Promise<void>
- getActiveWorkers()¶
Return the inference workers currently spawned by this
AniraWeb. The list is updated automatically whenspinUpInferenceWorker()adds a worker orInferenceWorker.stopremoves one.- Returns:
readonly InferenceWorker[]
- async spinUpInferenceWorker(workerOrUrl)¶
Spawn a new inference worker (a Web Worker hosting an
InferenceThread) and return its handle. Inference runs there instead of on the audio thread, keeping the audio worklet real-time-safe. Spin up multiple workers to run inference on multiple batches in parallel — see Architecture.- Arguments:
workerOrUrl (Worker | URL) – Optional override for the worker entry point. Pass a
URLto load a custom worker file (used by user-written JS backends, see Custom Inference Backends), or an already-constructedWorkerinstance to take ownership of one you spawned yourself. Omit to use anira’s bundled default worker.
- Returns:
Promise<InferenceWorker>
- async registerAudioWorkletForContext(audioContext, workletUrl)¶
Install anira’s audio worklet module on the given
AudioContext. Must be called once per context beforeconfigureAudioWorklet().- Arguments:
audioContext (AudioContext) – The Web Audio context to install the worklet on.
workletUrl (string | URL) – Optional URL of a custom worklet file (a subclass of
AniraAudioWorkletBase, see Custom Audio Worklets). Omit to use anira’s bundled default worklet, which handles the simple single-tensor case.
- Returns:
Promise<void>
- async configureAudioWorklet(audioContext, inferenceHandlerPtr, prePostProcessorPtr, audioWorkletNodeName='inference-processor', ioOptions={})¶
Construct an
AudioWorkletNodewired toinferenceHandlerPtrandprePostProcessorPtrand complete the configure handshake so the worklet is ready to process audio. Allocates the input / output scratch buffers in WASM memory and posts them to the worklet thread.- Arguments:
audioContext (AudioContext) – The Web Audio context to attach the node to.
inferenceHandlerPtr (PossiblePointer<InferenceHandler>) – The
InferenceHandler(or its raw pointer) that will run inference for this worklet.prePostProcessorPtr (PossiblePointer<PrePostProcessor>) – The
PrePostProcessorused during inference.audioWorkletNodeName (string) – Processor name registered via
registerProcessorinside the worklet file. Defaults to'inference-processor'(the bundled default worklet’s name).ioOptions (ConfigureAudioWorkletIOOptions) – Channel counts,
maxBufferSize, and optionalaudioWorkletNodeOptionsoverrides. See Custom Audio Worklets for the multi-tensor and custom-buffer-size cases.
- Returns:
Promise<AudioWorkletNode> – The connected, ready-to-use
AudioWorkletNode.
- getHeapF32()¶
Return a
Float32Arrayview over the WASM module’sHEAPF32buffer. Useful for reading or writing float32 data at raw heap offsets.
- getHeapU32()¶
Return a
Uint32Arrayview over the WASM module’sHEAPU32buffer. Useful for reading or writing pointer-sized values at raw heap offsets — for example the channel pointer arrays referenced byAniraAudioWorkletBase.buildMultiTensorPointers().
Class Factories¶
The instance also carries a factory method for every wrapper class.
Each factory takes the same arguments the underlying class
constructor would, but reuses aniraWeb’s WASM instance so you
don’t have to thread it through manually:
const config = aniraWeb.HostConfig(2048, 44100)
// equivalent to: new HostConfig(aniraWeb.getWasmInstance(), 2048, 44100)
The available factories (each linked to the underlying class reference) are:
Class BufferF — exposed as
aniraWeb.BufferClass HostConfig —
aniraWeb.HostConfigClass InferenceConfig —
aniraWeb.InferenceConfigClass InferenceHandler —
aniraWeb.InferenceHandlerClass JSBackendBase —
aniraWeb.JSBackendBaseClass JSPrePostProcessor —
aniraWeb.JSPrePostProcessorClass ModelData —
aniraWeb.ModelDataClass ONNXRuntimeWebBackend —
aniraWeb.ONNXRuntimeWebBackendClass PrePostProcessor —
aniraWeb.PrePostProcessorClass ProcessingSpec —
aniraWeb.ProcessingSpecClass RingBuffer —
aniraWeb.RingBufferClass TensorShape —
aniraWeb.TensorShapeaniraWeb.TensorShapeList,aniraWeb.VectorBufferF,aniraWeb.VectorFloat,aniraWeb.VectorInt64T,aniraWeb.VectorModelData,aniraWeb.VectorRingBuffer,aniraWeb.VectorSizeT,aniraWeb.VectorTensorShape,aniraWeb.VectorUnsignedInt,aniraWeb.VectorVectorInt64— thin wrappers overstd::vector<T>.aniraWeb.InferenceBackend— the backend enum (ONNX,LIBTORCH,TFLITE,CUSTOM).aniraWeb.InferenceThread— internal scheduler primitive, rarely used directly.