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

201 lines
8.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// HenonStepped.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Hénon-map oscillator (non-interpolated / stepped by default).
///
/// Iterates the Hénon recurrence at `freq` Hz:
/// ```
/// x[n+1] = 1 − a · x[n]² + b · x[n−1]
/// ```
/// and outputs `x`. With the canonical `a = 1.4`, `b = 0.3` the orbit is chaotic
/// and bounded to roughly `[-1.5, 1.5]`. Iterations bridge per `interpolation`
/// (default `.none` — held / stepped, the aliased SC `HenonN` character).
///
/// `freq`, `a`, and `b` are read once per block (no per-sample audio-rate
/// modulation). See ``ChaoticBase`` for the iteration / interpolation model.
public enum HenonStepped {
// MARK: - _Base
/// Shared Hénon state + parameters for `Audio` and `Control`.
public class _Base: ChaoticBase {
let aInput: any ValidGeneratorInput
let bInput: any ValidGeneratorInput
// Map state: x[n] and x[n-1].
var xn: Double
var xnm1: Double
// Per-block parameter snapshot read by `iterate()`.
var _a: Double = 0.0
var _b: Double = 0.0
init(
freq: any ValidGeneratorInput,
a: any ValidGeneratorInput,
b: any ValidGeneratorInput,
x0: Double,
x1: Double,
interpolation: TableInterpolation,
mul: any ValidGeneratorInput,
add: any ValidGeneratorInput
) {
self.aInput = a._resolvedInput()
self.bInput = b._resolvedInput()
self.xnm1 = x0
self.xn = x1
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
}
override func iterate() -> Double {
let xNew: Double = 1.0 - _a * xn * xn + _b * xnm1
xnm1 = xn
xn = xNew
return xNew
}
/// Snapshot the per-block parameters into the fields `iterate()` reads.
@inline(__always)
func readParams(graph: SynthGraph) {
_a = aInput.floatValue(graph: graph)
_b = bInput.floatValue(graph: graph)
}
}
// MARK: - Audio
/// Audio-rate Hénon oscillator.
///
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Quadratic coefficient. Defaults to `1.4` (canonical chaotic value). No clamping; the orbit diverges for `a ≳ 1.42`.
/// - b: Linear feedback coefficient. Defaults to `0.3`. No clamping; bounded near `|b| < 1`, classic value `0.3`.
/// - x0: Initial `x[n−1]`. Defaults to `0`.
/// - x1: Initial `x[n]`. 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: Quadratic coefficient. Defaults to `1.4`. No clamping; the orbit diverges for `a ≳ 1.42`.
/// - b: Linear feedback coefficient. Defaults to `0.3`. No clamping; bounded near `|b| < 1`, classic value `0.3`.
/// - x0: Initial `x[n−1]`. Defaults to `0`.
/// - x1: Initial `x[n]`. 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.4,
b: any ValidGeneratorInput = 0.3,
x0: Double = 0.0,
x1: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, b: b, x0: x0, x1: x1,
interpolation: interpolation, mul: mul, add: add)
}
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count)
guard count > 0 else { return _result }
readParams(graph: graph)
primeIfNeeded()
let dphase: Double = stepRate(freq: _freqInput.floatValue(graph: graph), sampleRate: graph.engine.sampleRate)
for i: Int in 0..<count {
_result[0][i] = chaoticInterpolate(_interpolation, phase: _phase,
hM1: _hM1, h0: _h0, h1: _h1, h2: _h2)
advancePhase(by: dphase)
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
// MARK: - Control
/// Control-rate Hénon 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: Quadratic coefficient. Defaults to `1.4`. No clamping; the orbit diverges for `a ≳ 1.42`.
/// - b: Linear feedback coefficient. Defaults to `0.3`. No clamping; bounded near `|b| < 1`, classic value `0.3`.
/// - x0: Initial `x[n−1]`. Defaults to `0`.
/// - x1: Initial `x[n]`. 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, ControlGenerator {
public var previousOutputValue: Double = 0.0
private var _rampTarget: Double = 0.0
public func floatValue(graph: SynthGraph) -> Double { previousOutputValue }
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Quadratic coefficient. Defaults to `1.4`. No clamping; the orbit diverges for `a ≳ 1.42`.
/// - b: Linear feedback coefficient. Defaults to `0.3`. No clamping; bounded near `|b| < 1`, classic value `0.3`.
/// - x0: Initial `x[n−1]`. Defaults to `0`.
/// - x1: Initial `x[n]`. 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.4,
b: any ValidGeneratorInput = 0.3,
x0: Double = 0.0,
x1: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, b: b, x0: x0, x1: x1,
interpolation: interpolation, mul: mul, add: add)
_setupSubBlockBuffer()
}
// Bypass sub-block loop: the map advances exactly one audio block per call.
public func calculatedSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_calculatedSamplesBlockRate(graph: graph, count: count)
}
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count)
guard count > 0 else { return _result }
readParams(graph: graph)
primeIfNeeded()
let dphase: Double = stepRate(freq: _freqInput.floatValue(graph: graph), sampleRate: graph.engine.sampleRate)
advancePhase(by: dphase * Double(count))
let target: Double = chaoticInterpolate(_interpolation, phase: _phase,
hM1: _hM1, h0: _h0, h1: _h1, h2: _h2)
let rampStart: Double = _rampTarget
_rampTarget = target
previousOutputValue = fillRampedResult(
into: &_result, from: rampStart, to: target, mulAdd: _mulAdd, graph: graph, count: count)
return _result
}
}
}
// MARK: - GeneratorEnum
extension HenonStepped: GeneratorEnum {
public typealias AudioType = HenonStepped.Audio
public typealias ControlType = HenonStepped.Control
}