Add Bitcrusher pedal with SampleRateDecimate and BitReduce generators
Two new RT-safe generators (ShoeGaze): SampleRateDecimate (sample-and-hold decimator, control-rate downsample factor) and BitReduce (quantise to 2^bits levels via integer shift, control-rate bit depth). BitcrusherPedal chains decimate → bit-reduce → tone low-pass (Crush/Bits/Tone). Registered with enclosure color + palette slot. Tests: build/non-silent, crush-changes-output, bits-change-output, knobs, registry. Suite 242. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
// BitReduce.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import LyrebirdSynth
|
||||
|
||||
/// Shared zero-fill fallback block, allocated once at module load, so the RT path never allocates.
|
||||
private let bitReduceZeroBlock: [Double] = makeSampleBuffer()
|
||||
|
||||
/// Bit-depth reducer: quantises the signal to `2^bits` amplitude levels, producing the gritty
|
||||
/// quantisation noise of the "bit" half of a lo-fi / bitcrusher effect.
|
||||
///
|
||||
/// `bits` is read at control rate (once per block) and clamped to `[1, 16]`. Each sample is
|
||||
/// rounded to the nearest step of size `2^(1-bits)` over the `[-1, 1]` range. Output is mono.
|
||||
public final class BitReduce: GeneratorBase, AudioGenerator {
|
||||
|
||||
/// Resolved input signal.
|
||||
let _input: any ValidGeneratorInput
|
||||
|
||||
/// `true` when `_input` resolved to a generator; classified once so the RT path runs no
|
||||
/// `is`/`as?` check.
|
||||
let _inputIsGenerator: Bool
|
||||
|
||||
/// Quantisation bit depth, read at block rate. Pass a control-rate node or scalar.
|
||||
let _bits: any ValidGeneratorInput
|
||||
|
||||
/// Quantisation bounds.
|
||||
private let minimumBits: Double = 1.0
|
||||
private let maximumBits: Double = 16.0
|
||||
|
||||
/// Creates a bit-depth reducer.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - input: Signal to quantise.
|
||||
/// - bits: Bit depth (control rate). Clamped to `[1, 16]`.
|
||||
public init(
|
||||
input: any ValidGeneratorInput,
|
||||
bits: any ValidGeneratorInput
|
||||
) {
|
||||
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
|
||||
_input = resolvedInput
|
||||
_inputIsGenerator = resolvedInput is any Generator
|
||||
_bits = bits._resolvedInput()
|
||||
super.init()
|
||||
}
|
||||
|
||||
public func _computeSamples(
|
||||
graph: SynthGraph,
|
||||
count: Int
|
||||
) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
guard count > 0 else { return _result }
|
||||
|
||||
let bits: Double = min(max(_bits.floatValue(graph: graph), minimumBits), maximumBits)
|
||||
// Number of quantisation levels = 2^bits, via an integer shift (avoids importing `pow`).
|
||||
let bitsInt: Int = Int(bits.rounded())
|
||||
let levels: Double = Double(1 << bitsInt)
|
||||
// Step between adjacent levels over [-1, 1], and its reciprocal for the round.
|
||||
let step: Double = 2.0 / levels
|
||||
let inverseStep: Double = levels / 2.0
|
||||
|
||||
let inputSamples: [Double] = _inputIsGenerator
|
||||
? (_input.calculatedSamples(graph: graph, count: count).first ?? bitReduceZeroBlock)
|
||||
: bitReduceZeroBlock
|
||||
let inputCount: Int = inputSamples.count
|
||||
let inputScalar: Double = _inputIsGenerator ? 0.0 : _input.floatValue(graph: graph)
|
||||
|
||||
for i: Int in 0..<count {
|
||||
let x: Double = _inputIsGenerator
|
||||
? (i < inputCount ? inputSamples[i] : 0.0)
|
||||
: inputScalar
|
||||
_result[0][i] = (x * inverseStep).rounded() * step
|
||||
}
|
||||
|
||||
return _result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user