77 lines
2.6 KiB
Swift
77 lines
2.6 KiB
Swift
// TestLoopSource.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import LyrebirdSynth
|
||
import LyrebirdUI
|
||
|
||
/// A synthesized test signal source that writes to a target audio bus.
|
||
///
|
||
/// Produces two detuned sine oscillators at 110 Hz and 165 Hz — an approximation
|
||
/// of a guitar's A2 and E3 strings — with a slow amplitude envelope driven by a
|
||
/// low-frequency sine LFO. This makes the board audible immediately without any
|
||
/// hardware connection.
|
||
///
|
||
/// The output is written to `targetBus` so downstream pedals read it from that bus.
|
||
/// `TestLoopSource` should be added to the engine **before** the first pedal in the
|
||
/// chain to ensure signal-order correctness (source evaluates first).
|
||
///
|
||
/// ## Parameters
|
||
///
|
||
/// - Parameters:
|
||
/// - targetBus: Audio bus index to write the test signal to. Typically the
|
||
/// pedalboard's head bus index.
|
||
/// - amplitude: Output amplitude in [0, 1]. Defaults to `0.25`.
|
||
public final class TestLoopSource: SynthesizerBase {
|
||
|
||
// MARK: - Parameters
|
||
|
||
/// Output amplitude in [0, 1].
|
||
@UIParam public var amplitude: Double = 0.25
|
||
|
||
// MARK: - Constants
|
||
|
||
/// Frequency of the first oscillator in Hz (approximates guitar A2 string).
|
||
private let freqA: Double = 110.0
|
||
|
||
/// Frequency of the second oscillator in Hz (approximates guitar E3 string, slightly detuned).
|
||
private let freqB: Double = 165.75
|
||
|
||
/// LFO frequency in Hz for amplitude modulation (slow tremolo).
|
||
private let lfoFreq: Double = 0.5
|
||
|
||
/// LFO depth as a fraction of base amplitude [0, 1].
|
||
private let lfoDepth: Double = 0.3
|
||
|
||
/// Gain for each individual oscillator before summing. Two oscillators × 0.5 = unity.
|
||
private let oscGain: Double = 0.5
|
||
|
||
// MARK: - Routing
|
||
|
||
/// The bus this source writes its output to.
|
||
private let targetBus: Int
|
||
|
||
// MARK: - Init
|
||
|
||
/// Creates a test loop source that writes to `targetBus`.
|
||
///
|
||
/// - Parameters:
|
||
/// - targetBus: Audio bus index to write the synthesized test signal to.
|
||
public init(targetBus: Int) {
|
||
self.targetBus = targetBus
|
||
super.init()
|
||
}
|
||
|
||
// MARK: - Build
|
||
|
||
public override func build() {
|
||
let oscA: OscSin.Audio = OscSin.Audio(freq: freqA, mul: oscGain)
|
||
let oscB: OscSin.Audio = OscSin.Audio(freq: freqB, phase: 0.25, mul: oscGain)
|
||
let lfo: OscSin.Audio = OscSin.Audio(freq: lfoFreq, mul: lfoDepth, add: 1.0 - lfoDepth)
|
||
let signal: any ValidGeneratorInput = (oscA + oscB) * lfo * $amplitude
|
||
output(bus: targetBus, source: signal)
|
||
}
|
||
}
|