290 lines
13 KiB
Swift
290 lines
13 KiB
Swift
// ChannelSelectTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("ChannelSelect")
|
|
struct ChannelSelectTests {
|
|
|
|
func makeEngine() throws -> SynthesisEngine {
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
return engine
|
|
}
|
|
|
|
// MARK: - ChannelSelect.Audio basics
|
|
|
|
@Test func audioExtractsChannel0FromPan2HardLeft() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -1.0)
|
|
let left: ChannelSelect.Audio = pan[0]
|
|
let right: ChannelSelect.Audio = pan[1]
|
|
|
|
let lOut: [[Double]] = left.calculatedSamples(graph: graph, count: 64)
|
|
let rOut: [[Double]] = right.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(lOut.count == 1, "ChannelSelect must return exactly 1 channel")
|
|
#expect(rOut.count == 1, "ChannelSelect must return exactly 1 channel")
|
|
#expect(abs(lOut[0][0] - 1.0) < 1e-6, "Hard-left pan: left channel should be 1.0")
|
|
#expect(abs(rOut[0][0]) < 1e-6, "Hard-left pan: right channel should be 0.0")
|
|
}
|
|
|
|
@Test func audioExtractsChannel1FromPan2HardRight() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 1.0)
|
|
let left: ChannelSelect.Audio = pan[0]
|
|
let right: ChannelSelect.Audio = pan[1]
|
|
|
|
let lOut: [[Double]] = left.calculatedSamples(graph: graph, count: 64)
|
|
let rOut: [[Double]] = right.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(abs(lOut[0][0]) < 1e-6, "Hard-right pan: left channel should be 0.0")
|
|
#expect(abs(rOut[0][0] - 1.0) < 1e-6, "Hard-right pan: right channel should be 1.0")
|
|
}
|
|
|
|
@Test func audioOutOfRangeChannelReturnsSilence() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 0.0)
|
|
let oob: ChannelSelect.Audio = pan[99]
|
|
let out: [[Double]] = oob.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(out.count == 1, "Out-of-range ChannelSelect must return 1-channel buffer")
|
|
#expect(out[0].prefix(64).allSatisfy { $0 == 0.0 }, "Out-of-range channel must be silent")
|
|
}
|
|
|
|
@Test func audioResultIsSingleChannel() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(0.5), pan: 0.0)
|
|
for ch: Int in 0..<2 {
|
|
let out: [[Double]] = pan[ch].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
#expect(out.count == 1, "Channel \(ch) selector must return exactly 1 outer channel")
|
|
}
|
|
}
|
|
|
|
// MARK: - Named aliases on Pan2
|
|
|
|
@Test func pan2LeftAliasMatchesSubscript0() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -0.5)
|
|
let viaSubscript: [[Double]] = pan[0].calculatedSamples(graph: graph, count: 64)
|
|
let viaAlias: [[Double]] = pan.left.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(viaSubscript[0][0] == viaAlias[0][0],
|
|
"pan.left must produce the same value as pan[0]")
|
|
}
|
|
|
|
@Test func pan2RightAliasMatchesSubscript1() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: 0.7)
|
|
let viaSubscript: [[Double]] = pan[1].calculatedSamples(graph: graph, count: 64)
|
|
let viaAlias: [[Double]] = pan.right.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(viaSubscript[0][0] == viaAlias[0][0],
|
|
"pan.right must produce the same value as pan[1]")
|
|
}
|
|
|
|
// MARK: - Named aliases on Pan4 subclass (PanAz)
|
|
|
|
@Test func pan4NamedAliasesMatchSubscripts() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
defer { engine.stop() }
|
|
|
|
// Pan4 itself is an open base class with no inputs; pick a concrete
|
|
// subclass to exercise the inherited channel-alias extension. PanAz at
|
|
// az = -π/4 hard-pans to LF (x = -1, y = +1).
|
|
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
|
pan.azimuth = -.pi / 4
|
|
|
|
let aliases: [ChannelSelect.Audio] = [
|
|
pan.frontLeft, pan.frontRight, pan.rearRight, pan.rearLeft
|
|
]
|
|
// Run alias and subscript in the SAME segment so the fan-out cache ensures
|
|
// the panner evaluates once and both channel selectors read from the same
|
|
// output buffer. With per-sample azimuth evaluation, alias and subscript
|
|
// must share the same underlying evaluation — different segments would
|
|
// produce different ramp positions.
|
|
for (ch, alias) in aliases.enumerated() {
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
let viaAlias: [[Double]] = alias.calculatedSamples(graph: graph, count: 64)
|
|
let viaSubscript: [[Double]] = pan[ch].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
#expect(viaAlias[0][0] == viaSubscript[0][0],
|
|
"PanAz named alias ch\(ch) must match pan[\(ch)] in the same segment")
|
|
}
|
|
}
|
|
|
|
// MARK: - Named aliases on FreqTracker
|
|
|
|
@Test func freqTrackerFreqAliasIsSingleChannel() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let tracker: FreqTracker = FreqTracker(in: ConstantGeneratorInput(0.0))
|
|
let out: [[Double]] = tracker.freq.calculatedSamples(graph: graph, count: 64)
|
|
#expect(out.count == 1, "tracker.freq must return 1 channel")
|
|
}
|
|
|
|
@Test func freqTrackerConfidenceAliasIsSingleChannel() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let tracker: FreqTracker = FreqTracker(in: ConstantGeneratorInput(0.0))
|
|
let out: [[Double]] = tracker.confidence.calculatedSamples(graph: graph, count: 64)
|
|
#expect(out.count == 1, "tracker.confidence must return 1 channel")
|
|
}
|
|
|
|
@Test func freqTrackerFreqAliasMatchesSubscript0() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let tracker: FreqTracker = FreqTracker(in: ConstantGeneratorInput(0.0))
|
|
let via0: [[Double]] = tracker[0].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
let viaAlias: [[Double]] = tracker.freq.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(via0[0][0] == viaAlias[0][0],
|
|
"tracker.freq must match tracker[0]")
|
|
}
|
|
|
|
@Test func freqTrackerConfidenceAliasMatchesSubscript1() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let tracker: FreqTracker = FreqTracker(in: ConstantGeneratorInput(0.0))
|
|
let via1: [[Double]] = tracker[1].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
let viaAlias: [[Double]] = tracker.confidence.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(via1[0][0] == viaAlias[0][0],
|
|
"tracker.confidence must match tracker[1]")
|
|
}
|
|
|
|
// MARK: - .channel(n) method is equivalent to subscript
|
|
|
|
@Test func channelMethodEqualsSubscript() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -0.3)
|
|
let via0: [[Double]] = pan[0].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
let viaMethod: [[Double]] = pan.channel(0).calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(via0[0][0] == viaMethod[0][0],
|
|
"pan[0] and pan.channel(0) must produce the same value")
|
|
}
|
|
|
|
// MARK: - GeneratorArray subscript
|
|
|
|
@Test func generatorArraySubscriptIsSingleChannel() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let oscs: GeneratorArray = expand([440.0, 880.0] as [any ValidGeneratorInput]) {
|
|
OscSin.Audio(freq: $0)
|
|
}
|
|
let ch0: [[Double]] = oscs[0].calculatedSamples(graph: graph, count: 64)
|
|
#expect(ch0.count == 1, "GeneratorArray[0] must return 1-channel output")
|
|
}
|
|
|
|
@Test func generatorArraySubscriptMatchesDirectChannel() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
// GeneratorArray with a hard-panned source: channel 0 = 1.0, channel 1 = 0.0
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -1.0)
|
|
let arr: GeneratorArray = GeneratorArray([pan]) // 1-generator array, 2-channel source
|
|
// arr[0] selects channel 0 of the GeneratorArray (which is channel 0 of Pan2's first channel)
|
|
let direct: [[Double]] = pan[0].calculatedSamples(graph: graph, count: 64)
|
|
advanceBlock(engine)
|
|
let viaArr: [[Double]] = arr[0].calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(abs(direct[0][0] - 1.0) < 1e-6, "Pan2 ch0 hard-left should be 1.0")
|
|
#expect(viaArr.count == 1, "GeneratorArray[0] must return 1-channel output")
|
|
}
|
|
|
|
// MARK: - Fan-out cache: shared source evaluates once per segment
|
|
|
|
@Test func sharedPan2EvaluatesOnceWithTwoChannelSelectors() throws {
|
|
let engine: SynthesisEngine = try makeEngine()
|
|
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
|
defer { engine.stop() }
|
|
|
|
let pan: Pan2 = Pan2(input: ConstantGeneratorInput(1.0), pan: -1.0)
|
|
let left: ChannelSelect.Audio = pan.left
|
|
let right: ChannelSelect.Audio = pan.right
|
|
|
|
// Both selectors call pan.calculatedSamples in the same segment.
|
|
// The segment-serial cache ensures pan._computeSamples fires once.
|
|
// Verify: left = 1.0, right = 0.0 — consistent data, no double evaluation.
|
|
let lOut: [[Double]] = left.calculatedSamples(graph: graph, count: 64)
|
|
let rOut: [[Double]] = right.calculatedSamples(graph: graph, count: 64)
|
|
|
|
#expect(abs(lOut[0][0] - 1.0) < 1e-6, "Left channel should be 1.0 (hard-left)")
|
|
#expect(abs(rOut[0][0]) < 1e-6, "Right channel should be 0.0 (hard-left)")
|
|
}
|
|
|
|
// MARK: - ChannelSelect.Control ramp interpolation
|
|
|
|
@Test func controlRampInterpolatesFromPreviousToTarget() throws {
|
|
// ChannelSelect.Control follows the same ramp pattern as In.Control.
|
|
// Use a 2-value source: block 1 constant 0.0, block 2 constant 1.0.
|
|
// ChannelSelect.Control should ramp from 0→1 across the second block.
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
|
try engine.start()
|
|
let defer_: () -> () = { engine.stop() }
|
|
defer { defer_() }
|
|
|
|
// Build a 2-channel source that holds block-rate values.
|
|
// We use a constant source for simplicity: both channels always 1.0.
|
|
let src: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
|
let sel: ChannelSelect.Control = ChannelSelect.Control(source: src, channel: 0)
|
|
|
|
// Block 1: previousOutputValue starts at 0.0, target = 1.0 → ramp 0→1.
|
|
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
|
let b1: [[Double]] = sel._computeSamples(graph: graph1, count: 64)
|
|
|
|
// Ramp from 0 to 1 over 64 samples: last sample should be very close to 1.0.
|
|
#expect(b1[0][0] < b1[0][63], "Ramp should increase from first to last sample")
|
|
#expect(abs(b1[0][63] - 1.0) < 1e-6, "Ramp last sample should reach target 1.0")
|
|
|
|
// Block 2: previousOutputValue is now 1.0, target still 1.0 → flat at 1.0.
|
|
advanceBlock(engine)
|
|
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
|
let b2: [[Double]] = sel._computeSamples(graph: graph2, count: 64)
|
|
#expect(abs(b2[0][0] - 1.0) < 1e-6, "Second block should be flat at 1.0")
|
|
#expect(abs(b2[0][63] - 1.0) < 1e-6, "Second block last sample should be 1.0")
|
|
}
|
|
}
|