What audio formats do you use?

Forums Programming sound engine: CocosDenshion et al What audio formats do you use?

This topic contains 25 replies, has 12 voices, and was last updated by  HiRez 1 year, 3 months ago.

Viewing 25 posts - 1 through 25 (of 26 total)
Author Posts
Author Posts
June 23, 2012 at 4:48 pm #242429

Omegas7
Participant
@omegas7

What are the best audio formats you use for soundtracks and for sound effects? MP3? WAV?

June 24, 2012 at 5:11 am #380842

davidlxk
Participant
@davidlxk

MP3 for soundtracks and CAF for sound effects

June 24, 2012 at 1:09 pm #380843

Dani
Participant
@davilca

I work with WAV on audio editors (I save the WAV sound effect and music files in 22050Hz, 16 bits, mono), then convert them to CAF (for sound effects) and AIFC (for music), to obtain smaller file sizes. As far as I know, MP3 can’t be looped because there is a little pause between loops, so I avoid MP3 if I need to loop the BG Music.

Some command lines to convert WAV files to CAF and AIFC:

afconvert -f caff -d ima4 sfx.wav
afconvert -f AIFC -d ima4 bgm.wav

Hope this helps.

June 24, 2012 at 4:49 pm #380844

itlgames
Moderator
@itlgames

+1 for caff and aifc too. And “-c 1″ on the afconvert will create mono files, in case you want to reduce even more the size and you don’t need stereo (unless you use headphones there will be not massive difference)

June 25, 2012 at 2:37 am #380845

KeitGames
Participant
@keitzer

mp3 on my golf game due to binary size (the mp3′s are mega small)

however i try to use as high quality as possible… because if you’ve got the space (by that mean, under 50 MB) why not?

June 25, 2012 at 4:01 am #380846

Steffen Altwiese
Moderator
@steve-oldmeadow

Apple’s recommendation is below. CAF files are Core Audio’s wrapper format, they can be wrapped around lots of different types of encoding such as little endian linear PCM, IMA4, mp3 and aac. For example, Dani is actually using IMA4 which is a lossy compression format that is about 1/4 the size of the equivalent little endian encoding.

In the past lots of people have had issues looping mp3 files that were fixed when they switched to AAC.

If you are playing sounds using OpenAL (e.g. you use CocosDenshion playEffect or ObjectAL) then all your sounds are converted to little endian linear PCM format in memory when they are loaded so the format you use is only really relevant to how much space they take in the download.

For uncompressed (highest quality) audio, use 16-bit, little endian, linear PCM audio data packaged in a CAF file. You can convert an audio file to this format in Mac OS X using the afconvert command-line tool, as shown here:

/usr/bin/afconvert -f caff -d LEI16 {INPUT} {OUTPUT}

The afconvert tool lets you convert to a wide range of audio data formats and file types. See the afconvert man page, and enter afconvert -h at a shell prompt, for more information.

For compressed audio when playing one sound at a time, and when you don’t need to play audio simultaneously with the iPod application, use the AAC format packaged in a CAF or m4a file.

For less memory usage when you need to play multiple sounds simultaneously, use IMA4 (IMA/ADPCM) compression. This reduces file size but entails minimal CPU impact during decompression. As with linear PCM data, package IMA4 data in a CAF file.

June 25, 2012 at 11:57 am #380847

Dani
Participant
@davilca

I found a useful post here: http://gamua.com/blog/2010/06/sound-on-ios-best-practices/

June 25, 2012 at 12:13 pm #380848

Steffen Altwiese
Moderator
@steve-oldmeadow

@Dani – do you use the sound engine from Sparrow? If not then that article is not really good advice/best practices.

June 25, 2012 at 12:45 pm #380849

Dani
Participant
@davilca

@Steve Olmeadow: Then I’m very confused now. What format do you recommend me to use for CocosDenshion and this scenario (an aviation shooter)?:

- Lots of different sounds playing almost at the same time.

- Background music loop.

I’m looking for this features (in that order):

1. Little CPU and performance impact.

2. Little file size.

As I said, I use this for sound effects:

afconvert -f caff -d ima4 sfx.wav

And this for music:

afconvert -f AIFC -d ima4 bgm.wav

Thanks in advance!

June 25, 2012 at 12:51 pm #380850

Yann
Participant
@playfripp

I did A LOT of test on the audio formats for Wawa Land, and this are the 2 formats that I use to get the best compromise between size and quality:

BGM – AIFC – IMA4 – MONO – 32k

SFX – AIFC – IMA4 – MONO – 24k

I also have one or two MP3s for good quality long SFX.

June 25, 2012 at 12:58 pm #380851

Steffen Altwiese
Moderator
@steve-oldmeadow

@Dani – for background music use AAC packaged in a CAF or M4A file. e.g

afconvert -d aac -f 'caff' -b 131072 background-music-lei.caf test_128.caf

For sound effects use 16 bit little endian linear PCM in a CAF file. If you have a looping sound and there is a glitch then switch to using .WAV as CocosDenshion will use a different loading method that may improve looping at the cost of slightly longer load times e.g

afconvert -f caff -d LEI16 sound.wav

Note that is Apple’s recommendation. However, what you are currently doing isn’t too bad. If you switch to following Apple’s recommendation you will find your background music should be better quality and a smaller file. Your effects will be larger files but better quality, memory use at run time will be exactly the same.

Here is a good article: http://www.raywenderlich.com/233/audio-101-for-iphone-developers-converting-and-recording

June 25, 2012 at 3:23 pm #380852

Dani
Participant
@davilca

Thanks @Steve Olmeadow, I have tried AAC-CAF for background music files and they loop OK, so I think this is the best solution (smaller file size, better quality, loopable). For sound effects I will use LEI16-CAF and mono, but I don’t know if I should use them in 44100Hz or 22050Hz. I will make some other tests.

Other questions: I’m not sure about how CocosDenshion manages sound effects. Does it make any difference playing sound effects in LEI16-CAF or IMA4-CAF from a performance point of view? I suppose IMA4 sounds need uncompression, but are they uncompressed each time I play it, or are they uncompressed once and stored in memory? If I preload all sound effects, will they remain in memory till I quit the app? Will they need any uncompression later?

What I do in my project is preloading all sound effects once at startup.

Thanks!

June 25, 2012 at 9:18 pm #380853

MisterX
Participant
@misterx

I believe that preloaded sound effects will remain in memory until they are removed or the app is shut down. I have also preloaded all sound effects at startup like you are doing.

June 26, 2012 at 3:43 am #380854

Steffen Altwiese
Moderator
@steve-oldmeadow

@Dani – 44100 or 22050 really comes down to the sounds and what you think sounds acceptable. Are you familiar with the Nyquist theorem? Basically it states that you require a sample rate twice the maximum frequency to accurately represent a sound. So, for a bassy sound with no frequencies above 11025 then 22050Hz will be fine. Of course 22050 uses half the memory of 44100 so if you are pushed for memory that may sway you. Apple also used to recommend having all the sounds at the same frequency for playback via OpenAL but I don’t think that is such an issue on current devices.

As for the internals of CocosDenshion, you are correct all sounds are uncompressed when they are loaded. They are stored in memory in little endian linear PCM format which is like the native internal audio format (often called canonical format). The memory use is slightly smaller than the file size of a LEI16 CAF file or a .WAV file as the files have header information that is discarded.

If you preload sounds they will remain in memory until they are unloaded or the app is terminated. The decompression only occurs when the sound is loaded.

Personally I like having sounds in LEI16 as it is very easy to see what your audio memory use will be by simply adding up the file sizes. Also, don’t forget that the audio files will be compressed in the IPA file so when comparing file size savings you should look at the change to the final IPA size rather than comparing the raw file sizes.

June 26, 2012 at 11:46 am #380855

Dani
Participant
@davilca

Ok, thanks @Steve Olmeadow, now I undertand this quite better.

One more question, when I setup the audio engine, I do this:

[CDSoundEngine setMixerSampleRate:CD_SAMPLE_RATE_MID];

That it’s supposed to setup the mixer to 22050Hz. Can I still play some sounds with different frequencies? What happens if I play a 44100Hz sound?

Thanks is advance.

June 26, 2012 at 11:55 am #380856

Steffen Altwiese
Moderator
@steve-oldmeadow

@Dani – yeah, that just relates to the recommendation Apple used to make in their best practices. They recommended setting the mixer rate and all samples to the same rate in a couple of their WWDC talks (probably 2009/2010). I don’t think it is relevant any more unless you are supporting 1st gen devices.

When you set the mixer rate as you have done then you can still play sounds with different samples rates. However, if the mixer rate is set to 22050 then 44100 sounds will be mixed down to that rate so a good rule of thumb would be to set the mixer rate to the same rate as your highest sample rate.

June 26, 2012 at 12:18 pm #380857

Dani
Participant
@davilca

@Steve Olmeadow: Ah, ok, thank you very much, man. I see all this audio issue clearer now, and I was making some mistakes, so I have to update my projects!

August 4, 2012 at 4:57 am #380858

jcljk
@jcljk

hi @Steve Oldmeadow,

thank you for the information. this is very helpful.

just a quick question, I have a background music that I’d like to convert to CAF using your below command:

- afconvert -d aac -f ‘caff’ -b 131072 background-music-lei.caf test_128.caf

However, my background music is in mp3 format. Do I need to use wav to convert to caf and then use the above command or i can use the below command to convert from mp3 direct to caf ?

- afconvert -d aac -f ‘caff’ -b 131072 game_theme.mp3 game_theme.caf

Thank you in advance for your help

August 4, 2012 at 5:11 am #380859

Steffen Altwiese
Moderator
@steve-oldmeadow

@jcljk – afconvert can convert any format it supports to any other format so there is no need to convert your mp3 to wav. Your command looks okay to me but I’m not an expert on afconvert syntax. You may need to play around with it a bit. afconvert has help. Just use afconvert –help.

August 4, 2012 at 5:18 am #380860

jcljk
@jcljk

Hi @Steve Oldmeadow,

Thank you for your reply.

So, you are saying that there is no difference between converting from wav to caf or mp3 to caf (in quality or performance)

To my understanding, wav is raw format whereas mp3 is encoded.

Wont we lose quality if we convert from mp3 instead of wav?

August 4, 2012 at 5:35 am #380861

Steffen Altwiese
Moderator
@steve-oldmeadow

Yes, you will get better quality converting wav to a compressed format than converting a compressed format to another compressed format. All the compressed formats are lossy which means quality is lost. I thought you only had the file as an mp3.

Having said that, I doubt the average person would hear any difference between a high quality mp3 and a wav. Especially through the speaker or headphones of a mobile device.

August 4, 2012 at 6:05 am #380862

jcljk
@jcljk

Thank you Steve. You are very helpful. I will try to get the wav file and convert it to caf.

January 16, 2013 at 3:15 pm #380863

baris
Participant
@baris

@Steve

‘As for the internals of CocosDenshion, you are correct all sounds are uncompressed when they are loaded. They are stored in memory in little endian linear PCM format which is like the native internal audio format (often called canonical format).’.

Do you think it might be worthwhile to look into keeping IMA4 files in memory and playing directly from there? On Ray Wenderlich’s page that you suggested, it says IMA4 files can be played “quite quickly and simultaneously” (which I assume means playing them without first converting into linear PCM in memory while loading).

We have a game with run-time memory limitations even after optimising the graphics quite a bit, so every little bit helps.

(Sorry to bring back this thread, but it was one of the best search results for iOS audio formats, so thanks!)

January 16, 2013 at 7:50 pm #380864

THX-101
Participant
@thx-101

LPCM 16bit 22kHz mono for sound fx, .caf.

IMA4:1 16bit 22kHz mono for music, .caf.

January 17, 2013 at 12:21 am #380865

Steffen Altwiese
Moderator
@steve-oldmeadow

@baris – IMA4 can’t be played back directly using OpenAL which is used for effects in CocosDenshion. You would have to use AVAudioPlayer. You could write your own sound engine based on AVAudioPlayer pretty easily but the performance probably won’t be as good as using OpenAL.

Viewing 25 posts - 1 through 25 (of 26 total)

You must be logged in to reply to this topic.