560724d116
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated synths were hard-freed instead of released). - New: quickstart project template + LyrebirdQuickstartDemo. - Docs: every example typechecked or provably illustrative (426->542); ~20 wrong-API examples fixed; root docs brought under the compile guard. - Fixed: NRT/Score demo Package.swift build from terminal again. See RELEASES.md for details.
130 lines
4.7 KiB
Swift
130 lines
4.7 KiB
Swift
// SynthesizerReleaseDispatchTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable import LyrebirdSynth
|
|
|
|
// These tests guard the dispatch of `release(engine:)` / `free(engine:)` on SynthesizerBase
|
|
// subclasses. Two distinct guards are at work, and both matter:
|
|
//
|
|
// 1. The `override` keyword below is a COMPILE-time guard. If the `open` members were ever
|
|
// removed from SynthesizerBase — putting the witness back on the Synthesizer extension
|
|
// default — these fixtures stop compiling outright. Do not "simplify" these by dropping
|
|
// `override`: that silently restores the original bug, in which a subclass method was
|
|
// statically shadowed and unreachable through `any Synthesizer`.
|
|
// 2. The `#expect`s are a RUNTIME guard on the existential and Group-cascade paths, which
|
|
// are how real callers reach these methods.
|
|
|
|
/// A gated synthesizer of the shape the docs tell users to write: override `release(engine:)`
|
|
/// to drop the gate instead of being hard-freed.
|
|
private final class GatedNote: SynthesizerBase {
|
|
var releaseCalled: Bool = false
|
|
var freeCalled: Bool = false
|
|
|
|
override func build() {}
|
|
|
|
override func release(engine: SynthesisEngine) throws {
|
|
releaseCalled = true
|
|
}
|
|
|
|
override func free(engine: SynthesisEngine) {
|
|
freeCalled = true
|
|
super.free(engine: engine)
|
|
}
|
|
}
|
|
|
|
/// A synthesizer with no gate — must still hit the `.noGate` default.
|
|
private final class UngatedNote: SynthesizerBase {
|
|
override func build() {}
|
|
}
|
|
|
|
@MainActor
|
|
private func makeEngine() -> SynthesisEngine {
|
|
let blockSize: Int = 512
|
|
let sampleRate: Double = 44100.0
|
|
let outputChannelCount: Int = 2
|
|
let inputChannelCount: Int = 0
|
|
|
|
return SynthesisEngine(
|
|
blockSize: blockSize,
|
|
sampleRate: sampleRate,
|
|
numOutputChannels: outputChannelCount,
|
|
numInputChannels: inputChannelCount,
|
|
driver: OfflineDriver()
|
|
)
|
|
}
|
|
|
|
@Suite("Synthesizer release dispatch")
|
|
struct SynthesizerReleaseDispatchTests {
|
|
|
|
/// The regression. `release` is a protocol requirement with an extension default; when
|
|
/// `SynthesizerBase` declared no member, the witness bound statically to that default and
|
|
/// a subclass override was unreachable through `any Synthesizer` — so every gated synth
|
|
/// got `.noGate` and was hard-freed instead of released.
|
|
@MainActor
|
|
@Test("subclass release is reached through an `any Synthesizer` existential")
|
|
func releaseDispatchesThroughExistential() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let note: GatedNote = GatedNote()
|
|
|
|
let erased: any Synthesizer = note
|
|
try erased.release(engine: engine)
|
|
|
|
#expect(note.releaseCalled, "release must dispatch to the subclass, not the extension default")
|
|
}
|
|
|
|
@MainActor
|
|
@Test("subclass free is reached through an `any Synthesizer` existential")
|
|
func freeDispatchesThroughExistential() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let note: GatedNote = GatedNote()
|
|
|
|
let erased: any Synthesizer = note
|
|
erased.free(engine: engine)
|
|
|
|
#expect(note.freeCalled, "free must dispatch to the subclass, not the extension default")
|
|
}
|
|
|
|
@MainActor
|
|
@Test("a synthesizer without a gate still throws .noGate")
|
|
func ungatedStillThrowsNoGate() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let note: UngatedNote = UngatedNote()
|
|
|
|
let erased: any Synthesizer = note
|
|
#expect(throws: SynthesizerError.noGate) {
|
|
try erased.release(engine: engine)
|
|
}
|
|
}
|
|
|
|
/// `Group.release` cascades to children through `any Synthesizer` and frees any child that
|
|
/// throws `.noGate`. A gated child must be released, never freed.
|
|
@MainActor
|
|
@Test("Group.release cascade releases a gated child instead of freeing it")
|
|
func groupCascadeReleasesGatedChild() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
try engine.start()
|
|
defer { engine.stop() }
|
|
|
|
let group: Group = Group()
|
|
try engine.add(group)
|
|
|
|
let note: GatedNote = GatedNote()
|
|
try engine.add(note, position: .tail(of: group))
|
|
engine.processBlock(inputSamples: [])
|
|
|
|
group.release(engine: engine)
|
|
|
|
#expect(note.releaseCalled, "gated child must have its release() called")
|
|
#expect(!note.freeCalled, "gated child must not be hard-freed by the cascade")
|
|
|
|
// The cascade left the child alive (correctly), so the group's self-free poll timer
|
|
// would keep running until the group deallocs. Empty the group so it cancels.
|
|
note.free(engine: engine)
|
|
engine.processBlock(inputSamples: [])
|
|
}
|
|
}
|