// SnippetScheduledLoopTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) 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..