- Android: BleMidi scans for the BLE MIDI service, connects through MidiManager.openBluetoothDevice, remembers and reconnects controllers; Settings › MIDI inputs › Bluetooth MIDI (Scan / Connect / Disconnect / Forget) - iOS: the system Bluetooth MIDI panel from Setup › MIDI Sync, connected controllers remembered and reconnected at launch via CoreBluetooth Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
6.6 KiB
Swift
115 lines
6.6 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import Foundation
|
|
|
|
/// App-level settings that persist across launches (Android's SettingsStore and
|
|
/// BindingStore). Song, mixer and toolbox settings live in the project instead.
|
|
/// Stored in UserDefaults; a backup carries them as a text file (`settings.txt`,
|
|
/// one `key=value` line each, see `exportText` / `importText`).
|
|
@MainActor
|
|
final class AppSettings: ObservableObject {
|
|
static let shared = AppSettings()
|
|
private let d = UserDefaults.standard
|
|
|
|
/// The colour theme's name (a built-in one or a user theme).
|
|
@Published var paletteName: String { didSet { d.set(paletteName, forKey: "palette") } }
|
|
/// FX tail captured after the sequencer stops (bars, 0..4).
|
|
@Published var recordTailBars: Int { didSet { d.set(recordTailBars, forKey: "recordTailBars") } }
|
|
/// The on-screen keyboards' MIDI channel (0 = none) and base octave.
|
|
@Published var kbdChannel: Int { didSet { d.set(kbdChannel, forKey: "kbdChannel") } }
|
|
@Published var kbdOctave: Int { didSet { d.set(kbdOctave, forKey: "kbdOctave") } }
|
|
/// The live meters inside the mixer's volume faders.
|
|
@Published var vuMeters: Bool { didSet { d.set(vuMeters, forKey: "vuMeters") } }
|
|
/// Send MIDI clock, Start/Stop and the sequenced notes to MIDI destinations.
|
|
@Published var sendMidiClock: Bool { didSet { d.set(sendMidiClock, forKey: "midiClockSend") } }
|
|
/// Follow an external MIDI clock's tempo and Start/Stop.
|
|
@Published var receiveMidiClock: Bool { didSet { d.set(receiveMidiClock, forKey: "midiClockRecv") } }
|
|
/// Keep the audio running in the background so incoming MIDI plays (sound module).
|
|
@Published var soundModule: Bool { didSet { d.set(soundModule, forKey: "soundModule") } }
|
|
/// MIDI sources the user switched off (MidiSourceKey).
|
|
@Published var midiSourcesOff: Set<String> { didSet { d.set(Array(midiSourcesOff).sorted(), forKey: "midiSourcesOff") } }
|
|
/// Bluetooth LE MIDI controllers to connect at launch: peripheral id -> name.
|
|
@Published var bleMidiDevices: [String: String] {
|
|
didSet { d.set(bleMidiDevices.map { "\($0.key)|\($0.value)" }.sorted(), forKey: "bleMidiDevices") }
|
|
}
|
|
/// Input bindings, one `code=actionId` line each (gamepad: `modifier:code=actionId`);
|
|
/// nil until the user changes them (the defaults apply).
|
|
@Published var keyboardBindings: String? { didSet { d.set(keyboardBindings, forKey: "bindKeyboard") } }
|
|
@Published var gamepadBindings: String? { didSet { d.set(gamepadBindings, forKey: "bindGamepad") } }
|
|
@Published var midiBindings: String? { didSet { d.set(midiBindings, forKey: "bindMidi") } }
|
|
|
|
private init() {
|
|
paletteName = d.string(forKey: "palette") ?? "Amber CRT"
|
|
recordTailBars = d.object(forKey: "recordTailBars") as? Int ?? 2
|
|
kbdChannel = d.object(forKey: "kbdChannel") as? Int ?? 1
|
|
kbdOctave = d.object(forKey: "kbdOctave") as? Int ?? 3
|
|
vuMeters = d.object(forKey: "vuMeters") as? Bool ?? true
|
|
sendMidiClock = d.bool(forKey: "midiClockSend")
|
|
receiveMidiClock = d.bool(forKey: "midiClockRecv")
|
|
soundModule = d.bool(forKey: "soundModule")
|
|
midiSourcesOff = Set(d.stringArray(forKey: "midiSourcesOff") ?? [])
|
|
bleMidiDevices = Dictionary((d.stringArray(forKey: "bleMidiDevices") ?? []).map { e -> (String, String) in
|
|
let parts = e.split(separator: "|", maxSplits: 1).map(String.init)
|
|
return (parts[0], parts.count > 1 ? parts[1] : parts[0])
|
|
}, uniquingKeysWith: { a, _ in a })
|
|
keyboardBindings = d.string(forKey: "bindKeyboard")
|
|
gamepadBindings = d.string(forKey: "bindGamepad")
|
|
midiBindings = d.string(forKey: "bindMidi")
|
|
}
|
|
|
|
// MARK: Backup text
|
|
|
|
private static let keys = ["palette", "recordTailBars", "kbdChannel", "kbdOctave", "vuMeters", "midiClockSend",
|
|
"midiClockRecv", "soundModule", "midiSourcesOff", "bleMidiDevices", "bindKeyboard",
|
|
"bindGamepad", "bindMidi"]
|
|
|
|
/// Every setting as `key=value` lines; multi-line values (bindings, the source
|
|
/// list) are escaped (`\n` → `\\n`).
|
|
func exportText() -> String {
|
|
var lines = ["SIZZLE-SETTINGS 1"]
|
|
for k in Self.keys {
|
|
guard let v = d.object(forKey: k) else { continue }
|
|
let text: String
|
|
switch v {
|
|
case let a as [String]: text = a.joined(separator: "\n")
|
|
case let n as NSNumber where k == "vuMeters" || k.hasPrefix("midiClock") || k == "soundModule": text = n.boolValue ? "true" : "false"
|
|
default: text = "\(v)"
|
|
}
|
|
lines.append(k + "=" + text.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\n", with: "\\n"))
|
|
}
|
|
return lines.joined(separator: "\n") + "\n"
|
|
}
|
|
|
|
/// Restore settings written by exportText (unknown keys are ignored).
|
|
func importText(_ text: String) {
|
|
var map: [String: String] = [:]
|
|
for line in text.split(separator: "\n") {
|
|
guard let eq = line.firstIndex(of: "=") else { continue }
|
|
var v = "", esc = false
|
|
for c in line[line.index(after: eq)...] {
|
|
if esc { v.append(c == "n" ? "\n" : c); esc = false } else if c == "\\" { esc = true } else { v.append(c) }
|
|
}
|
|
map[String(line[..<eq])] = v
|
|
}
|
|
if let v = map["palette"] { paletteName = v }
|
|
if let v = map["recordTailBars"].flatMap(Int.init) { recordTailBars = max(0, min(4, v)) }
|
|
if let v = map["kbdChannel"].flatMap(Int.init) { kbdChannel = max(0, min(16, v)) }
|
|
if let v = map["kbdOctave"].flatMap(Int.init) { kbdOctave = max(0, min(8, v)) }
|
|
if let v = map["vuMeters"] { vuMeters = v == "true" }
|
|
if let v = map["midiClockSend"] { sendMidiClock = v == "true" }
|
|
if let v = map["midiClockRecv"] { receiveMidiClock = v == "true" }
|
|
if let v = map["soundModule"] { soundModule = v == "true" }
|
|
if let v = map["midiSourcesOff"] { midiSourcesOff = Set(v.split(separator: "\n").map(String.init)) }
|
|
if let v = map["bleMidiDevices"] {
|
|
bleMidiDevices = Dictionary(v.split(separator: "\n").map { e -> (String, String) in
|
|
let parts = e.split(separator: "|", maxSplits: 1).map(String.init)
|
|
return (parts[0], parts.count > 1 ? parts[1] : parts[0])
|
|
}, uniquingKeysWith: { a, _ in a })
|
|
}
|
|
if let v = map["bindKeyboard"] { keyboardBindings = v }
|
|
if let v = map["bindGamepad"] { gamepadBindings = v }
|
|
if let v = map["bindMidi"] { midiBindings = v }
|
|
}
|
|
}
|