Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/GraphicEQPedalTests.swift
josh 69f50b2037 Add Limiter, Noise Gate, and Graphic EQ pedals + vertical-fader UI control
Three new pedals:
- LimiterPedal: linear boost gain stage into a LookAheadLimiter brick-wall
  ceiling. Reads engine sampleRate injected by PedalBoard.add at build time.
- NoiseGatePedal: AmplitudeFollower -> hysteretic Schmidt gate -> LinLin floor
  map -> Lag smoothing. Tuneable residual floor when the gate is closed.
- GraphicEQPedal: five cascaded RBJ filters (low/high shelf + three peaking
  mids) with per-band dB gains and a linear master output gain.

New UI control:
- PedalSlider descriptor and Pedal.effectSliders hook, alongside effectKnobs
  and effectSwitches.
- VerticalParameterSlider: a vertical fader bound to a UIParamNode.
- PedalView renders the fader bank and widens the enclosure to fit it.

Persistence/registry:
- PedalState.sliderValues (decodeIfPresent for backward compatibility); capture
  and apply in Pedal+Preset.
- Registry entries and palette ordering for all three pedals.

Tests: non-silent builds, limiter ceiling enforcement, gate open/closed/unity
floor power ratios, EQ flat output, global-gain scaling, and 6-slider preset
round-trip.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 12:36:42 -07:00

111 lines
4.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// GraphicEQPedalTests.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("GraphicEQPedal")
struct GraphicEQPedalTests {
/// Renders a `GraphicEQPedal` after applying `configure`, returning the interleaved output.
private func render(configure: (GraphicEQPedal) -> Void) throws -> [Float] {
let renderDuration: Double = 0.1
let sampleRate: Double = 44100.0
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
sampleRate: sampleRate
)
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: GraphicEQPedal = GraphicEQPedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
configure(pedal)
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, sampleRate: sampleRate)
}
@Test func flatEQProducesNonSilentOutput() throws {
let samples: [Float] = try render { _ in } // all gains 0 dB, unity output
#expect(!samples.isEmpty)
#expect(hasNonSilentOutput(samples), "Flat EQ produced silence")
}
@Test func publishesSixSliders() throws {
let pedal: GraphicEQPedal = GraphicEQPedal()
#expect(pedal.effectSliders.count == 6)
#expect(pedal.effectSliders.last?.label == "Gain")
}
@Test func globalGainScalesOutput() throws {
// The master gain is in dB. +12 dB should be far louder than -12 dB
// (24 dB apart ≈ 15.8× amplitude ≈ 250× power).
let quiet: [Float] = try render { $0.globalGain = -12.0 }
let loud: [Float] = try render { $0.globalGain = 12.0 }
func power(_ samples: [Float]) -> Double {
samples.reduce(0.0) { acc, s in
let v: Double = Double(s)
return acc + v * v
}
}
let quietPower: Double = power(quiet)
let loudPower: Double = power(loud)
#expect(quietPower > 0, "-12 dB EQ was silent")
// Expect well over an order of magnitude more power at +12 dB vs -12 dB.
let minPowerRatio: Double = 50.0
#expect(
loudPower > quietPower * minPowerRatio,
"Master dB gain did not scale output: loud=\(loudPower) quiet=\(quietPower)"
)
}
@Test func sliderValuesRoundTripThroughPresetState() throws {
// Band/global gains live in effectSliders; they must survive captureState/applyState.
let source: GraphicEQPedal = GraphicEQPedal()
source.lowGain = 6.0
source.lowMidGain = -3.0
source.midGain = 4.5
source.highMidGain = -7.0
source.highGain = 2.0
source.globalGain = 1.5
let state: PedalState = source.captureState()
#expect(state.sliderValues.count == 6)
let restored: GraphicEQPedal = GraphicEQPedal()
restored.applyState(state)
let tolerance: Double = 1e-6
#expect(abs(restored.lowGain - 6.0) < tolerance)
#expect(abs(restored.lowMidGain - (-3.0)) < tolerance)
#expect(abs(restored.midGain - 4.5) < tolerance)
#expect(abs(restored.highMidGain - (-7.0)) < tolerance)
#expect(abs(restored.highGain - 2.0) < tolerance)
#expect(abs(restored.globalGain - 1.5) < tolerance)
}
}