102 lines
4.2 KiB
Swift
102 lines
4.2 KiB
Swift
// CompressorPedalTests.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("CompressorPedal")
|
|
struct CompressorPedalTests {
|
|
|
|
@Test func buildsAndProducesNonSilentOutput() throws {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer {
|
|
engine.busAllocator.releaseAudioBus(srcBus)
|
|
engine.busAllocator.releaseAudioBus(dstBus)
|
|
}
|
|
|
|
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
|
|
let pedal: CompressorPedal = CompressorPedal(
|
|
inputBus: srcBus.index,
|
|
outputBus: dstBus.index
|
|
)
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
|
|
try engine.add(source)
|
|
try engine.add(pedal, position: NodePosition.after(source))
|
|
try engine.add(master, position: NodePosition.after(pedal))
|
|
try engine.start()
|
|
|
|
let samples: [Float] = readStereoCAF(url: url)
|
|
#expect(!samples.isEmpty)
|
|
#expect(hasNonSilentOutput(samples), "CompressorPedal produced silence")
|
|
}
|
|
|
|
@Test func ratioChangesOutput() throws {
|
|
// A high-ratio render should differ from a low-ratio render (bypass-equivalent).
|
|
let renderDuration: Double = 0.1
|
|
let numAudioBuses: Int = 64
|
|
|
|
func renderWithRatio(_ ratioValue: Double) throws -> [Float] {
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
numAudioBuses: numAudioBuses
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer {
|
|
engine.busAllocator.releaseAudioBus(srcBus)
|
|
engine.busAllocator.releaseAudioBus(dstBus)
|
|
}
|
|
|
|
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
|
|
let pedal: CompressorPedal = CompressorPedal(
|
|
inputBus: srcBus.index,
|
|
outputBus: dstBus.index
|
|
)
|
|
// Use a low threshold so the test signal (peak ~0.25 linear ≈ -12 dB) is
|
|
// well above it, ensuring ratio differences produce measurable gain changes.
|
|
pedal.threshold = -24.0
|
|
pedal.ratio = ratioValue
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
try engine.add(source)
|
|
try engine.add(pedal, position: NodePosition.after(source))
|
|
try engine.add(master, position: NodePosition.after(pedal))
|
|
try engine.start()
|
|
return readStereoCAF(url: url)
|
|
}
|
|
|
|
let lowRatioSamples: [Float] = try renderWithRatio(1.0)
|
|
let highRatioSamples: [Float] = try renderWithRatio(20.0)
|
|
|
|
#expect(hasNonSilentOutput(lowRatioSamples), "ratio=1 render was silent")
|
|
#expect(hasNonSilentOutput(highRatioSamples), "ratio=20 render was silent")
|
|
|
|
let skipSamples: Int = 1024
|
|
let windowSize: Int = 2048
|
|
guard lowRatioSamples.count >= skipSamples + windowSize,
|
|
highRatioSamples.count >= skipSamples + windowSize else { return }
|
|
|
|
let lowWindow: [Float] = Array(lowRatioSamples[skipSamples..<(skipSamples + windowSize)])
|
|
let highWindow: [Float] = Array(highRatioSamples[skipSamples..<(skipSamples + windowSize)])
|
|
let ssd: Double = zip(lowWindow, highWindow).reduce(0.0) { acc, pair in
|
|
let diff: Double = Double(pair.0 - pair.1)
|
|
return acc + diff * diff
|
|
}
|
|
#expect(ssd > 0, "ratio=1 and ratio=20 produced identical output — ratio knob has no effect")
|
|
}
|
|
}
|