How to mix audio using SDL or ALSA lib?

i have played a ".wav" file (use SDL_mixer lib), but i want to play multiply pcm audio file (particular .wav file) at same time. How to do that? Please suggest for me!
Last edited on
http://www.libsdl.org/cgi/docwiki.cgi/SDL_MixAudio
You can use SDL_MixAudio to mix audio data together. They say you should not use it to mix more than two audio data streams.
Yes, that say: "Note: Do not use this function for mixing together more than two streams of sample data". So i think can't use SDL_MixAudio to play multiply stream, particular stereo stream.
Mixing streams is not that hard so you can probably write the code for it yourself. To combine sound waves you just have to add the hight of the waves.

Example:
1
2
3
stream 1: -3 -2  0  2  3
stream 2:  0 10  5  2 -1
sum:      -3  8  5  4  2


The sum becomes the mixed stream. If you have 3 streams you just add all 3.

You also have to handle overflow/underflow. If the samples are of type Sint16 the sample value has to be between -32768 - 32767. If you get a value that is outside the range when you sum, and just convert it to a Sint16, the sound will overflow/underflow and it will sound very bad. To avoid this you can set all samples that sum to a smaller value than -32768 to -32768 and all samples that sum to a greater value than 32767 to 32767.

This will sound much better but if you have to do this with a lot of samples it will also sound bad. This is mostly a problem if you have a lot of loud sounds playing at the same time because loud sound often have larger sample values. I don't think you will have to worry about it too much.
Last edited on
Using SDL_mixer is probably easier. Have a look at Mix_PlayChannel: http://jcatki.no-ip.org:8080/SDL_mixer/SDL_mixer.html#SEC28
yeah, thank so much, i could played 2 .wav file at same time by loading 2 file into 2 Mix_Chunk then call Mix_PlayChannel() function. And addition question, "How to get raw data from pcm stream and synchronize exactly in realtime?". Please sugguest for me!!!
Topic archived. No new replies allowed.