// VoicePoolTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - Mock synthesizer /// Simple SynthesizerBase subclass used as a voice in pool tests. /// Writes a constant to bus 0 and exposes `amp` and `freq` as `@LiveParam`. private final class MockVoiceSynth: SynthesizerBase { @LiveParam var freq: Double = 440.0 @LiveParam var amp: Double = 0.0 override func build() { output(bus: 0, source: $freq * $amp) } } /// SynthesizerBase subclass with no amplitude-related @LiveParam — used to verify /// that the `.quietest` strategy falls back to `.oldest` when no amp param is found. private final class NoAmpSynth: SynthesizerBase { @LiveParam var freq: Double = 440.0 override func build() { output(bus: 0, source: $freq) } } // MARK: - Helpers private func makePoolEngine() -> SynthesisEngine { makeTestEngine(blockSize: 64, numAudioBuses: 16) } // MARK: - Tests @Suite("VoicePool") struct VoicePoolTests { // MARK: - Init @Test func initCreatesRequestedVoiceCount() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) #expect(pool.voices.count == 4) } @Test func initThrowsOnZeroVoiceCount() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) #expect(throws: VoicePoolError.self) { _ = try VoicePool( voiceCount: 0, group: group, engine: engine, factory: { MockVoiceSynth() } ) } } @Test func initThrowsOnNegativeVoiceCount() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) #expect(throws: VoicePoolError.self) { _ = try VoicePool( voiceCount: -1, group: group, engine: engine, factory: { MockVoiceSynth() } ) } } @Test func singleVoicePoolIsValid() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 1, group: group, engine: engine, factory: { MockVoiceSynth() } ) #expect(pool.voices.count == 1) } // MARK: - noteOn — basic allocation @Test func noteOnReturnsSynthesizerInstance() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) let voice: MockVoiceSynth = pool.noteOn(note: 60) #expect(pool.voices.contains(where: { $0 === voice })) } @Test func consecutiveNoteOnsReturnDifferentVoices() throws { // Each new note-on should pick a different (free) voice while capacity remains. let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) let v1: MockVoiceSynth = pool.noteOn(note: 60) let v2: MockVoiceSynth = pool.noteOn(note: 62) let v3: MockVoiceSynth = pool.noteOn(note: 64) #expect(v1 !== v2) #expect(v2 !== v3) #expect(v1 !== v3) } @Test func sameNoteOnTwiceUsesDistinctVoices() throws { // Two simultaneous note-ons for the same MIDI note must use separate voices (true poly). let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) let first: MockVoiceSynth = pool.noteOn(note: 60) let second: MockVoiceSynth = pool.noteOn(note: 60) #expect(first !== second) } // MARK: - noteOff @Test func noteOffFreesVoice() throws { // After noteOff, noteOn for a new pitch should be able to reuse the freed voice. let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let voiceCount: Int = 2 let pool: VoicePool = try VoicePool( voiceCount: voiceCount, group: group, engine: engine, factory: { MockVoiceSynth() } ) // Fill all voices. let v1: MockVoiceSynth = pool.noteOn(note: 60) let v2: MockVoiceSynth = pool.noteOn(note: 62) // Release note 60. pool.noteOff(note: 60) // New note-on should reuse v1 (now free) rather than stealing v2. let v3: MockVoiceSynth = pool.noteOn(note: 64) #expect(v3 === v1, "Pool should reuse the freed voice rather than stealing the still-active one") _ = v2 // suppress unused warning } @Test func noteOffForNonPlayingNoteDoesNotCrash() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) // Should not throw or crash — note 99 was never started. pool.noteOff(note: 99) } @Test func noteOffOnEmptyPoolDoesNotCrash() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) pool.noteOff(note: 60) } // MARK: - allNotesOff @Test func allNotesOffFreesAllVoices() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let voiceCount: Int = 3 let pool: VoicePool = try VoicePool( voiceCount: voiceCount, group: group, engine: engine, factory: { MockVoiceSynth() } ) // Activate all voices. _ = pool.noteOn(note: 60) _ = pool.noteOn(note: 62) _ = pool.noteOn(note: 64) pool.allNotesOff() // Each subsequent note-on should get a free voice (no steal needed). // We verify by checking that the pool allocates 3 distinct voices for 3 notes, // starting from fresh free state. let a: MockVoiceSynth = pool.noteOn(note: 70) let b: MockVoiceSynth = pool.noteOn(note: 71) let c: MockVoiceSynth = pool.noteOn(note: 72) #expect(a !== b) #expect(b !== c) #expect(a !== c) } @Test func allNotesOffOnIdlePoolDoesNotCrash() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) pool.allNotesOff() } // MARK: - Steal: .oldest @Test func oldestStrategyStealsMostRecentlyActivatedFirst() throws { // With 2 voices, activate v1 (note 60) then v2 (note 62). // A third note-on should steal v1 (oldest trigger serial). let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 2, group: group, engine: engine, stealStrategy: .oldest, factory: { MockVoiceSynth() } ) let v1: MockVoiceSynth = pool.noteOn(note: 60) let v2: MockVoiceSynth = pool.noteOn(note: 62) // Pool is full — next note-on steals oldest (v1). let v3: MockVoiceSynth = pool.noteOn(note: 64) #expect(v3 === v1, ".oldest should steal the earliest-triggered voice (v1)") _ = v2 } @Test func oldestStrategySequentialSteals() throws { // With 2 voices, fill then steal repeatedly to confirm oldest is always chosen. let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 2, group: group, engine: engine, stealStrategy: .oldest, factory: { MockVoiceSynth() } ) // Voice A is oldest. let vA: MockVoiceSynth = pool.noteOn(note: 60) let vB: MockVoiceSynth = pool.noteOn(note: 62) // Steal: should take vA (older). let stolen1: MockVoiceSynth = pool.noteOn(note: 64) #expect(stolen1 === vA) // Now vB is oldest; steal again. let stolen2: MockVoiceSynth = pool.noteOn(note: 65) #expect(stolen2 === vB) } // MARK: - Steal: .roundRobin @Test func roundRobinCyclesThroughVoicesInOrder() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let voiceCount: Int = 3 let pool: VoicePool = try VoicePool( voiceCount: voiceCount, group: group, engine: engine, stealStrategy: .roundRobin, factory: { MockVoiceSynth() } ) // Fill the pool. _ = pool.noteOn(note: 60) _ = pool.noteOn(note: 62) _ = pool.noteOn(note: 64) // Round-robin steals starting from index 0. let s1: MockVoiceSynth = pool.noteOn(note: 66) let s2: MockVoiceSynth = pool.noteOn(note: 67) let s3: MockVoiceSynth = pool.noteOn(note: 68) let s4: MockVoiceSynth = pool.noteOn(note: 69) // wraps back to index 0 #expect(s1 === pool.voices[0]) #expect(s2 === pool.voices[1]) #expect(s3 === pool.voices[2]) #expect(s4 === pool.voices[0]) } // MARK: - Steal: .quietest / .lowestAmplitude @Test func quietestStrategyStealsLowestAmpVoice() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 3, group: group, engine: engine, stealStrategy: .quietest, factory: { MockVoiceSynth() } ) let v1: MockVoiceSynth = pool.noteOn(note: 60) let v2: MockVoiceSynth = pool.noteOn(note: 62) let v3: MockVoiceSynth = pool.noteOn(note: 64) // Set distinct amp levels. v2 is quietest. v1.amp = 0.8 v2.amp = 0.1 v3.amp = 0.5 let stolen: MockVoiceSynth = pool.noteOn(note: 66) #expect(stolen === v2, ".quietest should steal the voice with amp=0.1 (v2)") } @Test func lowestAmplitudeIsAliasForQuietest() throws { // Same behavior as .quietest — just verify the alias picks the lowest amp voice. let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 2, group: group, engine: engine, stealStrategy: .lowestAmplitude, factory: { MockVoiceSynth() } ) let v1: MockVoiceSynth = pool.noteOn(note: 60) let v2: MockVoiceSynth = pool.noteOn(note: 62) v1.amp = 0.9 v2.amp = 0.2 let stolen: MockVoiceSynth = pool.noteOn(note: 64) #expect(stolen === v2, ".lowestAmplitude should steal v2 with amp=0.2") } @Test func quietestFallsBackToOldestWhenNoAmpParam() throws { // NoAmpSynth has no "amplitude"/"amp"/"gain" LiveParam. // .quietest should fall back to .oldest behavior. let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 2, group: group, engine: engine, stealStrategy: .quietest, factory: { NoAmpSynth() } ) let v1: NoAmpSynth = pool.noteOn(note: 60) let v2: NoAmpSynth = pool.noteOn(note: 62) // Should fall back to oldest (v1). let stolen: NoAmpSynth = pool.noteOn(note: 64) #expect(stolen === v1, ".quietest should fall back to oldest when no amp param found") _ = v2 } // MARK: - Voice reuse after noteOff @Test func noteOnAfterNoteOffReusesFreedVoice() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 2, group: group, engine: engine, stealStrategy: .oldest, factory: { MockVoiceSynth() } ) let first: MockVoiceSynth = pool.noteOn(note: 60) pool.noteOff(note: 60) // Voice is now free — next noteOn should reuse it without stealing. let reused: MockVoiceSynth = pool.noteOn(note: 72) #expect(reused === first, "noteOn after noteOff should reuse the freed voice") } // MARK: - discardableResult @Test func noteOnResultIsDiscardable() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let pool: VoicePool = try VoicePool( voiceCount: 4, group: group, engine: engine, factory: { MockVoiceSynth() } ) // Must compile without "result unused" warning; verify the pool changes state. pool.noteOn(note: 60) pool.noteOff(note: 60) } // MARK: - voices array @Test func voicesArrayContainsAllUniqueInstances() throws { let engine: SynthesisEngine = makePoolEngine() let group: Group = Group() try engine.add(group) let voiceCount: Int = 5 let pool: VoicePool = try VoicePool( voiceCount: voiceCount, group: group, engine: engine, factory: { MockVoiceSynth() } ) // All entries must be distinct object references. for i: Int in 0..