// 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). /// /// - Parameters: /// - input: Audio-rate signal to decimate. /// - downsample: Hold length in samples — effective rate is `sampleRate / downsample`. Control /// rate; clamped to `>= 1` (`1` is transparent). 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 /// Largest accepted decimation factor — bounds the control read so the `Int` conversion can /// never overflow on a NaN/inf/huge input. private let maximumFactor: Double = 1_048_576.0 /// 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 } // Sanitise the control read (finite + clamped) so the `Int` conversion can't overflow. let raw: Double = _downsample.floatValue(graph: graph) let safeFactor: Double = raw.isFinite ? min(max(raw, Double(minimumFactor)), maximumFactor) : Double(minimumFactor) let factor: Int = Int(safeFactor.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..