0.39.0 (73): export and import MIDI mappings as .szm text (Android and iOS)
- SIZZLE-MIDIMAP 1 format: TYPE, NAME, one parameter=CC line each - core/src/io/MidiMapIo (iOS) and io/MidiMapIo.kt (Android), tested on one shared fixture (midimap_test, MidiMapIoTest) - EXPORT / IMPORT in each device's MIDI learn panel on both platforms - Params::remove; iOS clears a mapping by removing it - ios/Makefile regenerates the Xcode project when source folders change Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4d9e2328cb
commit
6688718bed
@@ -0,0 +1,99 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.io
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.model.Project
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
|
||||
/**
|
||||
* A device's MIDI mapping as plain text (.szm), shared between Sizzle installs
|
||||
* (Android and iOS read and write the same format; the C++ core's io/MidiMapIo):
|
||||
*
|
||||
* SIZZLE-MIDIMAP 1
|
||||
* # comments start with '#'
|
||||
* TYPE=WAVETABLE
|
||||
* NAME=Lead
|
||||
* cutoff=74
|
||||
* resonance=71
|
||||
*
|
||||
* One `parameter=CC` line per mapped parameter (CC 0..127), in the device's
|
||||
* parameter order. In the song a mapping is the slot value `midicc@<parameter>`.
|
||||
*/
|
||||
object MidiMapIo {
|
||||
const val HEADER = "SIZZLE-MIDIMAP 1"
|
||||
const val EXT = ".szm"
|
||||
const val CC_PREFIX = "midicc@"
|
||||
|
||||
data class MidiMap(val type: String, val name: String, val bindings: List<Pair<String, Int>>)
|
||||
|
||||
/** [slot]'s mappings, in its device's parameter order. */
|
||||
fun fromSlot(slot: ToolboxSlot): MidiMap = MidiMap(
|
||||
type = slot.type?.name.orEmpty(),
|
||||
name = slot.name,
|
||||
bindings = slot.type?.params.orEmpty().mapNotNull { spec ->
|
||||
slot.string(CC_PREFIX + spec.key).toIntOrNull()?.takeIf { it in 0..127 }?.let { spec.key to it }
|
||||
},
|
||||
)
|
||||
|
||||
fun save(map: MidiMap): String = buildString {
|
||||
appendLine(HEADER)
|
||||
appendLine("# Sizzle MIDI mapping: parameter=CC number (0-127), one per line.")
|
||||
appendLine("TYPE=${map.type}")
|
||||
appendLine("NAME=${map.name}")
|
||||
for ((key, cc) in map.bindings) appendLine("$key=$cc")
|
||||
}
|
||||
|
||||
/** The mapping in [text], or null if it has no header or TYPE. Lines that aren't
|
||||
* `key=CC` with CC 0..127 are skipped; a repeated key or CC keeps its first line
|
||||
* (one control drives one parameter). */
|
||||
fun parse(text: String): MidiMap? {
|
||||
var header = false
|
||||
var type = ""
|
||||
var name = ""
|
||||
val bindings = ArrayList<Pair<String, Int>>()
|
||||
val keys = HashSet<String>()
|
||||
val ccs = HashSet<Int>()
|
||||
for (raw in text.lineSequence()) {
|
||||
val line = raw.trim()
|
||||
if (line.isEmpty() || line.startsWith("#")) continue
|
||||
if (line == HEADER) { header = true; continue }
|
||||
val eq = line.indexOf('=')
|
||||
if (eq <= 0) continue
|
||||
val key = line.substring(0, eq).trim()
|
||||
val value = line.substring(eq + 1).trim()
|
||||
when (key) {
|
||||
"TYPE" -> type = value
|
||||
"NAME" -> name = value
|
||||
else -> {
|
||||
val cc = value.toIntOrNull() ?: continue
|
||||
if (cc !in 0..127 || key in keys || cc in ccs) continue
|
||||
keys += key; ccs += cc
|
||||
bindings += key to cc
|
||||
}
|
||||
}
|
||||
}
|
||||
return if (header && type.isNotEmpty()) MidiMap(type, name, bindings) else null
|
||||
}
|
||||
|
||||
/** Replace toolbox [slotIndex]'s mappings with [map]'s: only parameters its device
|
||||
* has; a CC another parameter (of any slot) used moves to this one. Returns how
|
||||
* many were applied, or -1 when [map] is for another device type. */
|
||||
fun apply(project: Project, slotIndex: Int, map: MidiMap): Int {
|
||||
val target = project.toolbox.getOrNull(slotIndex) ?: return -1
|
||||
val type = target.type ?: return -1
|
||||
if (type.name != map.type) return -1
|
||||
type.params.forEach { target.values.remove(CC_PREFIX + it.key) }
|
||||
var applied = 0
|
||||
for ((key, cc) in map.bindings) {
|
||||
if (type.params.none { it.key == key }) continue
|
||||
for (s in project.toolbox) {
|
||||
s.values.entries.removeAll { it.key.startsWith(CC_PREFIX) && it.value.toIntOrNull() == cc }
|
||||
}
|
||||
target.set(CC_PREFIX + key, cc.toString())
|
||||
applied++
|
||||
}
|
||||
|
||||
return applied
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import cc.tiredsysadmin.android.sizzle.model.Pitch
|
||||
import cc.tiredsysadmin.android.sizzle.model.Project
|
||||
import cc.tiredsysadmin.android.sizzle.model.TimeSignature
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
|
||||
import kotlin.math.roundToInt
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
@@ -711,6 +712,34 @@ class AppViewModel(
|
||||
touched()
|
||||
}
|
||||
|
||||
/** [slotIndex]'s MIDI mapping as a shareable `.szm` file (in presets/.export,
|
||||
* which the FileProvider serves), or null for an empty slot. */
|
||||
fun midiMapExportFile(slotIndex: Int): java.io.File? {
|
||||
val slot = project.toolbox.getOrNull(slotIndex)?.takeIf { !it.isEmpty } ?: return null
|
||||
val map = cc.tiredsysadmin.android.sizzle.io.MidiMapIo.fromSlot(slot)
|
||||
val dir = java.io.File(appContext.filesDir, "presets/.export").apply { mkdirs() }
|
||||
val base = slot.name.ifBlank { slot.type?.name ?: "device" }
|
||||
.replace(Regex("[^A-Za-z0-9._ -]"), "_").trim().ifBlank { "device" }
|
||||
return java.io.File(dir, "$base MIDI${cc.tiredsysadmin.android.sizzle.io.MidiMapIo.EXT}").apply {
|
||||
writeText(cc.tiredsysadmin.android.sizzle.io.MidiMapIo.save(map))
|
||||
}
|
||||
}
|
||||
|
||||
/** Replace [slotIndex]'s MIDI mapping with the `.szm` [text]; null when done,
|
||||
* else why not (for a dialog). */
|
||||
fun importMidiMap(slotIndex: Int, text: String): String? {
|
||||
val io = cc.tiredsysadmin.android.sizzle.io.MidiMapIo
|
||||
val map = io.parse(text) ?: return "This doesn't look like a Sizzle MIDI mapping (.szm)."
|
||||
val slot = project.toolbox.getOrNull(slotIndex) ?: return "No device here."
|
||||
if (slot.type?.name != map.type) {
|
||||
val want = runCatching { ToolboxType.valueOf(map.type).displayName }.getOrDefault(map.type)
|
||||
return "This mapping is for a $want, not a ${slot.type?.displayName ?: "empty slot"}."
|
||||
}
|
||||
io.apply(project, slotIndex, map)
|
||||
touched()
|
||||
return null
|
||||
}
|
||||
|
||||
/** Remove the MIDI-CC binding for [slotIndex]'s [paramKey]. */
|
||||
fun clearParamCc(slotIndex: Int, paramKey: String) {
|
||||
project.toolbox.getOrNull(slotIndex)?.values?.remove(ccKey(paramKey))
|
||||
|
||||
@@ -422,6 +422,16 @@ private fun MidiLearnPopup(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> U
|
||||
val type = slot.type ?: return
|
||||
// Always disarm learn when the popup leaves the composition (closed / editor exited).
|
||||
DisposableEffect(slot.index) { onDispose { vm.cancelParamLearn() } }
|
||||
val context = LocalContext.current
|
||||
var importMessage by remember { mutableStateOf<String?>(null) }
|
||||
// IMPORT: a .szm mapping for this device type replaces this slot's mappings.
|
||||
val mapImporter = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
|
||||
if (uri == null) return@rememberLauncherForActivityResult
|
||||
val text = runCatching {
|
||||
context.contentResolver.openInputStream(uri)?.use { it.readBytes().takeIf { b -> b.size <= 1 shl 20 } }
|
||||
}.getOrNull()?.toString(Charsets.UTF_8)
|
||||
importMessage = if (text == null) "Couldn't read the selected file." else vm.importMidiMap(slot.index, text)
|
||||
}
|
||||
|
||||
Box(
|
||||
Modifier.fillMaxSize().background(c.background.copy(alpha = 0.92f))
|
||||
@@ -440,9 +450,28 @@ private fun MidiLearnPopup(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> U
|
||||
) {
|
||||
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
|
||||
Text("MIDI LEARN — ${slot.name}", color = c.accent,
|
||||
fontFamily = FontFamily.Monospace, fontSize = 14.sp)
|
||||
Text("DONE", color = c.textDim, fontFamily = FontFamily.Monospace, fontSize = 13.sp,
|
||||
modifier = Modifier.pointerInput(Unit) { detectTapGestures { onClose() } })
|
||||
fontFamily = FontFamily.Monospace, fontSize = 14.sp, modifier = Modifier.weight(1f))
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(14.dp)) {
|
||||
// Share this device's mapping as a .szm text file / take one in.
|
||||
Text("EXPORT", color = c.accent, fontFamily = FontFamily.Monospace, fontSize = 13.sp,
|
||||
modifier = Modifier.pointerInput(Unit) {
|
||||
detectTapGestures {
|
||||
vm.midiMapExportFile(slot.index)?.let { f ->
|
||||
val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", f)
|
||||
val send = Intent(Intent.ACTION_SEND).apply {
|
||||
this.type = "text/plain"
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
context.startActivity(Intent.createChooser(send, "Share MIDI mapping"))
|
||||
}
|
||||
}
|
||||
})
|
||||
Text("IMPORT", color = c.accent, fontFamily = FontFamily.Monospace, fontSize = 13.sp,
|
||||
modifier = Modifier.pointerInput(Unit) { detectTapGestures { mapImporter.launch(arrayOf("*/*")) } })
|
||||
Text("DONE", color = c.textDim, fontFamily = FontFamily.Monospace, fontSize = 13.sp,
|
||||
modifier = Modifier.pointerInput(Unit) { detectTapGestures { onClose() } })
|
||||
}
|
||||
}
|
||||
Text(
|
||||
"Tap LEARN, then move a knob/fader on your MIDI controller.",
|
||||
@@ -481,6 +510,14 @@ private fun MidiLearnPopup(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> U
|
||||
}
|
||||
}
|
||||
}
|
||||
importMessage?.let { msg ->
|
||||
AlertDialog(
|
||||
onDismissRequest = { importMessage = null },
|
||||
title = { Text("Import failed") },
|
||||
text = { Text(msg) },
|
||||
confirmButton = { TextButton(onClick = { importMessage = null }) { Text("OK") } },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.io
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.model.Project
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
|
||||
/** The same expectations as core/tests/midimap_test.cpp, on the same fixture. */
|
||||
class MidiMapIoTest {
|
||||
private val fixture = javaClass.getResource("/midimap/filter.szm")!!.readText()
|
||||
|
||||
@Test
|
||||
fun parsesTheSharedFixture() {
|
||||
val map = MidiMapIo.parse(fixture)
|
||||
assertNotNull(map)
|
||||
assertEquals("FILTER", map!!.type)
|
||||
assertEquals("Sweep", map.name)
|
||||
assertEquals(listOf("cutoff" to 74, "resonance" to 71, "nosuchparam" to 20, "release" to 7), map.bindings)
|
||||
assertNull(MidiMapIo.parse("TYPE=FILTER\ncutoff=1\n"))
|
||||
assertNull(MidiMapIo.parse("SIZZLE-MIDIMAP 1\ncutoff=1\n"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun appliesExportsAndRoundTrips() {
|
||||
val map = MidiMapIo.parse(fixture)!!
|
||||
val p = Project()
|
||||
p.toolbox[2].fill(ToolboxType.FILTER)
|
||||
p.toolbox[5].fill(ToolboxType.BITCRUSHER)
|
||||
p.toolbox[5].set("midicc@bits", "71")
|
||||
p.toolbox[2].set("midicc@warmth", "30")
|
||||
assertEquals(-1, MidiMapIo.apply(p, 5, map))
|
||||
assertEquals(3, MidiMapIo.apply(p, 2, map))
|
||||
assertFalse("midicc@bits" in p.toolbox[5].values)
|
||||
assertFalse("midicc@warmth" in p.toolbox[2].values)
|
||||
val applied = listOf("cutoff" to 74, "resonance" to 71, "release" to 7)
|
||||
val back = MidiMapIo.fromSlot(p.toolbox[2])
|
||||
assertEquals(applied, back.bindings)
|
||||
assertEquals(applied, MidiMapIo.parse(MidiMapIo.save(back))!!.bindings)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
SIZZLE-MIDIMAP 1
|
||||
# A shared fixture: core/tests/midimap_test.cpp and MidiMapIoTest.kt read it.
|
||||
TYPE=FILTER
|
||||
NAME=Sweep
|
||||
cutoff = 74
|
||||
resonance=71
|
||||
cutoff=12
|
||||
warmth=74
|
||||
envamt=300
|
||||
nosuchparam=20
|
||||
garbage line
|
||||
decay=x
|
||||
release=7
|
||||
Reference in New Issue
Block a user