// AudioFileFormatTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import AudioToolbox @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("AudioFileFormat") struct AudioFileFormatTests { // MARK: - Derived properties (golden per case) @Test("Container type IDs per case") func containerTypeIDs() { #expect(AudioFileFormat.wavFloat32.audioFileTypeID == kAudioFileWAVEType) #expect(AudioFileFormat.wavInt16.audioFileTypeID == kAudioFileWAVEType) #expect(AudioFileFormat.wavInt24.audioFileTypeID == kAudioFileWAVEType) #expect(AudioFileFormat.aiffFloat32.audioFileTypeID == kAudioFileAIFCType) #expect(AudioFileFormat.cafFloat32.audioFileTypeID == kAudioFileCAFType) } @Test("File extensions per case") func fileExtensions() { #expect(AudioFileFormat.wavFloat32.fileExtension == "wav") #expect(AudioFileFormat.wavInt16.fileExtension == "wav") #expect(AudioFileFormat.wavInt24.fileExtension == "wav") #expect(AudioFileFormat.aiffFloat32.fileExtension == "aiff") #expect(AudioFileFormat.cafFloat32.fileExtension == "caf") } @Test("Bit depth per case") func bitDepths() { #expect(AudioFileFormat.wavFloat32.fileBitsPerChannel == 32) #expect(AudioFileFormat.wavInt16.fileBitsPerChannel == 16) #expect(AudioFileFormat.wavInt24.fileBitsPerChannel == 24) #expect(AudioFileFormat.aiffFloat32.fileBitsPerChannel == 32) #expect(AudioFileFormat.cafFloat32.fileBitsPerChannel == 32) } @Test("Float vs integer encoding per case") func floatEncoding() { #expect(AudioFileFormat.wavFloat32.isFloatEncoding == true) #expect(AudioFileFormat.aiffFloat32.isFloatEncoding == true) #expect(AudioFileFormat.cafFloat32.isFloatEncoding == true) #expect(AudioFileFormat.wavInt16.isFloatEncoding == false) #expect(AudioFileFormat.wavInt24.isFloatEncoding == false) } @Test("Endianness per case (only AIFF-C is big-endian)") func endianness() { #expect(AudioFileFormat.aiffFloat32.isBigEndian == true) #expect(AudioFileFormat.wavFloat32.isBigEndian == false) #expect(AudioFileFormat.wavInt16.isBigEndian == false) #expect(AudioFileFormat.wavInt24.isBigEndian == false) #expect(AudioFileFormat.cafFloat32.isBigEndian == false) } // MARK: - Back-compat shorthands @Test("Back-compat shorthands map to Float32 cases") func backCompatShorthands() { #expect(AudioFileFormat.wav == .wavFloat32) #expect(AudioFileFormat.aiff == .aiffFloat32) #expect(AudioFileFormat.caf == .cafFloat32) } /// The deprecated `DiskOutFormat` alias still resolves for external source /// compatibility. Annotated deprecated so the alias reference does not raise an /// in-tree deprecation warning (the whole point of the alias is out-of-tree use). @available(*, deprecated, message: "Exercises the deprecated DiskOutFormat alias.") @Test("DiskOutFormat alias still resolves to AudioFileFormat") func diskOutFormatAliasResolves() { #expect(DiskOutFormat.wavFloat32 == AudioFileFormat.wavFloat32) #expect(DiskOutFormat.aiff == AudioFileFormat.aiffFloat32) } // MARK: - makeFileASBD golden fields @Test("File ASBD — WAV Float32 stereo") func fileASBDWavFloat32() { let asbd: AudioStreamBasicDescription = AudioFileFormat.wavFloat32.makeFileASBD(sampleRate: 48000, channels: 2) #expect(asbd.mSampleRate == 48000) #expect(asbd.mFormatID == kAudioFormatLinearPCM) #expect(asbd.mBitsPerChannel == 32) #expect(asbd.mChannelsPerFrame == 2) #expect(asbd.mFramesPerPacket == 1) #expect(asbd.mBytesPerFrame == 8) // 4 bytes/sample × 2 ch #expect(asbd.mBytesPerPacket == 8) #expect(asbd.mFormatFlags & kAudioFormatFlagIsFloat != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsPacked != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsBigEndian == 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger == 0) } @Test("File ASBD — WAV Int16 mono") func fileASBDWavInt16() { let asbd: AudioStreamBasicDescription = AudioFileFormat.wavInt16.makeFileASBD(sampleRate: 44100, channels: 1) #expect(asbd.mBitsPerChannel == 16) #expect(asbd.mBytesPerFrame == 2) // 2 bytes/sample × 1 ch #expect(asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsFloat == 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsBigEndian == 0) } @Test("File ASBD — WAV Int24 packed stereo") func fileASBDWavInt24() { let asbd: AudioStreamBasicDescription = AudioFileFormat.wavInt24.makeFileASBD(sampleRate: 96000, channels: 2) #expect(asbd.mBitsPerChannel == 24) #expect(asbd.mBytesPerFrame == 6) // 3 bytes/sample × 2 ch (packed) #expect(asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsPacked != 0) } @Test("File ASBD — AIFF-C Float32 is big-endian") func fileASBDAiffFloat32() { let asbd: AudioStreamBasicDescription = AudioFileFormat.aiffFloat32.makeFileASBD(sampleRate: 48000, channels: 1) #expect(asbd.mBitsPerChannel == 32) #expect(asbd.mBytesPerFrame == 4) #expect(asbd.mFormatFlags & kAudioFormatFlagIsFloat != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsBigEndian != 0) } // MARK: - makeClientASBD always native Float32 @Test("Client ASBD is native-endian Float32 regardless of file encoding") func clientASBDAlwaysFloat32() { for format: AudioFileFormat in [.wavFloat32, .wavInt16, .wavInt24, .aiffFloat32, .cafFloat32] { let asbd: AudioStreamBasicDescription = format.makeClientASBD(sampleRate: 48000, channels: 2) #expect(asbd.mFormatID == kAudioFormatLinearPCM) #expect(asbd.mBitsPerChannel == 32) #expect(asbd.mBytesPerFrame == 8) // Float32 × 2 ch #expect(asbd.mFormatFlags & kAudioFormatFlagIsFloat != 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsBigEndian == 0) #expect(asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger == 0) } } // MARK: - Large-file guard @Test("supportsLargeFiles is true only for CAF") func supportsLargeFiles() { #expect(AudioFileFormat.cafFloat32.supportsLargeFiles == true) for format: AudioFileFormat in [.wavFloat32, .wavInt16, .wavInt24, .aiffFloat32] { #expect(format.supportsLargeFiles == false) } } @Test("projectedByteCount tracks the format's bit depth") func projectedByteCount() { let frames: Int = 100 let channels: Int = 2 let bytesFloat32: Int = 4 let bytesInt16: Int = 2 let bytesInt24: Int = 3 #expect(AudioFileFormat.wavFloat32.projectedByteCount(frameCount: frames, channels: channels) == frames * channels * bytesFloat32) #expect(AudioFileFormat.wavInt16.projectedByteCount(frameCount: frames, channels: channels) == frames * channels * bytesInt16) #expect(AudioFileFormat.wavInt24.projectedByteCount(frameCount: frames, channels: channels) == frames * channels * bytesInt24) #expect(AudioFileFormat.aiffFloat32.projectedByteCount(frameCount: frames, channels: channels) == frames * channels * bytesFloat32) } @Test("riffOverflow flags an oversize WAV at the boundary and exempts CAF") func riffOverflowBoundary() { let cap: Int = lyrebirdMaxRIFFBytes let bytesPerSampleFloat32: Int = 4 // Largest mono Float32 frame count that still fits under the cap. let underFrames: Int = cap / bytesPerSampleFloat32 #expect(AudioFileFormat.wavFloat32.riffOverflow(frameCount: underFrames, channels: 1) == nil) // One frame past that overflows (the cap's remainder mod 4 < 4 guarantees crossing). let over: (projected: Int, cap: Int)? = AudioFileFormat.wavFloat32.riffOverflow(frameCount: underFrames + 1, channels: 1) #expect(over != nil) #expect(over?.cap == cap) #expect((over?.projected ?? 0) > cap) // AIFF-C is likewise 32-bit-capped. #expect(AudioFileFormat.aiffFloat32.riffOverflow(frameCount: underFrames + 1, channels: 1) != nil) // CAF is 64-bit — never flagged, even for absurd sizes. (Returns nil before // projectedByteCount runs, so the multiply cannot overflow Int.) #expect(AudioFileFormat.cafFloat32.riffOverflow(frameCount: Int.max / 8, channels: 8) == nil) } @Test("projectedByteCount / riffOverflow saturate instead of trapping on overflow") func projectedByteCountOverflowSafe() { // A frame count large enough to overflow the byte product must saturate to // Int.max (no trap) and be reported as over-cap for a 32-bit container. #expect(AudioFileFormat.wavFloat32.projectedByteCount(frameCount: Int.max, channels: 8) == Int.max) let over: (projected: Int, cap: Int)? = AudioFileFormat.wavFloat32.riffOverflow(frameCount: Int.max, channels: 8) #expect(over != nil) #expect(over?.projected == Int.max) } @Test("LyrebirdBuffer.write throws fileTooLargeForContainer for oversize WAV projection") func bufferWriteGuardDecision() { // The decision the buffer guard makes is exactly riffOverflow — asserted here // without allocating a multi-GB buffer. A 200M-frame × 8ch Float32 projection // is ~6.4 GB > cap. let frames: Int = 200_000_000 let channels: Int = 8 #expect(AudioFileFormat.wavFloat32.riffOverflow(frameCount: frames, channels: channels) != nil) #expect(AudioFileFormat.cafFloat32.riffOverflow(frameCount: frames, channels: channels) == nil) } // MARK: - Round-trip: write via factory-backed LyrebirdBuffer.write, reload /// Write a mono ramp buffer in each Float32 container and reload it, confirming /// sample rate, frame count, and channel count survive the round-trip. @Test("Round-trip write→load preserves rate/frames/channels", arguments: [ AudioFileFormat.wavFloat32, .aiffFloat32, .cafFloat32 ]) func roundTripFloat32(format: AudioFileFormat) throws { let frameCount: Int = 256 let sampleRate: Double = 44100 let buffer: LyrebirdBuffer = LyrebirdBuffer(count: frameCount) for i: Int in 0...size) let status: OSStatus = ExtAudioFileGetProperty( fileRef2, kExtAudioFileProperty_FileDataFormat, &size, &fileASBD ) #expect(status == noErr) #expect(fileASBD.mBitsPerChannel == format.fileBitsPerChannel) let isFloat: Bool = fileASBD.mFormatFlags & kAudioFormatFlagIsFloat != 0 #expect(isFloat == format.isFloatEncoding) } }