Files
sizzletracker-android/ios/app/FX/Editor/Bespoke/Bespoke.swift
T
ReactorcoremeltdownandClaude Opus 5.5 435305e42a iOS 0.5.0: hand-built editors (Tape Delay, Tape Engine, Mix Tightener, Granular scope, Wavetable tables, Grain Cloud source)
Effect visualisation through the C++ effect, engine and AU; custom wavetables and
grain sources load and persist with the song; the app's slot editor hosts the
file picker for bespoke views.

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

93 lines
4.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
/// What a device editor needs beyond its parameters: drawings of data the DSP
/// owns (wavetable frames, sample waveforms, an effect's live scope), loading
/// files into the device, and settings that aren't AU parameters (the Mix
/// Tightener's hidden stage trims). Two backends: SlotDataSource (a toolbox slot
/// of the app's song, Host/) and AUDataSource (an AUv3, the plugin extension and
/// the device lab). Every requirement has a default here; each device's file
/// implements what it needs in extensions of those classes.
@MainActor
protocol DeviceDataSource: AnyObject {
/// Oscillator [osc]'s current table, level 0 (frames × size samples), or nil.
func wavetableFrames(osc: Int) -> (frames: Int, size: Int, data: [Float])?
/// Peak envelope (0..1, [buckets] values) of the sample the setting [key] names.
func samplePeaks(key: String, buckets: Int) -> [Float]?
/// Load a user file into the device ([kind]: "wavetable", "grainSource", …).
/// Returns an error message, or nil on success.
func loadFile(_ url: URL, kind: String) -> String?
/// The effect's live scope (oldest → newest samples) and read-head position
/// (0..1), or nil while it has none (not on a channel, no signal).
func effectScope(capacity: Int) -> (samples: [Float], playhead: Float)?
/// A setting that isn't a parameter (text, as stored), or nil.
func hiddenValue(_ key: String) -> String?
func setHiddenValue(_ value: String, key: String)
}
extension DeviceDataSource {
func wavetableFrames(osc: Int) -> (frames: Int, size: Int, data: [Float])? { nil }
func samplePeaks(key: String, buckets: Int) -> [Float]? { nil }
func loadFile(_ url: URL, kind: String) -> String? { "Not available here yet." }
func effectScope(capacity: Int) -> (samples: [Float], playhead: Float)? { nil }
func hiddenValue(_ key: String) -> String? { nil }
func setHiddenValue(_ value: String, key: String) {}
}
/// A host's file picker for bespoke views (the app's slot editor provides one: on
/// iOS 15 a .fileImporter nested inside another one may not present). A view
/// calls it with what to do with the picked URL; without one (the plugins) it
/// uses its own .fileImporter.
private struct DeviceFilePickerKey: EnvironmentKey {
static let defaultValue: ((@escaping (URL) -> Void) -> Void)? = nil
}
extension EnvironmentValues {
var deviceFilePicker: ((@escaping (URL) -> Void) -> Void)? {
get { self[DeviceFilePickerKey.self] }
set { self[DeviceFilePickerKey.self] = newValue }
}
}
/// Hand-built pieces of device editors (the Android app's bespoke editors), by
/// device. DeviceEditor asks here first and falls back to the card layout. Each
/// device's views live in their own file in this folder.
@MainActor
enum BespokeEditors {
/// Shown above the tabs (e.g. the Grain Cloud's source strip).
static func header(_ model: DeviceModel) -> AnyView? {
switch model.toolboxType {
case "GRAIN_CLOUD": return GrainCloudBespoke.header(model)
default: return nil
}
}
/// Replaces the card pages entirely (devices whose Android editor is hand-built).
static func body(_ model: DeviceModel) -> AnyView? {
switch model.toolboxType {
case "DELAY": return DelayBespoke.body(model)
case "TAPE_ENGINE": return TapeEngineBespoke.body(model)
case "MIX_TIGHTENER": return MixTightenerBespoke.body(model)
case "GRANULAR": return GranularBespoke.body(model)
default: return nil
}
}
/// A card drawing the generic VizView doesn't know (kind "wavetable").
static func viz(_ viz: LayoutPanel.Viz, tone: Color, model: DeviceModel) -> AnyView? {
switch viz.kind {
case "wavetable": return WavetableBespoke.preview(osc: viz.osc ?? 1, tone: tone, model: model)
default: return nil
}
}
/// A card's title-row action (LayoutPanel.action, e.g. "loadTable").
static func action(_ action: String, tone: Color, model: DeviceModel) -> AnyView? {
switch action {
case "loadTable": return WavetableBespoke.loadTableAction(tone: tone, model: model)
default: return nil
}
}
}