ALTERNATIVE WAYS TO PLAY SOUNDS
Up until now, you've focused mainly on how to use the DirectX APIs to play sounds. However, this isn't the only way, and sometimes it isn't the best way. Included in this section are a couple of alternative ways to play sounds.
DirectSound
One alternative to using DirectMusic is to use the low-level DirectSound interfaces. Most of the time, if you've already committed to using DirectX, it doesn't make any sense to not use the higher-level DirectMusic Loader interface. My advice is to use DirectSound only if you don't want to use DirectX 8 (for example, you want your game to run under Windows NT 4.0).Here's how to load and play a sound using nothing but the DirectSound interfaces.
Initialize DirectSound. How exactly you do this depends on which version of DirectSound you're using, but in general the process is similar to what you learned last chapter.
Create a secondary buffer and load your wave file into it. This is the tricky part because it involves parsing the wave file format manually. Consider using code provided either by the DirectX sample programs or the Internet.
Play your sound by calling the Play method of the secondary buffer you created.
Delete the secondary buffer when you're done.
Tip | There are several DirectX sample programs that demonstrate how to play waves using the DirectSound interfaces. In the DirectX 8 SDK, there are a couple of files (dsutil.cpp and ![]() |
The Ch3p2_DirectSound sample program illustrates how to play back a sound using just the DirectSound interfaces.
Windows API Waveform Audio
If you don't want to use DirectX at all, there's another option for getting wave files out the speakers: use the Win32 API—specifically, the Waveform Audio functions. The Waveform Audio segment of the SDK is documented inside MSDN, at Platform SDK\Graphics and Multimedia Services\Multimedia Audio\Waveform Audio.In particular, pay attention to the Win32 API function, PlaySound, which can play a sound from disk, on a resource, or in memory. The Ch3p3_WaveformAudio sample program illustrates how to play a sound from memory using this function.
Tip | If you want to use PlaySound, you must link with the winmm.lib library. Be sure to add "winmm.lib" to the list of libraries on the linker tab in project settings or you'll get link errors. |
Windows MCI
If you want a little more than just a PlaySound function, but still don't want to use DirectX, you can play a sound using the Win32 API's Media Control Interface (MCI). The MCI was designed to communicate with a wide variety of multimedia devices, wave audio playback devices, MIDI sequencers, CD audio playback devices, and digital video playback devices.MCI is a good interface that gives you more control over playing sounds. In particular, you can play a segment of a wave file and specify your in and out points in time. That is, you can say, "play this sound from 2.54 seconds to 3.81 seconds."You'll learn how to use MCI to play CD data later in the book. There's also more information about it in MSDN, at Platform SDK\Graphics and Multimedia Services\Multimedia Audio\MCI.