Storing Sound Data


Both music and sound effects for games are stored in two basic types of files. The first is called digitized sound. The second way to store sound is in MIDI files. This section examines both types of sound files.

Digitized Sound

Digitized sound or music is recorded on the computer in a digital format. Digitized sound always sounds the same every time you play it back. That's what you want for your games, but it comes at a cost.

Digitized sound files contain a lot of information so they tend to be very large. Most people who record sounds and music use special compression techniques to make the files smaller. So when you record digitized sound, you can choose between compressed and uncompressed formats. Windows WAV files are an example of an uncompressed file format. WAV files result in high-quality sound, but take a lot of room on disks.

On the other hand, MP3 files are compressed. They take up much less room than WAV files. However, the sound quality is not quite as good. Nevertheless, the sound quality of MP3 files is good enough for the vast majority of game programmers. Almost every game stores its sound effects and music in MP3 files.

Note

LlamaWorks2D can play music off CDs in just the same way it plays music from hard drives.


There is a way to get high-quality sound without requiring huge amounts of space on the player's hard drive: Your game can stream CD-quality sound from the game's CD or DVD.

Factoid

CD-quality music is also called Redbook audio.


When your game streams sound off a CD or DVD, you may not have to use as much compression for your sound files as you otherwise would. As a result, you can get higher-quality music. Streaming sound also takes very little of the computer's memory.

The problem with streaming sound is that getting music off a CD or DVD is much slower than playing sounds stored in memory. It's so slow, in fact, that it is not possible to use streaming sound for sound effects. However, it's often used for background music.

Midi Files

In addition to digitized sound, another basic form for storing music exists. Instead of storing a recording of the sounds themselves, you can store a set of commands that computers can use to reproduce the sounds. The most common file format of this type is the Musical Instrument Digital Interface (MIDI) file.

Because you store only the commands needed to reproduce sound effects or music in MIDI files, they tend to be much smaller than any digitized sound format. There is one problem with MIDI files, however. The sound that is reproduced depends entirely on the quality and the settings of the sound card in the player's computer. That means your music and sound effects can sound different on different people's computers. As a result, most game programmers don't use MIDI files.

Using MIDI sound has the added advantage of dynamic music generation. If your game generates music dynamically, it actually composes music as your game runs. Games that employ dynamic sound generation follow a set of rules for automatically composing music. These rules dictate the style, tempo, key, and other features of the music.

Factoid

It is not uncommon for game developers to use MIDI files to produce music and sound effects with their own high-quality equipment. They then record the music and sound effects into a digital format such as MP3 for distribution with their games.


With dynamic music generation, games can play faster, more intense music as the action picks up and slower, more relaxed music as the game's pace slows down. For instance, in a two-player game in which the players battle each other, you as the game programmer don't know in advance when the action will be fast-paced. However, the game can figure it out from the number of shots being fired, the number of times players are actually hit, and the speed at which the characters are running. At those times, it can use dynamic music generation to compose and play fast, intense music. When no one is firing any weapons and everyone's sneaking slowly around, the game can play slow but scary music to indicate impending doom.

Note

Microsoft's DirectX supports dynamic music generation that sounds nearly the same on all computers. If you want to incorporate dynamic music generation into your games, I recommend that you learn and use DirectX after you become a proficient game programmer.


Sound Storage Techniques Compared

With all of these different method of doing audio in games, the question programmers often ask is "Which technique should I use?" As you might expect, the answer depends on your game. Table 8.1 lists the options you have.

Table 8.1. Comparing Sound Storage Techniques

Storage Method

Pros

Cons

Uncompressed digitized audio in memory

High-quality sound, fast access, always sounds the same on all computers

Takes huge amounts of memory

Compressed digitized audio in memory

Good-quality sound, fast access, always sounds the same on all computers

Takes medium to large amounts of memory

Streaming digitized audio

High-quality sound, requires little memory, always sounds the same on all computers

Very slow access

MIDI audio

Takes small amounts of disk space and memory

Does not sound the same on all computers


You can see from Table 8.1 that if you're creating a small, arcade-style game, then it's probably best to use compressed digitized sound for all music and sound effects and put them in memory. For larger games where the player encounters many different situations, you'll need a wide variety of music and sound effects. In such a case, it may be better to use compressed digitized music streamed from the CD or DVD for background music and compressed digitized sound effects stored in memory.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net