Chapter 7. Audio Adrenaline

This is the final chapter on the 2D game that we have been working on. Although our Robo Racer 2D game is almost complete, there is one element that we have yet to include to make it a complete game. Unless you like silent movies, you have probably noticed that we don't have any audio in this game. Most games depend on audio, and ours is no exception. In this chapter, we will cover audio and a few other housekeeping items.

  • Audio formats: It is important to understand how audio is represented in computers and how it is used in games. We will discuss sample rates and bits and help you understand how audio works.
  • Audio engine: We need some kind of audio engine to integrate audio into our game. We will discuss FMOD, a very popular engine that allows you to easily integrate audio using C++.
  • SFX: Sound effects play a huge role in most games and, we will add sound effects to our game to bring it to life.
  • Music: Most games utilize some kind of music. Music is handled in a different way than sound effects, and you will learn the differences between the two.
  • Final housekeeping: On a final note, for our game, we have left the game shutdown for this chapter. We have not been good programmers in that we have not properly released the objects in our game. We will learn why it is important to do so, and how to do it.

Bits and bytes

Audio is inherently an analog experience. Sound is created as compressed waves travel through the air and interact with our ear drums. Until recently, the techniques used to reproduce audio were also strictly audio as well. For example, a microphone records sound similarly to how our ears do by capturing changes in air pressure and converting them to electrical impulses. Speakers do the reverse by converting the electrical signals back into waves of air pressure.

Computers, on the other hand, are digital. Computers convert audio samples into bits and bytes by taking samples of the audio. To keep it simple, let's consider a system where the current frequency of the sound wave (that is, how fast the wave is moving) is captured as a 16 bit (2 byte) number. It turns out that a 16 bit number can capture numbers in a range from 0 to 65,536. Each sample of the sound wave must be encoded as a number in this range. Also, as we actually capture two samples each time (for stereo sound), we need 4 bytes to capture each sample.

The next important factor is how often you sample the sound. The range of audio frequencies run roughly from 20 to 20,000 Hz (Hz = cycles per second). A very smart person named Nyquist figured out that we have to sample audio at twice the frequency to accurately capture the wave. This means that we have to capture at least 40,000 samples each second to accurately capture a sound. Conversely, we have to play the sound back at the same frequency. This is why audio on compact discs are sampled at 44,100 Hz.

You should be able to see by now that it is going to take a lot of disk space and a lot of memory to work with sound. A one minute piece of audio will take about 10 MB of storage! This means that the same audio would require 10 MB of memory if we were to load the entire audio file at once.

You may wonder how modern games function at all. The music scores of some games are measured in hours, not minutes. Similarly, there may be hundreds or even thousands of sound effects, not to mention voice, which is also recorded as audio.

A sound by any other name

There are many formats that audio files can be stored in. We will deal with two common formats that are used in games: WAV files and MP3 files. A WAV file stores the audio data in an uncompressed format.

Although WAV files can be used for all of your audio, they are typically used for sound effects. Sound effects are typically very short, often less than 1 second. This means that the size of the file is going to be relatively small because the audio file is very short.

While sound effects are often saved as WAV files, music, typically, is not. This is because the length of music tends to be much longer than the length of sound effects. Loading a music file into memory that is three-to-five minutes long would take an exorbitant amount of memory.

There are two main techniques that are used to deal with larger audio files. First, data compression can be used to make the audio files smaller. One of the most common audio formats that provides data compression is the MP3 format. Using mathematical trickery, MP3 files store the sound data in less space without sacrificing any sound quality.

The second technique that is used to handle large files is streaming. Instead of loading the entire sound file into memory, the file is sent a piece at a time as a continuous stream of data, which is then played in the game.

There are some limitations to streaming. First, the transfer of data from a hard drive or another storage device is much slower that the transfer of data from memory. Streamed audio can suffer from lag, which is the amount of time that it takes for a sound to play from the time that the sound was triggered to play in code.

Lag is more critical for sound effects than it is for music. This is because a particular sound effect often coincides with something that just happened in the game. It would be disconcerting if the sound of a bullet occurred a half second after the bullet was fired! Music, on the other hand, often starts and runs for several minutes. A small lag in the start of the music can often be overlooked.

Making noise

Going into a full-blown course on creating sounds and music is, of course, beyond the scope of this book. However, I did want to give you a few resources to get you started.

The first question you may ask is where to find sounds. There are literally thousands of sites on the Web that provide sounds and music that can be used in games. Many charge a fee, while a few offer free audio.

One thing to keep in mind is that royalty-free doesn't necessarily mean free. Royalty-free audio means that once you obtain a license to use the audio, you won't have to pay any additional fees to use the music.

So, here's my big tip. Every site that I have found charges a small fee for both sound effects and music. But there is one way that I have found to obtain sounds for free using the Unity Asset Store. Go to http://unity3d.com and install the free version of Unity. Once you have started Unity, perform the following steps:

  1. Create a new project by clicking Create New Project tab from the Unity Project Wizard. Click Browse and navigate to or create a folder to store your project in. Then click Select Folder.
  2. Once Unity loads the project, click Window and then Asset Store from the menu.
  3. When the Asset Store window appears, enter a relevant search term (for example, music or SFX) in the Search Asset Store text box and press Enter.
  4. Browse the results for free assets. Click on any listing for more details. If you find something that you like, click the Download link.
  5. Once Unity has downloaded the asset, the Importing Package screen will appear titled. Click the Import button.
  6. You can now exit Unity and navigate to the folder where you created the new project. Then navigate inside the Assets folder. From here, it depends on the structure of the package that you imported, but if you browse around, you should be able to locate the audio files.

    Tip

    In fact, we are using a musical piece titled Jolly Bot provided by Robson Cozendey (www.cozendey.com). We also found a great SFX package from.

  7. You can now copy the audio files into your project!

    Tip

    As you browse around for audio files, you will run across some files with the ogg extension. This is a common audio format similar to MP3. However, the engine that we will use does not support ogg files, so you will need to convert them to MP3 files. Audacity, which is described next, will allow you to convert audio files from one format to another.

You may find that you want to edit or mix your audio files. Or, you may need to convert your audio files from one format to another. The best free tool that I found to work with audio is Audacity, and you can download it at http://audacity.sourceforge.net/. Audacity is a full-featured audio mixer that will allow you to play, edit, and convert audio files.

Tip

To export files to the MP3 format, you will need a copy of LAME installed on your system. You can download LAME from http://lame.buanzo.org/#lamewindl.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.76.227