// ScratchModelRenderTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) import Testing import Foundation import AudioToolbox import LyrebirdSynth @testable import LyrebirdLiveCompile @testable import LyrebirdLiveCompileUI @Suite("LyrebirdScratchModel render-to-file") @MainActor struct ScratchModelRenderTests { private func makeModel(text: String) -> LyrebirdScratchModel { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) return LyrebirdScratchModel(session: LiveSession(engine: engine, attachScope: false), initialText: text) } @Test func rendersWholeBufferToCAF() async throws { let model: LyrebirdScratchModel = makeModel(text: "context.play { OscSin.Audio(freq: 440) * 0.1 }") let url: URL = FileManager.default.temporaryDirectory .appendingPathComponent("LBRenderTest-\(UUID().uuidString).caf") defer { try? FileManager.default.removeItem(at: url) } let duration: Double = 0.25 let ok: Bool = await model.renderToFile(url: url, duration: duration) #expect(ok) #expect(FileManager.default.fileExists(atPath: url.path)) #expect(model.consoleEntries.contains { $0.kind == .info }) // The CAF should hold roughly `duration` seconds at the engine's rate. var fileRef: ExtAudioFileRef? #expect(ExtAudioFileOpenURL(url as CFURL, &fileRef) == noErr) guard let fileRef: ExtAudioFileRef = fileRef else { Issue.record("could not open rendered CAF") return } defer { ExtAudioFileDispose(fileRef) } var frames: Int64 = 0 var size: UInt32 = UInt32(MemoryLayout.size) #expect(ExtAudioFileGetProperty(fileRef, kExtAudioFileProperty_FileLengthFrames, &size, &frames) == noErr) // Allow a block of slack either way. let expected: Double = duration * 48_000 #expect(Double(frames) > expected - 128 && Double(frames) < expected + 512) } @Test func rendersSelectionWhenPresent() async { // Whole buffer has two statements; select only the first and render it. let model: LyrebirdScratchModel = makeModel( text: "context.play { OscSin.Audio(freq: 440) * 0.1 }\ncontext.play { OscSin.Audio(freq: 880) * 0.1 }") model.updateSelection("context.play { OscSin.Audio(freq: 440) * 0.1 }") #expect(model.hasSelection) let url: URL = FileManager.default.temporaryDirectory .appendingPathComponent("LBRenderTest-\(UUID().uuidString).caf") defer { try? FileManager.default.removeItem(at: url) } let ok: Bool = await model.renderToFile(url: url, duration: 0.1) #expect(ok) #expect(FileManager.default.fileExists(atPath: url.path)) } @Test func nonPositiveDurationFails() async { let model: LyrebirdScratchModel = makeModel(text: "context.play { OscSin.Audio(freq: 440) * 0.1 }") let url: URL = FileManager.default.temporaryDirectory .appendingPathComponent("LBRenderTest-\(UUID().uuidString).caf") defer { try? FileManager.default.removeItem(at: url) } let ok: Bool = await model.renderToFile(url: url, duration: 0) #expect(!ok) #expect(!FileManager.default.fileExists(atPath: url.path)) #expect(model.consoleEntries.contains { $0.kind == .error }) } @Test func blankBufferRendersNothing() async { let model: LyrebirdScratchModel = makeModel(text: " \n") let url: URL = FileManager.default.temporaryDirectory .appendingPathComponent("LBRenderTest-\(UUID().uuidString).caf") defer { try? FileManager.default.removeItem(at: url) } let ok: Bool = await model.renderToFile(url: url, duration: 0.1) #expect(!ok) #expect(!FileManager.default.fileExists(atPath: url.path)) } /// The `format:` argument reaches the NRT driver: rendering to a WAV Int24 target /// and an RF64 target produces files with the requested on-disk encoding. @Test(arguments: [ (AudioFileFormat.wavInt24, "wav", UInt32(24), false), (AudioFileFormat.rf64Float32, "wav", UInt32(32), true), ]) func rendersHonourFormat(format: AudioFileFormat, ext: String, bits: UInt32, isFloat: Bool) async throws { let model: LyrebirdScratchModel = makeModel(text: "context.play { OscSin.Audio(freq: 440) * 0.1 }") let url: URL = FileManager.default.temporaryDirectory .appendingPathComponent("LBRenderTest-\(UUID().uuidString)").appendingPathExtension(ext) defer { try? FileManager.default.removeItem(at: url) } let ok: Bool = await model.renderToFile(url: url, format: format, duration: 0.1) #expect(ok) #expect(FileManager.default.fileExists(atPath: url.path)) var fileRef: ExtAudioFileRef? #expect(ExtAudioFileOpenURL(url as CFURL, &fileRef) == noErr) let ref: ExtAudioFileRef = try #require(fileRef) defer { ExtAudioFileDispose(ref) } var asbd: AudioStreamBasicDescription = AudioStreamBasicDescription() var size: UInt32 = UInt32(MemoryLayout.size) #expect(ExtAudioFileGetProperty(ref, kExtAudioFileProperty_FileDataFormat, &size, &asbd) == noErr) #expect(asbd.mBitsPerChannel == bits) #expect((asbd.mFormatFlags & kAudioFormatFlagIsFloat != 0) == isFloat) } } #endif