// GranulatorPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Stereo input granulator. /// /// Chops the input into a cloud of overlapping windowed grains, each randomly scattered across the /// stereo field by `Spread`, for a lush, smeared, widened texture. Wraps the ``StereoGranulator`` /// generator (mono in → stereo out) as a `.widener`. /// /// ## Knobs /// - `grain`: Grain size in seconds. Range `[0.01, 0.5]`. /// - `overlap`: Overlapping grain count (density). Range `[1, 16]`. /// - `spread`: Stereo spread `[0, 1]` — 0 = centred (mono), 1 = full field. public final class GranulatorPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Scatters overlapping grains of your sound across the stereo field — a lush, smeared wash." } // MARK: - Parameters /// Grain size in seconds. Range [0.01, 0.5]. @UIParam public var grain: Double = 0.08 /// 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 // MARK: - Constants private static let defaultMix: Double = 0.5 /// Largest grain the buffer is allocated for, in seconds (≥ the `grain` knob maximum). private let maxGrainSize: Double = 0.5 // MARK: - Ranges private let grainRange: ClosedRange = 0.01 ... 0.5 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: "Grain", node: $grain, range: grainRange), PedalKnob(label: "Overlap", node: $overlap, range: overlapRange), PedalKnob(label: "Spread", node: $spread, range: spreadRange), ] } // MARK: - Channel topology public override var channelKind: PedalChannelKind { .widener } // MARK: - Init /// Creates a 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: "Granulator", inputBus: inputBus, outputBus: outputBus, initialMix: GranulatorPedal.defaultMix ) } // MARK: - Effect public override func wideEffect( _ dry: any ValidGeneratorInput ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { let granulator: StereoGranulator = StereoGranulator( input: dry, maxGrainSize: maxGrainSize, grainSize: $grain, overlap: $overlap, spread: $spread, sampleRate: sampleRate ) return (granulator.left, granulator.right) } }