48 lines
1.7 KiB
Swift
48 lines
1.7 KiB
Swift
// ConductorDemoVoice.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
|
|
/// A gated sine voice for the conductor demo. ``EventPattern`` holds its gate open for the note's
|
|
/// sustain, then closes it; the envelope plays its release and frees the voice, so a part that is
|
|
/// toggled off rings out cleanly instead of being cut.
|
|
final class ConductorDemoVoice: SynthesizerBase, EventGated {
|
|
|
|
/// Envelope attack length in seconds.
|
|
nonisolated static let attackSeconds: Double = 0.01
|
|
|
|
/// Envelope release length in seconds — how long a note rings after its gate closes.
|
|
nonisolated static let releaseSeconds: Double = 0.3
|
|
|
|
/// Oscillator frequency in Hz. Bound per event by the pattern.
|
|
@LiveParam var freq: Double = 440.0
|
|
|
|
/// Linear output gain, 0...1. Bound per event by the pattern.
|
|
@LiveParam var amp: Double = 0.2
|
|
|
|
/// Envelope gate. Open by default so the attack fires when the voice is added.
|
|
@LiveParam var gate: Double = 1.0
|
|
|
|
var eventGate: ParameterNode { $gate }
|
|
|
|
override func build() {
|
|
let envelope: Envelope = Envelope(
|
|
levels: [0.0, 1.0, 0.0],
|
|
durations: [ConductorDemoVoice.attackSeconds, ConductorDemoVoice.releaseSeconds],
|
|
curves: [.welch, .welch],
|
|
releaseNode: 1
|
|
)
|
|
let shaped: EnvelopeGen.Audio = EnvelopeGen.Audio(
|
|
envelope: envelope,
|
|
gate: $gate,
|
|
doneAction: .freeNode
|
|
)
|
|
let source: OscSin.Audio = OscSin.Audio(freq: $freq, mul: $amp)
|
|
let mono: any ValidGeneratorInput = source * shaped
|
|
output(bus: 0, source: Pan2(input: mono, pan: 0.0))
|
|
}
|
|
}
|