// 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 = 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.. = 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 = src.baseAddress else { return } lyrebirdDeinterleave(base, into: &buses, frameCount: 2, channelCount: 2) } #expect(buses == [[1, 2]]) } }