Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
## CHAPTER 9 Sound and Music This chapter covers the following topics: • Playing sound effects using multimedia functions and DirectSound • Playing MIDI music using MCI commands • Playing Red Book (CD) audio using MCI commands Most people discuss games in terms of the technology used for the graphics engine. Hot first-person shooters are discussed in terms of colored lighting, interactive environments, the polygon count of characters, etc. Real-time strategy game discussions feature such terms as computer AI, realism of surface features (such as hills), tactical advantages of weapons, etc. Although rarely the topic of discussion, the importance of the third major piece of a game, sound and music, is not lost on game developers. Regardless of the technological accomplishment represented by the game’s graphics engine, no game is truly complete without some sort of sound effects to bring the environment to life, or a driving soundtrack to set the mood. Would Quake have been as good if you couldn’t hear a heavy-breathing mutant around the corner? Would X-Wing have given you the same thrill without the musical fanfare following a completed mission? Indeed, music and sound effects round out a game and add the polish that can make a good game great. In this chapter, we examine various techniques for incorporating sound effects and music into Delphi games. We will examine using DirectSound for manipulation and playback of sound effects, and we will take a look at regular Windows MCI commands for playing music in both MIDI and Red Book Audio formats. DirectX 6.1 and above features a DirectX component known as DirectMusic. DirectMusic could potentially revitalize the use of MIDI through its incredible features. It provides an interface for supporting downloadable instrument sounds as well as methods to change the playback of MIDI files on the fly. This can result in context-sensitive musical playback that, supposedly, may never be the same twice. Unfortunately, we were unable to cover DirectMusic in this volume, but look for several articles about its use appearing in trade magazines in the very near future. ### Sound Effects Sound effects are very important in a game, as they help to immerse the player in the environment depicted by the game’s playing area. Viewing an animation of a ship exploding is much more exciting when accompanied by a loud roar. Walking down a dark passageway is much more realistic when the hollow sounds of boot heels on damp stone is heard. Using sound effects in this manner can draw the user into the environment in ways that even the most advanced 3-D engine could never hope to accomplish. In addition to immersion and suspension of belief, sound effects can give audible clues to the user that indicate a change in the environment. For example, the hum of an engine could be changed to reflect the amount of damage it has taken, providing a correlation to the engine’s maximum speed. A low growl or heavy breathing sound could signal the presence of an unknown assailant, heightening the user’s tension level and excitement. Even the subtle sounds of clicks and whirs emitted when the game’s buttons and sliders are manipulated help make the environment all the more real to the user. The Windows Multimedia API contains a number of sound output functions. Some are very easy to implement but lack the control that might be required for some games. Most games will need to play several sound effects at once, such as the whine of an engine, laser bursts, and explosions. Playing several sound effects simultaneously is known as mixing, and unfortunately such functionality is not built in to the Windows Multimedia API.
| mixing: Combining several sounds in real time in such a way that they can be played simultaneously.
Low-level multimedia sound output functions could be used to implement mixing or even manipulate the sounds in real time to create special effects such as echo or reverb. However, such code is difficult to write at best, and just as difficult to debug. Let’s examine the easiest method to implement sound output under Windows, and then we’ll take a look at how DirectSound provides all the sound output power that a game will ever need. #### Simplistic Sound Effects Not every game requires the power of full-blown DirectSound implementation. Indeed, if your game uses few sound effects, or does not require the ability to play several sounds simultaneously, it is much easier to implement the sound output functionality built into the Windows API. By far, the easiest yet most flexible function available for playing sound effects is the PlaySound function. The PlaySound function can play audio files in the WAV format, from either a file on disk, a resource, or even an image of the sound in memory. It can play the sound asynchronously, allowing the application to continue processing, and it can play sounds in a continuous loop. This function would be perfect for almost any gaming need except for one major drawback: It cannot play more than one sound at a time. As stated above, low-level audio functions could be used to implement mixing, but this is difficult, and now obsolete thanks to DirectSound. The PlaySound function is defined as:
    function PlaySound(

    pszSound: PChar;             // the sound to be played

    hmod: HMODULE;               // a module handle

    fdwSound: DWORD              // flags controlling sound playback

    ): BOOL;                     // returns TRUE or FALSE
The first parameter indicates the sound to be played, and can be a filename, an alias, or a resource identifier as specified by the flags in the third parameter. If the first parameter is set to nil, any currently playing WAV is stopped. The second parameter contains the handle of the executable or DLL if the sound to be played is a resource; otherwise it should be set to zero. The final parameter is a series of flags that indicate how to interpret the first parameter as well as how the sound file should be played, such as in a loop.
| Note: You must include MMSystem in the Uses clause of any unit that will use the PlaySound function.
The following example demonstrates how to use the PlaySound function to play sound effects from various locations.
Previous | Table of Contents | Next

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 whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.