f8878324fd
Mirror of Lyrebird dev HEAD (9f791c7c) minus maintainer-only paths. Patch release: fixes the allpass fractional-delay click (DelayLine/CombFilter/AllpassFilter/ DelayLineFeedback under a modulated delay) and lands the internal code-quality consolidation pass (~160 deduped sites + shared base classes/helpers, bit-identical output). No public API removed or changed. See RELEASES.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
140 lines
5.0 KiB
Swift
140 lines
5.0 KiB
Swift
// ClipNoise.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
// MARK: - ClipNoise
|
|
|
|
/// Clip noise (also called binary noise) — the sign of a white noise source.
|
|
/// Every output sample is exactly +1.0 or -1.0, with equal probability.
|
|
/// This is equivalent to `WhiteNoise.sign` but implemented directly without
|
|
/// the float normalisation step.
|
|
///
|
|
/// `ClipNoise.Audio` evaluates per sample at audio rate.
|
|
/// `ClipNoise.Control` draws one new value per audio block and delivers an interpolated
|
|
/// ramp to downstream audio-rate consumers.
|
|
///
|
|
/// Each instance is independently seeded so concurrent instances produce uncorrelated output.
|
|
public enum ClipNoise {
|
|
|
|
// MARK: - Shared base
|
|
|
|
/// Shared PRNG state and output buffer for both rate variants.
|
|
public class _Base: NoiseBase {
|
|
|
|
override init(
|
|
initialRngState: UInt64,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(initialRngState: initialRngState, mul: mul, add: add)
|
|
}
|
|
|
|
/// Draw one bipolar xorshift64 sample and return its sign (-1.0 or +1.0).
|
|
func nextClipSample() -> Double {
|
|
return xorshift64Next(&rngState) < 0 ? -1.0 : 1.0
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Audio-rate clip noise — produces +1.0 or -1.0 per output sample.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// let noise = ClipNoise.Audio()
|
|
/// let noise = ClipNoise.Audio(seed: 42) // deterministic for testing
|
|
/// ```
|
|
/// - Parameters:
|
|
/// - seed: PRNG seed. Defaults to a time-derived value for unique instances.
|
|
/// Pass an explicit seed for reproducible test output.
|
|
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
|
|
/// - add: Offset added after multiplier. Defaults to `0.0`.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
|
|
/// - Parameters:
|
|
/// - seed: PRNG seed. Defaults to a time-derived value for unique instances.
|
|
/// Pass an explicit seed for reproducible test output.
|
|
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
|
|
/// - add: Offset added after multiplier. Defaults to `0.0`.
|
|
public init(
|
|
seed: UInt64? = nil,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
let audioMixConstant: UInt64 = 0xA110_0004_5A5A_3C3C
|
|
let initialRngState: UInt64 = lyrebirdSeedXorshift64(from: seed, mixConstant: audioMixConstant)
|
|
super.init(initialRngState: initialRngState, mul: mul, add: add)
|
|
}
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
for i: Int in 0..<count {
|
|
_result[0][i] = nextClipSample()
|
|
}
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
|
|
// MARK: - Control
|
|
|
|
/// Control-rate clip noise — draws one new sign value per audio block and delivers an
|
|
/// interpolated ramp (from -1 to +1 or vice versa) to downstream audio-rate consumers.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// let noise = ClipNoise.Control()
|
|
/// ```
|
|
/// - Parameters:
|
|
/// - seed: PRNG seed. Defaults to a time-derived value.
|
|
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
|
|
/// - add: Offset added after multiplier. Defaults to `0.0`.
|
|
public final class Control: _Base, BlockRateControlGenerator {
|
|
|
|
public var previousOutputValue: Double = 0.0
|
|
private var _rampTarget: Double = 0.0
|
|
|
|
/// - Parameters:
|
|
/// - seed: PRNG seed. Defaults to a time-derived value.
|
|
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
|
|
/// - add: Offset added after multiplier. Defaults to `0.0`.
|
|
public init(
|
|
seed: UInt64? = nil,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
let controlMixConstant: UInt64 = 0xD2D2_D2D2_2D2D_2D2D
|
|
let initialRngState: UInt64 = lyrebirdSeedXorshift64(from: seed, mixConstant: controlMixConstant)
|
|
super.init(initialRngState: initialRngState, mul: mul, add: add)
|
|
}
|
|
|
|
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let target: Double = nextClipSample()
|
|
let rampStart: Double = _rampTarget
|
|
_rampTarget = target
|
|
guard count > 0 else { return _result }
|
|
previousOutputValue = fillRampedResult(
|
|
into: &_result, from: rampStart, to: target, mulAdd: _mulAdd, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ClipNoise: GeneratorEnum {
|
|
public typealias AudioType = ClipNoise.Audio
|
|
public typealias ControlType = ClipNoise.Control
|
|
}
|