Welcome back to the Digilent Blog!
A little while ago, you likely saw the post that I did on my Color Invaders project. It’s a lot of fun to play as is, but it was suggested that the game could be made even better if there were some sound effects included as well. So I did.
But how does one actually create custom sounds of their own? Let’s start with how sound works. Sounds and noise are pressure waves (or vibrations) in a particular environment that we can hear. Different sounds occur when the frequency of these vibrations change, altering the pitch that our ears detect. Speakers take advantage of this by vibrating a piece of material at different frequencies to produce a variety of sound.
For my project I used a piezoelectric speaker to create all of the game sounds that I wanted to make. By turning the voltage applied to the piezo element on and off at a desired frequency, this caused the piezo material to bend and unbend at that same rate, creating vibrations in the air that we can hear as sound. By using Wikipedia’s page of piano key frequencies as a reference, you can create some music.
However, video game sounds are not quite the same thing as music (at least to most ears). Certainly, they can use the same frequencies as music notes, but they commonly use other frequencies to communicate the desired sound effect. When I am creating my own arcade sound there are three things that I personally try to keep in mind:
- 1. Keep the sounds short and sweet — unless you are going to put in some extra work to implement some non-blocking functions or not be concerned about it, playing a sound on a microcontroller will effectively pause/stop your game for as long as it is playing. This can rapidly become very obvious (and annoying) if you have a sound for changing directions on a snake game or when you fire a missile.
- 2. Change the frequency of the sound in steps — this helps keep your sounds both short and sweet and more easily shows (well, sounds) like a progression in pitch has occurred. As long as the length of time spent on each pitch isn’t too long, it will not sound too choppy, even if you are jumping 30-50 Hz at a time.
- 3. Know what it is going to sound like — I realize that this is both obvious and vague at the same time, but what I’m trying to get at is the idea of knowing what a stereotypical sound is supposed to sound like. If you are creating a sound for when you fire a shot of some sort, it is going to start off higher in pitch (faster frequency) and then quickly drop off in pitch to simulate the shot going farther away from the source. An explosion noise is typically a lot of “white noise” so a random set of frequencies (decided by the user or via a circuit) would suffice to give an appropriate 8-bit video game explosion.
If you happen to create a sound and after playing it using a function like the one provided in the picture below decide that it doesn’t sound “quite right”, try making some of the longer tones match the frequency of a music note. These sound a lot more familiar to people and so sound less “off”. If you have a particular sound effect in mind such as the “wah, wah, wah, wahwawawawa” noise, but have no idea what frequencies or notes it uses (much like I didn’t know), you can always try searching for the appropriate notes on the internet to find some potential options as suggested in this forum post.
You can recreate the game for yourself with a chipKIT uC32 microcontroller, a WS2812 LED strip, a small piezoelectric speaker, and this code. You can also learn some more about it on Instructables. Try it out and let us know what you think!