memlnaut-nisps/firmware/MEMLNaut-NISPS/glue/audio_driver.hpp
monkey-w1n5t0n a77770f95d feat: curve truth, DriverConfig, real telemetry, engine benchmark
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.
2026-07-21 22:02:23 +02:00

161 lines
6.9 KiB
C++

// firmware/glue/audio_driver.hpp — Bridge memllib AudioDriver to nisps modes.
//
// memllib's AudioDriver delivers samples via a *block* callback shaped like:
// void cb(float in[][kBufferSize], float out[][kBufferSize], size_t nch, size_t nf)
// The nisps `Mode` concept exposes a per-sample `process(stereosample_t)`.
//
// We register a free function as the AudioDriver block callback and pump it
// frame-by-frame into the active mode. The mode pointer is held in a
// templated free function that captures it by reference at call-site (so we
// avoid std::function indirection in the audio path).
//
// The two `stereosample_t` types — the pre-existing firmware POD and the new
// nisps namespaced one — have identical memory layout (two floats L,R) so the
// bridge does an explicit field-wise copy. No reinterpret_cast.
#pragma once
#include <Arduino.h>
#include "nisps/core/perf.hpp"
#include "nisps/core/types.hpp"
#include "audio/AudioDriver.hpp"
#include "codec_config.hpp"
namespace nisps_firmware {
// ---------------------------------------------------------------------------
// Driver configuration — the active mode decides how the codec is set up.
//
// `Mode::driver_config()` (nisps/modes/base.hpp) returns the mode's engine's
// `nisps::DriverConfig`, or the mode's own override when the engine isn't what
// consumes the audio input. Nothing here knows which mode is compiled in: the
// two entry points below are the whole of the glue, and a mode that expresses
// no opinion gets `nisps::DriverConfig{}`'s defaults, which reproduce the
// firmware's historical hardcoded codec setup.
// ---------------------------------------------------------------------------
// Field-for-field translation into memllib's codec struct, after clamping to
// what the SGTL5000 can represent (see codec_config.hpp — that part is
// host-tested in tests/cpp/test_mode_driver_config.cpp).
inline AudioDriver::codec_config_t to_codec_config(const nisps::DriverConfig& cfg) noexcept {
const nisps::DriverConfig c = clamp_driver_config(cfg);
AudioDriver::codec_config_t out{};
out.mic_input = c.mic_input;
out.line_level = static_cast<size_t>(c.line_level);
out.mic_gain_dB = static_cast<size_t>(c.mic_gain_db);
out.output_volume = c.output_volume;
return out;
}
// Publish the mode's preferred sample rate to the driver.
//
// MUST run before `set_sys_clock_khz(AudioDriver::GetSysClockSpeed(), ...)` —
// the system clock is derived from the rate, and `GetSysClockSpeed()` panics on
// a rate it has no divider for. `select_sample_rate` therefore resolves
// "don't care" (0) and anything unsupported to 48 kHz rather than letting it
// reach the driver. Called on core 0 in setup(), i.e. before core 1 gets past
// its `g_serial_ready` handshake and reads `GetSampleRate()`.
template <typename Mode>
inline void apply_mode_sample_rate(const Mode& mode) noexcept {
AudioDriver::SetSampleRate(
static_cast<size_t>(select_sample_rate(mode.driver_config().sample_rate)));
}
// Bring the audio driver up configured for the active mode: codec input source
// (mic vs line), input gain step, mic pre-amp gain, analog output volume.
// Replaces the old parameterless `AudioDriver::Setup()`, which hardcoded line
// input for every variant regardless of what the mode's engine asked for.
template <typename Mode>
inline bool setup_audio_driver(const Mode& mode) {
return AudioDriver::Setup(to_codec_config(mode.driver_config()));
}
// Pointer to the active mode. The audio block callback reads through this.
// Set during setup1() before AudioDriver::Setup() is called. Marked
// `__not_in_flash("audio")` so the audio ISR path does not pay flash latency.
struct ActiveModeBridge {
void* mode_ptr = nullptr; // type-erased Mode*
void (*process_block)(void*,
float[][kBufferSize],
float[][kBufferSize],
size_t, size_t) = nullptr;
};
// Defined in the .ino (not inline-in-header) — `inline` and `__not_in_flash`
// section attributes don't combine cleanly: comdat groups want shared
// linkage, named sections want unique ownership. The .ino owns the
// definition.
extern volatile ActiveModeBridge AUDIO_MEM g_active_mode_bridge;
// Templated trampoline: instantiated once per concrete Mode type. It does the
// per-sample loop and the field-wise stereosample_t copy. Templated rather
// than virtual because the audio path forbids virtual dispatch (architecture
// §3.5).
//
// We can't use `__not_in_flash_func(name)` on a templated function because
// that macro stringifies the function name into the section attribute, and
// the template instantiations all share a single section name — collisions
// are benign at link time but a per-instantiation section would be cleaner.
// Use `__attribute__((hot))` instead for these templated trampolines and
// rely on the platform linker default for placement.
template <typename Mode>
__attribute__((hot)) static void process_block_typed(
void* mode_ptr,
float in[][kBufferSize],
float out[][kBufferSize],
size_t /*n_channels*/,
size_t n_frames) {
auto* mode = static_cast<Mode*>(mode_ptr);
for (size_t i = 0; i < n_frames; ++i) {
::stereosample_t fw_in{in[0][i], in[1][i]};
::nisps::stereosample_t in_ns{fw_in.L, fw_in.R};
const auto out_ns = mode->process(in_ns);
out[0][i] = out_ns.L;
out[1][i] = out_ns.R;
// Modes that override `analyse(stereosample_t)` (e.g. SoundAnalysisMIDI)
// see the input frame here, before the next sample.
if constexpr (requires { mode->analyse(in_ns); }) {
mode->analyse(in_ns);
}
}
}
// Inner forward — the .ino provides the actual block callback (with
// `__not_in_flash_func` placement) and forwards into this. Keeping the
// non-template body in a non-inline function avoids comdat / section
// conflicts.
inline void dispatch_audio_block(
float in[][kBufferSize],
float out[][kBufferSize],
size_t n_channels,
size_t n_frames) {
auto& bridge = const_cast<ActiveModeBridge&>(g_active_mode_bridge);
if (bridge.mode_ptr == nullptr || bridge.process_block == nullptr) {
for (size_t i = 0; i < n_frames; ++i) {
out[0][i] = 0.f;
out[1][i] = 0.f;
}
return;
}
bridge.process_block(bridge.mode_ptr, in, out, n_channels, n_frames);
}
// Public registration: call from setup1() with the active mode and the
// `__not_in_flash_func`-placed block callback that forwards into
// `dispatch_audio_block`.
template <typename Mode>
inline void register_audio_engine(Mode& mode, audiocallback_block_fptr_t block_cb) {
ActiveModeBridge b{};
b.mode_ptr = static_cast<void*>(&mode);
b.process_block = &process_block_typed<Mode>;
__sync_synchronize();
const_cast<ActiveModeBridge&>(g_active_mode_bridge) = b;
__sync_synchronize();
AudioDriver::SetBlockCallback(block_cb);
}
} // namespace nisps_firmware