// RecordBufferTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("RecordBuffer") struct RecordBufferTests { // MARK: - Helpers func makeEngine(blockSize: Int = 16) throws -> SynthesisEngine { let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) try engine.start() return engine } // MARK: - Basic recording @Test func recordsMonoInputIntoBuffer() throws { let engine: SynthesisEngine = try makeEngine() let graph: SynthGraph = makeSynthGraph(engine: engine) let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 0.5, buffer: buf, loop: false) _ = rec.calculatedSamples(graph: graph, count: 16) for i: Int in 0..<16 { #expect(buf[i, 0] == 0.5, "frame \(i): expected 0.5, got \(buf[i, 0])") } engine.stop() } @Test func rateIsAudio() { let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf) #expect(rec.rate == .audio) } // MARK: - preLevel mix @Test func preLevelMixesWithExistingContent() throws { // ParameterNode ramps over one block. Advance one block to let preLevel settle at 0.5, // then verify the second block's writes use the flat value. let engine: SynthesisEngine = try makeEngine(blockSize: 8) let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16) buf.fill(with: 1.0) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf, loop: true) rec.preLevel = 0.5 // mix: new*1 + existing*0.5 = 1.0 + 0.5 = 1.5 // Block 1 (frames 0–7): preLevel ramps 0→0.5; mixed values vary. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } advanceBlock(engine) // Block 2 (frames 8–15): preLevel flat at 0.5; every frame = 1.0 + 0.5*1.0 = 1.5. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } for i: Int in 8..<16 { #expect(abs(buf[i, 0] - 1.5) < 1e-9, "frame \(i): expected 1.5, got \(buf[i, 0])") } engine.stop() } @Test func recLevelScalesNewSignal() throws { // ParameterNode ramps over one block. Use a two-block test: block 1 lets recLevel // settle at 0.25; block 2 records with a flat 0.25 scale. let engine: SynthesisEngine = try makeEngine(blockSize: 8) let buf: LyrebirdBuffer = LyrebirdBuffer(count: 16) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf, loop: true) rec.recLevel = 0.25 // Block 1 (frames 0–7): recLevel ramps 1.0→0.25; values vary. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } advanceBlock(engine) // Block 2 (frames 8–15): recLevel flat at 0.25. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } for i: Int in 8..<16 { #expect(abs(buf[i, 0] - 0.25) < 1e-9, "frame \(i): expected 0.25, got \(buf[i, 0])") } engine.stop() } // MARK: - run gate @Test func runZeroPausesRecording() throws { // ParameterNode ramps over one block. Advance one block to let run settle at 0.0, // then verify the second block writes nothing. let engine: SynthesisEngine = try makeEngine(blockSize: 8) let buf: LyrebirdBuffer = LyrebirdBuffer(count: 32) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf) rec.run = 0.0 // Block 1 (frames 0–7): run ramps 1.0→0.0; some frames may be written. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } advanceBlock(engine) // Block 2 (frames 8–15): run is flat 0.0; no frames written. do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } for i: Int in 8..<16 { #expect(buf[i, 0] == 0.0, "frame \(i) should be unwritten (run=0)") } engine.stop() } @Test func runZeroHoldsWritePosition() throws { // Advance one block to let run ramp to 0.0, then record and verify write head stays put. let engine: SynthesisEngine = try makeEngine(blockSize: 8) let buf: LyrebirdBuffer = LyrebirdBuffer(count: 32) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf) rec.run = 0.0 // Block 1: run ramps down (some writes may occur). do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } advanceBlock(engine) // Snapshot write position after block 1 completes. // Block 2: run = 0.0 flat — write head should not advance. let bufSnapshot: [Double] = (0..<32).map { buf[$0, 0] } do { let graph: SynthGraph = makeSynthGraph(engine: engine) _ = rec.calculatedSamples(graph: graph, count: 8) } // Frames that should be zero in block 1 must remain zero in block 2. for i: Int in 8..<16 { #expect(buf[i, 0] == 0.0, "frame \(i) should still be unwritten; snapshot was \(bufSnapshot[i])") } engine.stop() } // MARK: - Loop behaviour @Test func loopWrapsWritePosition() throws { let engine: SynthesisEngine = try makeEngine(blockSize: 16) let graph: SynthGraph = makeSynthGraph(engine: engine) let bufferFrames: Int = 8 let buf: LyrebirdBuffer = LyrebirdBuffer(count: bufferFrames) let rec: RecordBuffer.Audio = RecordBuffer.Audio(input: 1.0, buffer: buf, loop: true) // One block of 16 samples → writes 16 frames into an 8-frame buffer (wraps once) _ = rec.calculatedSamples(graph: graph, count: 16) for i: Int in 0.. [[Double]] { _checkCount(count) return [[Double](repeating: value, count: count)] } func floatValue(graph: SynthGraph) -> Double { value } }