077a8ad9f0
- Add deterministic generator-level tests: BitReduceTests (level snapping + 1-bit floor), SampleRateDecimateTests (sample-and-hold groups), and ReverseDelayTests (silent-then-flat-replay). Asserts are output-gain independent. - Add class-level `- Parameters:` doc blocks to BitReduce, SampleRateDecimate, and ReverseDelay (dual-location param docs). - BitReduce: clamp the shift amount directly and note shift-safety; clarify mid-tread wording. - ReverseDelay: comment the unreachable baseAddress guard. Suite 247. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
95 lines
3.3 KiB
Swift
95 lines
3.3 KiB
Swift
// 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
|
|
|
|
/// 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
|
|
}
|
|
}
|