I'm in a situation where I need to export an audio file recorded with AudioKit, and then reimport it later for use some processing via AVAudioPCMBuffer.
Below is the code I'm using for exporting from AudioKit:
tape = recorder.audioFile!
player.load(audioFile: tape)
if let _ = player.audioFile?.duration {
recorder.stop()
tape.exportAsynchronously(name: "TempTestFile",
baseDir: .documents,
exportFormat: .caf) {_, exportError in
if let error = exportError {
AKLog("Export Failed \(error)")
} else {
AKLog("Export succeeded")
}
}
}
And here is where I'm trying to read back that audio file into my macOS app later, and fill an AVAudioPCMBuffer (where file is the audio file I'm trying to read):
let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: Double(sampleRate), channels: AVAudioChannelCount(channels), interleaved: interleaved)
if let buffer = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: AVAudioFrameCount(file.length)){
if ((try? file.read(into: buffer)) != nil) {
let arraySize = Int(buffer.frameLength)
switch type {
case is Double.Type:
let doublePointer = UnsafeMutablePointer<Double>.allocate(capacity: arraySize)
vDSP_vspdp(buffer.floatChannelData![0], 1, doublePointer, 1, vDSP_Length(arraySize))
return Array(UnsafeBufferPointer(start: doublePointer, count:arraySize)) as? [T]
case is Float.Type:
return Array(UnsafeBufferPointer(start: buffer.floatChannelData![0], count:arraySize)) as? [T]
default: return nil
}
}
}
However, I'm consistently getting an error of the following:
EXCEPTION (-50): "wrong number of buffers"
[avae] AVAEInternal.h:103:_AVAE_CheckNoErr: [AVAudioFile.mm:445:-[AVAudioFile readIntoBuffer:frameCount:error:]: (ExtAudioFileRead(_imp->_extAudioFile, &ioFrames, buffer.mutableAudioBufferList)): error -50
This is regardless of the file format used when exporting and importing the audio.
However, it does work fine if I'm using this .wav file that is read from directly inside the app.
Does anyone have any insights into why I can't seem to read the data from the audio file into an AVPCMBuffer.