Archive for the ‘Video Game Development’ Category

Writing an Audio Engine from Scratch

Monday, August 8th, 2011

I’ve just spent the last 3 days waist deep in audio file format specifications, audio APIs, and C reference manuals, and try as I might, there’s no other way to say this: writing clean, portable, low-latency, audio code in C is an incredibly difficult task. It’s practically a full time job.

There are lots of reasons for this, but it mostly has to do with the fact that C is a low-level language that (by definition) keeps you close to the hardware. This is great for speed, but terrible for portability and source code file size.

Another issue that I’ve learned about is the fact that there are at least 8 different ways to handle sound in a C program running on Windows (MME, DirectSound, WASAPI, ASIO), Linux (ALSA, JACK, OSS), and Mac OS X (Core Audio). And that despite the recent deprecation of some of those APIs, all 8 are still commonly found in the wild.

Talk about confusing. And, perhaps even more frustrating: many of those APIs are just different enough to make large portions of code written for one unusable in implementing another. Nothing like forcing a programmer to reinvent the wheel 8 different times with 8 different sets of tools.

Fortunately, there are people out there who strive to make all of this as easy as possible. To the developers over at SDL, PortAudio, OpenAL Soft, libsndfile, or any other project that implements an open-source cross-platform audio API in C: you deserve an immense amount of praise and gratitude for your work. I only hope my small financial contributions will go to a good cause.

Unfortunately, for all their hard work, I don’t think I’ll be able to use any of it.

My goal is to learn as much as I can about audio engines. The way I see it, the best way of going about that is writing one from scratch. I know there are tons of excellent libraries already out there, and if I used one, I cold probably be up and running in a day, but I wouldn’t really know or understand what I was running on. It’d all just be a black box to me.

To that end, I’ve decided to start ab initio. When I’ve learned much more than I know now, I’ll gladly stand on the shoulders of giants like libsndfile in future projects, but until then, it seems I’ll be spending the next week or two reinventing the audio engine wheel. But you know, I’m OK with that: it’ll be fun to get down and dirty with C again, and I always enjoy learning new things. :)