Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/ModulationPedal.swift
T
josh f29ab609bd Add Chorus, Sustainer, Ring Modulator pedals + pedal-info UI
Pedals (each registered, with PedalType static, enclosure color, palette order,
summary, and tests):
- Chorus: true-stereo widener (two anti-phase modulated delay lines), distinct
  from the mono-summed Modulation chorus.
- Sustainer: one-knob compressor (sustain drives threshold/ratio/makeup via
  LinLin) + tone low-pass.
- Ring Modulator: dry × tunable sine carrier.

Pedal-info UI:
- Pedal.summary (open var, overridden by all 21 pedals; mandatory going forward).
- PedalRegistry.summary(for:) for the picker.
- PedalPaletteView rows show the summary as a subtitle.
- PedalView gains an ⓘ button → PedalInfoView sheet showing the description and
  the effect controls (knobs/sliders/switches) with ranges/options — excludes
  the common Mix/Level/bypass.

Tests: per-pedal build/param-change/knob-list; every built-in pedal has a
non-empty summary; info excludes common controls. 183 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 18:05:30 -07:00

127 lines
4.1 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.
// ModulationPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Modulation pedal: stereo chorus folded to mono.
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry → Chorus.Audio(mix: 1.0, rate:, depth:) → stereo (2 ch)
/// → SumChannels.Audio(mul: 0.5) → mono
/// ```
///
/// `Chorus.Audio` has a built-in `mix` parameter that blends dry+wet internally.
/// Here it is set to `1.0` (fully wet) so the base `Pedal` class handles dry/wet
/// blending uniformly via its own `mix` parameter.
///
/// The stereo output from `Chorus` is summed to mono with a ×0.5 gain to compensate
/// for channel doubling.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - rate: Chorus LFO frequency in Hz. Range `[0.1, 10]`. Defaults to `1.5`.
/// - depth: Chorus modulation depth as a fraction of the delay time in [0, 1].
/// Smaller values (0.001–0.01) produce classic chorus width. Defaults to `0.002`.
public final class ModulationPedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Classic mono chorus for subtle thickening and shimmer."
}
// MARK: - Parameters
/// Chorus LFO rate in Hz. Range [0.1, 10].
@UIParam public var rate: Double = 1.5
/// Chorus modulation depth as a fraction of delay time in [0, 1].
@UIParam public var depth: Double = 0.002
// MARK: - Constants
/// Chorus `mix` value. Set to 1.0 (fully wet) so the base `Pedal` handles
/// the dry/wet blend via its own `mix` parameter.
private let chorusWetMix: Double = 1.0
/// Base delay time for the chorus in seconds.
private let chorusDelayTime: Double = 0.025
/// Maximum delay time that the chorus delay line is allocated for.
private let chorusMaxDelayTime: Double = 0.1
/// Gain applied to the summed stereo→mono output to compensate for channel doubling.
private let stereoSumGain: Double = 0.5
/// Default wet/dry blend — an even mix for classic chorus width.
private static let defaultMix: Double = 0.5
// MARK: - UI knobs
/// Rate knob range in Hz (chorus LFO frequency).
private let rateRange: ClosedRange<Double> = 0.1 ... 8.0
/// Depth knob range (fraction of delay time).
private let depthRange: ClosedRange<Double> = 0.0005 ... 0.01
// MARK: - Init
/// Creates a modulation (chorus) 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: "Modulation",
inputBus: inputBus,
outputBus: outputBus,
initialMix: ModulationPedal.defaultMix
)
}
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Rate", node: $rate, range: rateRange),
PedalKnob(label: "Depth", node: $depth, range: depthRange),
]
}
// MARK: - Effect
/// Applies stereo chorus 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 chorus output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Chorus.Audio produces a 2-channel stereo output.
let stereoChorus: Chorus.Audio = Chorus.Audio(
input: dry,
delayTime: chorusDelayTime,
depth: $depth,
rate: $rate,
mix: chorusWetMix,
sampleRate: sampleRate,
maxDelayTime: chorusMaxDelayTime
)
// Sum stereo → mono with ×0.5 to avoid level doubling.
return SumChannels.Audio(
input: stereoChorus,
mul: stereoSumGain
)
}
}