Files
Lyrebird/Sources/Lyrebird/Generators/Oscillators/LFTriangle.swift
T

183 lines
6.7 KiB
Swift

// LFTriangle.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// MARK: - LFTriangle
/// Non-band-limited triangle wave LFO. Range `[-1, 1]`.
///
/// Linear ascending ramp in the first half-cycle (`phase < 0.5`), descending in the second.
/// Formula: `phase < 0.5 ? 4*phase - 1 : 3 - 4*phase`. No anti-aliasing.
///
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping.
/// - phase: Initial phase offset in normalized cycles `[0, 1)`. Defaults to `0.0`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public enum LFTriangle {
// MARK: - Shared base
/// Shared state for audio-rate and control-rate LFTriangle variants.
public class _Base: OscillatorBase {}
// MARK: - Audio
/// Audio-rate non-band-limited triangle wave LFO. Evaluates frequency per sample.
///
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping.
/// - phase: Initial phase offset in normalized cycles `[0, 1)`. Defaults to `0.0`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Audio: _Base, AudioGenerator {
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping.
/// - phase: Initial phase offset in normalized cycles `[0, 1)`. Defaults to `0.0`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
freq: any ValidGeneratorInput,
phase: Double = 0.0,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, phase: phase, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let sampleRate: Double = Double(graph.engine.sampleRate)
// Descending intercept: when phase==0.5, ascending formula gives 4*0.5-1=1.
// Descending: 3 - 4*phase; at phase=0.5 gives 3-2=1, at phase=1 gives -1.
let triangleDescendingIntercept: Double = lfTriangleSlope - 1.0
let freqSamples: [Double]?
let scalarDt: Double
if _freqIsGenerator {
freqSamples = _freqInput.calculatedSamples(graph: graph, count: count).first
scalarDt = 0
} else {
freqSamples = nil
scalarDt = _freqInput.floatValue(graph: graph) / sampleRate
}
for i: Int in 0..<count {
let dt: Double
if let fs: [Double] = freqSamples {
dt = (i < fs.count ? fs[i] : 0) / sampleRate
} else {
dt = scalarDt
}
// Triangle: ascending in [0, 0.5), descending in [0.5, 1)
if _phase < lfHalfPhase {
_result[0][i] = lfTriangleSlope * _phase - 1.0
} else {
_result[0][i] = triangleDescendingIntercept - lfTriangleSlope * _phase
}
_phase += dt
if _phase >= 1.0 { _phase -= 1.0 }
else if _phase < 0.0 { _phase += 1.0 }
if _phase >= 1.0 || _phase < 0.0 {
_phase = _phase.truncatingRemainder(dividingBy: 1.0)
if _phase < 0.0 { _phase += 1.0 }
}
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
// MARK: - Control
/// Control-rate non-band-limited triangle wave LFO. Advances phase once per audio block.
///
/// Override `calculatedSamples` bypasses the sub-block loop — phase must advance exactly
/// once per audio block regardless of `controlBlockSize`.
///
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping.
/// - phase: Initial phase offset in normalized cycles `[0, 1)`. Defaults to `0.0`.
/// - 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
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping.
/// - phase: Initial phase offset in normalized cycles `[0, 1)`. Defaults to `0.0`.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
freq: any ValidGeneratorInput,
phase: Double = 0.0,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(freq: freq, phase: phase, mul: mul, add: add)
_setupSubBlockBuffer()
}
// Bypass sub-block loop: phase must advance exactly once per audio block.
public func calculatedSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_calculatedSamplesBlockRate(graph: graph, count: count)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let sampleRate: Double = Double(graph.engine.sampleRate)
let countFloat: Double = Double(count)
let freq: Double
if _freqIsGenerator {
freq = _freqInput.calculatedSamples(graph: graph, count: count).first?.first ?? 0
} else {
freq = _freqInput.floatValue(graph: graph)
}
let dt: Double = freq / sampleRate
_phase += dt * countFloat
normalizeOscPhase(&_phase)
let triangleDescendingIntercept: Double = lfTriangleSlope - 1.0
let target: Double
if _phase < lfHalfPhase {
target = lfTriangleSlope * _phase - 1.0
} else {
target = triangleDescendingIntercept - lfTriangleSlope * _phase
}
let rampStart: Double = _rampTarget
_rampTarget = target
guard count > 0 else { return _result }
previousOutputValue = fillRampedResult(
into: &_result, from: rampStart, to: target, mulAdd: _mulAdd, graph: graph, count: count)
return _result
}
}
}
extension LFTriangle: GeneratorEnum {
public typealias AudioType = LFTriangle.Audio
public typealias ControlType = LFTriangle.Control
}