Ambient Music in my app kills performance

Member
Posts: 29
Joined: 2008.10
Post: #1
I have enabled in my little game ambient music(the currently playing music from the iphone library) but i am experiencing some rather hard performance hits. Is optimizing my game the only thing to do?
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #2
AdrianM Wrote:I have enabled in my little game ambient music(the currently playing music from the iphone library) but i am experiencing some rather hard performance hits. Is optimizing my game the only thing to do?

I noticed that in 3.0, using AVAudioPlayer to play an mp3 with the ambient sound profile causes a big performance hit - apparently it causes AVAudioPlayer to do software decoding instead of hardware. This didn't happen in 2.2.

My solution was to check if music was already playing on startup - if it is, I use the ambient sound category and mute my music (no slowdown.)

If no music is playing, I set the ambientSolo sound category - now my AVAudioPlayer uses hardware decoding, causing no slowdown.

The only flaw is that if someone manages to start their audio AFTER my game is started, all of my audio is silenced. Have not licked that problem.
Quote this message in a reply
Member
Posts: 29
Joined: 2008.10
Post: #3
smasher Wrote:I noticed that in 3.0, using AVAudioPlayer to play an mp3 with the ambient sound profile causes a big performance hit - apparently it causes AVAudioPlayer to do software decoding instead of hardware. This didn't happen in 2.2.

My solution was to check if music was already playing on startup - if it is, I use the ambient sound category and mute my music (no slowdown.)

If no music is playing, I set the ambientSolo sound category - now my AVAudioPlayer uses hardware decoding, causing no slowdown.

The only flaw is that if someone manages to start their audio AFTER my game is started, all of my audio is silenced. Have not licked that problem.

Sounds interesting, but how do you check if music is already playing?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #4
AdrianM Wrote:Sounds interesting, but how do you check if music is already playing?

http://www.idevgames.com/forum/showthread.php?t=16048
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #5
I used this, but my performance is still low:

AudioSessionInitialize ( NULL, NULL,NULL,NULL);

AudioSessionSetActive (true);

UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;

AudioSessionSetProperty ( kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory );

is this even relevant for AVAudioPlayer?

thanks,
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #6
Newbrof Wrote:is this even relevant for AVAudioPlayer?

Yes, it most definitely is. One of the main issues with iPhone is which compressed audio stream gets to use the built-in audio decompression hardware. Only one stereo track may be using the hardware at any given time. I've tried testing that and got an extremely slow running game, and sometimes the device would lock up.
Quote this message in a reply
Member
Posts: 27
Joined: 2008.12
Post: #7
Newbrof Wrote:I used this, but my performance is still low:
AudioSessionInitialize ( NULL, NULL,NULL,NULL);
AudioSessionSetActive (true);
etc etc etc

That's the ticket, but you need to do it before you start any audio, of course.

You should comment out your AVAudioPlayer and test for the slowdown - if it still happens with no AVAudioPlayer alloc'd, then your performance problem is elsewhere. If it only happens with the music playing, the session category may be the answer.

-- Available Now: Dead Panic, a casual zombie shooter!
-- Development Blog: How to make a game under $1k
-- Twitter: xsmasher
Quote this message in a reply
Member
Posts: 24
Joined: 2009.08
Post: #8
smasher Wrote:That's the ticket, but you need to do it before you start any audio, of course.

You should comment out your AVAudioPlayer and test for the slowdown - if it still happens with no AVAudioPlayer alloc'd, then your performance problem is elsewhere. If it only happens with the music playing, the session category may be the answer.

yes, with no AVAudioPlayer I get my FPS solid at whatever I have set my NSTimer. With AVAudioplayer I get permanent FPS drop to 40

In a different thread we found out that it is related to MP3 decoding... even with ambient solo category, I occassionaly get permanet 40 FPS (it looks like software decoding is used). Usually I get 40 FPS when I switch one MP3 song to the next MP3 song. Even if I unload/deallocate AVAudioplayer completely.

So, now I use uncompressed WAV files (16 bit Midi-Sound is OK)...

am I the only one who is using OS 3.0 and MP3s for background music? Somebody else should have noticed FPS drops... or maybe everybody else is OK with 40 FPS?

my game looks a bit sluggish when moving with 40 FPS

cheers
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #9
I'm also having a problem with mp3's affecting performance. I'm using the code here http://www.idevgames.com/forum/showthread.php?t=16048 to see if the sound is playing first and if its not then I activate my music (4Mb, about 4mins duration). If music is playing already my music doesnt play and performance is fine, so I know that its checking correctly.

I'm getting a smooth framerate for about 90% of each second, then it slows right down for the last 10%. This continues on each second. I've tried lower the quality, which helped slightly. I also uncompressed it to WAV and had no performance issues, but that makes the track about 39Mb.

Any ideas?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #10
If you're running 3.x, Apple changed the way things work a little bit which turns off hardware decoding when using kAudioSessionCategory_AmbientSound. You'll probably want to use kAudioSessionCategory_SoloAmbientSound (notice the addition of "Solo") to get hardware decoding enabled again. I'll go change my post in that thread for 3.x.

[edit] ... just noticed smasher already mentioned this above ...
Quote this message in a reply
Member
Posts: 94
Joined: 2008.08
Post: #11
Awesome, that worked a treat, thanks!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ambient Sound performance on iOS 4.x hypersonic 3 5,058 Feb 17, 2011 11:03 PM
Last Post: Frank C.
  Xcode debugger kills me! jaguard 5 5,696 Nov 21, 2008 09:30 AM
Last Post: jaguard