Playing audio files

Apprentice
Posts: 11
Joined: 2009.01
Post: #1
Hi! I've just registered to the forums. My name is Ignacio and I'm a college student from Buenos Aires, Argentina. I'm trying to port a game I made (Flip Disc) from the PocketPC plaform to the iPhone.

I got a second hand MacBook last Thursday (my first Mac computer) so I'm a total newbie to this environment. I've managed to compile the game and, although the graphics are working fine in the simulator, I'm having some problems in the audio department.

I've been googling extensively on this and found out that there are many ways to play a sound (Audio Queues, OpenAL, AudioToolBox) but I haven't found a simple way to playback audio files by using a C interface. Can you tell me if I'm missing somethings obvious? I mean, there should be an easy way to reproduce a sound file in an iPhone...

Thanks in advance for all your help!

--Nacho
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #2
OK, so I've managed to play an mp3 file by using AVAudioPlayer. The sound plays great but I'm having small pauses or glitches during the loop. Googling around I found this small article but, unfortunately, using uncompressed sound files as the author suggests would take my game's size to around 100 MB, which I think it would be unacceptable. Anyone ever experienced a similar problem? What do you suggest?

Thanks in advance for your answers,

--Nacho
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #3
Yes, this is a well-known problem. I have yet to hear about a good solution or workaround for that audio gap at the end of a looping compressed audio file. What I and others do to work around it right now is just fade out the music file at the end of the track. If you find a seamless solution, please let us know!
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #4
Hi AnotherJake, thanks for your reply! I've read the whole 7 pages of the "Audio Queues" thread and I saw that in one of your last posts you recommend to switch to AVAudioPlayer. Are you aware of any other solution (maybe one that doesn't make use of AVAudioPlayer but a lower level framework instead) that yields better results on looped mp3s?

Thanks again for your reply,

--Nacho
Polar Motion
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #5
Nacho Wrote:Hi AnotherJake, thanks for your reply! I've read the whole 7 pages of the "Audio Queues" thread and I saw that in one of your last posts you recommend to switch to AVAudioPlayer. Are you aware of any other solution (maybe one that doesn't make use of AVAudioPlayer but a lower level framework instead) that yields better results on looped mp3s?

No, unfortunately not. If I did, I'd be using it myself. It seems like it should be possible, but I haven't figured out how to do it yet (not enough time to investigate right now). If you find something, again, please let us know!
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #6
Hi AnotherJake,

I'm using the EDGELIB library to make the game which provides support for a great number of mobile devices but, unfortunately, the audio functionality for the iPhone is still in beta. I asked in their forums about this and they told me that they know it's possible to loop mp3's with the AudioToolbox framework by adding the music multiple times to the queue. Since I'm new to MacOS myself, doing something like this would take me several days but have you tried an approach similar to what they suggested?

Thanks again for your reply,

--Nacho
Polar Motion
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #7
Nacho Wrote:I asked in their forums about this and they told me that they know it's possible to loop mp3's with the AudioToolbox framework by adding the music multiple times to the queue.]

Hmm... I wonder what exactly they mean by that. The problem is that *apparently* the decoding process leaves space left over in the final packets. Can you give me a link to their forum reply?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #8
AnotherJake Wrote:Hmm... I wonder what exactly they mean by that. The problem is that *apparently* the decoding process leaves space left over in the final packets. Can you give me a link to their forum reply?

Absolutely, check the thread here. And thanks again for all your prompt replies!

--Nacho
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #9
Thanks. Unfortunately he doesn't appear to address the loop gap issue. He just says he knows it can be looped by adding music to the queue multiple times. I have the looping down, I just can't figure out how to get rid of the audio gap at the end. Well, keep your eyes peeled for any more information.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #10
From what I've read in other forums MP3 files cannot be looped. MP3s are made from a group of samples that are a multiple of a fixed size so, when there are not enough samples, the file is filled with silence until the multiple is reached.

I "solved" it by saving all my audio files in aiff and later using the afconvert tool that is included with the iPhone SDK to convert them to IMA4 caf. The compression is not as great as with MP3, but it certainly takes less space than aiff or wav and it loops fine.

I hope this post has been useful.

--Nacho
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #11
Hey, thanks Nacho. I haven't heard of that approach before. Just how much bigger, on average, do you see those files being? For instance, how big would a 2 MB mp3 usually wind up being in IMA4 caf?
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2009.01
Post: #12
Hi! Sorry for my late reply. I don't remember the exact numbers since I ended up using aiff files directly, but I recall that a song of about 30 seconds (stereo, 44 KHz) took a little more than 1 MB.

Mind you, I haven't tested it on a real device yet, but in the emulator it seems to work fine.

--Nacho
Quote this message in a reply
Member
Posts: 61
Joined: 2009.01
Post: #13
In general it's best to use CAFF (uncompressed) and AIFF IMA4 (compressed) for all your sounds, and no other formats. This is what Apple says, and in my experience this has been true.

IMA4 has good enough compression to work just fine, as far as I can tell, and the nice thing about it is that any AIFF loader (including in other OSes and programming languages) can load it just find, because it's really just an AIFF with a specific compression applied.

Here are a couple comparisons:
3:40 - MP3: 4,308,690 bytes IMA4: 10,447,944 bytes
2:00 - MP3: 2,046,421 bytes IMA4: 5,610,560 bytes

As you can see, IMA4 is about twice as big as MP3, but that's pretty acceptable when you're talking much smoother playback and you'll probably have very few large audio files. Not to forget the fact that a CAFF or an uncompressed AIF will be 4 times the size of an IMA4 and 8 times the size of an MP3.

To convert to CAFF files from the command line:
Code:
afconvert -f caff -d LEI16 <INPUT> <OUTPUT>
To convert to IMA4 files from the command line:
Code:
afconvert -f AIFC -d ima4 <INPUT> <OUTPUT>

afconvert can read in almost anything, including mp3 and wav. Note that in the <OUTPUT> parameter you should put in the appropriate file extension (.caf for CAFF and .aif for IMA4). Also the capitalization of the -f and -d parameters is important/.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #14
Thanks for all the extra info, demonpants! Smile

Looks like a reasonably good workaround with the trade-off being approximately double-size files. Not too bad if you really need seamless looping.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Slowdown while playing many OpenAL sounds and accessing AVAudioPlayer.playing Rasterman 6 5,372 Aug 31, 2010 09:46 PM
Last Post: headkaze
  Playing .mod files on iPhone? Jamie W 0 3,006 Jul 23, 2009 05:06 AM
Last Post: Jamie W