Files
Lyrebird/Demos/LyrebirdMacOSDemo/LyrebirdMacOSDemo/Synthesizers/VectorOscillatorUI.swift
T
2026-06-01 18:27:40 -07:00

79 lines
2.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// VectorOscillatorUI.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// A stereo vector oscillator whose XY position, frequency, and amplitude are
/// live-controllable from the UI.
///
/// Four corner wavetables are crossfaded on a 2D XY plane:
///
/// C (triangle) ── D (Hanning) (y = 1)
/// │ │
/// A (sine) ──── B (saw-ish) (y = 0)
/// x=0 x=1
///
/// Moving X blends A↔B (and C↔D); moving Y blends the AB mix into the CD mix.
final class VectorOscillatorUI: SynthesizerBase {
/// X-axis blend position. 0 = corner A/C, 1 = corner B/D.
/// Normalized [0, 1]. XY pad range: 0…1.
@UIParam var x: Double = 0.5
/// Y-axis blend position. 0 = bottom (A, B), 1 = top (C, D).
/// Normalized [0, 1]. XY pad range: 0…1.
@UIParam var y: Double = 0.5
/// Oscillator frequency in Hz. Slider range: 40–2000 Hz.
@UIParam var frequency: Double = 220.0
/// Output amplitude (linear gain). Knob range: 0–1.
@UIParam var amplitude: Double = 0.25
private let bufferA: LyrebirdBuffer // corner A: sine
private let bufferB: LyrebirdBuffer // corner B: 16-partial saw-ish
private let bufferC: LyrebirdBuffer // corner C: triangle
private let bufferD: LyrebirdBuffer // corner D: Hanning window shape
override init() {
let tableSize = lyrebirdDefaultTableSize
let a = LyrebirdBuffer(count: tableSize)
a.fillSine()
let b = LyrebirdBuffer(count: tableSize)
b.fill(partials: 16)
let c = LyrebirdBuffer(count: tableSize)
c.fillTriangle()
let d = LyrebirdBuffer(count: tableSize)
d.fillHanning()
self.bufferA = a
self.bufferB = b
self.bufferC = c
self.bufferD = d
super.init()
}
override func build() {
let osc = VectorOscillator.Audio(
bufferA: bufferA,
bufferB: bufferB,
bufferC: bufferC,
bufferD: bufferD,
freq: $frequency,
x: $x,
y: $y,
mul: $amplitude
)
output(bus: 0, source: Pan2(input: osc, pan: 0.0))
}
}