Memory and music

Member
Posts: 110
Joined: 2009.07
Post: #1
I have created my sound library, using openAL for sound effects and AVPlayer to play background music. All things are going swimmingly.

So far, I'm using m4a files, but each song in 'non-crunchy' mode takes up between 4 and 8 meg! I look at games like 'Edge' that have 16+ tracks of long, beautiful music and I wonder how the hell it all fits in memory.

Do I give up, and create music algorithmically? I have some background in this type of thing, so it could be done.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #2
Madrayken Wrote:I have created my sound library, using openAL for sound effects and AVPlayer to play background music. All things are going swimmingly.

So far, I'm using m4a files, but each song in 'non-crunchy' mode takes up between 4 and 8 meg! I look at games like 'Edge' that have 16+ tracks of long, beautiful music and I wonder how the hell it all fits in memory.

Do I give up, and create music algorithmically? I have some background in this type of thing, so it could be done.

Streaming from disk perhaps ?
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #3
I'm relatively new to all this, so apologies if this is a silly question:

Would streaming from disk mean that the files would have to be separate from the .app bundle somehow? I've not yet got around to going through the whole packaging for release, and all my assets are part of my Xcode project at the moment, so I'm not sure how I'd do that.

Alternatively, you mean that streaming via AVPlayer should not load the entire file into memory at any point.

Any pointers much appreciated.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Member
Posts: 166
Joined: 2009.04
Post: #4
Yeah, my point is that there is no need to load the entire file into memory since having 5 MB taken by data which doesn't require random access is pure waste.
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #5
Hmm... sounds like I'm doing it already then. The reason this all came to my attention was that my app was crashing when music files were over 4meg. Perhaps the issue is elsewhere, since I'm using AVPlayer's:

Code:
AVAudioPlayer  *musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];

...to play my music.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Right, since that method name has "ContentsOf" in it, presumably that's to indicate that it slurps the entire file into memory. I don't see anything obvious in AV Foundation that would allow you to play streaming audio, so you may need to drop down to Core Audio or use OpenAL.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #7
AVAudioPlayer does stream from disk. Try using a different compressed format like mp3 instead, and see if that helps.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
I've never run into a problem that made me need to look closer, but my understanding was that AVAudioPlayer loaded the entire compressed audio file into memory and streamed the decompressed audio data out of that.

128kbps AAC files are pretty high quality and not that large. If he's talking about 8MB of memory usage, I assume that he means of uncompressed audio data.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #9
AVAudioPlayer does not load the entire files into memory that I've ever seen (unless it does it in some hidden system area that I can't measure). I'm guessing it just uses Audio Queues behind the scenes. Assuming that it does, then no, it definitely would not load the whole file into memory. If it's crashing on large files, then something else is up, which is why I was suggesting to try a different compressed format.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #10
AVAudioPlayer most assuredly does not load the entire file into memory.

Using Instruments and a 6.5MB mp3 file, the following happens on the device:

Without creating an instance of AVAudioPlayer, the application finishes launching and stalls at 738656 bytes of allocated data.

Running it again, this time creating the instance of AVAudioPlayer with the 6.5 MB file, the application finishes launching and stalls at 797424 bytes of allocated data. (delta < 0.06 MB)

Running it again, this time playing the AVAudioPlayer after its been loaded, the number of overall allocated bytes continually rises during playback.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #11
Those measurements are consistent with everything I've seen too.

longjumper Wrote:Running it again, this time playing the AVAudioPlayer after its been loaded, the number of overall allocated bytes continually rises during playback.

[slightly Off-topic]: If it's a small, but continual increase over time, then it's likely a known memory leak associated with Audio Queues. It's definitely not enough to cause much concern though. I've traced a leak in AVAudioPlayer a long time ago and saw that it was from the same system call as the one in Audio Queues (which I reported to Apple and they improved but did not resolve, last I checked), so I think that was one of the things that suggested to me that they are just using Audio Queues for AVAudioPlayer.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #12
No, the overall bytes are how many bytes that have been allocated during the course of runtime. The net bytes are the ones that are currently allocated.

The net bytes jump to the size of a few audio queue buffers and hover around that area while the audio plays.

I didn't check to see if the net bytes returned to their pre-play value after playback was done and the audio player was released.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #13
longjumper Wrote:No, the overall bytes are how many bytes that have been allocated during the course of runtime. The net bytes are the ones that are currently allocated.

Oops, I read that wrong.

The leak I'm talking about would show up in the leak tool BTW.
Quote this message in a reply
Member
Posts: 110
Joined: 2009.07
Post: #14
AnotherJake Wrote:Oops, I read that wrong.

The leak I'm talking about would show up in the leak tool BTW.

Oh, it does. :-)

And thank you so much for being so thorough in your responses, folks. I imagine the major problem here is that I'm actually using up a crap load of memory elsewhere without realising it.

Due to this bug I'm unable to use Instruments to help me figure out what's going on.

http://www.fluttermind.com
Fluttermind - Games for Everyone
Quote this message in a reply
Post Reply