关于ios:音频队列接口可以处理40ms音频帧吗?

Audio Queue Interface can deal with 40ms Audio Frame?

我正在尝试以40ms的时间段录制音频。
音频队列接口可以处理40ms音频帧?
如果"是",那么我们如何实现呢?

谢谢。


是的,可能的话,您需要相应地配置configure AudioQueue,

基本上,AudioQueue缓冲区的大小必须设置为40ms,所以大约为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int AQRecorder::ComputeRecordBufferSize(const AudioStreamBasicDescription *format, float seconds)
{
    int packets, frames, bytes = 0;
    try {
        frames = (int)ceil(seconds * format->mSampleRate);

        if (format->mBytesPerFrame > 0)
            bytes = frames * format->mBytesPerFrame;
        else {
            UInt32 maxPacketSize;
            if (format->mBytesPerPacket > 0)
                maxPacketSize = format->mBytesPerPacket;    // constant packet size
            else {
                UInt32 propertySize = sizeof(maxPacketSize);
                XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_MaximumOutputPacketSize, &maxPacketSize,
                                                    &propertySize),"couldn't get queue's maximum output packet size");
            }
            if (format->mFramesPerPacket > 0)
                packets = frames / format->mFramesPerPacket;
            else
                packets = frames;   // worst-case scenario: 1 frame in a packet
            if (packets == 0)       // sanity check
                packets = 1;
            bytes = packets * maxPacketSize;
        }
    } catch (CAXException e) {
        char buf[256];
        return 0;
    }  
    return bytes;
}

并设置格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void AQRecorder::SetupAudioFormat(UInt32 inFormatID)
{
    AudioStreamBasicDescription sRecordFormat;
    FillOutASBDForLPCM (sRecordFormat,
                        SAMPLING_RATE,
                        1,
                        8*BYTES_PER_PACKET,
                        8*BYTES_PER_PACKET,
                        false,
                        false
                        );
    memset(&mRecordFormat, 0, sizeof(mRecordFormat));

    mRecordFormat.SetFrom(sRecordFormat);
}

在我的情况下,

这些宏的值为

1
2
3
#define SAMPLING_RATE 16000
#define kNumberRecordBuffers    3
#define BYTES_PER_PACKET 2