v2.4.0 release: event patterns + live event conductor

This commit is contained in:
2026-12-03 08:45:07 -08:00
parent a175c437de
commit 6da3615707
40 changed files with 4223 additions and 19 deletions
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
0306C7C02FAC451300952EAF /* GVerbTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0306C7BF2FAC451300952EAF /* GVerbTest.swift */; };
0306C7C22FAC45F200952EAF /* ReverbDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0306C7C12FAC45F200952EAF /* ReverbDemo.swift */; };
631D4C5B1150427CB8D15210 /* EventPatternDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99B83EC83F6C48D98441D7F7 /* EventPatternDemo.swift */; };
035A2F422FAA4F67007F6289 /* PercPitchShift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035A2F3F2FAA4F67007F6289 /* PercPitchShift.swift */; };
035A2F432FAA4F67007F6289 /* PercSpectralPitchShift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035A2F412FAA4F67007F6289 /* PercSpectralPitchShift.swift */; };
035A2F482FAA4F67007F6289 /* PercSpectralStereoDiffuse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035A2F492FAA4F67007F6289 /* PercSpectralStereoDiffuse.swift */; };
@@ -26,6 +27,7 @@
/* Begin PBXFileReference section */
0306C7BF2FAC451300952EAF /* GVerbTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GVerbTest.swift; sourceTree = "<group>"; };
0306C7C12FAC45F200952EAF /* ReverbDemo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReverbDemo.swift; sourceTree = "<group>"; };
99B83EC83F6C48D98441D7F7 /* EventPatternDemo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventPatternDemo.swift; sourceTree = "<group>"; };
035A2F3E2FAA4F67007F6289 /* PercBufferRead.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PercBufferRead.swift; sourceTree = "<group>"; };
035A2F3F2FAA4F67007F6289 /* PercPitchShift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PercPitchShift.swift; sourceTree = "<group>"; };
035A2F402FAA4F67007F6289 /* PercPlayBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PercPlayBuffer.swift; sourceTree = "<group>"; };
@@ -56,6 +58,7 @@
isa = PBXGroup;
children = (
0306C7C12FAC45F200952EAF /* ReverbDemo.swift */,
99B83EC83F6C48D98441D7F7 /* EventPatternDemo.swift */,
035B00032FAB0000007F0001 /* DemoProtocol.swift */,
035B00042FAB0000007F0001 /* BufferDemos.swift */,
);
@@ -207,6 +210,7 @@
035A2F482FAA4F67007F6289 /* PercSpectralStereoDiffuse.swift in Sources */,
035A2F442FAA4F67007F6289 /* PercBufferRead.swift in Sources */,
0306C7C22FAC45F200952EAF /* ReverbDemo.swift in Sources */,
631D4C5B1150427CB8D15210 /* EventPatternDemo.swift in Sources */,
035A2F452FAA4F67007F6289 /* PercPlayBuffer.swift in Sources */,
C1A000000000000000000007 /* main.swift in Sources */,
C1A000000000000000000016 /* SineTone.swift in Sources */,
@@ -0,0 +1,110 @@
// EventPatternDemo.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import LyrebirdSynth
/// Demonstrates ``EventPattern`` — Lyrebird's event-driven player: bound value patterns drive a
/// fresh self-freeing voice per event, played as a dur/legato-timed stream.
///
/// Also shows two patterns running together via ``ParallelEventPattern``: a lead phrase and a
/// bass line that enters a few beats in on the same beat grid, with a single
/// ``EventPatternHandle/waitUntilComplete(releasePadding:)`` blocking until the longer of the two
/// finishes.
final class EventPatternDemo: DemoProtocol {
private let engine: SynthesisEngine
init(engine: SynthesisEngine) {
self.engine = engine
}
/// A gated voice (``EventGated``): a sine through an `EnvelopeGen` whose gate holds it at full
/// level while the note sounds, then plays a release and frees the voice via
/// `doneAction: .freeNode` when `EventPattern` closes the gate at the note's sustain. No
/// external removal, so the release is never hard-cut — nothing to tune, no click.
private final class BlipVoice: SynthesizerBase, EventGated {
/// Envelope release length in seconds — how long a note rings after its gate closes.
/// Exposed so the demo can let the final note's tail finish before stopping the engine.
nonisolated static let releaseSeconds: Double = 0.3
/// Envelope attack length in seconds.
nonisolated static let attackSeconds: Double = 0.01
@LiveParam var freq: Double = 440.0
@LiveParam var amp: Double = 0.2
@LiveParam var gate: Double = 1.0 // open by default so the attack fires on add
var eventGate: ParameterNode { $gate }
override func build() {
// `releaseNode: 1` holds at `levels[1]` (full) while the gate is open; when the gate
// drops to 0 the generator plays segment 1 (the release to 0) and then self-frees.
let envelope: Envelope = Envelope(
levels: [0.0, 1.0, 0.0],
durations: [BlipVoice.attackSeconds, BlipVoice.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)
output(bus: 0, source: source * shaped)
}
}
func runDemo() {
print("\n── EventPattern demo ──")
let tempoBPM: Double = 132.0
// Pattern 1 — the lead: a short A-minor-pentatonic phrase, written as named pitches rather
// than raw Hz (equivalent MIDI form: `Pitch.midi(69).frequency`, etc.), played three times.
let phrase: [Pitch] = [
.named(.a, 4), .named(.c, 5), .named(.d, 5), .named(.e, 5),
.named(.g, 5), .named(.e, 5), .named(.d, 5), .named(.c, 5),
]
let leadFreqs: [Double] = phrase.compactMap(\.frequency)
let lead: EventPattern<BlipVoice> = EventPattern(make: { BlipVoice() })
.bind(\.freq, .sequence(leadFreqs, repeats: 1))
.bind(\.amp, .uniformRandom(low: 0.15, high: 0.3))
.dur(.sequence([0.25, 0.25, 0.25, 0.5]))
.legato(0.8)
.tempo(tempoBPM)
.repeats(3)
// Pattern 2 — a bass line that enters `leadInBeats` after the lead and outlasts it.
// There is no cross-pattern clock: `ParallelEventPattern` launches both children from the
// one `play` call, so they share a start instant, and at the SAME tempo they share a beat
// grid. `addAfter(beats:)` staggers the bass's entry on that grid — the bass simply waits
// `leadInBeats` beats before its first note.
let leadInBeats: Double = 2.0
let bassPhrase: [Pitch] = [.named(.a, 2), .named(.a, 2), .named(.g, 2), .named(.e, 2)]
let bassFreqs: [Double] = bassPhrase.compactMap(\.frequency)
let bass: EventPattern<BlipVoice> = EventPattern(make: { BlipVoice() })
.bind(\.freq, .sequence(bassFreqs, repeats: 1))
.bind(\.amp, .uniformRandom(low: 0.2, high: 0.3))
.dur(.sequence([1.0]))
.legato(0.9)
.tempo(tempoBPM)
.repeats(1)
// Start both together, staggering the bass two beats in, and control them as one handle.
let ensemble: ParallelEventPattern = ParallelEventPattern()
ensemble.add(lead)
ensemble.addAfter(beats: leadInBeats, eventPattern: bass)
let handle: EventPatternHandle = ensemble.play(on: engine)
// `waitUntilComplete` blocks until EVERY child task has finished — so it waits for the
// longer-running bass, not just the lead. `releasePadding` then lets the final note's
// envelope release ring out before `main.swift` stops the engine — without it,
// `engine.stop()` would cut the tail (an end-of-phrase click).
handle.waitUntilComplete(releasePadding: BlipVoice.releaseSeconds)
print("\nEventPattern demo complete.")
}
}
+13 -3
View File
@@ -7,11 +7,18 @@
import Foundation
import LyrebirdSynth
// ── Demo selector ─────────────────────────────────────────────────────────────
// Change `demo` to `.buffer`, `.reverb`, or `.eventPattern` to switch demos.
private enum Demo { case buffer, reverb, eventPattern }
private let demo: Demo = .eventPattern
// ──────────────────────────────────────────────────────────────────────────────
// `makeDefaultAudioDriver()` selects the platform backend — Core Audio on Apple,
// ALSA on Linux (needs libasound) — so the demo builds and runs on both.
let engine = SynthesisEngine(
blockSize: 512,
sampleRate: 44100,
controlBlockSize: 64,
sampleRate: 48000,
numOutputChannels: 2,
numInputChannels: 2,
driver: makeDefaultAudioDriver()
@@ -24,7 +31,10 @@ do {
exit(1)
}
// BufferDemos(engine: engine).runDemo()
ReverbDemo(engine: engine).runDemo()
switch demo {
case .buffer: BufferDemos(engine: engine).runDemo()
case .reverb: ReverbDemo(engine: engine).runDemo()
case .eventPattern: EventPatternDemo(engine: engine).runDemo()
}
engine.stop()