Harden new generators against non-finite control inputs
Sanitise control-rate reads (finite check + clamp) before they reach seconds→frames Int conversions or feedback math, so a NaN/inf/huge size, delay, downsample, bits, decay, shimmer, damp, or feedback can never trap the Int() conversion nor poison a feedback loop: - ShimmerReverb: clamp() helper; bound size to maxSizeSeconds before Int. - ReverseDelay: finite-clamp delay (to maxDelaySeconds) and feedback. - SampleRateDecimate: clamp downsample to [1, maximumFactor] before Int. - BitReduce: finite-guard bits before Int(bits.rounded()). Adds a ShimmerReverb regression test (NaN controls → finite output). Keeps `import Foundation` in ShimmerReverb (cos for the init-time Hann table) with a clarifying comment. Suite 256. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Foundation // `cos` for the init-time Hann window (not used on the RT path)
|
||||
import LyrebirdSynth
|
||||
|
||||
/// Shared zero-fill fallback block, allocated once at module load, so the RT path never allocates.
|
||||
@@ -154,12 +154,17 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator {
|
||||
guard count > 0 else { return _result }
|
||||
|
||||
let sampleRate: Double = graph.engine.sampleRate
|
||||
let sizeSeconds: Double = _size.floatValue(graph: graph)
|
||||
let decay: Double = min(max(_decay.floatValue(graph: graph), 0.0), maxDecay)
|
||||
let shimmer: Double = min(max(_shimmer.floatValue(graph: graph), 0.0), maxShimmer)
|
||||
let damp: Double = min(max(_damp.floatValue(graph: graph), 0.0), 1.0)
|
||||
// Sanitise control reads (finite + clamped) so a NaN/inf/huge value can never reach the
|
||||
// feedback math or trap the seconds→frames `Int` conversion.
|
||||
let maxSizeSeconds: Double = Double(_maxDelayFrames) / sampleRate
|
||||
let sizeSeconds: Double = ShimmerReverb.clamp(_size.floatValue(graph: graph), minSizeSeconds, maxSizeSeconds)
|
||||
let decay: Double = ShimmerReverb.clamp(_decay.floatValue(graph: graph), 0.0, maxDecay)
|
||||
let shimmer: Double = ShimmerReverb.clamp(_shimmer.floatValue(graph: graph), 0.0, maxShimmer)
|
||||
let damp: Double = ShimmerReverb.clamp(_damp.floatValue(graph: graph), 0.0, 1.0)
|
||||
|
||||
let minFrames: Int = max(1, Int((minSizeSeconds * sampleRate).rounded()))
|
||||
// `sizeSeconds` is bounded to `maxSizeSeconds`, so `sizeSeconds · sampleRate <= _maxDelayFrames`
|
||||
// and the `Int` conversion cannot overflow.
|
||||
let sizeFrames: Int = min(max(Int((sizeSeconds * sampleRate).rounded()), minFrames), _maxDelayFrames)
|
||||
let halfGrain: Int = _grainFrames / 2
|
||||
|
||||
@@ -258,6 +263,13 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator {
|
||||
return y
|
||||
}
|
||||
|
||||
/// Returns `v` clamped to `[lo, hi]`, or `lo` when `v` is not finite — sanitising control reads
|
||||
/// before they reach the feedback math or an `Int` frame conversion.
|
||||
private static func clamp(_ v: Double, _ lo: Double, _ hi: Double) -> Double {
|
||||
guard v.isFinite else { return lo }
|
||||
return min(max(v, lo), hi)
|
||||
}
|
||||
|
||||
/// Wraps `index` into `[0, length)` (handles a single negative wrap, as produced by
|
||||
/// `writePos - offset` with `offset <= length`).
|
||||
private static func wrap(_ index: Int, _ length: Int) -> Int {
|
||||
|
||||
Reference in New Issue
Block a user