diff --git a/CHANGELOG.md b/CHANGELOG.md index f1b8488..1fa8b0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ version in `versions.properties`; versions before 0.28.0 are described in the gi log (one commit per version). Since 0.37.0 Android and iOS share one version and one feature set; the iOS side of each version is in `ios/CHANGELOG.md`. +## 0.39.1 (versionCode 74) + +- Sampler, SF2/XI, Grain Cloud and Wavetable preset bundles now travel between + Android and iOS in both directions (the fix is on the iOS side; Android's + bundle format is unchanged and is now checked against the iOS code by tests). + ## 0.39.0 (versionCode 73) - **MIDI mappings as files:** the ⇄ MIDI panel of every toolbox device has diff --git a/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/IosGoldenPresetBundle.kt b/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/IosGoldenPresetBundle.kt new file mode 100644 index 0000000..e1702b7 --- /dev/null +++ b/app/src/test/java/cc/tiredsysadmin/android/sizzle/audio/IosGoldenPresetBundle.kt @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown +// SPDX-License-Identifier: GPL-3.0-or-later + +package cc.tiredsysadmin.android.sizzle.audio + +import cc.tiredsysadmin.android.sizzle.io.PresetLibrary +import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot +import cc.tiredsysadmin.android.sizzle.model.ToolboxType +import org.junit.Assume.assumeTrue +import org.junit.Test +import java.io.File +import java.nio.file.Files + +/** + * Writes a Sampler preset bundle with Android's own PresetLibrary (save, then + * bundleForShare: what SHARE sends) to SIZZLE_GOLDEN_DIR/bundles/android_sampler.zip, + * for core/tests/bundle_test.cpp to import as the iOS app does. The kit: pad 0 = + * 100 frames at 44.1 kHz (start 0.25), pad 3 = 200 frames at 22.05 kHz. + * Runs only when SIZZLE_GOLDEN_DIR is set (`make -C ios golden`). + */ +class IosGoldenPresetBundle { + @Test + fun writeAndroidBundle() { + val out = System.getenv("SIZZLE_GOLDEN_DIR") + assumeTrue(out != null) + fun ramp(frames: Int, rate: Int) = SampleStore.Sample(FloatArray(frames) { it.toFloat() / frames - 0.5f }, rate) + SampleStore.put("golden-kick", ramp(100, 44_100)) + SampleStore.put("golden-snare", ramp(200, 22_050)) + val slot = ToolboxSlot(0).apply { + fill(ToolboxType.SAMPLER) + set("pad0", "golden-kick") + set("pad0S", 0.25f) + set("pad3", "golden-snare") + } + val lib = PresetLibrary(Files.createTempDirectory("presets").toFile()) + lib.save(slot, "Kit") + val zip = lib.bundleForShare(ToolboxType.SAMPLER, "Kit")!! + val dir = File(out, "bundles").apply { mkdirs() } + zip.copyTo(File(dir, "android_sampler.zip"), overwrite = true) + } +} diff --git a/app/src/test/java/cc/tiredsysadmin/android/sizzle/io/PresetBundleInteropTest.kt b/app/src/test/java/cc/tiredsysadmin/android/sizzle/io/PresetBundleInteropTest.kt new file mode 100644 index 0000000..0fea42d --- /dev/null +++ b/app/src/test/java/cc/tiredsysadmin/android/sizzle/io/PresetBundleInteropTest.kt @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown +// SPDX-License-Identifier: GPL-3.0-or-later + +package cc.tiredsysadmin.android.sizzle.io + +import cc.tiredsysadmin.android.sizzle.audio.SampleStore +import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot +import cc.tiredsysadmin.android.sizzle.model.ToolboxType +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import java.nio.file.Files + +/** + * A Sampler bundle written by the iOS app (core/src/io/PresetBundle, the fixture + * core/tests/bundle_test.cpp checks byte for byte) imports here like Android's own: + * pad 0 = 100 frames at 44.1 kHz starting at 0.25, pad 3 = 200 frames at 22.05 kHz. + */ +class PresetBundleInteropTest { + @Test + fun importsTheIosBundle() { + val bytes = javaClass.getResource("/presetbundle/ios_sampler.zip")!!.readBytes() + val lib = PresetLibrary(Files.createTempDirectory("presets").toFile()) + val slot = ToolboxSlot(0).apply { fill(ToolboxType.FILTER) } + assertTrue(lib.importZip(slot, bytes.inputStream())) + assertEquals(ToolboxType.SAMPLER, slot.type) + // (No name check: Android's PresetIo.import replaces the name with the device's, for its own bundles too.) + val pad0 = SampleStore.get(slot.string("pad0"))!! + val pad3 = SampleStore.get(slot.string("pad3"))!! + assertEquals(100, pad0.data.size) + assertEquals(44_100, pad0.sampleRate) + assertEquals(200, pad3.data.size) + assertEquals(22_050, pad3.sampleRate) + assertEquals(0.25f, slot.float("pad0S")) + assertEquals("", slot.string("pad1")) + } +} diff --git a/app/src/test/resources/presetbundle/ios_sampler.zip b/app/src/test/resources/presetbundle/ios_sampler.zip new file mode 100644 index 0000000..bdf41c2 Binary files /dev/null and b/app/src/test/resources/presetbundle/ios_sampler.zip differ diff --git a/core/src/io/PresetBundle.cpp b/core/src/io/PresetBundle.cpp new file mode 100644 index 0000000..3e9b142 --- /dev/null +++ b/core/src/io/PresetBundle.cpp @@ -0,0 +1,100 @@ +// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "io/PresetBundle.hpp" + +#include + +#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::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 &keep) { + std::vector entries; + if (!zip::read(zipBytes, size, entries, 64ull << 20)) return false; // ZipLimits.forBundle + std::string preset; + bool havePreset = false; + std::map *> 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 PresetBundle::exportFrom(const model::ToolboxSlot &slot, const std::string &baseName, + const std::function(const std::string &)> &wavOf) { + model::ToolboxSlot copy = slot; + std::vector 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 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(text.begin(), text.end())}); + return zip::write(entries); +} + +} // namespace sizzle::io diff --git a/core/src/io/PresetBundle.hpp b/core/src/io/PresetBundle.hpp new file mode 100644 index 0000000..c5cd8a5 --- /dev/null +++ b/core/src/io/PresetBundle.hpp @@ -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 `.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 +#include +#include +#include + +#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 &keep); + + /** + * [slot] as a bundle named [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 exportFrom(const model::ToolboxSlot &slot, const std::string &baseName, + const std::function(const std::string &id)> &wavOf); +}; + +} // namespace sizzle::io diff --git a/core/tests/bundle_test.cpp b/core/tests/bundle_test.cpp new file mode 100644 index 0000000..d6563ee --- /dev/null +++ b/core/tests/bundle_test.cpp @@ -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 +#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; +} diff --git a/docs/DEVELOPER_HANDOVER.md b/docs/DEVELOPER_HANDOVER.md index db905fb..cc56250 100644 --- a/docs/DEVELOPER_HANDOVER.md +++ b/docs/DEVELOPER_HANDOVER.md @@ -776,6 +776,18 @@ attack=0.01 ``` Copy/paste from the Toolbox parameter editor uses this exact text. +### Preset bundle (`.zip`) + +What SHARE sends for a Sampler, SF2/XI, Grain Cloud or Wavetable preset: a ZIP +holding `.szp` and the WAVs it names. In a song, pads name sample ids +(`padN`, `sfSample`); in a bundle they name files (`padNFile=_padN.wav`, +`sfFile=_sf.wav`). Android: `PresetLibrary.bundleForShare` / `importZip`. +iOS: `core/src/io/PresetBundle`. The two are checked against each other. +`IosGoldenPresetBundle` (part of `make -C ios golden`) writes a bundle with +Android's code for `core/tests/bundle_test.cpp` to import. `bundle_test` also +writes `app/src/test/resources/presetbundle/ios_sampler.zip` (checked byte for +byte), which `PresetBundleInteropTest` imports with Android's code. + ### MIDI mapping (`MidiMapIo`, `.szm`, 0.39.0) One device's parameter → MIDI CC mapping, exported and imported from the ⇄ MIDI diff --git a/ios/CHANGELOG.md b/ios/CHANGELOG.md index 6802c51..87eecb1 100644 --- a/ios/CHANGELOG.md +++ b/ios/CHANGELOG.md @@ -5,6 +5,14 @@ Sizzle for iOS/iPadOS (`cc.tiredsysadmin.ios.sizzle`). One entry per version in is shared with Android, continuing Android's numbering, and both apps ship the same features; the Android side of each version is in `../CHANGELOG.md`. +## 0.39.1 (build 74) + +- Fix: a Sampler preset shared from Android (a .zip with the preset and its + WAVs) failed with "Couldn't import this preset". Preset IMPORT now takes these + bundles, for the Sampler, SF2/XI loader, Grain Cloud and Wavetable, and their + sounds come along. SHARE sends the same kind of bundle, so presets with samples + also go from iOS to Android (before, they named iOS-only sample ids). + ## 0.39.0 (build 73) - MIDI mappings as files: EXPORT / IMPORT in each device's ⇄ MIDI sheet share a diff --git a/ios/Makefile b/ios/Makefile index 793ea27..21d20c7 100644 --- a/ios/Makefile +++ b/ios/Makefile @@ -86,6 +86,10 @@ build/core/midimap_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/midimap_test.cpp mkdir -p build/core $(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/midimap_test.cpp +build/core/bundle_test: $(CORE_SRC) $(CORE_HDR) ../core/tests/bundle_test.cpp + mkdir -p build/core + $(CXX) $(CXXFLAGS) -o $@ $(CORE_SRC) ../core/tests/bundle_test.cpp + build/core/midi_test: ../core/include/sizzle/Midi.hpp ../core/tests/midi_test.cpp mkdir -p build/core $(CXX) $(CXXFLAGS) -o $@ ../core/tests/midi_test.cpp @@ -102,7 +106,7 @@ zip-test: build/core/zip_test build/core/zip_test build/core/java.zip build/core/ours.zip unzip -tq build/core/ours.zip -core-test: build/core/golden_test build/core/sampler_test build/core/engine_test build/core/io_test build/core/midi_test build/core/host_test build/core/midimap_test zip-test +core-test: build/core/golden_test build/core/sampler_test build/core/engine_test build/core/io_test build/core/midi_test build/core/host_test build/core/midimap_test build/core/bundle_test zip-test @test -f $(GOLDEN)/input.f32 || $(MAKE) --no-print-directory golden build/core/golden_test $(GOLDEN) $(TYPES) build/core/sampler_test @@ -111,6 +115,7 @@ core-test: build/core/golden_test build/core/sampler_test build/core/engine_test build/core/midi_test build/core/host_test $(GOLDEN)/songs build/core/midimap_test ../app/src/test/resources/midimap/filter.szm + build/core/bundle_test $(GOLDEN)/bundles/android_sampler.zip ../app/src/test/resources/presetbundle/ios_sampler.zip # ------------------------------------------------------------------ app AU_SRC := app/FX/SizzleDeviceAU.mm app/FX/SizzleEffectAU.mm app/FX/SizzleInstrumentAU.mm app/FX/SizzleDeviceAU+Sources.mm diff --git a/ios/app/Host/Screens/Toolbox/SlotEditor.swift b/ios/app/Host/Screens/Toolbox/SlotEditor.swift index 80ce4d5..5460421 100644 --- a/ios/app/Host/Screens/Toolbox/SlotEditor.swift +++ b/ios/app/Host/Screens/Toolbox/SlotEditor.swift @@ -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 diff --git a/ios/app/Host/SizzleSession+Mix.h b/ios/app/Host/SizzleSession+Mix.h index 4035dbd..1fd2ebd 100644 --- a/ios/app/Host/SizzleSession+Mix.h +++ b/ios/app/Host/SizzleSession+Mix.h @@ -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 diff --git a/ios/app/Host/SizzleSession+Mix.mm b/ios/app/Host/SizzleSession+Mix.mm index 112e8a7..be3af61 100644 --- a/ios/app/Host/SizzleSession+Mix.mm +++ b/ios/app/Host/SizzleSession+Mix.mm @@ -9,6 +9,7 @@ #include #include #include +#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((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(); + }); + 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; diff --git a/versions.properties b/versions.properties index 8512eca..bb0bf15 100644 --- a/versions.properties +++ b/versions.properties @@ -12,8 +12,8 @@ # or polish. versionCode is monotonic and bumps by 1 on every release, # because F-Droid and the Android package manager order installs by it alone. -versionName = 0.39.0 -versionCode = 73 +versionName = 0.39.1 +versionCode = 74 # Namespace for every platform: cc.tiredsysadmin..sizzle. Android: # cc.tiredsysadmin.android.sizzle (app/), iOS/iPadOS: cc.tiredsysadmin.ios.sizzle