diff --git a/Sources/LyrebirdShoeGaze/Generators/BitReduce.swift b/Sources/LyrebirdShoeGaze/Generators/BitReduce.swift index 5baa3eb..3521f21 100644 --- a/Sources/LyrebirdShoeGaze/Generators/BitReduce.swift +++ b/Sources/LyrebirdShoeGaze/Generators/BitReduce.swift @@ -57,7 +57,10 @@ public final class BitReduce: GeneratorBase, AudioGenerator { _checkCount(count) guard count > 0 else { return _result } - let bits: Double = min(max(_bits.floatValue(graph: graph), minimumBits), maximumBits) + // Sanitise the control read (finite + clamped) so `Int(bits.rounded())` can't trap on a + // NaN/inf input. + let rawBits: Double = _bits.floatValue(graph: graph) + let bits: Double = rawBits.isFinite ? min(max(rawBits, minimumBits), maximumBits) : minimumBits // Number of quantisation levels = 2^bits, via an integer shift (avoids importing `pow`). // Clamp the shift amount directly too: `1 << bitsInt` is only safe while bits ∈ [1, 16]. let bitsInt: Int = min(max(Int(bits.rounded()), 1), 16) diff --git a/Sources/LyrebirdShoeGaze/Generators/ReverseDelay.swift b/Sources/LyrebirdShoeGaze/Generators/ReverseDelay.swift index c464cf1..f51da20 100644 --- a/Sources/LyrebirdShoeGaze/Generators/ReverseDelay.swift +++ b/Sources/LyrebirdShoeGaze/Generators/ReverseDelay.swift @@ -125,11 +125,16 @@ public final class ReverseDelay: GeneratorBase, AudioGenerator { guard count > 0 else { return _result } let sampleRate: Double = graph.engine.sampleRate - let delaySeconds: Double = _delay.floatValue(graph: graph) + // Sanitise control reads (finite + clamped): a NaN/inf/huge `delay` would otherwise trap the + // seconds→frames `Int` conversion, and a NaN `feedback` would poison the loop. + let maxDelaySeconds: Double = Double(_maxFrames) / sampleRate + let rawDelay: Double = _delay.floatValue(graph: graph) + let delaySeconds: Double = rawDelay.isFinite ? min(max(rawDelay, 0.0), maxDelaySeconds) : 0.0 let rawFeedback: Double = _feedback.floatValue(graph: graph) - let feedback: Double = min(max(rawFeedback, 0.0), maxFeedback) + let feedback: Double = rawFeedback.isFinite ? min(max(rawFeedback, 0.0), maxFeedback) : 0.0 // Target segment length, applied at the next boundary so a read never changes length mid-segment. + // `delaySeconds` is bounded to `maxDelaySeconds`, so the `Int` conversion cannot overflow. let targetLen: Int = min(max(Int((delaySeconds * sampleRate).rounded()), 1), _maxFrames) let inputSamples: [Double] = _inputIsGenerator diff --git a/Sources/LyrebirdShoeGaze/Generators/SampleRateDecimate.swift b/Sources/LyrebirdShoeGaze/Generators/SampleRateDecimate.swift index ca6d80e..c15cf5c 100644 --- a/Sources/LyrebirdShoeGaze/Generators/SampleRateDecimate.swift +++ b/Sources/LyrebirdShoeGaze/Generators/SampleRateDecimate.swift @@ -41,6 +41,10 @@ public final class SampleRateDecimate: GeneratorBase, AudioGenerator { /// 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: @@ -64,7 +68,12 @@ public final class SampleRateDecimate: GeneratorBase, AudioGenerator { _checkCount(count) guard count > 0 else { return _result } - let factor: Int = max(minimumFactor, Int(_downsample.floatValue(graph: graph).rounded())) + // 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) diff --git a/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift b/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift index a97d0f1..978de88 100644 --- a/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift +++ b/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift @@ -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 { diff --git a/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift b/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift index cf611b8..0e75e66 100644 --- a/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift +++ b/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift @@ -95,6 +95,15 @@ struct ShimmerReverbTests { #expect(abs(h / l - 2.0) < 0.01, "DC gain should be exactly linear (ratio 2.0)") } + @Test func nonFiniteControlsProduceFiniteOutput() throws { + // NaN control reads must be sanitised (not trap the seconds→frames conversion nor poison + // the feedback): the output must stay finite. + let samples: [Float] = try render( + oscFreq: 440.0, dc: 0.0, size: Double.nan, decay: Double.nan, shimmer: Double.nan + ) + #expect(samples.allSatisfy { $0.isFinite }, "non-finite controls produced NaN/inf output") + } + @Test func decayChangesTheTail() throws { // Small tank so feedback round-trips compound within the reader's window; different decay // must change the tail (direction-agnostic — a tuned comb is not monotonic in level).