Files
Lyrebird/Tests/LyrebirdPortableTests/InterleaveHelpersTests.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

88 lines
3.3 KiB
Swift

// InterleaveHelpersTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@_spi(LyrebirdExtensions) import LyrebirdSynth
// Planar↔interleaved conversion is the part of a hardware driver that fails
// quietly: a transposed index produces channel-swapped or aliased audio rather
// than an error, and on a headless CI box nobody hears it. These run everywhere.
@Suite("Driver channel interleaving")
struct InterleaveHelpersTests {
private func interleaved(
_ buses: [[Float]],
frameCount: Int,
channelCount: Int
) -> [Float] {
var out: [Float] = [Float](repeating: .nan, count: frameCount * channelCount)
out.withUnsafeMutableBufferPointer { dst in
guard let base: UnsafeMutablePointer<Float> = dst.baseAddress else { return }
lyrebirdInterleave(buses, into: base, frameCount: frameCount, channelCount: channelCount)
}
return out
}
@Test func interleavesStereoInFrameOrder() {
let buses: [[Float]] = [
[1, 2, 3],
[-1, -2, -3],
]
let out: [Float] = interleaved(buses, frameCount: 3, channelCount: 2)
#expect(out == [1, -1, 2, -2, 3, -3])
}
@Test func missingChannelIsWrittenAsSilence() {
// Engine configured for more output channels than buses provided.
let buses: [[Float]] = [[1, 2]]
let out: [Float] = interleaved(buses, frameCount: 2, channelCount: 3)
#expect(out == [1, 0, 0, 2, 0, 0])
}
@Test func shortBusIsPaddedWithSilence() {
let buses: [[Float]] = [
[1, 2],
[-1, -2, -3, -4],
]
// Channel 0 runs out after two frames; the rest of its frames are
// silence while channel 1 keeps playing.
let out: [Float] = interleaved(buses, frameCount: 4, channelCount: 2)
#expect(out == [1, -1, 2, -2, 0, -3, 0, -4])
}
@Test func deinterleaveIsTheInverseOfInterleave() {
let frameCount: Int = 5
let channelCount: Int = 3
let original: [[Float]] = (0..<channelCount).map { channel in
(0..<frameCount).map { Float(channel * 100 + $0) }
}
let packed: [Float] = interleaved(original, frameCount: frameCount, channelCount: channelCount)
var recovered: [[Float]] = [[Float]](
repeating: [Float](repeating: 0, count: frameCount),
count: channelCount
)
packed.withUnsafeBufferPointer { src in
guard let base: UnsafePointer<Float> = src.baseAddress else { return }
lyrebirdDeinterleave(base, into: &recovered, frameCount: frameCount, channelCount: channelCount)
}
#expect(recovered == original)
}
@Test func deinterleaveIgnoresChannelsWithNoDestinationBus() {
// Two channels arriving, one staging bus: the extra channel is dropped
// rather than overrunning the array.
let packed: [Float] = [1, -1, 2, -2]
var buses: [[Float]] = [[Float](repeating: 0, count: 2)]
packed.withUnsafeBufferPointer { src in
guard let base: UnsafePointer<Float> = src.baseAddress else { return }
lyrebirdDeinterleave(base, into: &buses, frameCount: 2, channelCount: 2)
}
#expect(buses == [[1, 2]])
}
}