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 processPrePost hook 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 multiple AniraWeb instances; 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 size bytes in the WASM heap and return the pointer.

Arguments:
  • size (number)

Returns:

number

free(ptr)

Free a pointer previously returned by malloc().

Arguments:
  • ptr (number)

getMemory()

Return the shared WebAssembly.Memory backing 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 str as a null-terminated UTF-8 string in the WASM heap and return the pointer. The caller is responsible for free()-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 when spinUpInferenceWorker() adds a worker or InferenceWorker.stop removes 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 URL to load a custom worker file (used by user-written JS backends, see Custom Inference Backends), or an already-constructed Worker instance 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 before configureAudioWorklet().

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 AudioWorkletNode wired to inferenceHandlerPtr and prePostProcessorPtr and 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 PrePostProcessor used during inference.

  • audioWorkletNodeName (string) – Processor name registered via registerProcessor inside the worklet file. Defaults to 'inference-processor' (the bundled default worklet’s name).

  • ioOptions (ConfigureAudioWorkletIOOptions) – Channel counts, maxBufferSize, and optional audioWorkletNodeOptions overrides. 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 Float32Array view over the WASM module’s HEAPF32 buffer. Useful for reading or writing float32 data at raw heap offsets.

getHeapU32()

Return a Uint32Array view over the WASM module’s HEAPU32 buffer. Useful for reading or writing pointer-sized values at raw heap offsets — for example the channel pointer arrays referenced by AniraAudioWorkletBase.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: