Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/ReverbPedal.swift
T
2026-06-21 12:25:18 -07:00

111 lines
3.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ReverbPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Reverb pedal: Freeverb stereo reverb folded to mono.
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry → FreeVerb.Audio(mix: 1.0, roomSize:, damping:) → stereo (2 ch)
/// → SumChannels.Audio(mul: 0.5) → mono (fold + compensate)
/// ```
///
/// `FreeVerb` is driven at `mix: 1.0` so it produces a pure wet signal; the base
/// `Pedal` class handles the wet/dry blend via its `mix` parameter. The stereo
/// output from `FreeVerb` is summed to mono with a ×0.5 gain to compensate for
/// the channel doubling.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - roomSize: Room size in [0, 1]. Larger values produce longer, denser tails.
/// Defaults to `0.6`.
/// - damping: High-frequency damping in [0, 1]. Higher values produce darker,
/// shorter tails. Defaults to `0.5`.
public final class ReverbPedal: Pedal {
// MARK: - Parameters
/// Room size in [0, 1]. Larger = longer reverb tail.
@UIParam public var roomSize: Double = 0.6
/// High-frequency damping in [0, 1]. Higher = darker tail.
@UIParam public var damping: Double = 0.5
// MARK: - Constants
/// FreeVerb mix value. Set to 1.0 so FreeVerb produces pure wet output;
/// the base Pedal handles dry/wet blending via its own `mix` parameter.
private let freeVerbWetMix: Double = 1.0
/// Gain applied to the summed stereo→mono fold to compensate for the doubling
/// of energy when two channels are added together.
private let stereoSumGain: Double = 0.5
/// Default wet/dry blend — a partial mix so the dry signal stays up front.
private static let defaultMix: Double = 0.3
// MARK: - UI knobs
/// Room-size and damping knob range (both normalised [0, 1]).
private let normalizedRange: ClosedRange<Double> = 0.0 ... 1.0
// MARK: - Init
/// Creates a reverb 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: "Reverb",
inputBus: inputBus,
outputBus: outputBus,
initialMix: ReverbPedal.defaultMix
)
}
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Room", node: $roomSize, range: normalizedRange),
PedalKnob(label: "Damp", node: $damping, range: normalizedRange),
]
}
// MARK: - Effect
/// Applies Freeverb stereo reverb and folds the result to mono.
///
/// Returns a mono wet signal. The base `Pedal` class mixes this with the
/// dry signal according to the `mix` parameter.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: Wet mono reverberated output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// FreeVerb produces a 2-channel stereo output.
let stereoVerb: FreeVerb.Audio = FreeVerb.Audio(
source: dry,
roomSize: $roomSize,
damping: $damping,
mix: freeVerbWetMix,
sampleRate: sampleRate
)
// Sum stereo → mono with ×0.5 to avoid level doubling.
return SumChannels.Audio(
input: stereoVerb,
mul: stereoSumGain
)
}
}