Files
Lyrebird/Sources/Lyrebird/Generators/Chaotic/FBSineStepped.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

192 lines
7.4 KiB
Swift

// FBSineStepped.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Feedback-sine-map oscillator (non-interpolated by default).
///
/// Iterates a feedback sine map at `freq` Hz, with the phase accumulator `y`
/// wrapped into `[0, 2π)`:
/// ```
/// x[n+1] = sin(im · y[n] + fb · x[n])
/// y[n+1] = (a · y[n] + c) mod 2π
/// ```
/// and outputs `x` (range `[-1, 1]` — the output is a sine). With the canonical
/// `im = 1`, `fb = 0.1`, `a = 1.1`, `c = 0.5` the orbit is chaotic. Iterations
/// bridge per `interpolation` (default `.none`).
///
/// `freq`, `im`, `fb`, `a`, and `c` are read once per block. See ``ChaoticBase``
/// for the iteration / interpolation model.
public enum FBSineStepped {
// MARK: - _Base
/// Shared feedback-sine state + parameters for `Audio` and `Control`.
public class _Base: ChaoticBase {
let imInput: any ValidGeneratorInput
let fbInput: any ValidGeneratorInput
let aInput: any ValidGeneratorInput
let cInput: any ValidGeneratorInput
// Map state.
var x: Double
var y: Double
// Per-block parameter snapshot read by `iterate()`.
var _im: Double = 0.0
var _fb: Double = 0.0
var _a: Double = 0.0
var _c: Double = 0.0
init(
freq: any ValidGeneratorInput,
im: any ValidGeneratorInput,
fb: any ValidGeneratorInput,
a: any ValidGeneratorInput,
c: any ValidGeneratorInput,
xi: Double,
yi: Double,
interpolation: TableInterpolation,
mul: any ValidGeneratorInput,
add: any ValidGeneratorInput
) {
self.imInput = im._resolvedInput()
self.fbInput = fb._resolvedInput()
self.aInput = a._resolvedInput()
self.cInput = c._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(_im * y + _fb * x)
y = chaoticWrapTwoPi(_a * y + _c)
x = xNew
return xNew
}
/// Snapshot the per-block parameters into the fields `iterate()` reads.
override func readParams(graph: SynthGraph) {
_im = imInput.floatValue(graph: graph)
_fb = fbInput.floatValue(graph: graph)
_a = aInput.floatValue(graph: graph)
_c = cInput.floatValue(graph: graph)
}
}
// MARK: - Audio
/// Audio-rate feedback-sine oscillator.
///
/// - Parameters:
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
/// - im: Phase index multiplier. Defaults to `1`.
/// - fb: Feedback amount. Defaults to `0.1`.
/// - a: Phase-accumulator multiplier. Defaults to `1.1`.
/// - c: Phase-accumulator increment. Defaults to `0.5`.
/// - xi: Initial `x`. Defaults to `0.1`.
/// - yi: Initial `y` (phase). Defaults to `0.1`.
/// - 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`.
/// - im: Phase index multiplier. Defaults to `1`.
/// - fb: Feedback amount. Defaults to `0.1`.
/// - a: Phase-accumulator multiplier. Defaults to `1.1`.
/// - c: Phase-accumulator increment. Defaults to `0.5`.
/// - xi: Initial `x`. Defaults to `0.1`.
/// - yi: Initial `y` (phase). Defaults to `0.1`.
/// - 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,
im: any ValidGeneratorInput = 1.0,
fb: any ValidGeneratorInput = 0.1,
a: any ValidGeneratorInput = 1.1,
c: any ValidGeneratorInput = 0.5,
xi: Double = 0.1,
yi: Double = 0.1,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, im: im, fb: fb, a: a, c: c, xi: xi, yi: yi,
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 feedback-sine 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`.
/// - im: Phase index multiplier. Defaults to `1`.
/// - fb: Feedback amount. Defaults to `0.1`.
/// - a: Phase-accumulator multiplier. Defaults to `1.1`.
/// - c: Phase-accumulator increment. Defaults to `0.5`.
/// - xi: Initial `x`. Defaults to `0.1`.
/// - yi: Initial `y` (phase). Defaults to `0.1`.
/// - 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`.
/// - im: Phase index multiplier. Defaults to `1`.
/// - fb: Feedback amount. Defaults to `0.1`.
/// - a: Phase-accumulator multiplier. Defaults to `1.1`.
/// - c: Phase-accumulator increment. Defaults to `0.5`.
/// - xi: Initial `x`. Defaults to `0.1`.
/// - yi: Initial `y` (phase). Defaults to `0.1`.
/// - 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,
im: any ValidGeneratorInput = 1.0,
fb: any ValidGeneratorInput = 0.1,
a: any ValidGeneratorInput = 1.1,
c: any ValidGeneratorInput = 0.5,
xi: Double = 0.1,
yi: Double = 0.1,
interpolation: TableInterpolation = .none,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, im: im, fb: fb, a: a, c: c, xi: xi, yi: yi,
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 FBSineStepped: GeneratorEnum {
public typealias AudioType = FBSineStepped.Audio
public typealias ControlType = FBSineStepped.Control
}