// DiffuserPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Stereo diffuser pedal: fans the frequency spectrum across the left and right channels. /// /// A `.widener` pedal. The dry signal is split into overlapping frequency bands; each band is /// panned by its centre frequency — lows toward the left, highs toward the right — and the /// `spread` knob sets how far the bands fan out. At `spread = 0` every band sits centre (a mono /// collapse); at `spread = 1` the spectrum is splayed hard across the stereo field, giving a /// wide, diffuse image without time smearing. /// /// Signal path (inside `wideEffect(_:)`), per band `k` at position `pos_k ∈ [-1, 1]`: /// ``` /// dry → RBJBandPass(freq_k) → ×gainL_k → Σ → left /// → ×gainR_k → Σ → right /// gainL_k = 0.5 − 0.5·pos_k·spread, gainR_k = 0.5 + 0.5·pos_k·spread (linear pan) /// ``` /// /// The wet/dry blend and bypass are handled by the `Pedal` base class. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - spread: Stereo spectral spread. `0` = centred (mono), `1` = fully fanned L↔R. /// Range `[0, 1]`. Defaults to `0.6`. public final class DiffuserPedal: Pedal { // MARK: - Parameters /// Stereo spectral spread in [0, 1]. `0` = mono, `1` = full fan. @UIParam public var spread: Double = 0.6 // MARK: - Constants private static let defaultMix: Double = 1.0 /// Centre pan gain (each band's gain when `spread == 0` or it sits dead-centre). private let centerGain: Double = 0.5 /// RBJ band-pass width in octaves — wide enough that adjacent bands overlap and recombine. private let bandwidthOctaves: Double = 1.5 /// The frequency bands and their base stereo positions (low = left, high = right). /// `position ∈ [-1, 1]`; multiplied by `spread` to place the band. private let bands: [(freq: Double, position: Double)] = [ (150.0, -1.0), (400.0, -0.5), (1000.0, 0.0), (2500.0, 0.5), (6000.0, 1.0), ] // MARK: - UI knobs private let spreadRange: ClosedRange = 0.0 ... 1.0 public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Spread", node: $spread, range: spreadRange), ] } // MARK: - Channel topology public override var channelKind: PedalChannelKind { .widener } // MARK: - Init /// Creates a stereo diffuser 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: "Diffuser", inputBus: inputBus, outputBus: outputBus, initialMix: DiffuserPedal.defaultMix ) } public override var summary: String { "Stereo diffuser that fans the frequency spectrum across the left and right channels." } // MARK: - Effect /// Splits the dry signal into frequency bands and pans each across the field by `spread`. /// /// - Parameter dry: Dry mono input signal. /// - Returns: The left and right diffused signals. public override func wideEffect( _ dry: any ValidGeneratorInput ) -> (left: any ValidGeneratorInput, right: any ValidGeneratorInput) { var left: (any ValidGeneratorInput)? var right: (any ValidGeneratorInput)? for band: (freq: Double, position: Double) in bands { let bandPass: RBJBandPass.Audio = RBJBandPass.Audio( input: dry, freq: band.freq, bandwidth: bandwidthOctaves ) // Linear pan, fully expressible as node arithmetic (no per-sample trig): // gainL = centerGain − (0.5·position)·spread // gainR = centerGain + (0.5·position)·spread let halfPosition: Double = centerGain * band.position let gainL: any ValidGeneratorInput = ($spread * (-halfPosition)) + centerGain let gainR: any ValidGeneratorInput = ($spread * halfPosition) + centerGain let leftContribution: any ValidGeneratorInput = bandPass * gainL let rightContribution: any ValidGeneratorInput = bandPass * gainR left = left.map { $0 + leftContribution } ?? leftContribution right = right.map { $0 + rightContribution } ?? rightContribution } return (left ?? dry, right ?? dry) } }