AudioQueue는 소리를 내지 않습니다. 문제가 무엇일까요? [복제]

Dec 08 2020

AudioToolbox Queue를 테스트하기 위해 작은 C ++ 앱을 작성했습니다. 버퍼가 작동하는지 확인하기 위해 임의의 데이터로 버퍼를 채우고 있습니다.

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가 계속 호출되어 확인했습니다. 하지만 아무 소리도 들리지 않습니다. 문제가 무엇일까요?

답변

1 RhythmicFistman Dec 08 2020 at 05:03

코드에있는 유일한 문제는 충분한 버퍼를 대기열에 넣지 않는다는 것입니다. AudioQueues최소한 두 가지가있는 것 같습니다.

AudioQueueBufferRef buffer;
for (int i = 0; i < 2; i++) {
    throwExceptionIfError(AudioQueueAllocateBuffer(queue, 1024, &buffer));
    buffer->mAudioDataByteSize = 1024;
    throwExceptionIfError(AudioQueueEnqueueBuffer(queue, buffer, 0, NULL));
}