223 lines
8.7 KiB
Swift
223 lines
8.7 KiB
Swift
// LFDutyCycle.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// MARK: - LFDutyCycle
|
|
|
|
/// Non-band-limited variable duty-cycle pulse wave LFO. Range `[-1, 1]`.
|
|
///
|
|
/// Output is `+1` when `phase < dutyCycle`, and `-1` otherwise. Duty cycle is clamped to
|
|
/// `[0, 1]` internally. At `dutyCycle = 0.5` produces the same output as `LFSquare`.
|
|
/// No anti-aliasing — intended for LFO / control-rate use.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Frequency in Hz. No internal clamping.
|
|
/// - dutyCycle: Pulse duty cycle, normalized `[0, 1]`. Clamped internally. `0.5` = square. Defaults to `0.5`.
|
|
/// - 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 LFDutyCycle {
|
|
|
|
// MARK: - Shared base
|
|
|
|
/// Shared state for audio-rate and control-rate LFDutyCycle variants.
|
|
public class _Base: OscillatorBase {
|
|
let _dutyCycleInput: any ValidGeneratorInput
|
|
let _dutyCycleIsGenerator: Bool
|
|
|
|
init(
|
|
freq: any ValidGeneratorInput,
|
|
dutyCycle: any ValidGeneratorInput,
|
|
phase: Double,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
let resolvedDutyCycle: any ValidGeneratorInput = dutyCycle._resolvedInput()
|
|
self._dutyCycleInput = resolvedDutyCycle
|
|
self._dutyCycleIsGenerator = resolvedDutyCycle is any Generator
|
|
super.init(freq: freq, phase: phase, mul: mul, add: add)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Audio-rate non-band-limited variable duty-cycle pulse LFO. Evaluates frequency per sample.
|
|
///
|
|
/// Supports audio-rate duty-cycle modulation (PWM) when `dutyCycle` is a Generator.
|
|
///
|
|
/// - Parameters:
|
|
/// - freq: Frequency in Hz. No internal clamping.
|
|
/// - dutyCycle: Pulse duty cycle, normalized `[0, 1]`. Clamped internally. `0.5` = square. Defaults to `0.5`.
|
|
/// - 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.
|
|
/// - dutyCycle: Pulse duty cycle, normalized `[0, 1]`. Clamped internally. `0.5` = square. Defaults to `0.5`.
|
|
/// - 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,
|
|
dutyCycle: any ValidGeneratorInput = 0.5,
|
|
phase: Double = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, dutyCycle: dutyCycle, 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
|
|
}
|
|
|
|
let dutySamples: [Double]?
|
|
let scalarDuty: Double
|
|
if _dutyCycleIsGenerator {
|
|
dutySamples = _dutyCycleInput.calculatedSamples(graph: graph, count: count).first
|
|
scalarDuty = 0
|
|
} else {
|
|
let rawDuty: Double = _dutyCycleInput.floatValue(graph: graph)
|
|
scalarDuty = min(max(rawDuty, 0.0), 1.0)
|
|
dutySamples = nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
let duty: Double
|
|
if let ds: [Double] = dutySamples {
|
|
let raw: Double = i < ds.count ? ds[i] : 0
|
|
duty = min(max(raw, 0.0), 1.0)
|
|
} else {
|
|
duty = scalarDuty
|
|
}
|
|
|
|
// Pulse: +1 while phase < duty, -1 otherwise
|
|
_result[0][i] = _phase < duty ? 1.0 : -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 variable duty-cycle pulse 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.
|
|
/// - dutyCycle: Pulse duty cycle, normalized `[0, 1]`. Clamped internally. `0.5` = square. Defaults to `0.5`.
|
|
/// - 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.
|
|
/// - dutyCycle: Pulse duty cycle, normalized `[0, 1]`. Clamped internally. `0.5` = square. Defaults to `0.5`.
|
|
/// - 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,
|
|
dutyCycle: any ValidGeneratorInput = 0.5,
|
|
phase: Double = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(freq: freq, dutyCycle: dutyCycle, 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 rawDuty: Double
|
|
if _dutyCycleIsGenerator {
|
|
rawDuty = _dutyCycleInput.calculatedSamples(graph: graph, count: count).first?.first ?? 0
|
|
} else {
|
|
rawDuty = _dutyCycleInput.floatValue(graph: graph)
|
|
}
|
|
let duty: Double = min(max(rawDuty, 0.0), 1.0)
|
|
|
|
let dt: Double = freq / sampleRate
|
|
_phase += dt * countFloat
|
|
normalizeOscPhase(&_phase)
|
|
|
|
let target: Double = _phase < duty ? 1.0 : -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 LFDutyCycle: GeneratorEnum {
|
|
public typealias AudioType = LFDutyCycle.Audio
|
|
public typealias ControlType = LFDutyCycle.Control
|
|
}
|