118 lines
4.8 KiB
Swift
118 lines
4.8 KiB
Swift
// SlopeAudioTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("Slope.Audio")
|
|
struct SlopeAudioTests {
|
|
|
|
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: Slope.Audio = Slope.Audio(input: 0.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
/// Output count matches request.
|
|
@Test func outputCountMatchesRequest() {
|
|
let gen: Slope.Audio = Slope.Audio(input: 0.5)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
|
}
|
|
|
|
/// First sample outputs 0.0 (lazy-init — no spike on startup).
|
|
@Test func firstSampleOutputsZero() {
|
|
let gen: Slope.Audio = Slope.Audio(input: 5.0)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(out[0][0] - 0.0) < 1e-12,
|
|
"first sample should be 0.0 (lazy-init), got \(out[0][0])")
|
|
}
|
|
|
|
/// Constant input produces slope 0 after the first sample.
|
|
@Test func constantInputSlopeIsZero() {
|
|
let gen: Slope.Audio = Slope.Audio(input: 3.0)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// Sample 0 = 0 (init). Samples 1+ = (3.0 - 3.0) * sr = 0.
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[0][i] - 0.0) < 1e-12,
|
|
"constant input: slope should be 0 at sample \(i), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Linear ramp input x[i] = i*k → slope = k * sampleRate for samples >= 1.
|
|
///
|
|
/// x[n] = n*k, x[n-1] = (n-1)*k → slope = (n*k - (n-1)*k)*sr = k*sr.
|
|
@Test func linearRampProducesConstantSlope() {
|
|
let k: Double = 0.001 // increment per sample
|
|
let expectedSlope: Double = k * sampleRate
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * k }
|
|
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// Sample 0: lazy-init (output 0).
|
|
#expect(abs(out[0][0] - 0.0) < 1e-12)
|
|
// Samples 1..blockSize-1: slope = k * sampleRate.
|
|
for i: Int in 1..<blockSize {
|
|
#expect(abs(out[0][i] - expectedSlope) < 1e-9,
|
|
"sample \(i): expected slope \(expectedSlope), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Cross-block continuity: prevSample carries across blocks.
|
|
@Test func crossBlockContinuity() {
|
|
// A step function: 0.0 in block 1, 1.0 in block 2.
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
let inputSignal: Closure = Closure { graph, _ in
|
|
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
|
|
}
|
|
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// First sample of block 2: prev = 0.0 (last sample of block 1), x = 1.0.
|
|
// slope = (1.0 - 0.0) * sampleRate.
|
|
let expectedSlope: Double = sampleRate
|
|
#expect(abs(out2[0][0] - expectedSlope) < 1e-9,
|
|
"block 2 sample 0: expected slope \(expectedSlope) (step from 0→1), got \(out2[0][0])")
|
|
}
|
|
|
|
/// Fan-out: two calls same segment return identical first channel.
|
|
@Test func fanOut() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let gen: Slope.Audio = Slope.Audio(input: inputSignal)
|
|
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])
|
|
}
|
|
}
|
|
|
|
/// mul/add: output is original*2+1.
|
|
@Test func mulAdd() {
|
|
let k: Double = 0.001
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * k }
|
|
let genBase: Slope.Audio = Slope.Audio(input: inputSignal)
|
|
let genScaled: Slope.Audio = Slope.Audio(input: inputSignal, 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])")
|
|
}
|
|
}
|
|
}
|