// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later import SwiftUI /// The Granular Glitch's editor (Android's GranularEditor): a dotted oscilloscope of /// the sound passing through with the granular read head drawn as a jumping /// playhead, and one horizontal Intensity slider below. The scope only shows data /// while the effect is running on signal (a mixer channel's FX in the app; render /// resources allocated in the plugin). @MainActor enum GranularBespoke { static func body(_ model: DeviceModel) -> AnyView? { AnyView(GranularEditorView(model: model)) } } private struct GranularEditorView: View { @ObservedObject var model: DeviceModel var body: some View { let intensity = Swift.min(Swift.max(model.value("intensity"), 0), 1) VStack(spacing: 8) { GranularScope(data: model.data) .frame(maxWidth: .infinity) .frame(height: 160) Text("Intensity \(Int((intensity * 100).rounded()))%") .font(Theme.mono(12)).foregroundColor(Theme.accent) HorizontalSlider(value: intensity) { model.set("intensity", Swift.min(Swift.max($0, 0), 1)) } .frame(maxWidth: .infinity) .frame(height: 40) Text("Low = occasional stutters ยท High = constant glitch & octave jumps.") .font(Theme.mono(10)).foregroundColor(Theme.dim) .multilineTextAlignment(.center) .frame(maxWidth: .infinity) } } } /// The scope, polled from the data source once per display frame. private struct GranularScope: View { let data: DeviceDataSource? private static let capacity = 128 // the effect fills up to its own scope size (96) var body: some View { TimelineView(.animation) { _ in let scope = data?.effectScope(capacity: Self.capacity) ZStack { Canvas { ctx, size in draw(scope, in: &ctx, size: size) } if scope == nil { Text("Add to a mixer channel's FX and play to see the scope.") .font(Theme.mono(11)).foregroundColor(Theme.dim) .multilineTextAlignment(.center) .padding(.horizontal, 8) } } } .background(Theme.background) .overlay(Rectangle().stroke(Theme.grid, lineWidth: 1)) } private func draw(_ scope: (samples: [Float], playhead: Float)?, in ctx: inout GraphicsContext, size: CGSize) { let w = size.width, h = size.height let cy = h * 0.5, amp = h * 0.42 var base = Path() base.move(to: CGPoint(x: 0, y: cy)) base.addLine(to: CGPoint(x: w, y: cy)) ctx.stroke(base, with: .color(Theme.grid), lineWidth: 1) guard let s = scope, s.samples.count > 1 else { return } // Dotted waveform of the signal passing through. let n = s.samples.count let step = w / CGFloat(n - 1) var dots = Path() for k in 0..= 0 && ph <= 1 { let px = CGFloat(ph) * w var line = Path() line.move(to: CGPoint(x: px, y: 0)) line.addLine(to: CGPoint(x: px, y: h)) ctx.stroke(line, with: .color(Theme.accent), lineWidth: 2) ctx.fill(Path(ellipseIn: CGRect(x: px - 3, y: cy - 3, width: 6, height: 6)), with: .color(Theme.accent)) } } } /// A horizontal fill-from-left slider like Android's retro faders: tap or drag to set. private struct HorizontalSlider: View { let value: Float let onChange: (Float) -> Void var body: some View { GeometryReader { geo in let w = geo.size.width let frac = CGFloat(Swift.min(Swift.max(value, 0.001), 1)) ZStack(alignment: .leading) { Theme.background HStack(spacing: 0) { Spacer(minLength: 0) Theme.accent.frame(width: 3) // thumb } .frame(width: Swift.max(w * frac, 3)) .background(Theme.accent.opacity(0.30)) } .overlay(Rectangle().stroke(Theme.grid, lineWidth: 1)) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0).onChanged { g in if w > 0 { onChange(Float(g.location.x / w)) } }) } } } // MARK: - plugin backend /// The effect AU's scope (SizzleEffectAU, channel 0). SizzleEffectAU.h isn't in the /// Swift bridging headers, so its two methods are declared here and reached by /// selector; any other device AU doesn't respond and has no scope. @objc private protocol EffectScopeAU { func copyEffectScope(_ out: UnsafeMutablePointer, capacity: Int) -> Int var effectPlayhead: Float { get } } extension AUDataSource { func effectScope(capacity: Int) -> (samples: [Float], playhead: Float)? { guard capacity > 0, au.responds(to: #selector(EffectScopeAU.copyEffectScope(_:capacity:))) else { return nil } let src = unsafeBitCast(au, to: EffectScopeAU.self) var buf = [Float](repeating: 0, count: capacity) let n = buf.withUnsafeMutableBufferPointer { src.copyEffectScope($0.baseAddress!, capacity: $0.count) } guard n >= 0 else { return nil } return (Array(buf.prefix(n)), src.effectPlayhead) } }