Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdOscillator.xctemplate/___FILEBASENAME___.swift
T
josh f8878324fd v2.1.1 release: delay-click fix + internal consolidation
Mirror of Lyrebird dev HEAD (9f791c7c) minus maintainer-only paths. Patch release:
fixes the allpass fractional-delay click (DelayLine/CombFilter/AllpassFilter/
DelayLineFeedback under a modulated delay) and lands the internal code-quality
consolidation pass (~160 deduped sites + shared base classes/helpers, bit-identical
output). No public API removed or changed. See RELEASES.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-07 21:20:49 -07:00

169 lines
6.4 KiB
Swift

//___FILEHEADER___
//
// In-tree template — drop this file under Sources/Lyrebird/Generators/Oscillators/.
// Extends `OscillatorBase`, whose designated init is module-internal, so this
// template is not usable from a project that depends on Lyrebird as a package.
// External users should start from the basic `LyrebirdGenerator` template and
// declare phase / freq / MulAddSpec inline.
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
///
/// Extends `OscillatorBase` so the standard `_phase` / `_freqInput` /
/// `_freqIsGenerator` / `_mulAdd` fields are inherited — no need to redeclare them.
/// `super.init(freq:phase:mul:add:)` normalises `phase` into `[0, 1)`.
///
/// ## Usage
/// ```swift
/// let osc = ___FILEBASENAMEASIDENTIFIER___.Audio(freq: 440.0)
/// let lfo = ___FILEBASENAMEASIDENTIFIER___.Control(freq: 2.0)
/// ```
///
/// - 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 enum ___FILEBASENAMEASIDENTIFIER___ {
/// Shared base — inherits all oscillator state from `OscillatorBase`.
public class _Base: OscillatorBase {}
/// Per-sample waveform function. Replace the formula below — `phase` is in
/// `[0, 1)`, output should typically be `[-1, 1]`.
@inline(__always)
static func _waveform(_ phase: Double) -> Double {
// Default: sawtooth. Replace with your formula.
return 2.0 * phase - 1.0
}
/// Audio-rate ___FILEBASENAMEASIDENTIFIER___. 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(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
}
_result[0][i] = ___FILEBASENAMEASIDENTIFIER___._waveform(_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
}
}
/// Control-rate ___FILEBASENAMEASIDENTIFIER___. Advances phase once per audio block,
/// emits a linear ramp toward the new target.
///
/// - 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, BlockRateControlGenerator {
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)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let sampleRate = 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 = freq / sampleRate
_phase += dt * countFloat
normalizeOscPhase(&_phase)
let target = ___FILEBASENAMEASIDENTIFIER___._waveform(_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 ___FILEBASENAMEASIDENTIFIER___: GeneratorEnum {
public typealias AudioType = ___FILEBASENAMEASIDENTIFIER___.Audio
public typealias ControlType = ___FILEBASENAMEASIDENTIFIER___.Control
}