Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/NoiseGatePedal.swift
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

153 lines
5.5 KiB
Swift
Raw Permalink 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.
// NoiseGatePedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Noise-gate pedal: an amplitude-tracked gate with a tuneable residual floor.
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry ─┬─────────────────────────────────────────────────────────┐
/// └→ AmplitudeFollower → Schmidt(lo: thr, hi: thr×ratio) │
/// → LinLin(0…1 → floor…1) │
/// → Lag(smooth) ───────────── × ──┴→ mono out
/// ```
///
/// An `AmplitudeFollower` tracks the dry signal level. A Schmitt trigger turns that
/// envelope into an open/closed gate with hysteresis (opens above `threshold × openRatio`,
/// closes below `threshold`) so the gate does not chatter around the threshold. The 0/1
/// gate is mapped to a gain in `[floor, 1]` and smoothed before multiplying the dry signal:
/// when the gate is closed the signal is attenuated to `floor` rather than hard-muted, so
/// `floor` sets how much of the quiet signal/noise tail bleeds through.
///
/// The wet/dry mix and bypass switching are handled by the `Pedal` base class.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - threshold: Gate close threshold as a linear amplitude. Envelope below this closes
/// the gate. Range `[0, 0.2]`. Defaults to `0.02`.
/// - floor: Residual gain applied while the gate is closed, in `[0, 1]`. `0` = full
/// mute, `1` = no gating. Range `[0, 1]`. Defaults to `0.0`.
public final class NoiseGatePedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Noise gate that silences hiss and hum below a tunable threshold."
}
// MARK: - Parameters
/// Gate close threshold (linear amplitude). Range [0, 0.2].
@UIParam public var threshold: Double = 0.02
/// Residual gain while closed, [0, 1]. Range [0, 1].
@UIParam public var floor: Double = 0.0
// MARK: - Constants
/// Default wet/dry blend — fully wet; the gate sits in-line.
private static let defaultMix: Double = 1.0
/// Envelope-follower attack time, in seconds — fast so the gate opens promptly on a note.
private let detectAttack: Double = 0.002
/// Envelope-follower release time, in seconds — slower so the gate holds through decay.
private let detectRelease: Double = 0.05
/// Open-threshold multiplier. The gate opens above `threshold × openRatio` and closes
/// below `threshold`, giving hysteresis that prevents chatter near the threshold.
private let openRatio: Double = 2.0
/// Gain-smoothing lag time, in seconds — removes the click of an instantaneous 0↔1 gate.
private let gainSmoothTime: Double = 0.01
/// Gate signal bounds for the `LinLin` floor map.
private let gateLo: Double = 0.0
private let gateHi: Double = 1.0
/// Upper gain the open gate maps to (unity).
private let openGain: Double = 1.0
// MARK: - UI knobs
/// Threshold knob range (linear amplitude).
private let thresholdRange: ClosedRange<Double> = 0.0 ... 0.2
/// Floor knob range (residual gain).
private let floorRange: ClosedRange<Double> = 0.0 ... 1.0
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Threshold", node: $threshold, range: thresholdRange),
PedalKnob(label: "Floor", node: $floor, range: floorRange),
]
}
// MARK: - Init
/// Creates a noise-gate 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: "Noise Gate",
inputBus: inputBus,
outputBus: outputBus,
initialMix: NoiseGatePedal.defaultMix
)
}
// MARK: - Effect
/// Gates the dry signal below `threshold`, attenuating closed passages to `floor`.
///
/// Output is mono.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: Gated mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Track the dry signal's amplitude envelope.
let envelope: AmplitudeFollower.Audio = AmplitudeFollower.Audio(
in: dry,
attackTime: detectAttack,
releaseTime: detectRelease
)
// Hysteretic open/closed gate: opens above threshold×openRatio, closes below threshold.
let gate: Schmidt.Audio = Schmidt.Audio(
input: envelope,
lo: $threshold,
hi: $threshold * openRatio
)
// Map the 0/1 gate to a gain in [floor, 1] so closed passages bleed `floor`, not silence.
let gain: LinLin.Audio = LinLin.Audio(
gate,
inLo: gateLo,
inHi: gateHi,
outLo: $floor,
outHi: openGain
)
// Smooth the gain to remove the click of an instantaneous gate transition.
let smoothed: Lag.Audio = Lag.Audio(
input: gain,
time: gainSmoothTime
)
return dry * smoothed
}
}