// 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 [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 #include #include #include #include #include #include #include #include #include "sizzle/Effect.hpp" #include "sizzle/TestVoice.hpp" using namespace sizzle; static std::vector readF32(const std::string &path) { std::ifstream f(path, std::ios::binary); std::vector bytes((std::istreambuf_iterator(f)), {}); std::vector 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 [TYPE ...]\n", argv[0]); return 2; } std::string dir = argv[1]; std::vector only(argv + 2, argv + argc); std::vector 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 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; struct Ev { int frame; bool on; int midi, vel; }; std::vector 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 == "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.empty()) { TestVoiceSetup setup{type.c_str(), &p, sr, tempo, clock0}; auto v = makeTestVoice(kind.c_str(), setup); if (!v) { std::printf("SKIP %-36s (not ported)\n", base.c_str()); ++skip; continue; } std::vector 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 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; }