2026-06-28 03:28:45 +02:00
|
|
|
/**
|
|
|
|
|
* Reactive spine — the external store that lives BELOW React.
|
|
|
|
|
*
|
|
|
|
|
* Per findings-design-and-manifold.md §4: the SolidJS spine was
|
|
|
|
|
* inputRaw → memo(processed) → memo(ml) → memo(routed) → effect(backend.send)
|
|
|
|
|
* which recomputes on Solid's reactive graph. In React we must NOT couple the
|
|
|
|
|
* per-frame audio inference to the render scheduler. So the spine is a tiny
|
|
|
|
|
* hand-rolled observable: the `setInput` ACTION derives processed → ml → routed
|
|
|
|
|
* EAGERLY + SYNCHRONOUSLY (input pipeline → WasmIML.processInto → output
|
|
|
|
|
* pipeline) and fires the single `backend.send` at the action TAIL, off React's
|
|
|
|
|
* render cycle.
|
|
|
|
|
*
|
|
|
|
|
* React subscribes via `useSyncExternalStore(subscribe, version)` — the version
|
|
|
|
|
* counter, NOT the array — and reads the live `Float32Array` imperatively (so
|
|
|
|
|
* canvases never re-render per frame).
|
|
|
|
|
*
|
|
|
|
|
* Buffers are reused (no per-frame allocation): `routedBuf` is a single
|
|
|
|
|
* Float32Array threaded through the output pipeline and handed to the backend.
|
|
|
|
|
*/
|
|
|
|
|
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
import { clamp } from './curves';
|
2026-06-28 03:28:45 +02:00
|
|
|
import {
|
|
|
|
|
defaultInputConfig,
|
|
|
|
|
defaultInputState,
|
|
|
|
|
processInput,
|
|
|
|
|
type InputConfig,
|
|
|
|
|
type InputState,
|
|
|
|
|
} from './input-pipeline';
|
|
|
|
|
import {
|
|
|
|
|
defaultOutputConfig,
|
|
|
|
|
defaultOutputState,
|
|
|
|
|
processOutput,
|
|
|
|
|
type OutputConfig,
|
|
|
|
|
type OutputState,
|
|
|
|
|
} from './output-pipeline';
|
|
|
|
|
import type { EngineSink, EngineStatePatch } from './sink';
|
|
|
|
|
import type { WasmIML } from './wasm-iml';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Float32Array that may be backed by either a plain ArrayBuffer or a
|
|
|
|
|
* SharedArrayBuffer (TS 5.7+ made `Float32Array` generic over its buffer).
|
|
|
|
|
* The output pipeline returns the loosely-typed form; we keep our reused
|
|
|
|
|
* buffers loosely typed too so assignment doesn't fight the lib types.
|
|
|
|
|
*/
|
|
|
|
|
type F32 = Float32Array<ArrayBufferLike>;
|
|
|
|
|
|
|
|
|
|
/** The single side-effect the spine fires at the tail of each `setInput`. */
|
|
|
|
|
export type BackendSend = (routed: Float32Array) => void;
|
|
|
|
|
|
|
|
|
|
export interface SpineState {
|
|
|
|
|
/** Monotonically increasing; bumped on every state change. */
|
|
|
|
|
version: number;
|
|
|
|
|
ready: boolean;
|
|
|
|
|
training: boolean;
|
|
|
|
|
exampleCount: number;
|
|
|
|
|
lastLoss: number | null;
|
|
|
|
|
lossHistory: ReadonlyArray<number>;
|
|
|
|
|
inputSize: number;
|
|
|
|
|
outputSize: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The spine doubles as the `EngineSink` consumed by `WasmIML`. WasmIML calls
|
|
|
|
|
* `setState/setOutputs/setWeights/emit`; the spine merges into its state,
|
|
|
|
|
* stashes the live output/weight buffers, and bumps the version counter so
|
|
|
|
|
* `useSyncExternalStore` consumers re-read.
|
|
|
|
|
*/
|
|
|
|
|
export class Spine implements EngineSink {
|
|
|
|
|
private state_: SpineState = {
|
|
|
|
|
version: 0,
|
|
|
|
|
ready: false,
|
|
|
|
|
training: false,
|
|
|
|
|
exampleCount: 0,
|
|
|
|
|
lastLoss: null,
|
|
|
|
|
lossHistory: [],
|
|
|
|
|
inputSize: 2,
|
|
|
|
|
outputSize: 126,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private listeners = new Set<() => void>();
|
|
|
|
|
private eventListeners = new Map<string, Set<(payload?: unknown) => void>>();
|
|
|
|
|
|
|
|
|
|
// Engine handles wired in via `attach`.
|
|
|
|
|
private iml: WasmIML | null = null;
|
|
|
|
|
private backendSend: BackendSend | null = null;
|
|
|
|
|
|
|
|
|
|
// Pipeline config + per-frame state.
|
|
|
|
|
inputConfig: InputConfig = defaultInputConfig();
|
|
|
|
|
outputConfig: OutputConfig = { ...defaultOutputConfig(), reuseBuffer: true };
|
|
|
|
|
private inputState: InputState = defaultInputState();
|
|
|
|
|
private outputState: OutputState = defaultOutputState();
|
|
|
|
|
|
|
|
|
|
// Reused per-frame buffers — NO per-frame allocation in the hot path.
|
|
|
|
|
private rawInput: [number, number] = [0.5, 0.5];
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
// Full input vector fed to the MLP. Axes 0,1 = primary pad (post input
|
|
|
|
|
// pipeline); axes 2+ = secondary controller(s), or the neutral 0.5 when no
|
|
|
|
|
// second controller is active (single-controller mode). Sized to the WASM
|
|
|
|
|
// head's input arity; defaults all-neutral.
|
|
|
|
|
private inputs: number[] = [0.5, 0.5];
|
2026-06-28 03:28:45 +02:00
|
|
|
// Last raw input, so `EngineApi.process()` can re-tick after a weight change.
|
|
|
|
|
lastRawX = 0.5;
|
|
|
|
|
lastRawY = 0.5;
|
|
|
|
|
private mlBuf: F32 = new Float32Array(126);
|
|
|
|
|
private routedBuf: F32 | null = null;
|
|
|
|
|
|
|
|
|
|
// Last live output (post-ML, pre-routing) and weights, read imperatively.
|
|
|
|
|
private liveOutputs: F32 = new Float32Array(126);
|
|
|
|
|
private liveWeights: F32 = new Float32Array(0);
|
|
|
|
|
|
|
|
|
|
private lastTickMs = 0;
|
|
|
|
|
|
|
|
|
|
// ---- EngineSink ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
setState(patch: EngineStatePatch): void {
|
|
|
|
|
let changed = false;
|
|
|
|
|
const s = this.state_;
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
if (patch.inputSize !== undefined && patch.inputSize !== s.inputSize) {
|
|
|
|
|
s.inputSize = patch.inputSize;
|
|
|
|
|
this.resizeInputs_(patch.inputSize);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
2026-06-28 03:28:45 +02:00
|
|
|
if (patch.outputSize !== undefined && patch.outputSize !== s.outputSize) {
|
|
|
|
|
s.outputSize = patch.outputSize;
|
|
|
|
|
// Resize hot buffers to the resolved output size.
|
|
|
|
|
this.mlBuf = new Float32Array(patch.outputSize);
|
|
|
|
|
this.routedBuf = new Float32Array(patch.outputSize);
|
|
|
|
|
this.liveOutputs = new Float32Array(patch.outputSize);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
if (patch.exampleCount !== undefined && patch.exampleCount !== s.exampleCount) { s.exampleCount = patch.exampleCount; changed = true; }
|
|
|
|
|
if (patch.lastLoss !== undefined && patch.lastLoss !== s.lastLoss) { s.lastLoss = patch.lastLoss; changed = true; }
|
|
|
|
|
if (patch.lossHistory !== undefined) { s.lossHistory = patch.lossHistory; changed = true; }
|
|
|
|
|
if (patch.training !== undefined && patch.training !== s.training) { s.training = patch.training; changed = true; }
|
|
|
|
|
if (patch.ready !== undefined && patch.ready !== s.ready) { s.ready = patch.ready; changed = true; }
|
|
|
|
|
if (changed) this.bump_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setOutputs(out: Float32Array): void {
|
|
|
|
|
if (this.liveOutputs.length === out.length) this.liveOutputs.set(out);
|
|
|
|
|
else this.liveOutputs = new Float32Array(out);
|
|
|
|
|
this.bump_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setWeights(w: Float32Array): void {
|
|
|
|
|
this.liveWeights = w;
|
|
|
|
|
this.bump_();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emit(event: string, payload?: unknown): void {
|
|
|
|
|
const set = this.eventListeners.get(event);
|
|
|
|
|
if (set) for (const fn of set) fn(payload);
|
|
|
|
|
// Prefix listeners ("ml." matches "ml.trained").
|
|
|
|
|
for (const [prefix, fns] of this.eventListeners) {
|
|
|
|
|
if (prefix.endsWith('.') && event.startsWith(prefix)) {
|
|
|
|
|
for (const fn of fns) fn(payload);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Wiring --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
attach(iml: WasmIML, backendSend: BackendSend | null): void {
|
|
|
|
|
this.iml = iml;
|
|
|
|
|
this.backendSend = backendSend;
|
|
|
|
|
if (this.routedBuf === null || this.routedBuf.length !== iml.architecture.outputSize) {
|
|
|
|
|
this.routedBuf = new Float32Array(iml.architecture.outputSize);
|
|
|
|
|
}
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
this.resizeInputs_(iml.architecture.inputSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Resize the full input vector to the WASM head arity, neutral-filling. */
|
|
|
|
|
private resizeInputs_(size: number): void {
|
|
|
|
|
if (size < 1) size = 1;
|
|
|
|
|
if (this.inputs.length === size) return;
|
|
|
|
|
const next = new Array<number>(size).fill(0.5);
|
|
|
|
|
for (let i = 0; i < Math.min(size, this.inputs.length); i++) next[i] = this.inputs[i]!;
|
|
|
|
|
this.inputs = next;
|
2026-06-28 03:28:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setBackendSend(backendSend: BackendSend | null): void {
|
|
|
|
|
this.backendSend = backendSend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- The hot action ------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drive a raw [0,1] XY input through processed → ml → routed eagerly and
|
|
|
|
|
* synchronously, then fire the single backend.send at the tail. Off render.
|
|
|
|
|
* Returns the routed buffer (live, reused — do not retain across calls).
|
|
|
|
|
*/
|
|
|
|
|
setInput(x: number, y: number): Float32Array | null {
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
return this.tick_(x, y, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drive the full composed input vector (from the modular InputLayer). Axes
|
|
|
|
|
* 0,1 are the primary controller (run through the input pipeline); axes 2+
|
|
|
|
|
* are secondary controllers, written through with a plain [0,1] clamp. Any
|
|
|
|
|
* axis the caller omits keeps its previous value (neutral 0.5 by default), so
|
|
|
|
|
* single-controller mode — where only axes 0,1 are supplied — leaves the
|
|
|
|
|
* second pair pinned at the neutral value.
|
|
|
|
|
*/
|
|
|
|
|
setInputs(arr: ReadonlyArray<number>): Float32Array | null {
|
|
|
|
|
return this.tick_(arr[0] ?? 0.5, arr[1] ?? 0.5, arr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private tick_(x: number, y: number, arr: ReadonlyArray<number> | null): Float32Array | null {
|
2026-06-28 03:28:45 +02:00
|
|
|
const iml = this.iml;
|
|
|
|
|
if (!iml) return null;
|
|
|
|
|
|
|
|
|
|
const now = (typeof performance !== 'undefined' ? performance.now() : Date.now());
|
|
|
|
|
const dt = this.lastTickMs > 0 ? (now - this.lastTickMs) / 1000 : 1 / 60;
|
|
|
|
|
this.lastTickMs = now;
|
|
|
|
|
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
// 1. processed (pure input pipeline) — primary controller, axes 0,1.
|
2026-06-28 03:28:45 +02:00
|
|
|
this.rawInput[0] = x;
|
|
|
|
|
this.rawInput[1] = y;
|
|
|
|
|
this.lastRawX = x;
|
|
|
|
|
this.lastRawY = y;
|
|
|
|
|
const proc = processInput(this.rawInput, this.inputConfig, this.inputState, dt);
|
|
|
|
|
this.inputState = proc.state;
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
this.inputs[0] = proc.x;
|
|
|
|
|
this.inputs[1] = proc.y;
|
|
|
|
|
// Secondary axes (2+): pass through with a [0,1] clamp. Omitted axes keep
|
|
|
|
|
// their prior value (neutral 0.5 in single-controller mode).
|
|
|
|
|
if (arr) {
|
|
|
|
|
for (let i = 2; i < this.inputs.length; i++) {
|
|
|
|
|
if (i < arr.length) this.inputs[i] = clamp(arr[i]!, 0, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-28 03:28:45 +02:00
|
|
|
|
|
|
|
|
// 2. ml (inference into the reused buffer; no alloc)
|
feat(inputs): single/double controller toggle (shared core + manifold)
Toggle between one and two 2-D controllers without rebuilding the MLP.
Shared core (nisps/modes/base.hpp):
- ModeBase per-channel pin mask + pin_value (default 0.5). Pinned channels feed
the neutral value to the MLP via effective_input(); tick_control/setup use it.
Single-controller mode pins the second controller's two channels — the net is
never resized, so trained state survives toggling.
Manifold/browser:
- Widen the WASM head MLP<2,…> -> MLP<4,10,14,18,126> (4 inputs = two 2-D
controllers). All bindings are dimension-driven via DefaultMLP::kInput.
- Spine holds a full N-axis input vector: axes 0,1 = primary pad (existing input
pipeline), axes 2+ = secondary controller, unused axes default to neutral 0.5.
engine-api.setInputs feeds the whole vector; input-layer passthrough pads 0.5
(the lossy 4->2 blend is now only a fallback for >4 axes).
- Inputs dock: Controllers Single/Double(+gamepad) toggle, default single.
- build-wasm.sh mirrors nisps.{js,wasm} into manifold/public.
Tests/docs:
- test_mode_paf_synth: pinned-inputs-neutralized covers the pin contract.
- parity_check.cpp + parity_wasm.mjs exercise all 4 inputs; native↔WASM parity
PASS (max delta 2.4e-7).
- CLAUDE.md / MAP.md / nisps/wasm/README.md updated (no longer "fixed MLP<2,…>").
Firmware settings-menu toggle (Phase 3) still pending.
2026-06-28 20:47:15 +02:00
|
|
|
for (let i = 0; i < this.inputs.length; i++) iml.setInput(i, this.inputs[i]!);
|
2026-06-28 03:28:45 +02:00
|
|
|
iml.processInto(this.mlBuf);
|
|
|
|
|
// Mirror to liveOutputs for imperative reads + bump.
|
|
|
|
|
this.liveOutputs.set(this.mlBuf.subarray(0, this.liveOutputs.length));
|
|
|
|
|
|
|
|
|
|
// 3. routed (output pipeline → reused routedBuf)
|
|
|
|
|
const routedRes = processOutput(this.mlBuf, this.outputConfig, this.outputState, dt * 1000);
|
|
|
|
|
this.outputState = routedRes.state;
|
|
|
|
|
const routed = routedRes.processed;
|
|
|
|
|
if (this.routedBuf && this.routedBuf.length === routed.length) {
|
|
|
|
|
this.routedBuf.set(routed);
|
|
|
|
|
} else {
|
|
|
|
|
this.routedBuf = routed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. single backend.send at the tail (off React render)
|
|
|
|
|
if (this.backendSend && this.routedBuf) this.backendSend(this.routedBuf);
|
|
|
|
|
|
|
|
|
|
this.bump_();
|
|
|
|
|
return this.routedBuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Imperative reads (canvas consumers bypass React) --------------
|
|
|
|
|
|
|
|
|
|
/** Live post-ML output vector. Reused — read, don't retain. */
|
|
|
|
|
outputs(): Float32Array {
|
|
|
|
|
return this.liveOutputs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Live routed (post output-pipeline) vector. Reused — read, don't retain. */
|
|
|
|
|
routedOutput(): Float32Array | null {
|
|
|
|
|
return this.routedBuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
weights(): Float32Array {
|
|
|
|
|
return this.liveWeights;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- useSyncExternalStore plumbing ---------------------------------
|
|
|
|
|
|
|
|
|
|
subscribe = (cb: () => void): (() => void) => {
|
|
|
|
|
this.listeners.add(cb);
|
|
|
|
|
return () => { this.listeners.delete(cb); };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
version = (): number => this.state_.version;
|
|
|
|
|
|
|
|
|
|
getState(): Readonly<SpineState> {
|
|
|
|
|
return this.state_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
on(event: string, fn: (payload?: unknown) => void): () => void {
|
|
|
|
|
let set = this.eventListeners.get(event);
|
|
|
|
|
if (!set) { set = new Set(); this.eventListeners.set(event, set); }
|
|
|
|
|
set.add(fn);
|
|
|
|
|
return () => { set!.delete(fn); };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bump_(): void {
|
|
|
|
|
this.state_ = { ...this.state_, version: this.state_.version + 1 };
|
|
|
|
|
for (const fn of this.listeners) fn();
|
|
|
|
|
}
|
|
|
|
|
}
|