Spacefight – Sound and music – Part 9

In 1992, you could compose your own music in Mario Paint (Nintendo, SNES)

One of the most overlooked things when you create your first game, is the sound. For me, the sound really makes a game come to life. I can just look back to when I was a kid. I could play a pretty bad Nintendo 8-bit game and still enjoy it. Solely because of the good tunes.

Making music

I’ve been looking hard for a complete DAW (Digital Audio Workstation) for Linux for ages. There are many interesting projects out there, but none that fills my needs. However there do exists some very competent and free applications out there like Ardour. The sound system under Linux is pretty well made, with support for “close-to” real-time audio. But it’s a bitch to configure properly. I think when all these issues has been addressed Linux can start competing as a serious contender as a DAW.

For a long time I’ve been using Avid Pro Tools under Windows while recording music with my two man band Skald. In the music production industry, Pro Tools has become the “industry standard” like Adobe Photoshop is the industry standard for the print and graphical design industry. It has done a very good job in making it possible for us to record and produce two records, even though you’ve been forced to restart your computer every 30min and the Pro Tools took approx. 3 minutes to start up.

One day I got tired of Pro Tools acting up. The Mbox2 mini also got some issues like crackling noise and hick ups. It was clearly time for an update. I purchased a new sound interface Roland Quad Capture and started out giving Reaper a go.

Reaper quickly came to amaze me. I downloaded the installation file (9 MB in size) and installed it. Reaper started up in less than 5 seconds which is amazingly quick in comparison to it’s contenders. Functionality-wise I’m more than happy. Reaper itself has wide a range of good renowned plug-ins, and it has a great support for VST plug-ins. The only thing I actually miss from Pro Tools is the magical L2007 mastering plugin from Massey Plugins. But I hope it will arrive to VST in the future or something as easy to use.

Even though Reaper is not a free program, it has a very fair pricing model. If you’re a home user or professional with less than $20.000 in revenue, you can go for the discounted license that only costs $60. Personally I think Reaper easily can compete Pro Tools and Cubase. Give it a try if you’re looking for a audio recording/editing platform if you’re not heavily dependent on Pro Tool plug-ins. And if you like it, support the guys over at Cockos and purchase a license.

Melody style

Since the game I’m making is quite a retro game. So I wanted to make some music that had a feeling of retro gaming. I did a quick Google search and I found the 38911 Bytes VST synth, which produces a close to Commodore 64:esque sound. I wrote one short melody for the menu screen and one melody for the actual game.

Sound effects

Sound effects is another matter though. I spent many hours looking for cool free VST instruments, which could make an effect I could use for the game. But no such luck. Luckily there are sound effects libraries online that offers a variety of effects for free. One of those websites are Where I found most of the effects for my game. I did however import all the sound effects in Reaper. I added some re verb, compression and adjusted the EQ, to make the effects sound more fitting in the game and making sure the weren’t over the DB limit.

Sound format

OGG was the most obvious choice since the license is very friendly and the sound quality is better at lower bitrates (from what I’ve heard, Spotify uses it). OGG is also supported in SDL_Mixer which supports mp3, wav, flac etc.

Sound in SDL

I have to admit that I struggled quite a bit getting the sound to work. It went from no sound, crash, no sound, sound, crash, and finally sound without crash. I also had to make the effects available for all game objects. So I so ended up writing a SoundLibrary, which loads, plays effects and music. SDL_Mixer makes a difference whether it’s a melody or an effect. First off I needed initialize the SDL_Mixer which I’ve done this way:

The Mix_AllocateChannels(24) is an important bit. When you have a lot of different effects playing at the same time, the amount of channels is the limit. The flags I passed indicates what audio formats I intend to use.

Like the animationlibrary, the soundlibrary is responsible for cleaning up all the allocated resources. It gives the possibility to adjust the sound on each effect when loading the sound, since a lot of the effects differs in volume.

One thing I had some problems with was the looped sound effect. In the game the ship has a shield the player can raise with the CTRL key. While the shield is active it should emit a shield noise, and when the player stops using the CTRL key it should stop, or when the shield meter runs out.

When a sound is played in SDL_Mixer it’s assigned to a channel. Think of the channel as a channel on a mixing board. It returns this channel as an integer. So now we know where the sound is played, and we can halt it. But an with a random value is hard to pass around, so I created a dictionary to keep track on which channels that has been reserved for looped audio. If you look at the example above I call “addReservedChannel” and call it “effect-loop”. So when I need to stop playing audio on that channel I call stopReservedChannel with the key “effect-loop” (if it exists).

The sound library consists of a vector, filled with objects I named sound. The sound object keeps track on the volume, what kind of sound it is (effect or melody) and the routines to play the sound and it seems to work and sounds fairly good!

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.