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>
101 lines
3.9 KiB
C++
101 lines
3.9 KiB
C++
// 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
|