I'm currently trying to write microphone data into a file using AudioQueueInputCallback
. My code callback looks like this:
func myAQInputCallback(inUserData: UnsafeMutableRawPointer?,
inQueue: AudioQueueRef,
inBuffer: AudioQueueBufferRef,
inStartTime: UnsafePointer<AudioTimeStamp>,
inNumPackets: UInt32,
inPacketDesc: UnsafePointer<AudioStreamPacketDescription>?) {
var error: OSStatus
var recorder = inUserData!.load(as: MyRecorder.self)
// Is this neccessary?
var tmpInOutNumPackets = inNumPackets
if inNumPackets > 0 {
error = AudioFileWritePackets(recorder.recordFile!,
false,
inBuffer.pointee.mAudioDataByteSize,
inPacketDesc,
recorder.recordPacket,
&tmpInOutNumPackets,
inBuffer.pointee.mAudioData)
checkError(error, msg: "Couldn't write packet to audio file.")
recorder.recordPacket += Int64(inNumPackets)
}
if recorder.running {
error = AudioQueueEnqueueBuffer(inQueue, inBuffer, 0, nil)
checkError(error, msg: "Couldn't enqueue buffer.")
}
}
Where MyRecorder
is a struct
looking like this:
struct MyRecorder {
var recordFile: AudioFileID?
var recordPacket: Int64
var running: Bool
}
The code fails at the AudioFileWritePackets
call (with errorCode: 1885563711 - 'kcp'
). What could case the failure here?
I'd be happy to provide further information, but currently I don't know which pieces of code are helpful here.
Also as you can see I'm creating an temporary copy of the function argument inNumPackets
, since otherwise AudioFileWritePackets
won't accept &inNumPackets
as an arguments (because it's an immutable let
-constant).