AudioQueue doesn’t produce any sound. What the problem can be?

  audiotoolbox, c++, core-audio, macos

I’ve written a small c++ app for testing AudioToolbox Queue. I am filling the buffer with some random data just to check it works.

static void HandleOutputBuffer(void* inUserData, AudioQueueRef queue, AudioQueueBufferRef inBuffer) {
    memset(inBuffer->mAudioData, rand() % 256, inBuffer->mAudioDataByteSize);
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, inBuffer, 0, NULL));
}

int main(int argc, const char * argv[]) {
    AudioQueueRef queue;
    AudioStreamBasicDescription format = {0};
    format.mBytesPerFrame = 4;
    format.mBitsPerChannel = 16;
    format.mChannelsPerFrame = 2;
    format.mFormatID = kAudioFormatLinearPCM;
    format.mFramesPerPacket = 1;
    format.mBytesPerPacket = format.mBytesPerFrame;
    format.mSampleRate = 44100;
    format.mReserved = 0;
    format.mFormatFlags =  kAudioFormatFlagIsSignedInteger |
            kAudioFormatFlagsNativeEndian |
            kLinearPCMFormatFlagIsPacked;
    throwExceptionIfError(AudioQueueNewOutput(&format, HandleOutputBuffer, NULL,
            NULL, // Use internal thread
            kCFRunLoopDefaultMode,
            0, // Reserved, must be 0
            &queue));

    AudioQueueBufferRef buffer;
    throwExceptionIfError(AudioQueueAllocateBuffer(queue, 1024, &buffer));
    buffer->mAudioDataByteSize = 1024;
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, buffer, 0, NULL));
    throwExceptionIfError(AudioQueueStart(queue, NULL));

    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
    }
    return NSApplicationMain(argc, argv);
}

HandleOutputBuffer is called continuously, I checked. However I can’t hear any sound. What the problem can be?

Source: Windows Questions C++

LEAVE A COMMENT