// AutoGranulatorPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Amplitude-tracking stereo granulator. /// /// Like ``GranulatorPedal``, but the grain size follows the input level: by default louder playing /// uses the **minimum** grain size and softer playing the **maximum** (so dynamics shrink and grow /// the grains). The `Response` switch flips that relationship. Grains are randomly scattered across /// the stereo field by `Spread`. Wraps ``StereoGranulator`` as a `.widener`. /// /// The level → grain-size map is `AmplitudeFollower` → `LinLin`, with a `CrossFade` selecting the /// normal vs flipped mapping from the `Response` switch. /// /// ## Knobs /// - `minGrain`: Minimum grain size in seconds. Range `[0.005, 0.1]`. /// - `maxGrain`: Maximum grain size in seconds. Range `[0.05, 0.4]`. /// - `overlap`: Overlapping grain count (density). Range `[1, 16]`. /// - `spread`: Stereo spread `[0, 1]`. /// /// ## Switches /// - `response` (two-state): `0` = Loud → Short grains, `1` = Loud → Long grains. public final class AutoGranulatorPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Granular wash whose grain size tracks your dynamics — loud shrinks grains, soft grows them." } // MARK: - Parameters /// Minimum grain size in seconds. Range [0.005, 0.1]. @UIParam public var minGrain: Double = 0.02 /// Maximum grain size in seconds. Range [0.05, 0.4]. @UIParam public var maxGrain: Double = 0.25 /// Overlapping grain count (density). Range [1, 16]. @UIParam public var overlap: Double = 4.0 /// Stereo spread [0, 1]. @UIParam public var spread: Double = 0.7 /// Level → grain-size response. `0` = Loud → Short, `1` = Loud → Long. Two-state switch. @UIParam public var response: Double = 0.0 // MARK: - Constants private static let defaultMix: Double = 0.5 /// Largest grain the buffer is allocated for, in seconds (≥ the `maxGrain` knob maximum). private let maxGrainSize: Double = 0.4 /// Amplitude at which the grain size reaches its loud-end value (follower output is unbounded; /// this sets the sensitivity). Values above it are clamped by the generator's grain-size clamp. private let ampSensitivity: Double = 0.5 private let amplitudeAttack: Double = 0.01 private let amplitudeRelease: Double = 0.2 // MARK: - Ranges private let minGrainRange: ClosedRange = 0.005 ... 0.1 private let maxGrainRange: ClosedRange = 0.05 ... 0.4 private let overlapRange: ClosedRange = 1.0 ... 16.0 private let spreadRange: ClosedRange = 0.0 ... 1.0 // MARK: - Knobs public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Min Grain", node: $minGrain, range: minGrainRange), PedalKnob(label: "Max Grain", node: $maxGrain, range: maxGrainRange), PedalKnob(label: "Overlap", node: $overlap, range: overlapRange), PedalKnob(label: "Spread", node: $spread, range: spreadRange), ] } // MARK: - Switches private let responseLoudShortValue: Double = 0.0 private let responseLoudLongValue: Double = 1.0 public override var effectSwitches: [PedalSwitch] { [ PedalSwitch( label: "Response", node: $response, options: [ PedalSwitch.Option(title: "Loud→Short", value: responseLoudShortValue), PedalSwitch.Option(title: "Loud→Long", value: responseLoudLongValue), ] ) ] } // MARK: - Channel topology public override var channelKind: PedalChannelKind { .widener } // MARK: - Init /// Creates an amplitude-tracking granulator 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: "Auto Granulator", inputBus: inputBus, outputBus: outputBus, initialMix: AutoGranulatorPedal.defaultMix ) } // MARK: - Effect public override func wideEffect( _ dry: any ValidGeneratorInput ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { let amplitude: AmplitudeFollower.Audio = AmplitudeFollower.Audio( in: dry, attackTime: amplitudeAttack, releaseTime: amplitudeRelease ) // Loud → short: amp 0 → maxGrain, amp ampSensitivity → minGrain. let loudShort: LinLin.Audio = LinLin.Audio( amplitude, inLo: 0.0, inHi: ampSensitivity, outLo: $maxGrain, outHi: $minGrain ) // Loud → long: amp 0 → minGrain, amp ampSensitivity → maxGrain. let loudLong: LinLin.Audio = LinLin.Audio( amplitude, inLo: 0.0, inHi: ampSensitivity, outLo: $minGrain, outHi: $maxGrain ) // `response` 0 → pan −1 → loudShort; 1 → pan +1 → loudLong (endpoints are full A / full B). let grainSize: CrossFade.Audio = CrossFade.Audio( inA: loudShort, inB: loudLong, pan: $response * 2.0 - 1.0 ) let granulator: StereoGranulator = StereoGranulator( input: dry, maxGrainSize: maxGrainSize, grainSize: grainSize, overlap: $overlap, spread: $spread, sampleRate: sampleRate ) return (granulator.left, granulator.right) } }