Here is some code I have to create an AVAudioFile instance based on Int16 samples.
let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100.0, channels: 2, interleaved: false)!
let audioFile = try AVAudioFile(forWriting: outputURL, settings: format.settings)
When writing to the file I get the following runtime error, presumably from CoreAudio.
CABufferList.h:184 ASSERTION FAILURE [(nBytes <= buf->mDataByteSize) != 0 is false]:
I read this as a size mismatch between what is specified in the format used to create the file and the file's own internal processingFormat
property, which is read-only. Here is my debugger console output showing the input format I created, along with the resulting AVAudioFile
fileFormat
and processingFormat
properties.
(lldb) po format
<AVAudioFormat 0x300e553b0: 2 ch, 44100 Hz, Int16, deinterleaved>
(lldb) po format.settings
▿ 7 elements
▿ 0 : 2 elements
- key : "AVNumberOfChannelsKey"
- value : 2
▿ 1 : 2 elements
- key : "AVLinearPCMBitDepthKey"
- value : 16
▿ 2 : 2 elements
- key : "AVFormatIDKey"
- value : 1819304813
▿ 3 : 2 elements
- key : "AVLinearPCMIsNonInterleaved"
- value : 1
▿ 4 : 2 elements
- key : "AVLinearPCMIsBigEndianKey"
- value : 0
▿ 5 : 2 elements
- key : "AVLinearPCMIsFloatKey"
- value : 0
▿ 6 : 2 elements
- key : "AVSampleRateKey"
- value : 44100
(lldb) po audioFile.fileFormat
<AVAudioFormat 0x300ea5400: 2 ch, 44100 Hz, Int16, interleaved>
(lldb) po audioFile.processingFormat
<AVAudioFormat 0x300ea5450: 2 ch, 44100 Hz, Float32, deinterleaved>
Please note that the input format I'm using does not match either the audio file fileFormat
or processingFormat
properties. The file format is interleaved even though I specified de-interleaved. This makes sense to me as working with audio files that are growing is much easier and more efficient with interleaved data. The head-scratcher is the processingFormat
. I specified Int16
samples and it is expecting Float32
? According to the format settings dictionary, we are specifying the correct key/value pairs.
Is this expected behavior? Does Apple always insist on Float32 internally or is this a bug?