diff --git a/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift b/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift index 978de88..40c0d9f 100644 --- a/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift +++ b/Sources/LyrebirdShoeGaze/Generators/ShimmerReverb.swift @@ -75,6 +75,9 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { private var _allpassBPos: Int = 0 private var _grainCounter: Int = 0 private var _dampState: Double = 0.0 + /// DC-blocker state (previous input/output) on the feedback path. + private var _dcX1: Double = 0.0 + private var _dcY1: Double = 0.0 // MARK: - Constants @@ -90,6 +93,11 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { private let safetyClamp: Double = 8.0 /// Below this magnitude feedback state is flushed to zero (denormal guard). private let denormalFloor: Double = 1.0e-15 + /// DC-blocker pole — a one-pole high-pass on the feedback path. Without it the octave-up of DC + /// (≈ DC) plus the DC-passing damping filter gives the loop a DC gain near `decay + shimmer`, + /// which exceeds 1 at higher settings and accumulates into a DC offset. ~7 Hz cutoff @ 44.1 kHz + /// — sub-bass only, so the shimmer wash is unaffected. + private let dcBlockCoeff: Double = 0.999 // MARK: - Init @@ -179,6 +187,8 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { var dampState: Double = _dampState var allpassAPos: Int = _allpassAPos var allpassBPos: Int = _allpassBPos + var dcX1: Double = _dcX1 + var dcY1: Double = _dcY1 _tank.withUnsafeMutableBufferPointer { tankBuffer in _allpassA.withUnsafeMutableBufferPointer { apABuffer in @@ -216,8 +226,13 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { hann[grainCounter] * tank[ShimmerReverb.wrap(writePos - offsetA, _bufLen)] + hann[phaseB] * tank[ShimmerReverb.wrap(writePos - offsetB, _bufLen)] - // Feedback: decayed damped tail + pitched shimmer, plus diffused input. - var writeVal: Double = diffused + decay * damped + shimmer * pitched + // Feedback: decayed damped tail + pitched shimmer, DC-blocked so the + // loop has no DC gain (the octave-up of DC is DC), then add the input. + let feedbackRaw: Double = decay * damped + shimmer * pitched + let feedback: Double = feedbackRaw - dcX1 + dcBlockCoeff * dcY1 + dcX1 = feedbackRaw + dcY1 = feedback + var writeVal: Double = diffused + feedback if writeVal > safetyClamp { writeVal = safetyClamp } else if writeVal < -safetyClamp { writeVal = -safetyClamp } // Flush denormals so they never recirculate in the tank (RT CPU guard). @@ -232,6 +247,7 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { if grainCounter >= _grainFrames { grainCounter = 0 } } if abs(dampState) < denormalFloor { dampState = 0.0 } + if abs(dcY1) < denormalFloor { dcY1 = 0.0 } } } } @@ -243,6 +259,8 @@ public final class ShimmerReverb: GeneratorBase, AudioGenerator { _dampState = dampState _allpassAPos = allpassAPos _allpassBPos = allpassBPos + _dcX1 = dcX1 + _dcY1 = dcY1 return _result } diff --git a/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift b/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift index 0e75e66..1df649a 100644 --- a/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift +++ b/Tests/LyrebirdShoeGazeTests/ShimmerReverbTests.swift @@ -104,6 +104,20 @@ struct ShimmerReverbTests { #expect(samples.allSatisfy { $0.isFinite }, "non-finite controls produced NaN/inf output") } + @Test func highShimmerDoesNotAccumulateDCOffset() throws { + // The octave-up of DC is DC and the damping filter passes DC, so without a feedback + // DC-blocker the loop DC gain (~decay + shimmer) exceeds 1 at high settings and builds an + // offset. Aggressive small-tank settings iterate the loop many times within the read window. + func meanDC(_ shimmer: Double) throws -> Double { + let s: [Float] = try render(oscFreq: 440.0, dc: 0.0, size: 0.005, decay: 0.92, shimmer: shimmer) + return s.reduce(0.0) { $0 + Double($1) } / Double(max(s.count, 1)) + } + let noShimmer: Double = try meanDC(0.0) + let maxShimmer: Double = try meanDC(0.85) + #expect(abs(maxShimmer) < 0.015, "high shimmer accumulated a DC offset (mean = \(maxShimmer))") + #expect(abs(maxShimmer - noShimmer) < 0.012, "shimmer added DC vs the no-shimmer baseline") + } + @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).