166 lines
6.9 KiB
Swift
166 lines
6.9 KiB
Swift
// MasterGainMuteTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@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..<engine.numOutputChannels {
|
|
let bus: [Float] = engine.audioBuses[ch]
|
|
#expect(abs((bus.first ?? 0) - 1.0) < 1e-5,
|
|
"channel \(ch) start sample wrong")
|
|
#expect(abs((bus.last ?? 0) - expectedLast) < 1e-5,
|
|
"channel \(ch) end sample wrong — vrampmul start may have leaked across channels")
|
|
}
|
|
}
|
|
|
|
@Test func muteZerosOutputAfterRamp() 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() }
|
|
engine.processBlock(inputSamples: []) // prime — pre-applied gain is now 1.0
|
|
engine.isMuted = true
|
|
engine.processBlock(inputSamples: []) // ramp 1.0 → 0.0 across this block
|
|
engine.processBlock(inputSamples: []) // flat 0.0 — vsmul fast path
|
|
for s: Float in engine.audioBuses[0] {
|
|
#expect(s == 0.0)
|
|
}
|
|
}
|
|
|
|
@Test func masterGainDoesNotTouchInputBuses() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(
|
|
blockSize: 64,
|
|
numOutputChannels: 1,
|
|
numInputChannels: 1,
|
|
numAudioBuses: 8
|
|
)
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
engine.masterGain = 0.25
|
|
let input: [[Float]] = [[Float](repeating: 1.0, count: 64)]
|
|
engine.processBlock(inputSamples: input)
|
|
// Bus 0 is the (silent) output, scaled — still 0. Bus 1 is the input copy,
|
|
// which must NOT be scaled by master gain.
|
|
let inputBus: [Float] = engine.audioBuses[engine.numOutputChannels]
|
|
for s: Float in inputBus {
|
|
#expect(s == 1.0)
|
|
}
|
|
}
|
|
|
|
/// A stop→start cycle that ended muted must not replay the stale ramp on
|
|
/// the first block after restart — `_previousAppliedGain` is reseeded in
|
|
/// `start()` so the first new block is a flat multiply at the live target.
|
|
///
|
|
/// `stop()` tears down all non-persistent nodes, so the test re-adds the
|
|
/// signal source after the second `start()`.
|
|
@Test func restartAfterMuteDoesNotRampFromStaleValue() throws {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, numOutputChannels: 1, numInputChannels: 0)
|
|
try engine.add(ConstantSynth(value: 1.0, outputBus: 0))
|
|
try engine.start()
|
|
engine.isMuted = true
|
|
engine.processBlock(inputSamples: []) // ramps 1.0 → 0.0
|
|
engine.processBlock(inputSamples: []) // flat 0.0
|
|
engine.stop()
|
|
engine.isMuted = false
|
|
engine.masterGain = 0.5
|
|
try engine.add(ConstantSynth(value: 1.0, outputBus: 0))
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
engine.processBlock(inputSamples: [])
|
|
// Without the reseed in `start()`, this block would ramp from a stale
|
|
// 0.0 up to 0.5 — the first sample would be 0.0. With the reseed it is
|
|
// a flat multiply at 0.5.
|
|
for s: Float in engine.audioBuses[0] {
|
|
#expect(abs(s - 0.5) < 1e-5)
|
|
}
|
|
}
|
|
}
|