178 lines
6.3 KiB
Swift
178 lines
6.3 KiB
Swift
// OscSawtooth.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// MARK: - OscSawtooth
|
|
|
|
/// Band-limited sawtooth wave oscillator using polyBLEP discontinuity correction.
|
|
///
|
|
/// Produces a sawtooth wave in the range `[-1, 1]` with significantly reduced aliasing
|
|
/// compared to a naive sawtooth. The polyBLEP correction is applied at each phase reset.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Frequency in Hz. Positive values advance the oscillator forward. No internal
|
|
/// clamping; aliasing increases toward Nyquist.
|
|
/// - phase: Initial phase offset in cycles `[0, 1)`. Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public enum OscSawtooth {
|
|
|
|
// MARK: - Shared base
|
|
|
|
/// Shared state for audio-rate and control-rate sawtooth oscillator variants.
|
|
public class _Base: OscillatorBase {}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Audio-rate band-limited sawtooth oscillator.
|
|
///
|
|
/// Evaluates frequency per sample; supports audio-rate frequency modulation.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Frequency in Hz. No internal clamping; aliasing increases toward Nyquist.
|
|
/// - phase: Initial phase offset in 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 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
|
|
let rawSaw: Double = 2.0 * _phase - 1.0
|
|
|
|
// PolyBLEP correction at the wrap discontinuity
|
|
let blep: Double = polyBLEP(_phase, dt)
|
|
|
|
_result[0][i] = rawSaw - blep
|
|
|
|
_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 band-limited sawtooth oscillator. Advances phase once per block.
|
|
///
|
|
/// Override `calculatedSamples` bypasses 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 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 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 OscSawtooth: GeneratorEnum {
|
|
public typealias AudioType = OscSawtooth.Audio
|
|
public typealias ControlType = OscSawtooth.Control
|
|
}
|