iOS Core Audio:在kAudioFormatFlagsCanonical和kAudioFormatFlagsAudioUnitCanonical之间转换 [英] iOS Core Audio : Converting between kAudioFormatFlagsCanonical and kAudioFormatFlagsAudioUnitCanonical

查看:244
本文介绍了iOS Core Audio:在kAudioFormatFlagsCanonical和kAudioFormatFlagsAudioUnitCanonical之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在以下格式之间进行转换:

I need to convert between this format :

        format.mSampleRate  = 44100.0; 
        format.mFormatID = kAudioFormatLinearPCM;
        format.mFormatFlags = kAudioFormatFlagsCanonical | kLinearPCMFormatFlagIsNonInterleaved;
        format.mBytesPerPacket = sizeof(AudioUnitSampleType);
        format.mFramesPerPacket = 1;
        format.mBytesPerFrame = sizeof(AudioUnitSampleType);
        format.mChannelsPerFrame = 2 ;
        format.mBitsPerChannel = sizeof(AudioUnitSampleType)*8;

此格式

format.mSampleRate  = 44100.0; 
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical;
format.mBytesPerPacket = sizeof(AudioUnitSampleType);
format.mFramesPerPacket = 1;
format.mBytesPerFrame = sizeof(AudioUnitSampleType);
format.mChannelsPerFrame = 2; 
format.mBitsPerChannel = sizeof(AudioUnitSampleType)*8;

在音频渲染回调的范围内,其中有以下代码和缓冲区[]在第二种格式和array []需要第一种格式。

within the confines of a audio render callback where there is the following code and buffer[] is in the 2nd format and array[] requires the 1st format.

for (k = 0; k < channels; k++){
    buffer = (AudioUnitSampleType *) ioData->mBuffers[k].mData;
    for(j=0; j < samples; j++){
        array[j] = buffer[j];
    }
}

我知道你可以使用Apple转换器单元,但是在我的情况下我无法使用Apple Converter音频设备(这是有原因的)。

I know you can use the Apple converter unit, but I cannot use the Apple Converter audio unit in my situation (there's a reason).

基本上两种格式之间的唯一区别是format.mFormatFlags
(kAudioUnitSampleFractionBits<< kLinearPCMFormatFlagsSampleFractionShift)的以下标志。

Basically the only difference between the 2 formats the following flag for format.mFormatFlags (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift).

如何将buffer [](包含第二种格式的数据)转换为array [](包含第一种格式的数据),反之亦然?

How can I convert buffer[] (containing data in the 2nd format) to array[] (containing data in the 1st format) and vice-versa?

谢谢。

推荐答案

好吧,如果您参考 kAudioFormatFlagsAudioUnitCanonical 上的文档,那么你请参阅:

Well, if you refer to the docs on kAudioFormatFlagsAudioUnitCanonical, you see:

kAudioFormatFlagsAudioUnitCanonical The flags for the canonical audio unit sample 
type. This matches AudioUnitSampleType.

The canonical audio sample type for audio units and other audio processing in 
iPhone OS is noninterleaved linear PCM with 8.24-bit fixed-point samples.

因此, buffer [] 中的样本数组采用8.24位定点格式。这是什么意思?

So, the samples in buffer[] array are in 8.24-bit fixed-point format. What does it mean?

8.24位定点格式用于表示具有固定精度的浮点数 - 一个32位整数,其中前8位代表整个部分,最后一位代表整个部分24位表示小数部分(小数点后的数字)。 (进一步阅读

8.24-bit fixed-point format is used to represent float numbers with fixed precision - a 32-bit integer where the first 8 bits represent the whole part, and the last 24 bits represent the fractional part (the numbers after the decimal). (Further reading)

在iOS音频单元中,存在细微差别 - 此浮点数(通常)的范围为[-1,1)( [-1.000000000000,+ 0.999969482421875]确切地说是)。转换为16位PCM时,只会剪切此范围之外的值。在大多数情况下,您可以验证前8位是0x00还是0xff(在2的补码中为-1)。

In iOS Audio Units, there's a minor difference - this float number (usually) ranges in [-1, 1) ([-1.000000000000, +0.999969482421875] to be exact). Values outside of this range are simply clipped when converting to 16-bit PCM. You can validate that the first 8 bits would be 0x00 or 0xff (-1 in two's compliment) for most part.

要将此表示转换为16位数,使用此:

To convert this representation to a 16-bit number, use this:

SIGN((SInt8)(val >> 24)) * 32768 * (val & 0xFFFFFF)/(float)(1<<24)

即:从8 MSB中提取符号,从24 LSB中提取小数值,除以24位整数(2 ^ 24)的范围,得到0到1之间的浮点数,最后乘以32768得到所需范围的值。

That is: extract the sign from the 8 MSB, extract fractional value from 24 LSB and divide by range of 24-bit integer (2^24) resulting in a float between 0 and 1, and finally multiply this by 32768 to get a value in desired range.

我自己也没试过 - 你可能需要在这里和那里调整一些东西。

I haven't tried this myself though - you might have to adjust a few things here and there.

这篇关于iOS Core Audio:在kAudioFormatFlagsCanonical和kAudioFormatFlagsAudioUnitCanonical之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆