Files
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

265 lines
11 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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..<frames {
for c: Int in 0..<channels {
interleaved[f * channels + c] = sample(frame: f, channel: c)
}
}
let writer: WavFileWriter = try WavFileWriter(
url: url, channels: channels, sampleRate: sampleRate, encoding: .float32
)
try writer.write(interleaved: interleaved, frames: frames)
try writer.finalize()
return url
}
@Test func loadMonoRoundTrips() throws {
let frames: Int = 500
let url: URL = try writeWAV(frames: frames, channels: 1)
defer { try? FileManager.default.removeItem(at: url) }
let buffer: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(buffer.count == frames)
#expect(buffer.channelCount == 1)
for f: Int in 0..<frames {
#expect(abs(buffer[f, 0] - Double(sample(frame: f, channel: 0))) < 1e-6, "frame \(f)")
}
}
@Test func loadAllChannelsRoundTrips() throws {
let frames: Int = 400
let channels: Int = 2
let url: URL = try writeWAV(frames: frames, channels: channels)
defer { try? FileManager.default.removeItem(at: url) }
let buffer: LyrebirdBuffer = try LyrebirdBuffer.loadAllChannels(url: url)
#expect(buffer.count == frames)
#expect(buffer.channelCount == channels)
for f: Int in 0..<frames {
for c: Int in 0..<channels {
#expect(abs(buffer[f, c] - Double(sample(frame: f, channel: c))) < 1e-6,
"frame \(f) channel \(c)")
}
}
}
@Test func loadSelectsTheRequestedChannel() throws {
let frames: Int = 300
let url: URL = try writeWAV(frames: frames, channels: 2)
defer { try? FileManager.default.removeItem(at: url) }
let right: LyrebirdBuffer = try LyrebirdBuffer.load(url: url, channel: 1)
for f: Int in 0..<frames {
#expect(abs(right[f, 0] - Double(sample(frame: f, channel: 1))) < 1e-6, "frame \(f)")
}
}
@Test func loadChannelOutOfRangeThrows() throws {
let url: URL = try writeWAV(frames: 100, channels: 1)
defer { try? FileManager.default.removeItem(at: url) }
#expect(throws: LyrebirdBufferIOError.channelOutOfRange(requested: 1, available: 1)) {
_ = try LyrebirdBuffer.load(url: url, channel: 1)
}
}
@Test func fillTruncatesLongerFileAndPadsShorterFile() throws {
let fileFrames: Int = 200
let url: URL = try writeWAV(frames: fileFrames, channels: 1)
defer { try? FileManager.default.removeItem(at: url) }
// Buffer shorter than the file: read stops at the buffer's length.
let short: LyrebirdBuffer = LyrebirdBuffer(count: 120)
try short.fill(fromFile: url)
for f: Int in 0..<120 {
#expect(abs(short[f, 0] - Double(sample(frame: f, channel: 0))) < 1e-6, "short \(f)")
}
// Buffer longer than the file: the tail is zero-padded.
let long: LyrebirdBuffer = LyrebirdBuffer(count: 260)
try long.fill(fromFile: url)
for f: Int in 0..<fileFrames {
#expect(abs(long[f, 0] - Double(sample(frame: f, channel: 0))) < 1e-6, "long \(f)")
}
for f: Int in fileFrames..<260 {
#expect(long[f, 0] == 0, "pad \(f)")
}
}
@Test func loadNormalisesToUnitPeak() throws {
// A quiet file: the loudest sample is well under 1. Normalising must
// scale the peak to ±1 while preserving the waveform shape.
let frames: Int = 256
let url: URL = tempURL()
defer { try? FileManager.default.removeItem(at: url) }
var interleaved: [Float] = [Float](repeating: 0, count: frames)
for f: Int in 0..<frames { interleaved[f] = Float(sin(Double(f) * 0.05)) * 0.25 }
let writer: WavFileWriter = try WavFileWriter(url: url, channels: 1, sampleRate: 44100, encoding: .float32)
try writer.write(interleaved: interleaved, frames: frames)
try writer.finalize()
let buffer: LyrebirdBuffer = try LyrebirdBuffer.load(url: url, normalise: true)
var peak: Double = 0
for f: Int in 0..<frames { peak = max(peak, abs(buffer[f, 0])) }
#expect(abs(peak - 1.0) < 1e-5, "normalised peak \(peak) should be ~1")
}
@Test func writeThenLoadRoundTripsInt16() throws {
let frames: Int = 400
let url: URL = tempURL()
defer { try? FileManager.default.removeItem(at: url) }
let source: LyrebirdBuffer = LyrebirdBuffer(count: frames)
for f: Int in 0..<frames { source.setValue(Double(sample(frame: f, channel: 0)), frame: f, channel: 0) }
try source.write(to: url, format: .wavInt16, sampleRate: 44100)
let reloaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(reloaded.count == frames)
for f: Int in 0..<frames {
// 16-bit quantisation: one step is ~3e-5 of full scale.
#expect(abs(reloaded[f, 0] - source[f, 0]) < 1e-4, "frame \(f)")
}
}
#if !canImport(AudioToolbox)
@Test func writeToAppleOnlyContainerThrowsOffApple() throws {
let url: URL = tempURL()
defer { try? FileManager.default.removeItem(at: url) }
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 16)
// CAF has no portable encoder; the WAV encodings do.
#expect(throws: LyrebirdBufferIOError.unsupportedFormatOnPlatform(.cafFloat32)) {
try buffer.write(to: url, format: .cafFloat32)
}
}
#endif
}
@Suite("Portable file I/O — DiskIn")
struct PortableDiskInTests {
private func tempURL() -> 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..<frames {
for c: Int in 0..<channels { interleaved[f * channels + c] = ramp(frame: f, channel: c) }
}
let writer: WavFileWriter = try WavFileWriter(
url: url, channels: channels, sampleRate: sampleRate, encoding: .float32
)
try writer.write(interleaved: interleaved, frames: frames)
try writer.finalize()
return url
}
@Test func streamsFileAtMatchingRateSampleAccurate() throws {
// Engine rate == file rate → the read-rate scale is 1, and at play rate
// 1.0 the read position lands on integer frames, so the streamed output
// is the file's channel-0 samples exactly (no interpolation error).
let sampleRate: Double = 44100
let fileFrames: Int = 4000
let url: URL = try writeWAV(frames: fileFrames, channels: 2, sampleRate: sampleRate)
defer { try? FileManager.default.removeItem(at: url) }
let engine: SynthesisEngine = makePortableTestEngine(blockSize: 64, sampleRate: sampleRate)
try engine.start()
defer { engine.stop() }
let player: DiskIn.Audio = try DiskIn.Audio(
url: url, channelCount: 2, sampleRate: sampleRate, rate: 1.0
)
// 40 blocks × 64 = 2560 samples, all inside the first (synchronously
// filled) half — deterministic, no background-refill dependency.
let out: [Double] = collectPortableBlocks(player, engine: engine, blocks: 40, channel: 0)
for i: Int in 0..<out.count {
#expect(abs(out[i] - Double(ramp(frame: i, channel: 0))) < 1e-6, "sample \(i)")
}
}
// Portable-only: on Apple `ExtAudioFile` performs the sample-rate
// conversion inside `DiskIn`, so the read position advances at rate 1 and
// the exact frame mapping below does not hold. Rate-ratio folding is a
// property of the portable backend, so this test tracks that backend's guard.
#if !canImport(AudioToolbox)
@Test func foldsSampleRateRatioIntoPlaybackSpeed() throws {
// File at half the engine rate: the portable read-rate scale is 0.5, so
// the read position advances half a frame per output sample. Output
// sample 2k then lands on file frame k (integer, no interpolation error).
let fileRate: Double = 22050
let engineRate: Double = 44100
let fileFrames: Int = 3000
let url: URL = try writeWAV(frames: fileFrames, channels: 1, sampleRate: fileRate)
defer { try? FileManager.default.removeItem(at: url) }
let engine: SynthesisEngine = makePortableTestEngine(blockSize: 64, sampleRate: engineRate)
try engine.start()
defer { engine.stop() }
let player: DiskIn.Audio = try DiskIn.Audio(
url: url, channelCount: 1, sampleRate: engineRate, rate: 1.0
)
let out: [Double] = collectPortableBlocks(player, engine: engine, blocks: 30, channel: 0)
// The read position lands on integer file frames at every even output
// sample; agreement is to Float round-trip precision (the fixture and
// the stream both pass through 32-bit samples), which is looser than
// the sample-accurate matching-rate case above.
var k: Int = 0
while k * 2 < out.count {
#expect(abs(out[k * 2] - Double(ramp(frame: k, channel: 0))) < 1e-5, "even sample \(k * 2)")
k += 1
}
// And it is genuinely slower: output well past the file's own frame
// count is still inside the file, because playback consumes half a file
// frame per output sample.
#expect(out.count > 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)
}
}
}