// TunerTests.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("ShoeGaze Tuner") struct TunerTests { private func makeShoeGaze(duration: Double) -> (ShoeGaze, SynthesisEngine, URL) { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 64) let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop) return (shoeGaze, engine, url) } @Test func engageAddsTapAndDisengageRemovesIt() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.2) defer { try? FileManager.default.removeItem(at: url) } let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) #expect(!shoeGaze.tunerEngaged) #expect(shoeGaze.currentPitchHz == nil) shoeGaze.engageTuner() #expect(shoeGaze.tunerEngaged) try engine.start() #expect(shoeGaze.currentPitchHz != nil) shoeGaze.disengageTuner() #expect(!shoeGaze.tunerEngaged) #expect(shoeGaze.currentPitchHz == nil) } @Test func engagingWithMuteOnMutesRigAndDisengageRestores() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.1) defer { try? FileManager.default.removeItem(at: url) } _ = engine let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) shoeGaze.tunerMutesRig = true #expect(!shoeGaze.rigMuted) shoeGaze.engageTuner() #expect(shoeGaze.rigMuted, "Engaging with tunerMutesRig=true should mute the rig") shoeGaze.disengageTuner() #expect(!shoeGaze.rigMuted, "Disengaging should restore the pre-engage mute state") } @Test func disengageRestoresPriorMutedStateNotAlwaysUnmuted() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.1) defer { try? FileManager.default.removeItem(at: url) } _ = engine let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) shoeGaze.tunerMutesRig = true shoeGaze.setRigMuted(true) // already muted before engaging, for an unrelated reason shoeGaze.engageTuner() #expect(shoeGaze.rigMuted) shoeGaze.disengageTuner() #expect(shoeGaze.rigMuted, "Rig was already muted before engaging — disengage must not unmute it") } @Test func tunerMutesRigFalseDoesNotTouchRigMute() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.1) defer { try? FileManager.default.removeItem(at: url) } _ = engine let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) shoeGaze.tunerMutesRig = false shoeGaze.engageTuner() #expect(!shoeGaze.rigMuted) shoeGaze.disengageTuner() #expect(!shoeGaze.rigMuted) } @Test func toggleTunerEngagedFlipsState() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.1) defer { try? FileManager.default.removeItem(at: url) } _ = engine let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) shoeGaze.toggleTunerEngaged() #expect(shoeGaze.tunerEngaged) shoeGaze.toggleTunerEngaged() #expect(!shoeGaze.tunerEngaged) } @Test func engageIsANoOpWhenAlreadyEngaged() throws { let (shoeGaze, engine, url): (ShoeGaze, SynthesisEngine, URL) = makeShoeGaze(duration: 0.1) defer { try? FileManager.default.removeItem(at: url) } _ = engine let board: PedalBoard = try shoeGaze.addBoard() try shoeGaze.finalizeBoard(board) shoeGaze.engageTuner() shoeGaze.tunerMutesRig = false // change after first engage — should have no effect until re-engage shoeGaze.engageTuner() #expect(shoeGaze.rigMuted, "Second engageTuner() call while already engaged must be a no-op") } /// With the rig muted, an engaged tuner must leave the audio output silent. Guards against the /// tuner's pitch-capture signal leaking into a hardware output channel: its scratch bus is a /// *control* bus, but writing it through the audio-rate `output(bus:)` helper would treat that /// index as an *audio* bus (colliding with the low output-bus indices) and, since the tap runs /// after `mainGroup`, bypass the rig mute — audible as crackle while the pitch is unstable. @Test func engagedTunerDoesNotLeakIntoOutput() throws { let renderDuration: Double = 0.4 let sampleRate: Double = 44100.0 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 256) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop) try withExtendedLifetime(shoeGaze) { let board: PedalBoard = try shoeGaze.addBoard() try board.add(VolumePedal()) try shoeGaze.finalizeBoard(board) // Feed the test-loop signal so the pitch tracker produces a real (non-zero) estimate — // the value that would leak if the scratch write hit an audio bus. shoeGaze.setInputMuted(false) shoeGaze.tunerMutesRig = true shoeGaze.engageTuner() #expect(shoeGaze.rigMuted) try engine.start() let samples: [Float] = readStereoCAF(url: url, sampleRate: sampleRate) // Ignore the opening window: the rig-mute gain ramps up over its first blocks (a separate, // pre-existing BoardOutputMaster behaviour), briefly passing the test-loop signal. Once it // settles the output must be exactly silent — the tuner leak, if present, persists for the // whole render (it bypasses the mute), so the settled tail catches it. let channels: Int = 2 let settleSeconds: Double = 0.15 let tailStart: Int = Int(settleSeconds * sampleRate) * channels let tail: [Float] = Array(samples[min(tailStart, samples.count)...]) #expect(!tail.isEmpty) #expect(!hasNonSilentOutput(tail), "once the rig mute settles, an engaged tuner must not leak its pitch signal into the output") } } }