prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
// OscDutyCycleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - OscDutyCycle Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("OscDutyCycle")
|
||||
struct OscDutyCycleTests {
|
||||
|
||||
@Test("OscDutyCycle.Audio rate is .audio")
|
||||
func testAudioRate() {
|
||||
#expect(OscDutyCycle.Audio(freq: 440.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("OscDutyCycle.Control rate is .control")
|
||||
func testControlRate() {
|
||||
#expect(OscDutyCycle.Control(freq: 440.0).rate == .control)
|
||||
}
|
||||
|
||||
@Test("OscDutyCycle.Audio produces 64 samples")
|
||||
func testAudioOutputCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = OscDutyCycle.Audio(freq: 440.0).calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
@Test("OscDutyCycle.Audio fan-out returns identical first channel")
|
||||
func testAudioFanOut() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: OscDutyCycle.Audio = OscDutyCycle.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]) }
|
||||
}
|
||||
|
||||
// MARK: dutyCycle=0 → all -1 (no high time)
|
||||
|
||||
@Test("OscDutyCycle dutyCycle=0 produces all -1")
|
||||
func testDutyCycleZeroAllLow() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// dutyCycle=0 → phase is never < duty, so raw = -1 every sample
|
||||
// BLEP rising at phase=0 wrap: polyBLEP adds small amount; but at low freq all corrections are tiny
|
||||
let osc: OscDutyCycle.Audio = OscDutyCycle.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] <= 0.0 + 1e-6,
|
||||
"dutyCycle=0 sample \(i) = \(out[0][i]), expected <= 0")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: dutyCycle=1 → all +1 (always high)
|
||||
|
||||
@Test("OscDutyCycle dutyCycle=1 produces all +1")
|
||||
func testDutyCycleOneAllHigh() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// dutyCycle=1 → phase is always < 1.0 = duty, so raw = +1 every sample
|
||||
let osc: OscDutyCycle.Audio = OscDutyCycle.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] >= 0.0 - 1e-6,
|
||||
"dutyCycle=1 sample \(i) = \(out[0][i]), expected >= 0")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: dutyCycle=0.5 matches OscSquare
|
||||
|
||||
@Test("OscDutyCycle dutyCycle=0.5 matches OscSquare output")
|
||||
func testDutyCycleHalfMatchesSquare() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let freq: Double = 440.0
|
||||
let square: OscSquare.Audio = OscSquare.Audio(freq: freq)
|
||||
let dutyCycle: OscDutyCycle.Audio = OscDutyCycle.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(abs(sqOut[0][i] - dcOut[0][i]) < 1e-10,
|
||||
"sample \(i): square=\(sqOut[0][i]) dutyCycle=\(dcOut[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: mul/add
|
||||
|
||||
@Test("OscDutyCycle.Audio mul:2 add:1 scales output")
|
||||
func testAudioMulAdd() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let oscBase: OscDutyCycle.Audio = OscDutyCycle.Audio(freq: 100.0, dutyCycle: 0.3)
|
||||
let oscMulAdd: OscDutyCycle.Audio = OscDutyCycle.Audio(freq: 100.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: Output bounded
|
||||
|
||||
@Test("OscDutyCycle.Audio output stays bounded")
|
||||
func testAudioOutputRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: OscDutyCycle.Audio = OscDutyCycle.Audio(freq: 440.0, dutyCycle: 0.3)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Control bypass
|
||||
|
||||
@Test("OscDutyCycle.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: OscDutyCycle.Control = OscDutyCycle.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 parameter
|
||||
|
||||
@Test("OscDutyCycle.Audio phase=0.5 starts mid-cycle")
|
||||
func testAudioPhaseOffset() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.5, duty=0.5 → raw pulse = -1, but BLEP correction fires at this
|
||||
// boundary (phaseFalling = phase - duty = 0.0 triggers the correction).
|
||||
// Output is near 0.0 (BLEP-corrected transition sample). Bounded in [-1, 1].
|
||||
let osc: OscDutyCycle.Audio = OscDutyCycle.Audio(freq: 1.0, dutyCycle: 0.5, phase: 0.5)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] >= -1.0)
|
||||
#expect(out[0][0] <= 1.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user