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
### Summary In this chapter, we discussed several techniques for playing both sound effects and music. We examined various DirectSound methods for performing digitized audio playback, including changing the format of the primary buffer as well as duplicating sound buffers. We also covered the steps required to output MIDI music as well as more superior techniques for playing audio tracks from a CD. When coding for sound or music output using DirectSound or Win32 multimedia API functions, it is important to keep these points in mind: • Sound effects are very important for game enjoyment, as they not only enhance the believability of the gaming environment but also provide the player with audible clues that indicate changes in the game state. • The PlaySound function is incredibly powerful for what it does. It can be used to play sounds from a file, from a resource, or from memory. While it may be adequate under some circumstances, it cannot play multiple sounds simultaneously. • DirectSound offers many features above and beyond what is available with Win32 API functions. Hardware acceleration is automatically utilized when available. A virtually unlimited number of sounds can be mixed and played simultaneously, and several special effects can be applied to the output, such as panning, volume control, and frequency adjustment. • Similar to DirectDraw, DirectSound programming consists of creating a DirectSound object and several sound buffers. The DirectSound object itself represents the developer’s direct interface to the audio hardware. Through the IDirectSound interface, the application can query the audio hardware’s capabilities, create sound buffers, and control other aspects of the hardware itself. • DirectSound buffers are circular in nature. This is important when dealing with streaming buffers, as accessing buffer memory may require extra steps when starting near the end of the buffer. • When DirectSound creates a secondary sound buffer, it automatically tries to locate the buffer in RAM on the sound card, if available. If there are no hardware buffers available, the sound is stored in system memory. Sound buffers located in hardware have the shortest path to the primary sound buffer, and thus are appropriately suited for short sounds that are needed quickly and will be repeated often. • Secondary sound buffers come in two flavors: static and streaming. Static buffers are used for short sounds that can be placed into memory in their entirety. These are typically short, often repeated or looping sounds. Streaming buffers are used for large sounds that cannot fit into a reasonably sized block of memory or for sounds that change often, and must by copied into the buffer in pieces as it is playing. By default, DirectSound tries to create a streaming buffer. • Sound buffers should be created in order of importance. This will ensure that the sounds used most often will enjoy the greatest performance. However, to optimize performance, the developer should dictate the type of buffer when one is created, and where it will be placed. In general, static buffers should be used for short, often repeated or looping sounds, and should be placed in hardware memory. Streaming buffers should be placed in system memory. The exception to this rule is when a sound buffer will be duplicated. Duplicating a hardware buffer requires using hardware resources that may be needed elsewhere. Thus, it is better to place static buffers that will be duplicated in system memory. • When a sound buffer is created, the developer can determine if the sound will continue to be heard when another application receives focus. By default, sounds will not be heard when the application loses focus (although they continue to play silently). • DirectSound is optimized for 16-bits-per-sample sounds. Therefore, changing the primary sound buffer format for 16-bit sounds can further reduce playback latency and reduce the load on the CPU when processing sound data. • The most complex step in initializing a secondary sound buffer is copying the data from a WAV file into the sound buffer itself. Unfortunately, DirectSound does not come with any built-in methods for retrieving audio data from a file or a resource. However, the DSUtil.pas, DSWaveFiles.pas, and DSWaveResources.pas units contain translations from DirectX SDK sound utility functions to copy audio data from a WAV file or a resource into a DirectSound buffer. • Music, like sound effects, adds depth to a game, giving it a feeling of completeness. Even more than sound effects, music adds mood to a game, and when used effectively can adjust the user’s frame of mind as effectively as a movie sound track. • The Windows Media Control Interface features the MCISendCommand function that allows us to easily play both MIDI music and Red Book Audio from CDs. However, DirectX 6.1 and above feature a new DirectX component called DirectMusic that may revolutionize the use of MIDI. • The MCISendCommand function is asynchronous by nature, and will return immediately unless the MCI_WAIT flag was specified. In this event, you can usually specify the MCI_NOTIFY flag to indicate that the function should notify the application when it has completed. This is accomplished by sending the application an MM_MCINOTIFY message. It is a simple matter to define a handler for this message. • MIDI, or Musical Instrument Digital Interface, is a format for describing musical sound, much the same as the WAV format describes digitized sound. It is an effective means of providing music within a small footprint, and both software for creating MIDI files as well as freeware or shareware MIDI songs are readily available. • MIDI musical sound quality is less than perfect. Many games are circumventing this limitation by playing their musical scores directly off of the CD on which the game is distributed. This offers the highest possible sound quality, although the amount of music available to the game will be limited by the number of scores and their length, as CD audio requires much more storage space than MIDI files. • Music data is stored on the CD in a format known as Red Book Audio. This format provides for stereo music at the highest quality, and is the format used by the professional music industry to record music onto conventional audio CDs. The end user’s machine must be set up correctly before it can play CD audio music. This involves installing a small wire that runs directly from the CD to the audio output hardware, and while most new machines have this installed automatically, it may not always be available.
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.