Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/PolyOctavePedal.swift
T
josh 20b2034fed feat(poly-octave): EHX POG-style polyphonic octave pedal
Add PolyOctavePedal: a fixed-interval polyphonic octave generator. Four
PitchShift voices at exact power-of-two ratios — −2, −1, +1, +2 octaves,
each single-stage within PitchShift's [0.25, 4.0] clamp — stacked under a
dry voice, each with its own level. Because the ratios need no pitch
tracking, chords stack as cleanly as single notes.

A Tone low-pass shapes the shifted voices (dry stays clean). An Attack
knob adds the POG's volume swell via a "slow-gear" gain: the ratio of a
slow envelope follower to a fast one, clamped to [0, 1] and guarded
against divide-by-zero — unity (instant) at Attack 0, blooming in as it
rises, independent of playing level. The five voice levels sum without an
internal limiter (as with BassOctaver); the level knob and board master
set final gain.

Wire into PedalType, the registry, and the pitch category; add to the
README pitch catalog. 12 tests: per-voice pitch (±1/±2 oct + unison) by
autocorrelation, swell neutral-vs-bloom, seeded determinism, a click-free
bypass flip at the real 512-sample block, knob labels, preset round-trip,
registry presence.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-24 09:53:43 -08:00

243 lines
11 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.
// PolyOctavePedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Polyphonic octave generator in the EHX POG mould.
///
/// Stacks four fixed pitch-shifted voices — two octaves down, one octave down, one octave up, two
/// octaves up — under the dry note, each with its own level, so a single note becomes an
/// organ-like chord of octaves. Unlike the ``WhammyPedal`` (one voice swept by an expression
/// pedal), every voice sounds at once at a **fixed** interval, and because the ratios are exact
/// powers of two the shifter needs no pitch tracking — chords shift as cleanly as single notes.
///
/// Each voice is a `PitchShift` — Lyrebird's four-head SOLA shifter, whose `pitchRatio` clamps to
/// `[0.25, 4.0]`, i.e. exactly ±2 octaves, so every voice here is a single stage (no cascade).
///
/// The five voice levels sum without an internal limiter (as with the OC-2-style
/// ``BassOctaverPedal``), so driving several to full can push the sum above unity; the pedal's
/// `level` knob and the board output master set the final gain, like a real mixer's channel sum.
///
/// A low-pass **Tone** control tames the shifted voices (the dry stays untouched), and an
/// **Attack** control adds the POG's signature volume **swell**: turned up, the octave voices fade
/// in after each note instead of sounding instantly, for a bowed / synth-pad bloom. The swell is a
/// "slow-gear" gain — the ratio of a slow envelope follower to a fast one — so at `Attack = 0` the
/// two followers are identical and the gain is unity (voices respond instantly), and the swell
/// never depends on how loud you play.
///
/// ## Knobs
/// - `subTwo`: −2 octave voice level in `[0, 1]`.
/// - `subOne`: −1 octave voice level in `[0, 1]`.
/// - `upOne`: +1 octave voice level in `[0, 1]`.
/// - `upTwo`: +2 octave voice level in `[0, 1]`.
/// - `dry`: Dry (direct) signal level in `[0, 1]`.
/// - `attack`: Octave-voice swell in `[0, 1]`. `0` = instant; higher = slower volume bloom.
/// - `tone`: Octave-voice low-pass cutoff in Hz. Range `[500, 12000]`.
public final class PolyOctavePedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Stacks fixed octave voices under your note — organ-like, with a volume swell."
}
// MARK: - Parameters
/// −2 octave voice level in `[0, 1]`.
@UIParam public var subTwo: Double = 0.0
/// −1 octave voice level in `[0, 1]`.
@UIParam public var subOne: Double = 0.0
/// +1 octave voice level in `[0, 1]`.
@UIParam public var upOne: Double = 0.5
/// +2 octave voice level in `[0, 1]`.
@UIParam public var upTwo: Double = 0.0
/// Dry (direct) signal level in `[0, 1]`.
@UIParam public var dry: Double = 1.0
/// Octave-voice swell in `[0, 1]`. `0` = instant; higher = slower volume bloom after each note.
@UIParam public var attack: Double = 0.0
/// Octave-voice low-pass cutoff in Hz. Range `[500, 12000]`.
@UIParam public var tone: Double = 8000.0
// MARK: - Constants
/// Pedal blends everything internally via the level knobs, so the base wet/dry stays fully wet.
private static let defaultMix: Double = 1.0
/// Fixed `PitchShift` playback ratios for the four voices (exact powers of two).
private let subTwoRatio: Double = 0.25 // −2 octaves
private let subOneRatio: Double = 0.5 // −1 octave
private let upOneRatio: Double = 2.0 // +1 octave
private let upTwoRatio: Double = 4.0 // +2 octaves
/// Grain window for each `PitchShift`, in seconds. A touch over the shifter's `0.1` default to
/// soften the periodic four-head comb without adding audible lag.
private let voiceWindowSize: Double = 0.11
/// Per-head timing jitter for each `PitchShift`, in seconds. Breaks the fixed-grid comb; kept
/// small so the voices stay tight.
private let voiceTimeDispersion: Double = 0.02
/// Fixed PRNG seeds, one per voice, so a given setting renders identically every time
/// (reproducible audio and exact-pitch tests). Distinct per voice so their grain dispersion
/// sequences decorrelate instead of jittering in lockstep.
private let subTwoSeed: UInt64 = 0x9E37_79B9_7F4A_7C15
private let subOneSeed: UInt64 = 0xC2B2_AE3D_27D4_EB4F
private let upOneSeed: UInt64 = 0x1656_67B1_9E37_79F9
private let upTwoSeed: UInt64 = 0x2545_F491_4F6C_DD1D
/// Fast envelope-follower attack for the swell's denominator, in seconds — near-instant, so it
/// tracks note onsets tightly. Also the floor of the mapped slow attack, so `attack = 0` makes
/// the two followers identical and the swell gain unity (neutral).
private let swellFastAttack: Double = 0.005
/// Slowest envelope-follower attack the `attack` knob maps to, in seconds (at `attack = 1`).
private let swellMaxAttack: Double = 1.5
/// Shared release time for both swell followers, in seconds. Equal on both so only the attack
/// differs between them.
private let swellRelease: Double = 0.15
/// Floor added to the swell denominator before the reciprocal, so a silent input (both
/// followers near zero) cannot divide by zero. Kept well below any real signal level
/// (`1e-6` ≈ −120 dB) so the neutral gain stays ~1 across the whole playing range — a larger
/// floor would pull the `attack = 0` gain toward `0.5` for quiet input.
private let swellDenomFloor: Double = 1.0e-6
/// Clamp bounds for the swell gain. With equal release times the two followers track in
/// lockstep once settled, so the ratio stays `≤ 1` in normal use (it dips below `1` only during
/// the attack ramp, when the slow follower lags); the ceiling is a defensive guard against a
/// transient overshoot from the reciprocal, and the floor keeps the gain non-negative.
private let swellGainFloor: Double = 0.0
private let swellGainCeil: Double = 1.0
/// Butterworth Q (no resonant peak) for the tone low-pass.
private let toneQ: Double = 0.707
// MARK: - Ranges
private let levelRange: ClosedRange<Double> = 0.0 ... 1.0
private let attackRange: ClosedRange<Double> = 0.0 ... 1.0
private let toneRange: ClosedRange<Double> = 500.0 ... 12000.0
// MARK: - Knobs
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "-2 Oct", node: $subTwo, range: levelRange),
PedalKnob(label: "-1 Oct", node: $subOne, range: levelRange),
PedalKnob(label: "+1 Oct", node: $upOne, range: levelRange),
PedalKnob(label: "+2 Oct", node: $upTwo, range: levelRange),
PedalKnob(label: "Dry", node: $dry, range: levelRange),
PedalKnob(label: "Attack", node: $attack, range: attackRange),
PedalKnob(label: "Tone", node: $tone, range: toneRange),
]
}
// MARK: - Init
/// Creates a polyphonic octave pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the dry input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(
name: "Poly Octave",
inputBus: inputBus,
outputBus: outputBus,
initialMix: PolyOctavePedal.defaultMix
)
}
// MARK: - Effect
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Four fixed-ratio voices. Each ratio is within PitchShift's ±2-octave clamp, so a single
// stage suffices (no cascade, unlike the Whammy's 3-octave Dive Bomb).
let subTwoVoice: PitchShift.Audio = makeVoice(dry: dry, ratio: subTwoRatio, seed: subTwoSeed)
let subOneVoice: PitchShift.Audio = makeVoice(dry: dry, ratio: subOneRatio, seed: subOneSeed)
let upOneVoice: PitchShift.Audio = makeVoice(dry: dry, ratio: upOneRatio, seed: upOneSeed)
let upTwoVoice: PitchShift.Audio = makeVoice(dry: dry, ratio: upTwoRatio, seed: upTwoSeed)
// Octave bus: each voice scaled by its level knob.
let octaveBus: any ValidGeneratorInput =
(subTwoVoice * $subTwo)
+ (subOneVoice * $subOne)
+ (upOneVoice * $upOne)
+ (upTwoVoice * $upTwo)
// Swell: a "slow-gear" gain applied to the octave bus only (the dry stays immediate).
let swelled: any ValidGeneratorInput = octaveBus * swellGain(dry: dry)
// Tone shapes the shifted voices, not the dry.
let octaveToned: RBJLowPass.Audio = RBJLowPass.Audio(
input: swelled,
freq: $tone,
q: toneQ
)
return (dry * $dry) + octaveToned
}
/// Builds one fixed-ratio octave voice.
///
/// - Parameters:
/// - dry: Dry input to shift.
/// - ratio: Fixed playback ratio (power of two) within PitchShift's `[0.25, 4.0]` clamp.
/// - seed: Per-voice PRNG seed for reproducible grain dispersion.
/// - Returns: The pitch-shifted voice (pre-level).
private func makeVoice(
dry: any ValidGeneratorInput,
ratio: Double,
seed: UInt64
) -> PitchShift.Audio {
PitchShift.Audio(
input: dry,
pitchRatio: ratio,
windowSize: voiceWindowSize,
timeDispersion: voiceTimeDispersion,
seed: seed
)
}
/// Builds the octave-voice swell gain: the ratio of a slow envelope follower to a fast one,
/// clamped to `[0, 1]`. Unity when `attack = 0` (both followers identical), dipping and then
/// recovering as `attack` rises, so the voices bloom in after each onset independent of level.
///
/// - Parameter dry: Dry input the swell tracks.
/// - Returns: A `[0, 1]` gain to multiply the octave bus by.
private func swellGain(dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// attack [0, 1] → slow-follower attack [swellFastAttack, swellMaxAttack] seconds.
let attackSpan: Double = swellMaxAttack - swellFastAttack
let slowAttackTime: any ValidGeneratorInput = ($attack * attackSpan) + swellFastAttack
let fastEnv: AmplitudeFollower.Audio = AmplitudeFollower.Audio(
in: dry,
attackTime: swellFastAttack,
releaseTime: swellRelease
)
let slowEnv: AmplitudeFollower.Audio = AmplitudeFollower.Audio(
in: dry,
attackTime: slowAttackTime,
releaseTime: swellRelease
)
// gain = slowEnv / (fastEnv + floor), clamped to [0, 1].
let denom: any ValidGeneratorInput = fastEnv + swellDenomFloor
let ratio: any ValidGeneratorInput = slowEnv * Reciprocal.Audio(denom)
return Clip.Audio(ratio, lo: swellGainFloor, hi: swellGainCeil)
}
}