228 lines
9.2 KiB
Swift
228 lines
9.2 KiB
Swift
// LatoocarfianStepped.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Latoocarfian-map (Pickover) oscillator (non-interpolated by default).
|
|
///
|
|
/// Iterates the Latoocarfian map at `freq` Hz:
|
|
/// ```
|
|
/// x[n+1] = sin(b · y[n]) + c · sin(b · x[n])
|
|
/// y[n+1] = sin(a · x[n]) + d · sin(a · y[n])
|
|
/// ```
|
|
/// and outputs `x`. With the canonical `a = 1`, `b = 3`, `c = 0.5`, `d = 0.5` the
|
|
/// orbit is chaotic and bounded to roughly `[-1.5, 1.5]`. Chaos generally requires
|
|
/// `|a|, |b|` in about `[0.5, 3]`. Iterations bridge per `interpolation`
|
|
/// (default `.none`).
|
|
///
|
|
/// `freq`, `a`, `b`, `c`, and `d` are read once per block. See ``ChaoticBase`` for
|
|
/// the iteration / interpolation model.
|
|
public enum LatoocarfianStepped {
|
|
|
|
// MARK: - _Base
|
|
|
|
/// Shared Latoocarfian state + parameters for `Audio` and `Control`.
|
|
public class _Base: ChaoticBase {
|
|
let aInput: any ValidGeneratorInput
|
|
let bInput: any ValidGeneratorInput
|
|
let cInput: any ValidGeneratorInput
|
|
let dInput: any ValidGeneratorInput
|
|
|
|
// Map state.
|
|
var x: Double
|
|
var y: Double
|
|
|
|
// Per-block parameter snapshot read by `iterate()`.
|
|
var _a: Double = 0.0
|
|
var _b: Double = 0.0
|
|
var _c: Double = 0.0
|
|
var _d: Double = 0.0
|
|
|
|
init(
|
|
freq: any ValidGeneratorInput,
|
|
a: any ValidGeneratorInput,
|
|
b: any ValidGeneratorInput,
|
|
c: any ValidGeneratorInput,
|
|
d: any ValidGeneratorInput,
|
|
xi: Double,
|
|
yi: Double,
|
|
interpolation: TableInterpolation,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
self.aInput = a._resolvedInput()
|
|
self.bInput = b._resolvedInput()
|
|
self.cInput = c._resolvedInput()
|
|
self.dInput = d._resolvedInput()
|
|
self.x = xi
|
|
self.y = yi
|
|
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
|
|
}
|
|
|
|
override func iterate() -> Double {
|
|
let xNew: Double = sin(_b * y) + _c * sin(_b * x)
|
|
let yNew: Double = sin(_a * x) + _d * sin(_a * y)
|
|
x = xNew
|
|
y = yNew
|
|
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)
|
|
_c = cInput.floatValue(graph: graph)
|
|
_d = dInput.floatValue(graph: graph)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Audio-rate Latoocarfian oscillator.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
|
|
/// - a: First angular factor. Defaults to `1`. Chaos generally needs `|a| ≤ 3`.
|
|
/// - b: Second angular factor. Defaults to `3`. Chaos generally needs `|b| ≤ 3`.
|
|
/// - c: `x` self-coupling. Defaults to `0.5`.
|
|
/// - d: `y` self-coupling. Defaults to `0.5`.
|
|
/// - xi: Initial `x`. Defaults to `0.5`.
|
|
/// - yi: Initial `y`. Defaults to `0.5`.
|
|
/// - 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: First angular factor. Defaults to `1`. Chaos generally needs `|a| ≤ 3`.
|
|
/// - b: Second angular factor. Defaults to `3`. Chaos generally needs `|b| ≤ 3`.
|
|
/// - c: `x` self-coupling. Defaults to `0.5`.
|
|
/// - d: `y` self-coupling. Defaults to `0.5`.
|
|
/// - xi: Initial `x`. Defaults to `0.5`.
|
|
/// - yi: Initial `y`. Defaults to `0.5`.
|
|
/// - 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.0,
|
|
b: any ValidGeneratorInput = 3.0,
|
|
c: any ValidGeneratorInput = 0.5,
|
|
d: any ValidGeneratorInput = 0.5,
|
|
xi: Double = 0.5,
|
|
yi: Double = 0.5,
|
|
interpolation: TableInterpolation = .none,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, a: a, b: b, c: c, d: d, xi: xi, yi: yi,
|
|
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 Latoocarfian 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: First angular factor. Defaults to `1`. Chaos generally needs `|a| ≤ 3`.
|
|
/// - b: Second angular factor. Defaults to `3`. Chaos generally needs `|b| ≤ 3`.
|
|
/// - c: `x` self-coupling. Defaults to `0.5`.
|
|
/// - d: `y` self-coupling. Defaults to `0.5`.
|
|
/// - xi: Initial `x`. Defaults to `0.5`.
|
|
/// - yi: Initial `y`. Defaults to `0.5`.
|
|
/// - 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: First angular factor. Defaults to `1`. Chaos generally needs `|a| ≤ 3`.
|
|
/// - b: Second angular factor. Defaults to `3`. Chaos generally needs `|b| ≤ 3`.
|
|
/// - c: `x` self-coupling. Defaults to `0.5`.
|
|
/// - d: `y` self-coupling. Defaults to `0.5`.
|
|
/// - xi: Initial `x`. Defaults to `0.5`.
|
|
/// - yi: Initial `y`. Defaults to `0.5`.
|
|
/// - 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.0,
|
|
b: any ValidGeneratorInput = 3.0,
|
|
c: any ValidGeneratorInput = 0.5,
|
|
d: any ValidGeneratorInput = 0.5,
|
|
xi: Double = 0.5,
|
|
yi: Double = 0.5,
|
|
interpolation: TableInterpolation = .none,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, a: a, b: b, c: c, d: d, xi: xi, yi: yi,
|
|
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 LatoocarfianStepped: GeneratorEnum {
|
|
public typealias AudioType = LatoocarfianStepped.Audio
|
|
public typealias ControlType = LatoocarfianStepped.Control
|
|
}
|