wav 的 AudioStreamBasicDescription 设置值

AudioStreamBasicDescription setting values for wav

我正在尝试在 iOS 上播放一个简单的 PCM 文件,但无法绕过 AudioStreamBasicDescription,而且 link 没有提供足够的信息。

我从终端获得这些值

afinfo BlameItOnTheNight.wav
File:           BlameItOnTheNight.wav
File type ID:   WAVE
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, 'lpcm' (0x0000000C) 16-bit little-endian signed integer
                no channel layout.
estimated duration: 9.938141 sec
audio bytes: 1753088
audio packets: 438272
bit rate: 1411200 bits per second
packet size upper bound: 4
maximum packet size: 4
audio data file offset: 44
optimized
source bit depth: I16
----

然后我在代码中选择值

- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
    format->mSampleRate = 44100.0;
    format->mFormatID = kAudioFormatLinearPCM;
    format->mFramesPerPacket = 1;
    format->mChannelsPerFrame = 2;
    format->mBytesPerFrame = format->mChannelsPerFrame * sizeof(Float32);
    format->mBytesPerPacket = format->mFramesPerPacket * format->mBytesPerFrame;
    format->mBitsPerChannel = sizeof(Float32) * 8;
    format->mReserved = 0;
    format->mFormatFlags =  kAudioFormatFlagIsSignedInteger |
    kAudioFormatFlagsNativeEndian |
    kAudioFormatFlagIsPacked;
}

音频播放速度非常快。

根据实际音频文件计算此值的正确方法是什么?

当我更改值时出现以下错误。

error for object 0x7fba72c50db8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug

然后我终于发现我的 AudioStreamBasicDescription bitsperchannel 值不正确,缓冲区大小也不够。

所以首先我将值更改为

- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
    format->mSampleRate = 44100.0;
    format->mFormatID = kAudioFormatLinearPCM;
    format->mFramesPerPacket = 1; //For uncompressed audio, the value is 1. For variable bit-rate formats, the value is a larger fixed number, such as 1024 for AAC
    format->mChannelsPerFrame = 2;
    format->mBytesPerFrame = format->mChannelsPerFrame * 2;
    format->mBytesPerPacket = format->mFramesPerPacket * format->mBytesPerFrame;
    format->mBitsPerChannel = 16;
    format->mReserved = 0;
    format->mFormatFlags =  kAudioFormatFlagIsSignedInteger |
    kAudioFormatFlagsNativeEndian |
    kLinearPCMFormatFlagIsPacked;
}

然后当我分配缓冲区时,我增加了大小

// Allocate and prime playback buffers
            playState.playing = true;
            for (int i = 0; i < NUM_BUFFERS && playState.playing; i++)
            {
                AudioQueueAllocateBuffer(playState.queue, 32000, &playState.buffers[i]);
                AudioOutputCallback(&playState, playState.queue, playState.buffers[i]);
            }

在我的原始代码中它被设置为8000,现在将其更改为32000 解决了问题。