Share full projects as .szgz bundles between instances (v0.16.0)

Add Export/Import project buttons to Settings → Project. A bundle is a ZIP holding
the full .szg project plus every sample WAV it references (Sampler pads, SoundFont),
so unlike a plain .szg — whose sample ids only resolve on the originating device —
it opens complete on another Sizzletracker instance.

Export shares the bundle via the system share sheet. Import replaces the current
project, extracts the WAVs to a persistent content-addressed samples dir, re-points
each slot's padNFile/sfFile at the extracted file, then rehydrates — so the audio is
restored for the session and survives a restart. Invalid files are rejected cleanly.
Covered by ProjectBundleIoTest.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-07-21 11:45:18 +02:00
co-authored by Claude Opus 4.8
parent b305f0a2b5
commit 3c4c30bf1f
6 changed files with 268 additions and 8 deletions
@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package space.rcmd.android.sizzle.io
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import space.rcmd.android.sizzle.audio.SampleStore
import space.rcmd.android.sizzle.model.Project
import space.rcmd.android.sizzle.model.SamplerPads
import space.rcmd.android.sizzle.model.ToolboxType
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.nio.file.Files
/** A project bundle must carry the referenced sample audio and rebuild it on import. */
class ProjectBundleIoTest {
@Test fun roundTripCarriesTheSampleAndRepointsTheSlot() {
// A source project with a Sampler pad referencing a sample in the store.
val id = "src-sample-id"
SampleStore.put(id, SampleStore.Sample(FloatArray(256) { it / 256f - 0.5f }, 48_000))
val src = Project().apply {
name = "Shared Tune"
toolbox[3].fill(ToolboxType.SAMPLER)
toolbox[3].set(SamplerPads.key(0), id)
}
val bytes = ByteArrayOutputStream().also { ProjectBundleIo.write(src, it) }.toByteArray()
// Import into a fresh project + samples dir (simulating another device).
val samplesDir = Files.createTempDirectory("bundle-samples").toFile()
val dst = Project()
assertTrue(ProjectBundleIo.read(dst, ByteArrayInputStream(bytes), samplesDir))
assertEquals("Shared Tune", dst.name)
assertEquals(ToolboxType.SAMPLER, dst.toolbox[3].type)
// The pad still references the original id (resolved by rehydrate at load time)…
assertEquals(id, dst.toolbox[3].string(SamplerPads.key(0)))
// …and now carries a portable file ref to the extracted WAV, which exists and decodes.
val wavPath = dst.toolbox[3].string("pad0File")
assertTrue("pad0File points at an extracted WAV", wavPath.isNotBlank())
val wav = File(wavPath)
assertTrue("extracted WAV exists on disk", wav.exists())
val decoded = SampleStore.decodeWav(wav.readBytes())
assertNotNull("the carried sample decodes", decoded)
assertTrue(decoded!!.data.isNotEmpty())
}
@Test fun invalidBundleIsRejected() {
val dst = Project()
val samplesDir = Files.createTempDirectory("bundle-samples2").toFile()
assertFalse(ProjectBundleIo.read(dst, ByteArrayInputStream("not a zip".toByteArray()), samplesDir))
}
}