memlnaut-nisps/nisps/CMakeLists.txt

130 lines
5.5 KiB
Text
Raw Normal View History

cmake_minimum_required(VERSION 3.20)
project(nisps_core CXX)
# ---------------------------------------------------------------------------
# Toolchain / standard
# ---------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Default to a Release build when invoked without -DCMAKE_BUILD_TYPE so host
# tests get optimized math; flip with `-DCMAKE_BUILD_TYPE=Debug` for stepping.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()
# ---------------------------------------------------------------------------
# Core interface library — header-only. Other parts of the build (ml/, dsp/,
# engines/, modes/) will link against this once they exist.
# ---------------------------------------------------------------------------
add_library(nisps_core INTERFACE)
target_include_directories(nisps_core
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}
)
target_compile_features(nisps_core INTERFACE cxx_std_20)
# ---------------------------------------------------------------------------
# Emscripten / WASM target detection
# ---------------------------------------------------------------------------
# When building for WASM via emcmake, ${EMSCRIPTEN} is set automatically. We
# don't actually emit a WASM binary from this CMakeLists yet — the WASM build
# script lives in stream 7 (playground/build) and assembles its own
# Emscripten link command. Here we just gate the host-only test executable so
# `emcmake cmake -S nisps -B build-wasm` configures cleanly.
if(EMSCRIPTEN)
message(STATUS "nisps_core: configuring for Emscripten/WASM target")
endif()
# ---------------------------------------------------------------------------
# Test scaffold — only built on the host. We use a hand-rolled assertion-
# based harness (see tests/cpp/test_helpers.hpp) rather than Catch2/doctest;
# the rationale is in test_helpers.hpp.
# ---------------------------------------------------------------------------
if(NOT EMSCRIPTEN)
set(NISPS_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../tests/cpp)
add_executable(nisps_core_tests
${NISPS_TEST_DIR}/test_main.cpp
${NISPS_TEST_DIR}/test_fixed_buffer.cpp
${NISPS_TEST_DIR}/test_ring_buffer.cpp
${NISPS_TEST_DIR}/test_rng.cpp
${NISPS_TEST_DIR}/test_math.cpp
feat(nisps/ml): MLP library with fixed-architecture template + spread-aware RL (meml-wmh) Stream 2 of the clean-slate rewrite: nisps/ml/ replaces src/memlp/ with a header-only, heap-free MLP that satisfies nisps::core::MLEngine. Files (nisps/ml/): - activations.hpp — ReLU (leaky 0.01 for parity), sigmoid, tanh - loss.hpp — MSE per-sample (fixes meml-ues double-scaling: returns the sample's MSE without an extra 1/N multiplication; the training loop averages explicitly) - init.hpp — uniform/Xavier/spread-aware weight init - training.hpp — gradient clip helper (±10.0 matches legacy) - rl.hpp — move_weights with per-layer Xavier scaling, weight decay (10% * spread), gaussian noise via the deterministic Rng (matches the legacy JS sum-of-three-uniforms shape); draw_weights also spread-aware - stats.hpp — per-layer mean/max/dead/saturating diagnostics - mlp.hpp — 4-layer (3 hidden + sigmoid output) MLP class with std::array-backed weights, biases, gradient accumulators, dataset ring buffer (default 128 examples), loss history (default 4096 iters). Bias is a separate per-layer parameter — no input-vector mutation. Flat get_weights/set_weights layout: weights all layers (row-major, layer order), then biases all layers. Tests (tests/cpp/, all 50 passing under -Wall -Wextra -Werror -Wpedantic): - test_mlp_init.cpp — deterministic seeding, spread regimes, static_assert MLEngine concept satisfied - test_mlp_inference.cpp — golden hand-computed forward pass match, sigmoid output range, set_input bounds - test_mlp_training.cpp — XOR convergence (loss < 0.01 in <2k iters), ring-buffer eviction - test_mlp_loss.cpp — meml-ues regression test: reported loss equals hand-computed average MSE without extra 1/N scaling; sample weights honoured - test_mlp_rl.cpp — move_weights respects output_pin_mask (final-layer rows + biases preserved); spread regimes; grad clear after draw_weights - test_mlp_serialize.cpp — get_weights/set_weights round-trip preserves inference exactly; eval_loss is non-mutating; infer_batch matches individual inference Verification: - Clean build, no warnings - 50 tests pass (22 prior + 28 new) - No std::vector / new / malloc in nisps/ml/ - All float literals .f-suffixed in code (comments excepted)
2026-04-29 14:55:43 +02:00
${NISPS_TEST_DIR}/test_mlp_init.cpp
${NISPS_TEST_DIR}/test_mlp_inference.cpp
${NISPS_TEST_DIR}/test_mlp_training.cpp
${NISPS_TEST_DIR}/test_mlp_loss.cpp
${NISPS_TEST_DIR}/test_mlp_rl.cpp
${NISPS_TEST_DIR}/test_mlp_serialize.cpp
)
target_link_libraries(nisps_core_tests PRIVATE nisps_core)
# Chris's rule: the core compiles cleanly under -Wall -Wextra -Werror.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
target_compile_options(nisps_core_tests PRIVATE
-Wall -Wextra -Werror -Wpedantic
)
elseif(MSVC)
target_compile_options(nisps_core_tests PRIVATE /W4 /WX)
endif()
enable_testing()
add_test(NAME nisps_core_tests COMMAND nisps_core_tests)
feat(nisps/engines): port firmware audio engines to AudioEngine concept (meml-1v6) Concept-based, no virtual dispatch, per-engine voice spaces as inline methods. Each engine satisfies nisps::AudioEngine via static_assert. - NoOpEngine: silent passthrough; used for sequencer-only modes and for the SoundAnalysisMIDI mode's audio path. - PAFSynthEngine (33 params, 7 voice spaces): 4-voice PAF synth with detune cascade, ring-mod, sine-shaper, ADSR, feedback delay. note_on/ note_off interface for MIDI keyboard. - ChannelStripEngine (24 params, 6 voice spaces): stereo console strip (pre-gain/HPF/LPF/2x peak/low-shelf/high-shelf/comp/post-gain). Voice spaces: WannabeNeve66, SSL4K, SSL9K, MaleVox, FemaleVox, Neve80 (stepped-frequency). - XIASRIEngine (24 params, "Direct" voice space): pitch-shift + 6 allpass + 2 comb + 4 delays. Direct NN→param mapping per firmware semantics. - VerbFXEngine (47 params, 12 voice spaces): 8-band SVF filterbank + 3-lane dynamic delay + 8-lpcomb/4-allpass Freeverb-style tail with cross-fades. All 12 voice spaces ported from voicespaces/VerbFX/*.hpp. - MEMLCeliumEngine (56 params): 2-track ratio sequencer + dual-voice PAF synth (7+7+22+20 layout). Sequencer triggers V0/V1 ADSR. - BreakOrEngine (56 params): 8-track ratio sequencer; emits NoteOn/ NoteOff/Clock events via pop_events(span). process() returns silence. - ElysiamorfEngine (40 params): 8-track FM-pair sequencer; emits CC events on CCs {1,2,3,4,5,9,11,12}. Silent audio path. - AnalysisEngine (0 params, 6 features): port of XiasriAnalysis (pitch via zero-crossing, aperiodicity via MAD, log-domain energy + attack derivative + brightness ratio). Inputs to ML on SoundAnalysisMIDI mode. All param_count() values match schemas/modes/*.json output_size. 4074 LOC total. CMake adds nisps_dsp_engine_tests target with 38 passing tests under -Wall -Wextra -Werror -Wpedantic.
2026-04-29 15:09:12 +02:00
# ---------------------------------------------------------------------
# DSP + engines tests (stream 3). Built as a separate executable so
# failures here don't take core/ML tests with them.
# ---------------------------------------------------------------------
add_executable(nisps_dsp_engine_tests
${NISPS_TEST_DIR}/test_main.cpp
${NISPS_TEST_DIR}/test_dsp_biquad.cpp
${NISPS_TEST_DIR}/test_dsp_delay.cpp
${NISPS_TEST_DIR}/test_dsp_reverb.cpp
${NISPS_TEST_DIR}/test_dsp_pitch_shift.cpp
${NISPS_TEST_DIR}/test_engine_no_op.cpp
${NISPS_TEST_DIR}/test_engine_paf_synth.cpp
${NISPS_TEST_DIR}/test_engine_channel_strip.cpp
${NISPS_TEST_DIR}/test_engine_xiasri.cpp
${NISPS_TEST_DIR}/test_engine_verb_fx.cpp
${NISPS_TEST_DIR}/test_engine_memlcelium.cpp
${NISPS_TEST_DIR}/test_engine_breakor.cpp
${NISPS_TEST_DIR}/test_engine_elysiamorf.cpp
${NISPS_TEST_DIR}/test_engine_analysis.cpp
)
target_link_libraries(nisps_dsp_engine_tests PRIVATE nisps_core)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
target_compile_options(nisps_dsp_engine_tests PRIVATE
-Wall -Wextra -Werror -Wpedantic
)
elseif(MSVC)
target_compile_options(nisps_dsp_engine_tests PRIVATE /W4 /WX)
endif()
add_test(NAME nisps_dsp_engine_tests COMMAND nisps_dsp_engine_tests)
# ---------------------------------------------------------------------
# Mode tests (stream 4). Cover concept satisfaction, voice space
# dispatch, control-event pumping, and end-to-end inference→audio.
# ---------------------------------------------------------------------
add_executable(nisps_modes_tests
${NISPS_TEST_DIR}/test_main.cpp
${NISPS_TEST_DIR}/test_mode_concepts.cpp
${NISPS_TEST_DIR}/test_mode_paf_synth.cpp
${NISPS_TEST_DIR}/test_mode_voice_space.cpp
${NISPS_TEST_DIR}/test_mode_breakor_events.cpp
)
target_link_libraries(nisps_modes_tests PRIVATE nisps_core)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang")
target_compile_options(nisps_modes_tests PRIVATE
-Wall -Wextra -Werror -Wpedantic
)
elseif(MSVC)
target_compile_options(nisps_modes_tests PRIVATE /W4 /WX)
endif()
add_test(NAME nisps_modes_tests COMMAND nisps_modes_tests)
endif()