Files
Lyrebird/Sources/Lyrebird/Generators/Noise/ExpRandNoise.swift
T
josh f8878324fd v2.1.1 release: delay-click fix + internal consolidation
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>
2026-10-07 21:20:49 -07:00

127 lines
5.0 KiB
Swift

// ExpRandNoise.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// MARK: - ExpRandNoise
/// Exponential (log-uniform) distribution noise in [lo, hi].
///
/// Both bounds must be positive. Samples are distributed uniformly on a logarithmic scale
/// so low values are no more likely than high values on a log scale.
///
/// `ExpRandNoise.Audio` produces one sample per output sample.
/// `ExpRandNoise.Control` produces one sample per audio block with linear interpolation.
public enum ExpRandNoise {
public class _Base: NoiseBase {
let loInput: any ValidGeneratorInput
let hiInput: any ValidGeneratorInput
init(lo: any ValidGeneratorInput, hi: any ValidGeneratorInput,
initialRngState: UInt64,
mul: any ValidGeneratorInput, add: any ValidGeneratorInput) {
let resolvedLo: any ValidGeneratorInput = lo._resolvedInput()
let resolvedHi: any ValidGeneratorInput = hi._resolvedInput()
self.loInput = resolvedLo
self.hiInput = resolvedHi
super.init(initialRngState: initialRngState, mul: mul, add: add)
}
}
/// - Parameters:
/// - lo: Lower bound (must be > 0). Defaults to `0.01`.
/// - hi: Upper bound (must be > 0). Defaults to `1.0`.
/// - seed: PRNG seed. Defaults to a time-derived value.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Audio: _Base, AudioGenerator {
/// - Parameters:
/// - lo: Lower bound (must be > 0). Defaults to `0.01`.
/// - hi: Upper bound (must be > 0). Defaults to `1.0`.
/// - seed: PRNG seed. Defaults to a time-derived value.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public init(
lo: any ValidGeneratorInput = 0.01,
hi: any ValidGeneratorInput = 1.0,
seed: UInt64? = nil,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
let audioMixConstant: UInt64 = 0x4444_5555_6666_7777
let state: UInt64 = lyrebirdSeedXorshift64(from: seed, mixConstant: audioMixConstant)
super.init(lo: lo, hi: hi, initialRngState: state, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let lo: Double = loInput.floatValue(graph: graph)
let hi: Double = hiInput.floatValue(graph: graph)
for i: Int in 0..<count {
_result[0][i] = _sampleExpRand(lo: lo, hi: hi, rng: &rngState)
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
/// - Parameters:
/// - lo: Lower bound (must be > 0). Defaults to `0.01`.
/// - hi: Upper bound (must be > 0). Defaults to `1.0`.
/// - seed: PRNG seed. Defaults to a time-derived value.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Control: _Base, BlockRateControlGenerator {
public var previousOutputValue: Double = 0.0
private var _rampTarget: Double = 0.0
/// - Parameters:
/// - lo: Lower bound (must be > 0). Defaults to `0.01`.
/// - hi: Upper bound (must be > 0). Defaults to `1.0`.
/// - seed: PRNG seed. Defaults to a time-derived value.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public init(
lo: any ValidGeneratorInput = 0.01,
hi: any ValidGeneratorInput = 1.0,
seed: UInt64? = nil,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
let controlMixConstant: UInt64 = 0x8888_9999_AAAA_BBBB
let state: UInt64 = lyrebirdSeedXorshift64(from: seed, mixConstant: controlMixConstant)
super.init(lo: lo, hi: hi, initialRngState: state, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let lo: Double = loInput.floatValue(graph: graph)
let hi: Double = hiInput.floatValue(graph: graph)
let target: Double = _sampleExpRand(lo: lo, hi: hi, rng: &rngState)
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 ExpRandNoise: GeneratorEnum {
public typealias AudioType = ExpRandNoise.Audio
public typealias ControlType = ExpRandNoise.Control
}