v1.6.0 release: soundfile format expansion (WAV int depths + RF64 >4GB, AIFF/CAF, AAC/ALAC compressed, 4GB guard, unified AudioFileFormat)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-22 17:45:34 -07:00
parent 62b2f5dbec
commit 8eb64f883f
33 changed files with 2650 additions and 352 deletions
@@ -176,6 +176,21 @@ private func writeTempMultichannelWAV(channels: [[Float]], sampleRate: Double =
return url
}
/// Reopen a written file and assert its on-disk PCM bit depth. A pure error-tolerance
/// check cannot tell "correctly quantised to N-bit" from "quantiser silently disabled,
/// still Float32" — a lossless Float32 round-trip has error smaller than the N-bit LSB
/// tolerance and would pass. This positively confirms the integer encoding happened.
private func expectFileBitDepth(_ url: URL, _ expected: UInt32) throws {
var fileRef: ExtAudioFileRef?
#expect(ExtAudioFileOpenURL(url as CFURL, &fileRef) == noErr)
let ref: ExtAudioFileRef = try #require(fileRef)
defer { ExtAudioFileDispose(ref) }
var asbd: AudioStreamBasicDescription = AudioStreamBasicDescription()
var size: UInt32 = UInt32(MemoryLayout<AudioStreamBasicDescription>.size)
#expect(ExtAudioFileGetProperty(ref, kExtAudioFileProperty_FileDataFormat, &size, &asbd) == noErr)
#expect(asbd.mBitsPerChannel == expected)
}
// MARK: - load(url:channel:normalise:)
@MainActor
@@ -409,6 +424,62 @@ struct LyrebirdBufferWriteTests {
}
}
@Test("write + load round-trip for WAV Int16 stays within one LSB")
func roundTripWavInt16() throws {
// A ramp across [-1, 1] exercises the quantiser over its full range.
let frameCount: Int = 64
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: .wavInt16, sampleRate: 44100)
// Positively confirm the file is 16-bit — a tolerance-only check would pass
// even if the write silently stayed Float32.
try expectFileBitDepth(url, 16)
let loaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(loaded.count == frameCount)
// Int16 quantisation step = 1 / 32768 ≈ 3.05e-5. Allow ~2 LSB of slack
// to cover rounding-mode differences in ExtAudioFile's conversion.
let oneLSB: Double = 1.0 / 32768.0
let tolerance: Double = 2.0 * oneLSB
for i: Int in 0..<frameCount {
#expect(abs(loaded[i] - values[i]) < tolerance,
"Int16 round-trip error at \(i): \(loaded[i]) vs \(values[i])")
}
}
@Test("write + load round-trip for WAV Int24 stays within one LSB")
func roundTripWavInt24() throws {
let frameCount: Int = 64
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: .wavInt24, sampleRate: 44100)
try expectFileBitDepth(url, 24)
let loaded: LyrebirdBuffer = try LyrebirdBuffer.load(url: url)
#expect(loaded.count == frameCount)
// Int24 quantisation step = 1 / 8388608 ≈ 1.19e-7. Allow ~2 LSB of slack.
let oneLSB: Double = 1.0 / 8388608.0
let tolerance: Double = 2.0 * oneLSB
for i: Int in 0..<frameCount {
#expect(abs(loaded[i] - values[i]) < tolerance,
"Int24 round-trip error at \(i): \(loaded[i]) vs \(values[i])")
}
}
@Test("write produces a file of non-zero size")
func writeProducesNonEmptyFile() throws {
let buffer: LyrebirdBuffer = LyrebirdBuffer(count: 512)
@@ -637,6 +708,44 @@ struct LyrebirdBufferMultichannelWriteTests {
}
}
@Test("3-channel WAV Int24 write preserves channel order within one LSB")
func writeThreeChannelWavInt24RoundTrip() throws {
let frameCount: Int = 32
let channelCount: Int = 3
// Distinct per-channel signals so a channel swap would fail the assert.
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 frame: Int in 0..<frameCount {
buffer.setValue(ch[c][frame], frame: frame, 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: .wavInt24, sampleRate: 44100)
try expectFileBitDepth(url, 24)
let reloaded: LyrebirdBuffer = try LyrebirdBuffer.loadAllChannels(url: url)
#expect(reloaded.count == frameCount)
#expect(reloaded.channelCount == channelCount)
let tolerance: Double = 2.0 / 8388608.0 // ~2 LSB at 24-bit
for c: Int in 0..<channelCount {
for frame: Int in 0..<frameCount {
#expect(abs(reloaded[frame, c] - ch[c][frame]) < tolerance,
"Int24 channel \(c) frame \(frame) mismatch")
}
}
}
@Test("mono write + load backwards compatibility regression guard")
func writeMonoBackwardsCompat() throws {
let values: [Double] = [0.1, -0.2, 0.3, -0.4, 0.5]