Files
Lyrebird/Sources/Lyrebird/Generators/Chaotic/CuspStepped.swift
T
josh f8878324fd v2.1.1 release: delay-click fix + internal consolidation
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>
2026-10-07 21:20:49 -07:00

158 lines
5.8 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.
// CuspStepped.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Cusp-map oscillator (non-interpolated by default).
///
/// Iterates the cusp map at `freq` Hz:
/// ```
/// x[n+1] = a − b · √|x[n]|
/// ```
/// and outputs `x`. With the canonical `a = 1`, `b = 1.9` the orbit is chaotic and
/// bounded to roughly `[-0.9, 1.0]`. Iterations bridge per `interpolation`
/// (default `.none`).
///
/// `freq`, `a`, and `b` are read once per block. See ``ChaoticBase`` for the
/// iteration / interpolation model.
public enum CuspStepped {
// MARK: - _Base
/// Shared cusp-map state + parameters for `Audio` and `Control`.
public class _Base: ChaoticBase {
let aInput: any ValidGeneratorInput
let bInput: any ValidGeneratorInput
// Map state.
var x: 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,
xi: Double,
interpolation: TableInterpolation,
mul: any ValidGeneratorInput,
add: any ValidGeneratorInput
) {
self.aInput = a._resolvedInput()
self.bInput = b._resolvedInput()
self.x = xi
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
}
override func iterate() -> Double {
let xNew: Double = _a - _b * (abs(x)).squareRoot()
x = xNew
return xNew
}
/// Snapshot the per-block parameters into the fields `iterate()` reads.
override func readParams(graph: SynthGraph) {
_a = aInput.floatValue(graph: graph)
_b = bInput.floatValue(graph: graph)
}
}
// MARK: - Audio
/// Audio-rate cusp-map oscillator.
///
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Offset term. Defaults to `1`. No clamping; some `(a,b)` pairs diverge.
/// - b: Square-root coefficient. Defaults to `1.9`.
/// - xi: Initial `x`. 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: Offset term. Defaults to `1`. No clamping; some `(a,b)` pairs diverge.
/// - b: Square-root coefficient. Defaults to `1.9`.
/// - xi: Initial `x`. 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.0,
b: any ValidGeneratorInput = 1.9,
xi: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, b: b, xi: xi,
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 cusp-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`.
/// - a: Offset term. Defaults to `1`. No clamping; some `(a,b)` pairs diverge.
/// - b: Square-root coefficient. Defaults to `1.9`.
/// - xi: Initial `x`. 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, BlockRateControlGenerator {
public func floatValue(graph: SynthGraph) -> Double { previousOutputValue }
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - a: Offset term. Defaults to `1`. No clamping; some `(a,b)` pairs diverge.
/// - b: Square-root coefficient. Defaults to `1.9`.
/// - xi: Initial `x`. 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.0,
b: any ValidGeneratorInput = 1.9,
xi: Double = 0.0,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, a: a, b: b, xi: xi,
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 CuspStepped: GeneratorEnum {
public typealias AudioType = CuspStepped.Audio
public typealias ControlType = CuspStepped.Control
}