Anira Web: from the 2.x TypeScript classes#
The 3.x package (@anira-project/anira) no longer wraps the 2.x C++ classes: the module exports
the C ABI (_anira_*) and anira/anira_em.h (_anira_em_*), the package is a thin typed layer
over them (Basic Usage has the whole setup), and anira starts the page’s other
threads itself, from C: the context’s inference threads are Wasm Workers, the audio path is a Wasm
Audio Worklet node whose render callback is C.
new AniraWeb()/AniraWeb.create()becomesawait Anira.create(); itsabiis the C ABI, one typed method per entry (anira.abi.anira_context_create(config)), which throws anAniraErroron a failed status.malloc,free,getHeapF32and the other heap helpers becomeanira.module(_malloc,HEAPF32, …);getAniraVersionisanira.version,drainAniraLoganira.drainLog().ModelData,TensorShape,ProcessingSpecandInferenceConfigbecome one model config:await ModelConfig.fromUrl(anira, url)reads the 3.x model file a native host reads (a 2.x JSON document too, JSON files) and fetches its model files.HostConfig(bufferSize, sampleRate)becomes the Hard contract:ContractHandle.hard(anira, { hostBlockMin: g.quantum, hostBlockMax: g.burst, hostRate: context.sampleRate })withg = await measureRenderGeometry(context), or a contract file (ContractHandle.fromUrl, thenhardHostBlocks): the render quantum is the smallest block, the context’s burst of quanta the largest. The budget is measured unlessbudgetMssets one;budgetMswithwarmup: ANIRA_WARMUP_NONEis an explicit budget without a warm-up pass. A running node changes its host blocks throughaudioNode.prepare(contract).InferenceHandler(pp, config),setInferenceBackendandpreparebecomePipeline.create(anira).addInference(model),InferenceHandler.create(context, pipeline)andhandler.prepare(contract); a context (Context.create(anira, { threads })) is the core and its pool.inferenceHandler.process(...)in a worklet becomes the handler’s audio node,await handler.createAudioNode(audioContext, { params, staticInputs, waitMs }): the render callback is C;setInputon a Static tensor becomes the node’s AudioParam (scalars) oraudioNode.setStaticInput(tensors).BufferF,RingBufferand theVector*classes have no successor: tensors are heap memory described byanira_tensorrecords. The node streams the model’s Streamed slots (inputSlots/outputSlots, Audio Nodes).JSPrePostProcessorandregisterPrePostProcessorbecomedefineProcessor(name, factory)in every scope that runs one of the processor’s phases (the page, the worklet’s module) andpipeline.addProcessor(name, { phases, flags }): the method names stay (preProcess,postProcess,beforeInference,afterInference), and they receive theanira_processor_ctxpointer instead of buffer vectors.JSBackendBaseandregisterProcessor(backend, className)becomedefineEngine(id, factory)in the context’s worker module andpipeline.addEngine(id, { flags });process(ctx, prepared)reads theanira_engine_ctx(engineTensors), andInferenceBackend.CUSTOMbecomes the engine id in the model entry. The ONNX Runtime Web backend isonnxRuntimeWeb({ model })from@anira-project/anira/engines/onnxruntime-web; the module’s built-in ONNX Runtime engine needs no JavaScript.A running inference Worker blocks its event loop, as in 2.x (
setupInferenceWorkerranrunLoopinside the Worker’sonmessage):processis synchronous, asJSBackendBase.processwas, and the ONNX Runtime Web engine runs through onnxruntime-web’s synchronous C API (_OrtRun) as the 2.x backend did (What an Inference Worker and the Worklet Can See).spinUpInferenceWorker()goes: the context’sthreads(andworkerModule) start the Workers atContext.create, andawait context.ready()waits until they run, which a prepare that warms up (the default) needs first.