Files
Lyrebird/Demos/LyrebirdDemo/LyrebirdDemo/Demos/BufferDemos.swift
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

150 lines
6.4 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.
// BufferDemos.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import LyrebirdSynth
final class BufferDemos: DemoProtocol {
private let engine: SynthesisEngine
init(engine: SynthesisEngine) {
self.engine = engine
}
func runDemo() {
// BufferDemos.swift lives at .../Demos/LyrebirdDemo/LyrebirdDemo/Demos/BufferDemos.swift
// — 4 components up to reach .../Demos/ where Mozart.aiff lives.
let percURL: URL = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent() // .../Demos/
.deletingLastPathComponent() // .../LyrebirdDemo/
.deletingLastPathComponent() // .../LyrebirdDemo (package)/
.deletingLastPathComponent() // .../Demos/
.appendingPathComponent("Mozart.aiff")
let percBuffer: LyrebirdBuffer
do {
percBuffer = try LyrebirdBuffer.load(url: percURL)
print("Loaded \(percURL.lastPathComponent): \(percBuffer.count) frames, SR \(percBuffer.sampleRate.map { "\($0) Hz" } ?? "unknown")")
} catch {
print("Failed to load Perc.aiff: \(error)")
engine.stop()
exit(1)
}
let sampleRate: Double = engine.sampleRate
let pitchSteps: [(Double, String)] = [
(0.5, "0.5 — octave down"),
(0.75, "0.75 — minor third down"),
(1.0, "1.0 — original pitch"),
(1.5, "1.5 — perfect fifth up"),
(2.0, "2.0 — octave up"),
]
let spectralRatios: [(Double, String)] = [
(0.5, "0.5 — octave down (phase vocoder)"),
(1.0, "1.0 — no shift (phase vocoder passthrough)"),
(1.5, "1.5 — perfect fifth up (phase vocoder)"),
(2.0, "2.0 — octave up (phase vocoder)"),
]
let diffuseSteps: [(Double, String)] = [
(0.0, "0.0 — bypass (mono passthrough on both channels)"),
(0.5, "0.5 — half decorrelation"),
(1.0, "1.0 — full per-channel phase randomisation"),
]
let stretchSteps: [(Double, String)] = [
(3.0, "3.0 — 3× stretch (one-third speed, pitch preserved)"),
(1.5, "1.5 — 1.5× stretch (three-quarter speed, pitch preserved)"),
(1.0, "1.0 — native speed (no stretch)"),
(0.5, "0.5 — 2× compression (double speed, pitch preserved)"),
(0.33, "0.33 — 3× compression (triple speed, pitch preserved)"),
]
// `runDemo()` is @MainActor (see DemoProtocol), so the @MainActor synths are constructed
// directly — no `MainActor.assumeIsolated` wrapping. Build them here, then hand the
// resulting instances / live-param node to the @Sendable task body.
let demo1: PercPlayBuffer = PercPlayBuffer(buffer: percBuffer, engineSampleRate: sampleRate)
let demo2: PercBufferRead = PercBufferRead(buffer: percBuffer)
let demo3: PercPitchShift = PercPitchShift(buffer: percBuffer, engineSampleRate: sampleRate)
let pitchRatioNode: ParameterNode = demo3.$pitchRatio
let demo4Array: [PercSpectralPitchShift] = spectralRatios.map { ratio, _ in
PercSpectralPitchShift(buffer: percBuffer, pitchRatio: ratio, engineSampleRate: sampleRate)
}
let demo5Array: [PercSpectralStereoDiffuse] = diffuseSteps.map { amount, _ in
PercSpectralStereoDiffuse(buffer: percBuffer, amount: amount, engineSampleRate: sampleRate)
}
let demo6Array: [PercTimeStretch] = stretchSteps.map { factor, _ in
PercTimeStretch(buffer: percBuffer, stretchFactor: factor)
}
LyrebirdTask.run(engine: engine) { ctx in
// Demo 1 — PlayBuffer (looping at native rate)
print("\n── Demo 1: PlayBuffer (looping) ──")
ctx.add(demo1)
try ctx.wait(seconds: 6)
ctx.remove(demo1)
try ctx.wait(seconds: 0.2)
// Demo 2 — BufferRead (sine-wave position scan)
print("\n── Demo 2: BufferRead (sine-wave position scan) ──")
ctx.add(demo2)
try ctx.wait(seconds: 6)
ctx.remove(demo2)
try ctx.wait(seconds: 0.2)
// Demo 3 — PlayBuffer → PitchShift (cycling through pitch ratios)
print("\n── Demo 3: PlayBuffer → PitchShift ──")
ctx.add(demo3)
for (ratio, label) in pitchSteps {
print(" pitchRatio \(label)")
ctx.set(pitchRatioNode, to: ratio) // live-param write from the task body
try ctx.wait(seconds: 3)
}
ctx.remove(demo3)
try ctx.wait(seconds: 0.5)
// Demo 4 — PlayBuffer → SpectralChain.Audio (with SpectralPitchShift)
print("\n── Demo 4: PlayBuffer → SpectralChain.Audio (SpectralPitchShift) ──")
for (index, (_, label)) in spectralRatios.enumerated() {
print(" pitchRatio \(label)")
let demo4: PercSpectralPitchShift = demo4Array[index]
ctx.add(demo4)
try ctx.wait(seconds: 5)
ctx.remove(demo4)
try ctx.wait(seconds: 0.3)
}
// Demo 5 — SpectralChain.Audio fan-out (mono → 2-channel decorrelated stereo)
print("\n── Demo 5: SpectralChain.Audio fan-out (SpectralStereoDiffuse) ──")
for (index, (_, label)) in diffuseSteps.enumerated() {
print(" amount \(label)")
let demo5: PercSpectralStereoDiffuse = demo5Array[index]
ctx.add(demo5)
try ctx.wait(seconds: 5)
ctx.remove(demo5)
try ctx.wait(seconds: 0.3)
}
// Demo 6 — TimeStretch (granular, pitch-preserved time stretching + compression)
print("\n── Demo 6: TimeStretch (granular time-stretch / compression) ──")
for (index, (_, label)) in stretchSteps.enumerated() {
print(" stretchFactor \(label)")
let demo6: PercTimeStretch = demo6Array[index]
ctx.add(demo6)
try ctx.wait(seconds: 6)
ctx.remove(demo6)
try ctx.wait(seconds: 0.3)
}
print("\nAll buffer demos complete.")
}
}
}