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>
This commit is contained in:
Reactorcoremeltdown
2026-11-16 16:39:33 +01:00
co-authored by Claude Opus 5.5
parent 6688718bed
commit 5240019b47
14 changed files with 417 additions and 5 deletions
+100
View File
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#include "io/PresetBundle.hpp"
#include <map>
#include "io/PresetIo.hpp"
#include "io/Zip.hpp"
#include "sampler/SampleStore.hpp"
namespace sizzle::io {
namespace {
constexpr int PADS = 16;
std::string baseNameOf(const std::string &path) {
const size_t slash = path.find_last_of("/\\");
return slash == std::string::npos ? path : path.substr(slash + 1);
}
bool endsWith(const std::string &s, const char *suffix) {
const size_t n = std::char_traits<char>::length(suffix);
return s.size() >= n && s.compare(s.size() - n, n, suffix) == 0;
}
} // namespace
bool PresetBundle::carriesSamples(const std::string &type) {
return type == "SAMPLER" || type == "SOUNDFONT" || type == "GRAIN_CLOUD" || type == "WAVETABLE";
}
bool PresetBundle::importInto(model::ToolboxSlot &slot, const uint8_t *zipBytes, size_t size,
const std::function<std::string(const std::string &, Sample)> &keep) {
std::vector<zip::Entry> entries;
if (!zip::read(zipBytes, size, entries, 64ull << 20)) return false; // ZipLimits.forBundle
std::string preset;
bool havePreset = false;
std::map<std::string, const std::vector<uint8_t> *> files;
for (const auto &e : entries) {
const std::string name = baseNameOf(e.name);
if (name.empty() || e.name.rfind("__MACOSX", 0) == 0 || name.rfind("._", 0) == 0) continue;
if (!havePreset && endsWith(name, ".szp")) { preset.assign(e.data.begin(), e.data.end()); havePreset = true; }
else files.emplace(name, &e.data);
}
if (!havePreset) return false;
model::ToolboxSlot fresh = slot;
if (!PresetIo::importText(fresh, preset)) return false;
auto resolve = [&](const char *fileKey, const char *idKey) {
const std::string ref = fresh.s(fileKey, "");
if (ref.empty()) return;
auto it = files.find(baseNameOf(ref));
if (it == files.end()) return;
auto sample = sampler::SampleStore::decodeWav(it->second->data(), it->second->size());
if (!sample) return;
const std::string id = keep(it->first, std::move(*sample));
if (!id.empty()) fresh.set(idKey, id.c_str());
};
if (fresh.type == "SAMPLER") {
for (int p = 0; p < PADS; ++p) {
const std::string pad = "pad" + std::to_string(p);
resolve((pad + "File").c_str(), pad.c_str());
}
} else if (carriesSamples(fresh.type)) {
resolve("sfFile", "sfSample");
}
slot = fresh;
return true;
}
std::vector<uint8_t> PresetBundle::exportFrom(const model::ToolboxSlot &slot, const std::string &baseName,
const std::function<std::vector<uint8_t>(const std::string &)> &wavOf) {
model::ToolboxSlot copy = slot;
std::vector<zip::Entry> entries;
auto carry = [&](const char *idKey, const char *fileKey, const std::string &fileName) {
const std::string id = copy.s(idKey, "");
copy.values.remove(fileKey);
if (id.empty()) return;
std::vector<uint8_t> wav = wavOf(id);
copy.values.remove(idKey); // a sample id means nothing on another device
if (wav.empty()) return;
copy.set(fileKey, fileName.c_str());
entries.push_back({fileName, std::move(wav)});
};
if (copy.type == "SAMPLER") {
for (int p = 0; p < PADS; ++p) {
const std::string pad = "pad" + std::to_string(p);
carry(pad.c_str(), (pad + "File").c_str(), baseName + "_" + pad + ".wav");
}
} else if (carriesSamples(copy.type)) {
carry("sfSample", "sfFile", baseName + "_sf.wav");
}
const std::string text = PresetIo::exportText(copy);
entries.insert(entries.begin(), zip::Entry{baseName + ".szp", std::vector<uint8_t>(text.begin(), text.end())});
return zip::write(entries);
}
} // namespace sizzle::io
+52
View File
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// Preset bundles: a device preset with its own sounds, as the Android app shares
// Sampler, SF2/XI, Grain Cloud and Wavetable presets (PresetLibrary.bundleForShare
// / importZip). A ZIP holding `<name>.szp` (PresetIo text) and the WAVs it names:
//
// Kick.szp ... pad0File=Kick_pad0.wav, pad0S=..., pad0E=...
// Kick_pad0.wav
// Pad_sf.wav (sfFile=Pad_sf.wav for SF2/XI, Grain Cloud, Wavetable)
//
// In a song a slot names its sounds by sample id (`padN`, `sfSample`); in a bundle
// by file (`padNFile`, `sfFile`). These functions translate between the two.
#pragma once
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "sizzle/Model.hpp"
#include "sizzle/Sample.hpp"
namespace sizzle::io {
class PresetBundle {
public:
/** Device types whose presets travel as bundles. */
static bool carriesSamples(const std::string &type);
/**
* Import bundle [zip] into [slot] (which takes the preset's device type, as
* PresetIo::importText). Every WAV the preset names is decoded and handed to
* [keep] (its file name, the sample), which stores it and returns its sample id;
* the slot then plays it (`padN` / `sfSample`). False if [zip] isn't a ZIP or
* holds no valid preset (the slot is then unchanged). The first `.szp` wins;
* macOS `__MACOSX/` and `._` entries are skipped, and only base names count.
*/
static bool importInto(model::ToolboxSlot &slot, const uint8_t *zip, size_t size,
const std::function<std::string(const std::string &file, Sample sample)> &keep);
/**
* [slot] as a bundle named [baseName] (`<baseName>.szp` and its WAVs). [wavOf]
* gives the WAV file bytes of a sample id (empty if unknown: that sound is left
* out). The preset names files instead of sample ids, as Android writes them.
*/
static std::vector<uint8_t> exportFrom(const model::ToolboxSlot &slot, const std::string &baseName,
const std::function<std::vector<uint8_t>(const std::string &id)> &wavOf);
};
} // namespace sizzle::io
+91
View File
@@ -0,0 +1,91 @@
// 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;
}