Skip to content

JavaScript API

Use the Neuroscript WebAssembly bindings to compile and run Neuroscript inside browser-based tooling, including the docs playgrounds and custom demos.

Build the WASM package

From the repo root:

bash
cd neuroscript-rs
wasm-pack build crates/neuroscript-wasm --target web

Bundle the generated package (neuroscript-rs/crates/neuroscript-wasm/pkg) into your web app.

Quick start

javascript
import init, { WasmEngine, compile_only } from "neuroscript-wasm";

await init();

compile_only("note @1 => note\n");

const engine = new WasmEngine("note @1 => note\n");
const outputs = await engine.runEvent(
  {
    kind: "Note",
    ch: 1,
    data1: 60,
    data2: 100,
    timestamp_ns: Date.now() * 1_000_000,
  },
  { bpm: 120, beat: 4_000_000 },
  Date.now() * 1_000_000
);

console.log(outputs);

API surface

  • compile_only(source: string): void compiles and validates source.
  • new WasmEngine(source: string) compiles and allocates state.
  • engine.runEvent(event, transport?, nowNs?) runs one MIDI event.
  • engine.recompile(source: string) replaces the compiled program.
  • engine.resetState() clears engine state.

Event and transport shapes

ts
type MidiEvent = {
  kind: "Note" | "NoteOn" | "NoteOff" | "CC" | "Clock" | "Start" | "Stop";
  ch: number;
  data1: number;
  data2: number;
  timestamp_ns: number;
};

type Transport = {
  bpm?: number | null;
  beat?: number | null;
};

The transport argument is optional. If nowNs is omitted, the engine uses event.timestamp_ns.

Where to use this

Use these bindings for the in-browser playgrounds, docs examples, or any site that needs to compile and run Neuroscript without a native backend.

Docs integration

When building the docs site, copy the WASM package into docs/web/public/neuroscript-wasm so the playground can load it as a static asset. The Makefile includes a helper target for this.

Built with ❤️ for musicians