4

所以我想将 -1 到 +1 范围内的浮点数正确缩放为 AUGraph 所期望的格式,其流格式设置如下:

size_t bytesPerSample = sizeof (AudioUnitSampleType); // is 4 bytes

stereoStreamFormat.mFormatID          = kAudioFormatLinearPCM;
stereoStreamFormat.mFormatFlags       = kAudioFormatFlagsAudioUnitCanonical;
stereoStreamFormat.mBytesPerPacket    = bytesPerSample;
stereoStreamFormat.mFramesPerPacket   = 1;
stereoStreamFormat.mBytesPerFrame     = bytesPerSample;
stereoStreamFormat.mChannelsPerFrame  = 2;                    
stereoStreamFormat.mBitsPerChannel    = 8 * bytesPerSample;
stereoStreamFormat.mSampleRate        = graphSampleRate; // 44.1k

这个问题帮助我设置了图表,但是当我像这样投射浮点数时:

sampleValueLeft = (Fixed) (floatVal * 32767.0f); 
// there doesn't seem to be any difference whether i cast into 
// SInt16 or SInt32 (which the Fixed type is defined to be)..

它工作,信号听起来不错,但很安静。所以我做错了缩放?以更大的数字缩放会扰乱信号。听起来不像削波,输出音量也没有变高。我不打算深入研究定点数学,我所需要的只是一个可以转换成正确格式的单行。

谢谢你!

编辑:我一直在使用不同的流格式,在此之前我无法弄清楚如何正确使用立体声信号。使用这种不同的设置,我对输出音量没有任何问题,所以我认为增益问题一定与缩放有关......

4

2 回答 2

5

Read this post, it is a really good explanation on the iOS 8.24 format.

Here is its conclusion:
Basically it tells you that the first (left) 8 bits are dedicated only for the (+/-) sign, the rest 24 bits are the sound.
So if you want to convert it to a Sint16 shift the bits 9 places to the right and cast. This way the (+/-) sign is preserved in the as the first bit and the sound data is reduced to a lower precision.
If you want it as a float in the range of (+/-)1 to 0, divide it by the maximum posible value which is 32768.

Here is the code:

    SInt16 sampleInt16 = (SInt16)(samples[i] >> 9);
    float sampleFloat = sampleInt16 / 32768.0;
于 2013-05-21T07:36:28.863 回答
3

我讨厌 8.24,因为我认为没有方便的函数可以用它来做数学运算。

反建议:在图表的前面放置一个转换器单元(AUConverter),并将输入 ASBD 设置为对您更方便的东西,例如 16 位整数或其他任何东西(我总是在 iOS 上使用整数......浮点数可能工作进入转换器,但我不会指望它)。不要设置转换器的输出ASBD;它只会默认为音频单元规范(8.24)。事实上,看看您是否可以在不使用 AUConverter 的情况下在您的第一台设备上设置一个方便的 ASBD。

于 2011-06-04T15:18:47.857 回答