85 lines
3.0 KiB
Swift
85 lines
3.0 KiB
Swift
// OscBandLimitedHelpers.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
// MARK: - PolyBLEP / PolyBLAMP helpers (file-scope, RT-safe, no allocation)
|
||
|
||
/// Two-point polynomial approximation of the band-limited step correction (BLEP residual).
|
||
///
|
||
/// At a discontinuity where phase resets, call this once *before* the reset and once
|
||
/// *after* using the reflected phase, and add the results to the raw waveform sample.
|
||
///
|
||
/// - Parameters:
|
||
/// - t: Normalised phase within `[0, 1)` at the sample where the discontinuity falls.
|
||
/// - dt: Phase increment per sample (freq / sampleRate).
|
||
/// - Returns: Correction value to add to the raw sawtooth/square/pulse sample.
|
||
@inline(__always)
|
||
func polyBLEP(
|
||
_ t: Double,
|
||
_ dt: Double
|
||
) -> Double {
|
||
guard dt > 0.0 else { return 0.0 }
|
||
if t < dt {
|
||
let t2: Double = t / dt
|
||
return t2 + t2 - t2 * t2 - 1.0
|
||
} else if t > 1.0 - dt {
|
||
let t2: Double = (t - 1.0) / dt
|
||
return t2 * t2 + t2 + t2 + 1.0
|
||
}
|
||
return 0.0
|
||
}
|
||
|
||
/// Two-point polynomial approximation of the band-limited ramp correction (BLAMP residual).
|
||
///
|
||
/// Use at slope discontinuities (corners) in waveforms such as triangle waves. Apply once
|
||
/// *before* each corner and once *after* using the reflected phase.
|
||
///
|
||
/// - Parameters:
|
||
/// - t: Normalised phase within `[0, 1)` at the sample near the corner.
|
||
/// - dt: Phase increment per sample (freq / sampleRate).
|
||
/// - Returns: Correction value to add to the raw triangle/ramp sample.
|
||
@inline(__always)
|
||
func polyBLAMP(
|
||
_ t: Double,
|
||
_ dt: Double
|
||
) -> Double {
|
||
guard dt > 0.0 else { return 0.0 }
|
||
if t < dt {
|
||
let t2: Double = t / dt
|
||
let t2m1: Double = t2 - 1.0
|
||
let t2p: Double = t2
|
||
// Cubic residual: dt/6 * ((t2-1)^3 - t2^3 + 2*t2 - 1)
|
||
return dt * (t2m1 * t2m1 * t2m1 - t2p * t2p * t2p + 2.0 * t2p - 1.0) / 6.0
|
||
} else if t > 1.0 - dt {
|
||
let t2: Double = (t - 1.0) / dt
|
||
let t2p1: Double = t2 + 1.0
|
||
let t2p: Double = t2
|
||
// Cubic residual: dt/6 * ((t2+1)^3 + t2^3 - 2*t2 - 1)
|
||
return dt * (t2p1 * t2p1 * t2p1 + t2p * t2p * t2p - 2.0 * t2p - 1.0) / 6.0
|
||
}
|
||
return 0.0
|
||
}
|
||
|
||
/// Half-cycle boundary shared by OscSquare, OscTriangle, and OscDutyCycle.
|
||
let oscBLHalfPhase: Double = 0.5
|
||
|
||
/// Triangle raw-waveform slope in normalized-phase units.
|
||
/// Amplitude spans 2 over a half period (0.5), so slope = 2 / 0.5 = 4.
|
||
let oscBLTriangleSlope: Double = 4.0
|
||
|
||
/// Normalises `phase` to `[0, 1)` after a block-sized increment.
|
||
///
|
||
/// Fast path subtracts 1.0 for the common single-wrap case; slow path uses
|
||
/// `truncatingRemainder` to handle extreme `freq × count` values. Shared by
|
||
/// all LF and band-limited oscillators.
|
||
@inline(__always)
|
||
func normalizeOscPhase(_ phase: inout Double) {
|
||
if phase >= 1.0 { phase -= 1.0 }
|
||
if phase >= 1.0 || phase < 0 {
|
||
phase = phase.truncatingRemainder(dividingBy: 1.0)
|
||
if phase < 0 { phase += 1.0 }
|
||
}
|
||
}
|