103 lines
4.3 KiB
Swift
103 lines
4.3 KiB
Swift
// OscSquareTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - OscSquare Tests
|
|
|
|
@Suite("OscSquare")
|
|
struct OscSquareTests {
|
|
|
|
@Test("OscSquare.Audio rate is .audio")
|
|
func testAudioRate() {
|
|
#expect(OscSquare.Audio(freq: 440.0).rate == .audio)
|
|
}
|
|
|
|
@Test("OscSquare.Control rate is .control")
|
|
func testControlRate() {
|
|
#expect(OscSquare.Control(freq: 440.0).rate == .control)
|
|
}
|
|
|
|
@Test("OscSquare.Audio produces 64 samples")
|
|
func testAudioOutputCount() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let out: [[Double]] = OscSquare.Audio(freq: 440.0).calculatedSamples(graph: graph, count: 64)
|
|
#expect(Array(out[0].prefix(64)).count == 64)
|
|
}
|
|
|
|
@Test("OscSquare.Audio fan-out returns identical first channel")
|
|
func testAudioFanOut() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSquare.Audio = OscSquare.Audio(freq: 220.0)
|
|
let out1: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
let out2: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
for i: Int in 0..<64 { #expect(out1[0][i] == out2[0][i]) }
|
|
}
|
|
|
|
@Test("OscSquare.Audio output is non-zero for audible frequency")
|
|
func testAudioNonZero() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSquare.Audio = OscSquare.Audio(freq: 440.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
let hasNonZero: Bool = out[0].prefix(512).contains { abs($0) > 0.5 }
|
|
#expect(hasNonZero)
|
|
}
|
|
|
|
@Test("OscSquare.Audio output stays bounded")
|
|
func testAudioOutputRange() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSquare.Audio = OscSquare.Audio(freq: 440.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
for i: Int in 0..<512 {
|
|
#expect(out[0][i] >= -2.0)
|
|
#expect(out[0][i] <= 2.0)
|
|
}
|
|
}
|
|
|
|
@Test("OscSquare.Audio phase=0 produces high output mid-cycle")
|
|
func testAudioPhaseZeroIsHigh() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSquare.Audio = OscSquare.Audio(freq: 1.0, phase: 0.0)
|
|
// Sample 0 is exactly at the rising discontinuity — polyBLEP correction
|
|
// makes it 0. Sample 1 onward is well into the high half: raw=+1, BLEP≈0.
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 2)
|
|
#expect(out[0][1] > 0.5)
|
|
}
|
|
|
|
@Test("OscSquare.Audio mul:2 add:1 scales output")
|
|
func testAudioMulAdd() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let oscBase: OscSquare.Audio = OscSquare.Audio(freq: 100.0)
|
|
let oscMulAdd: OscSquare.Audio = OscSquare.Audio(freq: 100.0, mul: 2.0, add: 1.0)
|
|
let base: [[Double]] = oscBase.calculatedSamples(graph: graph, count: 64)
|
|
let scaled: [[Double]] = oscMulAdd.calculatedSamples(graph: graph, count: 64)
|
|
for i: Int in 0..<64 {
|
|
let expected: Double = base[0][i] * 2.0 + 1.0
|
|
#expect(abs(scaled[0][i] - expected) < 1e-10)
|
|
}
|
|
}
|
|
|
|
@Test("OscSquare.Control second call in same segment is cache hit")
|
|
func testControlBypassCacheHit() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSquare.Control = OscSquare.Control(freq: 100.0)
|
|
let out1: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
let phaseAfterFirst: Double = osc._phase
|
|
let out2: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
#expect(osc._phase == phaseAfterFirst)
|
|
for i: Int in 0..<512 { #expect(out1[0][i] == out2[0][i]) }
|
|
}
|
|
|
|
}
|