// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // The randomizer's promise, checked by playing what it produces: over many seeds, // every device must come out in range, audible, and with headroom left. A rule in // Randomizer.cpp that drifts into silence or into clipping fails here rather than // in someone's track. // // randomizer_test [seeds] (default 200 per device) // // Per seed it checks three things: // 1. every value is inside its own ParamSpec (and an enum is one of its choices) // 2. rendering a note (or, for the reverb, a test signal) is AUDIBLE // 3. ...and stays under the clipping ceiling, and finite throughout // // The thresholds are deliberately generous: this is a net under the rules, not a // taste test. It prints the measured spread per device so a tightening or a new // device can be judged against real numbers. #include #include #include #include #include #include #include #include "echofield/GrainSources.hpp" #include "sizzle/Effect.hpp" #include "sizzle/Model.hpp" #include "sizzle/Randomizer.hpp" #include "sizzle/Specs.hpp" #include "voices/TestVoice.hpp" #include "wavetable/WaveTables.hpp" using namespace sizzle; namespace { constexpr int SR = 48000; /** Peak and RMS of a render, and whether every sample was finite. */ struct Level { float peak = 0.f; float rms = 0.f; size_t peakAt = 0; // sample index of the peak — a startup transient shows up here bool finite = true; }; Level measure(const std::vector &v) { Level l; double sum = 0; for (size_t i = 0; i < v.size(); ++i) { const float x = v[i]; if (!std::isfinite(x)) { l.finite = false; continue; } const float a = std::fabs(x); if (a > l.peak) { l.peak = a; l.peakAt = i; } sum += (double)x * x; } l.rms = v.empty() ? 0.f : (float)std::sqrt(sum / (double)v.size()); return l; } /** One held note through the instrument voice the engine would use, then its tail. */ Level playInstrument(const char *type, const Params &p, double holdSec, double tailSec) { const char *kind = std::strcmp(type, "GRAIN_CLOUD") == 0 ? "GRAIN" : "SYNTH"; TestVoiceSetup setup; setup.type = type; setup.params = &p; setup.sampleRate = SR; setup.tempoBpm = 120.f; setup.clock0 = 0.0; std::unique_ptr voice = makeTestVoice(kind, setup); if (!voice) return Level{}; const int block = 192; const int hold = (int)(holdSec * SR) / block; const int tail = (int)(tailSec * SR) / block; std::vector out; out.reserve((size_t)(hold + tail) * block); voice->blockStart(block); voice->noteOn(60, 100); for (int b = 0; b < hold; ++b) { voice->blockStart(block); for (int i = 0; i < block; ++i) out.push_back(voice->render()); } voice->noteOff(60); for (int b = 0; b < tail; ++b) { voice->blockStart(block); for (int i = 0; i < block; ++i) out.push_back(voice->render()); } return measure(out); } /** A test signal through the effect: a decaying saw burst, noise, then silence for * the tail — the same shape the golden scenarios use. */ Level playEffect(const char *type, const Params &p) { std::unique_ptr fx = Effect::create(type, SR); if (!fx) return Level{}; fx->update(p, 120.f); const int n = (int)(2.0 * SR); std::vector out; out.reserve((size_t)n); uint32_t seed = 0x1234567u; for (int i = 0; i < n; ++i) { const double t = (double)i / SR; float x = 0.f; if (t < 0.6) { x = (float)(0.6 * (2.0 * std::fmod(t * 110.0, 1.0) - 1.0) * std::exp(-t * 4.0)); } else if (t < 1.0) { seed = seed * 1103515245u + 12345u; x = (float)((seed >> 8) & 0xFFFF) / 65535.f * 0.5f - 0.25f; } out.push_back(fx->process(x)); } return measure(out); } /** Every value the randomizer wrote must belong to the device and sit in range. */ bool checkRanges(const DeviceSpec &dev, const Params &p, std::string &why) { for (int i = 0; i < p.size(); ++i) { const char *key = p.keyAt(i); const ParamSpec *ps = nullptr; for (int k = 0; k < dev.paramCount; ++k) if (std::strcmp(dev.params[k].key, key) == 0) { ps = &dev.params[k]; break; } if (!ps) { why = std::string("unknown key ") + key; return false; } if (ps->isEnum()) { bool found = false; for (int c = 0; c < ps->choiceCount(); ++c) if (std::strcmp(ps->choices[c], p.valueAt(i)) == 0) { found = true; break; } if (!found) { why = std::string(key) + " = '" + p.valueAt(i) + "' is not one of its choices"; return false; } } else { const float v = p.f(key, ps->def); if (!std::isfinite(v) || v < ps->min - 1e-4f || v > ps->max + 1e-4f) { why = std::string(key) + " = " + std::to_string(v) + " outside [" + std::to_string(ps->min) + ", " + std::to_string(ps->max) + "]"; return false; } } } return true; } struct Device { const char *type; double hold, tail; // instruments only float minPeak; // below this, nobody would hear it float maxPeak; // above this, the patch is into the limiter }; // The floors and ceilings are per device because the voices do not share a gain // structure: SynthVoice ends in OUT_GAIN 0.5, the Grain Cloud auto-levels itself // near RMS 0.126, and the reverb is measured wet+dry at its own levels. Each floor // sits at roughly half the quietest roll measured over 400 seeds — loose enough not // to flake on a seed nobody has drawn yet, tight enough that a rule sliding toward // silence (a filter parked off the note, a layer budget scaled too hard) fails here. const Device kDevices[] = { {"NES_SYNTH", 1.5, 1.0, 0.060f, 1.0f}, {"MORPH_PAD", 3.0, 1.5, 0.012f, 1.0f}, // a pad is genuinely the quietest of these {"GRAIN_CLOUD", 3.0, 1.0, 0.015f, 1.0f}, // the cloud's auto-level needs a moment to come up {"WAVETABLE", 1.5, 1.0, 0.030f, 1.0f}, {"AMBIENCE", 0.0, 0.0, 0.200f, 1.0f}, }; } // namespace /** `randomizer_test --dump [key=value ...]`: what that roll actually * set, and what it measured. The overrides make it a probe — re-render the same * patch with one value changed to find which one is responsible for a bad peak. */ int dumpOne(const char *type, uint32_t seed, int nOverride, char **override) { const DeviceSpec *dev = findDevice(type); if (!dev) { std::printf("no such device: %s\n", type); return 2; } Params p; if (!model::randomize(type, seed, p)) { std::printf("%s is not randomizable\n", type); return 2; } for (int i = 0; i < nOverride; ++i) { const char *eq = std::strchr(override[i], '='); if (!eq) continue; const std::string key(override[i], eq - override[i]); p.set(key.c_str(), eq + 1); std::printf(" (override %s = %s)\n", key.c_str(), eq + 1); } std::printf("%s seed %u -- \"%s\"\n", type, seed, model::randomName(type, seed).c_str()); for (int i = 0; i < p.size(); ++i) std::printf(" %-12s %s\n", p.keyAt(i), p.valueAt(i)); for (const Device &d : kDevices) { if (std::strcmp(d.type, type) != 0) continue; const Level l = dev->kind == Kind::Instrument ? playInstrument(type, p, d.hold, d.tail) : playEffect(type, p); std::printf(" -> peak %.4f at %.3f s rms %.5f %s\n", l.peak, (double)l.peakAt / SR, l.rms, l.finite ? "finite" : "NON-FINITE"); } return 0; } int main(int argc, char **argv) { if (argc > 3 && std::strcmp(argv[1], "--dump") == 0) { wavetable::WaveTables::prewarmBlocking(); echofield::GrainSources::prewarmBlocking(); return dumpOne(argv[2], (uint32_t)std::strtoul(argv[3], nullptr, 10), argc - 4, argv + 4); } const int seeds = argc > 1 ? std::atoi(argv[1]) : 200; wavetable::WaveTables::prewarmBlocking(); echofield::GrainSources::prewarmBlocking(); int pass = 0, fail = 0; std::printf("randomizer: %d seeds per device\n", seeds); std::printf(" %-13s %9s %9s %9s %9s %7s\n", "device", "peak min", "peak med", "peak max", "rms min", "result"); for (const Device &d : kDevices) { const DeviceSpec *dev = findDevice(d.type); if (!dev) { std::printf(" FAIL %s: no device spec\n", d.type); ++fail; continue; } if (!model::canRandomize(d.type)) { std::printf(" FAIL %s: not randomizable\n", d.type); ++fail; continue; } std::vector peaks; peaks.reserve((size_t)seeds); float worstRms = 1e9f; float worstPeak = 1e9f; uint32_t worstSeed = 0; int bad = 0; std::string firstProblem; for (int s = 0; s < seeds; ++s) { const uint32_t seed = (uint32_t)(s * 2654435761u + 12345u); Params p; if (!model::randomize(d.type, seed, p)) { if (firstProblem.empty()) firstProblem = "randomize() returned false"; ++bad; continue; } std::string why; if (!checkRanges(*dev, p, why)) { if (firstProblem.empty()) firstProblem = "seed " + std::to_string(seed) + ": " + why; ++bad; continue; } const Level l = dev->kind == Kind::Instrument ? playInstrument(d.type, p, d.hold, d.tail) : playEffect(d.type, p); peaks.push_back(l.peak); if (l.rms < worstRms) worstRms = l.rms; if (l.peak < worstPeak) { worstPeak = l.peak; worstSeed = seed; } const char *problem = nullptr; if (!l.finite) problem = "non-finite output"; else if (l.peak < d.minPeak) problem = "inaudible"; else if (l.peak > d.maxPeak) problem = "clipping"; if (problem) { if (firstProblem.empty()) firstProblem = "seed " + std::to_string(seed) + ": " + problem + " (peak " + std::to_string(l.peak) + ", rms " + std::to_string(l.rms) + ")"; ++bad; } } std::sort(peaks.begin(), peaks.end()); const float lo = peaks.empty() ? 0.f : peaks.front(); const float mid = peaks.empty() ? 0.f : peaks[peaks.size() / 2]; const float hi = peaks.empty() ? 0.f : peaks.back(); std::printf(" %-13s %9.4f %9.4f %9.4f %9.5f %7s quietest seed %u\n", d.type, lo, mid, hi, worstRms, bad == 0 ? "PASS" : "FAIL", worstSeed); if (bad != 0) { std::printf(" %d/%d bad; first: %s\n", bad, seeds, firstProblem.c_str()); ++fail; } else { ++pass; } } std::printf("randomizer: %d passed, %d failed\n", pass, fail); return fail == 0 ? 0 : 1; }