111 lines
3.7 KiB
Swift
111 lines
3.7 KiB
Swift
// TremoloPedal.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import LyrebirdSynth
|
||
import LyrebirdUI
|
||
|
||
/// Tremolo pedal: amplitude modulation via a unipolar sine LFO.
|
||
///
|
||
/// Signal chain (inside `effect(_:)`):
|
||
/// ```
|
||
/// dry × OscSin(freq: rate, mul: depth×0.5, add: 1−depth×0.5) → mono out
|
||
/// ```
|
||
///
|
||
/// The LFO oscillates between `[1 − depth, 1]` so full depth produces silence-to-unity
|
||
/// swells while zero depth leaves the signal unmodified. The result is a gain effect —
|
||
/// `defaultMix` is `1.0` (fully wet) so the base `Pedal` passes the tremolo signal through
|
||
/// unchanged; the `mix` knob can soften the wobble effect.
|
||
///
|
||
/// The wet/dry mix and bypass switching are handled by the `Pedal` base class.
|
||
///
|
||
/// ## Parameters
|
||
///
|
||
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
|
||
///
|
||
/// - Parameters:
|
||
/// - rate: LFO modulation rate in Hz. Range `[0.5, 12]`. Defaults to `4.0`.
|
||
/// - depth: Modulation depth in `[0, 1]`. `0` = no modulation, `1` = full tremolo.
|
||
/// Defaults to `0.7`.
|
||
public final class TremoloPedal: Pedal {
|
||
|
||
// MARK: - Parameters
|
||
|
||
/// LFO rate in Hz. Range [0.5, 12].
|
||
@UIParam public var rate: Double = 4.0
|
||
|
||
/// Modulation depth in [0, 1].
|
||
@UIParam public var depth: Double = 0.7
|
||
|
||
// MARK: - Constants
|
||
|
||
/// Default wet/dry blend — fully wet; tremolo is a gain effect so dry/wet
|
||
/// blending does not apply in the same way as a parallel effect.
|
||
private static let defaultMix: Double = 1.0
|
||
|
||
/// Scale factor applied to `depth` to compute the LFO half-amplitude swing.
|
||
/// At full depth the LFO oscillates over half the normalised range, so the
|
||
/// peak gain is 1.0 and the trough is `1 − depth`.
|
||
private let lfoDepthScale: Double = 0.5
|
||
|
||
// MARK: - UI knobs
|
||
|
||
/// Rate knob range in Hz (LFO frequency).
|
||
private let rateRange: ClosedRange<Double> = 0.5 ... 12.0
|
||
|
||
/// Depth knob range (modulation fraction).
|
||
private let depthRange: ClosedRange<Double> = 0.0 ... 1.0
|
||
|
||
public override var effectKnobs: [PedalKnob] {
|
||
[
|
||
PedalKnob(label: "Rate", node: $rate, range: rateRange),
|
||
PedalKnob(label: "Depth", node: $depth, range: depthRange),
|
||
]
|
||
}
|
||
|
||
// MARK: - Init
|
||
|
||
/// Creates a tremolo 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: "Tremolo",
|
||
inputBus: inputBus,
|
||
outputBus: outputBus,
|
||
initialMix: TremoloPedal.defaultMix
|
||
)
|
||
}
|
||
|
||
// MARK: - Effect
|
||
|
||
/// Multiplies the dry signal by a unipolar sine LFO.
|
||
///
|
||
/// The LFO range is `[1 − depth, 1]`. At `depth = 0` the multiplier is constant 1.0;
|
||
/// at `depth = 1` it swings between `0.0` and `1.0`.
|
||
///
|
||
/// Output is mono.
|
||
///
|
||
/// - Parameter dry: Dry mono input signal.
|
||
/// - Returns: Amplitude-modulated mono output.
|
||
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
||
// lfoScale = depth × lfoDepthScale → LFO swing half-amplitude
|
||
// lfoOffset = 1 − depth × lfoDepthScale → LFO centre, so peak = 1.0, trough = 1 − depth
|
||
let lfoScale: any ValidGeneratorInput = $depth * lfoDepthScale
|
||
let lfoOffset: any ValidGeneratorInput = 1.0 - ($depth * lfoDepthScale)
|
||
let lfo: OscSin.Audio = OscSin.Audio(
|
||
freq: $rate,
|
||
mul: lfoScale,
|
||
add: lfoOffset
|
||
)
|
||
return dry * lfo
|
||
}
|
||
}
|