console retirement plan

With new consoles on the market, and plans to move to San Francisco later this year, I figured it was time to cut down on some of the console clutter I’ve collected over the years. For now, I still have our 360 and PS3, but I’ve traded in a bunch of 360 games, given the Wii to my parents, and sold our long-disused PS2 and all our old PS2 and GameCube games. I’ve also sold off a bunch of DS and GBA games, and my DS Lite and GBA SP consoles themselves won’t be far behind.

Thankfully, if I ever get nostalgic, those systems I’ve retired — the PS2, GameCube, Wii, DS, and GBA — can all be emulated. In my last post I talked about using Dolphin to emulate the GameCube and Wii; for the PS2, there’s PCSX2, which like Dolphin can render games at high resolutions to improve quality; for the DS, there’s DeSmuME (and an excellent Android port of it, called DraStic); and for the GBA, there’s VBA-M.

Animal Crossing Wild World running (in Japanese, coincidentally) under DraStic, an excellent DS emulator for Android

Animal Crossing Wild World running (in Japanese, coincidentally) under DraStic, an excellent DS emulator for Android

For the Wii, GameCube, and PS2, I was even able to back up my save games, using GCMM and Savegame Manager GX on the Wii for the GameCube and Wii saves, respectively, and uLaunchElf on the PS2, to dump the saves to a USB stick. In both cases, I had to pull some tricks to run third-party code — on the Wii, installing the Homebrew Channel is fairly straightforward, but on the PS2 I had to use my Swap Magic discs.

To next-gen, or to PC?

New console launches are usually a cause for excitement, but their release just reminds me of what we’re losing with the end of the previous generation. Consoles have come and gone in the past, of course, but the move to digital distribution has made separating the consoles from their content much more difficult this time around. If I sold my 360 or PS3 today, I couldn’t resell all their digital content; I still own that content, and if I bought a new 360 in a few years I could reinstall it, but what about 5 years from now, or 10?

Even if it’s not perfect, the PC does offer a better alternative. Some old games are unplayable today, but if a game has a dedicated community, chances are that someone’s worked out how to run it on modern PCs. Steam’s DRM has so far proven mostly benign, and services like GOG and the Humble Store offer DRM-free downloads, too.

With dozens of great games on Steam, using Linux for PC gaming has never been more viable

With dozens of great games on Steam, using Linux for PC gaming has never been more viable

In fact, I seem to have made the jump to PC already, almost by accident. I’ve played a couple of big-name games this year, like Bioshock Infinite and Tomb Raider, but I’ve spent much more time playing smaller, more interesting games, like Kerbal Space Program, Gone Home, The Stanley Parable, Brothers: A Tale of Two Sons, Kentucky Route Zero, Papers, Please, and most recently, the Double Fine Adventure game, Broken Age, almost all of which are PC exclusives.

PC power

PC gaming has a lot of baggage — the image of the hardcore gamer that spends as much time upgrading and tweaking their Windows PC as they spend actually playing games on it — but these smaller games often defy that image. Most run on Linux and Mac OS X, and most also run on fairly modest PCs; in fact, I’ve spent more time gaming on my now 2011 Macbook Air in the last year than I have on any other system.

I’m sure I’ll want to play another big-budget graphical powerhouse eventually, and I’m not yet sure what I’ll do about that. By that time, a gaming PC with the power of the next-gen consoles might only cost as much as those consoles cost now. I like the idea of gaming laptops, but they’re expensive and clunky; only the Razer Blade delivers suitable power in a sleek, elegant form-factor, but at US$2000 it’s even more expensive than other laptops.

creating a dynamic soundtrack for switchbreak’s “civilian”

Over the last week I’ve put a bunch of time in to my new game project, a Switchbreak game called Civilian. I’ve been working on music for it, but this blog post isn’t about music — it’s about the crazy stunts you can pull in modern interpreted languages.

Dynamic music in Flash?

Most Flash games use a looping MP3 for background music — it takes just a couple of lines of code to implement, and while the looping isn’t perfectly seamless (there are brief pauses at the start and end, added by the MP3 encoder) it’s usually close enough. For Civilian, though, I wasn’t satisfied with a simple looped track. It’s a game about the passage of time and about player progression, and I wanted the music to reflect those things.

What I really wanted was a dynamic music system, something that would let me alter the music’s sequence or instrumentation on-the-fly in response to the player’s actions. There was no way that was going to work with simple, non-seamless looping MP3s, though — I needed to start working with audio data on a much lower level.

Writing a low-level mixer in AS3

Thankfully, the Flash 10 APIs do give you low-level audio functionality. You can get raw audio data out of an MP3 file, and then send that to audio buffers for playback; I’d already done just that in fact, to implement a seamless MP3 looper, and that gave me a crazy idea: if I could get audio data from one MP3 and play it back, could I also get data from two or more MP3s, mix them, and play them back all at once?

Once I’d confirmed with a simple proof-of-concept that the answer was an emphatic “yes”, I set about adding more tracks, and then implementing features like panning and volume conrol. By this point, the amount of CPU power required to run this mixing was significant — about 40% of one core on my 1.7Ghz i5 Macbook Air — but Flash had no trouble keeping up while running some simple gameplay at 60FPS.

A screenshot from my test app, with five channels of audio running

A screenshot from my test app, with five channels of audio running

From mixer to sequencer

A few days later I had more than just a mixer: I had a simple pattern-based sequencer. Instead of looping MP3s from start to finish, it splits the MP3 tracks in to bars, and then plays those bars in accordance with a sequence stored in an array in the AS3 code.

This actually fits quite well with how I tend to write my music. I can arrange the track basically how I want it in Ardour, then record each unique section of each track to audio, and string those sections together to produce a single MP3 track for each instrument. Then, I can create a sequence within the AS3 code that reassembles those sections in to my original arrangement.

Each bar can have its own settings, too, somewhat like the effects on each note in a tracker. So far, these just let me set the panning or volume for each track, or set up a volume slew (ie: a fade in or fade out) to run over the course of the bar.

Making the music dynamic was just a matter of replacing the static sequence array with code that generates the sequence on-the-fly. I have pattern templates for each track which I combine to create the sequence one bar a a time, adding or removing tracks or replacing one part with another (perhaps with a nice fade in/fade out) based on what’s happening within the game world.

Pushing interpreted languages

As if all the above wasn’t enough, I decided to add an optional audio filter on the output. For certain scenes in the game I want to be able to make the music sound like it’s coming from a radio, so I added a simple bandpass filter, based on a Biquad filter implementation from Dr. Dobbs. If the filter is having any impact on my sequencer’s CPU usage, it’s far too small to notice.

Eventually, I gave up trying to think of efficient ways of doing things, and just started doing them in the simplest way possible. I’ve since done some optimisation work, to help retain a steady frame rate on slower systems (using my old Latitude E6400, clocked down to 800Mhz, as my test machine), but those optimisations are totally unnecessary on more typical systems.

Ten years ago, I wrote audio mixing code for the GBA, and it looked something like this

Ten years ago, I wrote audio mixing code for the GBA, and it looked something like this

The last time I wrote audio mixing code, it was for the ARM7 CPU inside the Gameboy Advance. On that system, compiled C code wasn’t fast enough, so I had to re-write the critical loops in hand-optimised ARM assembler code to get the necessary performance. To see an interpreted language do the same things so easily is still somewhat mind-boggling, but it’s a testament to the advances made in modern interpreters, and to just how fast modern PCs are.

It’s somewhat fitting that this was the week that the GNOME developers announced that JavaScript would become the preferred language for GNOME app development. That announcement caused a surprising amount of backlash, but I think it makes perfect sense: not only is JavaScript a capable and incredibly flexible language with a huge developer community. but it performs incredibly well, too. In fact, I doubt that any other interpreted language has ever had as much developer time invested in improving its performance.

The writing’s on the wall for Flash, of course, but HTML5 and JavaScript are improving rapidly, and frameworks are being written that should make it just as easy to write games for them as it is to write for Flash today. When that happens, it should be a simple matter to port my dynamic music system to JavaScript, and I’ll be very excited to see that happen.