使用 AudioConverterRef 进行PCM采样率转换
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
- 用于保存输入源提供的数据,你负责填充该buffer
- 实际填充了多少个包,通过ioNumberDataPackets告诉调用者
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, 需要用户去申请内存。