171 lines
6.4 KiB
Swift
171 lines
6.4 KiB
Swift
// GbmanStepped.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
/// Gingerbreadman-map oscillator (non-interpolated / stepped by default).
|
||
///
|
||
/// Iterates the Gingerbreadman recurrence at `freq` Hz:
|
||
/// ```
|
||
/// x[n+1] = 1 − y[n] + |x[n]|
|
||
/// y[n+1] = x[n]
|
||
/// ```
|
||
/// and outputs `x`. The orbit is chaotic and bounded (roughly `|x| < 8`). It has
|
||
/// no shape parameters — only the initial conditions `xi` / `yi`. Iterations bridge
|
||
/// per `interpolation` (default `.none` — held / stepped, the aliased SC `GbmanN`
|
||
/// character).
|
||
///
|
||
/// `freq` is read once per block (no per-sample audio-rate modulation). See
|
||
/// ``ChaoticBase`` for the iteration / interpolation model.
|
||
public enum GbmanStepped {
|
||
|
||
// MARK: - _Base
|
||
|
||
/// Shared Gingerbreadman state for `Audio` and `Control`.
|
||
public class _Base: ChaoticBase {
|
||
// Map state: x[n] and y[n].
|
||
var x: Double
|
||
var y: Double
|
||
|
||
init(
|
||
freq: any ValidGeneratorInput,
|
||
xi: Double,
|
||
yi: Double,
|
||
interpolation: TableInterpolation,
|
||
mul: any ValidGeneratorInput,
|
||
add: any ValidGeneratorInput
|
||
) {
|
||
self.x = xi
|
||
self.y = yi
|
||
super.init(freq: freq, interpolation: interpolation, mul: mul, add: add)
|
||
}
|
||
|
||
override func iterate() -> Double {
|
||
let xNew: Double = 1.0 - y + abs(x)
|
||
y = x
|
||
x = xNew
|
||
return xNew
|
||
}
|
||
}
|
||
|
||
// MARK: - Audio
|
||
|
||
/// Audio-rate Gingerbreadman oscillator.
|
||
///
|
||
/// - Parameters:
|
||
/// - freq: Iteration frequency in Hz. Read per block. Defaults to `22050`.
|
||
/// - xi: Initial `x`. Defaults to `1.2`.
|
||
/// - yi: Initial `y`. Defaults to `2.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`.
|
||
/// - xi: Initial `x`. Defaults to `1.2`.
|
||
/// - yi: Initial `y`. Defaults to `2.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,
|
||
xi: Double = 1.2,
|
||
yi: Double = 2.1,
|
||
interpolation: TableInterpolation = .none,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(freq: freq, 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 }
|
||
|
||
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 Gingerbreadman 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`.
|
||
/// - xi: Initial `x`. Defaults to `1.2`.
|
||
/// - yi: Initial `y`. Defaults to `2.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`.
|
||
/// - xi: Initial `x`. Defaults to `1.2`.
|
||
/// - yi: Initial `y`. Defaults to `2.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,
|
||
xi: Double = 1.2,
|
||
yi: Double = 2.1,
|
||
interpolation: TableInterpolation = .none,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(freq: freq, 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 }
|
||
|
||
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 GbmanStepped: GeneratorEnum {
|
||
public typealias AudioType = GbmanStepped.Audio
|
||
public typealias ControlType = GbmanStepped.Control
|
||
}
|