Audio programming newbie

Member
Posts: 21
Joined: 2009.10
Post: #1
Hi again! Smile

Okay, I've very little audio programming experience - I've played with SDL_mixer in the past, but I've no understanding of the underlying principles of audio programming, and I'm not interested in those underlying principles - I just want working music and sound FX with as little effort as possible Rasp

Anyway, I'm looking to add audio, both music and sound FX, to a prototype I have running on my iPod touch; at the moment I'm using AudioServicesPlaySystemSound() and friends, but that isn't very flexible, and of course is useless for music; I'm just wondering whether there is a library out there which will work on both 2.x and 3.x, and has an approximation of the ideal interface I've listed below?

Cheers! Smile

Code:
PlayMusic( char* )
MusicPlaying() : bool
MusicVolume() : double
MusicVolume( double{0.0-1.0} )
PauseMusic()
MusicPaused() : bool
ResumeMusic()

Code:
CreateSFX( char* ) : SFX*
DestroySFX( SFX* )
PlaySFX( SFX*, leftVolume : double{0.0-1.0}, rightVolume : double{0.0-1.0})
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #2
Code:
PlayMusic( char* )
MusicPlaying() : bool
MusicVolume() : double
MusicVolume( double{0.0-1.0} )
PauseMusic()
MusicPaused() : bool
ResumeMusic()

AVAudioPlayer

Code:
CreateSFX( char* ) : SFX*
DestroySFX( SFX* )
PlaySFX( SFX*, leftVolume : double{0.0-1.0}, rightVolume : double{0.0-1.0})

Nothing that I know of on iPhone that simple.
Quote this message in a reply
Member
Posts: 21
Joined: 2009.10
Post: #3
Thanks for that AnotherJake! I'd seen references to AVAudioPlayer but thought it was introduced in 3.x - for whatever reason searching the documentation on my Mac doesn't return any results Wacko

AnotherJake Wrote:Nothing that I know of on iPhone that simple.

I did say that interface was my ideal Wink - not even SDL_mixer provided an interface that simple, sadly... anyway, I've taken a look at AVAudioPlayer, although I can't perform any tests where I am at the moment, so... is it possible/practical to use AVAudioPlayer for playing sound FX? As the documentation says...

iPhone OS Reference Library Wrote:Using an audio player you can:
  • Play sounds of any duration

[lots and lots cut]

play
Plays a sound asynchronously.
Code:
- (BOOL)play
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #4
the_wandering_monster Wrote:I did say that interface was my ideal Wink - not even SDL_mixer provided an interface that simple, sadly...
Hehe, my sfxLite library has an interface that simple, but I haven't put out an iPhone version yet.

the_wandering_monster Wrote:anyway, I've taken a look at AVAudioPlayer, although I can't perform any tests where I am at the moment, so... is it possible/practical to use AVAudioPlayer for playing sound FX? As the documentation says...

It's not really designed for that, no. Use it for background music, not sound effects. The best API to use for sound effects on iPhone is OpenAL. OpenAL isn't too hard to use, but it's not brain-dead easy either. Google for some iPhone OpenAL tutorials and that might help you get started. Otherwise, AudioServicesPlaySystemSound is probably your best bet for sound effects on iPhone.
Quote this message in a reply
Member
Posts: 21
Joined: 2009.10
Post: #5
Sorry to pester, but for what reasons is AVAudioPlayer not suitable for sound FX? Just performance reasons?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #6
Yes, performance reasons. I don't know how AVAudioPlayer is actually implemented, but I think it would be safe to assume it's based on Audio Queues. Audio Queues is used to stream audio data from disk primarily, including compressed audio. Plus, it's used to decode compressed audio automatically in specialized hardware on iPhone. Relative to simply playing an uncompressed sound from RAM, the queuing and decoding system of AVAudioPlayer is large and cumbersome because of what it has to do, and is simply not designed to do efficient playback of short sounds stored in RAM. Best to use other APIs which were designed for that purpose. You can certainly try to use AVAudioPlayer for that, but it just isn't recommended. If you do use AVAudioPlayer for that purpose, do be certain not to attempt to play more than one compressed sound at a time or you risk freezing the device (not sure about newer versions, but I've frozen on iPhone OS 2.x at one point, while messing around).
Quote this message in a reply
Member
Posts: 21
Joined: 2009.10
Post: #7
Cheers, I'll go take a look at OpenAL... again... no doubt über-programmers love its flexibility and aren't fazed by the attendant complexity, but I really wish there were a simpler way (which wasn't really just for playing beeps Rasp)

I'm confused by the warnings against using AVAudioPlayer to play multiple simultaneous sounds though, as the documentation states that you can "play multiple sounds simultaneously, one sound per audio player" Huh
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #8
the_wandering_monster Wrote:I'm confused by the warnings against using AVAudioPlayer to play multiple simultaneous sounds though, as the documentation states that you can "play multiple sounds simultaneously, one sound per audio player" Huh

You can play multiple sounds simultaneously with AVAudioPlayer, just don't try to play multiple *compressed* sounds simultaneously. The specialized audio decoding hardware in the iPhone can only handle one *compressed* stream at a time. They might have fixed it in newer versions of the OS so it won't freeze the device like it used to, but at the very best it would still only be able to decompress audio in software instead, which is *very* performance intensive. Note that IMA4 is kind of an exception, in that it is compressed a bit, but decodes pretty efficiently on the CPU.
Quote this message in a reply
Member
Posts: 21
Joined: 2009.10
Post: #9
Yeah, I still can't test where I am at the moment, but reading further the documentation states that multiple compressed sounds can be played, but only on 3.x, which limits its usefulness to me (and, as you mention, with every other sound after the first being decompressed in software); doesn't look like it will be too hard to get music working, but sound FX are another problem... if I'm able to make sense of OpenAL I'll definitely be releasing a wrapper for it for programmers like me Blush Rasp
Quote this message in a reply
Member
Posts: 65
Joined: 2009.03
Post: #10
I've put a simple sound manager class up on my blog. It allows you to play background music, create playlists for that music and also supports OpenAL for sound effects. The API is simple and it may be enough for your needs.

Sound Manager

Mike

iPhone Game Development Blog - 71Squared
Quote this message in a reply
Post Reply