// BassOctaverPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Monophonic bass octaver modeled on the Boss OC-2 Octave. /// /// Adds one and two sub-octaves below the input, mixed against the direct signal — the /// three-knob OC-2 panel layout (OCT1 = −1 octave, OCT2 = −2 octaves, Direct = dry level). /// /// Two algorithms share the knobs, picked by the `mode` switch: /// /// - **Flip-flop** (default): the authentic OC-2 path. The input is low-passed to isolate /// the fundamental, hard-limited into a square wave, then clocked through a ``SubOctaveDivider`` /// T flip-flop chain to produce `±1` square sub-octaves an octave and two octaves down. Those /// squares are amplitude-followed off the dry signal so they track its dynamics, then /// low-passed to tame the square edges. Strictly monophonic; glitches on chords like the /// original. Tracks low bass fundamentals (sub-100 Hz) cleanly because the divider works in /// the time domain. /// - **Tracking**: an FFT pitch tracker (`FreqTracker`) drives square oscillators at half and /// quarter the detected frequency. Cleaner and glitch-free, but limited by the tracker's /// ~90 Hz detection floor — best for notes above the low bass register. Flip-flop is the /// bass mode. /// /// ## Knobs /// - `oct1`: −1 octave voice level in `[0, 1]`. /// - `oct2`: −2 octave voice level in `[0, 1]`. /// - `direct`: Dry signal level in `[0, 1]`. /// /// ## Switches /// - `mode` (two-state): `0` = Flip-flop (authentic OC-2), `1` = Tracking (pitch-tracked). public final class BassOctaverPedal: Pedal { // MARK: - Parameters /// −1 octave voice level in `[0, 1]`. @UIParam public var oct1: Double = 0.7 /// −2 octave voice level in `[0, 1]`. @UIParam public var oct2: Double = 0.0 /// Dry (direct) signal level in `[0, 1]`. @UIParam public var direct: Double = 1.0 /// Algorithm selector. `0` = Flip-flop (authentic OC-2), `1` = Tracking. Driven by a two-state switch. @UIParam public var mode: Double = 0.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 /// DC-removal high-pass cutoff in Hz, applied before the divider's comparator. private let dcBlockFreq: Double = 30.0 /// Pre-divider low-pass cutoff in Hz. Isolates the fundamental so upper harmonics do not /// double-trigger the comparator an octave too high. private let conditionCutoff: Double = 500.0 /// Pre-gain into the `tanh` limiter. Large enough to square the conditioned signal to `±1` /// so the comparator sees clean edges regardless of input level. private let limiterDrive: Double = 50.0 /// Low-pass cutoff in Hz applied to each square sub-octave to soften its harsh edges. private let subToneCutoff: Double = 800.0 /// Butterworth Q (no resonant peak) for all conditioning filters. private let filterQ: Double = 0.707 /// Envelope-follower attack/release times in seconds; the sub-octaves track the dry dynamics. private let envAttack: Double = 0.005 private let envRelease: Double = 0.1 /// Frequency multipliers for the tracking-mode oscillators (one and two octaves down). private let octave1Ratio: Double = 0.5 private let octave2Ratio: Double = 0.25 /// Pitch-tracker detection bounds in Hz for tracking mode. private let trackMinFreq: Double = 60.0 private let trackMaxFreq: Double = 1000.0 /// Division ratios for the flip-flop divider. private let divisorOctave1: Int = 2 private let divisorOctave2: Int = 4 /// Half-step bias added before `Select`'s truncate-toward-zero index, turning the `mode` /// switch into a round-to-nearest position selector. private let selectRoundingBias: Double = 0.5 /// Switch position values. private let modeFlipFlopValue: Double = 0.0 private let modeTrackingValue: Double = 1.0 // MARK: - Ranges private let levelRange: ClosedRange = 0.0 ... 1.0 // MARK: - Knobs public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "OCT 1", node: $oct1, range: levelRange), PedalKnob(label: "OCT 2", node: $oct2, range: levelRange), PedalKnob(label: "Direct", node: $direct, range: levelRange), ] } // MARK: - Switches public override var effectSwitches: [PedalSwitch] { [ PedalSwitch( label: "Mode", node: $mode, options: [ PedalSwitch.Option(title: "Flip", value: modeFlipFlopValue), PedalSwitch.Option(title: "Track", value: modeTrackingValue), ] ) ] } // MARK: - Init /// Creates a bass octaver 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: "Bass Octaver", inputBus: inputBus, outputBus: outputBus, initialMix: BassOctaverPedal.defaultMix ) } // MARK: - Effect public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { // Sub-octaves track the dry signal's amplitude envelope. let env: AmplitudeFollower.Audio = AmplitudeFollower.Audio( in: dry, attackTime: envAttack, releaseTime: envRelease ) // Flip-flop branch: condition → limit → divide. let (oct1Flip, oct2Flip): (any ValidGeneratorInput, any ValidGeneratorInput) = flipFlopVoices(dry: dry, env: env) // Tracking branch: pitch-track → square oscillators. let (oct1Track, oct2Track): (any ValidGeneratorInput, any ValidGeneratorInput) = trackingVoices(dry: dry, env: env) // Per-voice mode select. `Select` advances both branches each block (warm state). let modeWhich: any ValidGeneratorInput = $mode + selectRoundingBias let oct1Voice: Select.Audio = Select.Audio( which: modeWhich, inputs: [oct1Flip, oct1Track] ) let oct2Voice: Select.Audio = Select.Audio( which: modeWhich, inputs: [oct2Flip, oct2Track] ) // OC-2 mixer: direct + OCT1 + OCT2, each scaled by its level knob. let directVoice: any ValidGeneratorInput = dry * $direct let sub1: any ValidGeneratorInput = oct1Voice * $oct1 let sub2: any ValidGeneratorInput = oct2Voice * $oct2 return directVoice + sub1 + sub2 } /// Builds the authentic flip-flop sub-octave voices: condition the input to a square, /// clock the divider chain, amplitude-follow, then soften the square edges. private func flipFlopVoices( dry: any ValidGeneratorInput, env: AmplitudeFollower.Audio ) -> (any ValidGeneratorInput, any ValidGeneratorInput) { let dcBlocked: RBJHighPass.Audio = RBJHighPass.Audio( input: dry, freq: dcBlockFreq, q: filterQ ) let conditioned: RBJLowPass.Audio = RBJLowPass.Audio( input: dcBlocked, freq: conditionCutoff, q: filterQ ) let limited: Tanh.Audio = Tanh.Audio(conditioned, mul: limiterDrive) let div1: SubOctaveDivider = SubOctaveDivider(input: limited, divisor: divisorOctave1) let div2: SubOctaveDivider = SubOctaveDivider(input: limited, divisor: divisorOctave2) let oct1: any ValidGeneratorInput = softened(div1) * env let oct2: any ValidGeneratorInput = softened(div2) * env return (oct1, oct2) } /// Builds the pitch-tracked sub-octave voices: detect the fundamental, run square /// oscillators an octave and two octaves below it, amplitude-follow, then soften. private func trackingVoices( dry: any ValidGeneratorInput, env: AmplitudeFollower.Audio ) -> (any ValidGeneratorInput, any ValidGeneratorInput) { let tracker: FreqTracker = FreqTracker( in: dry, minFreq: trackMinFreq, maxFreq: trackMaxFreq ) let freq: any ValidGeneratorInput = tracker.freq let osc1: OscSquare.Audio = OscSquare.Audio(freq: freq * octave1Ratio) let osc2: OscSquare.Audio = OscSquare.Audio(freq: freq * octave2Ratio) let oct1: any ValidGeneratorInput = softened(osc1) * env let oct2: any ValidGeneratorInput = softened(osc2) * env return (oct1, oct2) } /// Low-passes a raw square sub-octave to soften its edges before mixing. private func softened(_ square: any ValidGeneratorInput) -> RBJLowPass.Audio { RBJLowPass.Audio(input: square, freq: subToneCutoff, q: filterQ) } }