// MasterGainMuteTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Master gain and mute") struct MasterGainMuteTests { @Test func defaultMasterGainIsUnity() { let engine: SynthesisEngine = makeTestEngine() #expect(engine.masterGain == 1.0) #expect(engine.isMuted == false) } @Test func masterGainRoundTrips() { let engine: SynthesisEngine = makeTestEngine() engine.masterGain = 0.5 #expect(engine.masterGain == 0.5) engine.masterGain = 2.0 #expect(engine.masterGain == 2.0) } @Test func masterGainRejectsNonFiniteValues() { let engine: SynthesisEngine = makeTestEngine() engine.masterGain = 0.5 engine.masterGain = .nan #expect(engine.masterGain == 0.5) engine.masterGain = .infinity #expect(engine.masterGain == 0.5) engine.masterGain = -.infinity #expect(engine.masterGain == 0.5) } @Test func isMutedRoundTrips() { let engine: SynthesisEngine = makeTestEngine() engine.isMuted = true #expect(engine.isMuted == true) engine.isMuted = false #expect(engine.isMuted == false) } @Test func masterGainScalesOutputBus() throws { let engine: SynthesisEngine = makeTestEngine(blockSize: 64, numOutputChannels: 1, numInputChannels: 0) try engine.add(ConstantSynth(value: 1.0, outputBus: 0)) try engine.start() defer { engine.stop() } // First block: ramp from 1.0 → 1.0 (fast path — vsmul skipped at unity). engine.processBlock(inputSamples: []) // Set target gain and run one block — block ramps 1.0 → 0.5 across N samples // with multipliers `from, from+step, ..., from+(N-1)*step` where // `step = (to - from)/N`. The last multiplier is therefore // `to + (from - to)/N`, i.e. one step shy of the target. The next block's // ramp finishes the convergence. let from: Float = 1.0 let to: Float = 0.5 let n: Float = Float(engine.blockSize) let expectedLast: Float = to + (from - to) / n engine.masterGain = Double(to) engine.processBlock(inputSamples: []) let last: Float = engine.audioBuses[0].last ?? 0 #expect(abs(last - expectedLast) < 1e-5) // Second block at the same target: vsmul fast path applies a flat 0.5. engine.processBlock(inputSamples: []) for s: Float in engine.audioBuses[0] { #expect(abs(s - 0.5) < 1e-5) } } /// Regression test for a `vDSP_vrampmul`-mutates-`*start` bug: channels beyond /// the first must receive the same ramp shape as channel 0, not a flat target. @Test func masterGainRampIsAppliedToAllOutputChannels() throws { let engine: SynthesisEngine = makeTestEngine( blockSize: 64, numOutputChannels: 4, numInputChannels: 0, numAudioBuses: 8 ) try engine.add(ConstantSynth(value: 1.0, outputBus: 0)) try engine.add(ConstantSynth(value: 1.0, outputBus: 1)) try engine.add(ConstantSynth(value: 1.0, outputBus: 2)) try engine.add(ConstantSynth(value: 1.0, outputBus: 3)) try engine.start() defer { engine.stop() } engine.processBlock(inputSamples: []) // prime previousAppliedGain = 1.0 engine.masterGain = 0.5 engine.processBlock(inputSamples: []) // The first sample of every channel should equal `from` (1.0), the last // sample should equal `to + (from - to)/N`. Channel 0 was correct even // under the old buggy code; the canary is channels 1..3. let n: Float = Float(engine.blockSize) let expectedLast: Float = 0.5 + (1.0 - 0.5) / n for ch: Int in 0..