109 lines
3.4 KiB
Swift
109 lines
3.4 KiB
Swift
//___FILEHEADER___
|
|
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
// MARK: - ___FILEBASENAMEASIDENTIFIER___
|
|
|
|
/// SwiftUI view that drives a `SynthesizerBase` subclass with `@UIParam` slots
|
|
/// and visualises its output via `LyrebirdScopeView`.
|
|
///
|
|
/// Owns a controller object that:
|
|
/// 1. Holds the `SynthesisEngine` reference passed in.
|
|
/// 2. Holds the synth (replace `MyUISynth` with your `@UIParam`-annotated
|
|
/// synthesizer — see the `LyrebirdSynthesizer (UI)` file template).
|
|
/// 3. Wires `engine.scopeTap` to a `ScopeCapture` so the scope view
|
|
/// sees real audio.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// // In your App / scene root:
|
|
/// ___FILEBASENAMEASIDENTIFIER___(engine: engine)
|
|
/// ```
|
|
public struct ___FILEBASENAMEASIDENTIFIER___: View {
|
|
|
|
@State private var controller: Controller
|
|
|
|
public init(engine: SynthesisEngine) {
|
|
_controller = State(wrappedValue: Controller(engine: engine))
|
|
}
|
|
|
|
public var body: some View {
|
|
VStack(spacing: 20) {
|
|
HStack {
|
|
Text("___FILEBASENAMEASIDENTIFIER___").font(.headline)
|
|
Spacer()
|
|
Button(controller.isPlaying ? "Stop" : "Start") {
|
|
controller.toggle()
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.tint(controller.isPlaying ? .red : .green)
|
|
}
|
|
|
|
ParameterSlider(
|
|
"Frequency",
|
|
node: controller.synth.$frequency,
|
|
range: 110...880
|
|
)
|
|
|
|
HStack(spacing: 24) {
|
|
ParameterKnob("Amplitude", node: controller.synth.$amplitude, range: 0...1)
|
|
ParameterKnob("Pan", node: controller.synth.$pan, range: -1...1)
|
|
}
|
|
|
|
Divider()
|
|
LyrebirdStatusView(engine: controller.engine)
|
|
Divider()
|
|
LyrebirdScopeView(capture: controller.scope)
|
|
.frame(height: 120)
|
|
}
|
|
.padding()
|
|
}
|
|
|
|
// MARK: - Controller
|
|
|
|
/// Owns the synth, scope capture, and play/stop state. Lives for the
|
|
/// lifetime of the view via `@State`.
|
|
@Observable
|
|
final class Controller {
|
|
let engine: SynthesisEngine
|
|
let synth: MyUISynth = MyUISynth()
|
|
let scope: ScopeCapture
|
|
var isPlaying: Bool = false
|
|
|
|
init(engine: SynthesisEngine) {
|
|
self.engine = engine
|
|
self.scope = ScopeCapture(buses: [0, 1], bufferSize: 2048)
|
|
engine.scopeTap = scope
|
|
}
|
|
|
|
func toggle() {
|
|
if isPlaying {
|
|
engine.remove(synth)
|
|
} else {
|
|
do { try engine.add(synth) } catch { print("[Lyrebird] engine.add failed: \(error)") }
|
|
}
|
|
isPlaying.toggle()
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Placeholder synth (replace with your own)
|
|
|
|
/// Replace this with your own `@UIParam`-annotated SynthesizerBase subclass
|
|
/// (generated from the `LyrebirdSynthesizer (UI)` template). The Controller
|
|
/// above references it directly — keep the parameter names in sync, or rename
|
|
/// the slot bindings above to match your synth.
|
|
public final class MyUISynth: SynthesizerBase {
|
|
|
|
@UIParam public var frequency: Double = 440.0
|
|
@UIParam public var amplitude: Double = 0.25
|
|
@UIParam public var pan: Double = 0.0
|
|
|
|
public override func build() {
|
|
let osc = OscSin.Audio(freq: $frequency)
|
|
output(bus: 0, source: Pan2(input: osc * $amplitude, pan: $pan))
|
|
}
|
|
}
|