// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import Foundation extension DeviceModel { /// Backed by toolbox slot [slot] of the open song. convenience init(session: SizzleSession, slot: Int) { let type = session.type(ofSlot: slot) let specs = SizzleSession.paramSpecs(forType: type) let infos = specs.map { s in ParamInfo(key: s.key, label: s.label, min: s.minValue, max: s.maxValue, def: s.defaultValue, isInt: s.isInt, choices: s.choices) } func read() -> [String: Float] { var v: [String: Float] = [:] for s in specs { let text = session.value(ofSlot: slot, key: s.key) if s.choices.isEmpty { v[s.key] = Float(text) ?? s.defaultValue } else { v[s.key] = Float(s.choices.firstIndex(of: text) ?? Int(s.defaultValue)) } } return v } let byKey = Dictionary(uniqueKeysWithValues: specs.map { ($0.key, $0) }) self.init(toolboxType: type, displayName: session.name(ofSlot: slot), infos: infos, values: read(), presetNames: session.factoryPresets(forType: type), write: { key, v in guard let s = byKey[key] else { return } if s.choices.isEmpty { session.setNumber(v, ofSlot: slot, key: key) } else { session.setValue(s.choices[max(0, min(s.choices.count - 1, Int(v.rounded())))], ofSlot: slot, key: key) } }, applyPreset: { name in session.applyFactoryPreset(name, toSlot: slot) }, reread: read) data = SlotDataSource(session: session, slot: slot) } }