[Openal] Playing a sound twice at the same time.
Jason Daly
jdaly at ist.ucf.edu
Mon Aug 27 08:23:04 PDT 2007
Leben Ojanen wrote:
> Hi all, thanks for the previous help with my threading errors.
> Fortunately I was able to get a little help and fix everything, and
> just so you know the problem wasn't with OpenAL, yay.
>
Glad to hear it's working for you now :-)
> Right now I want to be able to load a sound once, then play it more
> than once, at the same time.
>
> So far I have been unable to do this, probably due to my lack of
> understanding of how OpenAL works.
>
> At this time, I can load a sound, but trying to play it twice with my
> current implementation causes the first "playback" to just stop while
> the other starts.
>
First of all, this is how it's supposed to work with a single source.
If you call alSourcePlay() on a playing source, the source is stopped,
then starts playing from the beginning again.
In order to hear multiple instances of the same sound, you'll need to
have multiple sources playing the sound. I'm assuming you're loading
the sound into a single buffer. All you need to do is create several
sources, and assign that buffer to each source. You can then call
alSourcePlay() on each source in succession to hear the sound.
I don't know what you're doing in your application exactly, but if you
need this layered playback to happen dynamically, you can create several
sources (say four or five) intended for this sound and manage them from
a pool. When you need to play the sound, you can find a free source in
the pool and start it playing. When the source is done playing, you can
mark it free again. If you run out of sources in the pool (e.g. all of
them are playing at the same time), you can pick the least recently
played source and call alSourcePlay() on it again to get the effect.
This should work pretty well for a transient sound like a gunshot,
though you may want to experiment with the number of sources in the pool
to get the best resource usage vs. quality tradeoff.
You can also generate sources on the fly as they're needed (instead of
pre-allocating them), but you'll have to be smarter about your source
management when you run out of sources. There's also a small amount of
latency added if you do this.
Hope this helps,
--
--"J"
"I'm a castaway stranded in a desolate land,
I can see the footprints in the virtual sand."
--Neil Peart
More information about the Openal
mailing list