From 284093c3021ee4c305c678aaf45b4728ac430a88 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 28 Jun 2026 15:08:09 -0700 Subject: [PATCH] Granulator review fixes: use shared xorshift helper + init param docs - Use Lyrebird's @_spi xorshift64Next(_:) instead of an inlined copy (@_spi(LyrebirdExtensions) import LyrebirdSynth); drop the local nextBipolar. - Add the init-level /// - Parameters: block to StereoGranulator. Suite 271; both apps build. Co-Authored-By: Claude Opus 4.8 --- .../Generators/StereoGranulator.swift | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Generators/StereoGranulator.swift b/Sources/LyrebirdShoeGaze/Generators/StereoGranulator.swift index ee0ed47..b7200a3 100644 --- a/Sources/LyrebirdShoeGaze/Generators/StereoGranulator.swift +++ b/Sources/LyrebirdShoeGaze/Generators/StereoGranulator.swift @@ -5,7 +5,7 @@ // SPDX-License-Identifier: MIT import Foundation // `cos`/`sin` for the init-time Hann table and per-grain pan gains -import LyrebirdSynth +@_spi(LyrebirdExtensions) import LyrebirdSynth /// Shared zero-fill fallback block, allocated once at module load, so the RT path never allocates. private let stereoGranulatorZeroBlock: [Double] = makeSampleBuffer() @@ -81,6 +81,16 @@ public final class StereoGranulator: GeneratorBase, AudioGenerator { // MARK: - Init + /// Creates a stereo granulator. + /// + /// - Parameters: + /// - input: Audio-rate mono signal to granulate. + /// - maxGrainSize: Largest grain length in seconds; sizes the pre-allocated buffer. + /// - grainSize: Grain length in seconds. Control rate; clamped to `[~0.003, maxGrainSize]`. + /// - overlap: Number of overlapping grains (spawn density). Control rate; clamped to `[1, 24]`. + /// - spread: Stereo spread `[0, 1]` — scales each grain's random pan. Control rate. + /// - sampleRate: Sample rate used to size the buffer and convert seconds → frames. + /// - seed: Optional PRNG seed for deterministic grain panning (tests). Defaults to a constant. public init( input: any ValidGeneratorInput, maxGrainSize: Double, @@ -227,20 +237,11 @@ public final class StereoGranulator: GeneratorBase, AudioGenerator { _vLen[voice] = grainLen // Read the grainLen samples just before the write head. _vReadHead[voice] = writeHead - grainLen - let pan: Double = StereoGranulator.nextBipolar(&rng) * spread + let pan: Double = xorshift64Next(&rng) * spread // shared RT-safe PRNG, [-1, 1) × spread let angle: Double = (pan + 1.0) * panAngleScale _vGainL[voice] = cos(angle) _vGainR[voice] = sin(angle) } - - /// xorshift64 step → uniform Double in `[-1, 1)`. RT-safe (no allocation, no locks). - @inline(__always) - private static func nextBipolar(_ state: inout UInt64) -> Double { - state ^= state << 13 - state ^= state >> 7 - state ^= state << 17 - return Double(state) / Double(UInt64.max) * 2.0 - 1.0 - } } // MARK: - Channel accessors