prep for 1.2.0 release updates

This commit is contained in:
2026-06-21 11:45:58 -07:00
parent 73191c958f
commit 032b7c39d3
286 changed files with 21116 additions and 121 deletions
@@ -0,0 +1,321 @@
// FrequencyShifterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("FrequencyShifter")
struct FrequencyShifterTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let testSampleRate: Double = 44100
init() throws {
try engine.start()
}
// MARK: - 1. Rate
@Test func rateIsAudio() {
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: 0.0,
sampleRate: testSampleRate
)
#expect(shifter.rate == .audio)
}
// MARK: - 2. Two-channel output count
@Test func outputCountTwoChannels() {
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(0.5),
shiftHz: 100.0,
sampleRate: testSampleRate
)
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 2, "FrequencyShifter.Audio must produce exactly 2 output channels")
#expect(Array(out[0].prefix(64)).count == 64, "Upper sideband must have 64 samples")
#expect(Array(out[1].prefix(64)).count == 64, "Lower sideband must have 64 samples")
}
// MARK: - 3. Fan-out
@Test func fanOutSingleEvaluation() {
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(0.3),
shiftHz: 50.0,
sampleRate: testSampleRate
)
let out1: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let out2: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let s1Upper: [Double] = Array(out1[0].prefix(512))
let s2Upper: [Double] = Array(out2[0].prefix(512))
let s1Lower: [Double] = Array(out1[1].prefix(512))
let s2Lower: [Double] = Array(out2[1].prefix(512))
#expect(s1Upper == s2Upper,
"Fan-out: upper sideband must be identical on second call in same segment")
#expect(s1Lower == s2Lower,
"Fan-out: lower sideband must be identical on second call in same segment")
}
// MARK: - 4. State advances once per block (cache hit does not double-advance)
@Test func stateAdvancesOncePerBlock() {
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(0.3),
shiftHz: 100.0,
sampleRate: testSampleRate
)
_ = shifter.calculatedSamples(graph: graph, count: 512)
_ = shifter.calculatedSamples(graph: graph, count: 512) // cache hit
advanceBlock(engine)
let block2: [[Double]] = shifter.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let allFiniteUpper: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite }
let allFiniteLower: Bool = Array(block2[1].prefix(512)).allSatisfy { $0.isFinite }
#expect(allFiniteUpper, "Upper sideband must be finite after block advance")
#expect(allFiniteLower, "Lower sideband must be finite after block advance")
}
// MARK: - 5. Deterministic first sample
@Test func firstSampleUpperEqualsLowerAtZeroShift() {
// Structural invariant: at θ = 0 (first sample with shiftHz = 0), cos(θ) = 1 and
// sin(θ) = 0 so the SSB matrix collapses to upper = lower = xA. Both output channels
// must match on sample 0 regardless of the underlying Hilbert cascade values.
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(1.0),
shiftHz: 0.0,
sampleRate: testSampleRate
)
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 1)
#expect(out[0][0] == out[1][0],
"At θ = 0 with zero shift, upper and lower outputs must match on sample 0")
#expect(out[0][0].isFinite)
}
@Test func nonZeroShiftProducesAsymmetricSidebands() {
// After a long enough render with shiftHz ≠ 0, the SSB matrix should diverge upper
// and lower outputs (the cos·sin asymmetry exercised). Verifies the SSB sign
// convention — a sign flip on the xB·sin term would leave both rows equal at every
// sample, not just at θ = 0.
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: OscSin.Audio(freq: 1_000.0),
shiftHz: 200.0,
sampleRate: testSampleRate
)
// Settle Hilbert IIRs so the shifter is in steady state.
for _: Int in 0..<32 {
_ = shifter.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = shifter.calculatedSamples(
graph: makeSynthGraph(engine: engine), count: 512
)
var anyDiff: Bool = false
for i: Int in 0..<512 {
if abs(out[0][i] - out[1][i]) > 1e-6 { anyDiff = true; break }
}
#expect(anyDiff,
"Non-zero shift: upper and lower sidebands must diverge once steady state reached")
}
// MARK: - 6. Zero shift — pass-through average
@Test func zeroShiftPassthrough() {
// With shiftHz=0: cos(θ)=1, sin(θ)=0 for all samples (phase stays at 0).
// upper = xA·1 + xB·0 = xA, lower = xA·1 − xB·0 = xA.
// Both channels equal path A output. Average of upper+lower = xA = path A output.
// After filter group delay, output should be non-zero and finite for a sine input.
let input: OscSin.Audio = OscSin.Audio(freq: 440.0)
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: input,
shiftHz: 0.0,
sampleRate: testSampleRate
)
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
// Both channels should be identical (xA·1 ± xB·0 = xA).
let allMatch: Bool = zip(upper, lower).allSatisfy { abs($0 - $1) < 1e-10 }
#expect(allMatch, "With shiftHz=0, upper and lower sidebands must be identical")
// Output should be non-trivial (filter introduces group delay but passes the sine).
let rmsUpper: Double = rmsValue(upper)
#expect(rmsUpper > 0.01, "Non-silent sine input with zero shift must produce non-trivial output")
}
// MARK: - 7. DC input → quadrature oscillator visible
@Test func dcInputProducesOscillatorOutput() {
// With DC input (constant 1.0): after the all-pass filters settle, xA ≈ xB ≈ 1.
// upper ≈ cos(θ) − sin(θ), lower ≈ cos(θ) + sin(θ).
// Both channels should be oscillating (non-DC) after many blocks.
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(1.0),
shiftHz: 400.0,
sampleRate: testSampleRate
)
// Let the Hilbert filters settle.
for _ in 0..<20 {
_ = shifter.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
let out: [[Double]] = shifter.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
// Compute RMS — a constant output would have very low AC content.
// An oscillating signal at 400 Hz will have significant variation.
let maxUpper: Double = upper.max() ?? 0
let minUpper: Double = upper.min() ?? 0
let rangeUpper: Double = maxUpper - minUpper
#expect(rangeUpper > 0.5, "DC input with 400 Hz shift should produce oscillating upper sideband")
let maxLower: Double = lower.max() ?? 0
let minLower: Double = lower.min() ?? 0
let rangeLower: Double = maxLower - minLower
#expect(rangeLower > 0.5, "DC input with 400 Hz shift should produce oscillating lower sideband")
}
// MARK: - 8. Audio-rate shift input
@Test func audioRateShiftInput() {
// Drive shiftHz with an OscSin generator — verify output is non-trivial and non-NaN.
let shiftMod: OscSin.Audio = OscSin.Audio(freq: 2.0) // 2 Hz LFO
let input: OscSin.Audio = OscSin.Audio(freq: 440.0)
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: input,
shiftHz: shiftMod,
sampleRate: testSampleRate
)
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
let allFiniteUpper: Bool = upper.allSatisfy { $0.isFinite }
let allFiniteLower: Bool = lower.allSatisfy { $0.isFinite }
#expect(allFiniteUpper, "Audio-rate shift: upper sideband must not contain NaN/Inf")
#expect(allFiniteLower, "Audio-rate shift: lower sideband must not contain NaN/Inf")
let rmsUpper: Double = rmsValue(upper)
#expect(rmsUpper > 0.001, "Audio-rate shift: upper sideband must be non-trivial")
}
// MARK: - 9. mulAdd applied
@Test func mulAddApplied() {
// Zero input → zero output for both sidebands.
// With mul:2, add:1 → output must be 1.0 for all samples.
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: 0.0,
shiftHz: 100.0,
sampleRate: testSampleRate,
mul: 2.0,
add: 1.0
)
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
let allNearOneUpper: Bool = upper.allSatisfy { abs($0 - 1.0) < 1e-10 }
let allNearOneLower: Bool = lower.allSatisfy { abs($0 - 1.0) < 1e-10 }
#expect(allNearOneUpper, "Upper sideband: mul:2 add:1 on zero input must produce 1.0")
#expect(allNearOneLower, "Lower sideband: mul:2 add:1 on zero input must produce 1.0")
}
// MARK: - 10. Denormal safety
@Test func denormalSafe() {
// Feed a tiny sub-normal-adjacent input for many blocks — verify no NaN or Inf.
let tinyInput: Double = 1e-300
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: ConstantGeneratorInput(tinyInput),
shiftHz: 200.0,
sampleRate: testSampleRate
)
for _ in 0..<1000 {
let out: [[Double]] = shifter.calculatedSamples(graph: graph, count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
let upperOk: Bool = upper.allSatisfy { $0.isFinite }
let lowerOk: Bool = lower.allSatisfy { $0.isFinite }
if !upperOk || !lowerOk {
#expect(Bool(false), "Denormal input must not produce NaN/Inf in output")
break
}
advanceBlock(engine)
}
}
// MARK: - 11. Channel aliases
@Test func channelAliasesMatchSubscripts() {
let input: OscSin.Audio = OscSin.Audio(freq: 220.0)
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: input,
shiftHz: 50.0,
sampleRate: testSampleRate
)
let upperOut: [[Double]] = shifter.upper.calculatedSamples(graph: graph, count: 64)
let lowerOut: [[Double]] = shifter.lower.calculatedSamples(graph: graph, count: 64)
let fullOut: [[Double]] = shifter.calculatedSamples(graph: graph, count: 64)
for i: Int in 0..<64 {
#expect(abs(upperOut[0][i] - fullOut[0][i]) < 1e-10,
"upper alias must match channel 0 at index \(i)")
#expect(abs(lowerOut[0][i] - fullOut[1][i]) < 1e-10,
"lower alias must match channel 1 at index \(i)")
}
}
// MARK: - 12. Integration with engine
@Test func integrationWithEngine() throws {
final class FreqShiftSynth: SynthesizerBase {
override func build() {
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: OscSin.Audio(freq: 440.0),
shiftHz: 100.0,
sampleRate: 44100
)
addOutputGenerator(Out.Audio(bus: 0, source: shifter.upper))
addOutputGenerator(Out.Audio(bus: 1, source: shifter.lower))
}
}
let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
try intEngine.start()
let synth: FreqShiftSynth = FreqShiftSynth()
try intEngine.add(synth)
for _ in 0..<4 {
intEngine.processBlock(inputSamples: [])
}
intEngine.stop()
}
// MARK: - 13. Upper and lower sidebands differ (non-trivial signal)
@Test func upperAndLowerSidebandsAreDifferent() {
let input: OscSin.Audio = OscSin.Audio(freq: 440.0)
let shifter: FrequencyShifter.Audio = FrequencyShifter.Audio(
input: input,
shiftHz: 200.0,
sampleRate: testSampleRate
)
// Let the Hilbert filters settle so path A and B outputs diverge.
for _ in 0..<10 {
_ = shifter.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
let out: [[Double]] = shifter.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let upper: [Double] = Array(out[0].prefix(512))
let lower: [Double] = Array(out[1].prefix(512))
// upper = xA·cos + xB·sin, lower = xA·cos − xB·sin.
// They differ when xB·sin ≠ 0, i.e. once the Hilbert filter is producing output.
let anyDiff: Bool = zip(upper, lower).contains { abs($0 - $1) > 1e-6 }
#expect(anyDiff, "Upper and lower sidebands must differ once the Hilbert filter is active")
}
}