Files
2026-06-01 18:27:40 -07:00

45 lines
1.7 KiB
Swift

// LyrebirdFM.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
/// Two-operator FM synthesizer used by the AUv3 demo extension.
///
/// Carrier and modulator are both `OscSin.Audio`. The modulator's output is
/// added to the carrier's frequency input; modulation depth is
/// `modIndex * carrierFreq` so the index reads as a unitless ratio (1.0 ≈
/// one octave of FM bandwidth at the carrier).
///
/// All four `@LiveParam`s map onto AU parameters in `LyrebirdFMAudioUnit`'s
/// `parameterMetadata()` table.
final class LyrebirdFM: SynthesizerBase {
/// Carrier frequency in Hz. Range [20, 8000]. Default 440 (A4).
@LiveParam var carrierFreq: Double = 440.0
/// Modulator-to-carrier frequency ratio. Range [0.25, 16]. Default 2.0
/// (one octave above carrier — produces an even-harmonic-rich timbre).
@LiveParam var modRatio: Double = 2.0
/// Modulation index (unitless). Range [0, 8]. Default 0.5 — mild
/// modulation; sounds close to a sine with extra brightness.
@LiveParam var modIndex: Double = 0.5
/// Output amplitude. Range [0, 1]. Default 0.3 — leaves headroom.
@LiveParam var amp: Double = 0.3
override func build() {
let modFreq = $carrierFreq * $modRatio
let modDepth = $modIndex * $carrierFreq
let modulator = OscSin.Audio(freq: modFreq, mul: modDepth)
let carrier = OscSin.Audio(freq: $carrierFreq + modulator)
let voice = carrier * $amp
// Mono → both stereo channels for AUv3 host compatibility.
output(bus: 0, source: voice)
output(bus: 1, source: voice)
}
}