A RANDOM button in the preset bar of the Synth, Morph Pad, Grain Cloud, Wavetable and Ambience Reverb editors: it rolls a playable patch and names it. The rules live in core/src/model/Randomizer.cpp and are shared, so a seed gives the same patch on both platforms. Android reaches it over JNI as preset text and imports it; iOS writes the Params into the slot. Neither touches the slot's own files — a loaded sample or an imported wavetable survives a roll, because the dice are for the settings, not for the sounds. Scattering values over the parameter ranges produces silence and the occasional scream, so the rules encode three things instead: something always sounds (a layer is always up, an envelope that decays to nothing gets a body, a filter is never shut, a reverb is never fully dry); levels share a budget and resonance, drive and saturation stay out of the range where a patch screams; and tunings come from consonant intervals rather than a uniform draw. core/tests/randomizer_test.cpp is the specification: it renders every device over hundreds of seeds through the real voice and fails on anything out of range, silent, clipping or non-finite. Every bound that looks arbitrary was put there by a seed it caught — a band-pass parked above the note (peak 0.010 against a 0.3 median), a Vel -> Cutoff mod slot closing the filter (0.41 -> 0.02), a choir-only Morph Pad through its own band-pass, and MORPH TO, which a roll now leaves alone because MORPH TIME carries a held note all the way onto a factory drone at a level the randomizer never chose (1.33 with every layer of its own at zero). --dump <TYPE> <seed> [key=value ...] re-renders one roll with overrides, which is how each of those was found. Also fixes the shared engine not compiling outside Apple's toolchain: a missing <cstdint> in core/include/sizzle/Model.hpp. No change to the apps, but the engine's tests and benchmark now build with GCC too — which is how the rules above were iterated while the Mac was unreachable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
271 lines
11 KiB
C++
271 lines
11 KiB
C++
// 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 <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#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<float> &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<TestVoice> 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<float> 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<Effect> fx = Effect::create(type, SR);
|
|
if (!fx) return Level{};
|
|
fx->update(p, 120.f);
|
|
|
|
const int n = (int)(2.0 * SR);
|
|
std::vector<float> 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 <TYPE> <seed> [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<float> 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;
|
|
}
|