我在使用TPCircularBuffer时遇到了一些麻烦。
当我直接从我的回调缓冲区打印出样本时,我看到了正确的值,但是一旦我将它们复制到TPCircularBuffer中,它们就不正确了。
有什么想法吗?
我在测试应用中使用过TPCircularBuffer,一切正常。
非常感谢您的帮助!
这些是代码的相关部分:
// Declare and initialize TPCircularBuffer
TPCircularBuffer circularBuffer;
TPCircularBufferInit(&circularBuffer, 4096);
// Callback stores signed integers in inBuffer->mAudioData.
SInt16 * buffer = (SInt16*) inBuffer->mAudioData;
int32_t availableBytes;
// Each SInt16 is 2 bytes.
// So, this should place 512 SInt16 values from the buffer
// into the circularBuffer, right?
TPCircularBufferProduceBytes(&circularBuffer, &buffer, 1024);
SInt16 *ptr2 = TPCircularBufferTail(&circularBuffer, &availableBytes);
printf("Total byte size of buffer: %i\n", (int)availableBytes);
printf("circularBuffer[0] = %i\n", ptr2[0]);
printf("buffer[0] = %i\n", buffer[0]);
这是打印输出:
Total byte size of buffer: 1024
circularBuffer[0] = -16384
buffer[0] = 0
答案 0 :(得分:0)
我不太确定你在问什么,所以这里有一个简单的例子。
初始化:
TPCircularBuffer tpBuffer;
int secondsOfBuffer = 8;
float sampleRate = 44100.0;
int bufferByteSize = secondsOfBuffer * sampleRate * sizeof(SInt16);
TPCircularBufferInit(&tpBuffer, bufferByteSize);
写到头上:
int32_t availableBytes;
SInt16 *head = TPCircularBufferHead(&tpBuffer, &availableBytes);
int sampleCount = 1024;
for (int i = 0; i < 1024; i++) {
head[i] = i;
}
TPCircularBufferProduce(&tpBuffer, sampleCount * sizeof(SInt16));
从尾巴阅读:
SInt16 *tail = TPCircularBufferTail(&tpBuffer, &availableBytes);
int32_t availableBytes;
int availableSamples = availableBytes / sizeof(SInt16);
for (int i = 0; i < availableSamples; i++) {
printf("%i\n",tail[i]);
}
TPCircularBufferConsume(&tpBuffer, sizeof(SInt16) * sampleCount);