// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The app's state around SizzleHost: which devices play, which editor shows, /// and the results of the benchmark / plugin check. @MainActor final class HostModel: ObservableObject { let host = SizzleHost() let devices = SizzleHost.devices var instruments: [SizzleHostDevice] { devices.filter { $0.isInstrument } } var effects: [SizzleHostDevice] { devices.filter { !$0.isInstrument } } @Published var instrument: SizzleHostDevice? @Published var effect: SizzleHostDevice? /// Which device's editor is shown: false = the instrument, true = the effect. @Published var editingEffect = false @Published var octave = 4 @Published var report: [String] = [] @Published var busy = false @Published var message: String? init() { if let first = instruments.first { select(first) } } func select(_ device: SizzleHostDevice) { host.selectInstrument(device) { [weak self] error in guard let self = self else { return } if let error = error { self.message = error.localizedDescription; return } self.instrument = device self.editingEffect = false } } func selectEffect(_ device: SizzleHostDevice?) { host.selectEffect(device) { [weak self] error in guard let self = self else { return } if let error = error { self.message = error.localizedDescription; return } self.effect = device self.editingEffect = device != nil } } var editedUnit: SizzleDeviceAU? { editingEffect ? host.effect : host.instrument } var canLoadSample: Bool { let t = instrument?.toolboxType return t == "SAMPLER" || t == "SOUNDFONT" } func loadSample(_ url: URL, pad: Int) { do { try host.loadSample(from: url, pad: Int32(pad)) } catch { message = error.localizedDescription } } func benchmark() { busy = true report = [] host.benchmark({ [weak self] line in self?.report.append(line) }, done: { [weak self] in self?.busy = false }) } }