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
@@ -147,7 +147,8 @@ struct SlotEditor: View {
let name = url.deletingPathExtension().lastPathComponent
switch kind {
case .preset:
guard let text = String(data: data, encoding: .utf8), session.importPresetText(text, intoSlot: slot) else {
// Preset text, or a bundle with its sounds (how Android shares Sampler presets).
guard session.importPresetData(data, intoSlot: slot) else {
let display = SizzleSession.deviceTypes.first { $0.type == type }?.displayName ?? type
importError = "Couldn't import this preset. The file may be corrupt, or not a valid \(display) preset or bundle."
return
@@ -295,7 +296,7 @@ private struct PresetBar: View {
let name = slotName.trimmingCharacters(in: .whitespaces).isEmpty ? displayName : slotName
_ = session.savePreset(ofSlot: slot, name: name)
names = session.libraryPresets(forType: type)
guard let url = session.presetFile(forType: type, name: name) else { return }
guard let url = session.presetShareURL(forType: type, name: name) else { return }
#if canImport(UIKit)
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
let root = UIApplication.shared.connectedScenes.compactMap { ($0 as? UIWindowScene)?.keyWindow }.first?.rootViewController
+7
View File
@@ -41,6 +41,13 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)importPresetText:(NSString *)text intoSlot:(NSInteger)slot;
/// The preset's file, for sharing (nil when missing).
- (nullable NSURL *)presetFileForType:(NSString *)type name:(NSString *)name;
/// Import a picked preset file into [slot]: preset text, or a bundle (a ZIP with
/// the preset and its WAVs, as Android shares Sampler, SF2/XI, Grain Cloud and
/// Wavetable presets). Saved to the library like importPresetText.
- (BOOL)importPresetData:(NSData *)data intoSlot:(NSInteger)slot;
/// What SHARE sends for a saved preset: a bundle with its sounds (Android's
/// layout) for the device types that carry samples, else the .szp itself.
- (nullable NSURL *)presetShareURLForType:(NSString *)type name:(NSString *)name;
@end
+52
View File
@@ -9,6 +9,7 @@
#include <set>
#include <string>
#include <vector>
#include "io/PresetBundle.hpp"
#include "io/PresetIo.hpp"
#include "sizzle/DeviceSpecs.gen.hpp"
@@ -17,6 +18,7 @@ using namespace sizzle;
/// SizzleSession.mm's own: put back into the store the samples the song refers to.
@interface SizzleSession (MixPrivate)
- (void)rehydrateSamples;
- (NSURL *)sampleURL:(const std::string &)id;
@end
namespace {
@@ -268,6 +270,56 @@ NSString *sanitize(NSString *name) {
return [self savePresetOfSlot:slot name:name.length ? name : [self typeOfSlot:slot]];
}
- (BOOL)importPresetData:(NSData *)data intoSlot:(NSInteger)slot {
if (slot < 0 || slot >= model::Project::TOOLBOX_SLOTS) return NO;
if (data.length < 4 || memcmp(data.bytes, "PK\x03\x04", 4)) {
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return text && [self importPresetText:text intoSlot:slot];
}
// A bundle: each sound becomes one of the song's samples (store + Documents/samples).
__block model::ToolboxSlot fresh;
[self read:^(const model::Project &p, const engine::Engine &) { fresh = p.toolbox[(size_t)slot]; }];
sampler::SampleStore &store = [self sampleStore];
const bool ok = io::PresetBundle::importInto(fresh, (const uint8_t *)data.bytes, data.length,
[self, &store](const std::string &file, Sample sample) {
std::string stem = file.substr(0, file.rfind('.'));
std::string id = stem + "#" + NSUUID.UUID.UUIDString.UTF8String;
const auto wav = sampler::SampleStore::encodeWav(sample);
[[NSData dataWithBytes:wav.data() length:wav.size()] writeToURL:[self sampleURL:id] atomically:YES];
store.put(id, std::move(sample));
return id;
});
if (!ok) return NO;
[self edit:^(model::Project &p, engine::Engine &e) {
p.toolbox[(size_t)slot] = fresh;
++p.toolbox[(size_t)slot].version;
e.rebuildFxChains();
}];
NSString *name = [self nameOfSlot:slot];
return [self savePresetOfSlot:slot name:name.length ? name : [self typeOfSlot:slot]];
}
- (nullable NSURL *)presetShareURLForType:(NSString *)type name:(NSString *)name {
NSURL *file = [self presetFileForType:type name:name];
if (!file || !io::PresetBundle::carriesSamples(type.UTF8String)) return file;
NSString *text = [NSString stringWithContentsOfURL:file encoding:NSUTF8StringEncoding error:nil];
model::ToolboxSlot slot;
if (!text || !io::PresetIo::importText(slot, text.UTF8String)) return file;
sampler::SampleStore &store = [self sampleStore];
NSString *base = sanitize(name);
const auto zip = io::PresetBundle::exportFrom(slot, base.UTF8String, [self, &store](const std::string &id) {
if (NSData *d = [NSData dataWithContentsOfURL:[self sampleURL:id]])
return std::vector<uint8_t>((const uint8_t *)d.bytes, (const uint8_t *)d.bytes + d.length);
const Sample *s = store.get(id.c_str());
return s ? sampler::SampleStore::encodeWav(*s) : std::vector<uint8_t>();
});
NSURL *dir = [NSFileManager.defaultManager.temporaryDirectory URLByAppendingPathComponent:@"PresetShare" isDirectory:YES];
[NSFileManager.defaultManager removeItemAtURL:dir error:nil];
[NSFileManager.defaultManager createDirectoryAtURL:dir withIntermediateDirectories:YES attributes:nil error:nil];
NSURL *out = [dir URLByAppendingPathComponent:[base stringByAppendingPathExtension:@"zip"]];
return [[NSData dataWithBytes:zip.data() length:zip.size()] writeToURL:out atomically:YES] ? out : nil;
}
- (NSURL *)presetFileForType:(NSString *)type name:(NSString *)name {
NSURL *u = [self mixPresetURL:type name:name];
return [NSFileManager.defaultManager fileExistsAtPath:u.path] ? u : nil;