解码ima4音频格式 [英] Decoding ima4 audio format
问题描述
为了减少iPhone应用程序的下载大小,我正在压缩一些音频文件。具体来说,我在命令行上使用afconvert将.wav格式改为.caf格式w / ima4压缩。
To reduce the download size of an iPhone application I'm compressing some audio files. Specifically I'm using afconvert on the command line to change .wav format to .caf format w/ ima4 compression.
我读过这个(wooji-juice.com)关于这个确切话题的精彩帖子。我遇到了解码ima4数据包的麻烦。我看了他们的示例代码,我被卡住了。请帮助w /一些伪代码或示例代码,它们可以指导我正确的方向。
I've read this (wooji-juice.com) awesome post about this exact topic. I'm having trouble w/ the "decoding ima4 packets" step. I've looked at their sample code and I'm stuck. Please help w/ some pseudo code or sample code that can guide me in the right direction.
谢谢!
其他信息:
以下是我已经完成的内容以及我遇到问题的地方...
我可以在模拟器和手机上播放.wav文件。
我可以使用命令行上的afconvert将.wav文件压缩为.caf w / ima4压缩。我正在使用带CrashLanding的SoundEngine(我修复了一个内存泄漏)。
我修改了SoundEngine代码以查找mFormatID'ima4'。
Additional info: Here is what I've completed and where I'm having trouble... I can play .wav files in both the simulator and on the phone. I can compress .wav files to .caf w/ ima4 compression using afconvert on the command line. I'm using the SoundEngine that came w/ CrashLanding (I fixed one memory leak). I modified the SoundEngine code to look for the mFormatID 'ima4'.
我不明白上面链接的博客帖子开头w /计算大小解压缩数据。为什么我需要这样做?此外,数据包一词是指什么?我对任何类型的音频编程都很陌生。
I don't understand the blog post linked above starting w/ "Calculating the size of the unpacked data". Why do I need to do this? Also, what does the term "packet" refer to? I'm very new to any sort of audio programming.
推荐答案
从 Wooji-Juice ,多媒体Wiki 和 Apple ,这是我的建议(可能需要一些实验):
After gathering all the data from Wooji-Juice, Multimedia Wiki and Apple, here is my proposal (may need some experiment):
文件结构
- Apple IMA4文件由34个字节的数据包组成。这是用于构建文件的数据包单元。
- 每个34字节的数据包有两部分:
- 第一部分2个字节包含前导码:初始预测器和步骤索引
- 剩下的32个字节包含声音半字节(4位的半字节用于检索16位样本)
解码
每个34字节的数据包将导致64位16位样本的解压缩。因此,未压缩数据的大小为每个数据包128个字节。
Each packet of 34 bytes will lead to the decompression of 64 samples of 16 bits. So the size of the uncompressed data is 128 bytes per packet.
解码伪代码如下所示:
int[] ima_index_table = ... // Index table from [Multimedia Wiki][2] int[] step_table = ... // Step table from [Multimedia Wiki][2] byte[] packet = ... // A packet of 34 bytes compressed short[] output = ... // The output buffer of 128 bytes int preamble = (packet[0] << 8) | packet[1]; int predictor = preamble && 0xFF80; // See [Multimedia Wiki][2] int step_index = preamble && 0x007F; // See [Multimedia Wiki][2] int i; int j = 0; for(i = 2; i < 34; i++) { byte data = packet[i]; int lower_nibble = data && 0x0F; int upper_nibble = (data && 0xF0) >> 4; // Decode the lower nibble step_index += ima_index_table[lower_nibble]; diff = ((signed)nibble + 0.5f) * step / 4; predictor += diff; step = ima_step_table[step index]; // Clamp the predictor value to stay in range if (predictor > 65535) output[j++] = 65535; else if (predictor < -65536) output[j++] = -65536; else output[j++] = (short) predictor; // Decode the uppper nibble step_index += ima_index_table[upper_nibble]; diff = ((signed)nibble + 0.5f) * step / 4; predictor += diff; step = ima_step_table[step index]; // Clamp the predictor value to stay in range if (predictor > 65535) output[j++] = 65535; else if (predictor < -65536) output[j++] = -65536; else output[j++] = (short) predictor; }
这篇关于解码ima4音频格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!