Files
Lyrebird/Tests/LyrebirdTests/Buffers/RF64WriterTests.swift
T

158 lines
6.9 KiB
Swift
Raw 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.
// RF64WriterTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import AudioToolbox
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("RF64Writer")
struct RF64WriterTests {
// MARK: - Header golden bytes
@Test("RF64 header has RF64/WAVE/ds64/fmt/data structure")
func headerStructure() throws {
let url: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString).appendingPathExtension("wav")
defer { try? FileManager.default.removeItem(at: url) }
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 8, channelCount: 2)
try buffer.write(to: url, format: .rf64Float32, sampleRate: 48000)
let data: Data = try Data(contentsOf: url)
#expect(data.count >= 104)
func fourCC(_ off: Int) -> String { String(decoding: data[off..<off+4], as: UTF8.self) }
#expect(fourCC(0) == "RF64")
#expect(fourCC(8) == "WAVE")
#expect(fourCC(12) == "ds64")
#expect(fourCC(48) == "fmt ")
#expect(fourCC(96) == "data")
// RIFF-size and data-size sentinels.
func u32(_ off: Int) -> UInt32 {
data[off..<off+4].reversed().reduce(UInt32(0)) { ($0 << 8) | UInt32($1) }
}
#expect(u32(4) == 0xFFFF_FFFF) // RIFF size sentinel
#expect(u32(100) == 0xFFFF_FFFF) // data size sentinel
}
// MARK: - CoreAudio interop (RF64 read-back)
/// The definitive interop proof: write RF64 with RF64Writer, then reopen with
/// CoreAudio's `ExtAudioFile` (via `LyrebirdBuffer.load`). CoreAudio reads RF64,
/// so a successful load with matching samples proves the container is valid.
@Test("RF64 Float32 round-trips through CoreAudio reader")
func rf64Float32RoundTrip() throws {
let frameCount: Int = 128
let sampleRate: Double = 44100
let values: [Double] = (0..<frameCount).map { -1.0 + 2.0 * Double($0) / Double(frameCount - 1) }
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frameCount)
buffer.fill { i, _ in values[i] }
let url: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString).appendingPathExtension("wav")
defer { try? FileManager.default.removeItem(at: url) }
try buffer.write(to: url, format: .rf64Float32, sampleRate: sampleRate)
let loaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(loaded.count == frameCount)
#expect(loaded.channelCount == 1)
let reloadedRate: Double = try #require(loaded.sampleRate)
#expect(abs(reloadedRate - sampleRate) < 1.0)
for i: Int in 0..<frameCount {
#expect(abs(loaded[i] - values[i]) < 1e-5, "RF64 Float32 mismatch at \(i)")
}
}
@Test("RF64 Int24 round-trips through CoreAudio reader within one LSB")
func rf64Int24RoundTrip() throws {
let frameCount: Int = 128
let values: [Double] = (0..<frameCount).map { -1.0 + 2.0 * Double($0) / Double(frameCount - 1) }
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frameCount)
buffer.fill { i, _ in values[i] }
let url: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString).appendingPathExtension("wav")
defer { try? FileManager.default.removeItem(at: url) }
try buffer.write(to: url, format: .rf64Int24, sampleRate: 44100)
let loaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(loaded.count == frameCount)
let tolerance: Double = 2.0 / 8388608.0
for i: Int in 0..<frameCount {
#expect(abs(loaded[i] - values[i]) < tolerance, "RF64 Int24 mismatch at \(i)")
}
}
@Test("RF64 multichannel preserves channel order")
func rf64MultichannelRoundTrip() throws {
let frameCount: Int = 64
let channelCount: Int = 3
let ch: [[Double]] = [
(0..<frameCount).map { 0.01 * Double($0) },
(0..<frameCount).map { -0.02 * Double($0) },
(0..<frameCount).map { 0.5 - 0.03 * Double($0) }
]
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frameCount, channelCount: channelCount)
for c: Int in 0..<channelCount {
for f: Int in 0..<frameCount { buffer.setValue(ch[c][f], frame: f, channel: c) }
}
let url: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString).appendingPathExtension("wav")
defer { try? FileManager.default.removeItem(at: url) }
try buffer.write(to: url, format: .rf64Float32, sampleRate: 48000)
let reloaded: LyrebirdBuffer = try LyrebirdBuffer.loadAllChannels(url: url)
#expect(reloaded.channelCount == channelCount)
#expect(reloaded.count == frameCount)
for c: Int in 0..<channelCount {
for f: Int in 0..<frameCount {
#expect(abs(reloaded[f, c] - ch[c][f]) < 1e-5, "RF64 ch \(c) frame \(f) mismatch")
}
}
}
/// Odd `data`-payload length (mono Int24 × odd frames = odd byte count) must get a
/// RIFF word-alignment pad byte and still read back correctly through CoreAudio.
@Test("RF64 Int24 mono odd frame count round-trips (pad-byte path)")
func rf64Int24OddFramesPadByte() throws {
let frameCount: Int = 127 // 127 × 3 bytes = 381 (odd) → needs a pad byte
let values: [Double] = (0..<frameCount).map { -0.5 + Double($0) / Double(frameCount) }
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frameCount)
buffer.fill { i, _ in values[i] }
let url: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString).appendingPathExtension("wav")
defer { try? FileManager.default.removeItem(at: url) }
try buffer.write(to: url, format: .rf64Int24, sampleRate: 44100)
// File length must be even (header 104 + 381 data + 1 pad = 486).
let fileBytes: Int = try Data(contentsOf: url).count
#expect(fileBytes % 2 == 0)
let loaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(loaded.count == frameCount)
let tolerance: Double = 2.0 / 8388608.0
for i: Int in 0..<frameCount {
#expect(abs(loaded[i] - values[i]) < tolerance, "odd-frame Int24 mismatch at \(i)")
}
}
@Test("RF64 format is exempt from the 4 GB large-file guard")
func rf64ExemptFromCap() {
#expect(AudioFileFormat.rf64Float32.supportsLargeFiles == true)
#expect(AudioFileFormat.rf64Int24.supportsLargeFiles == true)
#expect(AudioFileFormat.rf64Float32.usesRF64Writer == true)
// riffOverflow must never flag RF64, even at absurd sizes.
#expect(AudioFileFormat.rf64Float32.riffOverflow(frameCount: Int.max / 8, channels: 8) == nil)
}
}