1

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).

1 Answers1

1

That error is actually 'pck?' or kAudioFileInvalidPacketOffsetError, which is described as

A packet offset was past the end of the file, or not at the end of the file when writing a VBR format, or a corrupt packet size was read when building the packet table.

The problem is probably to do with the fact that you're not using the actual number of packets written, which is found in tmpInOutNumPackets after AudioFileWritePackets returns.

So try

recorder.recordPacket += Int64(tmpInOutNumPackets)

instead.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Thank you for your answer. The change didn't fix my problem, but the error description definitely helped me. I found, that `record.recordPacket` doesn't change after one callback call (like the `struct` is passed by value - even though I used `UnsafeMutableRawPointer?`). [Here is a follow-up question.](http://stackoverflow.com/questions/42596246/swift-3-pass-struct-by-reference-via-unsafemutablerawpointer). –  Mar 04 '17 at 12:56
  • aha - well spotted – Rhythmic Fistman Mar 04 '17 at 21:38