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:
Reactorcoremeltdown
2026-11-16 16:31:46 +01:00
co-authored by Claude Opus 5.5
parent 4d9e2328cb
commit 6688718bed
19 changed files with 580 additions and 8 deletions
+12
View File
@@ -4,6 +4,7 @@
#include "sizzle/Params.hpp"
#include <cctype>
#include <cstdio>
#include <cstring>
namespace sizzle {
@@ -38,6 +39,17 @@ void Params::set(const char *key, const char *value) {
std::snprintf(vals_[i], kValLen, "%s", value);
}
void Params::remove(const char *key) {
const int i = find(key);
if (i < 0) return;
for (int k = i; k + 1 < n_; ++k) { // keep the insertion order of the rest
std::memcpy(keys_[k], keys_[k + 1], kKeyLen);
std::memcpy(vals_[k], vals_[k + 1], kValLen);
}
--n_;
keys_[n_][0] = vals_[n_][0] = '\0';
}
void Params::set(const char *key, float value) {
// Shortest text that reads back as the same float (Kotlin stores toString()).
char buf[kValLen];
+97
View File
@@ -0,0 +1,97 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#include "io/MidiMapIo.hpp"
#include <set>
#include "io/KotlinText.hpp"
#include "sizzle/Specs.hpp"
namespace sizzle::io {
namespace {
std::string ccKey(const std::string &param) { return std::string(MidiMapIo::CC_PREFIX) + param; }
/// The CC [slot] maps [param] to, or -1 (Kotlin: string(key).toIntOrNull()).
int ccOf(const model::ToolboxSlot &slot, const std::string &param) {
int v = -1;
return kt::toIntOrNull(slot.s(ccKey(param).c_str(), ""), v) ? v : -1;
}
} // namespace
MidiMap MidiMapIo::fromSlot(const model::ToolboxSlot &slot) {
MidiMap m;
m.type = slot.type;
m.name = slot.name;
if (const DeviceSpec *spec = findDevice(slot.type.c_str())) {
for (int i = 0; i < spec->paramCount; ++i) {
const int cc = ccOf(slot, spec->params[i].key);
if (cc >= 0 && cc <= 127) m.bindings.emplace_back(spec->params[i].key, cc);
}
}
return m;
}
std::string MidiMapIo::save(const MidiMap &map) {
std::string out = std::string(HEADER) + "\n";
out += "# Sizzle MIDI mapping: parameter=CC number (0-127), one per line.\n";
out += "TYPE=" + map.type + "\n";
out += "NAME=" + map.name + "\n";
for (const auto &[key, cc] : map.bindings) out += key + "=" + std::to_string(cc) + "\n";
return out;
}
std::optional<MidiMap> MidiMapIo::parse(const std::string &text) {
MidiMap m;
bool header = false, typed = false;
std::set<std::string> keys;
std::set<int> ccs;
for (const std::string &raw : kt::lines(text)) {
const std::string line = kt::trim(raw);
if (line.empty() || line[0] == '#') continue;
if (line == HEADER) { header = true; continue; }
const size_t eq = line.find('=');
if (eq == std::string::npos || eq == 0) continue;
const std::string key = kt::trim(line.substr(0, eq)), value = kt::trim(line.substr(eq + 1));
if (key == "TYPE") { m.type = value; typed = !value.empty(); continue; }
if (key == "NAME") { m.name = value; continue; }
int cc = -1;
if (!kt::toIntOrNull(value, cc) || cc < 0 || cc > 127 || keys.count(key) || ccs.count(cc)) continue;
keys.insert(key);
ccs.insert(cc);
m.bindings.emplace_back(key, cc);
}
if (!header || !typed) return std::nullopt;
return m;
}
int MidiMapIo::apply(model::Project &project, int slot, const MidiMap &map) {
if (slot < 0 || slot >= model::Project::TOOLBOX_SLOTS) return -1;
model::ToolboxSlot &target = project.toolbox[(size_t)slot];
if (target.type != map.type) return -1;
const DeviceSpec *spec = findDevice(target.type.c_str());
if (!spec) return -1;
for (int i = 0; i < spec->paramCount; ++i) target.values.remove(ccKey(spec->params[i].key).c_str());
int applied = 0;
for (const auto &[key, cc] : map.bindings) {
bool known = false;
for (int i = 0; i < spec->paramCount && !known; ++i) known = key == spec->params[i].key;
if (!known) continue;
// One physical control drives one parameter: take the CC from wherever it was.
for (auto &s : project.toolbox) {
if (s.isEmpty()) continue;
const DeviceSpec *other = findDevice(s.type.c_str());
for (int i = 0; other && i < other->paramCount; ++i)
if (ccOf(s, other->params[i].key) == cc) s.values.remove(ccKey(other->params[i].key).c_str());
}
target.set(ccKey(key).c_str(), std::to_string(cc).c_str());
++applied;
}
++target.version;
return applied;
}
} // namespace sizzle::io
+52
View File
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// A device's MIDI mapping as plain text (.szm), the same format the Android app
// reads and writes (io/MidiMapIo.kt):
//
// 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>`.
#pragma once
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "sizzle/Model.hpp"
namespace sizzle::io {
struct MidiMap {
std::string type; // ToolboxType name
std::string name; // the slot's name when exported (informational)
std::vector<std::pair<std::string, int>> bindings; // parameter key, CC
};
class MidiMapIo {
public:
static constexpr const char *HEADER = "SIZZLE-MIDIMAP 1";
static constexpr const char *CC_PREFIX = "midicc@";
/** [slot]'s mappings, in its device's parameter order. */
static MidiMap fromSlot(const model::ToolboxSlot &slot);
static std::string save(const MidiMap &map);
/** The mapping in [text], or nothing 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). */
static std::optional<MidiMap> parse(const std::string &text);
/** Replace toolbox [slot]'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. */
static int apply(model::Project &project, int slot, const MidiMap &map);
};
} // namespace sizzle::io