231 lines
8.3 KiB
Swift
231 lines
8.3 KiB
Swift
// BowedStringTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("BowedString")
|
||
struct BowedStringTests {
|
||
|
||
// MARK: - Test fixture
|
||
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
let testSampleRate: Double = 44100
|
||
let testMaxDelayTime: Double = 0.05
|
||
|
||
init() throws {
|
||
try engine.start()
|
||
}
|
||
|
||
// MARK: - Rate
|
||
|
||
@Test func rateIsAudio() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
#expect(bowed.rate == .audio)
|
||
}
|
||
|
||
// MARK: - Output count
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out: [[Double]] = bowed.calculatedSamples(graph: graph, count: 512)
|
||
#expect(out.count >= 1)
|
||
#expect(out[0].count >= 512)
|
||
}
|
||
|
||
// MARK: - Silent at zero excitation
|
||
|
||
@Test func silentAtZeroExcitation() {
|
||
// With bowVelocity = 0 and zero-initialised delay lines, output must be near zero.
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
bowVelocity: 0.0,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out: [[Double]] = bowed.calculatedSamples(graph: graph, count: 512)
|
||
let samples: [Double] = Array(out[0].prefix(512))
|
||
let allNearZero: Bool = samples.allSatisfy { abs($0) < 1e-6 }
|
||
#expect(allNearZero, "Output must be near zero with bowVelocity = 0")
|
||
}
|
||
|
||
// MARK: - Non-silent with excitation
|
||
|
||
@Test func nonSilentWithExcitation() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: 0.5,
|
||
bowForce: 3.0,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
// Render several blocks to allow waveguide to build up.
|
||
var lastSamples: [Double] = []
|
||
for _ in 0..<10 {
|
||
let out: [[Double]] = bowed.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||
lastSamples = Array(out[0].prefix(512))
|
||
advanceBlock(engine)
|
||
}
|
||
let rms: Double = rmsValue(lastSamples)
|
||
#expect(rms > 0, "BowedString must produce non-zero output with bowVelocity > 0")
|
||
#expect(lastSamples.allSatisfy { $0.isFinite }, "All samples must be finite")
|
||
}
|
||
|
||
// MARK: - Fan-out single evaluation
|
||
|
||
@Test func fanOutSingleEvaluation() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let out1: [[Double]] = bowed.calculatedSamples(graph: graph, count: 512)
|
||
let out2: [[Double]] = bowed.calculatedSamples(graph: graph, count: 512)
|
||
let s1: [Double] = Array(out1[0].prefix(512))
|
||
let s2: [Double] = Array(out2[0].prefix(512))
|
||
#expect(s1 == s2, "Fan-out: two calculatedSamples calls in same segment must return identical data")
|
||
}
|
||
|
||
// MARK: - State advances across blocks
|
||
|
||
@Test func stateAdvancesAcrossBlocks() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
bowVelocity: 0.5,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
_ = bowed.calculatedSamples(graph: graph, count: 512)
|
||
_ = bowed.calculatedSamples(graph: graph, count: 512) // cache hit
|
||
advanceBlock(engine)
|
||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||
let block2: [[Double]] = bowed.calculatedSamples(graph: graph2, count: 512)
|
||
let allFinite: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite }
|
||
#expect(allFinite, "All output samples must be finite after block advance")
|
||
}
|
||
|
||
// MARK: - mulAdd applied
|
||
|
||
@Test func mulAddApplied() {
|
||
let bowed1: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: ConstantGeneratorInput(0.5),
|
||
bowForce: ConstantGeneratorInput(3.0),
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let bowed2: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: ConstantGeneratorInput(0.5),
|
||
bowForce: ConstantGeneratorInput(3.0),
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime,
|
||
mul: 2.0,
|
||
add: 1.0
|
||
)
|
||
let out1: [Double] = Array(bowed1.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||
let out2: [Double] = Array(bowed2.calculatedSamples(graph: graph, count: 512)[0].prefix(512))
|
||
var relationshipHolds: Bool = true
|
||
for i: Int in 0..<512 {
|
||
let expected: Double = out1[i] * 2.0 + 1.0
|
||
if abs(out2[i] - expected) > 1e-10 {
|
||
relationshipHolds = false
|
||
break
|
||
}
|
||
}
|
||
#expect(relationshipHolds, "mul:2.0 add:1.0 must produce output × 2 + 1")
|
||
}
|
||
|
||
// MARK: - Integration with engine
|
||
|
||
@Test func integrationWithEngine() throws {
|
||
final class BowedSynth: SynthesizerBase {
|
||
override func build() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: 0.5,
|
||
sampleRate: 44100,
|
||
maxDelayTime: 0.05
|
||
)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: bowed))
|
||
}
|
||
}
|
||
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||
try intEngine.start()
|
||
let synth: BowedSynth = BowedSynth()
|
||
try intEngine.add(synth)
|
||
for _ in 0..<4 {
|
||
intEngine.processBlock(inputSamples: [])
|
||
}
|
||
intEngine.stop()
|
||
}
|
||
|
||
// MARK: - Finite after many blocks
|
||
|
||
@Test func finiteAfterManyBlocks() {
|
||
let bowed: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: 0.5,
|
||
bowForce: 3.0,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let blockCount: Int = 200
|
||
var allFinite: Bool = true
|
||
for _ in 0..<blockCount {
|
||
let out: [[Double]] = bowed.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
|
||
let samples: [Double] = Array(out[0].prefix(512))
|
||
if !samples.allSatisfy({ $0.isFinite }) {
|
||
allFinite = false
|
||
break
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(allFinite, "BowedString must produce finite output for 200 blocks")
|
||
}
|
||
|
||
// MARK: - Bow position affects tone
|
||
|
||
@Test func bowPositionAffectsTone() {
|
||
// Warm up two instances with different bow positions; output must differ.
|
||
let bowed1: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: 0.5,
|
||
bowPosition: 0.05,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
let bowed2: BowedString.Audio = BowedString.Audio(
|
||
freq: 220.0,
|
||
bowVelocity: 0.5,
|
||
bowPosition: 0.4,
|
||
sampleRate: testSampleRate,
|
||
maxDelayTime: testMaxDelayTime
|
||
)
|
||
|
||
// Run warm-up blocks to let waveguides develop.
|
||
let warmupBlocks: Int = 20
|
||
for b: Int in 0..<warmupBlocks {
|
||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||
_ = bowed1.calculatedSamples(graph: g, count: 512)
|
||
_ = bowed2.calculatedSamples(graph: g, count: 512)
|
||
advanceBlock(engine)
|
||
_ = b
|
||
}
|
||
let gFinal: SynthGraph = makeSynthGraph(engine: engine)
|
||
let out1: [Double] = Array(bowed1.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||
let out2: [Double] = Array(bowed2.calculatedSamples(graph: gFinal, count: 512)[0].prefix(512))
|
||
let identical: Bool = out1 == out2
|
||
#expect(!identical, "Different bowPosition values must produce different output buffers")
|
||
}
|
||
}
|