[LAD] Libraries for simple audio programming, libao, other options?

nick thomas jesuswaffle at gmail.com
Wed May 2 20:48:06 UTC 2007

On 5/2/07, Stephen Cameron <smcameron at yahoo.com> wrote:
> So, I found libao ( http://xiph.org/ao/doc/ ) which looks
> nice and simple, and the sample program I tried
> ( http://xiph.org/ao/doc/ao_example.c ) worked right off
> the bat,
If you want real-time, then you'd better use a callback-based API.
PortAudio might be a good choice for your application:


> and the idea of synthesizing my own sounds by
> constructing waveforms using sine waves is appealing for
> a retro styled app like mine (though I have no experience
> doing that and only a vague memory of trigonometry), but
> I wonder about how to mix sound (add the waveforms, I guess)
> and construct them on the fly (or trigger pre-computed
> chunks of audio).
Since you're just doing this for fun, you might opt to synthesize your
own sounds in real-time, but it would probably be more "sensible" to
synthesize them ahead of time. In this case, I suppose it really comes
down to your personal preference.

To answer your question about mixing: yes, you just add the waveforms.
You have to be sure to avoid clipping, so it can be a bit more
complicated than that, but that's the gist of it. An explanation of
how analog-emulation synthesis works is out of the scope of this
email, but I'm sure there's some literature you can find on the
subject. For my own part, I just trawled Wikipedia and gradually
assembled a picture of how it all works, but that's a rather
time-consuming option. Maybe someone else can recommend some
literature. :)

> So, I imagine that for a game, in order to satisfy the
> real-time-ish requirements, I'd instead construct maybe
> 1/10th or 1/20th second chunks, which would be queued up
> in my program for playback, and whenever some event happens
> which demands some sound be produced _right now_ (like the
> user presses the "fire" button, and so needs some bleeping
> laser gun sounds to be produced) I would then start adding
> (arithmetic addition) laser-gun-waveform-data into whatever
> chunks were already in the queue, and so out would come
> the sound...
I think that if you look at the way PortAudio works, you'll see that
this sort of "queueing" logic is taken care of for you by the library,
so you don't need to worry about it.

> I might also want to playback some ogg encoded files for maybe
> prerecorded sound effects and music to be mixed
> in with the synthesized sound effects.  libao won't do that by itself, but
> I think there are some other libs on xiph.org that do that, but
> I'm not sure how well they'd accomodate mixing random bits of
> sound in with them.
If you're going for the "retro" effect, you might actually consider
synthesizing the music in real-time from a MIDI-style score. :) If
you'd rather do it pre-recorded, however, the approach that you'll
take would be to find a decoder library, have it decode your music
into a memory buffer (probably streaming it somehow), and then mix
that into your own output buffer along with the sound effects.

Hope that helps!

More information about the Linux-audio-dev mailing list