Tricks of the Windows Game Programming Gurus:Sounding Off with DirectSound and DirectMusic

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

## Chapter 10
Sounding Off with DirectSound and DirectMusic

Historically, creating sound and music on the PC has been a nightmare. However, with the advent of DirectSound and DirectMusic, it’s all too easy. This chapter will cover the following topics:

  • Fundamentals of sound
  • Digital sound versus synthesized sound
  • Sound hardware
  • DirectSound API
  • Sound file formats
  • DirectMusic API
  • Adding sound support to your library

### Sound Programming on the PC

Sound programming is one of those things that always gets put off until the end. Writing a sound system is difficult because not only do you have to understand sound and music, but you have to make sure the sound system works on every single sound card. Here lies the problem. In the past, most game programmers used a third-party sound library such as the Miles Sound System, Diamondware Sound Toolkit, or something similar. Each system has its pros and cons, but the biggest problem is price. A sound library that works for DOS and Windows can cost thousands of dollars.

You don’t have to worry about DOS anymore, but you do have to worry about Windows. It’s true that Windows has sound and multimedia support, but it was never designed to have the ultra-high performance needed for a real-time video game. Thankfully, DirectSound and DirectMusic solve all these problems and more. Not only are DirectSound and DirectMusic free, but they are extremely high performance, have support for a million different sound cards, and have extensions to do as little or as much as you need.

For example, DirectSound has 3D support under DirectSound3D, and DirectMusic can do a whole lot more than play MIDI files. DirectMusic is a new real-time music composing and playback technology based on DLS (Downloadable Sounds) data. This means that not only will music sound the same on every single sound card, but DirectMusic can create music on-the-fly for your game based on preprogrammed templates, motifs, and personalities that you supply. Getting DirectMusic’s AI to compose for you takes a lot of work, but it may be worth it for games in which you want to change the mood based on the gameplay but don’t want to compose 10-20 different versions of each song yourself. With that in mind, let’s learn a little about sound.

### And Then There Was Sound...

Sound is one of those physical manifestations that has a circular definition. If you went out on the street and asked people what sound is, most of them would probably reply with, “Hmmm, stuff you hear with your ears, like sounds and noises.” (Go ahead, try it...) That’s true, but it still doesn’t get you closer to the actual physics of sound, and that’s important if you’re going to record, manipulate, and play sound.

Sound is a mechanical pressure wave emitted from a source, as shown in Figure 10.1. Sound can exist only in an environment such as our atmosphere, which is filled with gases such as nitrogen, oxygen, helium, etc. Sound can travel in water also, but it moves at a much higher velocity than in air because the medium’s increased density makes it more conductive. Sort of. Close enough. :)

Figure 10.1 A sound wave.

A sound wave is really the motion of molecules. When a speaker moves in and out, it moves the surrounding air in and out mechanically, that is, by contact with the molecules, and at some point the sound wave makes its way to your ears. However, because sound travels by a wave propagating through the air via mechanical collisions, it takes time to get to you. That’s why sound travels so slowly, relatively speaking. You can see something happen, such as a car crash, and not hear it for a second or two if it’s happening far enough away. This is because a mechanical wave in air, or sound wave, can only travel at about 600 MPH or 344 m/s (meters per second), more or less depending on the density and temperature of the air. Table 10.1 lists the velocities of sound in air, seawater, and steel, for average temperatures.

Table 10.1 Velocity of Sound in Various Materials

Material/Medium | Approximate Velocity of Sound
--- | ---

Air | 344 m/s
Seawater | 1,478 m/s
Steel | 5,064 m/s

Looking at Table 10.1, you can see why sonar works so well underwater but sucks in air (it’s too slow, for one thing). A sonar pulse, or ping, travels underwater at 1,478 m/s or, roughly, 14.78 m/s × 3.2 ft/m × 1 mi/5280 ft × 3,600 seconds/1 hour = 3,224.7 miles per hour! This, compared to the average of 750 miles per hour for sound in air, should tell you why sonar scans are almost instantaneous for objects that are moving underwater and within reasonable distance.

> Tip: If you’re interested, the velocity of sound c (not to be confused with C the note, or c the speed of light) is equal to the frequency × wavelength, or f*λ. In addition, the velocity can be computed based on factors like tension and density of the medium with this equation:

> c = sqrt(tension factor/density factor)

> where the tension and density are context-sensitive and only a loose starting point. In real life, there are a number of versions of this equation for gases, solids, and liquids.

Moving on, sound is a mechanical wave that travels through air at a constant velocity—the speed of sound. There are two parameters a traveling sound wave can have: amplitude and frequency. The amplitude of the sound is how much air volume is moved. A large speaker (or someone with a big mouth) moves a lot of air, so the sounds are stronger or more intense. The frequency of the sound is how many complete waves or cycles per second are emanating from the source, and is measured in hertz, or Hz. Most humans can hear in the range of 20-20,000Hz.

Furthermore, the average male has a voice that ranges from 20-2,000Hz, while a female voice ranges from 70-3,000Hz. Men have more bass, and women have more treble. Figure 10.2 shows the amplitude and frequency of some standard waveforms.

Figure 10.2 Various waveforms.

A waveform can be thought of as the shape of a sound’s amplitude changes. Some sounds change smoothly, while other sounds rise up and then sharply fall off. Even if two sounds have the same amplitude and frequency, their particular shapes will make them sound different to us.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.