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>
204 lines
8.7 KiB
Swift
204 lines
8.7 KiB
Swift
// LorenzLinear.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
/// Lorenz strange-attractor oscillator (linear-interpolated by default).
|
||
///
|
||
/// Integrates the Lorenz system by forward Euler at each iteration:
|
||
/// ```
|
||
/// x' = x + h · s · (y − x)
|
||
/// y' = y + h · (x · (r − z) − y)
|
||
/// z' = z + h · (x · y − b · z)
|
||
/// ```
|
||
/// and outputs the `x` coordinate. Iterations advance at `freq` Hz; the audio
|
||
/// signal bridges successive `x` values per `interpolation` (default `.linear`).
|
||
///
|
||
/// Output is the **raw** `x` coordinate — on the canonical attractor `|x|` runs to
|
||
/// roughly `20`. Scale with `mul` (e.g. `mul: 0.05`) for a `[-1, 1]`-ish signal.
|
||
///
|
||
/// `freq`, `s`, `r`, `b`, and `h` are read once per block (no per-sample audio-rate
|
||
/// modulation). See ``ChaoticBase`` for the iteration / interpolation model.
|
||
public enum LorenzLinear {
|
||
|
||
// MARK: - _Base
|
||
|
||
/// Shared Lorenz state + parameters for `Audio` and `Control`.
|
||
public class _Base: ChaoticBase {
|
||
let sInput: any ValidGeneratorInput
|
||
let rInput: any ValidGeneratorInput
|
||
let bInput: any ValidGeneratorInput
|
||
let hInput: any ValidGeneratorInput
|
||
|
||
// Map state.
|
||
var x: Double
|
||
var y: Double
|
||
var z: Double
|
||
|
||
// Per-block parameter snapshot read by `iterate()`.
|
||
var _s: Double = 0.0
|
||
var _r: Double = 0.0
|
||
var _b: Double = 0.0
|
||
var _h: Double = 0.0
|
||
|
||
init(
|
||
freq: any ValidGeneratorInput,
|
||
s: any ValidGeneratorInput,
|
||
r: any ValidGeneratorInput,
|
||
b: any ValidGeneratorInput,
|
||
h: any ValidGeneratorInput,
|
||
xi: Double,
|
||
yi: Double,
|
||
zi: Double,
|
||
interpolation: TableInterpolation,
|
||
mul: any ValidGeneratorInput,
|
||
add: any ValidGeneratorInput
|
||
) {
|
||
self.sInput = s._resolvedInput()
|
||
self.rInput = r._resolvedInput()
|
||
self.bInput = b._resolvedInput()
|
||
self.hInput = h._resolvedInput()
|
||
self.x = xi
|
||
self.y = yi
|
||
self.z = zi
|
||
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
|
||
}
|
||
|
||
override func iterate() -> Double {
|
||
let x1: Double = x + _h * _s * (y - x)
|
||
let y1: Double = y + _h * (x * (_r - z) - y)
|
||
let z1: Double = z + _h * (x * y - _b * z)
|
||
x = x1
|
||
y = y1
|
||
z = z1
|
||
return x
|
||
}
|
||
|
||
/// Snapshot the per-block parameters into the fields `iterate()` reads.
|
||
override func readParams(graph: SynthGraph) {
|
||
_s = sInput.floatValue(graph: graph)
|
||
_r = rInput.floatValue(graph: graph)
|
||
_b = bInput.floatValue(graph: graph)
|
||
_h = hInput.floatValue(graph: graph)
|
||
}
|
||
}
|
||
|
||
// MARK: - Audio
|
||
|
||
/// Audio-rate Lorenz oscillator.
|
||
///
|
||
/// - Parameters:
|
||
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
|
||
/// - s: Prandtl number (σ). Defaults to `10`. No clamping; the canonical chaotic regime is `s ≈ 10`.
|
||
/// - r: Rayleigh number (ρ). Defaults to `28`. No clamping; chaos onsets near `r ≈ 24.74` — small/large values plus a big `h` can diverge.
|
||
/// - b: Geometric factor (β). Defaults to `2.667`.
|
||
/// - h: Euler integration step. Defaults to `0.02`. Above ~0.03 the forward-Euler orbit diverges (SC's nominal `0.05` is unstable here), so keep it small.
|
||
/// - xi: Initial `x`. Defaults to `0.1`.
|
||
/// - yi: Initial `y`. Defaults to `0`.
|
||
/// - zi: Initial `z`. Defaults to `0`.
|
||
/// - interpolation: Bridging between iterations. Defaults to `.linear`.
|
||
/// - 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`.
|
||
/// - s: Prandtl number (σ). Defaults to `10`. No clamping; the canonical chaotic regime is `s ≈ 10`.
|
||
/// - r: Rayleigh number (ρ). Defaults to `28`. No clamping; chaos onsets near `r ≈ 24.74` — small/large values plus a big `h` can diverge.
|
||
/// - b: Geometric factor (β). Defaults to `2.667`.
|
||
/// - h: Euler integration step. Defaults to `0.02`. Keep below ~0.03 (forward Euler diverges above).
|
||
/// - xi: Initial `x`. Defaults to `0.1`.
|
||
/// - yi: Initial `y`. Defaults to `0`.
|
||
/// - zi: Initial `z`. Defaults to `0`.
|
||
/// - interpolation: Bridging between iterations. Defaults to `.linear`.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public override init(
|
||
freq: any ValidGeneratorInput = chaoticDefaultFrequency,
|
||
s: any ValidGeneratorInput = 10.0,
|
||
r: any ValidGeneratorInput = 28.0,
|
||
b: any ValidGeneratorInput = 2.667,
|
||
h: any ValidGeneratorInput = 0.02,
|
||
xi: Double = 0.1,
|
||
yi: Double = 0.0,
|
||
zi: Double = 0.0,
|
||
interpolation: TableInterpolation = .linear,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(freq: freq, s: s, r: r, b: b, h: h, xi: xi, yi: yi, zi: zi,
|
||
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 Lorenz 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`.
|
||
/// - s: Prandtl number (σ). Defaults to `10`. No clamping; the canonical chaotic regime is `s ≈ 10`.
|
||
/// - r: Rayleigh number (ρ). Defaults to `28`. No clamping; chaos onsets near `r ≈ 24.74` — small/large values plus a big `h` can diverge.
|
||
/// - b: Geometric factor (β). Defaults to `2.667`.
|
||
/// - h: Euler integration step. Defaults to `0.02`. Keep below ~0.03 (forward Euler diverges above).
|
||
/// - xi: Initial `x`. Defaults to `0.1`.
|
||
/// - yi: Initial `y`. Defaults to `0`.
|
||
/// - zi: Initial `z`. Defaults to `0`.
|
||
/// - interpolation: Bridging between iterations. Defaults to `.linear`.
|
||
/// - 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`.
|
||
/// - s: Prandtl number (σ). Defaults to `10`. No clamping; the canonical chaotic regime is `s ≈ 10`.
|
||
/// - r: Rayleigh number (ρ). Defaults to `28`. No clamping; chaos onsets near `r ≈ 24.74` — small/large values plus a big `h` can diverge.
|
||
/// - b: Geometric factor (β). Defaults to `2.667`.
|
||
/// - h: Euler integration step. Defaults to `0.02`. Keep below ~0.03 (forward Euler diverges above).
|
||
/// - xi: Initial `x`. Defaults to `0.1`.
|
||
/// - yi: Initial `y`. Defaults to `0`.
|
||
/// - zi: Initial `z`. Defaults to `0`.
|
||
/// - interpolation: Bridging between iterations. Defaults to `.linear`.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public override init(
|
||
freq: any ValidGeneratorInput = chaoticDefaultFrequency,
|
||
s: any ValidGeneratorInput = 10.0,
|
||
r: any ValidGeneratorInput = 28.0,
|
||
b: any ValidGeneratorInput = 2.667,
|
||
h: any ValidGeneratorInput = 0.02,
|
||
xi: Double = 0.1,
|
||
yi: Double = 0.0,
|
||
zi: Double = 0.0,
|
||
interpolation: TableInterpolation = .linear,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(freq: freq, s: s, r: r, b: b, h: h, xi: xi, yi: yi, zi: zi,
|
||
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 LorenzLinear: GeneratorEnum {
|
||
public typealias AudioType = LorenzLinear.Audio
|
||
public typealias ControlType = LorenzLinear.Control
|
||
}
|