OpenAL: how to play multiple sounds at the same time and mix them?
Asked Answered
U

1

5

I have used SDL_Mixer before, and it does this job correctly: when i play one sound with one single function call 10 times in sequence, all the sounds will get mixed together. But in OpenAL when i play a sound with alSourcePlay(), it just plays one sound without mixing into the previous sounds.

So, how can i play more than 1 sound at the same time?

Underbid answered 22/4, 2011 at 22:23 Comment(0)
P
12

You need one source per sound.

A buffer holds the raw sound samples, and can feed one or several sources (at a time and simultaneously). A listener defines where your "ear" is (there is only ever one!). A source is a single instance of a sound, given a location in space, a volume, a buffer to pull samples from, and so on.

So, for 2 sounds to play simultaneously, you need 2 sources.

Procne answered 22/4, 2011 at 23:0 Comment(5)
ah... this will complicate things, but i think i can do it, thanks, ill accept answer when ive tested it successfully. btw what you mean "there is only ever one!" ? doesnt stereo need two ears? so if i rotated my head in the game, the sound would be heard differently. or does openal do this trick somehow?Underbid
Maybe "head" is a better word instead of "ear". Of course you hear stereo, or even surround if your soundcard supports it. The listener is where your head is located in virtual space (and there is only one). The concept behind sources and buffers is actually not complicated at all. Sources are just what the name says... sources of sound somewhere in virtual space. Buffers only provide the data.Procne
just one more question... do i have to delete the source once i have finished playing it? atm im storing the current frame sounds into a vector, and then in the end of render loop i go through them and do this for each: alGenSources(1, source); alSourcef(source[0], ...); alSourcePlay(source[0]); so i need another loop that checks if the sound has stopped playing, and if so, then i call alDeleteSources() for those sounds that have finished playing ?Underbid
You can delete sources that have finished playing (check alGetSourcei with AL_SOURCE_STATE), but you need not necessarily. Nothing prevents you from playing a source again (with the same or different sound/volume/position).Procne
thanks, i think i start to understand openAL now, basically, i need to first allocate 256 array of sources with alGenSources() (because thats the limit windows can play at once), and then never again touch to that function. if i play new sound, i just modify the sources array data. (feel free to point out if theres anything incorrect here!)Underbid

© 2022 - 2024 — McMap. All rights reserved.