MOD me up!
MOD files are like MIDI files, only the MOD includes it's own instrument set, called samples, and instructions on how to repeat and alter those samples to make a tune.
Good news: there are nice-sounding, funny MOD files that are about 30KB in size.
Better news: There is a popular library to play them! It's called Mikmod, and your distro has it (and it's a dependency for KDE's multimedia packages too).
Even better news: It has support for playing simple sounds (samples in mod lingo) by calling a couple of functions.
Awesome news: It includes a software mixer so you can just tell it to play this, then play that, then that, and a tune in the background, and everything sounds at the same time.
So, we have a winner. This baby can handle everything I need for the game!
But... is that a snake in your pocket?
I can't find a Python binding for it. I am sure as soon as I post this article someone is going to come up and tell me, here they are, moron! But I just can't find any.
So, I decided to do something I wanted to do already and learn to use Pyrex. Pyrex is a tool to write python extensions, with almost-free access to C libraries, in an almost-python language (only minor syntax differences).
That way, I could write a Python module to use Mikmod.
You know what? It was almost scarily simple 2. I didn't wrap all of Mikmod 3 because I don't need it, but now I can do stuff for games and apps almost trivially.
Even more: Pyrex has awesome distutils support, so building the extensions, usually a pain in the rear, is trivial (mostly you just copy and delete stuff, with some search and replace).
One thing I found I did nicely is this: Mikmod requires you to call Mikmod_Update every once in a while so it fills the soundcard's buffer with stuff to play. If you don't, it skips.
So, I just started a thread that loops and takes care of it. You don't even have to know about it to use the extension. Oh, sure, if your Mikmod is not threadsafe, it breaks. Well, get a decent Mikmod package, then.
How does it look?
Here's a whole noisy proggie
#Load the modules