227 lines
9.0 KiB
Swift
227 lines
9.0 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.
|
|
@inline(__always)
|
|
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)
|
|
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 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, 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`.
|
|
/// - 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)
|
|
_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 FBSineStepped: GeneratorEnum {
|
|
public typealias AudioType = FBSineStepped.Audio
|
|
public typealias ControlType = FBSineStepped.Control
|
|
}
|