Suggestions for Sound Implementation - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Suggestions for Sound Implementation (/thread-1371.html)
Pages: 1 2
Suggestions for Sound Implementation - soulstorm - Apr 30, 2009 05:15 AM
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.
Suggestions for Sound Implementation - Malarkey - Apr 30, 2009 09:14 AM
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.
Suggestions for Sound Implementation - Ingemar - May 1, 2009 01:42 AM
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!
and some comments about what needs updating:
Suggestions for Sound Implementation - AnotherJake - May 1, 2009 07:11 AM
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.
Suggestions for Sound Implementation - Frank C. - May 1, 2009 11:09 AM
Ingemar Wrote:...SoundEngine works just fine.If leaking memory and crashing is "fine" then yes, it works just fine
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.
Suggestions for Sound Implementation - AnotherJake - May 1, 2009 11:23 AM
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.
Suggestions for Sound Implementation - Ingemar - May 4, 2009 01:53 AM
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.
Suggestions for Sound Implementation - pabris - May 4, 2009 08:06 AM
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?
Suggestions for Sound Implementation - Frank C. - May 4, 2009 01:43 PM
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...
Suggestions for Sound Implementation - Ingemar - May 4, 2009 09:08 PM
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.
Suggestions for Sound Implementation - Ingemar - May 5, 2009 05:35 AM
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?
Suggestions for Sound Implementation - soulstorm - May 14, 2009 12:43 PM
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.
Suggestions for Sound Implementation - Gillissie - May 15, 2009 12:29 PM
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.
Suggestions for Sound Implementation - Frank C. - May 15, 2009 01:13 PM
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.
Suggestions for Sound Implementation - AnotherJake - May 15, 2009 01:22 PM
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);