美文网首页
使用 AudioConverterRef 进行PCM采样率转换

使用 AudioConverterRef 进行PCM采样率转换

作者: yxibng | 来源:发表于2022-12-03 22:45 被阅读0次

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, 需要用户去申请内存。

相关文章

  • 音视频-AAC编码

    采样格式 必须是16位整数PCM。 采样率 支持的采样率有(Hz): 8000、11025、12000、16000...

  • iOS音频(基础篇)-常用的音频格式

    PCM格式 PCM属于编码格式,PCM是经过话筒后直接得到的未经压缩的数据流数据的大小 = 采样率 * 采样位数 ...

  • FFmpeg录制音频PCM,WAV, PCM 转WAV

    音频录制与播放命令 录制 播放播放PCM需要指定相关参数: ar:采样率 ac:声道数 f:采样格式 PCM音频录...

  • 使用Java分离音频左右声道

    使用Java分离音频左右声道 1.音频属性相关 音频采样所得的PCM都含有三个要素:声道(channel)、采样率...

  • 10_PCM转WAV

    播放器是无法直接播放PCM的,因为播放器并不知道PCM的采样率、声道数、位深度等参数。当PCM转成某种特定的音频文...

  • Android音频开发:PCM转WAV格式音频

    一、wav 和 pcm 一般通过麦克风采集的录音数据都是PCM格式的,即不包含头部信息,播放器无法知道音频采样率、...

  • 音视频学习(五)

    音频编码 WAV编码是在PCM数据格式的前面加44个字节,分别用来存储PCM的采样率、声道数、数据格式等信息特点:...

  • PCM转WAV

    PCM存在问题 PCM是音频文件元数据,并不包含采样率、声道数、位深度等参数。播放器不知道相关参数无法直接读取 W...

  • 音视频开发02--使用LAME库转换pcm文件到mp3

    android 使用 AudioRecord 对麦克风进行录音得到的是 pcm 格式的原始音频数据,pcm文件是不...

  • Android音视频开发-入门(二)

    目标任务 使用AudioRecord 采集PCM数据 使用AudioTrack API播放PCM音频 将Pcm数据...

网友评论

      本文标题:使用 AudioConverterRef 进行PCM采样率转换

      本文链接:https://www.haomeiwen.com/subject/nhemfdtx.html