// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI import UniformTypeIdentifiers /// The Wavetable's own pieces inside its generic cards (Android's WavetableEditor.kt): /// each oscillator card draws that oscillator's table, and Oscillator 1's card /// carries LOAD TABLE, because a loaded WAV becomes the "Custom" table and is /// selected on oscillator 1. @MainActor enum WavetableBespoke { static func preview(osc: Int, tone: Color, model: DeviceModel) -> AnyView? { AnyView(WavetablePreview(osc: osc, tone: tone, model: model)) } static func loadTableAction(tone: Color, model: DeviceModel) -> AnyView? { AnyView(LoadTableAction(model: model)) } /// What the preview draws of a table: up to 16 of its frames, 128 points each. struct Curves { let frames: Int let rows: [(points: [Float], frame: Int)] } /// Curves by table (a factory name, or a custom sample id + frame length): /// tables are immutable once built, so each is read and thinned out once. private static var cache: [String: Curves] = [:] static func curves(key: String, osc: Int, data: DeviceDataSource?) -> Curves? { if let c = cache[key] { return c } guard let t = data?.wavetableFrames(osc: osc), t.frames > 0, t.size >= 128, t.data.count >= t.frames * t.size else { return nil } let n = t.size, step = Swift.max(1, t.frames / 16) let rows = stride(from: 0, to: t.frames, by: step).map { f -> (points: [Float], frame: Int) in ((0..<128).map { i in t.data[f * n + i * n / 128] }, f) } let c = Curves(frames: t.frames, rows: rows) if cache.count > 64 { cache.removeAll() } cache[key] = c return c } /// The table oscillator [osc] plays, and the key its curves are cached under. static func tableKey(osc: Int, model: DeviceModel) -> (name: String, custom: Bool, key: String) { let name = model.choice("o\(osc)table") guard name == "Custom" else { return (name, false, "F|" + name) } let id = model.data?.hiddenValue("sfSample") ?? "" return (name, true, "C|\(id)|\(model.choice("wtframe"))") } /// Native floats from a bridge call's bytes. static func floats(_ d: Data) -> [Float] { d.withUnsafeBytes { Array($0.bindMemory(to: Float.self)) } } } /// Every frame of oscillator [osc]'s table stacked back to front, the one at its POSITION lit. struct WavetablePreview: View { let osc: Int let tone: Color @ObservedObject var model: DeviceModel /// Bumped while the table is being built, to look again. @State private var retry = 0 var body: some View { let (name, custom, key) = WavetableBespoke.tableKey(osc: osc, model: model) let curves = WavetableBespoke.curves(key: key, osc: osc, data: model.data) let unloaded = custom && model.data?.hiddenValue("sfSample") == nil let caption = curves.map { "\(custom ? "your table" : name) · \($0.frames) frames" } ?? (unloaded ? "Custom · no table loaded" : "\(name) (preparing…)") let pos = model.value("o\(osc)pos") ZStack(alignment: .topLeading) { if let c = curves { Canvas { ctx, size in draw(c, pos: pos, in: ctx, size: size) } } Text(caption).font(Theme.mono(9)).foregroundColor(Theme.dim) .padding(.horizontal, 4).padding(.vertical, 2) } .frame(maxWidth: .infinity, minHeight: 72, maxHeight: 72) .background(Theme.background) .overlay(Rectangle().stroke(Theme.grid, lineWidth: 1)) .task(id: retry) { guard curves == nil, !unloaded, retry < 60 else { return } try? await Task.sleep(nanoseconds: 300_000_000) retry += 1 } } private func draw(_ c: WavetableBespoke.Curves, pos: Float, in ctx: GraphicsContext, size: CGSize) { let k = c.rows.count let rowH = size.height * 0.45 let lit = Int(pos * Float(c.frames - 1) + 0.5) let near = Swift.max(1, c.frames / k) let w = size.width * 0.76 for idx in stride(from: k - 1, through: 0, by: -1) { let row = c.rows[idx] let depth = k > 1 ? CGFloat(idx) / CGFloat(k - 1) : 0 let x0 = size.width * 0.04 + depth * size.width * 0.18 let yMid = size.height * 0.72 - depth * size.height * 0.45 let on = abs(row.frame - lit) * 2 <= near let n = row.points.count var path = Path() for i in 0.. (frames: Int, size: Int, data: [Float])? { guard let p = au.parameterTree?.allParameters.first(where: { $0.identifier == "o\(osc)table" }), let names = p.valueStrings, !names.isEmpty else { return nil } let name = names[Swift.max(0, Swift.min(names.count - 1, Int(p.value.rounded())))] var frames = 0, size = 0 guard let d = SizzleDeviceAU.factoryWavetable(named: name, blocking: Snapshot.active, frames: &frames, size: &size) else { return nil } return (frames, size, WavetableBespoke.floats(d)) } }