[LAD] mixing music and sound effects using libao

Hermann Meyer brummer- at web.de
Thu Apr 30 12:33:59 UTC 2015

Am 30.04.2015 um 10:41 schrieb dave at 661.org:
> I've been playing around with libsndfile, libvorbisfile, libmodplug, 
> and libao to provide background music and sound effects for a game 
> engine.  I have a solid handle on making one noise at a time, but I 
> don't understand mixing.  At first I thought I would have to write my 
> own mixer and handle the channels, sample rate, and sample size issues 
> myself.  I really don't want to have to do that.  Then I noticed in 
> the libao documentation that nothing says that I cannot spawn two 
> threads, each of which calls ao_open_live() and ao_play() on different 
> sound files.  Indeed, the docs for ao_initialize() seem to suggest 
> this can be done with the statement that it be called in the main thread.
> I tried this and it works so erratically that I'm not sure libao was 
> written with this in mind.  I get these results in order of likelihood:
> 1) "Segmentation fault"
> 2) "*** glibc detected *** ./threadtest7: malloc(): memory corruption
>     (fast): 0x0000000001bc5390 *** Bus error"
> 3) "*** glibc detected *** ./threadtest7: double free or corruption 
> (out):
>     0x00000000018ba2e0 ***"
> 4) A bit of the first file, then the second file.
> 5) Only the second file.
> 6) Both files messily mixed with a zipping noise. (very rare)
> Is libao in fact intentionally capable of doing this?  If not, what 
> are my options for mixing music and sound effects?  I really don't 
> want to use SDL_mixer for a variety of reasons, chiefly because my 
> program is terminal-only.  Virtual beer if you can guess what the 
> program is.


It seems libao is able to do this. Attached is the example code from the 
libao side, roughly hacked in a second thread to play to signals 
simultaneous. Works stable here.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ao_example.c
Type: text/x-csrc
Size: 3658 bytes
Desc: not available
URL: <http://lists.linuxaudio.org/pipermail/linux-audio-dev/attachments/20150430/b78e8cae/attachment.c>

More information about the Linux-audio-dev mailing list