1.0.0 release
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
// EnvelopeTimeScaleVerificationTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
/// One-off verification of `EnvelopeGen.{Audio,Control}` timeScale behavior
|
||||
/// — reproduces the MyPiece scenario: envelope durations [0.2, 0.6, 0.2]
|
||||
/// (sum 1.0), timeScale 8.0 → expected total 8 seconds.
|
||||
@Suite("EnvelopeGen timeScale verification")
|
||||
struct EnvelopeTimeScaleVerificationTests {
|
||||
|
||||
/// Drive an Audio envelope through `expectedSampleCount` samples at the
|
||||
/// engine's blockSize and check that the envelope's `isDone` flag flips
|
||||
/// at the right moment.
|
||||
@Test("EnvelopeGen.Audio timeScale=8 on a 1.0s envelope reaches isDone at ~8s")
|
||||
func testAudioEnvTimeScale8() {
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 48000.0
|
||||
let engine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: blockSize,
|
||||
sampleRate: sampleRate,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
|
||||
let envelope: Envelope = Envelope(
|
||||
levels: [0.0, 1.0, 1.0, 0.0],
|
||||
durations: [0.2, 0.6, 0.2],
|
||||
curves: [.sine, .lin, .sine]
|
||||
)
|
||||
let env: EnvelopeGen.Audio = EnvelopeGen.Audio(
|
||||
envelope: envelope,
|
||||
timeScale: 8.0,
|
||||
doneAction: .freeNode
|
||||
)
|
||||
|
||||
// Drive the envelope directly via _computeSamples. Sum of durations *
|
||||
// timeScale = 1.0 * 8.0 = 8.0s. At sampleRate=48k, that's 384000 samples
|
||||
// = 6000 blocks @ blockSize=64.
|
||||
let expectedTotalSamples: Int = Int(8.0 * sampleRate)
|
||||
let blocksUntilDone: Int = (expectedTotalSamples + blockSize - 1) / blockSize
|
||||
|
||||
let graph: SynthGraph = SynthGraph(
|
||||
engine: engine,
|
||||
busOffset: 0,
|
||||
parameterTable: ParameterTable(),
|
||||
scratchBuses: nil
|
||||
)
|
||||
|
||||
var doneAtBlock: Int = -1
|
||||
for blockIdx: Int in 0..<(blocksUntilDone + 50) {
|
||||
engine.advanceSegmentSerial()
|
||||
_ = env.calculatedSamples(graph: graph, count: blockSize)
|
||||
if env.needsFree && doneAtBlock < 0 {
|
||||
doneAtBlock = blockIdx
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
#expect(doneAtBlock >= 0, "Envelope should have completed within budget")
|
||||
let elapsedSeconds: Double = Double(doneAtBlock * blockSize) / sampleRate
|
||||
#expect(abs(elapsedSeconds - 8.0) < 0.1,
|
||||
"Envelope should complete at ~8s, got \(elapsedSeconds)s (block \(doneAtBlock))")
|
||||
}
|
||||
|
||||
/// Independent SynthesizerBase instances each writing `OscSin.Audio *
|
||||
/// EnvelopeGen.Control` to the same audio bus — expect their outputs to
|
||||
/// sum additively. Verifies bug B premise.
|
||||
@Test("Multiple synths writing OscSin*env to one bus sum on the bus")
|
||||
func testMultipleSynthsAccumulate() throws {
|
||||
final class TestNote: SynthesizerBase {
|
||||
override func build() {
|
||||
let env: EnvelopeGen.Control = EnvelopeGen.Control(
|
||||
envelope: Envelope(
|
||||
levels: [0.0, 1.0, 0.0],
|
||||
durations: [0.1, 0.1],
|
||||
curves: [.lin, .lin]
|
||||
),
|
||||
timeScale: 10.0 // → 2s total, plenty of sustain
|
||||
)
|
||||
output(bus: 4, source: OscSin.Audio(freq: 300.0) * env)
|
||||
}
|
||||
}
|
||||
|
||||
let engine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: 64,
|
||||
sampleRate: 48000,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let a: TestNote = TestNote()
|
||||
try engine.add(a)
|
||||
|
||||
// Drive blocks so the first envelope ramps to ~1 (after ~0.5s).
|
||||
for _ in 0..<400 { engine.processBlock(inputSamples: []) }
|
||||
let bus4: Int = 4
|
||||
var soloSample: Float = 0
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
soloSample = max(soloSample, abs(engine.audioBuses[bus4][i]))
|
||||
}
|
||||
#expect(soloSample > 0.1,
|
||||
"Solo note should write a non-trivial sample to bus 4 (got \(soloSample))")
|
||||
|
||||
// Add a second note — same frequency, same phase 0 at insertion → they
|
||||
// should sum constructively. Run a few more blocks then compare.
|
||||
let b: TestNote = TestNote()
|
||||
try engine.add(b)
|
||||
for _ in 0..<400 { engine.processBlock(inputSamples: []) }
|
||||
var duoSample: Float = 0
|
||||
for i: Int in 0..<engine.blockSize {
|
||||
duoSample = max(duoSample, abs(engine.audioBuses[bus4][i]))
|
||||
}
|
||||
|
||||
#expect(duoSample > soloSample * 1.4,
|
||||
"Two notes should be louder than one (solo=\(soloSample) duo=\(duoSample))")
|
||||
}
|
||||
|
||||
@Test("EnvelopeGen.Control timeScale=8 on a 1.0s envelope reaches isDone at ~8s")
|
||||
func testControlEnvTimeScale8() {
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 48000.0
|
||||
let engine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: blockSize,
|
||||
sampleRate: sampleRate,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
driver: OfflineDriver()
|
||||
)
|
||||
|
||||
let envelope: Envelope = Envelope(
|
||||
levels: [0.0, 1.0, 1.0, 0.0],
|
||||
durations: [0.2, 0.6, 0.2],
|
||||
curves: [.sine, .lin, .sine]
|
||||
)
|
||||
let env: EnvelopeGen.Control = EnvelopeGen.Control(
|
||||
envelope: envelope,
|
||||
timeScale: 8.0,
|
||||
doneAction: .freeNode
|
||||
)
|
||||
|
||||
let expectedTotalSamples: Int = Int(8.0 * sampleRate)
|
||||
let blocksUntilDone: Int = (expectedTotalSamples + blockSize - 1) / blockSize
|
||||
|
||||
let graph: SynthGraph = SynthGraph(
|
||||
engine: engine,
|
||||
busOffset: 0,
|
||||
parameterTable: ParameterTable(),
|
||||
scratchBuses: nil
|
||||
)
|
||||
|
||||
var doneAtBlock: Int = -1
|
||||
for blockIdx: Int in 0..<(blocksUntilDone + 50) {
|
||||
engine.advanceSegmentSerial()
|
||||
_ = env.calculatedSamples(graph: graph, count: blockSize)
|
||||
if env.needsFree && doneAtBlock < 0 {
|
||||
doneAtBlock = blockIdx
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
#expect(doneAtBlock >= 0, "Envelope should have completed within budget")
|
||||
let elapsedSeconds: Double = Double(doneAtBlock * blockSize) / sampleRate
|
||||
#expect(abs(elapsedSeconds - 8.0) < 0.1,
|
||||
"Envelope should complete at ~8s, got \(elapsedSeconds)s (block \(doneAtBlock))")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user