077a8ad9f0
- Add deterministic generator-level tests: BitReduceTests (level snapping + 1-bit floor), SampleRateDecimateTests (sample-and-hold groups), and ReverseDelayTests (silent-then-flat-replay). Asserts are output-gain independent. - Add class-level `- Parameters:` doc blocks to BitReduce, SampleRateDecimate, and ReverseDelay (dual-location param docs). - BitReduce: clamp the shift amount directly and note shift-safety; clarify mid-tread wording. - ReverseDelay: comment the unreachable baseAddress guard. Suite 247. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
71 lines
2.7 KiB
Swift
71 lines
2.7 KiB
Swift
// SampleRateDecimateTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("SampleRateDecimate")
|
|
struct SampleRateDecimateTests {
|
|
|
|
/// Minimal synth: a sine through a `SampleRateDecimate`, written to a mono bus.
|
|
private final class DecimateSynth: SynthesizerBase {
|
|
private let freq: Double
|
|
private let factor: Double
|
|
private let outBus: Int
|
|
|
|
init(freq: Double, factor: Double, outBus: Int) {
|
|
self.freq = freq
|
|
self.factor = factor
|
|
self.outBus = outBus
|
|
super.init()
|
|
}
|
|
|
|
override func build() {
|
|
let osc: OscSin.Audio = OscSin.Audio(freq: freq)
|
|
output(bus: outBus, source: SampleRateDecimate(input: osc, downsample: factor))
|
|
}
|
|
}
|
|
|
|
private func render(freq: Double, factor: Double) throws -> [Float] {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer { engine.busAllocator.releaseAudioBus(dstBus) }
|
|
let synth: DecimateSynth = DecimateSynth(freq: freq, factor: factor, outBus: dstBus.index)
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
try engine.add(synth)
|
|
try engine.add(master, position: NodePosition.after(synth))
|
|
try engine.start()
|
|
return readStereoCAF(url: url)
|
|
}
|
|
|
|
/// Left-channel frame `i` (interleaved stereo).
|
|
private func left(_ samples: [Float], _ frame: Int) -> Float {
|
|
samples[frame * 2]
|
|
}
|
|
|
|
@Test func holdsEachSampleForTheFactor() throws {
|
|
// Factor 4: frames latch at 0, 4, 8, … and hold for four output samples each.
|
|
let samples: [Float] = try render(freq: 2000.0, factor: 4.0)
|
|
// Group [8, 11] holds the value latched at frame 8 (a non-zero point on the sine).
|
|
#expect(left(samples, 8) == left(samples, 9))
|
|
#expect(left(samples, 8) == left(samples, 10))
|
|
#expect(left(samples, 8) == left(samples, 11))
|
|
// The next group latches a fresh value.
|
|
#expect(left(samples, 8) != left(samples, 12), "decimator did not re-latch at the group boundary")
|
|
}
|
|
|
|
@Test func factorOneIsTransparent() throws {
|
|
let samples: [Float] = try render(freq: 2000.0, factor: 1.0)
|
|
// No two adjacent samples forced equal — a moving sine should differ frame-to-frame.
|
|
#expect(left(samples, 8) != left(samples, 9), "factor 1 should pass the signal through unheld")
|
|
}
|
|
}
|