263 lines
10 KiB
Swift
263 lines
10 KiB
Swift
// TuneablePitchShiftTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("TuneablePitchShift")
|
||
struct TuneablePitchShiftTests {
|
||
|
||
func makeEngine(blockSize: Int = 512) throws -> SynthesisEngine {
|
||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||
try engine.start()
|
||
return engine
|
||
}
|
||
|
||
// MARK: - Silence
|
||
|
||
@Test func silenceInProducesSilence() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.0, pitchRatio: 1.0, seed: 1)
|
||
let out: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
|
||
#expect(out.count == 1, "TuneablePitchShift.Audio should output 1 channel, got \(out.count)")
|
||
let sum: Double = out[0].prefix(512).reduce(0.0, { $0 + abs($1) })
|
||
#expect(sum == 0.0, "Silence in should produce silence out")
|
||
}
|
||
|
||
// MARK: - Non-silent output
|
||
|
||
@Test func nonSilentInputProducesNonSilentOutput() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
// Use a constant input so capture buffer fills with non-zero signal.
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, pitchRatio: 1.0, windowSize: 0.05, overlap: 4, seed: 1)
|
||
|
||
// Run several blocks to fill capture buffer past one window.
|
||
for _ in 0..<20 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let out: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
let sum: Double = out[0].prefix(512).reduce(0.0, { $0 + abs($1) })
|
||
#expect(sum > 0.0, "Non-silent input should produce non-silent output after warm-up")
|
||
}
|
||
|
||
// MARK: - Channel count
|
||
|
||
@Test func outputIsMono() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, seed: 1)
|
||
let out: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
#expect(out.count == 1, "TuneablePitchShift.Audio must output exactly 1 channel")
|
||
}
|
||
|
||
// MARK: - Pitch ratio clamping
|
||
|
||
@Test func pitchRatioAboveMaxClamped() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
// ratio=8.0 should clamp to 4.0 — no crash.
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, pitchRatio: 8.0, windowSize: 0.05, seed: 1)
|
||
#expect(throws: Never.self) {
|
||
for _ in 0..<4 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
}
|
||
|
||
@Test func pitchRatioBelowMinClamped() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
// ratio=0.01 should clamp to 0.25 — no crash.
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, pitchRatio: 0.01, windowSize: 0.05, seed: 1)
|
||
#expect(throws: Never.self) {
|
||
for _ in 0..<4 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Grain pool saturation
|
||
|
||
@Test func grainPoolSaturationNocrash() throws {
|
||
// overlap > pool size: new triggers are silently dropped, no crash.
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(
|
||
input: 0.5,
|
||
pitchRatio: 1.0,
|
||
windowSize: 0.05,
|
||
overlap: lyrebirdGrainPoolSize + 10,
|
||
seed: 1
|
||
)
|
||
#expect(throws: Never.self) {
|
||
for _ in 0..<4 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Pitch dispersion
|
||
|
||
@Test func pitchDispersionIntroducesVariance() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
// Warm up capture buffer with 0.5 signal.
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(
|
||
input: 0.5,
|
||
pitchRatio: 1.0,
|
||
windowSize: 0.05,
|
||
overlap: 4,
|
||
pitchDispersion: 0.5,
|
||
seed: 99
|
||
)
|
||
|
||
var allSame: Bool = true
|
||
var prev: [Double] = []
|
||
for b: Int in 0..<10 {
|
||
let out: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
let cur: [Double] = Array(out[0].prefix(512))
|
||
if b > 0 && cur != prev { allSame = false; break }
|
||
prev = cur
|
||
advanceBlock(engine)
|
||
}
|
||
// With dispersion, consecutive blocks should differ — proves PRNG varies per-grain.
|
||
#expect(!allSame, "pitchDispersion > 0 should produce varying output across blocks")
|
||
}
|
||
|
||
// MARK: - mul / add
|
||
|
||
@Test func mulScalesOutput() throws {
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
let ps1: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, windowSize: 0.05, overlap: 4, seed: 1, mul: 1.0)
|
||
let ps2: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, windowSize: 0.05, overlap: 4, seed: 1, mul: 2.0)
|
||
|
||
for _ in 0..<20 {
|
||
_ = ps1.calculatedSamples(graph: graph, count: 512)
|
||
_ = ps2.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
|
||
let out1: [[Double]] = ps1.calculatedSamples(graph: graph, count: 512)
|
||
let out2: [[Double]] = ps2.calculatedSamples(graph: graph, count: 512)
|
||
let rms1: Double = out1[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
||
let rms2: Double = out2[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
||
|
||
#expect(rms1 > 0, "mul=1 output should have non-zero energy after warm-up")
|
||
if rms1 > 0 {
|
||
// mul=2 → energy is 4× that of mul=1 (within tolerance for floating point).
|
||
let ratio: Double = rms2 / rms1
|
||
#expect(ratio > 3.5 && ratio < 4.5, "mul=2 should quadruple energy, got ratio \(ratio)")
|
||
}
|
||
}
|
||
|
||
// MARK: - Deterministic output
|
||
|
||
@Test func deterministicOutputWithFixedSeed() throws {
|
||
// Fixed seed + constant input must produce bit-identical output across runs.
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
defer { engine.stop() }
|
||
|
||
func run() -> [Double] {
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(input: 0.5, pitchRatio: 1.0,
|
||
windowSize: 0.05, overlap: 4, seed: 0xCAFE_BABE)
|
||
var out: [Double] = []
|
||
// Warm up: fill capture buffer past one window.
|
||
for _ in 0..<12 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
let block: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
out = Array(block[0].prefix(512))
|
||
return out
|
||
}
|
||
|
||
let first: [Double] = run()
|
||
let second: [Double] = run()
|
||
|
||
// Some samples must be non-zero (not silence).
|
||
let nonZero: Bool = first.contains { abs($0) > 1e-9 }
|
||
#expect(nonZero, "Output after warm-up must be non-zero for constant input")
|
||
|
||
// Both runs must match sample-for-sample (fixed seed → deterministic PRNG).
|
||
for i: Int in 0..<512 {
|
||
#expect(first[i] == second[i],
|
||
"Sample \(i) differs across runs: \(first[i]) vs \(second[i])")
|
||
}
|
||
}
|
||
|
||
// MARK: - High pitch ratio read-head safety
|
||
|
||
@Test func highPitchRatioReadsRecentInputNotStaleWrap() throws {
|
||
// Regression: prior to the lookback fix, at pitchRatio > 2 the grain phase
|
||
// overtook the writer mid-grain and wrapped to stale circular content.
|
||
// After the fix the lookback is `pitchRatio * windowSamples` so the grain
|
||
// always trails the writer. With a constant input and overlap = 4 (triangle
|
||
// OLA = 1.0), warmed-up output must equal the input level for every sample.
|
||
let engine: SynthesisEngine = try makeEngine()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
defer { engine.stop() }
|
||
|
||
let inputLevel: Double = 0.5
|
||
let ps: TuneablePitchShift.Audio = TuneablePitchShift.Audio(
|
||
input: inputLevel,
|
||
pitchRatio: 4.0,
|
||
windowSize: 0.05,
|
||
overlap: 4,
|
||
seed: 7
|
||
)
|
||
|
||
// Warm up long enough for the largest possible lookback (4 × windowSize).
|
||
for _ in 0..<20 {
|
||
_ = ps.calculatedSamples(graph: graph, count: 512)
|
||
advanceBlock(engine)
|
||
}
|
||
let out: [[Double]] = ps.calculatedSamples(graph: graph, count: 512)
|
||
let block: [Double] = Array(out[0].prefix(512))
|
||
|
||
// Constant input × triangle OLA at overlap=4 → near-constant output equal to
|
||
// input × OLA gain (≈ 2.0 for triangle window at hop = window/4). The buggy
|
||
// version returned ~0 for a fraction of samples (stale wrap hit zero-initialized
|
||
// regions), so avg dropped well below the OLA gain and minSample dipped toward 0.
|
||
let olaGain: Double = 2.0
|
||
let expectedLevel: Double = inputLevel * olaGain
|
||
let avg: Double = block.reduce(0, +) / Double(block.count)
|
||
#expect(abs(avg - expectedLevel) < 0.1,
|
||
"Avg output \(avg) must be near \(expectedLevel) at pitchRatio=4")
|
||
|
||
// No sample should drop far below half of the expected level — that would
|
||
// indicate the grain read into stale circular content.
|
||
let minSample: Double = block.min() ?? 0
|
||
#expect(minSample > expectedLevel * 0.5,
|
||
"Min output \(minSample) must stay near \(expectedLevel) — stale-wrap reads would drive it toward 0")
|
||
}
|
||
}
|