[英]Accessing raw audio data with Audio Queue using AudioFileReadPackets
我正在嘗試訪問發送到Audio Queue回調的原始音頻數據,但是在調用AudioFileReadPackets之后,我的字節數(numBytes)為0。
void AQRecorder::MyInputBufferHandler(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp * inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription* inPacketDesc) {
AQRecorder *aqr = (AQRecorder *)inUserData;
try {
if (inNumPackets > 0) {
//read packets
UInt32 numBytes;
OSStatus result = AudioFileReadPackets(aqr->mRecordFile, // The audio file from which packets of audio data are to be read.
FALSE, // Set to true to cache the data. Otherwise, set to false.
&numBytes, // On output, a pointer to the number of bytes actually returned.
(__bridge AudioStreamPacketDescription*)inPacketDesc, // A pointer to an array of packet descriptions that have been allocated.
aqr->mRecordPacket, // The packet index of the first packet you want to be returned.
&inNumPackets, // On input, a pointer to the number of packets to read. On output, the number of packets actually read.
inBuffer->mAudioData); // A pointer to user-allocated memory.
inBuffer->mAudioDataByteSize = numBytes;
SInt16 *testBuffer = (SInt16*)inBuffer->mAudioData;
for (int i=0; i < numBytes; i++)
{
UInt16 currentData = testBuffer[i];
printf("Current data in testbuffer is %d", currentData);
}
// if we're not stopping, re-enqueue the buffe so that it gets filled again
if (aqr->IsRunning())
XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
} catch (CAXException e) {
char buf[256];
fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
}
}
當我僅打印testBuffer的第一個索引時,我能夠獲得0到4294967296之間的值,因此它似乎不是空的。 有什么想法為什么AudioFileReadPackets執行后numBytes為0?
我仍然不確定為什么numBytes為0,但是我從這個問題中找到了可行的解決方案。
為了獲得testBuffer的大小,我使用了以下代碼:
int sampleCount = inBuffer->mAudioDataBytesCapacity / sizeof(SInt16);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.