192 lines
7.6 KiB
Swift
192 lines
7.6 KiB
Swift
// OscSawtoothTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - OscSawtooth Tests
|
|
|
|
@Suite("OscSawtooth")
|
|
struct OscSawtoothTests {
|
|
|
|
// MARK: Rate
|
|
|
|
@Test("OscSawtooth.Audio rate is .audio")
|
|
func testAudioRate() {
|
|
let osc: OscSawtooth.Audio = OscSawtooth.Audio(freq: 440.0)
|
|
#expect(osc.rate == .audio)
|
|
}
|
|
|
|
@Test("OscSawtooth.Control rate is .control")
|
|
func testControlRate() {
|
|
let osc: OscSawtooth.Control = OscSawtooth.Control(freq: 440.0)
|
|
#expect(osc.rate == .control)
|
|
}
|
|
|
|
// MARK: Output count
|
|
|
|
@Test("OscSawtooth.Audio produces 64 samples")
|
|
func testAudioOutputCount() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Audio = OscSawtooth.Audio(freq: 440.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
#expect(Array(out[0].prefix(64)).count == 64)
|
|
}
|
|
|
|
@Test("OscSawtooth.Control produces 64 samples")
|
|
func testControlOutputCount() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Control = OscSawtooth.Control(freq: 440.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
#expect(Array(out[0].prefix(64)).count == 64)
|
|
}
|
|
|
|
// MARK: Fan-out
|
|
|
|
@Test("OscSawtooth.Audio fan-out returns identical first channel")
|
|
func testAudioFanOut() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Audio = OscSawtooth.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("OscSawtooth.Control fan-out returns identical first channel")
|
|
func testControlFanOut() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Control = OscSawtooth.Control(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])
|
|
}
|
|
}
|
|
|
|
// MARK: State advances across blocks
|
|
|
|
@Test("OscSawtooth.Audio state advances across blocks")
|
|
func testAudioStateAdvances() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Audio = OscSawtooth.Audio(freq: 440.0)
|
|
|
|
let block1: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
let block2: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
|
|
let identical: Bool = zip(block1[0].prefix(512), block2[0].prefix(512)).allSatisfy { abs($0 - $1) < 1e-12 }
|
|
#expect(!identical)
|
|
}
|
|
|
|
@Test("OscSawtooth.Control state advances across blocks")
|
|
func testControlStateAdvances() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let osc: OscSawtooth.Control = OscSawtooth.Control(freq: 1.0)
|
|
|
|
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
|
let v1: [[Double]] = osc.calculatedSamples(graph: g1, count: 512)
|
|
advanceBlock(engine)
|
|
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
|
let v2: [[Double]] = osc.calculatedSamples(graph: g2, count: 512)
|
|
|
|
#expect(v1[0][511] != v2[0][511])
|
|
}
|
|
|
|
// MARK: Phase offset
|
|
|
|
@Test("OscSawtooth.Audio phase=0.5 starts at mid-cycle")
|
|
func testAudioPhaseOffset() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
// phase=0.5 → first sample raw = 2*0.5-1 = 0.0 (before BLEP correction)
|
|
let osc: OscSawtooth.Audio = OscSawtooth.Audio(freq: 1.0, phase: 0.5)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
|
// Raw value near 0, BLEP correction is tiny at freq=1 Hz
|
|
#expect(abs(out[0][0]) < 0.1)
|
|
}
|
|
|
|
// MARK: Output range
|
|
|
|
@Test("OscSawtooth.Audio output stays near [-1, 1]")
|
|
func testAudioOutputRange() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Audio = OscSawtooth.Audio(freq: 440.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
for i: Int in 0..<512 {
|
|
// polyBLEP can push slightly outside [-1,1] transiently — allow a margin
|
|
#expect(out[0][i] >= -2.0)
|
|
#expect(out[0][i] <= 2.0)
|
|
}
|
|
}
|
|
|
|
// MARK: mul/add
|
|
|
|
@Test("OscSawtooth.Audio mul:2 add:1 scales output")
|
|
func testAudioMulAdd() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let oscBase: OscSawtooth.Audio = OscSawtooth.Audio(freq: 100.0)
|
|
let oscMulAdd: OscSawtooth.Audio = OscSawtooth.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,
|
|
"sample \(i): expected \(expected), got \(scaled[0][i])")
|
|
}
|
|
}
|
|
|
|
@Test("OscSawtooth.Control mul:2 add:1 scales output")
|
|
func testControlMulAdd() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
|
|
let oscBase: OscSawtooth.Control = OscSawtooth.Control(freq: 1.0)
|
|
let oscMulAdd: OscSawtooth.Control = OscSawtooth.Control(freq: 1.0, mul: 2.0, add: 1.0)
|
|
|
|
let base: [Double] = Array(oscBase.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
let scaled: [Double] = Array(oscMulAdd.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
|
|
|
// Both start from 0 ramp, verify final sample relationship
|
|
let baseLast: Double = base[63]
|
|
let scaledLast: Double = scaled[63]
|
|
#expect(abs(scaledLast - (baseLast * 2.0 + 1.0)) < 1e-6)
|
|
}
|
|
|
|
// MARK: Control bypass — second call in same segment is cache hit
|
|
|
|
@Test("OscSawtooth.Control second call in same segment is cache hit (state advances once)")
|
|
func testControlBypassCacheHit() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: OscSawtooth.Control = OscSawtooth.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)
|
|
let phaseAfterSecond: Double = osc._phase
|
|
|
|
// Phase must not have advanced a second time
|
|
#expect(phaseAfterFirst == phaseAfterSecond)
|
|
// Both results are identical (cache hit)
|
|
for i: Int in 0..<512 {
|
|
#expect(out1[0][i] == out2[0][i])
|
|
}
|
|
}
|
|
}
|