AudioQueueInputCallback called with empty buffer

172 Views Asked by At

I try to record PCM audio with iPhone microphone but when I start my audio queue it stop itself after call my callback function as many times as there is buffer. In my case 3 times. This is my code :

class AudioModel {
    private var inQueue: AudioQueueRef!

    init() {
        // Describe PCM format. 8kHz, 16bits, mono
        var inFormat = AudioStreamBasicDescription(mSampleRate: 8000.0, // 8kb
            mFormatID: kAudioFormatLinearPCM,
            mFormatFlags: kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked,
            mBytesPerPacket: 2,
            mFramesPerPacket: 1,
            mBytesPerFrame: 2,
            mChannelsPerFrame: 1,
            mBitsPerChannel: 16,
            mReserved: 0)

        AudioQueueNewInput(&inFormat, audioQueueInputCallback, nil, nil, nil, 0, &inQueue)

        for _ in 0...2 {
            var bufferRef: AudioQueueBufferRef!
            AudioQueueAllocateBuffer(inQueue, 320, &bufferRef)
            AudioQueueEnqueueBuffer(inQueue, bufferRef, 0, nil)
        }
    }

    func startRecord() {
        AudioQueueStart(inQueue, nil)
    }

    func pauseRecord() {
        AudioQueuePause(inQueue)
    }

    private let audioQueueInputCallback: AudioQueueInputCallback = { (inUserData, inAQ, inBuffer, inStartTime, inNumberPacketDescriptions, inPacketDescs) in
        print("AudioQueueInputCallback called")
        print("mAudioDataByteSize: \(inBuffer.pointee.mAudioDataByteSize)")
        var err = AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nil)
        if err != noErr {
            print("AudioQueueEnqueueBuffer err: \(err)")
            return
        }
    }
}

When I call startRecord I've this out put and nothing else

AudioQueueInputCallback called
mAudioDataByteSize: 0
AudioQueueInputCallback called
mAudioDataByteSize: 0
AudioQueueInputCallback called
mAudioDataByteSize: 0

After 2 weeks to develop with Audio Queue Services API, some comportment still misteres for me.

0

There are 0 best solutions below