prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,531 @@
|
||||
// BinaryOpGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
/// Multi-channel test fixture — emits a fixed scalar per channel.
|
||||
/// Used to verify channel-aware broadcasting in BinaryOpGenerator.
|
||||
fileprivate final class StereoConstantGen: GeneratorBase, AudioGenerator {
|
||||
let leftValue: Double
|
||||
let rightValue: Double
|
||||
public override var outputChannelCount: Int { 2 }
|
||||
|
||||
init(left: Double, right: Double) {
|
||||
self.leftValue = left
|
||||
self.rightValue = right
|
||||
super.init(channels: 2)
|
||||
}
|
||||
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
for i: Int in 0..<count {
|
||||
_result[0][i] = leftValue
|
||||
_result[1][i] = rightValue
|
||||
}
|
||||
return _result
|
||||
}
|
||||
}
|
||||
|
||||
/// 3-channel test fixture used to verify 3×3 element-wise composition.
|
||||
fileprivate final class TripleConstantGen: GeneratorBase, AudioGenerator {
|
||||
public override var outputChannelCount: Int { 3 }
|
||||
|
||||
init(_ a: Double, _ b: Double, _ c: Double) {
|
||||
super.init(channels: 3)
|
||||
for i: Int in 0..<_result[0].count {
|
||||
_result[0][i] = a
|
||||
_result[1][i] = b
|
||||
_result[2][i] = c
|
||||
}
|
||||
}
|
||||
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
return _result
|
||||
}
|
||||
}
|
||||
|
||||
/// 4-channel test fixture used to verify Pan4-shape (4-row) broadcast paths.
|
||||
fileprivate final class QuadConstantGen: GeneratorBase, AudioGenerator {
|
||||
public override var outputChannelCount: Int { 4 }
|
||||
|
||||
init(_ a: Double, _ b: Double, _ c: Double, _ d: Double) {
|
||||
super.init(channels: 4)
|
||||
for i: Int in 0..<_result[0].count {
|
||||
_result[0][i] = a
|
||||
_result[1][i] = b
|
||||
_result[2][i] = c
|
||||
_result[3][i] = d
|
||||
}
|
||||
}
|
||||
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
return _result
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("BinaryOpGenerator")
|
||||
struct BinaryOpGeneratorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph {
|
||||
makeSynthGraph(engine: engine)
|
||||
}
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func multiplyTwoConstants() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = (2.0 as Double) * ConstantAudioGen(3.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 6.0 })
|
||||
}
|
||||
|
||||
@Test func addTwoConstants() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(1.0) + (0.5 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 1.5 })
|
||||
}
|
||||
|
||||
@Test func subtractConstants() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(1.0) - (0.25 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.75 })
|
||||
}
|
||||
|
||||
@Test func divideConstants() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(4.0) / (2.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 2.0 })
|
||||
}
|
||||
|
||||
@Test func resultRateIsAudio() {
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(1.0) * ConstantAudioGen(1.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func countIsRespected() {
|
||||
let count: Int = 4
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(1.0) + (0.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
// The pre-allocated result buffer is sized to lyrebirdDefaultResultBufferSize;
|
||||
// only the first `count` elements are valid for this call.
|
||||
#expect(samples[0].count >= count)
|
||||
for i: Int in 0..<count {
|
||||
#expect(samples[0][i] == 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func floatValueUsesOp() {
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(3.0) * (2.0 as Double)
|
||||
#expect(gen.floatValue(graph: graph) == 6.0)
|
||||
}
|
||||
|
||||
@Test func chainedOps() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = (ConstantAudioGen(2.0) * (3.0 as Double)) + (1.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 7.0 })
|
||||
}
|
||||
|
||||
// Order-sensitive operands: a flipped vDSP subtract/divide argument order
|
||||
// would produce the reciprocal/negated value, so these lock the orientation.
|
||||
|
||||
@Test func scalarMinusGenerator() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = (1.0 as Double) - ConstantAudioGen(0.25)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.75 })
|
||||
}
|
||||
|
||||
@Test func scalarDividedByGenerator() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = (1.0 as Double) / ConstantAudioGen(4.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.25 })
|
||||
}
|
||||
|
||||
@Test func generatorMinusGenerator() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(3.0) - ConstantAudioGen(2.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 1.0 })
|
||||
}
|
||||
|
||||
@Test func generatorDividedByGenerator() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantAudioGen(8.0) / ConstantAudioGen(2.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 4.0 })
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Multi-channel broadcast
|
||||
|
||||
@MainActor
|
||||
@Suite("BinaryOpGenerator multi-channel broadcast")
|
||||
struct BinaryOpGeneratorMultiChannelTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph {
|
||||
makeSynthGraph(engine: engine)
|
||||
}
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test("stereo × scalar produces 2-row result, both channels scaled")
|
||||
func stereoTimesScalar() {
|
||||
let count: Int = 8
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let gen: BinaryOpGenerator = stereo * (0.5 as Double)
|
||||
#expect(gen.outputChannelCount == 2)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples.count == 2)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.5 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 1.0 })
|
||||
}
|
||||
|
||||
@Test("scalar × stereo produces 2-row result")
|
||||
func scalarTimesStereo() {
|
||||
let count: Int = 8
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let gen: BinaryOpGenerator = (3.0 as Double) * stereo
|
||||
#expect(gen.outputChannelCount == 2)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 3.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 6.0 })
|
||||
}
|
||||
|
||||
@Test("mono generator + stereo generator broadcasts mono to both channels")
|
||||
func monoPlusStereoBroadcasts() {
|
||||
let count: Int = 8
|
||||
let mono: ConstantAudioGen = ConstantAudioGen(10.0)
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let gen: BinaryOpGenerator = mono + stereo
|
||||
#expect(gen.outputChannelCount == 2)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 12.0 })
|
||||
}
|
||||
|
||||
@Test("stereo + mono broadcasts mono on rhs")
|
||||
func stereoPlusMonoBroadcasts() {
|
||||
let count: Int = 8
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let mono: ConstantAudioGen = ConstantAudioGen(10.0)
|
||||
let gen: BinaryOpGenerator = stereo + mono
|
||||
#expect(gen.outputChannelCount == 2)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 12.0 })
|
||||
}
|
||||
|
||||
@Test("stereo + stereo is element-wise")
|
||||
func stereoPlusStereoElementWise() {
|
||||
let count: Int = 8
|
||||
let a: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let b: StereoConstantGen = StereoConstantGen(left: 10.0, right: 20.0)
|
||||
let gen: BinaryOpGenerator = a + b
|
||||
#expect(gen.outputChannelCount == 2)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 22.0 })
|
||||
}
|
||||
|
||||
@Test("stereo × stereo element-wise")
|
||||
func stereoTimesStereoElementWise() {
|
||||
let count: Int = 8
|
||||
let a: StereoConstantGen = StereoConstantGen(left: 2.0, right: 3.0)
|
||||
let b: StereoConstantGen = StereoConstantGen(left: 4.0, right: 5.0)
|
||||
let gen: BinaryOpGenerator = a * b
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 8.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 15.0 })
|
||||
}
|
||||
|
||||
@Test("ChannelSelect on multi-row BinaryOpGenerator extracts correct channel")
|
||||
func channelSelectOnBinaryOp() {
|
||||
let count: Int = 8
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 1.0, right: 2.0)
|
||||
let scaled: BinaryOpGenerator = stereo * (0.5 as Double)
|
||||
let right: ChannelSelect.Audio = scaled.channel(1)
|
||||
let samples: [[Double]] = right.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 1.0 })
|
||||
}
|
||||
|
||||
@Test("floatValue on N-row BinaryOpGenerator returns channel-0 sample-0")
|
||||
func floatValueReturnsFirstChannelFirstSample() {
|
||||
let stereo: StereoConstantGen = StereoConstantGen(left: 7.0, right: 100.0)
|
||||
let gen: BinaryOpGenerator = stereo * (2.0 as Double)
|
||||
#expect(gen.floatValue(graph: graph) == 14.0)
|
||||
}
|
||||
|
||||
@Test("outputChannelCount is max of operands")
|
||||
func outputChannelCountMaxOfOperands() {
|
||||
let stereoA: StereoConstantGen = StereoConstantGen(left: 1, right: 2)
|
||||
let stereoB: StereoConstantGen = StereoConstantGen(left: 3, right: 4)
|
||||
let monoA: ConstantAudioGen = ConstantAudioGen(1.0)
|
||||
let monoB: ConstantAudioGen = ConstantAudioGen(2.0)
|
||||
#expect((stereoA * monoA).outputChannelCount == 2)
|
||||
#expect((monoA * stereoA).outputChannelCount == 2)
|
||||
#expect((stereoA * stereoB).outputChannelCount == 2)
|
||||
#expect((monoA * monoB).outputChannelCount == 1)
|
||||
#expect((stereoA * (0.5 as Double)).outputChannelCount == 2)
|
||||
}
|
||||
|
||||
@Test("3-channel × 3-channel element-wise")
|
||||
func tripleTimesTripleElementWise() {
|
||||
let count: Int = 8
|
||||
let a: TripleConstantGen = TripleConstantGen(1.0, 2.0, 3.0)
|
||||
let b: TripleConstantGen = TripleConstantGen(10.0, 20.0, 30.0)
|
||||
let gen: BinaryOpGenerator = a * b
|
||||
#expect(gen.outputChannelCount == 3)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 40.0 })
|
||||
#expect(samples[2].prefix(count).allSatisfy { $0 == 90.0 })
|
||||
}
|
||||
|
||||
@Test("scalar × 3-channel broadcasts")
|
||||
func scalarTimesTriple() {
|
||||
let count: Int = 8
|
||||
let triple: TripleConstantGen = TripleConstantGen(1.0, 2.0, 3.0)
|
||||
let gen: BinaryOpGenerator = triple * (10.0 as Double)
|
||||
#expect(gen.outputChannelCount == 3)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 10.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 20.0 })
|
||||
#expect(samples[2].prefix(count).allSatisfy { $0 == 30.0 })
|
||||
}
|
||||
|
||||
@Test("mono + 4-channel broadcasts mono to all four channels")
|
||||
func monoPlusQuadBroadcasts() {
|
||||
let count: Int = 8
|
||||
let mono: ConstantAudioGen = ConstantAudioGen(100.0)
|
||||
let quad: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||||
let gen: BinaryOpGenerator = mono + quad
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples.count == 4)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 101.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 102.0 })
|
||||
#expect(samples[2].prefix(count).allSatisfy { $0 == 103.0 })
|
||||
#expect(samples[3].prefix(count).allSatisfy { $0 == 104.0 })
|
||||
}
|
||||
|
||||
@Test("4-channel × scalar scales every channel")
|
||||
func quadTimesScalar() {
|
||||
let count: Int = 8
|
||||
let quad: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||||
let gen: BinaryOpGenerator = quad * (0.5 as Double)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 0.5 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 1.0 })
|
||||
#expect(samples[2].prefix(count).allSatisfy { $0 == 1.5 })
|
||||
#expect(samples[3].prefix(count).allSatisfy { $0 == 2.0 })
|
||||
}
|
||||
|
||||
@Test("4-channel + 4-channel element-wise (matches Pan4 + Pan4 shape)")
|
||||
func quadPlusQuadElementWise() {
|
||||
let count: Int = 8
|
||||
let a: QuadConstantGen = QuadConstantGen(1.0, 2.0, 3.0, 4.0)
|
||||
let b: QuadConstantGen = QuadConstantGen(10.0, 20.0, 30.0, 40.0)
|
||||
let gen: BinaryOpGenerator = a + b
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == 11.0 })
|
||||
#expect(samples[1].prefix(count).allSatisfy { $0 == 22.0 })
|
||||
#expect(samples[2].prefix(count).allSatisfy { $0 == 33.0 })
|
||||
#expect(samples[3].prefix(count).allSatisfy { $0 == 44.0 })
|
||||
}
|
||||
|
||||
// The 2 × 3 mismatch trap (`precondition` at `BinaryOpGenerator.init`) is correctness-
|
||||
// critical but cannot be tested via Swift Testing — `precondition` terminates the
|
||||
// process rather than throwing. The trap's behavior is exercised manually and verified
|
||||
// by code review. The construction `StereoConstantGen() * TripleConstantGen()` would
|
||||
// trap with the message "BinaryOpGenerator: incompatible channel counts (2 vs 3) ...".
|
||||
}
|
||||
|
||||
// MARK: - Existential operator overloads (any ValidGeneratorInput op any ValidGeneratorInput)
|
||||
|
||||
@MainActor
|
||||
@Suite("BinaryOpGenerator existential operators")
|
||||
struct BinaryOpGeneratorExistentialTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph {
|
||||
makeSynthGraph(engine: engine)
|
||||
}
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// These tests cover the `any ValidGeneratorInput op any ValidGeneratorInput` overloads.
|
||||
// All four operators are exercised with an existential-typed LHS so the compiler
|
||||
// cannot resolve to a stdlib or concrete-Generator overload.
|
||||
|
||||
@Test("existential * scalar produces correct output")
|
||||
func existentialTimesScalar() {
|
||||
let count: Int = 8
|
||||
let freq: any ValidGeneratorInput = 440.0
|
||||
let scaled: Double = 1.5
|
||||
let gen: BinaryOpGenerator = freq * scaled // (any ValidGeneratorInput) * (Double as any ValidGeneratorInput)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 440.0 * 1.5
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("existential + scalar produces correct output")
|
||||
func existentialPlusScalar() {
|
||||
let count: Int = 8
|
||||
let freq: any ValidGeneratorInput = 440.0
|
||||
let gen: BinaryOpGenerator = freq + (10.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 450.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("existential - scalar produces correct output")
|
||||
func existentialMinusScalar() {
|
||||
let count: Int = 8
|
||||
let freq: any ValidGeneratorInput = 440.0
|
||||
let gen: BinaryOpGenerator = freq - (40.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 400.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("existential / scalar produces correct output")
|
||||
func existentialDividedByScalar() {
|
||||
let count: Int = 8
|
||||
let freq: any ValidGeneratorInput = 440.0
|
||||
let gen: BinaryOpGenerator = freq / (2.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 220.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("existential op existential both non-generator produces BinaryOpGenerator")
|
||||
func existentialOpExistential() {
|
||||
let count: Int = 8
|
||||
let a: any ValidGeneratorInput = 3.0
|
||||
let b: any ValidGeneratorInput = 4.0
|
||||
let gen: BinaryOpGenerator = a * b
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 12.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
/// Regression guard: concrete `Double op Double` must still resolve to `Double`
|
||||
/// (stdlib overload), not `BinaryOpGenerator`. If the existential overloads above
|
||||
/// introduced ambiguity, this line would fail to compile with a type error.
|
||||
@Test("Double op Double still resolves to Double (stdlib wins)")
|
||||
func doubleOpDoubleRemainsDouble() {
|
||||
let a: Double = 440.0
|
||||
let b: Double = 1.5
|
||||
// These must be Double, not BinaryOpGenerator.
|
||||
// If our overloads caused ambiguity, the type annotations below would fail to compile.
|
||||
let product: Double = a * b
|
||||
let sum: Double = a + b
|
||||
let diff: Double = a - b
|
||||
let quot: Double = a / b
|
||||
#expect(product == 440.0 * 1.5)
|
||||
#expect(sum == 440.0 + 1.5)
|
||||
#expect(diff == 440.0 - 1.5)
|
||||
#expect(quot == 440.0 / 1.5)
|
||||
}
|
||||
|
||||
@Test("existential op produces a BinaryOpGenerator that is a Generator")
|
||||
func existentialOpResultIsGenerator() {
|
||||
let freq: any ValidGeneratorInput = 440.0
|
||||
let gen: BinaryOpGenerator = freq * (2.0 as Double)
|
||||
// The result must conform to Generator so it can be wired into a signal graph.
|
||||
#expect(gen is any Generator)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - ConstantGeneratorInput
|
||||
|
||||
@MainActor
|
||||
@Suite("ConstantGeneratorInput")
|
||||
struct ConstantGeneratorInputTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph {
|
||||
makeSynthGraph(engine: engine)
|
||||
}
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test("calculatedSamples returns all-constant block")
|
||||
func calculatedSamplesIsConstant() {
|
||||
let constantValue: Double = 440.0
|
||||
let count: Int = 8
|
||||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(constantValue)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
#expect(samples[0].prefix(count).allSatisfy { $0 == constantValue })
|
||||
}
|
||||
|
||||
@Test("floatValue returns the constant directly")
|
||||
func floatValueReturnsConstant() {
|
||||
let constantValue: Double = 220.0
|
||||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(constantValue)
|
||||
#expect(gen.floatValue(graph: graph) == constantValue)
|
||||
}
|
||||
|
||||
@Test("rate is audio")
|
||||
func rateIsAudio() {
|
||||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test("needsInterpolation is false")
|
||||
func needsInterpolationIsFalse() {
|
||||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||||
// ConstantGeneratorInput is an AudioGenerator → needsInterpolation() == false.
|
||||
#expect(gen.needsInterpolation() == false)
|
||||
}
|
||||
|
||||
@Test("ConstantGeneratorInput * scalar resolves via Category A overload")
|
||||
func constGenInputTimesScalar() {
|
||||
let count: Int = 8
|
||||
let gen: BinaryOpGenerator = ConstantGeneratorInput(440.0) * (2.0 as Double)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 880.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("ConstantGeneratorInput + OscSin wires correctly")
|
||||
func constGenInputPlusOscSin() {
|
||||
// Verifies ConstantGeneratorInput participates in the Generator op Generator path.
|
||||
let count: Int = 8
|
||||
let offset: ConstantGeneratorInput = ConstantGeneratorInput(100.0)
|
||||
let osc: ConstantAudioGen = ConstantAudioGen(440.0)
|
||||
let gen: BinaryOpGenerator = offset + osc // Generator + Generator → BinaryOpGenerator
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let expected: Double = 540.0
|
||||
#expect(samples[0].prefix(count).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test("fan-out cache: calculatedSamples called twice returns same block")
|
||||
func fanOutCacheHit() {
|
||||
let count: Int = 8
|
||||
let gen: ConstantGeneratorInput = ConstantGeneratorInput(440.0)
|
||||
let first: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
let second: [[Double]] = gen.calculatedSamples(graph: graph, count: count)
|
||||
// Same segment serial → cache hit → identical buffer reference returned.
|
||||
for i: Int in 0..<count {
|
||||
#expect(first[0][i] == second[0][i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
// MathGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Darwin
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("MathGenerators — waveshaping + mapping")
|
||||
struct MathGeneratorTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Tanh.Audio
|
||||
|
||||
@Test func tanhAudioAtZero() {
|
||||
let gen: Tanh.Audio = Tanh.Audio(ConstantAudioGen(0.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func tanhAudioKnownValue() {
|
||||
let gen: Tanh.Audio = Tanh.Audio(ConstantAudioGen(1.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = Double(tanh(1.0))
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func tanhAudioSaturates() {
|
||||
let gen: Tanh.Audio = Tanh.Audio(ConstantAudioGen(100.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-6 })
|
||||
}
|
||||
|
||||
@Test func tanhControlRamps() {
|
||||
let gen: Tanh.Control = Tanh.Control(ConstantAudioGen(1.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = Double(tanh(1.0))
|
||||
#expect(samples[0][7] == expected)
|
||||
#expect(samples[0][0] < expected)
|
||||
}
|
||||
|
||||
// MARK: - Pow.Audio / Control
|
||||
|
||||
@Test func powAudioSquare() {
|
||||
let gen: Pow.Audio = Pow.Audio(ConstantAudioGen(3.0), exp: 2.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 9.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func powAudioIdentityAtOne() {
|
||||
let gen: Pow.Audio = Pow.Audio(ConstantAudioGen(5.0), exp: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 5.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func powControlReachesTarget() {
|
||||
let gen: Pow.Control = Pow.Control(ConstantAudioGen(4.0), exp: 0.5)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 2.0) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - Clip.Audio / Control
|
||||
|
||||
@Test func clipAudioWithinRange() {
|
||||
let gen: Clip.Audio = Clip.Audio(ConstantAudioGen(0.5), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.5) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func clipAudioClampsAbove() {
|
||||
let gen: Clip.Audio = Clip.Audio(ConstantAudioGen(2.0), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func clipAudioClampsBelow() {
|
||||
let gen: Clip.Audio = Clip.Audio(ConstantAudioGen(-3.0), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - (-1.0)) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func clipControlReachesTarget() {
|
||||
let gen: Clip.Control = Clip.Control(ConstantAudioGen(0.5), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 0.5) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - Fold.Audio / Control
|
||||
|
||||
@Test func foldAudioBoundaryIsIdentity() {
|
||||
let gen: Fold.Audio = Fold.Audio(ConstantAudioGen(0.5), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.5) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func foldAudioReflectsAbove() {
|
||||
// 1.2 folds to hi - (1.2 - hi) = 1.0 - 0.2 = 0.8
|
||||
let gen: Fold.Audio = Fold.Audio(ConstantAudioGen(1.2), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.8) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func foldAudioMultiLevel() {
|
||||
// 2.5: reflect at 1.0 → -0.5
|
||||
let gen: Fold.Audio = Fold.Audio(ConstantAudioGen(2.5), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - (-0.5)) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func foldControlReachesTarget() {
|
||||
let gen: Fold.Control = Fold.Control(ConstantAudioGen(1.2), lo: -1.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 0.8) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - Wrap.Audio / Control
|
||||
|
||||
@Test func wrapAudioWithinRange() {
|
||||
let gen: Wrap.Audio = Wrap.Audio(ConstantAudioGen(0.5), lo: 0.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.5) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func wrapAudioAboveRange() {
|
||||
// 1.3 wraps into [0,1) → 0.3
|
||||
let gen: Wrap.Audio = Wrap.Audio(ConstantAudioGen(1.3), lo: 0.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.3) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func wrapAudioNegative() {
|
||||
// -0.3 wraps into [0,1) → 0.7
|
||||
let gen: Wrap.Audio = Wrap.Audio(ConstantAudioGen(-0.3), lo: 0.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.7) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func wrapControlReachesTarget() {
|
||||
let gen: Wrap.Control = Wrap.Control(ConstantAudioGen(1.3), lo: 0.0, hi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 0.3) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - SoftClip.Audio / Control
|
||||
|
||||
@Test func softClipAudioAtZero() {
|
||||
let gen: SoftClip.Audio = SoftClip.Audio(ConstantAudioGen(0.0), drive: 2.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func softClipAudioNormalisedAtOne() {
|
||||
// tanh(1 * drive) / tanh(drive) with x=1 → 1
|
||||
let gen: SoftClip.Audio = SoftClip.Audio(ConstantAudioGen(1.0), drive: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func softClipControlReachesTarget() {
|
||||
let gen: SoftClip.Control = SoftClip.Control(ConstantAudioGen(1.0), drive: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
|
||||
// MARK: - LinLin.Audio / Control
|
||||
|
||||
@Test func linLinAudioIdentity() {
|
||||
let gen: LinLin.Audio = LinLin.Audio(ConstantAudioGen(0.5), inLo: 0.0, inHi: 1.0, outLo: 0.0, outHi: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 0.5) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func linLinAudioScalesRange() {
|
||||
// map 0.5 from [-1,1] to [0,100] → 75
|
||||
let gen: LinLin.Audio = LinLin.Audio(ConstantAudioGen(0.5), inLo: -1.0, inHi: 1.0, outLo: 0.0, outHi: 100.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 75.0) < 1e-8 })
|
||||
}
|
||||
|
||||
@Test func linLinControlReachesTarget() {
|
||||
let gen: LinLin.Control = LinLin.Control(ConstantAudioGen(0.5), inLo: -1.0, inHi: 1.0, outLo: 0.0, outHi: 100.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 75.0) < 1e-8)
|
||||
}
|
||||
|
||||
// MARK: - LinExp.Audio / Control
|
||||
|
||||
@Test func linExpAudioMatchesMusicMath() {
|
||||
let outLo: Double = 200.0
|
||||
let outHi: Double = 2000.0
|
||||
let gen: LinExp.Audio = LinExp.Audio(ConstantAudioGen(0.5), inLo: 0.0, inHi: 1.0, outLo: outLo, outHi: outHi)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = Double(linToExp(0.5, low: Double(outLo), high: Double(outHi)))
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-8 })
|
||||
}
|
||||
|
||||
@Test func linExpControlReachesTarget() {
|
||||
let outLo: Double = 200.0
|
||||
let outHi: Double = 2000.0
|
||||
let gen: LinExp.Control = LinExp.Control(ConstantAudioGen(0.5), inLo: 0.0, inHi: 1.0, outLo: outLo, outHi: outHi)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = Double(linToExp(0.5, low: Double(outLo), high: Double(outHi)))
|
||||
#expect(abs(samples[0][7] - expected) < 1e-8)
|
||||
}
|
||||
|
||||
// MARK: - MulAdd
|
||||
|
||||
@Test func tanhAudioMulAdd() {
|
||||
let gen: Tanh.Audio = Tanh.Audio(ConstantAudioGen(0.0), mul: 2.0, add: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// tanh(0) = 0; 0 * 2 + 1 = 1
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func clipAudioMulAdd() {
|
||||
let gen: Clip.Audio = Clip.Audio(ConstantAudioGen(0.5), lo: -1.0, hi: 1.0, mul: 2.0, add: 0.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func tanhControlMulAdd() {
|
||||
let gen: Tanh.Control = Tanh.Control(ConstantAudioGen(0.0), mul: 2.0, add: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// tanh(0)*2+1 = 1; ramp from 0 → 1, last sample = 1
|
||||
#expect(abs(samples[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Additional math generators
|
||||
|
||||
@MainActor
|
||||
@Suite("Abs")
|
||||
struct AbsTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPositive() {
|
||||
let s: [[Double]] = Abs.Audio(ConstantAudioGen(3.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 3.0) < 1e-10 })
|
||||
}
|
||||
@Test func audioNegative() {
|
||||
let s: [[Double]] = Abs.Audio(ConstantAudioGen(-3.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 3.0) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Abs.Control(ConstantAudioGen(-5.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 5.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Sign")
|
||||
struct SignTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPositive() {
|
||||
let s: [[Double]] = Sign.Audio(ConstantAudioGen(7.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 1.0 })
|
||||
}
|
||||
@Test func audioNegative() {
|
||||
let s: [[Double]] = Sign.Audio(ConstantAudioGen(-7.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == -1.0 })
|
||||
}
|
||||
@Test func audioZero() {
|
||||
let s: [[Double]] = Sign.Audio(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 0.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Sign.Control(ConstantAudioGen(3.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Floor")
|
||||
struct FloorTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPositive() {
|
||||
let s: [[Double]] = Floor.Audio(ConstantAudioGen(2.7)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 2.0 })
|
||||
}
|
||||
@Test func audioNegative() {
|
||||
let s: [[Double]] = Floor.Audio(ConstantAudioGen(-2.3)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == -3.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Floor.Control(ConstantAudioGen(2.9)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Ceil")
|
||||
struct CeilTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPositive() {
|
||||
let s: [[Double]] = Ceil.Audio(ConstantAudioGen(2.1)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 3.0 })
|
||||
}
|
||||
@Test func audioNegative() {
|
||||
let s: [[Double]] = Ceil.Audio(ConstantAudioGen(-2.7)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == -2.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Ceil.Control(ConstantAudioGen(1.1)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Round")
|
||||
struct RoundTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioRoundsUp() {
|
||||
let s: [[Double]] = Round.Audio(ConstantAudioGen(2.6)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 3.0 })
|
||||
}
|
||||
@Test func audioRoundsDown() {
|
||||
let s: [[Double]] = Round.Audio(ConstantAudioGen(2.4)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 2.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Round.Control(ConstantAudioGen(2.6)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 3.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Sqrt")
|
||||
struct SqrtTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioKnownValue() {
|
||||
let s: [[Double]] = Sqrt.Audio(ConstantAudioGen(9.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 3.0) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Sqrt.Control(ConstantAudioGen(4.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 2.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Reciprocal")
|
||||
struct ReciprocalTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioKnownValue() {
|
||||
let s: [[Double]] = Reciprocal.Audio(ConstantAudioGen(4.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 0.25) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Reciprocal.Control(ConstantAudioGen(2.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 0.5) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Log")
|
||||
struct LogTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioAtOne() {
|
||||
let s: [[Double]] = Log.Audio(ConstantAudioGen(1.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func audioKnownValue() {
|
||||
let s: [[Double]] = Log.Audio(ConstantAudioGen(Foundation.M_E)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Log.Control(ConstantAudioGen(Foundation.M_E)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Exp")
|
||||
struct ExpTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioAtZero() {
|
||||
let s: [[Double]] = Exp.Audio(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
@Test func audioAtOne() {
|
||||
let s: [[Double]] = Exp.Audio(ConstantAudioGen(1.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - Foundation.M_E) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Exp.Control(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Min (binary)")
|
||||
struct MinTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPicksSmaller() {
|
||||
let s: [[Double]] = Min.Audio(ConstantAudioGen(3.0), ConstantAudioGen(5.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 3.0 })
|
||||
}
|
||||
@Test func audioPicksNegative() {
|
||||
let s: [[Double]] = Min.Audio(ConstantAudioGen(-1.0), ConstantAudioGen(1.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == -1.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Min.Control(ConstantAudioGen(2.0), ConstantAudioGen(7.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Max (binary)")
|
||||
struct MaxTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioPicksLarger() {
|
||||
let s: [[Double]] = Max.Audio(ConstantAudioGen(3.0), ConstantAudioGen(5.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 5.0 })
|
||||
}
|
||||
@Test func audioPicksPositive() {
|
||||
let s: [[Double]] = Max.Audio(ConstantAudioGen(-1.0), ConstantAudioGen(1.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { $0 == 1.0 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Max.Control(ConstantAudioGen(2.0), ConstantAudioGen(7.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0][7] == 7.0)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("ExpLin")
|
||||
struct ExpLinTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioAtInLo() {
|
||||
// signal = inLo → t=0 → outLo
|
||||
let s: [[Double]] = ExpLin.Audio(ConstantAudioGen(200.0), inLo: 200.0, inHi: 2000.0, outLo: 0.0, outHi: 1.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func audioAtInHi() {
|
||||
// signal = inHi → t=1 → outHi
|
||||
let s: [[Double]] = ExpLin.Audio(ConstantAudioGen(2000.0), inLo: 200.0, inHi: 2000.0, outLo: 0.0, outHi: 1.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
@Test func audioInverseOfLinExp() {
|
||||
// geometric midpoint of [200, 2000] → ExpLin output = 0.5
|
||||
let freq: Double = Double(Foundation.sqrt(200.0 * 2000.0))
|
||||
let s: [[Double]] = ExpLin.Audio(ConstantAudioGen(freq), inLo: 200.0, inHi: 2000.0, outLo: 0.0, outHi: 1.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 0.5) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = ExpLin.Control(ConstantAudioGen(2000.0), inLo: 200.0, inHi: 2000.0, outLo: 0.0, outHi: 1.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("ExpExp")
|
||||
struct ExpExpTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioAtInLo() {
|
||||
// signal = inLo → t=0 → outLo
|
||||
let s: [[Double]] = ExpExp.Audio(ConstantAudioGen(100.0), inLo: 100.0, inHi: 1000.0, outLo: 200.0, outHi: 2000.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 200.0) < 1e-8 })
|
||||
}
|
||||
@Test func audioAtInHi() {
|
||||
// signal = inHi → t=1 → outHi
|
||||
let s: [[Double]] = ExpExp.Audio(ConstantAudioGen(1000.0), inLo: 100.0, inHi: 1000.0, outLo: 200.0, outHi: 2000.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 2000.0) < 1e-8 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = ExpExp.Control(ConstantAudioGen(1000.0), inLo: 100.0, inHi: 1000.0, outLo: 200.0, outHi: 2000.0)
|
||||
.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 2000.0) < 1e-8)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Ring1 Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("Ring1")
|
||||
struct Ring1Tests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
// a=1, b=2 → (1+1)*2 = 4
|
||||
@Test func audioCorrectFormula() {
|
||||
let gen: Ring1.Audio = Ring1.Audio(ConstantAudioGen(1.0), ConstantAudioGen(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 4.0
|
||||
#expect(Array(samples[0].prefix(8)).allSatisfy { abs($0 - expected) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func controlReachesTarget() {
|
||||
let gen: Ring1.Control = Ring1.Control(ConstantAudioGen(1.0), ConstantAudioGen(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 4.0
|
||||
#expect(abs(samples[0][7] - expected) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Ring2 Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("Ring2")
|
||||
struct Ring2Tests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
// a=1, b=2 → (1+1)*2 + 1 = 5
|
||||
@Test func audioCorrectFormula() {
|
||||
let gen: Ring2.Audio = Ring2.Audio(ConstantAudioGen(1.0), ConstantAudioGen(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 5.0
|
||||
#expect(Array(samples[0].prefix(8)).allSatisfy { abs($0 - expected) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func controlReachesTarget() {
|
||||
let gen: Ring2.Control = Ring2.Control(ConstantAudioGen(1.0), ConstantAudioGen(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 5.0
|
||||
#expect(abs(samples[0][7] - expected) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Ring3 Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("Ring3")
|
||||
struct Ring3Tests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
// a=2, b=3 → 2*2*3 = 12
|
||||
@Test func audioCorrectFormula() {
|
||||
let gen: Ring3.Audio = Ring3.Audio(ConstantAudioGen(2.0), ConstantAudioGen(3.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 12.0
|
||||
#expect(Array(samples[0].prefix(8)).allSatisfy { abs($0 - expected) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func controlReachesTarget() {
|
||||
let gen: Ring3.Control = Ring3.Control(ConstantAudioGen(2.0), ConstantAudioGen(3.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 12.0
|
||||
#expect(abs(samples[0][7] - expected) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Ring4 Tests
|
||||
|
||||
@MainActor
|
||||
@Suite("Ring4")
|
||||
struct Ring4Tests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
// a=2, b=3 → 2*2*3 - 2*3 = 12 - 6 = 6
|
||||
@Test func audioCorrectFormula() {
|
||||
let gen: Ring4.Audio = Ring4.Audio(ConstantAudioGen(2.0), ConstantAudioGen(3.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 6.0
|
||||
#expect(Array(samples[0].prefix(8)).allSatisfy { abs($0 - expected) < 1e-10 })
|
||||
}
|
||||
|
||||
@Test func controlReachesTarget() {
|
||||
let gen: Ring4.Control = Ring4.Control(ConstantAudioGen(2.0), ConstantAudioGen(3.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 6.0
|
||||
#expect(abs(samples[0][7] - expected) < 1e-10)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Cos")
|
||||
struct CosTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioCosOfZero() {
|
||||
let s: [[Double]] = Cos.Audio(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
@Test func audioCosOfPi() {
|
||||
let s: [[Double]] = Cos.Audio(ConstantAudioGen(Double.pi)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - (-1.0)) < 1e-10 })
|
||||
}
|
||||
@Test func audioCosOfHalfPi() {
|
||||
let s: [[Double]] = Cos.Audio(ConstantAudioGen(Double.pi / 2.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Cos.Control(ConstantAudioGen(Double.pi / 3.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 0.5) < 1e-10) // cos(π/3) = 0.5
|
||||
}
|
||||
@Test func generatorEnumConformance() {
|
||||
#expect(Cos.AudioType.self == Cos.Audio.self)
|
||||
#expect(Cos.ControlType.self == Cos.Control.self)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Sin")
|
||||
struct SinTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioSinOfZero() {
|
||||
let s: [[Double]] = Sin.Audio(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func audioSinOfHalfPi() {
|
||||
let s: [[Double]] = Sin.Audio(ConstantAudioGen(Double.pi / 2.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 })
|
||||
}
|
||||
@Test func audioSinOfPi() {
|
||||
let s: [[Double]] = Sin.Audio(ConstantAudioGen(Double.pi)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Sin.Control(ConstantAudioGen(Double.pi / 6.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 0.5) < 1e-10) // sin(π/6) = 0.5
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Tan")
|
||||
struct TanTests {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8); var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
init() throws { try engine.start() }
|
||||
|
||||
@Test func audioTanOfZero() {
|
||||
let s: [[Double]] = Tan.Audio(ConstantAudioGen(0.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0) < 1e-10 })
|
||||
}
|
||||
@Test func audioTanOfQuarterPi() {
|
||||
let s: [[Double]] = Tan.Audio(ConstantAudioGen(Double.pi / 4.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(s[0].prefix(8).allSatisfy { abs($0 - 1.0) < 1e-10 }) // tan(π/4) = 1
|
||||
}
|
||||
@Test func controlReachesTarget() {
|
||||
let s: [[Double]] = Tan.Control(ConstantAudioGen(Double.pi / 4.0)).calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(s[0][7] - 1.0) < 1e-10)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
// RangeMapperTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Darwin
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("RangeMapper — bipolar curve mapping")
|
||||
struct RangeMapperTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Endpoints — linear
|
||||
|
||||
@Test func linearAtMinusOneMapsToLow() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 220.0, high: 440.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 220.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func linearAtPlusOneMapsToHigh() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 220.0, high: 440.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 440.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func linearAtZeroMapsToMidpoint() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 220.0, high: 440.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 330.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Endpoints — exponential
|
||||
|
||||
@Test func exponentialAtMinusOneMapsToLow() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 200.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func exponentialAtPlusOneMapsToHigh() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 2000.0) < 1e-6 })
|
||||
}
|
||||
|
||||
@Test func exponentialAtZeroIsGeometricMean() {
|
||||
// t = 0.5 → low * exp(0.5 * log(high/low)) = sqrt(low * high)
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = (200.0 * 2000.0).squareRoot()
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-6 })
|
||||
}
|
||||
|
||||
// MARK: - Endpoints — power
|
||||
|
||||
@Test func powerTwoAtZeroIsQuarter() {
|
||||
// t = 0.5, t^2 = 0.25 → low + 0.25 * (high - low) = 0 + 25 = 25
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .power(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 25.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func powerHalfAtZeroIsSqrtHalf() {
|
||||
// t = 0.5, t^0.5 = sqrt(0.5) → low + sqrt(0.5)*(high-low)
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .power(0.5))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 100.0 * 0.5.squareRoot()
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func powerAtEndpointsIsExact() {
|
||||
let lo: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 0.0, high: 100.0, curve: .power(2.0))
|
||||
let hi: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 0.0, high: 100.0, curve: .power(2.0))
|
||||
let loS: [[Double]] = lo.calculatedSamples(graph: graph, count: 8)
|
||||
let hiS: [[Double]] = hi.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(loS[0].prefix(8).allSatisfy { abs($0 - 0.0) < 1e-9 })
|
||||
#expect(hiS[0].prefix(8).allSatisfy { abs($0 - 100.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Endpoints — curve(k)
|
||||
|
||||
@Test func curveAtEndpointsIsExact() {
|
||||
let lo: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 0.0, high: 100.0, curve: .curve(4.0))
|
||||
let hi: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 0.0, high: 100.0, curve: .curve(4.0))
|
||||
let loS: [[Double]] = lo.calculatedSamples(graph: graph, count: 8)
|
||||
let hiS: [[Double]] = hi.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(loS[0].prefix(8).allSatisfy { abs($0 - 0.0) < 1e-9 })
|
||||
#expect(hiS[0].prefix(8).allSatisfy { abs($0 - 100.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func curveAtMidpointMatchesFormula() {
|
||||
// t = 0.5, k = 4: low + (high-low) * (1 - exp(2)) / (1 - exp(4))
|
||||
let k: Double = 4.0
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .curve(k))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = 100.0 * (1.0 - Foundation.exp(0.5 * k)) / (1.0 - Foundation.exp(k))
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func curveNearZeroFallsThroughToLinear() {
|
||||
// |k| < rangeCurveLinearEpsilon (1e-4) → linear behaviour.
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .curve(1.0e-5))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// t = 0.5 → midpoint = 50
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 50.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Modulated low / high
|
||||
|
||||
@Test func modulatedLowRamps() {
|
||||
// Line ramps low from 100 to 200 over 1 block (8 samples @ 44100 Hz is fine — Line is control-rate, target is reached at end-of-block).
|
||||
let line: Line.Control = Line.Control(start: 100.0, end: 200.0, dur: 8.0 / 44100.0)
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: line, high: 1000.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// At input = -1 the output equals low exactly. Low is ramping 100 → 200 across the block.
|
||||
// Because RangeMapper reads `low` once per block via floatValue (the block target), the output
|
||||
// is constant within the block at the new target = 200.
|
||||
#expect(abs(samples[0][7] - 200.0) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func modulatedHighRamps() {
|
||||
let line: Line.Control = Line.Control(start: 500.0, end: 900.0, dur: 8.0 / 44100.0)
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(1.0), low: 100.0, high: line)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 900.0) < 1e-6)
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulAddAppliesAfterCurve() {
|
||||
// input -1 → low = 100; * 2 + 1 = 201
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 100.0, high: 200.0, mul: 2.0, add: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 201.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Control variant
|
||||
|
||||
@Test func controlLinearReachesTarget() {
|
||||
// -1 → 220; last sample of ramp = 220.
|
||||
let gen: RangeMapper.Control = RangeMapper.Control(ConstantAudioGen(-1.0), low: 220.0, high: 440.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 220.0) < 1e-9)
|
||||
}
|
||||
|
||||
@Test func controlExponentialReachesTarget() {
|
||||
let gen: RangeMapper.Control = RangeMapper.Control(ConstantAudioGen(0.0), low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = (200.0 * 2000.0).squareRoot()
|
||||
#expect(abs(samples[0][7] - expected) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func controlRampsBetweenBlockTargets() {
|
||||
let src: MutableControlGen = MutableControlGen(-1.0)
|
||||
let gen: RangeMapper.Control = RangeMapper.Control(src, low: 0.0, high: 100.0)
|
||||
// First block: target = 0.0 (from -1), ramp from 0.0 (initial) → 0.0.
|
||||
_ = gen.calculatedSamples(graph: graph, count: 8)
|
||||
advanceBlock(engine)
|
||||
// Second block: change source to +1 → target = 100.0.
|
||||
src.target = 1.0
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// Linear ramp from 0 to 100 across 8 samples: last sample == 100.
|
||||
#expect(abs(samples[0][7] - 100.0) < 1e-9)
|
||||
// Earlier samples are strictly below 100.
|
||||
#expect(samples[0][0] < 100.0)
|
||||
// Step size = (100 - 0) / 8 = 12.5; sample[0] = 12.5.
|
||||
#expect(abs(samples[0][0] - 12.5) < 1e-9)
|
||||
}
|
||||
|
||||
// MARK: - Method sugar
|
||||
|
||||
@Test func methodSugarMatchesExplicitConstruction() {
|
||||
// osc.range(low: 220, high: 440) ≡ RangeMapper.Audio(osc, low: 220, high: 440)
|
||||
let osc1: ConstantAudioGen = ConstantAudioGen(0.5)
|
||||
let osc2: ConstantAudioGen = ConstantAudioGen(0.5)
|
||||
let viaMethod: RangeMapper.Audio = osc1.range(low: 220.0, high: 440.0)
|
||||
let viaInit: RangeMapper.Audio = RangeMapper.Audio(osc2, low: 220.0, high: 440.0)
|
||||
let m: [[Double]] = viaMethod.calculatedSamples(graph: graph, count: 8)
|
||||
let i: [[Double]] = viaInit.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8)
|
||||
for k: Int in 0..<8 {
|
||||
#expect(abs(m[0][k] - i[0][k]) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func methodSugarPropagatesCurve() {
|
||||
let osc1: ConstantAudioGen = ConstantAudioGen(0.0)
|
||||
let osc2: ConstantAudioGen = ConstantAudioGen(0.0)
|
||||
let viaMethod: RangeMapper.Audio = osc1.range(low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let viaInit: RangeMapper.Audio = RangeMapper.Audio(osc2, low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let m: [[Double]] = viaMethod.calculatedSamples(graph: graph, count: 8)
|
||||
let i: [[Double]] = viaInit.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8)
|
||||
for k: Int in 0..<8 {
|
||||
#expect(abs(m[0][k] - i[0][k]) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func methodSugarOnControlReturnsControl() {
|
||||
let src: ConstantControlGen = ConstantControlGen(0.0)
|
||||
let wrapped: RangeMapper.Control = src.range(low: 220.0, high: 440.0)
|
||||
let samples: [[Double]] = wrapped.calculatedSamples(graph: graph, count: 8)
|
||||
// 0 → midpoint = 330; ramp ends at 330.
|
||||
#expect(abs(samples[0][7] - 330.0) < 1e-9)
|
||||
}
|
||||
|
||||
// MARK: - .power(n) shaping
|
||||
|
||||
@Test func powerWithPositiveN() {
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 0.0, high: 100.0, curve: .power(3.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
// t = 0.5, t^3 = 0.125 → 12.5
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 12.5) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func powerWithNegativeNMidpoint() {
|
||||
// n = -1, t = 0.5 → pow(0.5, -1) = 2 → low + 2*(high-low) = 100 + 200 = 300 (overshoots).
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(0.0), low: 100.0, high: 200.0, curve: .power(-1.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 300.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func powerWithNegativeNDivergesAtMinusOne() {
|
||||
// n = -1, t = 0 (input == -1 bipolar) → pow(0, -1) = +inf per IEEE 754.
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 100.0, high: 200.0, curve: .power(-1.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { $0.isInfinite && $0 > 0 })
|
||||
}
|
||||
|
||||
@Test func powerWithZeroNIsConstantHigh() {
|
||||
// pow(t, 0) = 1 for all t (including t == 0 by IEEE 754) → low + 1*(high-low) = high.
|
||||
let gen: RangeMapper.Audio = RangeMapper.Audio(ConstantAudioGen(-1.0), low: 100.0, high: 200.0, curve: .power(0.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 200.0) < 1e-9 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// UnipolarRangeMapperTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Darwin
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("UnipolarRangeMapper — unipolar curve mapping")
|
||||
struct UnipolarRangeMapperTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Endpoints
|
||||
|
||||
@Test func linearAtZeroMapsToLow() {
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.0), low: 80.0, high: 8000.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 80.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func linearAtOneMapsToHigh() {
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(1.0), low: 80.0, high: 8000.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 8000.0) < 1e-9 })
|
||||
}
|
||||
|
||||
@Test func linearAtHalfMapsToMidpoint() {
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 0.0, high: 100.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 50.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Curves
|
||||
|
||||
@Test func exponentialAtHalfIsGeometricMean() {
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 200.0, high: 2000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
let expected: Double = (200.0 * 2000.0).squareRoot()
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - expected) < 1e-6 })
|
||||
}
|
||||
|
||||
@Test func powerTwoAtHalfIsQuarter() {
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.5), low: 0.0, high: 100.0, curve: .power(2.0))
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 25.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulAddAppliesAfterCurve() {
|
||||
// input 0 → low = 100; * 2 + 1 = 201
|
||||
let gen: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(ConstantAudioGen(0.0), low: 100.0, high: 200.0, mul: 2.0, add: 1.0)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(samples[0].prefix(8).allSatisfy { abs($0 - 201.0) < 1e-9 })
|
||||
}
|
||||
|
||||
// MARK: - Control
|
||||
|
||||
@Test func controlReachesTarget() {
|
||||
let gen: UnipolarRangeMapper.Control = UnipolarRangeMapper.Control(ConstantAudioGen(1.0), low: 80.0, high: 8000.0, curve: .exponential)
|
||||
let samples: [[Double]] = gen.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 8000.0) < 1e-6)
|
||||
}
|
||||
|
||||
// MARK: - Method sugar
|
||||
|
||||
@Test func methodSugarMatchesExplicit() {
|
||||
let src1: ConstantAudioGen = ConstantAudioGen(0.5)
|
||||
let src2: ConstantAudioGen = ConstantAudioGen(0.5)
|
||||
let viaMethod: UnipolarRangeMapper.Audio = src1.unipolarRange(low: 80.0, high: 8000.0, curve: .exponential)
|
||||
let viaInit: UnipolarRangeMapper.Audio = UnipolarRangeMapper.Audio(src2, low: 80.0, high: 8000.0, curve: .exponential)
|
||||
let m: [[Double]] = viaMethod.calculatedSamples(graph: graph, count: 8)
|
||||
let i: [[Double]] = viaInit.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 8)
|
||||
for k: Int in 0..<8 {
|
||||
#expect(abs(m[0][k] - i[0][k]) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func methodSugarOnControlReturnsControl() {
|
||||
let src: ConstantControlGen = ConstantControlGen(1.0)
|
||||
let wrapped: UnipolarRangeMapper.Control = src.unipolarRange(low: 80.0, high: 8000.0)
|
||||
let samples: [[Double]] = wrapped.calculatedSamples(graph: graph, count: 8)
|
||||
#expect(abs(samples[0][7] - 8000.0) < 1e-9)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user