Re: Clean up memory after playing sound
Chris Robinson <[email protected]>
| Newsgroups | gmane.comp.lib.openal |
|---|---|
| Message-ID | <[email protected]> |
On Saturday, May 29, 2010 7:56:51 am Nameless wrote: > I'm using OpenAL as sound engine in FPS game, and I came to first problem. > I'm playing weapon sounds every time when player will shoot, and generating > buffer / source for every sound. Problem is, I always get to the limit, and > then, it refuses to play sound again. I still don't have idea, how to clean > up sound memory, so, after every sound played, it'll clean up memory data, > so, I'll stay under limit always. Maybe there's better method, but I still > don't know which. Thanks, Nameless. Hi. With OpenAL, you only have a limited number of sources. Because of that, it's best to keep the buffers and sources separate. A buffer represents a sound, while a source represents an instance of a sound. One buffer can be used by multiple sources at the same time, so you typically only have to worry about loading a sound effect into a buffer once. Then you keep that buffer around so it can be used by future sources when its played again. For efficiency, it's best to allocate a number of sources when the app starts, and put them into a "FreeSources" array for easy access. Calling alGenSources and alDeleteSources all the time during play probably isn't the best thing to do. When a sound is played, you first make sure the sound is loaded (and load it into a buffer as needed), then you take a source out of FreeSources, set the initial properties, attach the buffer to it, play it, and put it in an ActiveSources array. When the source is done playing, you take it out of the ActiveSources array, detach the buffer, and put it back into FreeSources. That's the basic method I use for OpenAL playback in an FPS, and it works very well. It can be a bit tricky to find an optimal method to handle when you run out of active sources (ie. when more sounds try to play than you have sources for), but there are ways of dealing with that, too. Hope that helps. :) _______________________________________________ Openal mailing list [email protected] http://opensource.creative.com/mailman/listinfo/openal