Files
Lyrebird/Sources/Lyrebird/Generators/Chaotic/ChaoticHelpers.swift
T
2026-06-21 11:45:58 -07:00

47 lines
1.6 KiB
Swift

// ChaoticHelpers.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Default iteration frequency (Hz) for the chaotic oscillators — one iteration
/// every other sample at 44.1 kHz, matching the SuperCollider chaotic-UGen
/// defaults. `@usableFromInline` so it can back the public `init` default args.
@usableFromInline
internal let chaoticDefaultFrequency: Double = 22050.0
/// Upper bound on map iterations per output sample. Caps the `advancePhase`
/// `while` loop so a pathological caller-supplied `freq` (e.g. `freq ≫ sampleRate`)
/// cannot do unbounded work on the RT thread.
internal let chaoticMaxStepRate: Double = 64.0
/// Wrap a phase/angle into `[0, 2π)`. Used by the angle-state chaotic maps
/// (`StandardN`, `FBSineN`) to keep their unbounded accumulators bounded.
@inline(__always)
internal func chaoticWrapTwoPi(_ v: Double) -> Double {
v - lyrebirdTwoPi * (v / lyrebirdTwoPi).rounded(.down)
}
// MARK: - Interpolation kernel
/// Bridge between successive chaotic-map iterations for the current segment.
///
/// `t ∈ [0, 1)` is the fraction through the `[h0, h1]` segment; `hM1` / `h2` are
/// the surrounding iterations used only by the cubic kernel, which delegates to
/// the shared `catmullRomCubic` spline.
@inline(__always)
internal func chaoticInterpolate(
_ interpolation: TableInterpolation,
phase t: Double,
hM1: Double, h0: Double, h1: Double, h2: Double
) -> Double {
switch interpolation {
case .none:
return h0
case .linear:
return h0 + t * (h1 - h0)
case .cubic:
return catmullRomCubic(hM1, h0, h1, h2, t: t)
}
}