Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileTests/SnippetScheduledLoopTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

105 lines
4.2 KiB
Swift

// SnippetScheduledLoopTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS) || os(Linux)
import Testing
import LyrebirdSynth
@testable import LyrebirdLiveCompile
/// Proves the scratch editor runs *functional* Swift — a real `for` loop that
/// schedules a timed sequence — not just single-graph expressions.
@MainActor
@Suite("Snippet scheduled loop")
struct SnippetScheduledLoopTests {
@Test func snippetCanUseFoundationMath() throws {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 512,
controlBlockSize: 64,
sampleRate: 44_100,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
let session: LiveSession = LiveSession(engine: engine, attachScope: false)
try engine.start()
// `pow` / `sin` come from Foundation, which the harness preamble imports —
// a snippet can't add the import itself. This would fail to compile
// ("cannot find 'pow' in scope") without it.
let snippet: String = """
let semitone: Double = pow(2.0, 1.0 / 12.0)
let freq: Double = 440.0 * semitone
context.play { OscSin.Audio(freq: freq) * (0.1 * sin(0.0) + 0.1) }
context.reportMessage("math ok \\(freq)")
"""
try session.compileAndRun(snippet)
#expect(session.lastSnippetMessage?.hasPrefix("math ok") == true)
session.clearTracked()
}
@Test func loopSchedulesTenSelfFreeingTones() throws {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 512,
controlBlockSize: 64,
sampleRate: 44_100,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
let session: LiveSession = LiveSession(engine: engine)
try engine.start()
// A `for` loop that schedules ten short sine tones, 0.35 s apart. Each is
// a decaying amplitude envelope (`Line` with `doneAction: .freeNode`)
// times a sine, so it plays ~0.2 s and then frees itself — no manual
// cleanup, no blocking the main thread.
let snippet: String = """
let scale: [Double] = [261.63, 293.66, 329.63, 349.23, 392.0,
440.0, 493.88, 523.25, 587.33, 659.25]
for i in 0..<10 {
let freq: Double = scale[i]
let tone = ClosureSynthesizer {
Line.Control(start: 0.25, end: 0.0, dur: 0.2, doneAction: .freeNode)
* OscSin.Audio(freq: freq)
}
do { try context.engine.schedule(tone, after: Double(i) * 0.35) }
catch { context.reportMessage("schedule failed: \\(error)") }
}
context.reportMessage("scheduled 10 tones")
"""
try session.compileAndRun(snippet)
// The `for` loop and the scheduling calls compiled and ran.
#expect(session.lastSnippetMessage == "scheduled 10 tones")
// Advance ~4.5 s of audio; the scheduled adds fire over time. (Each
// tone's `.freeNode` self-frees via the engine's async auto-free watcher
// in the running app — that background timer doesn't get a turn inside
// this synchronous processBlock loop, so here the tones accumulate; the
// auto-free path itself is covered by `AutoFreeOnDoneTests`.)
let blocks: Int = Int(4.5 * 44_100 / 512)
var peakActive: Int = 0
var producedAudio: Bool = false
for _ in 0..<blocks {
engine.processBlock(inputSamples: [])
peakActive = max(peakActive, engine.synthesizerCount)
if engine.audioBuses[0].contains(where: { $0 != 0 }) { producedAudio = true }
}
#expect(peakActive == 10, "all ten scheduled tones should have entered the tree over time")
#expect(producedAudio, "the scheduled tones should have written audio to the bus")
engine.stop()
}
}
#endif