Files
sizzletracker-android/core/tests/bundle_test.cpp
T
ReactorcoremeltdownandClaude Opus 5.5 5240019b47 0.39.1 (74): preset bundles between Android and iOS, both ways
iOS couldn't import Android's Sampler preset bundle (a ZIP with the .szp and
its WAVs) and shared sample presets as bare text naming iOS sample ids.

- core/src/io/PresetBundle: import/export in Android's bundle layout
  (padNFile / sfFile), used by iOS IMPORT and SHARE
- bundle_test imports a bundle written by Android's PresetLibrary
  (IosGoldenPresetBundle); PresetBundleInteropTest imports the iOS-written
  fixture with Android's importZip

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 16:39:33 +01:00

92 lines
3.8 KiB
C++

// 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 <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <map>
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<uint8_t> readFile(const char *path) {
std::ifstream f(path, std::ios::binary);
return std::vector<uint8_t>((std::istreambuf_iterator<char>(f)), {});
}
static Sample ramp(int frames, int rate) {
std::vector<float> 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<uint8_t> &zip, const char *what) {
std::map<std::string, Sample> 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 <android_sampler.zip|-> <ios_sampler.zip> [--write]\n"); return 2; }
// The kit as an iOS slot: pads name sample ids.
std::map<std::string, Sample> 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<uint8_t>() : 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;
}