Suggestions for Sound Implementation

Apprentice
Posts: 12
Joined: 2009.04
Post: #1
I am building a game which is actually a proof of concept that has the following capabilities as sound is concerned: I want it to play effects (2 or 3 effects simultaneously), to have looped sounds, and background music. What do you suggest using?

I originally intended to use SoundEngine.h from Apple's crashLanding.app game for my little game, but I see that many people have problems using it. So I would like to ask what capabilities does SoundEngine have?

1) Can it play 2 or 3 effects simultaneously?
2) Does it have any limitations on Sound formats?
3) Can it fade in and out sounds?
4) Can it be used with the simulator?

Thank you in advance.
Quote this message in a reply
Member
Posts: 257
Joined: 2004.06
Post: #2
AVFoundation and AVAudioPlayer. Don't use the SoundEngine code. From what I understand, it was only meant as demo code and not to be used in actual production. If you need a little more control, you can go with OpenAL, as well.

The brains and fingers behind Malarkey Software (plus caretaker of the world's two brattiest felines).
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #3
Malarkey Wrote:Don't use the SoundEngine code. From what I understand, it was only meant as demo code and not to be used in actual production.
When googling for it, I find that many people have a differenty opinion, that SoundEngine works just fine. However, it is not available from Apple any more. I wouldn't mind checking it out.

PS: Found it!

http://www.71squared.co.uk/2009/03/iphon...utorial-1/

and some comments about what needs updating:

http://www.iphonedevsdk.com/forum/iphone...-code.html
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #4
SoundEngine is pretty convoluted, but that's what I used to figure out how to get OpenAL working on iPhone. It uses AudioQueues for background music and OpenAL for sound effects. For iPhone I would recommend using AVAudioPlayer for background music.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #5
Ingemar Wrote:...SoundEngine works just fine.
If leaking memory and crashing is "fine" then yes, it works just fine Wink

Check Apple's oalTouch example if you need a guide on how to implement OpenAL on the iPhone. OpenAL itself is a tiny API and dead simple, but some iphone-specifics and audio loading code can get a little ugly.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #6
Actually, the memory leaks were probably from Audio Queues, not SoundEngine, and in that case have been fixed by Apple in more recent versions of iPhone OS (I know this because I was in contact with Apple about the Audio Queue memory leaks). Not saying that I'd recommend SoundEngine though, since I personally think it is overkill, and convoluted (as I already mentioned). OpenAL isn't hard to use at all, and AVAudioPlayer is dead simple.
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #7
Speaking of audio, how about libao? Is there someone here who has used it and can make an assessment? It comes with a Mac "plugin", which is a layer on CoreAudio, so it does have Mac support.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.05
Post: #8
Hi everyone,
I’m about to release my app and I have been using soundengine so far. I don’t know if it has bug or not. So far, I have not seen any problem. But regardless, because I have to launch the app quickly, I don’t have time anymore to research other alternative. So, I’m pretty much stuck with it for now.

So, I’m also interested to hear the answer to the original question, especially #3. How to do fade-in and fade-out with soundengine for background music, if it’s even possible?

Thanks.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #9
Ingemar Wrote:Speaking of audio, how about libao?
Last time I looked, it didn't seem to do much other than load and play/stream sounds - no control over positioning/mixing of any sort. Plus it's GPL...
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #10
Frank C. Wrote:Last time I looked, it didn't seem to do much other than load and play/stream sounds - no control over positioning/mixing of any sort. Plus it's GPL...
GPL can be a problem. Features can be added. What I want is a nice cross-platform abstraction layer. But you are right of course, if I can't even make stereo placement, it is too primitive.
Quote this message in a reply
Member
Posts: 260
Joined: 2005.05
Post: #11
I took the Mac plugin code for libao, and could turn that into a reasonably small Core Audio demo. It is still uncomfortably large, but a lot smaller than anything Apple made on the subject. Still under GPL, so it doesn't really help...

I think I'll turn to OpenAL now. CoreAudio seems like a complicated, time-consuming dead end. SDL is another option that could be right. Is there anyone who can compare the two?
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2009.04
Post: #12
Thank you all for your answers. I didn't like the other offers, and I took the time to write my own sound engine. It supports positioning, effects like fade in and out, and changing gain on the fly. Loading and playing sounds works fine, and I am already using it without problems.

Maybe I will post something when I am sure I won't cause any memory leaks.
Quote this message in a reply
Member
Posts: 73
Joined: 2009.03
Post: #13
Soulstorm,
I know I would appreciate any help you can offer. I am looking at the example code in oalTouch, and it is way more complex than some people have made it sound.

Can anyone explain how the sound file and the source are related? This example loads the sound file into some kind of data structure, but I don't see how this is related to the source when starting and stopping the sound. What if I want to play various different sounds, even if only one at a time? I thought that each sound would be assigned to a source, but that doesn't seem to be the case.
Quote this message in a reply
Member
Posts: 446
Joined: 2002.09
Post: #14
Gillissie Wrote:Can anyone explain how the sound file and the source are related? This example loads the sound file into some kind of data structure, but I don't see how this is related to the source when starting and stopping the sound. What if I want to play various different sounds, even if only one at a time? I thought that each sound would be assigned to a source, but that doesn't seem to be the case.

Audio files are loaded into buffers (see alGenBuffers and alBufferDataStaticProc). You then bind buffers to a source (or sources) using alSourcei(mysource, AL_BUFFER, mybuffer). Once a source has a buffer attached you can use alSourcePlay/Pause/Stop etc. to control playback.

Most OpenAL examples simply map one buffer to one source and leave it at that (even that SoundEngine code works like this). But it's far more powerful when you keep buffers and sources separate, and consider sources as "speakers" and buffers as "effects". You can then tell any speaker to play any effect at any time, allowing the most flexibility.

I should also note that the alBufferDataStaticProc stuff is a very un-Apple like mess but it lets you manage the audio data rather than having OpenAL make a copy. The code is all laid out in the oalTouch example and once it's done you don't have to look at it again. Set it and forget it.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #15
Gillissie Wrote:Soulstorm,
I know I would appreciate any help you can offer. I am looking at the example code in oalTouch, and it is way more complex than some people have made it sound.

Can anyone explain how the sound file and the source are related? This example loads the sound file into some kind of data structure, but I don't see how this is related to the source when starting and stopping the sound. What if I want to play various different sounds, even if only one at a time? I thought that each sound would be assigned to a source, but that doesn't seem to be the case.

I had been typing this out when Frank chimed in already, so I guess I'll just add my explanation in addition to his.

OpenAL is basically three parts that you have to think about: a listener, buffers and sources

You init OpenAL by opening an audio device -- simple. Then you create a context for that device and make it the current context -- simple. Each context has exactly one listener. The listener is essentially a virtual equivalent of you in 3D space. Basically it has a position, an orientation (what angle it's positioned in space and what it's looking at), and a velocity (which is only used for Doppler effects). For 2D you'd just set the listener's position/orientation at init and leave it there.

Next up, You load your sound file into a buffer of memory. That's the hardest part since there are no utilities built into OpenAL to do that anymore, and definitely not on iPhone. Just use the code in oalTouch to do that, specifically the MyGetOpenALAudioData function, which as you will notice, returns a block of memory filled with audio for you to pass on to OpenAL. You then pass that data to OpenAL using alBufferDataStaticProc in the oalTouch sample. That particular function is also conveniently found in MyOpenALSupport. alBufferDataStaticProc returns an ID which you use to tell OpenAL which buffer of sound you want to use in future calls.

Okay, so now you have OpenAL initialized, your listener positioned, and loaded a sound file and sent it to OpenAL and it returned an ID for you to refer to it with. Like I said, the loading was the hard part. Note that you cannot directly play a buffer. Instead, you attach the buffer to a source and play the source. Also note that you can attach a buffer to any number of sources.

A source is the opposite of the listener, and you can have multiple sources. A source would represent an object in the game. Note that a source doesn't actually have to represent an object in the game, but that's the paradigm. The source keeps track of things like position and velocity as you specify them. The idea is that if you have a spaceship moving around on screen and it shoots lasers, then the source of the lasers moves around along with the spaceship so that it can be panned from speaker to speaker to give the effect of position. Along with those various pieces of info you specify, like position, you also have to specify which buffer you want the source to play. You do that with something like alSourcei(mySourceID, AL_BUFFER, myLaserSoundBufferID); Then later on, when you want to play the sound, you call alSourcePlay(mySourceID);
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  gluUnproject implementation TomorrowPlusX 3 10,386 Sep 19, 2006 08:27 AM
Last Post: TomorrowPlusX
  Shader implementation questions akb825 12 5,251 Jun 5, 2006 04:39 PM
Last Post: akb825
  New OpenAL implementation willThimbleby 7 3,534 Oct 28, 2003 05:51 PM
Last Post: deekpyro
  Implementation of 2D Tiles using OpenGL aaronsullivan 18 12,548 Jun 14, 2003 05:41 PM
Last Post: OneSadCookie