使用 AudioConverterRef 进行PCM采样率转换

2022-12-03  本文已影响0人  yxibng

demo

参考: https://github.com/yxibng/AudioConverterDemo

创建AudioConverterRef

OSStatus AudioConverterNew(      const AudioStreamBasicDescription * inSourceFormat,
                        const AudioStreamBasicDescription * inDestinationFormat,
                        AudioConverterRef __nullable * __nonnull outAudioConverter) 

参数说明:
inSourceFormat: 输入音频格式描述
inDestinationFormat: 目标音频格式描述

参考: 如何设置AudioStreamBasicDescription

给AudioConverterRef 喂数据,读取重采样后的数据

extern OSStatus
AudioConverterFillComplexBuffer(    AudioConverterRef                   inAudioConverter,
                                    AudioConverterComplexInputDataProc  inInputDataProc,
                                    void * __nullable                   inInputDataProcUserData,
                                    UInt32 *                            ioOutputDataPacketSize,
                                    AudioBufferList *                   outOutputData,
                                    AudioStreamPacketDescription * __nullable outPacketDescription)

参数说明:

inAudioConverter: 上一步创建的AudioConverterRef对象
inInputDataProc: 一个回调,用来提供重采样数据源,可能被重复调用
inInputDataProcUserData: 用户自定义数据,会被传递到inInputDataProc回调中
ioOutputDataPacketSize:

outOutputData: AudioBufferList, 用来存储此次调用读取到的重采样的数据
outPacketDescription: 针对非pcm的压缩格式,每个数据包的大小或时长,需要提供对一个的包描述

返回值:

AudioConverterComplexInputDataProc 提供的返回值有关系。

AudioConverterComplexInputDataProc

typedef OSStatus
(*AudioConverterComplexInputDataProc)(  AudioConverterRef               inAudioConverter,
                                        UInt32 *                        ioNumberDataPackets,
                                        AudioBufferList *               ioData,
                                        AudioStreamPacketDescription * __nullable * __nullable outDataPacketDescription,
                                        void * __nullable               inUserData)

通过该方法提供待重采样的音频数据。

参数说明:
inAudioConverter: 调用该方法的AudioConverterRef对象
ioNumberDataPackets

ioData

outPacketDescription: 针对非pcm的压缩格式,每个数据包的大小或时长,需要提供对一个的包描述
inUserData: AudioConverterFillComplexBuffer 调用时传入的用户数据

使用结束,释放

extern OSStatus
AudioConverterDispose(  AudioConverterRef   inAudioConverter)

注意

数据源如果是多声道,必须是non-interleaved的。

遇到的问题

崩溃在 CrashIfClientProvidedBogusAudioBufferList

Thread 3 Queue : com.apple.NSXPCConnection.user.endpoint (serial)
#0  0x00000001fbf6108c in CrashIfClientProvidedBogusAudioBufferList ()
#1  0x00000001f5c29f60 in acv1::AudioConverterChain::CallInputProc(unsigned int) ()
#2  0x00000001f5c43448 in acv1::AudioConverterChain::FillBufferFromInputProc(unsigned int*, CADeprecated::CABufferList*) ()
#3  0x00000001f5c55e94 in acv1::BufferedAudioConverter::GetInputBytes(unsigned int, unsigned int&, CADeprecated::CABufferList const*&) ()
#4  0x00000001f5c29a1c in acv1::CBRConverter::RenderOutput(CADeprecated::CABufferList*, unsigned int, unsigned int&, AudioStreamPacketDescription*) ()
#5  0x00000001f5c3a5a8 in acv1::BufferedAudioConverter::FillBuffer(unsigned int&, AudioBufferList&, AudioStreamPacketDescription*) ()
#6  0x00000001f5c55e64 in acv1::BufferedAudioConverter::GetInputBytes(unsigned int, unsigned int&, CADeprecated::CABufferList const*&) ()
#7  0x00000001f5d4e324 in acv1::Resampler2Wrapper::RenderOutput(CADeprecated::CABufferList*, unsigned int, unsigned int&) ()
#8  0x00000001f5e1bba0 in acv1::SampleRateConverter::RenderOutput(CADeprecated::CABufferList*, unsigned int, unsigned int&, AudioStreamPacketDescription*) ()
#9  0x00000001f5c3a5a8 in acv1::BufferedAudioConverter::FillBuffer(unsigned int&, AudioBufferList&, AudioStreamPacketDescription*) ()
#10 0x00000001f5c55e64 in acv1::BufferedAudioConverter::GetInputBytes(unsigned int, unsigned int&, CADeprecated::CABufferList const*&) ()
#11 0x00000001f5c29a1c in acv1::CBRConverter::RenderOutput(CADeprecated::CABufferList*, unsigned int, unsigned int&, AudioStreamPacketDescription*) ()
#12 0x00000001f5c3a5a8 in acv1::BufferedAudioConverter::FillBuffer(unsigned int&, AudioBufferList&, AudioStreamPacketDescription*) ()
#13 0x00000001f5c46304 in acv1::AudioConverterChain::RenderOutput(CADeprecated::CABufferList*, unsigned int, unsigned int&, AudioStreamPacketDescription*) ()
#14 0x00000001f5c3a5a8 in acv1::BufferedAudioConverter::FillBuffer(unsigned int&, AudioBufferList&, AudioStreamPacketDescription*) ()
#15 0x00000001f5c2b3c8 in acv1::_AudioConverterFillComplexBuffer(OpaqueAudioConverter*, int (*)(OpaqueAudioConverter*, unsigned int*, AudioBufferList*, AudioStreamPacketDescription**, void*), void*, unsigned int*, AudioBufferList*, AudioStreamPacketDescription*, AudioStreamPacketDependencyInfo*) ()

原因

The callback is given an audio buffer list pointed to by ioData. This buffer list may refer to
existing buffers owned and allocated by the audio converter, in which case the callback may
use them and copy input audio data into them. However, the buffer list may also be empty
(mDataByteSize == 0 and/or mData == NULL), in which case the callback must provide its own
buffers. The callback manipulates the members of ioData to point to one or more buffers
of audio data (multiple buffers are used with non-interleaved PCM data). The
callback is responsible for not freeing or altering this buffer until it is called again.

提供数据的 AudioConverterComplexInputDataProc , mData 可能为NULL, 尝试返回一个 mData = NULL 的 ioData 会报这个错。
经测试:iOS 16 mData 不为空,系统已经申请好了内存,iOS 15 mData 为NULL, 需要用户去申请内存。

上一篇下一篇

猜你喜欢

热点阅读