Files
sizzletracker-android/core/tests/golden_test.cpp
T
ReactorcoremeltdownandClaude Opus 5 ba98ec56f4 0.41.2 (78): cleanup and optimisation round (Android and iOS)
Nothing user-visible changes, except that the Android app is half the size and
both are lighter on the battery.

R8 is on for release builds (21.5 MB -> 10.1 MB); only the two JNI classes are
pinned by name, since the app reflects over nothing of its own, and `make dist`
keeps the mapping beside the APK. The Kotlin DSP that core/ replaced in 0.37.0
was still compiled into every APK with nothing calling it — it moves to the test
source set, where it belongs as the reference the C++ is checked against — and
the golden tests' TestVoice wrappers leave core/src for core/tests/voices, so
neither app compiles them either.

Speed: channel gain, mute/solo and the FX chain are read once per audio block
instead of once per frame in Engine::fillBlock (both platforms, bit-identical —
all 61 reference scenarios still match); the transport pollers idle at 100 ms
(Android) and 10 Hz (iOS) instead of polling flat out while a song is stopped,
woken by any transport call; ProjectIo.save builds into one pre-sized buffer and
a burst of edits is coalesced into one engine sync; the Kotlin grain sources and
factory wavetables are built when their editor opens rather than at launch, where
they duplicated ~6 MB the engine already renders in core/; and Xcode builds the
DSP core at -O2, the level the golden tests measure, not -Os.

Fix: `make lint` failed on a manifest asking for ACCESS_FINE_LOCATION without
ACCESS_COARSE_LOCATION (the BLE MIDI scan on Android 8-11), so `make check`
passes again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-11-18 21:01:29 +01:00

155 lines
7.2 KiB
C++

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Renders every reference scenario exported by IosGoldenExport / IosGoldenInstruments
// (Kotlin) through the C++ core and compares sample by sample. Effect scenarios
// process input.f32; instrument scenarios ("kind" line) play their note script.
//
// golden_test <golden dir> [TYPE ...]
//
// A scenario passes when the largest difference is below 1e-4 (float rounding and
// libm vs Java Math differ by an ulp here and there). Types with no C++ port yet are
// listed as SKIP. Exit status is non-zero if any scenario fails.
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "sizzle/Effect.hpp"
#include "voices/TestVoice.hpp"
#include "wavetable/WaveTables.hpp"
using namespace sizzle;
static std::vector<float> readF32(const std::string &path) {
std::ifstream f(path, std::ios::binary);
std::vector<char> bytes((std::istreambuf_iterator<char>(f)), {});
std::vector<float> out(bytes.size() / 4);
std::memcpy(out.data(), bytes.data(), out.size() * 4);
return out;
}
int main(int argc, char **argv) {
if (argc < 2) { std::fprintf(stderr, "usage: %s <golden dir> [TYPE ...]\n", argv[0]); return 2; }
std::string dir = argv[1];
std::vector<std::string> only(argv + 2, argv + argc);
std::vector<float> input = readF32(dir + "/input.f32");
if (input.empty()) { std::fprintf(stderr, "no input.f32 in %s (run make -C ios golden)\n", dir.c_str()); return 2; }
std::vector<std::string> scns;
if (DIR *d = opendir(dir.c_str())) {
while (dirent *e = readdir(d)) {
std::string n = e->d_name;
if (n.size() > 4 && n.compare(n.size() - 4, 4, ".scn") == 0) scns.push_back(n.substr(0, n.size() - 4));
}
closedir(d);
}
std::sort(scns.begin(), scns.end());
int pass = 0, fail = 0, skip = 0;
for (const auto &base : scns) {
std::ifstream f(dir + "/" + base + ".scn");
std::string line, type, kind;
int sr = 48000, block = 256, frames = 0;
float tempo = 120.f;
double clock0 = 0;
int smpRate = 0, loopStart = -1, loopEnd = -1;
long long frasilSeeds = 0, grainSeeds = 0;
std::string table;
struct Ev { int frame; bool on; int midi, vel; };
std::vector<Ev> evs;
Params p;
while (std::getline(f, line)) {
std::istringstream ls(line);
std::string tag; ls >> tag;
if (tag == "type") ls >> type;
else if (tag == "sr") ls >> sr;
else if (tag == "tempo") ls >> tempo;
else if (tag == "kind") ls >> kind;
else if (tag == "block") ls >> block;
else if (tag == "frames") ls >> frames;
else if (tag == "clock") ls >> clock0;
else if (tag == "sample") ls >> smpRate >> loopStart >> loopEnd;
else if (tag == "seeds") ls >> frasilSeeds >> grainSeeds;
else if (tag == "table") { std::getline(ls, table); if (!table.empty() && table[0] == ' ') table.erase(0, 1); }
else if (tag == "on") { Ev e{0, true, 0, 0}; ls >> e.frame >> e.midi >> e.vel; evs.push_back(e); }
else if (tag == "off") { Ev e{0, false, 0, 0}; ls >> e.frame >> e.midi; evs.push_back(e); }
else if (tag == "p") {
std::string key, val; ls >> key; std::getline(ls, val);
if (!val.empty() && val[0] == ' ') val.erase(0, 1);
p.set(key.c_str(), val.c_str());
}
}
std::string label = kind.empty() ? type : kind + "_" + type;
if (!only.empty() && std::find(only.begin(), only.end(), type) == only.end()
&& std::find(only.begin(), only.end(), label) == only.end()) continue;
if (kind == "TABLE") {
std::vector<float> want = readF32(dir + "/" + base + ".f32"), got;
wavetable::WaveTables::prewarmBlocking();
if (const wavetable::WaveTable *t = wavetable::WaveTables::factory(table.c_str()))
for (int l = 0; l < wavetable::WaveTable::LEVELS; ++l)
got.insert(got.end(), t->data(l), t->data(l) + (size_t)t->frames * t->size(l));
double maxErr = got.size() == want.size() ? 0 : 1e9; size_t at = 0;
for (size_t i = 0; i < got.size() && i < want.size(); ++i) {
double e = std::fabs((double)got[i] - want[i]);
if (!(e <= maxErr)) { maxErr = e; at = i; }
}
bool ok = maxErr < 1e-4;
std::printf("%s %-36s max diff %.3g at %zu\n", ok ? "PASS" : "FAIL", base.c_str(), maxErr, at);
ok ? ++pass : ++fail;
continue;
}
if (!kind.empty()) {
TestVoiceSetup setup{type.c_str(), &p, sr, tempo, clock0};
setup.frasilSeeds = (uint32_t)frasilSeeds;
setup.grainSeeds = (uint32_t)grainSeeds;
std::vector<float> smp;
if (smpRate > 0) {
smp = readF32(dir + "/" + base + ".smp.f32");
setup.sample = smp.data();
setup.sampleLength = (int)smp.size();
setup.sampleRate2 = smpRate;
setup.loopStart = loopStart;
setup.loopEnd = loopEnd;
}
auto v = makeTestVoice(kind.c_str(), setup);
if (!v) { std::printf("SKIP %-36s (not ported)\n", base.c_str()); ++skip; continue; }
std::vector<float> want = readF32(dir + "/" + base + ".f32");
double maxErr = 0; size_t at = 0, next = 0;
for (int i = 0; i < frames && i < (int)want.size(); ++i) {
if (i % block == 0) v->blockStart(block);
while (next < evs.size() && evs[next].frame == i) {
const Ev &e = evs[next++];
if (e.on) v->noteOn(e.midi, e.vel); else v->noteOff(e.midi);
}
double err = std::fabs((double)v->render() - (double)want[i]);
if (!(err <= maxErr)) { maxErr = err; at = (size_t)i; }
}
bool ok = maxErr < 1e-4 && (int)want.size() == frames;
std::printf("%s %-36s max diff %.3g at sample %zu\n", ok ? "PASS" : "FAIL", base.c_str(), maxErr, at);
ok ? ++pass : ++fail;
continue;
}
auto fx = Effect::create(type.c_str(), sr);
if (!fx) { std::printf("SKIP %-36s (not ported)\n", base.c_str()); ++skip; continue; }
std::vector<float> want = readF32(dir + "/" + base + ".f32");
fx->update(p, tempo);
double maxErr = 0; size_t at = 0;
for (size_t i = 0; i < input.size() && i < want.size(); ++i) {
float y = fx->process(input[i]);
double e = std::fabs((double)y - (double)want[i]);
if (!(e <= maxErr)) { maxErr = e; at = i; } // NaN counts as worst
}
bool ok = maxErr < 1e-4 && want.size() == input.size();
std::printf("%s %-36s max diff %.3g at sample %zu\n", ok ? "PASS" : "FAIL", base.c_str(), maxErr, at);
ok ? ++pass : ++fail;
}
std::printf("\n%d passed, %d failed, %d not ported\n", pass, fail, skip);
return fail ? 1 : 0;
}