iOS 0.6.0: milestone 5 (recording, MIDI, input bindings, themes, bundles, backups)
- Master recording (MasterSink ring + writer thread) and the sample recorder - CoreMIDI in/out: sound module, clock send/follow, virtual "Sizzle" ports; platform-free parser/module/clock in core (midi_test) - Hardware keyboard and gamepads via GameController; rebindable keyboard, gamepad and MIDI CC actions; per-parameter MIDI learn - Colour themes (.szt), project bundles (.szgz), backup & restore; ZIP in core with zlib (zip_test against jar and unzip) - Persistent app settings; audio pauses in the background unless sound module Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
435305e42a
commit
64e21e0bcc
@@ -0,0 +1,99 @@
|
||||
// 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") } }
|
||||
/// 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") ?? [])
|
||||
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", "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["bindKeyboard"] { keyboardBindings = v }
|
||||
if let v = map["bindGamepad"] { gamepadBindings = v }
|
||||
if let v = map["bindMidi"] { midiBindings = v }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user