192 lines
7.8 KiB
Swift
192 lines
7.8 KiB
Swift
// LFSawtoothTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - LFSawtooth Tests
|
|
|
|
@Suite("LFSawtooth")
|
|
struct LFSawtoothTests {
|
|
|
|
// MARK: Rate
|
|
|
|
@Test("LFSawtooth.Audio rate is .audio")
|
|
func testAudioRate() {
|
|
#expect(LFSawtooth.Audio(freq: 1.0).rate == .audio)
|
|
}
|
|
|
|
@Test("LFSawtooth.Control rate is .control")
|
|
func testControlRate() {
|
|
#expect(LFSawtooth.Control(freq: 1.0).rate == .control)
|
|
}
|
|
|
|
// MARK: Output count
|
|
|
|
@Test("LFSawtooth.Audio produces 64 samples")
|
|
func testAudioOutputCount() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 1.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
#expect(Array(out[0].prefix(64)).count == 64)
|
|
}
|
|
|
|
@Test("LFSawtooth.Control produces 64 samples")
|
|
func testControlOutputCount() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Control = LFSawtooth.Control(freq: 1.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
|
#expect(Array(out[0].prefix(64)).count == 64)
|
|
}
|
|
|
|
// MARK: Fan-out
|
|
|
|
@Test("LFSawtooth.Audio fan-out returns identical first channel")
|
|
func testAudioFanOut() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 1.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("LFSawtooth.Control fan-out returns identical first channel")
|
|
func testControlFanOut() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Control = LFSawtooth.Control(freq: 1.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: Output range
|
|
|
|
@Test("LFSawtooth.Audio output stays in [-1, 1]")
|
|
func testAudioOutputRange() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 10.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
|
for i: Int in 0..<512 {
|
|
#expect(out[0][i] >= -1.0)
|
|
#expect(out[0][i] <= 1.0)
|
|
}
|
|
}
|
|
|
|
// MARK: Waveform shape — specific phase values
|
|
|
|
@Test("LFSawtooth.Audio phase=0 starts near -1")
|
|
func testAudioPhaseZeroStartsNearMinusOne() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
// phase=0 → raw = 2*0 - 1 = -1.0
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 1.0, phase: 0.0)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
|
#expect(abs(out[0][0] - (-1.0)) < 1e-9)
|
|
}
|
|
|
|
@Test("LFSawtooth.Audio phase=0.5 produces near 0")
|
|
func testAudioPhaseHalfNearZero() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
// phase=0.5 → raw = 2*0.5 - 1 = 0.0
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 1.0, phase: 0.5)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
|
#expect(abs(out[0][0]) < 1e-9)
|
|
}
|
|
|
|
@Test("LFSawtooth.Audio phase=0.25 produces near -0.5")
|
|
func testAudioPhaseQuarterNearMinusHalf() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
// phase=0.25 → raw = 2*0.25 - 1 = -0.5
|
|
let osc: LFSawtooth.Audio = LFSawtooth.Audio(freq: 1.0, phase: 0.25)
|
|
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
|
#expect(abs(out[0][0] - (-0.5)) < 1e-9)
|
|
}
|
|
|
|
// MARK: GeneratorEnum
|
|
|
|
@Test("LFSawtooth GeneratorEnum AudioType rate is .audio")
|
|
func testGeneratorEnumAudio() {
|
|
#expect(LFSawtooth.AudioType.self == LFSawtooth.Audio.self)
|
|
#expect(LFSawtooth.Audio(freq: 1.0).rate == .audio)
|
|
}
|
|
|
|
@Test("LFSawtooth GeneratorEnum ControlType rate is .control")
|
|
func testGeneratorEnumControl() {
|
|
#expect(LFSawtooth.ControlType.self == LFSawtooth.Control.self)
|
|
#expect(LFSawtooth.Control(freq: 1.0).rate == .control)
|
|
}
|
|
|
|
// MARK: mul/add
|
|
|
|
@Test("LFSawtooth.Audio mul:2 add:1 scales output")
|
|
func testAudioMulAdd() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let oscBase: LFSawtooth.Audio = LFSawtooth.Audio(freq: 5.0)
|
|
let oscMulAdd: LFSawtooth.Audio = LFSawtooth.Audio(freq: 5.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("LFSawtooth.Control mul:2 add:1 scales output")
|
|
func testControlMulAdd() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let oscBase: LFSawtooth.Control = LFSawtooth.Control(freq: 1.0)
|
|
let oscMulAdd: LFSawtooth.Control = LFSawtooth.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))
|
|
let baseLast: Double = base[63]
|
|
let scaledLast: Double = scaled[63]
|
|
#expect(abs(scaledLast - (baseLast * 2.0 + 1.0)) < 1e-6)
|
|
}
|
|
|
|
// MARK: Control cache-hit bypass
|
|
|
|
@Test("LFSawtooth.Control second call in same segment is cache hit")
|
|
func testControlCacheHit() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let osc: LFSawtooth.Control = LFSawtooth.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])
|
|
}
|
|
}
|
|
|
|
// MARK: State advances across blocks
|
|
|
|
@Test("LFSawtooth.Control state advances across blocks")
|
|
func testControlStateAdvances() {
|
|
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
|
let osc: LFSawtooth.Control = LFSawtooth.Control(freq: 5.0)
|
|
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
|
let v1: [Double] = Array(osc.calculatedSamples(graph: g1, count: 512)[0].prefix(512))
|
|
advanceBlock(engine)
|
|
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
|
let v2: [Double] = Array(osc.calculatedSamples(graph: g2, count: 512)[0].prefix(512))
|
|
#expect(v1[511] != v2[511])
|
|
}
|
|
}
|