192 lines
7.5 KiB
Swift
192 lines
7.5 KiB
Swift
// StandardStepped.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Standard-map (Chirikov kicked-rotor) oscillator (non-interpolated by default).
|
|
///
|
|
/// Iterates the standard map at `freq` Hz, both variables wrapped into `[0, 2π)`:
|
|
/// ```
|
|
/// y[n+1] = (y[n] + k · sin(x[n])) mod 2π
|
|
/// x[n+1] = (x[n] + y[n+1]) mod 2π
|
|
/// ```
|
|
/// and outputs `x` (range `[0, 2π)` — scale with `mul`, e.g. `mul: 1/π, add: -1`
|
|
/// for `[-1, 1)`). Larger `k` → stronger chaos. Iterations bridge per
|
|
/// `interpolation` (default `.none`).
|
|
///
|
|
/// `freq` and `k` are read once per block. See ``ChaoticBase`` for the iteration /
|
|
/// interpolation model.
|
|
public enum StandardStepped {
|
|
|
|
// MARK: - _Base
|
|
|
|
/// Shared standard-map state + parameter for `Audio` and `Control`.
|
|
public class _Base: ChaoticBase {
|
|
let kInput: any ValidGeneratorInput
|
|
|
|
// Map state (angles in [0, 2π)).
|
|
var x: Double
|
|
var y: Double
|
|
|
|
// Per-block parameter snapshot read by `iterate()`.
|
|
var _k: Double = 0.0
|
|
|
|
init(
|
|
freq: any ValidGeneratorInput,
|
|
k: any ValidGeneratorInput,
|
|
xi: Double,
|
|
yi: Double,
|
|
interpolation: TableInterpolation,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
self.kInput = k._resolvedInput()
|
|
self.x = xi
|
|
self.y = yi
|
|
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
|
|
}
|
|
|
|
override func iterate() -> Double {
|
|
y = chaoticWrapTwoPi(y + _k * sin(x))
|
|
x = chaoticWrapTwoPi(x + y)
|
|
return x
|
|
}
|
|
|
|
/// Snapshot the per-block parameter into the field `iterate()` reads.
|
|
@inline(__always)
|
|
func readParams(graph: SynthGraph) {
|
|
_k = kInput.floatValue(graph: graph)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Audio-rate standard-map oscillator.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
|
|
/// - k: Kick strength. Defaults to `1`. Larger → stronger chaos. No clamping.
|
|
/// - xi: Initial `x` (angle). Defaults to `0.5`.
|
|
/// - yi: Initial `y` (momentum). 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`.
|
|
/// - k: Kick strength. Defaults to `1`. Larger → stronger chaos. No clamping.
|
|
/// - xi: Initial `x` (angle). Defaults to `0.5`.
|
|
/// - yi: Initial `y` (momentum). 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,
|
|
k: any ValidGeneratorInput = 1.0,
|
|
xi: Double = 0.5,
|
|
yi: Double = 0.0,
|
|
interpolation: TableInterpolation = .none,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, k: k, 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 standard-map 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`.
|
|
/// - k: Kick strength. Defaults to `1`. Larger → stronger chaos. No clamping.
|
|
/// - xi: Initial `x` (angle). Defaults to `0.5`.
|
|
/// - yi: Initial `y` (momentum). 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`.
|
|
/// - k: Kick strength. Defaults to `1`. Larger → stronger chaos. No clamping.
|
|
/// - xi: Initial `x` (angle). Defaults to `0.5`.
|
|
/// - yi: Initial `y` (momentum). 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,
|
|
k: any ValidGeneratorInput = 1.0,
|
|
xi: Double = 0.5,
|
|
yi: Double = 0.0,
|
|
interpolation: TableInterpolation = .none,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, k: k, 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 StandardStepped: GeneratorEnum {
|
|
public typealias AudioType = StandardStepped.Audio
|
|
public typealias ControlType = StandardStepped.Control
|
|
}
|