prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
// KlangTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - Local DFT helper
|
||||
|
||||
private func dftMag(samples: [Double], freq: Double, sampleRate: Double) -> Double {
|
||||
let n: Int = samples.count
|
||||
guard n > 0 else { return 0 }
|
||||
var re: Double = 0.0
|
||||
var im: Double = 0.0
|
||||
for i: Int in 0..<n {
|
||||
let phase: Double = 2.0 * Double.pi * freq * Double(i) / sampleRate
|
||||
re += samples[i] * cos(phase)
|
||||
im -= samples[i] * sin(phase)
|
||||
}
|
||||
return sqrt(re * re + im * im) / Double(n)
|
||||
}
|
||||
|
||||
/// Build a `[freq, amp, phase]` spec buffer (Klang channel order).
|
||||
private func makeSpec(_ triplets: [(Double, Double, Double)]) -> LyrebirdBuffer {
|
||||
let buf: LyrebirdBuffer = LyrebirdBuffer(count: triplets.count, channelCount: klangSpecChannelCount)
|
||||
for (i, t) in triplets.enumerated() {
|
||||
buf.setValue(t.0, frame: i, channel: klangFreqChannel)
|
||||
buf.setValue(t.1, frame: i, channel: klangAmpChannel)
|
||||
buf.setValue(t.2, frame: i, channel: klangPhaseChannel)
|
||||
}
|
||||
return buf
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Klang")
|
||||
struct KlangTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func dftPeaksAtAllPartials() {
|
||||
let spec: LyrebirdBuffer = makeSpec([
|
||||
(300.0, 1.0, 0.0),
|
||||
(900.0, 0.7, 0.0),
|
||||
])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200)
|
||||
|
||||
let mag300: Double = dftMag(samples: samples, freq: 300, sampleRate: sr)
|
||||
let mag900: Double = dftMag(samples: samples, freq: 900, sampleRate: sr)
|
||||
let mag600: Double = dftMag(samples: samples, freq: 600, sampleRate: sr)
|
||||
#expect(mag300 > mag600)
|
||||
#expect(mag900 > mag600)
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
}
|
||||
|
||||
@Test func ampWeightsPartials() {
|
||||
// Louder partial at 300 should have larger DFT magnitude than the quiet 900.
|
||||
let spec: LyrebirdBuffer = makeSpec([
|
||||
(300.0, 1.0, 0.0),
|
||||
(900.0, 0.1, 0.0),
|
||||
])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200)
|
||||
let mag300: Double = dftMag(samples: samples, freq: 300, sampleRate: sr)
|
||||
let mag900: Double = dftMag(samples: samples, freq: 900, sampleRate: sr)
|
||||
#expect(mag300 > mag900 * 2.0)
|
||||
}
|
||||
|
||||
@Test func freqscaleShiftsPartial() {
|
||||
let spec: LyrebirdBuffer = makeSpec([(300.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec, freqscale: 2.0)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200)
|
||||
let mag300: Double = dftMag(samples: samples, freq: 300, sampleRate: sr)
|
||||
let mag600: Double = dftMag(samples: samples, freq: 600, sampleRate: sr)
|
||||
#expect(mag600 > mag300)
|
||||
}
|
||||
|
||||
@Test func freqoffsetShiftsPartial() {
|
||||
let spec: LyrebirdBuffer = makeSpec([(300.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec, freqoffset: 200.0)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200)
|
||||
let mag300: Double = dftMag(samples: samples, freq: 300, sampleRate: sr)
|
||||
let mag500: Double = dftMag(samples: samples, freq: 500, sampleRate: sr)
|
||||
#expect(mag500 > mag300)
|
||||
}
|
||||
|
||||
@Test func singlePartialApproximatesSine() {
|
||||
// One partial, amp 1 → output amplitude should be near 1.0 RMS·√2.
|
||||
let spec: LyrebirdBuffer = makeSpec([(440.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 100)
|
||||
let peak: Double = samples.map { abs($0) }.max() ?? 0
|
||||
#expect(peak > 0.98)
|
||||
#expect(peak < 1.02)
|
||||
}
|
||||
|
||||
@Test func initialPhaseAffectsFirstSample() {
|
||||
// Quarter-cycle phase offset → first sample near sin(2π·0.25) = 1.0.
|
||||
let spec: LyrebirdBuffer = makeSpec([(100.0, 1.0, 0.25)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec, interpolation: .none)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 1)
|
||||
#expect(abs(samples[0] - 1.0) < 0.05)
|
||||
}
|
||||
|
||||
@Test func setBufferNilSilences() {
|
||||
let spec: LyrebirdBuffer = makeSpec([(300.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec)
|
||||
_ = collectBlocks(voice, engine: engine, blocks: 2)
|
||||
voice.setBuffer(nil, engine: engine)
|
||||
let after: [Double] = collectBlocks(voice, engine: engine, blocks: 10)
|
||||
#expect(rmsValue(after) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func setBufferRetunesBank() {
|
||||
let spec: LyrebirdBuffer = makeSpec([(300.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec)
|
||||
_ = collectBlocks(voice, engine: engine, blocks: 2)
|
||||
let newSpec: LyrebirdBuffer = makeSpec([(700.0, 1.0, 0.0)])
|
||||
voice.setBuffer(newSpec, engine: engine)
|
||||
let after: [Double] = collectBlocks(voice, engine: engine, blocks: 200)
|
||||
let mag300: Double = dftMag(samples: after, freq: 300, sampleRate: sr)
|
||||
let mag700: Double = dftMag(samples: after, freq: 700, sampleRate: sr)
|
||||
#expect(mag700 > mag300)
|
||||
}
|
||||
|
||||
@Test func mulScalesOutput() {
|
||||
let spec: LyrebirdBuffer = makeSpec([(440.0, 1.0, 0.0)])
|
||||
let plain: Klang.Audio = Klang.Audio(buffer: makeSpec([(440.0, 1.0, 0.0)]))
|
||||
let scaled: Klang.Audio = Klang.Audio(buffer: spec, mul: 0.5)
|
||||
let p: [Double] = collectBlocks(plain, engine: engine, blocks: 40)
|
||||
let s: [Double] = collectBlocks(scaled, engine: engine, blocks: 40)
|
||||
#expect(abs(rmsValue(s) - 0.5 * rmsValue(p)) < 1e-9 + 1e-3 * rmsValue(p))
|
||||
#expect(s.allSatisfy { $0.isFinite })
|
||||
}
|
||||
|
||||
@Test func deterministicFirstSampleMatchesTableLookup() {
|
||||
// Single partial, .none interpolation, initial phase 0.25 cycle. The phase
|
||||
// accumulator seeds at table index tableSize/4 (a power-of-two table makes this
|
||||
// exact), where the sine table holds sin(π/2) = 1.0. So sample 0 = amp · 1.0.
|
||||
let amp: Double = 0.7
|
||||
let spec: LyrebirdBuffer = makeSpec([(100.0, amp, 0.25)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec, interpolation: .none)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 1)
|
||||
#expect(abs(samples[0] - amp) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func phaseZeroFirstSampleIsZero() {
|
||||
// sin(0) = 0 → a single partial starting at phase 0 emits 0 on sample 0.
|
||||
let spec: LyrebirdBuffer = makeSpec([(440.0, 1.0, 0.0)])
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: spec, interpolation: .none)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 1)
|
||||
#expect(abs(samples[0]) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func finiteWithManyPartials() {
|
||||
var triplets: [(Double, Double, Double)] = []
|
||||
for h: Int in 1...16 {
|
||||
triplets.append((110.0 * Double(h), 1.0 / Double(h), 0.0))
|
||||
}
|
||||
let voice: Klang.Audio = Klang.Audio(buffer: makeSpec(triplets), mul: 0.1)
|
||||
let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 100)
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
// LFDutyCycleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFDutyCycle Tests
|
||||
|
||||
@MainActor
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// LFGaussAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFGaussAudioTests
|
||||
|
||||
@MainActor
|
||||
@Suite("LFGauss.Audio")
|
||||
struct LFGaussAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func outputWithinUnitRange() {
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.15)
|
||||
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 50)
|
||||
#expect(samples.allSatisfy { $0 > 0.0 && $0 <= 1.0 + 1e-12 })
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
}
|
||||
|
||||
@Test func peakReachesOneAtMidPeriod() {
|
||||
// One full period long enough to land a sample near phase 0.5 (peak).
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2)
|
||||
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 40)
|
||||
let maxVal: Double = samples.max() ?? 0
|
||||
#expect(maxVal > 0.999)
|
||||
}
|
||||
|
||||
@Test func narrowerWidthSpendsMoreTimeNearZero() throws {
|
||||
let wide: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.4)
|
||||
let sWide: [Double] = collectBlocks(wide, engine: engine, blocks: 60)
|
||||
let meanWide: Double = sWide.reduce(0, +) / Double(sWide.count)
|
||||
|
||||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr)
|
||||
try engine2.start()
|
||||
let narrow: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.05)
|
||||
let sNarrow: [Double] = collectBlocks(narrow, engine: engine2, blocks: 60)
|
||||
let meanNarrow: Double = sNarrow.reduce(0, +) / Double(sNarrow.count)
|
||||
|
||||
// Narrower bell → lower average level (more of the cycle near zero).
|
||||
#expect(meanNarrow < meanWide)
|
||||
}
|
||||
|
||||
@Test func deterministicValueAtPeakPhase() {
|
||||
// phase 0.5 → x = 0 → exp(0) = 1.0 on the first sample.
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.1, width: 0.2, phase: 0.5)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
#expect(abs(samples[0][0] - 1.0) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func deterministicValueAtTroughPhase() {
|
||||
// phase 0 → x = -1 → exp(-0.5 · (1/width)²).
|
||||
let width: Double = 0.2
|
||||
let a: Double = -1.0 / width
|
||||
let expected: Double = exp(-0.5 * a * a)
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.1, width: width, phase: 0.0)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
#expect(abs(samples[0][0] - expected) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func audioRatePeriodModNoCrash() {
|
||||
let lfo: OscSin.Audio = OscSin.Audio(freq: 3.0)
|
||||
let periodGen: BinaryOpGenerator = lfo * 0.005 + 0.02 // sweeps 15–25 ms
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: periodGen, width: 0.2)
|
||||
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 20)
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
#expect(samples.allSatisfy { $0 > 0.0 && $0 <= 1.0 + 1e-12 })
|
||||
}
|
||||
|
||||
@Test func audioRateWidthModNoCrash() {
|
||||
let lfo: OscSin.Audio = OscSin.Audio(freq: 0.5)
|
||||
let widthGen: BinaryOpGenerator = lfo * 0.1 + 0.2
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: widthGen)
|
||||
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 20)
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
}
|
||||
|
||||
@Test func participatesInFanOutCache() {
|
||||
let gauss: LFGauss.Audio = LFGauss.Audio(period: 0.02, width: 0.2)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let r1: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
let r2: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
|
||||
}
|
||||
|
||||
@Test func mulAddScalesAndOffsets() {
|
||||
let plain: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2, phase: 0.5)
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let p: Double = plain.calculatedSamples(graph: g1, count: engine.blockSize)[0][0]
|
||||
|
||||
let scaled: LFGauss.Audio = LFGauss.Audio(period: 0.05, width: 0.2, phase: 0.5,
|
||||
mul: 2.0, add: 1.0)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let s: Double = scaled.calculatedSamples(graph: g2, count: engine.blockSize)[0][0]
|
||||
#expect(abs(s - (p * 2.0 + 1.0)) < 1e-12)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// LFGaussControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFGaussControlTests
|
||||
|
||||
@MainActor
|
||||
@Suite("LFGauss.Control")
|
||||
struct LFGaussControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func outputWithinUnitRange() {
|
||||
let gauss: LFGauss.Control = LFGauss.Control(period: 0.05, width: 0.2)
|
||||
let samples: [Double] = collectBlocks(gauss, engine: engine, blocks: 60)
|
||||
// Linear ramp between bell values stays bounded in [0, 1].
|
||||
#expect(samples.allSatisfy { $0.isFinite })
|
||||
#expect(samples.allSatisfy { $0 >= -1e-12 && $0 <= 1.0 + 1e-12 })
|
||||
}
|
||||
|
||||
@Test func lastSampleMatchesBellValueAtAdvancedPhase() {
|
||||
// First block: phase advances by dt·count from 0; ramp end == bell value at new phase.
|
||||
let period: Double = 0.05
|
||||
let width: Double = 0.2
|
||||
let gauss: LFGauss.Control = LFGauss.Control(period: period, width: width, phase: 0.0)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let samples: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
|
||||
let dt: Double = 1.0 / (period * sr)
|
||||
let advancedPhase: Double = dt * Double(engine.blockSize)
|
||||
let x: Double = 2.0 * advancedPhase - 1.0
|
||||
let aTerm: Double = x / width
|
||||
let expected: Double = exp(-0.5 * aTerm * aTerm)
|
||||
#expect(abs(samples[0][engine.blockSize - 1] - expected) < 1e-10)
|
||||
}
|
||||
|
||||
@Test func participatesInFanOutCache() {
|
||||
let gauss: LFGauss.Control = LFGauss.Control(period: 0.05, width: 0.2)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let r1: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
let r2: [[Double]] = gauss.calculatedSamples(graph: g, count: engine.blockSize)
|
||||
#expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
// LFSawtoothTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFSawtooth Tests
|
||||
|
||||
@MainActor
|
||||
@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])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
// LFSquareTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFSquare Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("LFSquare")
|
||||
struct LFSquareTests {
|
||||
|
||||
@Test("LFSquare.Audio rate is .audio")
|
||||
func testAudioRate() {
|
||||
#expect(LFSquare.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFSquare.Control rate is .control")
|
||||
func testControlRate() {
|
||||
#expect(LFSquare.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
@Test("LFSquare.Audio produces 64 samples")
|
||||
func testAudioOutputCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = LFSquare.Audio(freq: 1.0).calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
@Test("LFSquare.Audio fan-out returns identical first channel")
|
||||
func testAudioFanOut() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFSquare.Audio = LFSquare.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("LFSquare.Audio output stays in [-1, 1]")
|
||||
func testAudioOutputRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFSquare.Audio = LFSquare.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 || out[0][i] == -1.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Waveform shape
|
||||
|
||||
@Test("LFSquare.Audio phase=0 first sample is +1")
|
||||
func testAudioPhaseZeroIsHigh() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0 → 0 < 0.5 → +1
|
||||
let osc: LFSquare.Audio = LFSquare.Audio(freq: 1.0, phase: 0.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] == 1.0)
|
||||
}
|
||||
|
||||
@Test("LFSquare.Audio phase=0.75 first sample is -1")
|
||||
func testAudioPhaseThreeQuartersIsLow() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.75 → 0.75 >= 0.5 → -1
|
||||
let osc: LFSquare.Audio = LFSquare.Audio(freq: 1.0, phase: 0.75)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] == -1.0)
|
||||
}
|
||||
|
||||
// MARK: GeneratorEnum
|
||||
|
||||
@Test("LFSquare GeneratorEnum AudioType rate is .audio")
|
||||
func testGeneratorEnumAudio() {
|
||||
#expect(LFSquare.AudioType.self == LFSquare.Audio.self)
|
||||
#expect(LFSquare.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFSquare GeneratorEnum ControlType rate is .control")
|
||||
func testGeneratorEnumControl() {
|
||||
#expect(LFSquare.ControlType.self == LFSquare.Control.self)
|
||||
#expect(LFSquare.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
// MARK: mul/add
|
||||
|
||||
@Test("LFSquare.Audio mul:2 add:1 scales output")
|
||||
func testAudioMulAdd() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let oscBase: LFSquare.Audio = LFSquare.Audio(freq: 5.0)
|
||||
let oscMulAdd: LFSquare.Audio = LFSquare.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)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Control cache-hit bypass
|
||||
|
||||
@Test("LFSquare.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: LFSquare.Control = LFSquare.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 shifts waveform
|
||||
|
||||
@Test("LFSquare.Audio phase=0.5 first sample is -1 (second half of cycle)")
|
||||
func testAudioPhaseOffsetHalf() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.5 → 0.5 >= 0.5 → -1
|
||||
let osc: LFSquare.Audio = LFSquare.Audio(freq: 1.0, phase: 0.5)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(out[0][0] == -1.0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// LFTriangleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - LFTriangle Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("LFTriangle")
|
||||
struct LFTriangleTests {
|
||||
|
||||
@Test("LFTriangle.Audio rate is .audio")
|
||||
func testAudioRate() {
|
||||
#expect(LFTriangle.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFTriangle.Control rate is .control")
|
||||
func testControlRate() {
|
||||
#expect(LFTriangle.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
@Test("LFTriangle.Audio produces 64 samples")
|
||||
func testAudioOutputCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = LFTriangle.Audio(freq: 1.0).calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
@Test("LFTriangle.Audio fan-out returns identical first channel")
|
||||
func testAudioFanOut() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFTriangle.Audio = LFTriangle.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("LFTriangle.Audio output stays in [-1, 1]")
|
||||
func testAudioOutputRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFTriangle.Audio = LFTriangle.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
|
||||
|
||||
@Test("LFTriangle.Audio phase=0 starts at -1")
|
||||
func testAudioPhaseZeroStartsAtBottom() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0 → 4*0 - 1 = -1.0
|
||||
let osc: LFTriangle.Audio = LFTriangle.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("LFTriangle.Audio phase=0.25 produces 0 (ascending zero crossing)")
|
||||
func testAudioPhaseQuarterIsZero() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.25 → 4*0.25 - 1 = 0.0
|
||||
let osc: LFTriangle.Audio = LFTriangle.Audio(freq: 1.0, phase: 0.25)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(out[0][0]) < 1e-9)
|
||||
}
|
||||
|
||||
@Test("LFTriangle.Audio phase=0.5 produces +1 (peak)")
|
||||
func testAudioPhaseHalfIsPeak() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.5 → 3 - 4*0.5 = 1.0
|
||||
let osc: LFTriangle.Audio = LFTriangle.Audio(freq: 1.0, phase: 0.5)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(out[0][0] - 1.0) < 1e-9)
|
||||
}
|
||||
|
||||
@Test("LFTriangle.Audio phase=0.75 produces 0 (descending zero crossing)")
|
||||
func testAudioPhaseThreeQuartersIsZero() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.75 → 3 - 4*0.75 = 0.0
|
||||
let osc: LFTriangle.Audio = LFTriangle.Audio(freq: 1.0, phase: 0.75)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(out[0][0]) < 1e-9)
|
||||
}
|
||||
|
||||
// MARK: GeneratorEnum
|
||||
|
||||
@Test("LFTriangle GeneratorEnum AudioType rate is .audio")
|
||||
func testGeneratorEnumAudio() {
|
||||
#expect(LFTriangle.AudioType.self == LFTriangle.Audio.self)
|
||||
#expect(LFTriangle.Audio(freq: 1.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("LFTriangle GeneratorEnum ControlType rate is .control")
|
||||
func testGeneratorEnumControl() {
|
||||
#expect(LFTriangle.ControlType.self == LFTriangle.Control.self)
|
||||
#expect(LFTriangle.Control(freq: 1.0).rate == .control)
|
||||
}
|
||||
|
||||
// MARK: mul/add
|
||||
|
||||
@Test("LFTriangle.Audio mul:2 add:1 scales output")
|
||||
func testAudioMulAdd() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let oscBase: LFTriangle.Audio = LFTriangle.Audio(freq: 5.0)
|
||||
let oscMulAdd: LFTriangle.Audio = LFTriangle.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)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Control cache-hit bypass
|
||||
|
||||
@Test("LFTriangle.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: LFTriangle.Control = LFTriangle.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: Ramp reaches target
|
||||
|
||||
@Test("LFTriangle.Control last sample equals ramp target")
|
||||
func testControlRampTarget() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: LFTriangle.Control = LFTriangle.Control(freq: 5.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
||||
// Last sample should equal previousOutputValue (the ramp end)
|
||||
let last: Double = out[0][63]
|
||||
#expect(abs(last - osc.previousOutputValue) < 1e-9)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
// OscSawtoothTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - OscSawtooth Tests
|
||||
|
||||
@MainActor
|
||||
@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])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// OscSquareTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - OscSquare Tests
|
||||
|
||||
@MainActor
|
||||
@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]) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
// OscTableTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("OscTable")
|
||||
struct OscTableTests {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Frequency that advances the phase exactly `step` table entries per sample.
|
||||
private func freqForStep(_ step: Double, tableSize: Int, sampleRate: Double) -> Double {
|
||||
step * Double(sampleRate) / Double(tableSize)
|
||||
}
|
||||
|
||||
// MARK: - Deterministic output (power-of-two table)
|
||||
|
||||
@Test("OscTable with integer-step freq reads table entries in order")
|
||||
func testDeterministicIntegerStep() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Build a small ramp table: [0, 1, 2, 3, 4, 5, 6, 7]
|
||||
let tableSize: Int = 8
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fill { i, _ in Double(i) }
|
||||
|
||||
// freq such that freqIncrement = 1.0 → reads table[0], table[1], …
|
||||
let freq: Double = freqForStep(1.0, tableSize: tableSize, sampleRate: 44100)
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: freq)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 8)
|
||||
for i: Int in 0..<8 {
|
||||
// frac == 0 → output == table[i] exactly
|
||||
#expect(abs(out[0][i] - Double(i)) < 1e-10,
|
||||
"sample \(i): expected \(Double(i)), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Phase offset
|
||||
|
||||
@Test("OscTable with phase=0.25 starts at quarter-cycle position")
|
||||
func testPhaseOffset() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 8)
|
||||
table.fill { i, _ in Double(i) }
|
||||
|
||||
// Very low freq → freqIncrement ≈ 0, so phase barely moves.
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 0.001, phase: 0.25)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
// Phase 0.25 in an 8-sample table → table units = 2.0 → table[2] = 2.0
|
||||
#expect(abs(out[0][0] - 2.0) < 1e-3)
|
||||
}
|
||||
|
||||
// MARK: - State advances across blocks
|
||||
|
||||
@Test("OscTable phase accumulates correctly across consecutive blocks")
|
||||
func testStateAdvancesAcrossBlocks() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Use engine shared sine table
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: engine.sharedTables.sineTable, freq: 440.0)
|
||||
|
||||
let block1: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
let block2: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
// The two blocks must differ (oscillator phase advanced)
|
||||
let identical: Bool = zip(block1[0], block2[0]).allSatisfy { abs($0 - $1) < 1e-12 }
|
||||
#expect(!identical)
|
||||
}
|
||||
|
||||
// MARK: - Sub-block count
|
||||
|
||||
@Test("Sub-block call followed by full block continues from correct phase")
|
||||
func testSubBlockCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tableSize: Int = 8
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fill { i, _ in Double(i) }
|
||||
|
||||
// freqIncrement = 1.0
|
||||
let freq: Double = freqForStep(1.0, tableSize: tableSize, sampleRate: 44100)
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: freq)
|
||||
|
||||
// Call with count=4 → reads table[0…3]
|
||||
let part1: [[Double]] = osc.calculatedSamples(graph: graph, count: 4)
|
||||
for i: Int in 0..<4 {
|
||||
#expect(abs(part1[0][i] - Double(i)) < 1e-10)
|
||||
}
|
||||
|
||||
// Advance serial to simulate a new block boundary (sub-block split)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Call with count=4 again → should read table[4…7]
|
||||
let part2: [[Double]] = osc.calculatedSamples(graph: graph, count: 4)
|
||||
for i: Int in 0..<4 {
|
||||
#expect(abs(part2[0][i] - Double(i + 4)) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Invalid buffer outputs silence
|
||||
|
||||
@Test("OscTable outputs silence after buffer is invalidated")
|
||||
func testInvalidatedBufferOutputsSilence() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0)
|
||||
|
||||
// First block — should be non-silent
|
||||
let beforeFree: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let allZeroBefore: Bool = beforeFree[0].prefix(512).allSatisfy { $0 == 0.0 }
|
||||
#expect(!allZeroBefore)
|
||||
|
||||
// Invalidate the buffer
|
||||
table.invalidate()
|
||||
|
||||
// Advance serial so the next call is not a cache hit (simulates a new engine block)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Next block — must be all zeros
|
||||
let afterFree: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(afterFree[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - OscTable vs OscSin agreement
|
||||
|
||||
@Test("OscTable using engine sine table matches OscSin output")
|
||||
func testMatchesOscSin() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let testFreq: Double = 440.0
|
||||
|
||||
let oscSin: OscSin.Audio = OscSin.Audio(freq: testFreq)
|
||||
let oscTable: OscTable.Audio = OscTable.Audio(table: engine.sharedTables.sineTable, freq: testFreq)
|
||||
|
||||
let sinOut: [[Double]] = oscSin.calculatedSamples(graph: graph, count: 512)
|
||||
let tableOut: [[Double]] = oscTable.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(sinOut[0][i] - tableOut[0][i]) < 1e-12,
|
||||
"divergence at sample \(i): OscSin=\(sinOut[0][i]) OscTable=\(tableOut[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Non-power-of-two table
|
||||
|
||||
@Test("OscTable with non-power-of-two table produces in-range output")
|
||||
func testNonPowerOfTwoTableRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Sine fill → all samples must stay in [-1, 1]
|
||||
let tableSize: Int = 100 // not a power of two
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fillSine()
|
||||
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 441.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-10)
|
||||
#expect(out[0][i] <= 1.0 + 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("OscTable non-power-of-two: integer-step freq reads table entries in order with correct wrap")
|
||||
func testNonPowerOfTwoIntegerStep() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Ramp table: [0, 1, 2, …, N-1]
|
||||
let tableSize: Int = 10 // not a power of two
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fill { i, _ in Double(i) }
|
||||
|
||||
// freqIncrement = 1.0 → reads table[0], table[1], … wrapping at 10
|
||||
let freq: Double = freqForStep(1.0, tableSize: tableSize, sampleRate: 44100)
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: freq)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: tableSize * 2)
|
||||
for i: Int in 0..<tableSize * 2 {
|
||||
let expected: Double = Double(i % tableSize)
|
||||
#expect(abs(out[0][i] - expected) < 1e-10,
|
||||
"sample \(i): expected \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Negative frequency
|
||||
|
||||
@Test("OscTable with negative freq does not crash on non-power-of-two table")
|
||||
func testNegativeFreqNonPowerOfTwo() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tableSize: Int = 100 // not a power of two
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fillSine()
|
||||
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: -441.0)
|
||||
// Must not crash, and output must stay within table range [-1, 1]
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-10)
|
||||
#expect(out[0][i] <= 1.0 + 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("OscTable with negative freq does not crash on power-of-two table")
|
||||
func testNegativeFreqPowerOfTwo() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: -440.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-10)
|
||||
#expect(out[0][i] <= 1.0 + 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - floatValue
|
||||
|
||||
@Test("floatValue returns first sample of a one-sample block")
|
||||
func testFloatValue() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0)
|
||||
|
||||
let fv: Double = osc.floatValue(graph: graph)
|
||||
// Phase starts at 0 → sine(0) = 0
|
||||
#expect(abs(fv) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - Interpolation modes
|
||||
|
||||
@Test("OscTable .none interpolation produces in-range output")
|
||||
func testInterpolationNoneRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0, interpolation: .none)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-10)
|
||||
#expect(out[0][i] <= 1.0 + 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("OscTable .none at phase=0.25 returns table[N/4] exactly")
|
||||
func testInterpolationNoneDeterministicQuarterPhase() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Use the engine's shared sine table: index N/4 = sin(π/2) = 1.0.
|
||||
let table: LyrebirdBuffer = engine.sharedTables.sineTable
|
||||
// freq=0, phase=0.25 → intPhase = tableSize/4 = 8192, frac=0; .none returns table[8192].
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 0.0, phase: 0.25, interpolation: .none)
|
||||
let s: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(s[0][0] - 1.0) < 1e-6)
|
||||
}
|
||||
|
||||
@Test("OscTable .cubic interpolation produces in-range output")
|
||||
func testInterpolationCubicRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0, interpolation: .cubic)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-4)
|
||||
#expect(out[0][i] <= 1.0 + 1e-4)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("OscTable .cubic at phase=0.25 matches sine(π/2)=1.0")
|
||||
func testInterpolationCubicDeterministicQuarterPhase() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// freq=0, phase=0.25 → intPhase=8192, frac=0. Catmull-Rom with frac=0 = p0 = sin(π/2).
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: engine.sharedTables.sineTable, freq: 0.0, phase: 0.25, interpolation: .cubic)
|
||||
let s: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(s[0][0] - 1.0) < 1e-6)
|
||||
}
|
||||
|
||||
@Test("OscTable all modes agree at integer phase steps")
|
||||
func testInterpolationModesAgreeAtIntegerSteps() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Ramp table — no ambiguity about interpolation target values
|
||||
let tableSize: Int = 8
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fill { i, _ in Double(i) }
|
||||
|
||||
// freqIncrement = 1.0 → fractional part always 0
|
||||
let freq: Double = freqForStep(1.0, tableSize: tableSize, sampleRate: 44100)
|
||||
let oscNone: OscTable.Audio = OscTable.Audio(table: table, freq: freq, interpolation: .none)
|
||||
let oscLinear: OscTable.Audio = OscTable.Audio(table: table, freq: freq, interpolation: .linear)
|
||||
let oscCubic: OscTable.Audio = OscTable.Audio(table: table, freq: freq, interpolation: .cubic)
|
||||
|
||||
let outNone: [[Double]] = oscNone.calculatedSamples(graph: graph, count: 8)
|
||||
let outLinear: [[Double]] = oscLinear.calculatedSamples(graph: graph, count: 8)
|
||||
let outCubic: [[Double]] = oscCubic.calculatedSamples(graph: graph, count: 8)
|
||||
|
||||
for i: Int in 0..<8 {
|
||||
#expect(abs(outNone[0][i] - outLinear[0][i]) < 1e-10,
|
||||
"none vs linear diverge at sample \(i)")
|
||||
#expect(abs(outCubic[0][i] - outLinear[0][i]) < 1e-10,
|
||||
"cubic vs linear diverge at sample \(i)")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("OscTable .none differs from .linear at fractional phases")
|
||||
func testInterpolationNoneDiffersFromLinear() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
table.fillSine()
|
||||
|
||||
let oscNone: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0, interpolation: .none)
|
||||
let oscLinear: OscTable.Audio = OscTable.Audio(table: table, freq: 440.0, interpolation: .linear)
|
||||
|
||||
let outNone: [[Double]] = oscNone.calculatedSamples(graph: graph, count: 512)
|
||||
let outLinear: [[Double]] = oscLinear.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let somesDiffer: Bool = (0..<512).contains { abs(outNone[0][$0] - outLinear[0][$0]) > 1e-6 }
|
||||
#expect(somesDiffer)
|
||||
}
|
||||
|
||||
@Test("OscTable .cubic works with non-power-of-two table")
|
||||
func testCubicNonPowerOfTwo() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let tableSize: Int = 100 // not a power of two
|
||||
let table: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
table.fillSine()
|
||||
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: table, freq: 441.0, interpolation: .cubic)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i] >= -1.0 - 1e-4)
|
||||
#expect(out[0][i] <= 1.0 + 1e-4)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// OscTriangleTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - OscTriangle Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("OscTriangle")
|
||||
struct OscTriangleTests {
|
||||
|
||||
@Test("OscTriangle.Audio rate is .audio")
|
||||
func testAudioRate() {
|
||||
#expect(OscTriangle.Audio(freq: 440.0).rate == .audio)
|
||||
}
|
||||
|
||||
@Test("OscTriangle.Control rate is .control")
|
||||
func testControlRate() {
|
||||
#expect(OscTriangle.Control(freq: 440.0).rate == .control)
|
||||
}
|
||||
|
||||
@Test("OscTriangle.Audio produces 64 samples")
|
||||
func testAudioOutputCount() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = OscTriangle.Audio(freq: 440.0).calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
@Test("OscTriangle.Audio fan-out returns identical first channel")
|
||||
func testAudioFanOut() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: OscTriangle.Audio = OscTriangle.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("OscTriangle.Audio output stays near [-1, 2] (BLAMP transient)")
|
||||
func testAudioOutputRange() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: OscTriangle.Audio = OscTriangle.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("OscTriangle.Audio phase=0 starts near -1 (bottom of triangle)")
|
||||
func testAudioPhaseZeroStartsAtBottom() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0 → raw = 4*0 - 1 = -1.0
|
||||
let osc: OscTriangle.Audio = OscTriangle.Audio(freq: 1.0, phase: 0.0)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
// At 1 Hz in 44100 Hz, dt is tiny so BLAMP correction is negligible
|
||||
#expect(abs(out[0][0] - (-1.0)) < 0.01)
|
||||
}
|
||||
|
||||
@Test("OscTriangle.Audio phase=0.25 starts near 0 (ascending zero crossing)")
|
||||
func testAudioPhaseQuarterNearZero() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// phase=0.25 → raw = 4*0.25 - 1 = 0.0
|
||||
let osc: OscTriangle.Audio = OscTriangle.Audio(freq: 1.0, phase: 0.25)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 1)
|
||||
#expect(abs(out[0][0]) < 0.01)
|
||||
}
|
||||
|
||||
@Test("OscTriangle.Audio mul:2 add:1 scales output")
|
||||
func testAudioMulAdd() {
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let oscBase: OscTriangle.Audio = OscTriangle.Audio(freq: 100.0)
|
||||
let oscMulAdd: OscTriangle.Audio = OscTriangle.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("OscTriangle.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: OscTriangle.Control = OscTriangle.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]) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// PhasorAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Phasor.Audio")
|
||||
struct PhasorAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: Phasor.Audio = Phasor.Audio()
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Default range [0, 1), default rate 1.0/sample — output count correct.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: Phasor.Audio = Phasor.Audio()
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// With no trigger, phase should start at `start` (0.0) and ramp by `rate` per sample.
|
||||
@Test func defaultRampFromZero() {
|
||||
// rate = 0.01 so 64 samples reaches 0.64 without wrapping.
|
||||
let rate: Double = 0.01
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: rate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = Double(i) * rate
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"sample \(i): expected \(expected) got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Non-default start/end range.
|
||||
@Test func nonDefaultStartEndRange() {
|
||||
let rate: Double = 0.1
|
||||
let start: Double = 2.0
|
||||
let end: Double = 4.0
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: rate, start: start, end: end)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// First sample should equal start (2.0), then increment by 0.1 per sample.
|
||||
#expect(abs(out[0][0] - 2.0) < 1e-12)
|
||||
#expect(abs(out[0][1] - 2.1) < 1e-12)
|
||||
}
|
||||
|
||||
/// When rate equals span / count, the phasor wraps exactly to start after one block.
|
||||
@Test func wrapExactlyAtEnd() {
|
||||
let start: Double = 0.0
|
||||
let end: Double = 1.0
|
||||
let span: Double = end - start
|
||||
// Set rate so that after exactly 64 samples we should wrap and be at 0 again.
|
||||
// Use a rate that divides span evenly: span / 64 increments produce one full cycle.
|
||||
let rate: Double = span / Double(blockSize) // 1/64
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: rate, start: start, end: end)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Sample 0 = 0.0, sample 63 = 63 * (1/64) = 63/64.
|
||||
#expect(abs(out[0][0] - 0.0) < 1e-12)
|
||||
#expect(abs(out[0][blockSize - 1] - (Double(blockSize - 1) * rate)) < 1e-12)
|
||||
// Advance to block 2 — sample 0 of block 2 should be 0.0 again (wrapped back).
|
||||
advanceBlock(engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(out2[0][0] - 0.0) < 1e-12,
|
||||
"expected wrap back to 0.0, got \(out2[0][0])")
|
||||
}
|
||||
|
||||
/// Rising-edge trigger resets phase to resetPos at the exact trigger sample.
|
||||
@Test func triggerResetToResetPos() {
|
||||
let rate: Double = 0.01
|
||||
let resetPos: Double = 0.5
|
||||
// Trig goes ≤0 → >0 at sample 32.
|
||||
let trigInput: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: Phasor.Audio = Phasor.Audio(trig: trigInput, rate: rate, resetPos: resetPos)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Samples 0..31 ramp from 0 by 0.01 each step.
|
||||
for i: Int in 0..<32 {
|
||||
let expected: Double = Double(i) * rate
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"pre-trig sample \(i): expected \(expected) got \(out[0][i])")
|
||||
}
|
||||
// At sample 32 the trig fires → phase resets to resetPos before output.
|
||||
#expect(abs(out[0][32] - resetPos) < 1e-12,
|
||||
"trig sample 32: expected \(resetPos) got \(out[0][32])")
|
||||
// Samples 33+ continue from resetPos + rate.
|
||||
for i: Int in 33..<blockSize {
|
||||
let expected: Double = resetPos + Double(i - 32) * rate
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"post-trig sample \(i): expected \(expected) got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calculatedSamples calls on the same graph return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: 0.01)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// Second call in same segment is a cache hit — state does not double-advance.
|
||||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: 0.01)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
// Compare out1 against expected: sample i = i * 0.01.
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = Double(i) * 0.01
|
||||
#expect(abs(out1[0][i] - expected) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
/// Cross-block phase continuity: block 2 starts where block 1 left off.
|
||||
@Test func crossBlockContinuity() {
|
||||
let rate: Double = 0.001 // very slow, won't wrap in 2 blocks
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: rate)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After block 1, phase = blockSize * rate = 64 * 0.001 = 0.064.
|
||||
// Block 2, sample 0 should output that phase before incrementing.
|
||||
let expectedBlock2Start: Double = Double(blockSize) * rate
|
||||
#expect(abs(out2[0][0] - expectedBlock2Start) < 1e-12,
|
||||
"cross-block start: expected \(expectedBlock2Start) got \(out2[0][0])")
|
||||
}
|
||||
|
||||
/// Large rate > span wraps via truncatingRemainder without hanging.
|
||||
@Test func largeRateWrapsWithoutHang() {
|
||||
// rate = 5.0, span = 1.0 → wraps 5 times per sample; should still produce valid output.
|
||||
let gen: Phasor.Audio = Phasor.Audio(rate: 5.0, start: 0.0, end: 1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// All samples should be in [0, 1).
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[0][i] >= 0.0, "sample \(i) below start: \(out[0][i])")
|
||||
#expect(out[0][i] < 1.0, "sample \(i) at or above end: \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Negative rate ramps down and wraps up at start.
|
||||
@Test func negativeRateRampsDown() {
|
||||
// Start at 0.5 (via trig), rate = -0.01. Phase decreases each sample and wraps at 0.0.
|
||||
// Use a rate that is exactly representable in IEEE 754 to avoid FP-drift issues.
|
||||
// 0.5 - k * 0.01 in IEEE 754 accumulates error; verify monotonic decrease and wrap behaviour
|
||||
// rather than exact sample-level equality past the first few steps.
|
||||
let rate: Double = -0.01
|
||||
let gen: Phasor.Audio = Phasor.Audio(
|
||||
trig: Closure { _, i in i == 0 ? 1.0 : 0.0 },
|
||||
rate: rate,
|
||||
start: 0.0,
|
||||
end: 1.0,
|
||||
resetPos: 0.5
|
||||
)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Sample 0 = resetPos = 0.5 (trig fires at sample 0 resetting to 0.5).
|
||||
#expect(abs(out[0][0] - 0.5) < 1e-12)
|
||||
// Subsequent samples are strictly decreasing until a wrap occurs.
|
||||
var foundWrap: Bool = false
|
||||
for i: Int in 1..<blockSize {
|
||||
if out[0][i] > out[0][i - 1] {
|
||||
foundWrap = true
|
||||
// After wrap, output should be in [0, 1).
|
||||
#expect(out[0][i] >= 0.0 && out[0][i] < 1.0,
|
||||
"wrapped sample \(i) out of range: \(out[0][i])")
|
||||
break
|
||||
}
|
||||
// Before wrap: still in [0, 1) and decreasing.
|
||||
#expect(out[0][i] >= 0.0 && out[0][i] < 0.5,
|
||||
"pre-wrap sample \(i) should be in [0, 0.5): \(out[0][i])")
|
||||
}
|
||||
// A wrap must have occurred within 64 samples (0.5 / 0.01 = 50 steps).
|
||||
#expect(foundWrap, "expected a wrap within \(blockSize) samples with rate=\(rate)")
|
||||
}
|
||||
|
||||
/// mul/add: with mul=2.0 add=1.0, output equals original*2+1.
|
||||
@Test func mulAdd() {
|
||||
let rate: Double = 0.01
|
||||
let genBase: Phasor.Audio = Phasor.Audio(rate: rate)
|
||||
let genScaled: Phasor.Audio = Phasor.Audio(rate: rate, mul: 2.0, add: 1.0)
|
||||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// PhasorControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Phasor.Control")
|
||||
struct PhasorControlTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: Phasor.Control = Phasor.Control()
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Output count correct.
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: Phasor.Control = Phasor.Control()
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
/// After block 1, previousOutputValue reflects rate * count advancement.
|
||||
@Test func advancesRateTimesCountPerBlock() {
|
||||
let rate: Double = 0.01
|
||||
// start=0, end=1 (defaults). Phase after block 1 = rate * 64 = 0.64.
|
||||
let gen: Phasor.Control = Phasor.Control(rate: rate)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedAfterBlock1: Double = rate * Double(blockSize)
|
||||
#expect(abs(gen.previousOutputValue - expectedAfterBlock1) < 1e-12,
|
||||
"previousOutputValue after block 1: expected \(expectedAfterBlock1) got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// After block 2, previousOutputValue is rate * 2 * count (no wrap).
|
||||
@Test func advancesTwoBlocksCorrectly() {
|
||||
let rate: Double = 0.001 // 0.001 * 64 * 2 = 0.128 — no wrapping.
|
||||
let gen: Phasor.Control = Phasor.Control(rate: rate)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let expectedAfterBlock2: Double = rate * Double(blockSize) * 2.0
|
||||
#expect(abs(gen.previousOutputValue - expectedAfterBlock2) < 1e-12,
|
||||
"previousOutputValue after block 2: expected \(expectedAfterBlock2) got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// A reset edge in the block sets phase to resetPos.
|
||||
@Test func triggerResetEdgeInBlock() {
|
||||
let rate: Double = 0.001
|
||||
let resetPos: Double = 0.75
|
||||
// Rising edge at sample 16 resets phase to resetPos.
|
||||
let trigInput: Closure = Closure { _, i in i == 16 ? 1.0 : 0.0 }
|
||||
let gen: Phasor.Control = Phasor.Control(trig: trigInput, rate: rate, resetPos: resetPos)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After the block: phase = resetPos + rate * blockSize (advance from resetPos).
|
||||
let expectedPhase: Double = resetPos + rate * Double(blockSize)
|
||||
#expect(abs(gen.previousOutputValue - expectedPhase) < 1e-12,
|
||||
"after reset: expected \(expectedPhase) got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output is ramped (not flat): last sample of first block should equal the target.
|
||||
@Test func outputIsRamped() {
|
||||
let rate: Double = 0.01
|
||||
let gen: Phasor.Control = Phasor.Control(rate: rate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
let target: Double = gen.previousOutputValue
|
||||
#expect(abs(lastSample - target) < 1e-12,
|
||||
"last sample \(lastSample) should equal target \(target)")
|
||||
}
|
||||
|
||||
/// Fan-out: two calls same segment return identical first channel.
|
||||
@Test func fanOut() {
|
||||
let gen: Phasor.Control = Phasor.Control(rate: 0.01)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Phasor.Control = Phasor.Control(rate: 0.01)
|
||||
// Before first block, floatValue == previousOutputValue == 0.
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Sub-block bypass: _computeSamples runs exactly once per audio block.
|
||||
@Test func subBlockBypassRunsOncePerAudioBlock() {
|
||||
let rate: Double = 0.01
|
||||
let gen: Phasor.Control = Phasor.Control(rate: rate)
|
||||
// Call calculatedSamples twice on the same graph serial — second call must be cache hit.
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let phaseAfterFirstCall: Double = gen.previousOutputValue
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Phase must not have advanced on second call.
|
||||
#expect(gen.previousOutputValue == phaseAfterFirstCall)
|
||||
// Outputs must be identical.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,694 @@
|
||||
// VectorOscillatorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - VectorOscillatorTests
|
||||
|
||||
@MainActor
|
||||
@Suite("VectorOscillator")
|
||||
struct VectorOscillatorTests {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
/// Build four mono buffers of `lyrebirdDefaultTableSize`. A=sine, B=8-partial saw,
|
||||
/// C=Hanning window, D=triangle. Distinct shapes so corner reads are detectably different.
|
||||
private func makeFourBuffers() -> (LyrebirdBuffer, LyrebirdBuffer, LyrebirdBuffer, LyrebirdBuffer) {
|
||||
let size: Int = lyrebirdDefaultTableSize
|
||||
let a: LyrebirdBuffer = LyrebirdBuffer(count: size); a.fillSine()
|
||||
let b: LyrebirdBuffer = LyrebirdBuffer(count: size); b.fill(partials: 8)
|
||||
let c: LyrebirdBuffer = LyrebirdBuffer(count: size); c.fillHanning()
|
||||
let d: LyrebirdBuffer = LyrebirdBuffer(count: size); d.fillTriangle()
|
||||
return (a, b, c, d)
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test("VectorOscillator.Audio rate is .audio")
|
||||
func testRate() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.0, y: 0.0
|
||||
)
|
||||
#expect(osc.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test("VectorOscillator.Audio produces 64 samples")
|
||||
func testOutputCount() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.5, y: 0.5
|
||||
)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test("VectorOscillator.Audio fan-out returns identical first channel")
|
||||
func testFanOut() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.25, y: 0.75
|
||||
)
|
||||
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: - Cache hit does not double-advance phase
|
||||
|
||||
@Test("Second calculatedSamples in same segment is a cache hit; new block advances phase")
|
||||
func testCacheHitDoesNotDoubleAdvance() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.5, y: 0.5
|
||||
)
|
||||
|
||||
let block1: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
// After the first call, the cache serial must match the engine's current
|
||||
// segment serial — proving the second call below is a true cache hit
|
||||
// rather than a deterministic recompute.
|
||||
#expect(osc._segmentSerial == engine.segmentSerial)
|
||||
let block1b: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(block1[0][i] == block1b[0][i])
|
||||
}
|
||||
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let block2: [[Double]] = osc.calculatedSamples(graph: graph2, count: 512)
|
||||
let identical: Bool = zip(Array(block1[0].prefix(512)), Array(block2[0].prefix(512)))
|
||||
.allSatisfy { abs($0 - $1) < 1e-12 }
|
||||
#expect(!identical)
|
||||
}
|
||||
|
||||
// MARK: - Corner A: x = 0, y = 0 ≡ OscTable on bufferA
|
||||
|
||||
@Test("x = 0, y = 0 matches OscTable on bufferA sample-for-sample")
|
||||
func testCornerA() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0
|
||||
)
|
||||
let ref: OscTable.Audio = OscTable.Audio(table: a, freq: testFreq)
|
||||
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(oscOut[0][i] - refOut[0][i]) < tolerance,
|
||||
"sample \(i): osc=\(oscOut[0][i]) ref=\(refOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Corner B: x = 1, y = 0 ≡ OscTable on bufferB
|
||||
|
||||
@Test("x = 1, y = 0 matches OscTable on bufferB sample-for-sample")
|
||||
func testCornerB() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 330.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 1.0, y: 0.0
|
||||
)
|
||||
let ref: OscTable.Audio = OscTable.Audio(table: b, freq: testFreq)
|
||||
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(oscOut[0][i] - refOut[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Corner C: x = 0, y = 1 ≡ OscTable on bufferC
|
||||
|
||||
@Test("x = 0, y = 1 matches OscTable on bufferC sample-for-sample")
|
||||
func testCornerC() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 220.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 1.0
|
||||
)
|
||||
let ref: OscTable.Audio = OscTable.Audio(table: c, freq: testFreq)
|
||||
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(oscOut[0][i] - refOut[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Corner D: x = 1, y = 1 ≡ OscTable on bufferD
|
||||
|
||||
@Test("x = 1, y = 1 matches OscTable on bufferD sample-for-sample")
|
||||
func testCornerD() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 110.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 1.0, y: 1.0
|
||||
)
|
||||
let ref: OscTable.Audio = OscTable.Audio(table: d, freq: testFreq)
|
||||
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(oscOut[0][i] - refOut[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Center: x = 0.5, y = 0.5 ≡ average of all four corners
|
||||
|
||||
@Test("x = 0.5, y = 0.5 produces average of four single-table renders")
|
||||
func testCenterIsAverageOfFour() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 220.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5
|
||||
)
|
||||
let oscA: OscTable.Audio = OscTable.Audio(table: a, freq: testFreq)
|
||||
let oscB: OscTable.Audio = OscTable.Audio(table: b, freq: testFreq)
|
||||
let oscC: OscTable.Audio = OscTable.Audio(table: c, freq: testFreq)
|
||||
let oscD: OscTable.Audio = OscTable.Audio(table: d, freq: testFreq)
|
||||
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let oa: [[Double]] = oscA.calculatedSamples(graph: graph, count: 512)
|
||||
let ob: [[Double]] = oscB.calculatedSamples(graph: graph, count: 512)
|
||||
let oc: [[Double]] = oscC.calculatedSamples(graph: graph, count: 512)
|
||||
let od: [[Double]] = oscD.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = (oa[0][i] + ob[0][i] + oc[0][i] + od[0][i]) * 0.25
|
||||
#expect(
|
||||
abs(out[0][i] - expected) < tolerance,
|
||||
"sample \(i): got \(out[0][i]) expected \(expected)"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - X clamp below
|
||||
|
||||
@Test("x clamped below 0 returns left-edge column (lerp(A,C, y))")
|
||||
func testXClampBelow() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 220.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let oscClamped: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: -5.0, y: 0.0
|
||||
)
|
||||
let oscRef: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0
|
||||
)
|
||||
|
||||
let outClamped: [[Double]] = oscClamped.calculatedSamples(graph: graph, count: 512)
|
||||
let outRef: [[Double]] = oscRef.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(outClamped[0][i] - outRef[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Y clamp above
|
||||
|
||||
@Test("y clamped above 1 returns top-edge row (lerp(C,D, x))")
|
||||
func testYClampAbove() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 220.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let oscClamped: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 1.0, y: 99.0
|
||||
)
|
||||
let oscRef: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 1.0, y: 1.0
|
||||
)
|
||||
|
||||
let outClamped: [[Double]] = oscClamped.calculatedSamples(graph: graph, count: 512)
|
||||
let outRef: [[Double]] = oscRef.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(outClamped[0][i] - outRef[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Phase modulation: 0.5-cycle offset inverts sine
|
||||
|
||||
@Test("phase = 0.5 cycles inverts a sine corner relative to phase = 0.0")
|
||||
func testPhaseModulationHalfCycleInversion() {
|
||||
let size: Int = lyrebirdDefaultTableSize
|
||||
let s1: LyrebirdBuffer = LyrebirdBuffer(count: size); s1.fillSine()
|
||||
let s2: LyrebirdBuffer = LyrebirdBuffer(count: size); s2.fillSine()
|
||||
let s3: LyrebirdBuffer = LyrebirdBuffer(count: size); s3.fillSine()
|
||||
let s4: LyrebirdBuffer = LyrebirdBuffer(count: size); s4.fillSine()
|
||||
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let oscNoPM: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: s1, bufferB: s2, bufferC: s3, bufferD: s4,
|
||||
freq: testFreq, x: 0.5, y: 0.5, phase: 0.0
|
||||
)
|
||||
let oscWithPM: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: s1, bufferB: s2, bufferC: s3, bufferD: s4,
|
||||
freq: testFreq, x: 0.5, y: 0.5, phase: 0.5
|
||||
)
|
||||
|
||||
let outNoPM: [[Double]] = oscNoPM.calculatedSamples(graph: graph, count: 512)
|
||||
let outWithPM: [[Double]] = oscWithPM.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
// Linear interp on a 32768-sample sine table at 440 Hz / 44.1 kHz easily
|
||||
// hits 1e-10. Loose tolerance hides interp regressions.
|
||||
let tolerance: Double = 1e-10
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(outNoPM[0][i] + outWithPM[0][i]) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - mul/add
|
||||
|
||||
@Test("mul:2.0, add:1.0 doubles and offsets output")
|
||||
func testMulAdd() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let base: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5
|
||||
)
|
||||
let scaled: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5,
|
||||
mul: 2.0, add: 1.0
|
||||
)
|
||||
|
||||
let baseOut: [[Double]] = base.calculatedSamples(graph: graph, count: 512)
|
||||
let scaledOut: [[Double]] = scaled.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = baseOut[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaledOut[0][i] - expected) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - x and y as Generators
|
||||
|
||||
@Test("x and y driven by Generators do not crash and stay bounded with sine corners")
|
||||
func testGeneratorXY() {
|
||||
let size: Int = lyrebirdDefaultTableSize
|
||||
let s1: LyrebirdBuffer = LyrebirdBuffer(count: size); s1.fillSine()
|
||||
let s2: LyrebirdBuffer = LyrebirdBuffer(count: size); s2.fillSine()
|
||||
let s3: LyrebirdBuffer = LyrebirdBuffer(count: size); s3.fillSine()
|
||||
let s4: LyrebirdBuffer = LyrebirdBuffer(count: size); s4.fillSine()
|
||||
|
||||
let xGen: BinaryOpGenerator = LFSawtooth.Control(freq: 1.0) * 0.5 + 0.5
|
||||
let yGen: BinaryOpGenerator = LFSawtooth.Control(freq: 0.5) * 0.5 + 0.5
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: s1, bufferB: s2, bufferC: s3, bufferD: s4,
|
||||
freq: 440.0, x: xGen, y: yGen
|
||||
)
|
||||
|
||||
let out1: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = osc.calculatedSamples(graph: graph2, count: 512)
|
||||
|
||||
let outRange: Double = 1.1
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out1[0][i] >= -outRange && out1[0][i] <= outRange)
|
||||
#expect(out2[0][i] >= -outRange && out2[0][i] <= outRange)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - freq as Generator (true FM)
|
||||
|
||||
@Test("freq driven by a Generator does not crash and produces non-zero output")
|
||||
func testGeneratorFreq() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let freqGen: BinaryOpGenerator = LFSawtooth.Control(freq: 1.0) * 100.0 + 300.0 // ~200..400 Hz
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: freqGen, x: 0.5, y: 0.5
|
||||
)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let nonZero: Bool = (0..<512).contains { out[0][$0] != 0.0 }
|
||||
#expect(nonZero)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[0][i].isFinite)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Silence after invalidation
|
||||
|
||||
@Test("VectorOscillator.Audio outputs silence when any buffer is invalidated")
|
||||
func testInvalidatedBufferOutputsSilence() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.0, y: 0.0 // reads only A; we still expect silence after C invalidation
|
||||
)
|
||||
|
||||
let before: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
// sample 0 of fillSine() at phase 0 is exactly 0.0; assert sample 1 is
|
||||
// non-zero to actually verify bufferA is being read at corner A.
|
||||
#expect(before[0][1] != 0.0)
|
||||
let allZeroBefore: Bool = Array(before[0].prefix(512)).allSatisfy { $0 == 0.0 }
|
||||
#expect(!allZeroBefore)
|
||||
|
||||
c.invalidate()
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let after: [[Double]] = osc.calculatedSamples(graph: graph2, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(after[0][i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Silence path applies mul/add (constant `add` survives invalidation)
|
||||
|
||||
@Test("Invalidated-buffer silence path applies mul/add — `add: 0.5` yields 0.5 output")
|
||||
func testInvalidatedBufferAppliesAddOffset() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 440.0, x: 0.5, y: 0.5,
|
||||
mul: 1.0, add: 0.5
|
||||
)
|
||||
|
||||
// Pre-invalidate sanity check — without this, a regression that returns
|
||||
// 0 from the non-silence path would still appear to pass when add=0.5.
|
||||
let beforeOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
let beforeMatchesAddOnly: Bool = (0..<512).allSatisfy { abs(beforeOut[0][$0] - 0.5) < 1e-12 }
|
||||
#expect(!beforeMatchesAddOnly, "non-silence path should produce signal + add, not just add")
|
||||
|
||||
b.invalidate()
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph2, count: 512)
|
||||
let tolerance: Double = 1e-12
|
||||
for i: Int in 0..<512 {
|
||||
#expect(abs(out[0][i] - 0.5) < tolerance, "sample \(i): expected 0.5, got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Interpolation modes produce different output
|
||||
|
||||
@Test(".none / .linear / .cubic interpolation produce different outputs at non-integer phase")
|
||||
func testInterpolationModes() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
// 333 Hz with 32768-table at 44100 SR yields a per-sample increment of
|
||||
// ~247.6 — non-integer, so interpolation mode matters.
|
||||
let testFreq: Double = 333.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let oscNone: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0,
|
||||
interpolation: .none
|
||||
)
|
||||
let oscLin: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0,
|
||||
interpolation: .linear
|
||||
)
|
||||
let oscCub: VectorOscillator.Audio = VectorOscillator.Audio(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0,
|
||||
interpolation: .cubic
|
||||
)
|
||||
|
||||
let on: [[Double]] = oscNone.calculatedSamples(graph: graph, count: 512)
|
||||
let ol: [[Double]] = oscLin.calculatedSamples(graph: graph, count: 512)
|
||||
let oc: [[Double]] = oscCub.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let noneEqLinear: Bool = zip(Array(on[0].prefix(512)), Array(ol[0].prefix(512)))
|
||||
.allSatisfy { abs($0 - $1) < 1e-12 }
|
||||
let linearEqCubic: Bool = zip(Array(ol[0].prefix(512)), Array(oc[0].prefix(512)))
|
||||
.allSatisfy { abs($0 - $1) < 1e-12 }
|
||||
#expect(!noneEqLinear)
|
||||
#expect(!linearEqCubic)
|
||||
}
|
||||
|
||||
// MARK: - Buffer mismatch trap (smoke — preconditions should fire on construction misuse)
|
||||
|
||||
// Note: precondition traps cannot be caught from Swift tests; we rely on /lyrebird_review and
|
||||
// CLAUDE.md authoring rules to enforce correct usage. No #expect-trap test here.
|
||||
|
||||
// MARK: - Control rate
|
||||
|
||||
@Test("VectorOscillator.Control rate is .control")
|
||||
func testControlRate() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let osc: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 1.0, x: 0.5, y: 0.5
|
||||
)
|
||||
#expect(osc.rate == .control)
|
||||
}
|
||||
|
||||
// MARK: - Control output count
|
||||
|
||||
@Test("VectorOscillator.Control produces 64 samples")
|
||||
func testControlOutputCount() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 1.0, x: 0.5, y: 0.5
|
||||
)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
// MARK: - Control fan-out cache
|
||||
|
||||
@Test("VectorOscillator.Control fan-out returns identical first channel in same segment")
|
||||
func testControlFanOut() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 1.0, x: 0.25, y: 0.75
|
||||
)
|
||||
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: - Control corner A target equals OscTable.Control target on bufferA
|
||||
|
||||
@Test("VectorOscillator.Control x=0,y=0 ramp target matches OscTable.Control on bufferA")
|
||||
func testControlCornerAMatchesOscTable() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 1.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let vec: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.0, y: 0.0
|
||||
)
|
||||
let ref: OscTable.Control = OscTable.Control(table: a, freq: testFreq)
|
||||
|
||||
let vecOut: [[Double]] = vec.calculatedSamples(graph: graph, count: 64)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
#expect(
|
||||
abs(vecOut[0][63] - refOut[0][63]) < tolerance,
|
||||
"last sample: vec=\(vecOut[0][63]) ref=\(refOut[0][63])"
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Control center target = mean of four corner OscTable.Control targets
|
||||
|
||||
@Test("VectorOscillator.Control x=0.5,y=0.5 target is mean of four OscTable.Control targets")
|
||||
func testControlCenterIsMeanOfFourTargets() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 2.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let vec: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5
|
||||
)
|
||||
let oa: OscTable.Control = OscTable.Control(table: a, freq: testFreq)
|
||||
let ob: OscTable.Control = OscTable.Control(table: b, freq: testFreq)
|
||||
let oc: OscTable.Control = OscTable.Control(table: c, freq: testFreq)
|
||||
let od: OscTable.Control = OscTable.Control(table: d, freq: testFreq)
|
||||
|
||||
let vOut: [[Double]] = vec.calculatedSamples(graph: graph, count: 64)
|
||||
let aOut: [[Double]] = oa.calculatedSamples(graph: graph, count: 64)
|
||||
let bOut: [[Double]] = ob.calculatedSamples(graph: graph, count: 64)
|
||||
let cOut: [[Double]] = oc.calculatedSamples(graph: graph, count: 64)
|
||||
let dOut: [[Double]] = od.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let expected: Double = (aOut[0][63] + bOut[0][63] + cOut[0][63] + dOut[0][63]) * 0.25
|
||||
let tolerance: Double = 1e-9
|
||||
#expect(abs(vOut[0][63] - expected) < tolerance)
|
||||
}
|
||||
|
||||
// MARK: - Control ramp: last sample equals previousOutputValue
|
||||
|
||||
@Test("VectorOscillator.Control ramp last sample equals previousOutputValue")
|
||||
func testControlRampLastSample() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let osc: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 0.5, x: 0.7, y: 0.3
|
||||
)
|
||||
let out: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out[0][63] == osc.previousOutputValue)
|
||||
}
|
||||
|
||||
// MARK: - Control mul/add
|
||||
|
||||
@Test("VectorOscillator.Control with mul:2.0, add:1.0 doubles and offsets output")
|
||||
func testControlMulAdd() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let testFreq: Double = 1.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let base: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5
|
||||
)
|
||||
let scaled: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: testFreq, x: 0.5, y: 0.5,
|
||||
mul: 2.0, add: 1.0
|
||||
)
|
||||
|
||||
let baseOut: [[Double]] = base.calculatedSamples(graph: graph, count: 64)
|
||||
let scaledOut: [[Double]] = scaled.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<64 {
|
||||
let expected: Double = baseOut[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaledOut[0][i] - expected) < tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Control silence on invalidation
|
||||
|
||||
@Test("VectorOscillator.Control ramps to 0 (mul/add applied) when any buffer is invalidated")
|
||||
func testControlInvalidationRampsToZero() {
|
||||
let (a, b, c, d) = makeFourBuffers()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let osc: VectorOscillator.Control = VectorOscillator.Control(
|
||||
bufferA: a, bufferB: b, bufferC: c, bufferD: d,
|
||||
freq: 1.0, x: 0.5, y: 0.5
|
||||
)
|
||||
|
||||
// Prime: one block to settle ramp at a non-zero target.
|
||||
_ = osc.calculatedSamples(graph: graph, count: 64)
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let primed: [[Double]] = osc.calculatedSamples(graph: graph2, count: 64)
|
||||
#expect(primed[0][63] != 0.0)
|
||||
|
||||
// Invalidate one buffer (even unused — full scan must catch it).
|
||||
c.invalidate()
|
||||
advanceBlock(engine)
|
||||
let graph3: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = osc.calculatedSamples(graph: graph3, count: 64)
|
||||
|
||||
// After one ramp-to-zero block, previousOutputValue == 0.
|
||||
#expect(osc.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
// MARK: - GeneratorEnum conformance
|
||||
|
||||
@Test("VectorOscillator GeneratorEnum AudioType / ControlType resolve correctly")
|
||||
func testGeneratorEnumConformance() {
|
||||
// Type-level smoke test — confirms the typealiases compile and resolve.
|
||||
#expect(VectorOscillator.AudioType.self == VectorOscillator.Audio.self)
|
||||
#expect(VectorOscillator.ControlType.self == VectorOscillator.Control.self)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,648 @@
|
||||
// WavetableMorphTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - WavetableMorphTests
|
||||
|
||||
@MainActor
|
||||
@Suite("WavetableMorph")
|
||||
struct WavetableMorphTests {
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test("WavetableMorph.Audio rate is .audio")
|
||||
func testRate() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fillSine()
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
|
||||
#expect(morph.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test("WavetableMorph.Audio produces 64 samples")
|
||||
func testOutputCount() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fillSine()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
|
||||
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test("WavetableMorph.Audio fan-out returns identical first channel")
|
||||
func testFanOut() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fill(partials: 4)
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: 0.5)
|
||||
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
let out2: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
for i: Int in 0..<64 {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - State advances once per block (cache hit on second call)
|
||||
|
||||
@Test("Second calculatedSamples in same segment is a cache hit, does not double-advance phase")
|
||||
func testCacheHitDoesNotDoubleAdvance() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fillSine()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0)
|
||||
|
||||
// First call — computes and caches.
|
||||
let block1: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
// Second call in same segment — must be a cache hit (identical output).
|
||||
let block1b: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(block1[0][i] == block1b[0][i])
|
||||
}
|
||||
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Third call (new block) — phase has advanced, output must differ.
|
||||
let block2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
|
||||
let identical: Bool = zip(Array(block1[0].prefix(512)), Array(block2[0].prefix(512)))
|
||||
.allSatisfy { abs($0 - $1) < 1e-12 }
|
||||
#expect(!identical)
|
||||
}
|
||||
|
||||
// MARK: - position == 0 matches OscTable on tables[0]
|
||||
|
||||
@Test("position == 0 matches OscTable reading tables[0] sample-for-sample")
|
||||
func testPositionZeroMatchesOscTable() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.0)
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
|
||||
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - position == N-1 matches OscTable on tables[N-1]
|
||||
|
||||
@Test("position == N-1 matches OscTable reading tables[N-1] sample-for-sample")
|
||||
func testPositionLastMatchesOscTable() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
let tC: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tC.fill(partials: 16)
|
||||
|
||||
let testFreq: Double = 330.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// N-1 == 2.0 for three tables
|
||||
let lastPosition: Double = 2.0
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB, tC], freq: testFreq, position: lastPosition)
|
||||
let osc: OscTable.Audio = OscTable.Audio(table: tC, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
|
||||
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - position == 0.5 produces average of two single-table renders
|
||||
|
||||
@Test("position == 0.5 between two tables produces average of two OscTable renders")
|
||||
func testMidpointIsAverage() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 220.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.5)
|
||||
let oscA: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
|
||||
let oscB: OscTable.Audio = OscTable.Audio(table: tB, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
let outA: [[Double]] = oscA.calculatedSamples(graph: graph, count: 512)
|
||||
let outB: [[Double]] = oscB.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = (outA[0][i] + outB[0][i]) * 0.5
|
||||
#expect(
|
||||
abs(morphOut[0][i] - expected) < tolerance,
|
||||
"sample \(i): morph=\(morphOut[0][i]) expected=\(expected)"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Position clamped above N-1
|
||||
|
||||
@Test("position clamped above N-1 returns last table only")
|
||||
func testPositionClampAbove() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 110.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// position == 99 far exceeds N-1 == 1; must clamp to tables[1].
|
||||
let morphClamped: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 99.0)
|
||||
let oscLast: OscTable.Audio = OscTable.Audio(table: tB, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morphClamped.calculatedSamples(graph: graph, count: 512)
|
||||
let oscOut: [[Double]] = oscLast.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
|
||||
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Position clamped below 0
|
||||
|
||||
@Test("position clamped below 0 returns tables[0] only")
|
||||
func testPositionClampBelow() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 110.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// position == -5.0 is far below 0; must clamp to tables[0].
|
||||
let morphClamped: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: -5.0)
|
||||
let oscFirst: OscTable.Audio = OscTable.Audio(table: tA, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morphClamped.calculatedSamples(graph: graph, count: 512)
|
||||
let oscOut: [[Double]] = oscFirst.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(morphOut[0][i] - oscOut[0][i]) < tolerance,
|
||||
"sample \(i): morph=\(morphOut[0][i]) osc=\(oscOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Phase modulation: 0.5-cycle offset inverts a sine
|
||||
|
||||
@Test("phase = 0.5 cycles inverts a sine table relative to phase = 0.0")
|
||||
func testPhaseModulationHalfCycleInversion() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tSine: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tSine.fillSine()
|
||||
let tSine2: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tSine2.fillSine()
|
||||
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Both morphers use position == 0 so they read only tables[0].
|
||||
// morphNoPM uses phase == 0.0; morphWithPM uses phase == 0.5 (half-cycle).
|
||||
let morphNoPM: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tSine, tSine2], freq: testFreq, position: 0.0, phase: 0.0)
|
||||
let morphWithPM: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tSine, tSine2], freq: testFreq, position: 0.0, phase: 0.5)
|
||||
|
||||
let outNoPM: [[Double]] = morphNoPM.calculatedSamples(graph: graph, count: 512)
|
||||
let outWithPM: [[Double]] = morphWithPM.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
// Compare against independent OscTable references to confirm sign inversion.
|
||||
// A 0.5-cycle phase shift on a sine table: sin(x + π) = -sin(x).
|
||||
let tolerance: Double = 1e-6
|
||||
for i: Int in 0..<512 {
|
||||
#expect(
|
||||
abs(outNoPM[0][i] + outWithPM[0][i]) < tolerance,
|
||||
"sample \(i): expected sign inversion but noPM=\(outNoPM[0][i]) withPM=\(outWithPM[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - mulAdd
|
||||
|
||||
@Test("WavetableMorph.Audio with mul:2.0, add:1.0 doubles and offsets output")
|
||||
func testMulAdd() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fillSine()
|
||||
|
||||
let testFreq: Double = 440.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morphBase: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: testFreq, position: 0.0)
|
||||
let morphMulAdd: WavetableMorph.Audio = WavetableMorph.Audio(
|
||||
tables: [tA, tB],
|
||||
freq: testFreq,
|
||||
position: 0.0,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
|
||||
let baseOut: [[Double]] = morphBase.calculatedSamples(graph: graph, count: 512)
|
||||
let mulAddOut: [[Double]] = morphMulAdd.calculatedSamples(graph: graph, count: 512)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = baseOut[0][i] * 2.0 + 1.0
|
||||
#expect(
|
||||
abs(mulAddOut[0][i] - expected) < tolerance,
|
||||
"sample \(i): expected \(expected), got \(mulAddOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Position as a generator (no crash, output in range for sine tables)
|
||||
|
||||
@Test("Position driven by a generator does not crash and keeps sine output in [-1, 1]")
|
||||
func testGeneratorPositionNoCrash() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fillSine()
|
||||
|
||||
// LFSawtooth.Control output is in [-1, 1]; after scaling by 0.5 + 0.5 it maps to [0, 1],
|
||||
// which is a valid position range for two tables (N-1 == 1).
|
||||
let posGen: BinaryOpGenerator = LFSawtooth.Control(freq: 2.0) * 0.5 + 0.5
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: posGen)
|
||||
|
||||
// Two blocks — test block-boundary instance reuse per CLAUDE.md feedback memory.
|
||||
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
|
||||
|
||||
let outRange: Double = 1.1 // slight headroom above [-1, 1] for linear interpolation rounding
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out1[0][i] >= -outRange, "block1 sample \(i) below range: \(out1[0][i])")
|
||||
#expect(out1[0][i] <= outRange, "block1 sample \(i) above range: \(out1[0][i])")
|
||||
#expect(out2[0][i] >= -outRange, "block2 sample \(i) below range: \(out2[0][i])")
|
||||
#expect(out2[0][i] <= outRange, "block2 sample \(i) above range: \(out2[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Silence after invalidation
|
||||
|
||||
@Test("WavetableMorph.Audio outputs silence when any table is invalidated")
|
||||
func testInvalidatedTableOutputsSilence() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(tables: [tA, tB], freq: 440.0, position: 0.5)
|
||||
|
||||
// First block — should be non-silent.
|
||||
let before: [[Double]] = morph.calculatedSamples(graph: graph, count: 512)
|
||||
let allZeroBefore: Bool = Array(before[0].prefix(512)).allSatisfy { $0 == 0.0 }
|
||||
#expect(!allZeroBefore)
|
||||
|
||||
tB.invalidate()
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Next block — must be all zeros.
|
||||
let after: [[Double]] = morph.calculatedSamples(graph: graph2, count: 512)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(after[0][i] == 0.0, "sample \(i) expected 0.0 after invalidation, got \(after[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Control: silence after any table is invalidated (including unused one)
|
||||
|
||||
@Test("WavetableMorph.Control outputs silence when any table is invalidated, including an unused one")
|
||||
func testControlInvalidatedTableOutputsSilence() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
let tC: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tC.fill(partials: 16)
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// position: 0.5 → reads between tables[0] and tables[1]; tables[2] is never touched.
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB, tC], freq: 1.0, position: 0.5)
|
||||
|
||||
// First block — should be non-zero (sine table midpoint at phase 0 may start at 0,
|
||||
// so check previousOutputValue after a second block instead).
|
||||
_ = morph.calculatedSamples(graph: graph, count: 64)
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let before: [[Double]] = morph.calculatedSamples(graph: graph2, count: 64)
|
||||
let nonZeroBefore: Bool = (0..<64).contains { before[0][$0] != 0.0 }
|
||||
#expect(nonZeroBefore, "expected non-zero output before invalidation")
|
||||
|
||||
// Invalidate the unused table (tables[2]). The full-scan must detect it.
|
||||
tC.invalidate()
|
||||
advanceBlock(engine)
|
||||
let graph3: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = morph.calculatedSamples(graph: graph3, count: 64)
|
||||
|
||||
// After one ramp-to-zero block, previousOutputValue must be 0.
|
||||
#expect(morph.previousOutputValue == 0.0,
|
||||
"expected previousOutputValue == 0.0 after invalidation, got \(morph.previousOutputValue)")
|
||||
}
|
||||
|
||||
// MARK: - Integration: runs inside Synthesizer wired to engine without crash
|
||||
|
||||
@Test("WavetableMorph.Audio runs inside a Synthesizer without crash")
|
||||
func testIntegration() throws {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
try engine.start()
|
||||
|
||||
final class MorphSynth: SynthesizerBase {
|
||||
let tables: [LyrebirdBuffer]
|
||||
init(tables: [LyrebirdBuffer]) {
|
||||
self.tables = tables
|
||||
super.init()
|
||||
}
|
||||
override func build() {
|
||||
let morph: WavetableMorph.Audio = WavetableMorph.Audio(
|
||||
tables: tables,
|
||||
freq: 440.0,
|
||||
position: 0.5
|
||||
)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: morph))
|
||||
}
|
||||
}
|
||||
|
||||
let synth: MorphSynth = MorphSynth(tables: [tA, tB])
|
||||
try engine.add(synth)
|
||||
|
||||
// Run two blocks via processBlock — the proper engine path that initialises buses.
|
||||
engine.processBlock(inputSamples: [])
|
||||
engine.processBlock(inputSamples: [])
|
||||
|
||||
// Verify the engine wrote non-zero samples to bus 0.
|
||||
let anyNonZero: Bool = engine.audioBuses[0].prefix(512).contains { $0 != 0.0 }
|
||||
#expect(anyNonZero)
|
||||
}
|
||||
|
||||
// MARK: - Control rate
|
||||
|
||||
@Test("WavetableMorph.Control rate is .control")
|
||||
func testControlRate() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fillSine()
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0)
|
||||
#expect(morph.rate == .control)
|
||||
}
|
||||
|
||||
// MARK: - Control output count
|
||||
|
||||
@Test("WavetableMorph.Control produces 64 samples")
|
||||
func testControlOutputCount() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fillSine()
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0)
|
||||
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
}
|
||||
|
||||
// MARK: - Control fan-out cache
|
||||
|
||||
@Test("WavetableMorph.Control fan-out returns identical first channel in same segment")
|
||||
func testControlFanOut() {
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: lyrebirdDefaultTableSize)
|
||||
tB.fill(partials: 4)
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0, position: 0.5)
|
||||
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
let out2: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
for i: Int in 0..<64 {
|
||||
#expect(out1[0][i] == out2[0][i])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Control position == 0 matches OscTable.Control on tables[0]
|
||||
|
||||
@Test("WavetableMorph.Control position==0 matches OscTable.Control reading tables[0]")
|
||||
func testControlPositionZeroMatchesOscTable() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 1.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.0)
|
||||
let osc: OscTable.Control = OscTable.Control(table: tA, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
let oscOut: [[Double]] = osc.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
// Both advance phase by the same amount; last sample of the ramp should match.
|
||||
let tolerance: Double = 1e-9
|
||||
#expect(
|
||||
abs(morphOut[0][63] - oscOut[0][63]) < tolerance,
|
||||
"last sample: morph=\(morphOut[0][63]) osc=\(oscOut[0][63])"
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Control midpoint averages two OscTable.Control renders
|
||||
|
||||
@Test("WavetableMorph.Control position==0.5 target is average of two OscTable.Control targets")
|
||||
func testControlMidpointIsAverage() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 8)
|
||||
|
||||
let testFreq: Double = 2.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.5)
|
||||
let oscA: OscTable.Control = OscTable.Control(table: tA, freq: testFreq)
|
||||
let oscB: OscTable.Control = OscTable.Control(table: tB, freq: testFreq)
|
||||
|
||||
let morphOut: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
let outA: [[Double]] = oscA.calculatedSamples(graph: graph, count: 64)
|
||||
let outB: [[Double]] = oscB.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
// The ramp target is the average; last sample reflects that.
|
||||
let expectedTarget: Double = (outA[0][63] + outB[0][63]) * 0.5
|
||||
let tolerance: Double = 1e-9
|
||||
#expect(
|
||||
abs(morphOut[0][63] - expectedTarget) < tolerance,
|
||||
"last sample: morph=\(morphOut[0][63]) expected=\(expectedTarget)"
|
||||
)
|
||||
}
|
||||
|
||||
// MARK: - Control ramp: last sample equals target
|
||||
|
||||
@Test("WavetableMorph.Control ramp last sample equals the new table-read target")
|
||||
func testControlRamp() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fill(partials: 4)
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
// Use position: 1.0 so the generator reads tables[1] exclusively.
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 0.5, position: 1.0)
|
||||
let out: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
// The last sample of the ramp is previousOutputValue after the block.
|
||||
#expect(out[0][63] == morph.previousOutputValue)
|
||||
}
|
||||
|
||||
// MARK: - Control mulAdd
|
||||
|
||||
@Test("WavetableMorph.Control with mul:2.0, add:1.0 doubles and offsets output")
|
||||
func testControlMulAdd() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fillSine()
|
||||
|
||||
let testFreq: Double = 1.0
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morphBase: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: testFreq, position: 0.0)
|
||||
let morphMulAdd: WavetableMorph.Control = WavetableMorph.Control(
|
||||
tables: [tA, tB],
|
||||
freq: testFreq,
|
||||
position: 0.0,
|
||||
mul: 2.0,
|
||||
add: 1.0
|
||||
)
|
||||
|
||||
let baseOut: [[Double]] = morphBase.calculatedSamples(graph: graph, count: 64)
|
||||
let mulAddOut: [[Double]] = morphMulAdd.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let tolerance: Double = 1e-9
|
||||
for i: Int in 0..<64 {
|
||||
let expected: Double = baseOut[0][i] * 2.0 + 1.0
|
||||
#expect(
|
||||
abs(mulAddOut[0][i] - expected) < tolerance,
|
||||
"sample \(i): expected \(expected), got \(mulAddOut[0][i])"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Control: generator-driven position does not crash
|
||||
|
||||
@Test("WavetableMorph.Control position driven by a generator does not crash")
|
||||
func testControlGeneratorPositionNoCrash() {
|
||||
let tableSize: Int = lyrebirdDefaultTableSize
|
||||
let tA: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tA.fillSine()
|
||||
let tB: LyrebirdBuffer = LyrebirdBuffer(count: tableSize)
|
||||
tB.fillSine()
|
||||
|
||||
// OscSin.Control output mapped to [0, 1] → valid position range for 2 tables.
|
||||
let posGen: BinaryOpGenerator = OscSin.Control(freq: 0.5) * 0.5 + 0.5
|
||||
let engine: SynthesisEngine = makeTestEngine(sampleRate: 44100)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let morph: WavetableMorph.Control = WavetableMorph.Control(tables: [tA, tB], freq: 1.0, position: posGen)
|
||||
|
||||
// Two blocks to test cross-block instance reuse.
|
||||
let out1: [[Double]] = morph.calculatedSamples(graph: graph, count: 64)
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = morph.calculatedSamples(graph: graph2, count: 64)
|
||||
|
||||
// Output must stay in [-1, 1] range (sine tables).
|
||||
let outRange: Double = 1.1
|
||||
for i: Int in 0..<64 {
|
||||
#expect(out1[0][i] >= -outRange && out1[0][i] <= outRange,
|
||||
"block1 sample \(i) out of range: \(out1[0][i])")
|
||||
#expect(out2[0][i] >= -outRange && out2[0][i] <= outRange,
|
||||
"block2 sample \(i) out of range: \(out2[0][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user