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