// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // host::EngineHost: renders a song exactly as the bare engine does, and a synced // project text ends up as the engine's song without stopping playback. // argv[1]: the reference songs folder (core/golden/songs). #include "host/EngineHost.hpp" #include "echofield/GrainSources.hpp" #include "io/ProjectIo.hpp" #include "wavetable/WaveTables.hpp" #include #include #include #include #include using namespace sizzle; static int failures = 0; #define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++failures; } } while (0) static std::string readFile(const std::string &path) { std::ifstream f(path, std::ios::binary); std::stringstream ss; ss << f.rdbuf(); return ss.str(); } int main(int argc, char **argv) { if (argc < 2) { std::printf("usage: host_test \n"); return 2; } // midifx: arp, transposer and the MIDI LFO (which writes its target slot as it plays). const std::string text = readFile(std::string(argv[1]) + "/midifx.szg"); const std::string plain = readFile(std::string(argv[1]) + "/mix.szg"); CHECK(!text.empty() && !plain.empty()); wavetable::WaveTables::prewarmBlocking(); // the host builds them in the background echofield::GrainSources::prewarmBlocking(); constexpr int SR = 48000, FRAMES = 48000; // 1. The host renders what the engine renders (odd buffer sizes, 192-frame blocks inside). { host::EngineHost h(SR); h.loadProjectText(text); h.rebuildArrangement(); h.rebuildFxChains(); h.play(); std::vector a(FRAMES * 2); for (int done = 0; done < FRAMES;) { const int n = std::min(384, FRAMES - done); // whole blocks: the LFO steps per block h.render(a.data() + 2 * done, n); done += n; } model::Project p; io::ProjectIo::loadInto(p, text); engine::Engine e(SR); e.setProject(&p); e.rebuildArrangement(); e.rebuildFxChains(); e.play(); std::vector b(FRAMES * 2); for (int done = 0; done < FRAMES; done += 192) e.fillBlock(b.data() + 2 * done, std::min(192, FRAMES - done)); double peak = 0, diff = 0; for (size_t i = 0; i < a.size(); ++i) { peak = std::max(peak, (double)std::fabs(a[i])); diff = std::max(diff, (double)std::fabs(a[i] - b[i])); } CHECK(peak > 0.01); CHECK(diff == 0); } // 2. Syncing an edited song changes the engine's song, playing on. { const std::string &text = plain; // no LFO rewriting a slot meanwhile host::EngineHost h(SR); h.loadProjectText(text); h.play(); std::vector buf(2 * 4800); h.render(buf.data(), 4800); model::Project p; io::ProjectIo::loadInto(p, text); p.tempoBpm = 97.f; p.patterns[0].tracks[1][3] = model::Cell{64, 90, 2}; p.mixer.channels[2].volume = 0.5f; p.toolbox[0].set("cutoff", 0.25f); p.arrangement.set(3, 5, 2); const std::string edited = io::ProjectIo::save(p); h.syncProjectText(edited); CHECK(h.isPlaying()); h.render(buf.data(), 4800); CHECK(h.projectText() == edited); h.syncProjectText(text); // and back CHECK(h.projectText() == io::ProjectIo::save([&] { model::Project q; io::ProjectIo::loadInto(q, text); return q; }())); } std::printf(failures ? "host_test: %d failure(s)\n" : "host_test: ok\n", failures); return failures ? 1 : 0; }