106 lines
4.0 KiB
Swift
106 lines
4.0 KiB
Swift
// ChaoticBase.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Shared base for strange-attractor / chaotic-map oscillators.
|
|
///
|
|
/// A chaotic oscillator iterates a discrete map (Lorenz, Hénon, …) at a chosen
|
|
/// `freq` (iterations per second), **not** once per audio sample. Between
|
|
/// successive iterations the audio output bridges the two map values according to
|
|
/// `interpolation`:
|
|
///
|
|
/// - `.none` — hold the current iteration value until the next (hard, aliased).
|
|
/// - `.linear` — straight-line ramp between successive iterations.
|
|
/// - `.cubic` — 4-point cubic across the surrounding iterations (smoothest).
|
|
///
|
|
/// Same shape as `NoiseBase`: the base owns the cross-rate shared state
|
|
/// (`_freqInput`, `_mulAdd`, `_interpolation`, the 4-value history ring, and the
|
|
/// sub-iteration phase accumulator). Concrete per-map `_Base` subclasses add the
|
|
/// map's own variables and the `iterate()` recurrence; `Audio` / `Control`
|
|
/// variants inherit that subclass.
|
|
///
|
|
/// `freq` and the map parameters are read once per block (`floatValue`) — chaotic
|
|
/// oscillators do not support per-sample audio-rate parameter modulation.
|
|
public class ChaoticBase: GeneratorBase {
|
|
let _freqInput: any ValidGeneratorInput
|
|
var _mulAdd: MulAddSpec
|
|
let _interpolation: TableInterpolation
|
|
|
|
/// History ring around the current `[_h0, _h1]` segment:
|
|
/// `_hM1` = one iteration before the segment start, `_h2` = one after the end.
|
|
/// `.none` reads `_h0`, `.linear` lerps `_h0…_h1`, `.cubic` uses all four.
|
|
var _hM1: Double = 0.0
|
|
var _h0: Double = 0.0
|
|
var _h1: Double = 0.0
|
|
var _h2: Double = 0.0
|
|
|
|
/// Fraction `[0, 1)` through the current iteration segment.
|
|
var _phase: Double = 0.0
|
|
|
|
/// Lazy one-shot history priming guard (the concrete map seeds its variables
|
|
/// at `init`, then the first `_computeSamples` fills the ring via `iterate()`).
|
|
var _seeded: Bool = false
|
|
|
|
init(
|
|
freq: any ValidGeneratorInput,
|
|
interpolation: TableInterpolation,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
let resolvedFreq: any ValidGeneratorInput = freq._resolvedInput()
|
|
self._freqInput = resolvedFreq
|
|
self._interpolation = interpolation
|
|
self._mulAdd = MulAddSpec(mul: mul, add: add)
|
|
super.init()
|
|
}
|
|
|
|
/// Advance the map one iteration and return its new output value.
|
|
///
|
|
/// Overridden by every concrete map `_Base`. The base implementation returns
|
|
/// `0` — a missing override would silence the oscillator rather than trap on
|
|
/// the RT thread.
|
|
func iterate() -> Double { 0.0 }
|
|
|
|
/// Fill the history ring on first use: four warm-up iterations seed
|
|
/// `_hM1, _h0, _h1, _h2` so the cubic kernel has real neighbours and the
|
|
/// transient settles onto the attractor before audio is taken.
|
|
@inline(__always)
|
|
func primeIfNeeded() {
|
|
if _seeded { return }
|
|
_hM1 = iterate()
|
|
_h0 = iterate()
|
|
_h1 = iterate()
|
|
_h2 = iterate()
|
|
_phase = 0.0
|
|
_seeded = true
|
|
}
|
|
|
|
/// Per-sample phase increment for `freq`, clamped to `[0, chaoticMaxStepRate]`.
|
|
///
|
|
/// Negative `freq` holds (returns `0`) rather than driving `_phase` negative —
|
|
/// the map has no backward iteration. The upper clamp bounds the `advancePhase`
|
|
/// loop so an extreme `freq` cannot stall the RT thread.
|
|
@inline(__always)
|
|
func stepRate(freq: Double, sampleRate: Double) -> Double {
|
|
let raw: Double = freq / sampleRate
|
|
if raw <= 0 { return 0.0 }
|
|
return min(raw, chaoticMaxStepRate)
|
|
}
|
|
|
|
/// Advance the sub-iteration phase by `dphase`, performing as many `iterate()`
|
|
/// steps as the phase crosses (shifting the history ring forward each time).
|
|
@inline(__always)
|
|
func advancePhase(by dphase: Double) {
|
|
_phase += dphase
|
|
while _phase >= 1.0 {
|
|
_phase -= 1.0
|
|
_hM1 = _h0
|
|
_h0 = _h1
|
|
_h1 = _h2
|
|
_h2 = iterate()
|
|
}
|
|
}
|
|
}
|