Files
Lyrebird/Sources/Lyrebird/Lyrebird.docc/Generators/Oscillators.md
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

9.2 KiB
Raw Blame History

Oscillators

@Metadata { @PageKind(article) }

Table-lookup, band-limited, LFO, and impulse oscillators shipped with the Lyrebird module.

All paired oscillators (OscSin, OscTable, Impulse) expose both an Audio and a Control variant under a caseless enum namespace conforming to GeneratorEnum. See doc:GeneratorProtocol for the namespace pattern and doc:WritingGenerators for authoring guidance.

Output buffers are pre-allocated to lyrebirdDefaultResultBufferSize (512 samples) in init. Each _computeSamples implementation opens with a precondition — no heap allocation in steady state.


OscSin

Table-lookup sine oscillator with a 32 768-sample single-cycle table and selectable interpolation.

let lfo: OscSin.Control = OscSin.Control(freq: 5.0, mul: 10.0, add: 440.0)

_ = OscSin.Audio(freq: 440.0)                                          // constant freq, linear interp
_ = OscSin.Audio(freq: lfo, phase: 0.25)                              // generator input, quarter-phase offset
_ = OscSin.Audio(freq: 440.0, interpolation: .cubic)                  // Catmull-Rom cubic
_ = OscSin.Audio(freq: 440.0, table: engine.sharedTables.sineTable)   // share engine's pre-computed table
  • freq — any ValidGeneratorInput. Scalar inputs call floatValue once per block. Generator inputs are read at audio rate, enabling true FM synthesis.
  • phase — fraction of a full cycle [0, 1).
  • interpolation — TableInterpolation: .none, .linear (default), .cubic.
  • table — optional; defaults to a module-level lazy singleton. Pass engine.sharedTables.sineTable to avoid the second allocation.

OscTable

Table-lookup oscillator for user-supplied LyrebirdBuffer content.

let table = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
table.fill { i, n in Double(i) / Double(n) * 2.0 - 1.0 }  // raw sawtooth
let osc = OscTable.Audio(table: table, freq: 440.0)

// Band-limited sawtooth — no aliasing at 440 Hz / 44100 Hz sample rate
let blTable = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
blTable.fillBandLimited(.sawtooth, fundamental: 440.0, sampleRate: 44100.0)
let blOsc = OscTable.Audio(table: blTable, freq: 440.0)

// Chebyshev waveshaper table — only produces the 3rd harmonic when driven by a sine
let chebTable = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
chebTable.fillChebyshev(coefficients: [0, 0, 1])
let shaper = OscTable.Audio(table: chebTable, freq: 440.0)

// Cubic interpolation for a small table
let smallTable = LyrebirdBuffer(count: 64)
smallTable.fillSine()
let osc2 = OscTable.Audio(table: smallTable, freq: 440.0, interpolation: .cubic)
  • Accepts any table size (power-of-two or not). Index wrapping uses %.
  • Checks table.isValid (acquiring load) each block; outputs silence when invalidated.
  • freq supports audio-rate generator inputs for FM/PM with arbitrary waveforms.

The full set of buffer fill methods — harmonic series, Chebyshev polynomials, window functions, waveshaper curves, band-limited waveforms, inharmonic partials, and WavetableSet factory methods — is documented in doc:WavetableFill.


Impulse

Outputs 1.0 once per period, 0.0 otherwise.

Impulse.Audio(freq: 4.0)                  // fires 4× per second
Impulse.Audio(freq: 1.0, initPhase: 0.5)  // first fire at half-period offset

freq supports audio-rate generator inputs for per-sample timing accuracy.


Band-Limited Oscillators

Oscillators using polyBLEP (polynomial bandlimited step) and polyBLAMP (polynomial bandlimited ramp) discontinuity correction. Significantly reduced aliasing compared to naive waveform generators at audio frequencies.

All four types expose both Audio and Control variants under a GeneratorEnum-conforming enum namespace. All support mul/add and a phase offset.

OscSawtooth

Linear ascending ramp reset to −1 at each period. PolyBLEP step correction at the reset point.

let lfo: OscSin.Audio = OscSin.Audio(freq: 5.0)
_ = OscSawtooth.Audio(freq: 440.0)
_ = OscSawtooth.Audio(freq: lfo * 200 + 440, phase: 0.5)   // phase offset, audio-rate FM
_ = OscSawtooth.Control(freq: 2.0)                          // LFO use at control rate
  • freq — any ValidGeneratorInput. Generator inputs enable audio-rate FM.
  • phase — normalized cycle offset [0, 1). Defaults to 0.0.

OscSquare

Symmetric square wave (duty cycle fixed at 0.5). PolyBLEP corrections at both the rising and falling discontinuities.

OscSquare.Audio(freq: 110.0)
OscSquare.Audio(freq: 220.0, phase: 0.25)

For variable duty cycles use OscDutyCycle.

OscTriangle

Triangle wave (linear ascent and descent, no discontinuities). PolyBLAMP slope correction at the two peak-and-trough inflection points; reduces harmonic distortion from naive rounding.

OscTriangle.Audio(freq: 330.0)
OscTriangle.Audio(freq: 330.0, phase: 0.25)

OscDutyCycle

Variable duty-cycle pulse wave. PolyBLEP correction applied at both the rising edge (phase = 0) and the falling edge (phase = dutyCycle). Supports audio-rate PWM when dutyCycle is a Generator.

let lfo: OscSin.Audio = OscSin.Audio(freq: 5.0)
_ = OscDutyCycle.Audio(freq: 440.0)                             // 0.5 = square
_ = OscDutyCycle.Audio(freq: 440.0, dutyCycle: 0.25)            // 25% duty cycle
_ = OscDutyCycle.Audio(freq: 440.0, dutyCycle: lfo * 0.4 + 0.5) // audio-rate PWM
_ = OscDutyCycle.Control(freq: 1.0, dutyCycle: 0.3)
  • dutyCycle — normalized [0, 1]. Clamped internally. 0.5 = square wave. Defaults to 0.5.
  • freq supports audio-rate modulation for FM combined with PWM.

Note: PolyBLEP/BLAMP correction reduces aliasing but does not eliminate it entirely at high fundamental frequencies. For truly alias-free output at mid-high frequencies use OscTable with a band-limited table computed via fillBandLimited(_:fundamental:sampleRate:).


LFO Oscillators

Raw piecewise-math waveforms without anti-aliasing. Intended for low-frequency use (LFOs, sub-audio envelopes, modulation signals) where aliasing is inaudible or irrelevant. Lower overhead than the polyBLEP oscillators.

All four types expose both Audio and Control variants under a GeneratorEnum-conforming enum namespace.

LFSawtooth

Linear ascending ramp. Formula: 2 × phase − 1. Range [−1, 1].

let lfo: OscSin.Audio = OscSin.Audio(freq: 0.5)
_ = LFSawtooth.Audio(freq: 0.5)                       // slow ascending ramp, 0.5 Hz
_ = LFSawtooth.Audio(freq: lfo, phase: 0.5)           // phase offset
_ = LFSawtooth.Control(freq: 2.0)                     // one new ramp value per audio block

LFSquare

Symmetric square wave, no correction. Formula: phase < 0.5 ? 1.0 : −1.0. Range {−1, 1}.

LFSquare.Audio(freq: 1.0)
LFSquare.Control(freq: 0.5)    // toggles each second

For audio-rate use with reduced aliasing prefer OscSquare or OscDutyCycle.

LFTriangle

Triangle wave. Formula: 4 × |phase − 0.5| − 1. Range [−1, 1], peak at mid-cycle.

LFTriangle.Audio(freq: 2.0)
LFTriangle.Control(freq: 0.25)   // four-second triangle cycle

LFDutyCycle

Variable duty-cycle pulse with no BLEP correction. Same formula as OscDutyCycle but unfiltered. Supports audio-rate PWM via a Generator dutyCycle input.

let envGen: OscSin.Audio = OscSin.Audio(freq: 0.5, mul: 0.3, add: 0.5)
_ = LFDutyCycle.Audio(freq: 1.0, dutyCycle: 0.3)            // 30% duty, 1 Hz
_ = LFDutyCycle.Audio(freq: 2.0, dutyCycle: envGen)         // envelope-driven PWM
_ = LFDutyCycle.Control(freq: 0.5, dutyCycle: 0.75)
  • dutyCycle — normalized [0, 1]. Clamped internally. Defaults to 0.5.

Phasor

Phasor is a linear ramp / phase accumulator. Unlike the oscillators above it is driven by a per-sample increment rate (not a frequency in Hz) and ramps over an arbitrary range [start, end), wrapping when it reaches end. A rising edge on trig resets the phase to resetPos. This is the canonical oscillator-sync source and wavetable / buffer scan driver.

Phasor.Audio(rate: 1.0, start: 0.0, end: 512.0)             // scan a 512-frame table
Phasor.Audio(trig: sync, rate: inc, start: 0.0, end: 1.0)   // re-triggerable [0,1) ramp
Phasor.Control(rate: 0.001, start: 0.0, end: 1.0)           // slow control ramp
  • trig — any ValidGeneratorInput; a rising edge resets phase to resetPos. Defaults to 0.0.
  • rate — per-sample increment (NOT Hz). For a Hz-based scan use rate = freq * (end - start) / sampleRate. Defaults to 1.0.
  • start / end — ramp bounds; output is [start, end). Default 0.0 / 1.0.
  • resetPos — value the phase jumps to on a trigger. Defaults to 0.0.

Choosing Between Band-Limited and LFO Oscillators

Use case Recommendation
Audio-rate signals at any audible frequency OscSawtooth / OscSquare / OscTriangle / OscDutyCycle
LFO modulation (< ~20 Hz) LFSawtooth / LFSquare / LFTriangle / LFDutyCycle
Completely alias-free output OscTable with fillBandLimited
Audio-rate PWM at high frequencies OscDutyCycle (polyBLEP)
Custom waveform / wavetable morphing OscTable + WavetableSet (see doc:WavetableFill)