Files
Lyrebird/Tests/LyrebirdTests/ScopeTests.swift
T

104 lines
4.1 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.
// ScopeTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("ScopeCapture")
final class ScopeTests {
// MARK: - Unit tests (no engine)
@Test func snapshotInitiallyZero() {
let scope: ScopeCapture = ScopeCapture(buses: [0, 1], bufferSize: 64)
let snap: [[Float]] = scope.snapshot()
#expect(snap.count == 2)
#expect(snap[0].allSatisfy { $0 == 0 })
#expect(snap[1].allSatisfy { $0 == 0 })
}
@Test func snapshotBufferSizeMatchesInit() {
let scope: ScopeCapture = ScopeCapture(buses: [0, 1], bufferSize: 128)
let snap: [[Float]] = scope.snapshot()
#expect(snap[0].count == 128)
#expect(snap[1].count == 128)
}
@Test func writeAndSnapshotRoundTrip() {
let scope: ScopeCapture = ScopeCapture(buses: [0, 1], bufferSize: 16)
// Fill exactly one buffer with known values
let busA: [Float] = [Float](repeating: 0.5, count: 16)
let busB: [Float] = [Float](repeating: -0.5, count: 16)
scope.write(audioBuses: [busA, busB], count: 16)
// After one full buffer, front should contain the written values
let snap: [[Float]] = scope.snapshot()
#expect(snap[0].allSatisfy { abs($0 - 0.5) < 1e-6 })
#expect(snap[1].allSatisfy { abs($0 + 0.5) < 1e-6 })
}
@Test func partialWriteNoFlipYet() {
let scope: ScopeCapture = ScopeCapture(buses: [0], bufferSize: 64)
let bus: [Float] = [Float](repeating: 1.0, count: 32)
scope.write(audioBuses: [bus], count: 32)
// Buffer not full yet — snapshot still shows the initially-zero front
let snap: [[Float]] = scope.snapshot()
#expect(snap[0].allSatisfy { $0 == 0 })
}
@Test func multipleBlocksAccumulate() {
let scope: ScopeCapture = ScopeCapture(buses: [0], bufferSize: 64)
let block: [Float] = [Float](repeating: 1.0, count: 16)
// 4 × 16 = 64 fills the back buffer → flip
for _ in 0..<4 { scope.write(audioBuses: [block], count: 16) }
let snap: [[Float]] = scope.snapshot()
#expect(snap[0].allSatisfy { abs($0 - 1.0) < 1e-6 })
}
@Test func wraparoundProducesValidSnapshot() {
let scope: ScopeCapture = ScopeCapture(buses: [0], bufferSize: 32)
let ones: [Float] = [Float](repeating: 1.0, count: 20)
// First write partially fills back (20/32)
scope.write(audioBuses: [ones], count: 20)
let twos: [Float] = [Float](repeating: 2.0, count: 20)
// Second write: 12 samples fill back → flip, then 8 samples go into new back
scope.write(audioBuses: [twos], count: 20)
// Snapshot is the just-flipped front: first 20 samples are 1.0, last 12 are 2.0
let snap: [[Float]] = scope.snapshot()
#expect(snap[0].count == 32)
#expect(snap[0].prefix(20).allSatisfy { abs($0 - 1.0) < 1e-6 })
#expect(snap[0].suffix(12).allSatisfy { abs($0 - 2.0) < 1e-6 })
}
// MARK: - Engine integration
@Test func engineScopeTapReceivesData() throws {
let blockSize: Int = 64
let scope: ScopeCapture = ScopeCapture(buses: [0, 1], bufferSize: blockSize)
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
engine.scopeTap = scope
try engine.start()
defer { engine.stop() }
// Add a sine tone so buses 0+1 get signal
final class TestSynth: SynthesizerBase {
override func build() {
output(bus: 0, source: Pan2(input: OscSin.Audio(freq: 440.0) * 0.5))
}
}
let synth: TestSynth = TestSynth()
try engine.add(synth)
// Run exactly bufferSize samples so the scope flips at least once
engine.processBlock(inputSamples: [])
let snap: [[Float]] = scope.snapshot()
#expect(snap.count == 2)
#expect(snap[0].count == blockSize)
// At least some non-zero samples in bus 0 after a sine wave block
#expect(snap[0].contains { $0 != 0 })
}
}