Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/ModulationPedal.swift
T
josh 2772abdc56 Release 2.1.0: tempo-synced pedal parameters, editable BPM, control mapping badges
Tempo sync
- Lock a delay time or modulation LFO rate to a note division (whole..sixteenth,
  dotted, triplet) of the shared clock. Value is derived from the tempo and follows
  tap tempo, the stepper, the editable BPM field, and MIDI.
- Delay time: Delay, FeedbackDelay, PingPong, ReverseDelay, MultiTap (per tap).
  LFO rate (inverse mapping): Tremolo, Vibrato, Chorus, Flanger, Phaser, Modulation.
- Core types NoteDivision / TempoSync / TempoSyncableParam; Pedal.tempoSyncableParams /
  tempoSync / applyTempoSync / setSync; ShoeGaze.setBpm broadcast recomputes all pedals;
  tempoSync persisted in PedalState (lenient decode) and recomputed on load + recall.
- Observable TempoSyncRevision so the sync badge refreshes on any state-change path.

UI
- Division picker with drawn music-notation glyphs (NoteGlyphView + rasterized cache):
  noteheads, stems, flags, augmentation dot, bracketed 3 for triplets. Presented as a
  height-capped popover so it stays on-screen on iOS.
- Editable BPM text field: integer-only, live-filtered, clamped 30-300, commit on
  return / Done / blur.
- Divisions that clamp at the current tempo are disabled in the picker; an active
  clamped sync shows a warning marker.
- In-context control mapping badges (PC / CC / KEY) per pedal control, with a legend.

Delays
- Maximum delay time raised to 5 s across the delay pedals (was 1-2 s).

Docs: CHANGELOG.md, RELEASES.md, docs/plans/2.2-tempo-synced-parameters.md.
All 454 tests pass; passed lyrebird_external_review (RT-safe, main-thread param writes).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 08:47:22 -07:00

133 lines
4.3 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 tempoSyncableParams: [TempoSyncableParam] {
[
TempoSyncableParam(key: "rate", label: "Rate", node: $rate, kind: .rate, range: rateRange),
]
}
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
)
}
}