// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // Renders the whole songs exported by IosGoldenSongs (Kotlin) through the C++ // engine and compares with AudioEngine's output sample by sample: // Engine(sr) -> setProject -> play() -> fillBlock(buf, block) repeated. // // engine_test /songs [SONG ...] // // PASS when the largest difference over the whole render is below 1e-4. Prints the // max diff and the first sample (frame, channel) that differs by more than 1e-6. #include #include #include #include #include #include #include #include #include "echofield/GrainSources.hpp" #include "sampler/SampleStore.hpp" #include "sizzle/Engine.hpp" #include "sizzle/Model.hpp" #include "wavetable/WaveTables.hpp" #if __has_include("io/ProjectIo.hpp") #include "io/ProjectIo.hpp" #define HAVE_PROJECT_IO 1 #else #define HAVE_PROJECT_IO 0 #endif 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; } static std::string readText(const std::string &path) { std::ifstream f(path, std::ios::binary); return std::string((std::istreambuf_iterator(f)), {}); } #if !HAVE_PROJECT_IO // TEMPORARY: a minimal ProjectIo.loadInto for the .szg fields the engine reads, until // core/src/io/ProjectIo.hpp exists (then the real loader is used automatically). namespace tmpio { static std::vector split(const std::string &s, char c) { std::vector out; std::string cur; for (char ch : s) { if (ch == c) { out.push_back(cur); cur.clear(); } else cur += ch; } out.push_back(cur); return out; } static std::string attr(const std::string &line, const std::string &key) { std::string inner = line.substr(1, line.size() - 2); for (const auto &tok : split(inner, ' ')) { size_t eq = tok.find('='); if (eq != std::string::npos && tok.substr(0, eq) == key) return tok.substr(eq + 1); } return ""; } static int toInt(const std::string &s, int fb) { try { return std::stoi(s); } catch (...) { return fb; } } static void region(model::LoopRegion &r, const std::string &line) { r.enabled = attr(line, "en") == "1"; r.start = toInt(attr(line, "s"), 0); r.end = toInt(attr(line, "e"), 0); r.repeats = toInt(attr(line, "r"), 2); } static void loadInto(model::Project &p, const std::string &text) { p.patterns.clear(); for (auto &lane : p.arrangement.slots) std::fill(lane.begin(), lane.end(), model::Arrangement::EMPTY); p.arrangement.laneMuted.fill(false); for (auto &s : p.toolbox) s.clearSlot(); for (auto &ch : p.mixer.channels) { ch.instrumentSlot = -1; ch.fxSlots.fill(-1); ch.midiChannel = 0; ch.volume = 0.8f; ch.mute = ch.solo = ch.limiter = false; } std::string section; model::Pattern *cur = nullptr; std::istringstream in(text); std::string line; while (std::getline(in, line)) { while (!line.empty() && (line.back() == '\r' || line.back() == ' ')) line.pop_back(); if (line.empty() || line.rfind("SIZZLETRACKER", 0) == 0) continue; if (line.rfind("[PATTERN", 0) == 0) { p.patterns.emplace_back(toInt(attr(line, "id"), (int)p.patterns.size()), attr(line, "name"), toInt(attr(line, "len"), 16)); cur = &p.patterns.back(); section = "PATTERN"; } else if (line.rfind("[ARRANGEMENT", 0) == 0) { p.arrangement.lengthBars = toInt(attr(line, "bars"), model::Arrangement::MAX_BARS); p.arrangement.loopEnabled = attr(line, "loop") == "1"; std::string m = attr(line, "mutes"); for (size_t i = 0; i < m.size() && i < p.arrangement.laneMuted.size(); ++i) p.arrangement.laneMuted[i] = m[i] == '1'; section = "ARRANGEMENT"; } else if (line.rfind("[REGION A", 0) == 0) region(p.arrangement.regionA, line); else if (line.rfind("[REGION B", 0) == 0) region(p.arrangement.regionB, line); else if (line.rfind("[MIXER]", 0) == 0) section = "MIXER"; else if (line.rfind("[TOOLBOX]", 0) == 0) section = "TOOLBOX"; else if (section.empty()) { size_t eq = line.find('='); if (eq == std::string::npos) continue; std::string k = line.substr(0, eq), v = line.substr(eq + 1); if (k == "NAME") p.name = v; else if (k == "TEMPO") p.tempoBpm = std::stof(v); else if (k == "SIG") { for (int i = 0; i < model::TIME_SIGNATURE_COUNT; ++i) if (v == model::info((model::TimeSignature)i).name) p.timeSignature = (model::TimeSignature)i; } else if (k == "ROOT") p.rootNote = toInt(v, 0); else if (k == "ACTIVE") p.activePatternId = toInt(v, 0); } else if (section == "PATTERN") { auto f = split(line, ','); if (f.size() < 5 || !cur) continue; int t = toInt(f[0], -1), ln = toInt(f[1], -1); if (t < 0 || t >= model::Pattern::TRACK_COUNT || ln < 0 || ln >= cur->length) continue; auto &c = cur->cell(t, ln); c.note = toInt(f[2], model::Cell::EMPTY); c.velocity = std::clamp(toInt(f[3], 127), 0, 127); c.channel = std::clamp(toInt(f[4], 1), 1, 16); } else if (section == "ARRANGEMENT") { auto f = split(line, ','); if (f.size() >= 3) p.arrangement.set(toInt(f[0], 0), toInt(f[1], 0), toInt(f[2], 0)); } else if (section == "MIXER") { auto f = split(line, ','); if (f.size() < 10) continue; auto &ch = p.mixer.channels[toInt(f[0], 0)]; ch.instrumentSlot = toInt(f[1], -1); for (int i = 0; i < 4; ++i) ch.fxSlots[i] = toInt(f[2 + i], -1); ch.midiChannel = toInt(f[6], 0); ch.volume = std::stof(f[7]); ch.mute = f[8] == "1"; ch.solo = f[9] == "1"; ch.limiter = f.size() > 10 && f[10] == "1"; } else if (section == "TOOLBOX") { size_t eq = line.find('='); int idx = toInt(line.substr(0, eq), -1); auto parts = split(line.substr(eq + 1), ';'); if (idx < 0 || idx >= model::Project::TOOLBOX_SLOTS || parts.size() < 2) continue; auto &slot = p.toolbox[idx]; slot.fill(parts[0]); slot.name = parts[1]; if (parts.size() >= 3 && !parts[2].empty()) for (const auto &kv : split(parts[2], ',')) { size_t e = kv.find('='); if (e != std::string::npos) slot.set(kv.substr(0, e).c_str(), kv.substr(e + 1).c_str()); } } } for (int id = 0; id < model::Arrangement::LANE_COUNT; ++id) { bool has = false; for (auto &pt : p.patterns) has |= pt.id == id; if (!has) p.patterns.emplace_back(id, "BLK" + std::to_string(id + 1), 16); } std::sort(p.patterns.begin(), p.patterns.end(), [](const model::Pattern &a, const model::Pattern &b) { return a.id < b.id; }); p.activePatternId = std::clamp(p.activePatternId, 0, (int)p.patterns.size() - 1); } } // namespace tmpio #endif static void loadProject(model::Project &p, const std::string &text) { #if HAVE_PROJECT_IO io::ProjectIo::loadInto(p, text); #else tmpio::loadInto(p, text); #endif } int main(int argc, char **argv) { if (argc < 2) { std::fprintf(stderr, "usage: %s [SONG ...]\n", argv[0]); return 2; } std::string dir = argv[1]; std::vector songs(argv + 2, argv + argc); if (songs.empty()) songs = {"demo", "mix", "waltz", "midifx"}; wavetable::WaveTables::prewarmBlocking(); echofield::GrainSources::prewarmBlocking(); #if !HAVE_PROJECT_IO std::printf("note: io/ProjectIo.hpp not found, using the test's temporary .szg loader\n"); #endif int pass = 0, fail = 0; for (const auto &song : songs) { std::ifstream mf(dir + "/" + song + ".meta"); if (!mf) { std::printf("FAIL %-10s no %s.meta\n", song.c_str(), song.c_str()); ++fail; continue; } int sr = 48000, block = 256; double seconds = 0; engine::EngineGlobals g; std::string sampleId; int smpRate = 0, loopStart = -1, loopEnd = -1; std::string line; while (std::getline(mf, line)) { std::istringstream ls(line); std::string tag; ls >> tag; if (tag == "sr") ls >> sr; else if (tag == "block") ls >> block; else if (tag == "seconds") ls >> seconds; else if (tag == "clock") { std::string v; ls >> v; g.clockSeconds = std::strtod(v.c_str(), nullptr); } else if (tag == "seeds") { long long f, gr; ls >> f >> gr; g.frasilSeeds = (uint32_t)f; g.grainSeeds = (uint32_t)gr; } else if (tag == "sample") ls >> sampleId >> smpRate >> loopStart >> loopEnd; } if (seconds <= 0) continue; sampler::SampleStore store; if (!sampleId.empty()) store.put(sampleId, Sample(readF32(dir + "/" + song + ".smp.f32"), smpRate, loopStart, loopEnd)); model::Project project; loadProject(project, readText(dir + "/" + song + ".szg")); std::vector want = readF32(dir + "/" + song + ".render.f32"); const int frames = (int)(seconds * sr) / block * block; engine::Engine eng(sr, &store, g); eng.setProject(&project); eng.play(); std::vector buf((size_t)block * 2); double maxErr = (size_t)frames * 2 == want.size() ? 0 : 1e9; size_t maxAt = 0, firstAt = (size_t)-1; for (int b = 0; b < frames / block; ++b) { eng.fillBlock(buf.data(), block); for (int k = 0; k < block * 2; ++k) { const size_t i = (size_t)b * block * 2 + k; if (i >= want.size()) break; const double e = std::fabs((double)buf[k] - (double)want[i]); if (!(e <= maxErr)) { maxErr = e; maxAt = i; } if (firstAt == (size_t)-1 && !(e <= 1e-6)) firstAt = i; } } const bool ok = maxErr < 1e-4; std::printf("%s %-10s max diff %.3g at frame %zu", ok ? "PASS" : "FAIL", song.c_str(), maxErr, maxAt / 2); if (firstAt != (size_t)-1) std::printf(", first > 1e-6 at frame %zu ch %zu (%.3f s)", firstAt / 2, firstAt % 2, (double)(firstAt / 2) / sr); std::printf("\n"); ok ? ++pass : ++fail; } std::printf("engine: %d passed, %d failed\n", pass, fail); return fail == 0 ? 0 : 1; }