如何使用原生定点类型(8.24)进行iOS音频处理
所以我想将 -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)..
:可以用,信号听起来不错,但是很安静。那么我的缩放方法是错误的吗? 缩放较大的数字会扰乱信号。听起来不像削波,并且输出音量也不会变高。我不打算深入研究定点数学,我所需要的只是将其转换为正确格式的一行代码。
谢谢你!
编辑:我一直在使用不同的流格式,在此之前我无法弄清楚如何正确使用立体声信号。通过这种不同的设置,我在输出音量方面没有遇到任何问题,所以我认为增益问题一定与缩放有关......
so i want to properly scale floats in the range of -1 to +1 into the format expected by an AUGraph with a stream format that is set up like this:
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
this question helped me setup the graph, but when i cast a float like this:
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)..
it works, the signal sounds good, but is very quiet. So i'm doing the scaling wrong?
Scaling by a bigger number messes up the signal. Doesn't sound like clipping, and the output volume does not get higher. I do not intend to deeply study fixed point math, all i need is a one-liner which casts into the proper format.
thank you!
edit: i had been using a different stream format before which i could not figure out how to use properly with a stereo signal. With this different setup, i have had no trouble with the output volume though, so i figure the gain problem must have to do with the scaling...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
阅读这篇帖子,这是对 iOS 8.24 格式的很好的解释。
结论如下:
基本上它告诉您前(左)8 位仅专用于 (+/-) 符号,其余 24 位是声音。
因此,如果您想将其转换为 Sint16,请将位向右移动 9 位并进行强制转换。这样,(+/-) 符号将保留在第一位,并且声音数据会降低到较低的精度。
如果您希望它作为 (+/-)1 到 0 范围内的浮点数,请将其除以最大可能值 32768。
代码如下:
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:
我讨厌 8.24,因为我认为没有方便的函数可以用它来做数学。
反建议:在图表的前面放置一个转换器单元(AUConverter),并将输入ASBD设置为对您来说更方便的东西,例如16位整数或其他(我总是在iOS上使用整数......浮点数可能工作进入转换器,但我不会指望它)。不要设置转换器的输出ASBD;它只会默认为规范的音频单元(8.24)。事实上,看看您是否可以在不使用 AUConverter 的情况下在您的第一台设备上设置方便的 ASBD。
I hate 8.24, because I don't think there are convenience functions to do math with it.
Counter-proposal: put a converter unit (AUConverter) at the front of your graph, and set the input ASBD to be something more convenient for you, like 16-bit ints or whatever (I always use ints on iOS... floats may work going into a converter, but I wouldn't count on it). Don't set the converter's output ASBD; it'll just default to audio unit canonical (8.24). In fact, see if you can set a convenient ASBD on your first unit without using an AUConverter.