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,89 @@
|
||||
// SampleRateDecimate.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 sampleRateDecimateZeroBlock: [Double] = makeSampleBuffer()
|
||||
|
||||
/// Sample-rate reducer (decimator): a sample-and-hold that latches a new input value only once
|
||||
/// every `downsample` samples, holding it in between. The aliased, stair-stepped result is the
|
||||
/// "crushed sample rate" half of a lo-fi / bitcrusher effect.
|
||||
///
|
||||
/// `downsample` is read at control rate (once per block). `1` passes the signal through; `N` holds
|
||||
/// each sample for `N` output samples (effective rate `sampleRate / N`). Output is mono (channel 0).
|
||||
public final class SampleRateDecimate: 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
|
||||
|
||||
/// Downsample factor, read at block rate. Pass a control-rate node or scalar.
|
||||
let _downsample: any ValidGeneratorInput
|
||||
|
||||
/// Samples remaining before the next latch. `0` latches a fresh input on the current sample.
|
||||
var _countdown: Int = 0
|
||||
|
||||
/// Currently held output value.
|
||||
var _held: Double = 0.0
|
||||
|
||||
/// Smallest decimation factor (pass-through).
|
||||
private let minimumFactor: Int = 1
|
||||
|
||||
/// Creates a sample-rate decimator.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - input: Signal to decimate.
|
||||
/// - downsample: Hold length in samples (control rate). Clamped to `>= 1`; `1` is transparent.
|
||||
public init(
|
||||
input: any ValidGeneratorInput,
|
||||
downsample: any ValidGeneratorInput
|
||||
) {
|
||||
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
|
||||
_input = resolvedInput
|
||||
_inputIsGenerator = resolvedInput is any Generator
|
||||
_downsample = downsample._resolvedInput()
|
||||
super.init()
|
||||
}
|
||||
|
||||
public func _computeSamples(
|
||||
graph: SynthGraph,
|
||||
count: Int
|
||||
) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
guard count > 0 else { return _result }
|
||||
|
||||
let factor: Int = max(minimumFactor, Int(_downsample.floatValue(graph: graph).rounded()))
|
||||
|
||||
let inputSamples: [Double] = _inputIsGenerator
|
||||
? (_input.calculatedSamples(graph: graph, count: count).first ?? sampleRateDecimateZeroBlock)
|
||||
: sampleRateDecimateZeroBlock
|
||||
let inputCount: Int = inputSamples.count
|
||||
let inputScalar: Double = _inputIsGenerator ? 0.0 : _input.floatValue(graph: graph)
|
||||
|
||||
var countdown: Int = _countdown
|
||||
var held: Double = _held
|
||||
|
||||
for i: Int in 0..<count {
|
||||
let x: Double = _inputIsGenerator
|
||||
? (i < inputCount ? inputSamples[i] : 0.0)
|
||||
: inputScalar
|
||||
if countdown <= 0 {
|
||||
held = x
|
||||
countdown = factor
|
||||
}
|
||||
countdown -= 1
|
||||
_result[0][i] = held
|
||||
}
|
||||
|
||||
_countdown = countdown
|
||||
_held = held
|
||||
return _result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user