Files
Lyrebird/Sources/Lyrebird/Generators/Chaotic/LinCongStepped.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

170 lines
6.7 KiB
Swift

// LinCongStepped.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Linear-congruential-map oscillator (non-interpolated / stepped by default).
///
/// Iterates the linear-congruential recurrence at `freq` Hz:
/// ```
/// x[n+1] = (a · x[n] + c) mod m
/// ```
/// and outputs `x`, which stays in `[0, m)` by construction. With the canonical
/// `a = 1.1`, `c = 0.13`, `m = 1` the orbit fills `[0, 1)` chaotically. Iterations
/// bridge per `interpolation` (default `.none` — held / stepped, the aliased SC
/// `LinCongN` character).
///
/// `freq`, `a`, `c`, and `m` are read once per block (no per-sample audio-rate
/// modulation). See ``ChaoticBase`` for the iteration / interpolation model.
public enum LinCongStepped {
// MARK: - _Base
/// Shared LCG state + parameters for `Audio` and `Control`.
public class _Base: ChaoticBase {
let aInput: any ValidGeneratorInput
let cInput: any ValidGeneratorInput
let mInput: any ValidGeneratorInput
// Map state.
var x: Double
// Per-block parameter snapshot read by `iterate()`.
var _a: Double = 0.0
var _c: Double = 0.0
var _m: Double = 1.0
init(
freq: any ValidGeneratorInput,
a: any ValidGeneratorInput,
c: any ValidGeneratorInput,
m: any ValidGeneratorInput,
xi: Double,
interpolation: TableInterpolation,
mul: any ValidGeneratorInput,
add: any ValidGeneratorInput
) {
self.aInput = a._resolvedInput()
self.cInput = c._resolvedInput()
self.mInput = m._resolvedInput()
self.x = xi
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
}
override func iterate() -> Double {
let raw: Double = _a * x + _c
// Reduce modulo m into [0, m). Guard m <= 0 to avoid a divide-by-zero.
let xNew: Double = _m > 0 ? raw - _m * (raw / _m).rounded(.down) : raw
x = xNew
return xNew
}
/// Snapshot the per-block parameters into the fields `iterate()` reads.
override func readParams(graph: SynthGraph) {
_a = aInput.floatValue(graph: graph)
_c = cInput.floatValue(graph: graph)
_m = mInput.floatValue(graph: graph)
}
}
// MARK: - Audio
/// Audio-rate linear-congruential oscillator.
///
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Multiplier. Defaults to `1.1`.
/// - c: Increment. Defaults to `0.13`.
/// - m: Modulus; output stays in `[0, m)`. Defaults to `1`. `m <= 0` disables the reduction.
/// - xi: Initial `x`. Defaults to `0`.
/// - interpolation: Bridging between iterations. Defaults to `.none`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Audio: _Base, AudioGenerator {
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Multiplier. Defaults to `1.1`.
/// - c: Increment. Defaults to `0.13`.
/// - m: Modulus; output stays in `[0, m)`. Defaults to `1`. `m <= 0` disables the reduction.
/// - xi: Initial `x`. Defaults to `0`.
/// - interpolation: Bridging between iterations. Defaults to `.none`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
freq: any ValidGeneratorInput = chaoticDefaultFrequency,
a: any ValidGeneratorInput = 1.1,
c: any ValidGeneratorInput = 0.13,
m: any ValidGeneratorInput = 1.0,
xi: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, c: c, m: m, xi: xi,
interpolation: interpolation, mul: mul, add: add)
}
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count)
return renderAudioBlock(graph: graph, count: count)
}
}
// MARK: - Control
/// Control-rate linear-congruential oscillator. Advances the map by one audio
/// block per call and ramps from the previous block value to the new one.
///
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Multiplier. Defaults to `1.1`.
/// - c: Increment. Defaults to `0.13`.
/// - m: Modulus; output stays in `[0, m)`. Defaults to `1`. `m <= 0` disables the reduction.
/// - xi: Initial `x`. Defaults to `0`.
/// - interpolation: Bridging between iterations. Defaults to `.none`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Control: _Base, BlockRateControlGenerator {
public func floatValue(graph: SynthGraph) -> Double { previousOutputValue }
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Multiplier. Defaults to `1.1`.
/// - c: Increment. Defaults to `0.13`.
/// - m: Modulus; output stays in `[0, m)`. Defaults to `1`. `m <= 0` disables the reduction.
/// - xi: Initial `x`. Defaults to `0`.
/// - interpolation: Bridging between iterations. Defaults to `.none`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
freq: any ValidGeneratorInput = chaoticDefaultFrequency,
a: any ValidGeneratorInput = 1.1,
c: any ValidGeneratorInput = 0.13,
m: any ValidGeneratorInput = 1.0,
xi: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, c: c, m: m, xi: xi,
interpolation: interpolation, mul: mul, add: add)
}
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count)
return renderControlBlock(graph: graph, count: count)
}
}
}
// MARK: - GeneratorEnum
extension LinCongStepped: GeneratorEnum {
public typealias AudioType = LinCongStepped.Audio
public typealias ControlType = LinCongStepped.Control
}