使用 AudioQueue 读取音频缓冲区数据
我正在尝试通过 AudioQueue 读取音频数据。 当我这样做时,我可以验证文件的位深度是否为 16 位。 但是当我获得实际的样本数据时,我只看到从 -128 到 128 的值。但我也看到可疑的交错数据,这让我非常确定我只是没有正确读取数据。
首先,我可以验证源文件是 44100、16 位、单声道 wav 文件。
我的缓冲区是这样分配的:
char *buffer= NULL; buffer = malloc(BUFFER_SIZE); assert(buffer);
所有相关的值都被设置并用于:
AudioFileReadPackets(inAudioFile,false,&bytesRead,NULL,packetNum,&numPackets,buffer);
作为测试,只是为了让我可以看到检索到的数据,我运行:
for(int i=0;i<BUFFER_SIZE;i++){ NSLog(@"%i", buffer[i]); }
现在,我知道我的源文件到处都是峰值,但是我的值只能看到最大值为 -128 和 128。由于这是一个 16 位文件,我预计这些值将是 -32768 到 32768。
此外,数据中似乎有两种模式。 下面是返回数据的示例:
70 -13 31 -11 -118 -9 -15 -7 116 -4 31 -1 28 1 84 2 -123 3 -97 4 110 5 54 6 126
现在查看从第二行开始的每隔一行:-13。 看看它是如何增加的,不是均匀的,但至少是平稳的? 奇数行远没有那么平滑。
我的第一个想法是这是交错的立体声数据,但不是,它只有一个通道,所以不应该有任何交错,对吗?
我最好的猜测是我只是错误地读取了数据,因此示例数据跨越了两个返回。 知道如何正确阅读吗?
感谢您阅读整个问题以及您可以提供的任何见解。
I am attempting to read audio data via AudioQueue. When I do so, I can verify that the bit depth of the file is 16-bit. But when I get the actual sample data, I'm only seeing values from -128 to 128. But I'm also seeing suspicious looking interleaved data, which makes me pretty sure that I'm just not reading the data correctly.
So to begin with, I can verify that the source file is 44100, 16-bit, mono wav file.
My buffer is allocated thusly:
char *buffer= NULL; buffer = malloc(BUFFER_SIZE); assert(buffer);
All the relevant values are set and used in:
AudioFileReadPackets(inAudioFile,false,&bytesRead,NULL,packetNum,&numPackets,buffer);
As a test, just so that I can see the data retrieved, I run:
for(int i=0;i<BUFFER_SIZE;i++){ NSLog(@"%i", buffer[i]); }
Now, I know my source file peaks all over the place, but the values I see only max at -128 and 128. Being as this is a 16-bit file, I would expect that the values would instead be -32768 to 32768.
In addition, there seems to be two patterns in the data. Here's an example of the data returned:
70 -13 31 -11 -118 -9 -15 -7 116 -4 31 -1 28 1 84 2 -123 3 -97 4 110 5 54 6 126
Now take a look at every other row starting with the second row: -13. See how it increases, not evenly, but at least smoothly? The odd-numbered rows aren't anywhere near that smooth.
My first thought would be that this is interleaved stereo data, but no, it's only one channel, so there shouldn't be any interleaving, right?
My best guess is that I'm just reading the data incorrectly, so the sample data is being spanned across two returns. Any idea how to read it correctly?
Thanks for reading through the whole question, and for any insight you can offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是原因。 您正在迭代有符号的字节,而不是 16 位样本。
将变量声明为保存指向两字节值的指针:然后
,迭代字节数一半以上的样本:
我会将
BUFFER_SIZE
重命名为BUFFER_SIZE_BYTES
以澄清它。That's the reason. You're iterating over signed bytes, not 16-bit samples.
Declare the variable as holding a pointer to two-byte values instead:
Then, iterate over half as many samples as bytes:
I would rename
BUFFER_SIZE
toBUFFER_SIZE_BYTES
to clarify it.