gpt4 book ai didi

macos - 为什么音频队列输入回调中的数据包编号有所不同?

转载 作者:行者123 更新时间:2023-12-02 22:23:18 26 4
gpt4 key购买 nike

我使用Audio Queue Services在Mac OS X上录制PCM音频数据。它可以工作,但是我在回调中得到的帧数有所不同。

static void MyAQInputCallback(void *inUserData, AudioQueueRef inQueue, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription *inPacketDesc)

在我的音频输入队列的每次调用中,我希望获得5毫秒(240帧/ inNumPackets,48 kHz)的音频数据。

这是我使用的音频格式:
AudioStreamBasicDescription recordFormat = {0};
memset(&recordFormat, 0, sizeof(recordFormat));
recordFormat.mFormatID = kAudioFormatLinearPCM;
recordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
recordFormat.mBytesPerPacket = 4;
recordFormat.mFramesPerPacket = 1;
recordFormat.mBytesPerFrame = 4;
recordFormat.mChannelsPerFrame = 2;
recordFormat.mBitsPerChannel = 16;

我有两个960字节的缓冲区入队:
for (int i = 0; i < 2; ++i) {
AudioQueueBufferRef buffer;
AudioQueueAllocateBuffer(queue, 960, &buffer);
AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
}

我的问题:对于240帧( inNumPackets)的每204次,仅使用192帧调用一次回调。

为什么会发生这种情况,我能做些什么来不断获得240帧?

最佳答案

音频队列在音频单元的顶部运行。操作系统很可能会将音频单元缓冲区配置为2的幂,并且您返回的音频队列缓冲区会从较大的音频单元缓冲区中剔除。

204 * 240 + 192 = 12个4096的音频单元缓冲区。

如果您希望固定长度的缓冲区不是2的幂,则最好的办法是让应用程序将传入的缓冲区重新缓冲(保存直到有足够的数据)到所需的长度。无锁的圆形fifo /缓冲区可能适用于此目的。

关于macos - 为什么音频队列输入回调中的数据包编号有所不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com