Four items from one workflow, committed together because their build and CI
wiring genuinely interleaves — nisps/CMakeLists.txt, run-all-tests.sh and
ci.yml each carry hunks from two of them, and the stage renumbering (1/5 ->
1/6) touches every line. Splitting would produce commits that do not build,
which is worse than a commit that does four things and says so.
S26 part 2 — the curve declaration now matches reality. params[].curve stays
the mode-wide DEFAULT; a voice_spaces entry may now be {name, curve_overrides}
declaring only the slots where THAT voice space deviates. The 6 modes with one
voice space are byte-identical. The values were derived MECHANICALLY by a new
codegen/curve-audit.ts that models the four idioms a p[N]*p[N] regex misses
(alias form, memlcelium's implicit-counter sq() lambda, loop-generated indices,
smooth_params_), inlines helpers, and RAISES rather than guessing when it
cannot reduce an expression. A drift gate cross-checks 1179 (voice space x
param) slots against engine source on every run and was proved to fail loudly
on three drift classes. Application stays in the engine: nisps/engines,
nisps/pipeline and nisps/core are untouched, generated output is pure insertion
(755 insertions, 0 deletions), and the rebuilt nisps.wasm was byte-identical.
S4 / 7.2 — firmware reads the active mode's driver config at mode start, and
mic/line is real. My brief assumed the engine owns this; the code disagreed and
the code was right. sound_analysis_midi's EngineT is NoOpEngine — the mic lives
on a separately-composed AnalysisEngine member — so engine-level wiring would
have compiled, passed every gate, and left the one mic mode on line input.
Hence a mode-level seam defaulting to engine().driver_config(). Separately,
DriverConfig's defaults (line_level 0, output_volume 1.0) had drifted from
memllib's actual 3/0.8 because nothing had ever read them; wiring them as-is
would have made every silent mode louder and its line input maximally
insensitive — a behaviour change disguised as plumbing. Now pinned by a test.
Also: GetSysClockSpeed() panic()s on unsupported sample rates and runs on the
first line of setup(), so sample_rate needed a fallback ahead of clock setup.
CI's firmware env list gains soundanalysismidi — it is the only mic variant and
nothing else compiles that path.
Plan 5e — telemetry is real. A loss_history C-API entry across the full 5-layer
chain lets the browser read the per-iteration loss the core already records.
The audit named one fabrication site; there were two — wasm-iml.ts's
synchronous train() published lossHistory: [loss] as well. A third, ctx.loss,
was not merely dead but actively synthetic (fallbacks of prev * 0.82 and a
literal 0.5, rendered by nothing) and is deleted. The firmware buffer stays
untouched, per the L25 call. EngineApi.lossHistory() reads spine state rather
than the MLP handle, because trainAsync() fits on the worker's mirror net and
the handle would give a subtly-wrong second answer.
Plan 5f — engine throughput is measurable. One source compiled twice (CMake
natively, emcc for WASM) so the targets compare directly and no WASM export is
added. Sequencers are driven into a working state, and every row prints its own
working-state evidence so a number produced by an idle engine is visible rather
than plausible. Reports, never asserts: a wall-clock threshold on shared
hardware is meaningless or flaky, same call as the firmware size job.
ALIGNMENT: the telemetry defect is deleted (built, not deferred); the
performance defect is rewritten to what is actually left — these are HOST
numbers, and nothing measures the RP2350 at 150 MHz, which is the target the
mission's constraint is about. Q4 (memllib ownership) and Q5 (legacy feedback
modes) are closed.
Corrections to my own earlier claims, both found by agents contradicting the
brief: manifold/ONBOARDING.md was NOT "now accurate" — its primitives list
still named five deleted primitives and cited a seededGradient() that does not
exist. And the parity harness misses the sequencer engines because it runs 128
frames while their sequencers evaluate every 400-500 samples, NOT because
all-params-0.5 fails to trigger them (it does trigger: 0.5 maps to ratio 2,
firing three times per bar). The fix is a longer window, not different params.
Gates: run-all-tests.sh ALL GREEN — 4/4 ctest, parity PASS, lint clean, curve
drift 1179 slots ok, 39 e2e (was 33). Firmware: 5 envs built including the mic
variant.
231 lines
8.5 KiB
C++
231 lines
8.5 KiB
C++
// firmware/MEMLNaut-NISPS/src/main.cpp — Thin entry point.
|
|
//
|
|
// The heavy lifting lives under:
|
|
// - nisps/... — platform-agnostic ML, DSP, engines, modes
|
|
// (repo root; reached via -I, see platformio.ini)
|
|
// - firmware/MEMLNaut-NISPS/glue/... — hardware bindings (audio driver, MIDI,
|
|
// peripherals, output router)
|
|
// - firmware/MEMLNaut-NISPS/lib/memllib/ — vendored hardware-abstraction library
|
|
// (PlatformIO private library; see
|
|
// lib/memllib/VENDORED.md)
|
|
//
|
|
// This file does only:
|
|
// 1. Instantiate the mode selected at compile time by the active PlatformIO
|
|
// env (`-DMEMLNAUT_MODE_TYPE=...`, or `-DNISPS_SELFTEST=1` for the
|
|
// guided hardware self-test — see platformio.ini, one [env] per variant).
|
|
// 2. setup() / loop() on core 0:
|
|
// - boot board (sample rate from the mode's driver config, then clock)
|
|
// - bind peripherals → mode.set_input
|
|
// - bind MIDI in → mode.note_on/update_bpm/...
|
|
// - run mode.tick_control() at ML cadence (5ms)
|
|
// 3. setup1() / loop1() on core 1:
|
|
// - register the audio bridge so AudioDriver streams into mode.process
|
|
// - bring the codec up on the mode's driver config (mic vs line, gains)
|
|
// - pump engine events / drain MIDI out at sub-ms cadence
|
|
|
|
// ---- Hardware ----
|
|
// main.cpp is a plain .cpp (PlatformIO builds src/ as-is; no .ino ->
|
|
// preprocessed-sketch step), so Arduino.h is no longer supplied implicitly.
|
|
#include <Arduino.h>
|
|
#include "PicoDefs.hpp"
|
|
#include "audio/AudioDriver.hpp"
|
|
#include "hardware/memlnaut/MEMLNaut.hpp"
|
|
#include "interface/MIDIInOut.hpp"
|
|
#include "utils/perf.hpp"
|
|
#include "hardware/structs/bus_ctrl.h"
|
|
|
|
// ---- Glue ----
|
|
#include "glue/audio_driver.hpp"
|
|
#include "glue/midi_io.hpp"
|
|
#include "glue/mode_select.hpp"
|
|
#include "glue/output_router.hpp"
|
|
#include "glue/peripherals.hpp"
|
|
#include "glue/settings_view.hpp"
|
|
|
|
// ---- Mode selection ----
|
|
// MEMLNAUT_MODE_TYPE and NISPS_SELFTEST are supplied by the active PlatformIO
|
|
// [env] (platformio.ini), one env per firmware variant — no in-source mode
|
|
// registry, no build-time file rewriting. NISPS_SELFTEST is left undefined
|
|
// (== 0 in the #if below) by every normal-mode env; only the `selftest` env
|
|
// defines it.
|
|
#ifndef NISPS_SELFTEST
|
|
#define NISPS_SELFTEST 0
|
|
#endif
|
|
|
|
#include <memory>
|
|
|
|
// Inter-core handshake flags + stack flag — shared by BOTH the normal-mode and
|
|
// self-test build paths, so they live outside the fork below.
|
|
volatile bool APP_SRAM g_core0_ready = false;
|
|
volatile bool APP_SRAM g_core1_ready = false;
|
|
volatile bool APP_SRAM g_serial_ready = false;
|
|
volatile bool APP_SRAM g_iface_ready = false;
|
|
|
|
bool core1_separate_stack = true;
|
|
|
|
#if !NISPS_SELFTEST
|
|
// =====================================================================
|
|
// Normal-mode build path (an engine + ML mode runs the device).
|
|
// =====================================================================
|
|
|
|
using ActiveMode = MEMLNAUT_MODE_TYPE;
|
|
|
|
ActiveMode AUDIO_MEM g_mode;
|
|
|
|
// Definition of the audio bridge declared in glue/audio_driver.hpp.
|
|
// Lives in the audio SRAM section so the per-block callback dereferences it
|
|
// without paying flash latency.
|
|
volatile nisps_firmware::ActiveModeBridge AUDIO_MEM nisps_firmware::g_active_mode_bridge{};
|
|
|
|
// Global MIDI handle (shared across cores like the legacy entry point did).
|
|
std::shared_ptr<MIDIInOut> APP_SRAM g_midi;
|
|
|
|
// Audio block callback — placed in SRAM via __not_in_flash_func so the audio
|
|
// ISR avoids XIP latency. Forwards into the (header-inline) dispatch helper.
|
|
void AUDIO_FUNC(audio_block_callback)(
|
|
float in[][kBufferSize],
|
|
float out[][kBufferSize],
|
|
size_t n_channels,
|
|
size_t n_frames) {
|
|
nisps_firmware::dispatch_audio_block(in, out, n_channels, n_frames);
|
|
}
|
|
|
|
static uint32_t get_rosc_entropy_seed(int bits) {
|
|
uint32_t seed = 0;
|
|
for (int i = 0; i < bits; ++i) {
|
|
busy_wait_us_32(5);
|
|
seed <<= 1;
|
|
seed |= (rosc_hw->randombit & 1);
|
|
}
|
|
return seed;
|
|
}
|
|
|
|
// =====================================================================
|
|
// Core 0 — UI / hardware polling / ML inference
|
|
// =====================================================================
|
|
|
|
void setup() {
|
|
// The active mode's engine picks the sample rate (0 ⇒ don't care ⇒ 48 kHz).
|
|
// This has to happen before the system clock is derived from it below, and
|
|
// therefore before core 1 clears the g_serial_ready handshake and reads
|
|
// AudioDriver::GetSampleRate() in setup1().
|
|
nisps_firmware::apply_mode_sample_rate(g_mode);
|
|
set_sys_clock_khz(AudioDriver::GetSysClockSpeed(), true);
|
|
bus_ctrl_hw->priority = BUSCTRL_BUS_PRIORITY_DMA_W_BITS
|
|
| BUSCTRL_BUS_PRIORITY_DMA_R_BITS
|
|
| BUSCTRL_BUS_PRIORITY_PROC1_BITS;
|
|
|
|
const uint32_t seed = get_rosc_entropy_seed(32);
|
|
srand(seed);
|
|
g_mode.ml().seed(static_cast<uint64_t>(seed));
|
|
g_mode.ml().draw_weights(g_mode.param_schema().default_spread);
|
|
|
|
g_midi = std::make_shared<MIDIInOut>();
|
|
|
|
Serial.begin(115200);
|
|
Serial.println("Serial initialised.");
|
|
WRITE_VOLATILE(g_serial_ready, true);
|
|
|
|
MEMLNaut::Initialize();
|
|
pinMode(33, OUTPUT);
|
|
|
|
// Wire hardware → mode I/O channels.
|
|
nisps_firmware::bind_peripherals(g_mode);
|
|
|
|
WRITE_VOLATILE(g_iface_ready, true);
|
|
Serial.println("Bound peripherals to mode.");
|
|
|
|
WRITE_VOLATILE(g_core0_ready, true);
|
|
while (!READ_VOLATILE(g_core1_ready)) {
|
|
MEMORY_BARRIER();
|
|
delay(1);
|
|
}
|
|
|
|
MEMLNaut::Instance()->addSystemInfoView();
|
|
// Settings menu (e.g. Joystick: Dual/Single for the 4-input modes).
|
|
nisps_firmware::wire_settings(g_mode);
|
|
Serial.println("Finished initialising core 0.");
|
|
}
|
|
|
|
PERF_DECLARE(MLSTATS);
|
|
|
|
#define ML_INFERENCE_PERIOD_US 5000
|
|
|
|
void loop() {
|
|
PERIODIC_RUN_US({
|
|
PERF_BEGIN(MLSTATS);
|
|
g_mode.tick_control();
|
|
MEMLNaut::Instance()->loop();
|
|
PERF_END(MLSTATS);
|
|
}, ML_INFERENCE_PERIOD_US)
|
|
|
|
PERIODIC_RUN_US({
|
|
static size_t blip_counter = 0;
|
|
if (blip_counter++ > 10) {
|
|
blip_counter = 0;
|
|
Serial.println(".");
|
|
digitalWrite(33, HIGH);
|
|
// `const`, not `constexpr`: memllib made kSampleRate a runtime
|
|
// `extern size_t` so a mode can pick its own rate. This is a
|
|
// once-per-second diagnostic print, so the divide is free.
|
|
const float audioHeadroomMul = 1.0f / (1000000.f * 48.0f / kSampleRate);
|
|
Serial.printf("ml: %d, aud: %d, q: %f\n",
|
|
PERF_GET_MEAN(MLSTATS),
|
|
AUDIOLOOP_MEAN,
|
|
AUDIOLOOP_MEAN * audioHeadroomMul);
|
|
} else {
|
|
digitalWrite(33, LOW);
|
|
}
|
|
}, 100000)
|
|
}
|
|
|
|
// =====================================================================
|
|
// Core 1 — real-time audio + MIDI I/O drain
|
|
// =====================================================================
|
|
|
|
void setup1() {
|
|
while (!READ_VOLATILE(g_serial_ready)) { MEMORY_BARRIER(); delay(1); }
|
|
while (!READ_VOLATILE(g_iface_ready)) { MEMORY_BARRIER(); delay(1); }
|
|
|
|
if (g_midi) {
|
|
g_midi->Setup(/*n_outputs=*/16);
|
|
g_midi->SetMIDISendChannel(1);
|
|
nisps_firmware::bind_midi_input(g_midi, g_mode);
|
|
}
|
|
|
|
g_mode.setup(static_cast<float>(AudioDriver::GetSampleRate()));
|
|
|
|
nisps_firmware::register_audio_engine(g_mode, &audio_block_callback);
|
|
// Codec setup follows the ACTIVE mode: mic vs line input, input gain step,
|
|
// mic pre-amp gain, analog output volume (glue/audio_driver.hpp).
|
|
nisps_firmware::setup_audio_driver(g_mode);
|
|
|
|
WRITE_VOLATILE(g_core1_ready, true);
|
|
while (!READ_VOLATILE(g_core0_ready)) { MEMORY_BARRIER(); delay(1); }
|
|
Serial.println("Finished initialising core 1.");
|
|
}
|
|
|
|
void loop1() {
|
|
PERIODIC_RUN_US({
|
|
nisps_firmware::drain_outputs(g_midi, g_mode);
|
|
}, 1000)
|
|
|
|
PERIODIC_RUN_US({
|
|
if (g_midi) g_midi->Poll();
|
|
}, 1000)
|
|
}
|
|
|
|
#else
|
|
// =====================================================================
|
|
// SelfTest build path — guided hardware self-test rig (no engine / no ML).
|
|
// All four entry points delegate into glue/selftest.hpp.
|
|
// =====================================================================
|
|
|
|
#include "glue/selftest.hpp"
|
|
|
|
void setup() { nisps_firmware::selftest::setup(); }
|
|
void loop() { nisps_firmware::selftest::loop(); }
|
|
void setup1() { nisps_firmware::selftest::setup1(); }
|
|
void loop1() { nisps_firmware::selftest::loop1(); }
|
|
|
|
#endif // NISPS_SELFTEST
|