Ported from the Android tabs by parallel agents (each screen with its own SizzleSession category); slot editors use the card editor with a preset library; songs load/save in the Android project format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
3.0 KiB
Swift
82 lines
3.0 KiB
Swift
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import SwiftUI
|
|
|
|
/// Toolbox navigation that outlives the tab (ToolboxScreen is rebuilt on each
|
|
/// switch): the tab to go back to when a slot editor opened from elsewhere closes
|
|
/// (Android's SlotEditorRequest.returnTab), and a counter that rebuilds the open
|
|
/// editor after its slot changed underneath it (a preset import).
|
|
@MainActor
|
|
final class ToolboxNav: ObservableObject {
|
|
static let shared = ToolboxNav()
|
|
var returnTab: AppModel.Tab?
|
|
@Published var editorEpoch = 0
|
|
|
|
/// Android's openSlotEditor: show [slot]'s settings over the Toolbox tab;
|
|
/// closing them returns to [returnTab].
|
|
static func open(_ slot: Int, app: AppModel, returnTab: AppModel.Tab?) {
|
|
guard slot >= 0, !app.session.type(ofSlot: slot).isEmpty else { return }
|
|
shared.returnTab = returnTab
|
|
app.editingSlot = slot
|
|
app.tab = .toolbox
|
|
}
|
|
|
|
func close(app: AppModel) {
|
|
app.editingSlot = nil
|
|
if let t = returnTab { app.tab = t }
|
|
returnTab = nil
|
|
}
|
|
}
|
|
|
|
/// Notes held on any on-screen keyboard or pad, for lighting keys (Android's heldNotes).
|
|
@MainActor
|
|
final class HeldNotes: ObservableObject {
|
|
static let shared = HeldNotes()
|
|
@Published private(set) var counts: [Int: Int] = [:]
|
|
|
|
func isHeld(_ midi: Int) -> Bool { (counts[midi] ?? 0) > 0 }
|
|
func on(_ midi: Int) { counts[midi, default: 0] += 1 }
|
|
func off(_ midi: Int) {
|
|
let n = (counts[midi] ?? 0) - 1
|
|
if n <= 0 { counts.removeValue(forKey: midi) } else { counts[midi] = n }
|
|
}
|
|
}
|
|
|
|
/// Waveform peaks per sample id, so redraws (marker drags) don't rescan the sample.
|
|
@MainActor
|
|
enum PeakCache {
|
|
private static var cache: [String: [Float]] = [:]
|
|
|
|
/// (min, max) pairs, interleaved; nil when the key holds no loaded sample.
|
|
static func peaks(slot: Int, key: String, buckets: Int = 400) -> [Float]? {
|
|
let session = SizzleSession.shared
|
|
let id = session.value(ofSlot: slot, key: key)
|
|
guard !id.isEmpty else { return nil }
|
|
let cacheKey = "\(id)|\(buckets)"
|
|
if let p = cache[cacheKey] { return p }
|
|
guard let data = session.samplePeaks(ofSlot: slot, key: key, buckets: buckets) else { return nil }
|
|
let p = data.withUnsafeBytes { Array($0.bindMemory(to: Float.self)) }
|
|
if cache.count > 64 { cache.removeAll() }
|
|
cache[cacheKey] = p
|
|
return p
|
|
}
|
|
}
|
|
|
|
/// Min/max waveform lines (the Sampler and SF2/XI previews).
|
|
struct WaveformShape: Shape {
|
|
let peaks: [Float]
|
|
func path(in r: CGRect) -> Path {
|
|
var p = Path()
|
|
let n = peaks.count / 2
|
|
guard n > 0 else { return p }
|
|
let bw = r.width / CGFloat(n), mid = r.height / 2
|
|
for i in 0..<n {
|
|
let x = r.minX + CGFloat(i) * bw
|
|
p.move(to: CGPoint(x: x, y: r.minY + mid - CGFloat(peaks[2 * i + 1]) * mid))
|
|
p.addLine(to: CGPoint(x: x, y: r.minY + mid - CGFloat(peaks[2 * i]) * mid + 0.5))
|
|
}
|
|
return p
|
|
}
|
|
}
|