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

170 lines
6.1 KiB
Swift

// LFSawtooth.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
// MARK: - LFSawtooth
/// Non-band-limited sawtooth LFO oscillator. Range `[-1, 1]`.
///
/// Produces a linear ascending ramp: `2 * phase - 1`. No anti-aliasing — intended for
/// LFO / control-rate use where aliasing is inaudible or irrelevant.
///
/// - Parameters:
/// - freq: Frequency in Hz. Positive values advance phase forward. 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 LFSawtooth {
// MARK: - Shared base
/// Shared state for audio-rate and control-rate LFSawtooth variants.
public class _Base: OscillatorBase {}
// MARK: - Audio
/// Audio-rate non-band-limited sawtooth LFO. Evaluates frequency per sample.
///
/// - Parameters:
/// - freq: Frequency in Hz. No internal clamping; aliasing increases toward Nyquist.
/// - 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; aliasing increases toward Nyquist.
/// - 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)
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
}
// Raw sawtooth: 2*phase - 1
_result[0][i] = 2.0 * _phase - 1.0
_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 sawtooth 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 target: Double = 2.0 * _phase - 1.0
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 LFSawtooth: GeneratorEnum {
public typealias AudioType = LFSawtooth.Audio
public typealias ControlType = LFSawtooth.Control
}