Files
sizzletracker-android/ios/app/FX/Editor/DeviceModel.swift
T
ReactorcoremeltdownandClaude Opus 5.5 29650b88d7 iOS milestone 3 groundwork: SizzleSession bridge, app shell with tabs, slot-backed editors, screen snapshots
SizzleSession owns the song, engine (192-frame chunks, try-lock on the audio
thread), samples and audio output; AppModel/RootView give the four tabs;
DeviceModel now works on toolbox slots too; make screen-snapshots renders the
app's tabs on the Mac.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:15:28 +01:00

104 lines
4.3 KiB
Swift

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import AudioToolbox
import Foundation
/// One parameter as the editor sees it: what Sizzle's spec says about it.
struct ParamInfo {
let key: String
let label: String
let min: Float
let max: Float
let def: Float
let isInt: Bool
let choices: [String]
var isEnum: Bool { !choices.isEmpty }
/// On/off switches stored as 0..1 numbers (the Tape Delay's "Head N On").
var isToggle: Bool { !isEnum && key.hasSuffix("On") && min == 0 && max == 1 }
}
/// The editor's view of one device, whichever backs it: an AUv3 (the plugin
/// extension, the device lab) or a toolbox slot of the open song (the app's
/// Toolbox tab). Values are numbers; choice lists by index.
@MainActor
final class DeviceModel: ObservableObject {
let toolboxType: String
let displayName: String
let params: [String: ParamInfo]
let order: [String]
let presetNames: [String]
@Published private(set) var values: [String: Float] = [:]
@Published var presetName: String?
private let write: (String, Float) -> Void
private let applyPresetNamed: (String) -> Void
private let reread: () -> [String: Float]
private var cleanup: () -> Void = {}
/// The designated initializer; the app adds a slot-backed one (Host/SlotDeviceModel.swift).
init(toolboxType: String, displayName: String, infos: [ParamInfo], values: [String: Float],
presetNames: [String], write: @escaping (String, Float) -> Void,
applyPreset: @escaping (String) -> Void, reread: @escaping () -> [String: Float]) {
self.toolboxType = toolboxType
self.displayName = displayName
self.params = Dictionary(uniqueKeysWithValues: infos.map { ($0.key, $0) })
self.order = infos.map { $0.key }
self.values = values
self.presetNames = presetNames
self.write = write
self.applyPresetNamed = applyPreset
self.reread = reread
}
deinit { cleanup() }
/// Backed by an AUv3: its parameter tree, presets and host automation.
convenience init(au: SizzleDeviceAU) {
let all = au.parameterTree?.allParameters ?? []
let infos = all.map { p in
ParamInfo(key: p.identifier, label: au.specLabel(for: p), min: p.minValue, max: p.maxValue,
def: au.defaultValue(for: p), isInt: au.isIntegerParameter(p), choices: p.valueStrings ?? [])
}
let byKey = Dictionary(uniqueKeysWithValues: all.map { ($0.identifier, $0) })
let presets = au.factoryPresets ?? []
var token: AUParameterObserverToken?
self.init(toolboxType: au.toolboxType, displayName: au.displayName, infos: infos,
values: Dictionary(uniqueKeysWithValues: all.map { ($0.identifier, $0.value) }),
presetNames: presets.map { $0.name },
write: { key, v in byKey[key]?.setValue(v, originator: token) },
applyPreset: { name in if let p = presets.first(where: { $0.name == name }) { au.currentPreset = p } },
reread: { Dictionary(uniqueKeysWithValues: all.map { ($0.identifier, $0.value) }) })
presetName = au.currentPreset?.name
token = au.parameterTree?.token(byAddingParameterObserver: { [weak self] address, value in
DispatchQueue.main.async {
guard let self = self, let p = all.first(where: { $0.address == address }) else { return }
self.values[p.identifier] = value
}
})
if let tree = au.parameterTree, let t = token { cleanup = { tree.removeParameterObserver(t) } }
}
func value(_ key: String) -> Float { values[key] ?? params[key]?.def ?? 0 }
func set(_ key: String, _ v: Float) {
guard let info = params[key] else { return }
var x = Swift.min(Swift.max(v, info.min), info.max)
if info.isInt { x = x.rounded() }
values[key] = x
write(key, x)
}
func choice(_ key: String) -> String {
guard let info = params[key], info.isEnum else { return "" }
let i = Int(value(key).rounded())
return info.choices[Swift.max(0, Swift.min(info.choices.count - 1, i))]
}
func applyPreset(named name: String) {
applyPresetNamed(name)
presetName = name
values = reread()
}
}