Ah, a good 7 hours worth of sleep, nothing quite like it

Well, I've been working on a very procedural-content-heavy concept (main inspirations: Dwarf Fortress and Spore) for months, on and off, and I've always had vague fantasies how complementing that with a procedural soundtrack would be very cool. Well, a few days ago I finally got curious enough to actually give the idea a try; I figured I had the music theoretical background basically down (I dabble in jazz guitar, you see), so I got hacking.
First things first, choice of language. I played around in puredata for a while, and it's a really cool environment with tremendous possibilities, but me, I'm simply much quicker writing good old-fashioned code instead of dragging boxes around. So, a "real" language. While my main project is in D (which I can only recommend, it's such a boost of productivity over C++), I figured for this extremely explorative experiment something even more dynamic would be a good match, so I settled for Python. The immediate edit-run cycle turned out to be invaluable for testing, as compared to the edit-compile-run of statically compiled languages. Of course, should this ever get to a really usable state, I would reimplement it in a more static language.
Second, I needed a way to actually coax notes out of the speakers. Perhaps there are free softsynth libraries which can do this easily (I'm curious, are there?), but the easiest way to go seemed to be MIDI. In reality, it turned out there's a surprising scarcity of non-bitrotted MIDI libraries in Python (for a language which otherwise seems to have libraries for everything and the kitchen sink), so in the end I settled for a combination of the C portmidi library (which I had to compile myself too, what a hassle) and a crappy ctypes wrapper called portmidizero around that for the Python side. I actually had to fix bugs in that wrapper as I went as it would sometimes crash randomly, but eventually I was able to output notes conveniently to the soundcard and I was good to go. Of course, the basic cheap-soundcard-MIDI sound is awful, but hey, it's an experiment.
So next, the really interesting stuff, the music. I started teaching Python about scales, so my program knows about the major and minor scale as well as the modes of the major scale, i.e. Dorian, Mixolydian, that sort of stuff (if you know a bit about music theory, that should sound familiar).
Then I taught it to form simple seventh chords over a scale by just doing basic thirds stacking, and finally I implemented chord progressions so I could do stuff like a I-IV-V-I progression. I actually did some experiments with that particular progression, but it just sounded awfully stilted and formulaic, so for now I've settled with a more modal approach where I mostly harp on one root chord.
Somewhere along the way I implemented a simple pseudo-walking bass which would simply play chord notes on every beat as well as sometimes chord extensions in between, which actually sounds pretty nice for a first experiment.
As for rhythm, I started with a class which would accept patterns in a form like 'XOXO' and translate that into proper timings. That turned out to be a great concept as I could then very easily write algorithms to generate patterns or create variations on them as they are simply strings.
There are no drumloops so far, but with this rhythm system in place (which is used for all instruments so far), it shouldn't be hard to do something basic.
The last thing I've done so far is the solo-type melody you hear in the mp3, and that is basically a very simply Markov process for the notes with some more or less randomly chosen note durations, with care taken that it realigns with the beat every once in a while so that it doesn't sound too random. The jazzy sound of it might on one hand come from this whole jazz theory thing I've put into it; on the other hand, it actually seems simpler to me to randomly create the kind of jagged, syncopated lines characteristic of jazz/funk improvisation as compared to simpler, more catchy melodies. Though I definitely want to try the latter. The current form of "melody" would get on the player's nerves within minutes, so I need something more subdued as well.
At the moment, the structure of it all is very hardcoded. There probably is a wide range of stylistic variations possible already (I haven't even played with the parameters all that much), but it's not readily accessible. The ultimate goal would be to make this into a (open-source) library which everyone could integrate into their game; ideally, you would simply give it parameters like levels of "danger", "calmness", "excitement" etc and it would generate a soundtrack in realtime for you. That's of course a long way off. But it's nice to dream, isn't it?

Well, that turned out much longer than I hoped. Sorry, I've got an annoying tendency to ramble.