1.0.0 release
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
// LFDutyCycleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFDutyCycle Tests
|
||||
|
||||
@Suite("LFDutyCycle")
|
||||
struct LFDutyCycleTests {
|
||||
|
||||
@Test("LFDutyCycle.Audio rate is .audio")
|
||||
func testAudioRate() {
|
||||
#expect(LFDutyCycle.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle.Control rate is .control")
|
||||
func testControlRate() {
|
||||
#expect(LFDutyCycle.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle.Audio produces 64 samples")
|
||||
func testAudioOutputCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = LFDutyCycle.Audio(freq: 1.0).calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle.Audio fan-out returns identical first channel")
|
||||
func testAudioFanOut() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.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]) }
|
||||
}
|
||||
|
||||
// MARK: dutyCycle extreme values
|
||||
|
||||
@Test("LFDutyCycle.Audio dutyCycle=0.0 produces all -1")
|
||||
func testDutyCycleZeroAllLow() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// dutyCycle=0 → phase never < 0 → always -1
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 1.0, dutyCycle: 0.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] == -1.0,
|
||||
"dutyCycle=0 sample \(i) = \(out[0][i]), expected -1")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle.Audio dutyCycle=1.0 produces all +1")
|
||||
func testDutyCycleOneAllHigh() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// dutyCycle=1 → phase always < 1.0 → always +1
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 1.0, dutyCycle: 1.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] == 1.0,
|
||||
"dutyCycle=1 sample \(i) = \(out[0][i]), expected +1")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: dutyCycle=0.5 matches LFSquare
|
||||
|
||||
@Test("LFDutyCycle.Audio dutyCycle=0.5 matches LFSquare output")
|
||||
func testDutyCycleHalfMatchesLFSquare() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let freq: Double = 5.0
|
||||
let square: LFSquare.Audio = LFSquare.Audio(freq: freq)
|
||||
let dutyCycle: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: freq, dutyCycle: 0.5)
|
||||
let sqOut: [[Double]] = square.calculatedSamples(graph: graph, count: 512)
|
||||
let dcOut: [[Double]] = dutyCycle.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(sqOut[0][i] == dcOut[0][i],
|
||||
"sample \(i): square=\(sqOut[0][i]) dutyCycle=\(dcOut[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Output only ±1 (no analog ramps)
|
||||
|
||||
@Test("LFDutyCycle.Audio output is only +1 or -1")
|
||||
func testAudioOutputRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 10.0, dutyCycle: 0.3)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] == 1.0 || out[0][i] == -1.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Audio-rate dutyCycle generator input
|
||||
|
||||
@Test("LFDutyCycle.Audio audio-rate dutyCycle generator input does not crash")
|
||||
func testAudioRateDutyCycleGeneratorInput() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// Use a sine wave as the duty cycle input — values move through [-1,1] but get clamped to [0,1]
|
||||
let dutyGen: OscSin.Audio = OscSin.Audio(freq: 2.0, mul: 0.5, add: 0.5)
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 5.0, dutyCycle: dutyGen)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
// Verify output is bounded in [-1, 1]
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0)
|
||||
#expect(out[0][i] <= 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: GeneratorEnum
|
||||
|
||||
@Test("LFDutyCycle GeneratorEnum AudioType rate is .audio")
|
||||
func testGeneratorEnumAudio() {
|
||||
#expect(LFDutyCycle.AudioType.self == LFDutyCycle.Audio.self)
|
||||
#expect(LFDutyCycle.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle GeneratorEnum ControlType rate is .control")
|
||||
func testGeneratorEnumControl() {
|
||||
#expect(LFDutyCycle.ControlType.self == LFDutyCycle.Control.self)
|
||||
#expect(LFDutyCycle.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
// MARK: mul/add
|
||||
|
||||
@Test("LFDutyCycle.Audio mul:2 add:1 scales output")
|
||||
func testAudioMulAdd() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let oscBase: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 5.0, dutyCycle: 0.3)
|
||||
let oscMulAdd: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 5.0, dutyCycle: 0.3, 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)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Control cache-hit bypass
|
||||
|
||||
@Test("LFDutyCycle.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: LFDutyCycle.Control = LFDutyCycle.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: Phase offset
|
||||
|
||||
@Test("LFDutyCycle.Audio phase=0.7 dutyCycle=0.5 first sample is -1")
|
||||
func testAudioPhaseOffsetBeyondDuty() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.7, duty=0.5 → 0.7 >= 0.5 → -1
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 1.0, dutyCycle: 0.5, phase: 0.7)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] == -1.0)
|
||||
}
|
||||
|
||||
@Test("LFDutyCycle.Audio phase=0.2 dutyCycle=0.5 first sample is +1")
|
||||
func testAudioPhaseOffsetWithinDuty() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.2, duty=0.5 → 0.2 < 0.5 → +1
|
||||
let osc: LFDutyCycle.Audio = LFDutyCycle.Audio(freq: 1.0, dutyCycle: 0.5, phase: 0.2)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] == 1.0)
|
||||
}
|
||||
|
||||
// MARK: State advances across blocks
|
||||
|
||||
@Test("LFDutyCycle.Control state advances across blocks")
|
||||
func testControlStateAdvances() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
// Use a frequency whose phase crosses the duty boundary between blocks.
|
||||
// At 44100 Hz, 512 samples/block: dt_per_block = 100/44100*512 ≈ 1.16.
|
||||
// Phase wraps multiple times per block; collect two blocks and compare phase.
|
||||
let osc: LFDutyCycle.Control = LFDutyCycle.Control(freq: 100.0)
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = osc.calculatedSamples(graph: g1, count: 512)
|
||||
let phaseAfterBlock1: Double = osc._phase
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = osc.calculatedSamples(graph: g2, count: 512)
|
||||
let phaseAfterBlock2: Double = osc._phase
|
||||
// Phase must have changed (it wraps, so we compare the raw accumulated phase)
|
||||
#expect(phaseAfterBlock1 != phaseAfterBlock2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user