// BitcrusherPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Lo-fi bitcrusher: sample-rate decimation followed by bit-depth reduction. /// /// A sample-and-hold drops the effective sample rate (aliasing, stair-stepping); a quantiser then /// reduces the bit depth (gritty quantisation noise). A post `Tone` low-pass tames the harsh /// aliasing for a more usable lo-fi voice. Wraps the ``SampleRateDecimate`` and ``BitReduce`` /// generators, run fully wet so the pedal's `Mix` knob sets the blend. /// /// ## Knobs /// - `crush`: Sample-rate decimation factor (hold length in samples). Range `[1, 50]`. /// - `bits`: Bit depth. Range `[1, 16]`. /// - `tone`: Post low-pass cutoff in Hz. Range `[800, 8000]`. public final class BitcrusherPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Lo-fi crusher: drops the sample rate and bit depth for gritty digital grunge." } // MARK: - Parameters /// Sample-rate decimation factor (hold length in samples). Range [1, 50]. @UIParam public var crush: Double = 8.0 /// Bit depth. Range [1, 16]. @UIParam public var bits: Double = 8.0 /// Post low-pass cutoff in Hz. Range [800, 8000]. @UIParam public var tone: Double = 4000.0 // MARK: - Constants /// Bitcrusher is heard fully wet; the `Mix` knob can dial it back. private static let defaultMix: Double = 1.0 /// Butterworth Q (no resonant peak) for the tone low-pass. private let filterQ: Double = 0.707 // MARK: - Ranges private let crushRange: ClosedRange = 1.0 ... 50.0 private let bitsRange: ClosedRange = 1.0 ... 16.0 private let toneRange: ClosedRange = 800.0 ... 8000.0 // MARK: - Knobs public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Crush", node: $crush, range: crushRange), PedalKnob(label: "Bits", node: $bits, range: bitsRange), PedalKnob(label: "Tone", node: $tone, range: toneRange), ] } // MARK: - Init /// Creates a bitcrusher 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: "Bitcrusher", inputBus: inputBus, outputBus: outputBus, initialMix: BitcrusherPedal.defaultMix ) } // MARK: - Effect public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { let decimated: SampleRateDecimate = SampleRateDecimate(input: dry, downsample: $crush) let crushed: BitReduce = BitReduce(input: decimated, bits: $bits) return RBJLowPass.Audio(input: crushed, freq: $tone, q: filterQ) } }