diff --git a/Sources/LyrebirdShoeGaze/PedalType.swift b/Sources/LyrebirdShoeGaze/PedalType.swift index 851054d..f5a5441 100644 --- a/Sources/LyrebirdShoeGaze/PedalType.swift +++ b/Sources/LyrebirdShoeGaze/PedalType.swift @@ -63,6 +63,7 @@ public extension PedalType { static let chorus = PedalType("Chorus") static let flanger = PedalType("Flanger") static let vibrato = PedalType("Vibrato") + static let rotary = PedalType("Rotary") static let ringModulator = PedalType("Ring Modulator") static let diffuser = PedalType("Diffuser") diff --git a/Sources/LyrebirdShoeGaze/Pedals/RotaryPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/RotaryPedal.swift new file mode 100644 index 0000000..3d32d55 --- /dev/null +++ b/Sources/LyrebirdShoeGaze/Pedals/RotaryPedal.swift @@ -0,0 +1,173 @@ +// RotaryPedal.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import LyrebirdSynth +import LyrebirdUI + +/// Rotary-speaker (Leslie) emulation. +/// +/// A spinning speaker produces correlated amplitude modulation (volume throb) and frequency +/// modulation (Doppler pitch wobble). This pedal builds both from one rotor LFO per channel, with +/// the left and right rotors in anti-phase so the image rotates across the stereo field. A two-speed +/// switch (Chorale / Tremolo) selects the rotor speed, and the **Accel** knob sets how quickly the +/// rotor ramps between speeds — the authentic spin-up / spin-down glide — via a `Lag` on the rate. +/// +/// Signal path (per channel, inside `wideEffect(_:)`): +/// ``` +/// dry → DelayLine(delay: base ± fm·sin(rate)) → × (1 ± am·sin(rate)) → wet +/// ``` +/// +/// ## Knobs +/// - `depth`: Rotor intensity — scales both the AM throb and the FM Doppler. Range `[0, 1]`. +/// - `accel`: Spin-up / spin-down ramp time in seconds. Range `[0.05, 3]`. +/// +/// ## Switches +/// - `speed` (two-state): `0` = Chorale (slow), `1` = Tremolo (fast). +public final class RotaryPedal: Pedal { + + /// One-line description shown in the picker and info panel. + public override var summary: String { + "Spinning Leslie rotor: Doppler pitch wobble plus volume throb, two-speed with ramp." + } + + // MARK: - Parameters + + /// Rotor intensity — scales both AM and FM. Range [0, 1]. + @UIParam public var depth: Double = 0.7 + + /// Spin-up / spin-down ramp time in seconds. Range [0.05, 3]. + @UIParam public var accel: Double = 0.5 + + /// Rotor speed selector. `0` = Chorale (slow), `1` = Tremolo (fast). Driven by a two-state switch. + @UIParam public var speed: Double = 0.0 + + // MARK: - Constants + + /// Rotary is heard fully wet; the `Mix` knob can dial it back if desired. + private static let defaultMix: Double = 1.0 + + /// Chorale (slow) rotor rate in Hz. + private let slowRateHz: Double = 0.8 + + /// Tremolo (fast) rotor rate in Hz. + private let fastRateHz: Double = 6.5 + + /// Maximum FM (Doppler) sweep half-swing in seconds, at full depth. + private let fmDepthScale: Double = 0.0008 + + /// Maximum AM (throb) depth, at full depth. + private let amDepthScale: Double = 0.4 + + /// Center delay the FM sweeps around, in seconds. + private let baseDelayTime: Double = 0.004 + + /// Maximum delay the lines are allocated for, in seconds. + private let maxDelayTime: Double = 0.02 + + /// LFO phase (cycles) for the left rotor. + private let leftPhase: Double = 0.0 + + /// LFO phase (cycles) for the right rotor — a half cycle opposite the left. + private let rightPhase: Double = 0.5 + + // MARK: - Ranges + + private let depthRange: ClosedRange = 0.0 ... 1.0 + private let accelRange: ClosedRange = 0.05 ... 3.0 + + // MARK: - Knobs + + public override var effectKnobs: [PedalKnob] { + [ + PedalKnob(label: "Depth", node: $depth, range: depthRange), + PedalKnob(label: "Accel", node: $accel, range: accelRange), + ] + } + + // MARK: - Switches + + private let speedChoraleValue: Double = 0.0 + private let speedTremoloValue: Double = 1.0 + + public override var effectSwitches: [PedalSwitch] { + [ + PedalSwitch( + label: "Speed", + node: $speed, + options: [ + PedalSwitch.Option(title: "Chorale", value: speedChoraleValue), + PedalSwitch.Option(title: "Tremolo", value: speedTremoloValue), + ] + ) + ] + } + + // MARK: - Channel topology + + public override var channelKind: PedalChannelKind { .widener } + + // MARK: - Init + + /// Creates a rotary-speaker pedal. + /// + /// - Parameters: + /// - inputBus: Audio bus index to read the dry input from. Defaults to `0`. + /// - outputBus: Audio bus index to write the processed output to. Defaults to `0`. + public init( + inputBus: Int = 0, + outputBus: Int = 0 + ) { + super.init( + name: "Rotary", + inputBus: inputBus, + outputBus: outputBus, + initialMix: RotaryPedal.defaultMix + ) + } + + // MARK: - Effect + + public override func wideEffect( + _ dry: any ValidGeneratorInput + ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { + // Rotor rate glides between the two speeds — the spin-up / spin-down ramp. Shared by both + // rotors (fan-out is cached per block). + let rateTarget: any ValidGeneratorInput = $speed * (fastRateHz - slowRateHz) + slowRateHz + let rate: Lag.Audio = Lag.Audio(input: rateTarget, time: $accel) + return (voice(dry, rate: rate, phase: leftPhase), + voice(dry, rate: rate, phase: rightPhase)) + } + + /// One rotor voice: a Doppler-swept delay line whose output is amplitude-modulated by the same + /// rotor LFO, both at `rate` and the given `phase`. + private func voice( + _ dry: any ValidGeneratorInput, + rate: any ValidGeneratorInput, + phase: Double + ) -> any ValidGeneratorInput { + let fmSweep: OscSin.Audio = OscSin.Audio( + freq: rate, + phase: phase, + mul: $depth * fmDepthScale, + add: baseDelayTime + ) + let delayed: DelayLine = DelayLine( + input: dry, + maxDelay: maxDelayTime, + delay: fmSweep, + sampleRate: sampleRate, + interpolation: .linear + ) + // Throb gain in [1 - 2·depth·amScale, 1]. + let amGain: OscSin.Audio = OscSin.Audio( + freq: rate, + phase: phase, + mul: $depth * amDepthScale, + add: 1.0 - $depth * amDepthScale + ) + return delayed * amGain + } +} diff --git a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift index a34d2d8..b52a436 100644 --- a/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift +++ b/Sources/LyrebirdShoeGaze/Presets/PedalRegistry.swift @@ -54,6 +54,7 @@ public enum PedalRegistry { "Chorus": { ChorusPedal() }, "Flanger": { FlangerPedal() }, "Vibrato": { VibratoPedal() }, + "Rotary": { RotaryPedal() }, "Sustainer": { SustainerPedal() }, "Ring Modulator": { RingModulatorPedal() }, "Diffuser": { DiffuserPedal() }, diff --git a/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift b/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift index b263dc2..5d7d166 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalPaletteView.swift @@ -52,7 +52,7 @@ public struct PedalPaletteView: View { /// appended afterwards (sorted) so the palette never silently drops a newly added pedal. private static let preferredOrder: [String] = [ "Overdrive", "Distortion", "Compressor", "Sustainer", "Limiter", "Noise Gate", "Fuzz", - "Graphic EQ", "Tremolo", "Phaser", "Modulation", "Chorus", "Flanger", "Vibrato", + "Graphic EQ", "Tremolo", "Phaser", "Modulation", "Chorus", "Flanger", "Vibrato", "Rotary", "Auto-Wah", "Mutant", "Ring Modulator", "Diffuser", "Bass Octaver", "Octave Splitter", "Harmonizer", "Delay", "Feedback Delay", "Multi-Tap Delay", "Ping-Pong", "Reverb", diff --git a/Sources/LyrebirdShoeGazeUI/PedalView.swift b/Sources/LyrebirdShoeGazeUI/PedalView.swift index 23d14f6..e7e3149 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalView.swift @@ -35,6 +35,7 @@ private func enclosureColor(for displayName: String) -> Color { case "Chorus": return Color(red: 0.16, green: 0.50, blue: 0.64) // aqua case "Flanger": return Color(red: 0.28, green: 0.52, blue: 0.78) // steel blue case "Vibrato": return Color(red: 0.42, green: 0.30, blue: 0.70) // violet-blue + case "Rotary": return Color(red: 0.45, green: 0.34, blue: 0.20) // Leslie cabinet walnut case "Sustainer": return Color(red: 0.22, green: 0.46, blue: 0.30) // pine green case "Ring Modulator": return Color(red: 0.64, green: 0.30, blue: 0.10) // burnt amber case "Diffuser": return Color(red: 0.34, green: 0.30, blue: 0.62) // periwinkle diff --git a/Tests/LyrebirdShoeGazeTests/RotaryPedalTests.swift b/Tests/LyrebirdShoeGazeTests/RotaryPedalTests.swift new file mode 100644 index 0000000..397596b --- /dev/null +++ b/Tests/LyrebirdShoeGazeTests/RotaryPedalTests.swift @@ -0,0 +1,97 @@ +// RotaryPedalTests.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Testing +import Foundation +import LyrebirdSynth +import LyrebirdUI +@testable import LyrebirdShoeGaze + +@MainActor +@Suite("RotaryPedal") +struct RotaryPedalTests { + + private func render( + configure: (RotaryPedal) -> Void + ) throws -> [Float] { + let renderDuration: Double = 0.3 + let numAudioBuses: Int = 64 + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( + duration: renderDuration, + numAudioBuses: numAudioBuses + ) + defer { try? FileManager.default.removeItem(at: url) } + + let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 2) + defer { + engine.busAllocator.releaseAudioBus(srcBus) + engine.busAllocator.releaseAudioBus(dstBus) + } + + let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) + let pedal: RotaryPedal = RotaryPedal(inputBus: srcBus.index, outputBus: dstBus.index) + configure(pedal) + let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) + + try engine.add(source) + try engine.add(pedal, position: NodePosition.after(source)) + try engine.add(master, position: NodePosition.after(pedal)) + try engine.start() + + return readStereoCAF(url: url) + } + + private func windowedSSD(_ a: [Float], _ b: [Float]) -> Double { + let skipSamples: Int = 4096 + let windowSize: Int = 4096 + guard a.count >= skipSamples + windowSize, + b.count >= skipSamples + windowSize else { return 0.0 } + let aWindow: ArraySlice = a[skipSamples..<(skipSamples + windowSize)] + let bWindow: ArraySlice = b[skipSamples..<(skipSamples + windowSize)] + return zip(aWindow, bWindow).reduce(0.0) { acc, pair in + let diff: Double = Double(pair.0 - pair.1) + return acc + diff * diff + } + } + + @Test func buildsAndProducesNonSilentOutput() throws { + let samples: [Float] = try render { _ in } + #expect(!samples.isEmpty) + #expect(hasNonSilentOutput(samples), "rotary output was silent") + } + + @Test func speedChangesOutput() throws { + let chorale: [Float] = try render { pedal in pedal.speed = 0.0; pedal.accel = 0.05 } + let tremolo: [Float] = try render { pedal in pedal.speed = 1.0; pedal.accel = 0.05 } + #expect(hasNonSilentOutput(tremolo), "fast rotary was silent") + #expect(windowedSSD(chorale, tremolo) > 0, "changing speed did not change the output") + } + + @Test func exposesDepthAccelKnobsAndSpeedSwitch() throws { + let pedal: RotaryPedal = RotaryPedal() + #expect(pedal.effectKnobs.map { $0.label } == ["Depth", "Accel"]) + #expect(pedal.effectSwitches.map { $0.label } == ["Speed"]) + #expect(pedal.effectSwitches.first?.options.map { $0.title } == ["Chorale", "Tremolo"]) + } + + @Test func presetRoundTripPreservesSpeed() throws { + let pedal: RotaryPedal = RotaryPedal() + pedal.speed = 1.0 + pedal.depth = 0.4 + let state: PedalState = pedal.captureState() + + let restored: RotaryPedal = RotaryPedal() + restored.applyState(state) + #expect(restored.speed == 1.0) + #expect(restored.depth == 0.4) + } + + @Test func registeredInBuiltInRegistry() throws { + let pedal: Pedal? = PedalRegistry.makePedal(type: "Rotary") + #expect(pedal is RotaryPedal) + } +}