Monday, 19 September 2011

Use 'ima4' compression to reduce CAF file size during conversion

While playing with sound, the most frequently encountered problem will be file format conversion.

Apple has this Technical Q&A QA1534 which talks about how to create .CAF files from .AIF files using "afconvert" utility.

If you searched on the net you can also find lots of info about converting MP3/WAV to CAF files.

However I couldn't find much information about how to reduce the .CAF file size. I think you play around either the bit rate or the sample rate, however that would more or less impact the music/sound quality.

Later I found this discussion about OpenAL file formats in Apple Support Communities which talks about "IMA4" compression - which is something I never heard before.

I did a few simple experiments as below, converting same WAV file to CAF using different sample rate and "LEI16", "IMA4" data format:

afconvert -f caff -d LEI16@44100 -c 1 correct01.wav correct01_44100.caf
afconvert -f caff -d LEI16@128000 -c 1 correct01.wav correct01_128000.caf
afconvert -f caff -d 'ima4' -c 1 correct01.wav correct01_ima4.caf
afconvert -f caff -d 'ima4@44100' -c 1 correct01.wav correct01_ima4_44100.caf
afconvert -f caff -d 'ima4@128000' -c 1 correct01.wav correct01_ima4_128000.caf


The result file size is as below:


With same 44100 sample rate, using IMA4 compression reduced the file size from 313 KB to 86 KB, about 73% smaller! With the 12800 sample rate, the result is also similar. Don't know what's the default sample rate when using "-d 'ima4'",  without the parameter, the result file of 19KB is the smallest.

I guess for small, short sound effect files, should be ok with lower sample rate as it dramatically reduced the file size, but for looping or more important music, you might want to play around with the numbers a bit to get the best balance/result.

Hope you find this information helpful. And if you are aware of any better/other compression or whatever method to further reduce file size (hopefully without loosing too much sound quality?) please let me know!!

[Update 09/Feb/2014]
Thanks for the comment by a reader, I have corrected the mistake in the article.

You can get further info from this link from Ray Wenderlich as below:

Bit Rates

There’s an important piece of terminology related to audio encoding that we need to mention next: bit rates.
The bit rate is the number of bytes per second that an audio file takes up. Some encodings such as AAC or MP3 let you specify the number of bytes to compress the audio file to. When you lower the bytes per second, you lose quality as well.
You should choose a bit rate based on your particular sound file – try it out at different bit rates and see where the best match between file size and quality is. If your file is mostly speech, you can probably get away with a lower bit rate.
Here’s a table that gives an overview of the most common bit rates:
  • 32kbit/s: AM Radio quality
  • 48kbit/s: Common rate for long speech podcasts
  • 64kbit/s: Common rate for normal-length speech podcasts
  • 96kbit/s: FM Radio quality
  • 128kbit/s: Most common bit rate for MP3 music
  • 160kbit/s: Musicians or sensitive listeners prefer this from 128kbit/s
  • 192kbit/s: Digital radio broadcasting quality
  • 320kbit/s: Virtually indistinguishable from CDs
  • 500kbit/s-1,411kbit/s: Lossless audio encoding such as linear PCM

Sample Rates

There’s one final piece of terminology to cover before we move on: sample rates.
When converting an analog signal to digital format, the sample rate is how often the sound wave is sampled to make a digital signal.
Almost always, 44,100Hz is used because that is the same rate for CD audio.

5 comments:

  1. when you write 'ima4@128000' you are actually changing the sample rate, not the bit rate. To change the bit rate you would have to use -b

    ReplyDelete
    Replies
    1. Thanks for the comment, yes you correct.

      Delete
  2. Though there are caf converter that can convert CAF to MP3, few of them can convert MP3 to CAF.

    Luckily i find a app and a step by step guide on how to convert MP3 to CAF or convert CAF to MP3 at http://www.ecouponcode.org/how-to/mp3-to-caf-converter.html

    ReplyDelete
  3. Though there are caf converter that can convert CAF to MP3, few of them can convert MP3 to CAF.

    Luckily i find a app and a step by step guide on how to convert MP3 to CAF or convert CAF to MP3 at Allavsoft.

    ReplyDelete
  4. You can also use -d 'aac@44100', that will keep your file size really low.
    CAF is just a container, it can be lossless but doesn't have to be.

    ReplyDelete