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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3b035d6a7d
commit
435305e42a
@@ -3,8 +3,191 @@
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// The TapeEngine's hand-built editor parts (stub: card layout until ported).
|
||||
/// The Tape Engine's hand-built editor (Android's TapeEngineEditor): a reel-to-reel
|
||||
/// transport, two NAB reels feeding tape under tension down to a playhead block
|
||||
/// between them, then round knobs for wow, flutter, hiss and the wow's tempo sync.
|
||||
///
|
||||
/// Press and hold a reel to brake the tape. Like Android, the brake is the `speed`
|
||||
/// parameter: pressing writes 0.5 and releasing writes 1 through DeviceModel.set (an
|
||||
/// AU parameter in the plugin, the slot's value in the app). TapeEngine.cpp reads
|
||||
/// `speed` as its target and glides its read rate toward it (SPEED_GLIDE per sample,
|
||||
/// ≈0.19 s at 44.1 kHz), so the pitch dives and springs back; the reels ease toward
|
||||
/// the same target at the same pace.
|
||||
@MainActor
|
||||
enum TapeEngineBespoke {
|
||||
static func body(_ model: DeviceModel) -> AnyView? { nil }
|
||||
static func body(_ model: DeviceModel) -> AnyView? { AnyView(TapeEngineEditorView(model: model)) }
|
||||
|
||||
static let brakeSpeed: Float = 0.5 // ≈ one octave down while a reel is held
|
||||
static let minSpeed: Float = 0.25
|
||||
}
|
||||
|
||||
/// The transport's geometry and motion constants (Android's TapeEngineEditor).
|
||||
private enum TapeReel {
|
||||
static let degPerSecond = 360.0 // one turn a second at full speed
|
||||
static let glideSeconds = 0.19 // the DSP's speed glide time constant
|
||||
static let lX: CGFloat = 0.30, rX: CGFloat = 0.70, centreY: CGFloat = 0.38
|
||||
static let height: CGFloat = 180
|
||||
|
||||
static func radius(_ size: CGSize) -> CGFloat { Swift.min(size.height * 0.30, size.width * 0.20) }
|
||||
|
||||
/// Whether [p] is on either reel (the brake's hit area).
|
||||
static func hit(_ p: CGPoint, _ size: CGSize) -> Bool {
|
||||
let r = radius(size), cy = size.height * centreY
|
||||
return hypot(p.x - size.width * lX, p.y - cy) <= r || hypot(p.x - size.width * rX, p.y - cy) <= r
|
||||
}
|
||||
}
|
||||
|
||||
/// The reels' angle and a display speed eased toward the target, advanced by wall
|
||||
/// time on each frame (not published: the TimelineView redraws).
|
||||
private final class TapeReelClock {
|
||||
var angle = 0.0
|
||||
var speed = 1.0
|
||||
private var last: Date?
|
||||
|
||||
func advance(to now: Date, target: Double) -> Double {
|
||||
let dt = last.map { Swift.min(0.1, Swift.max(0, now.timeIntervalSince($0))) } ?? 0
|
||||
last = now
|
||||
speed += (target - speed) * (1 - exp(-dt / TapeReel.glideSeconds))
|
||||
angle = (angle + speed * TapeReel.degPerSecond * dt).truncatingRemainder(dividingBy: 360)
|
||||
return angle
|
||||
}
|
||||
}
|
||||
|
||||
private struct TapeEngineEditorView: View {
|
||||
@ObservedObject var model: DeviceModel
|
||||
@State private var clock = TapeReelClock()
|
||||
@State private var touching = false
|
||||
@State private var braking = false
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 8) {
|
||||
transport
|
||||
Text("Hold a reel to brake the tape — pitch dives, then springs back.")
|
||||
.font(Theme.mono(11)).foregroundColor(Theme.dim).multilineTextAlignment(.center)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
.frame(maxWidth: .infinity)
|
||||
HStack(alignment: .top, spacing: 6) {
|
||||
knob("wow", "Wow")
|
||||
knob("flutter", "Flutter")
|
||||
knob("hiss", "Hiss")
|
||||
knob("division", "Wow Sync")
|
||||
}
|
||||
.padding(.top, 2)
|
||||
}
|
||||
.onDisappear { release() }
|
||||
}
|
||||
|
||||
private var transport: some View {
|
||||
let target = Double(Swift.min(1, Swift.max(TapeEngineBespoke.minSpeed, model.value("speed"))))
|
||||
return GeometryReader { g in
|
||||
TimelineView(.animation) { tl in
|
||||
let angle = clock.advance(to: tl.date, target: target)
|
||||
Canvas { ctx, size in TapeTransportDrawing.draw(ctx, size, angle: angle) }
|
||||
}
|
||||
.contentShape(Rectangle())
|
||||
.gesture(DragGesture(minimumDistance: 0)
|
||||
.onChanged { d in
|
||||
guard !touching else { return }
|
||||
touching = true
|
||||
if TapeReel.hit(d.startLocation, g.size) {
|
||||
braking = true
|
||||
model.set("speed", TapeEngineBespoke.brakeSpeed)
|
||||
}
|
||||
}
|
||||
.onEnded { _ in release() })
|
||||
}
|
||||
.frame(height: TapeReel.height)
|
||||
.background(Theme.surface)
|
||||
}
|
||||
|
||||
private func release() {
|
||||
touching = false
|
||||
if braking { braking = false; model.set("speed", 1) }
|
||||
}
|
||||
|
||||
@ViewBuilder private func knob(_ key: String, _ label: String) -> some View {
|
||||
if let p = model.params[key] {
|
||||
KnobView(label: label, valueText: Format.value(p, model.value(key)), value: model.value(key),
|
||||
min: p.min, max: p.max, def: p.def, tone: Theme.accent) { model.set(key, $0) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The transport drawing: tape, playhead, then the two reels over it.
|
||||
private enum TapeTransportDrawing {
|
||||
static func draw(_ ctx: GraphicsContext, _ size: CGSize, angle: Double) {
|
||||
let cx0 = size.width * TapeReel.lX, cx1 = size.width * TapeReel.rX
|
||||
let cy = size.height * TapeReel.centreY
|
||||
let r = TapeReel.radius(size)
|
||||
|
||||
// Playhead block between and below the reels.
|
||||
let hw = (cx1 - cx0) * 0.46, hh = size.height * 0.13
|
||||
let hx = size.width * 0.5, hy = cy + r + size.height * 0.14
|
||||
let tl = CGPoint(x: hx - hw / 2, y: hy - hh / 2), tr = CGPoint(x: hx + hw / 2, y: hy - hh / 2)
|
||||
|
||||
// Tensioned tape: from each reel's bottom straight to the playhead and across its top.
|
||||
var tape = Path()
|
||||
tape.move(to: CGPoint(x: cx0, y: cy + r))
|
||||
tape.addLine(to: tl); tape.addLine(to: tr)
|
||||
tape.addLine(to: CGPoint(x: cx1, y: cy + r))
|
||||
ctx.stroke(tape, with: .color(Theme.dim), lineWidth: 1.5)
|
||||
|
||||
let head = Path(roundedRect: CGRect(x: tl.x, y: tl.y, width: hw, height: hh), cornerRadius: hh * 0.45)
|
||||
ctx.fill(head, with: .color(Theme.grid))
|
||||
ctx.stroke(head, with: .color(Theme.accent), lineWidth: 1.2)
|
||||
var gap = Path()
|
||||
gap.move(to: CGPoint(x: hx, y: hy - hh / 2 + 2)); gap.addLine(to: CGPoint(x: hx, y: hy + hh / 2 - 2))
|
||||
ctx.stroke(gap, with: .color(Theme.accent), lineWidth: 1.2)
|
||||
|
||||
reel(ctx, CGPoint(x: cx0, y: cy), r, angle)
|
||||
reel(ctx, CGPoint(x: cx1, y: cy), r, angle)
|
||||
}
|
||||
|
||||
/// One NAB reel: metal flange, three rotating rounded-trapezoid bobbin holes, the hub.
|
||||
private static func reel(_ ctx: GraphicsContext, _ c: CGPoint, _ r: CGFloat, _ angle: Double) {
|
||||
ctx.fill(circle(c, r), with: .color(Theme.grid))
|
||||
var holes = Path()
|
||||
for k in 0..<3 { holes.addPath(bobbinHole(c, r, angle + Double(k) * 120)) }
|
||||
ctx.fill(holes, with: .color(Theme.surface))
|
||||
ctx.fill(circle(c, r * 0.26), with: .color(Theme.surface))
|
||||
ctx.stroke(circle(c, r * 0.26), with: .color(Theme.accent), lineWidth: 1.6)
|
||||
ctx.stroke(circle(c, r * 0.14), with: .color(Theme.accent), lineWidth: 1.1)
|
||||
ctx.stroke(circle(c, r), with: .color(Theme.dim), lineWidth: 1.4)
|
||||
}
|
||||
|
||||
private static func circle(_ c: CGPoint, _ r: CGFloat) -> Path {
|
||||
Path(ellipseIn: CGRect(x: c.x - r, y: c.y - r, width: r * 2, height: r * 2))
|
||||
}
|
||||
|
||||
/// A hole at [deg] around the reel: wide toward the hub, narrow toward the rim.
|
||||
private static func bobbinHole(_ c: CGPoint, _ r: CGFloat, _ deg: Double) -> Path {
|
||||
let rIn = r * 0.40, rOut = r * 0.86
|
||||
let v = [polar(c, rIn, deg - 44), polar(c, rOut, deg - 13), polar(c, rOut, deg + 13), polar(c, rIn, deg + 44)]
|
||||
return roundedPoly(v, corner: r * 0.09)
|
||||
}
|
||||
|
||||
/// A closed polygon through [v] with every corner rounded to [corner].
|
||||
private static func roundedPoly(_ v: [CGPoint], corner: CGFloat) -> Path {
|
||||
var p = Path()
|
||||
let n = v.count
|
||||
for i in 0..<n {
|
||||
let cur = v[i], prev = v[(i + n - 1) % n], next = v[(i + 1) % n]
|
||||
let a = toward(cur, prev, corner), b = toward(cur, next, corner)
|
||||
if i == 0 { p.move(to: a) } else { p.addLine(to: a) }
|
||||
p.addQuadCurve(to: b, control: cur)
|
||||
}
|
||||
p.closeSubpath()
|
||||
return p
|
||||
}
|
||||
|
||||
private static func toward(_ from: CGPoint, _ to: CGPoint, _ len: CGFloat) -> CGPoint {
|
||||
let dx = to.x - from.x, dy = to.y - from.y
|
||||
let d = Swift.max(1e-3, hypot(dx, dy))
|
||||
return CGPoint(x: from.x + dx / d * len, y: from.y + dy / d * len)
|
||||
}
|
||||
|
||||
private static func polar(_ c: CGPoint, _ rad: CGFloat, _ deg: Double) -> CGPoint {
|
||||
let a = deg * .pi / 180
|
||||
return CGPoint(x: c.x + rad * CGFloat(cos(a)), y: c.y + rad * CGFloat(sin(a)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user