377 lines
16 KiB
Swift
377 lines
16 KiB
Swift
// 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)
|
|
}
|
|
}
|
|
}
|