// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // io::PresetBundle both ways with the Android app: // argv[1]: a Sampler bundle Android's PresetLibrary wrote (IosGoldenPresetBundle; // core/golden/bundles/android_sampler.zip), imported here; // argv[2]: app/src/test/resources/presetbundle/ios_sampler.zip, the bundle this // code writes, which Android's PresetBundleInteropTest imports. Checked // byte for byte here (`--write` regenerates it). // Both hold the same kit: pad 0 = 100 frames at 44.1 kHz (start 0.25), pad 3 = 200 // frames at 22.05 kHz. #include "io/PresetBundle.hpp" #include "io/PresetIo.hpp" #include "sampler/SampleStore.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::vector readFile(const char *path) { std::ifstream f(path, std::ios::binary); return std::vector((std::istreambuf_iterator(f)), {}); } static Sample ramp(int frames, int rate) { std::vector d((size_t)frames); for (int i = 0; i < frames; ++i) d[(size_t)i] = (float)i / (float)frames - 0.5f; return Sample(std::move(d), rate); } /// Imports [zip] and checks it is the kit; samples kept by file name. static void checkKit(const std::vector &zip, const char *what) { std::map kept; model::ToolboxSlot slot; slot.fill("FILTER"); // the bundle's type replaces it const bool ok = io::PresetBundle::importInto(slot, zip.data(), zip.size(), [&](const std::string &file, Sample s) { kept[file] = std::move(s); return "id:" + file; }); std::printf(" %s: %s\n", what, ok ? "imported" : "NOT imported"); CHECK(ok && slot.type == "SAMPLER"); const std::string p0 = slot.s("pad0", ""), p3 = slot.s("pad3", ""); CHECK(p0.rfind("id:", 0) == 0 && p3.rfind("id:", 0) == 0 && !*slot.s("pad1", "")); CHECK(kept.count(p0.substr(3)) && kept[p0.substr(3)].data.size() == 100 && kept[p0.substr(3)].sampleRate == 44100); CHECK(kept.count(p3.substr(3)) && kept[p3.substr(3)].data.size() == 200 && kept[p3.substr(3)].sampleRate == 22050); CHECK(slot.f("pad0S", -1) == 0.25f); } int main(int argc, char **argv) { if (argc < 3) { std::printf("usage: bundle_test [--write]\n"); return 2; } // The kit as an iOS slot: pads name sample ids. std::map store; store["kick#1"] = ramp(100, 44100); store["snare#2"] = ramp(200, 22050); model::ToolboxSlot slot; slot.fill("SAMPLER"); slot.name = "Kit"; slot.set("pad0", "kick#1"); slot.set("pad0S", 0.25f); slot.set("pad3", "snare#2"); const auto zip = io::PresetBundle::exportFrom(slot, "Kit", [&](const std::string &id) { auto it = store.find(id); return it == store.end() ? std::vector() : sampler::SampleStore::encodeWav(it->second); }); checkKit(zip, "own bundle"); if (argc > 3 && !std::strcmp(argv[3], "--write")) { std::ofstream(argv[2], std::ios::binary).write((const char *)zip.data(), (std::streamsize)zip.size()); std::printf(" wrote %s\n", argv[2]); } else { CHECK(readFile(argv[2]) == zip); // the fixture Android imports is what this code writes } if (std::strcmp(argv[1], "-")) { const auto android = readFile(argv[1]); if (android.empty()) std::printf(" (no Android bundle at %s: run `make golden`)\n", argv[1]); else checkKit(android, "Android bundle"); } std::printf(failures ? "bundle_test: %d failure(s)\n" : "bundle_test: ok\n", failures); return failures ? 1 : 0; }