v1.7.0 release: amp-modeling core (ToneStack, TriodeStage, LSTMAmp)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
// TriodeStageTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Darwin
|
||||
@testable import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("TriodeStage")
|
||||
struct TriodeStageTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
let sr: Double = 44100.0
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
private func goertzel(_ x: [Double], cyclesPerSample: Double) -> Double {
|
||||
let omega: Double = 2.0 * Double.pi * cyclesPerSample
|
||||
let cosine: Double = cos(omega)
|
||||
let sine: Double = sin(omega)
|
||||
let coeff: Double = 2.0 * cosine
|
||||
var s1: Double = 0.0
|
||||
var s2: Double = 0.0
|
||||
for v: Double in x {
|
||||
let s0: Double = v + coeff * s1 - s2
|
||||
s2 = s1
|
||||
s1 = s0
|
||||
}
|
||||
let real: Double = s1 - s2 * cosine
|
||||
let imag: Double = s2 * sine
|
||||
return (real * real + imag * imag).squareRoot()
|
||||
}
|
||||
|
||||
private func steadyTail(_ gen: any Generator, blocks: Int = 200) -> [Double] {
|
||||
let all: [Double] = collectBlocks(gen, engine: engine, blocks: blocks)
|
||||
return Array(all.suffix(engine.blockSize * 100))
|
||||
}
|
||||
|
||||
@Test func buildsNonSilentAndBounded() {
|
||||
let sine: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.8)
|
||||
let stage: TriodeStage = TriodeStage(input: sine, drive: 2.0, bias: 0.2)
|
||||
let tail: [Double] = steadyTail(stage)
|
||||
#expect(tail.contains { abs($0) > 1.0e-4 }, "TriodeStage was silent")
|
||||
#expect(tail.allSatisfy { $0.isFinite }, "non-finite output")
|
||||
let peak: Double = tail.map { abs($0) }.max() ?? 0
|
||||
#expect(peak <= 1.2, "TriodeStage output not bounded near ±1: peak=\(peak)")
|
||||
}
|
||||
|
||||
@Test func biasAddsEvenHarmonics() {
|
||||
// A symmetric (bias 0) tanh produces only odd harmonics → the 2nd harmonic is ~0.
|
||||
// Grid bias makes the transfer asymmetric → a real 2nd harmonic appears (tube warmth).
|
||||
let fundamental: Double = 1000.0
|
||||
func secondHarmonic(bias: Double) -> Double {
|
||||
let sine: OscSin.Audio = OscSin.Audio(freq: fundamental, mul: 0.8)
|
||||
let stage: TriodeStage = TriodeStage(input: sine, drive: 3.0, bias: bias, oversample: 4)
|
||||
return goertzel(steadyTail(stage), cyclesPerSample: 2.0 * fundamental / sr)
|
||||
}
|
||||
let unbiased: Double = secondHarmonic(bias: 0.0)
|
||||
let biased: Double = secondHarmonic(bias: 0.5)
|
||||
#expect(biased > unbiased * 3.0, "bias did not add 2nd-harmonic content (biased=\(biased) unbiased=\(unbiased))")
|
||||
}
|
||||
|
||||
@Test func couplingBlocksDC() {
|
||||
// The output coupling (DC blocker) must remove the standing offset a biased stage adds
|
||||
// to a constant input — steady output decays to ~0.
|
||||
let dc: ConstantGeneratorInput = ConstantGeneratorInput(0.5)
|
||||
let stage: TriodeStage = TriodeStage(input: dc, drive: 2.0, bias: 0.3)
|
||||
let tail: [Double] = steadyTail(stage, blocks: 300)
|
||||
let meanAbs: Double = tail.reduce(0.0) { $0 + abs($1) } / Double(tail.count)
|
||||
#expect(meanAbs < 1.0e-3, "coupling did not block DC (meanAbs=\(meanAbs))")
|
||||
}
|
||||
|
||||
@Test func oversamplingReducesAliasing() {
|
||||
// 0.3·Fs driven hard: the odd harmonics (3·f0 = 0.9·Fs) fold to a 0.1·Fs alias at base
|
||||
// rate. Measure alias energy relative to the fundamental vs a naive base-rate stage.
|
||||
let fundamental: Double = 0.3 * sr
|
||||
let drive: Double = 3.0
|
||||
let pole: Double = 0.995
|
||||
let fundBin: Double = 0.3
|
||||
let aliasBin: Double = 0.1
|
||||
|
||||
let sine: OscSin.Audio = OscSin.Audio(freq: fundamental, mul: 0.9)
|
||||
let stage: TriodeStage = TriodeStage(input: sine, drive: drive, bias: 0.0, oversample: 4)
|
||||
let osTail: [Double] = steadyTail(stage)
|
||||
let osRatio: Double = goertzel(osTail, cyclesPerSample: aliasBin)
|
||||
/ goertzel(osTail, cyclesPerSample: fundBin)
|
||||
|
||||
// Naive base-rate reference: tanh(drive·x) then the same DC blocker, no oversampling.
|
||||
let cyclesPerSample: Double = fundamental / sr
|
||||
var naive: [Double] = [Double](repeating: 0.0, count: osTail.count)
|
||||
var x1: Double = 0.0
|
||||
var y1: Double = 0.0
|
||||
for n: Int in 0..<naive.count {
|
||||
let x: Double = 0.9 * sin(2.0 * Double.pi * cyclesPerSample * Double(n))
|
||||
let shaped: Double = tanh(drive * x)
|
||||
let y: Double = shaped - x1 + pole * y1
|
||||
x1 = shaped
|
||||
y1 = y
|
||||
naive[n] = y
|
||||
}
|
||||
let naiveRatio: Double = goertzel(naive, cyclesPerSample: aliasBin)
|
||||
/ goertzel(naive, cyclesPerSample: fundBin)
|
||||
|
||||
#expect(naiveRatio > 0.05, "baseline did not alias as expected (ratio=\(naiveRatio))")
|
||||
#expect(osRatio < naiveRatio * 0.5, "oversampling did not reduce aliasing: os=\(osRatio) naive=\(naiveRatio)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user