// PortableFileIOTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @_spi(LyrebirdExtensions) import LyrebirdSynth // Exercises the off-Apple file-I/O paths — `LyrebirdBuffer.load` / `loadAllChannels` // / `fill` / `write` and `DiskIn.Audio` — through the portable `WavFileWriter` / // `WavFileReader` backend. Fixtures are written with `WavFileWriter` (portable, // both platforms) so these run everywhere, unlike the AudioToolbox-based // `LyrebirdBufferFileIOTests` in the macOS-only suite. On Apple these same paths // go through `ExtAudioFile`; a round trip that agrees on both is the point. @Suite("Portable file I/O — LyrebirdBuffer") struct PortableBufferFileIOTests { private func tempURL() -> URL { FileManager.default.temporaryDirectory .appendingPathComponent("lyrebird-fileio-\(UUID().uuidString).wav") } /// Distinct, in-range value for (frame, channel) so a transposed read shows up. private func sample(frame: Int, channel: Int) -> Float { Float((sin(Double(frame) * 0.02 + Double(channel) * 1.3)) * 0.6) } private func writeWAV(frames: Int, channels: Int, sampleRate: Double = 44100) throws -> URL { let url: URL = tempURL() var interleaved: [Float] = [Float](repeating: 0, count: frames * channels) for f: Int in 0.. URL { FileManager.default.temporaryDirectory .appendingPathComponent("lyrebird-diskin-\(UUID().uuidString).wav") } private func ramp(frame: Int, channel: Int) -> Float { Float(sin(Double(frame) * 0.013 + Double(channel))) * 0.5 } private func writeWAV(frames: Int, channels: Int, sampleRate: Double) throws -> URL { let url: URL = tempURL() var interleaved: [Float] = [Float](repeating: 0, count: frames * channels) for f: Int in 0.. fileFrames / 2) } #endif @Test func nonWavFileThrows() throws { let url: URL = tempURL() defer { try? FileManager.default.removeItem(at: url) } try Data([0, 1, 2, 3, 4, 5, 6, 7]).write(to: url) #expect(throws: (any Error).self) { _ = try DiskIn.Audio(url: url, channelCount: 1, sampleRate: 44100) } } }