IOS音频转换器 AudioConverterRef

1.创建
extern OSStatus
AudioConverterNewSpecific(  const AudioStreamBasicDescription * inSourceFormat,
                            const AudioStreamBasicDescription * inDestinationFormat,
                            UInt32                              inNumberClassDescriptions,
                            const AudioClassDescription *       inClassDescriptions,
                            AudioConverterRef __nullable * __nonnull outAudioConverter)

   inSourceFormat  和 inDestinationFormat 是传入的源音频格式和目标音频格式
   inNumberClassDescriptions 和 inClassDescriptions是传入的音频编码器描述类和它的个数
   outAudioConverter是传出的AudioConverter引用
      
例子:
AudioStreamBasicDescription inf = {0};
inf.mSampleRate = 44100;
inf.mChannelsPerFrame = 1;  //这里只能是1,否则报错
inf.mBitsPerChannel = 16;
inf.mFormatFlags =  kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
inf.mFormatID = kAudioFormatLinearPCM;
inf.mFramesPerPacket = 1;
inf.mBytesPerFrame = inf.mBitsPerChannel * inf.mChannelsPerFrame / 8;
inf.mBytesPerPacket = inf.mFramesPerPacket*inf.mBytesPerFrame;


AudioStreamBasicDescription outf = { 0 };
outf.mFormatID = kAudioFormatMPEG4AAC;
outf.mFormatFlags = 0;
outf.mFramesPerPacket = 1024;
outf.mSampleRate = 44100;
outf.mChannelsPerFrame = 2;
<pre name="code" class="html">const OSType subtype = kAudioFormatMPEG4AAC;    // kAudioEncoderComponentType的时候使用目标类型
                                                // kAudioDecoderComponentType的时候使用源类型
 
 
AudioClassDescription requestedCodecs[2] = {
    {
        kAudioEncoderComponentType,
        subtype,
        kAppleSoftwareAudioCodecManufacturer
    },
    {
        kAudioEncoderComponentType,
        subtype,
        kAppleHardwareAudioCodecManufacturer
    }
};
AudioConverterRef audioConverter;
OSStatus result = AudioConverterNewSpecific(inputFormat, outputFormat,2, requestedCodecs, &audioConverter);


 
 

2.释放

extern OSStatus
AudioConverterDispose(  AudioConverterRef   inAudioConverter)
inAudioConverter是AudioConverter的引用
例子:
if(audioConverter != NULL){
<span style="white-space:pre">	</span>AudioConverterDispose(audioConverter);
}


3.编码

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

inAudioConverter是传入的AudioConverter的引用
inInputDataProc是用户自己实现的获取编码数据的Callback函数(
inInputDataProcUserData是获取编码数据的Callback函数中使用的用户数据(一般传输编码session对象)
ioOutputDataPacketSize输出数据长度(通过获取AudioConverter的kAudioConverterPropertyMaximumOutputPacketSize属性获取或自定义足够的长度)
outOutputData输出数据
outPacketDescription输出数据描述
例子:
static OSStatus FillAudioDataCallback(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets,AudioBufferList *bufferList, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData)
void *data = malloc(length);
    
UInt32 outputDataPackets = 1;
    
AudioBufferList outputData;
outputData.mNumberBuffers = 1;
outputData.mBuffers[0].mNumberChannels = 2;// 输出类型声道数
outputData.mBuffers[0].mDataByteSize = (UInt32)length;
outputData.mBuffers[0].mData = data;
    
AudioStreamPacketDescription outputPacketDescription;
OSStatus status = AudioConverterFillComplexBuffer(audioConverter, FillAudioDataCallback(__bridge void *)self, &outputDataPackets, &outputData, &outputPacketDescription);






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值