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

You simply pass the filename and the standard DirectSound control flags to the function, such as `DSBCAPS_CTRLDEFAULT` or whatever. After that, here’s what happens:

  1. The function opens up the `.WAV` file from the disk and extracts the important information about it.
  2. The function proceeds to create a DirectSound buffer and fills it in.
  3. The function stores the information in an open slot in the `sound_fx[]` array and returns the index, which I refer to as the ID of the sound.
  4. Finally, the rest of your API will use the ID number to refer to the sound, and you can do whatever you want with it, such as play it. Here’s an example:

    // load the sound

    int id = DSound_Load_WAV(“test.wav”);

    // manually play the sound buffer, later we will wrapper this

    sound_fx[id].lpdsbuffer->Play(0,0,DSBPLAY_LOOPING);

Make sure to check out `DEMO10_3.CPP` on the disk. (Remember to link with `DSOUND.LIB`, and `WINMM.LIB`.) It’s a complete demo of DirectSound and the `Dsound_Load_WAV()` function. In addition, the program lets you manipulate the sound in real-time with scrollbars, so not only is it cool, but you can see how to add scrollbar controls to your applications!

Of course, I’m going to take all this DirectSound stuff and show you the complete library (`T3DLIB3.CPP|H`), but first let’s take a look at DirectMusic.

### DirectMusic: The Great Experiment

DirectMusic is one of the most exciting components of DirectX. As I said before, writing digital sound software is hard, but writing software that plays MIDI files is gnarly! DirectMusic plays MIDI files, and it does a whole lot more. Here’s a list of its capabilities:

  • Supports DLS instruments (downloadable sounds). This means that when you play a MIDI file using DirectMusic, it will always sound the same no matter what kind of hardware you have.
  • Supports on-the-fly composition of music using the Interactive Music Engine. DirectMusic allows you to set up templates, personalities, and variations of mood for your songs. Then DirectMusic will take your song data and rewrite the music in real-time and generate more music!
  • Supports an unlimited number of MIDI channels, limited only by the processing power of your PC. Normal MIDI supports 16 channels, or 16 individual sounds, at once. There are 65,536 channel groups under DirectMusic, so you have almost unlimited tracks that can be played at the same time.
  • Uses hardware acceleration if available, but the Microsoft software synthesizer is default and sounds as good as wave table or wave guide synthesis.

The only bad news about DirectMusic is that it is as complex as Direct3D! I have read the online documents for it (around 500 pages), and I can tell you one thing: They didn’t have simplicity in mind, but they did have power in mind. Luckily, all you want to do is play a darn MIDI file, so I’m going to show you exactly how to do that and create an API around DirectMusic so you can load and play MIDI files.

### DirectMusic Architecture

DirectMusic is rather large, so I’m not going to go into any detail about it. It’s a topic for an entire book. However, I’m going to talk about the interfaces that you’re going to work with. Take a look at Figure 10.12 for DirectMusic’s main interfaces.

Figure 10.12 The main interfaces of DirectMusic.

The descriptions of these interfaces are as follows:

  IDirectMusic—This is the main interface of DirectMusic, but unlike DirectDraw and DirectSound, you don’t necessarily need it to use DirectMusic. It is created by default and hidden away when you create a DirectMusic performance object—thank God.
  IDirectMusicPerformance—This is the main interface as far as you’re concerned. The performance object controls and manipulates the playback of all musical data. In addition, it creates an IDirectMusic object when it is created.
  IDirectMusicLoader—This is used to load all data, including MIDI, DLS, and so forth. You use this to load your MIDI files from disk. So you have a MIDI loader—what a relief!
  IDirectMusicSegment—This represents a chunk of musical data; each MIDI file you load will be represented by this interface.
  IDirectMusicSegmentState—This is linked to a segment, but it’s related to the current state of the segment rather than the data.
  IDirectMusicPort—This is where the digital data representing your MIDI music is streamed out to. In most cases it will be the Microsoft Software Synthesizer, but you can always enumerate other possible ports that are hardware-accelerated.

Generally speaking, DirectMusic is a MIDI-to-digital real-time converter with DSP (Digital Signal Processing) abilities. As I mentioned in the DirectSound discussion on MIDI, the problem with MIDI is that it can sound different from one machine to the next based on the hardware and the instrument patches. DirectMusic gets around this by using pure digital samples of the instruments in the form of DLS files. Therefore, whenever you make a song, you can use the default DLS file or create your own instrument file. The catch is that the instruments are digital in nature and come with your music. Digital sound always plays the same through a D/A, so the music always sounds the same. Take a look at Figure 10.13 to see this.

Figure 10.13 DirectMusic relies on digital samples rather than synthesis.

> Note: The default DLS instruments that are available on every machine loaded with DirectMusic are the Roland GM/GS (General MIDI). They sound great, but you can’t modify them in any way—Roland doesn’t want you making them look bad!

You’re probably going “What the heck?” right about now. I know, I know, it always seems that everything is more complicated than it needs to be. But complexity is good because it allows future technology and innovation to fit in. That is the basis of DirectMusic.

### Starting Up DirectMusic

DirectMusic is the first component of DirectX that is totally pure COM (Component Object Model), meaning that there aren’t any helper functions in an import library to create the COM objects for you. Alas, you must create the COM objects yourself with calls to the COM library. So the only files any application needs are the DirectMusic header files. There isn’t any import `.LIB` file. The header files are

    dmksctrl.h

    dmusici.h

    dmusicc.h

    dmusicf.h

Just make sure to include these in your applications, and COM will take care of the rest. Let’s take a look at the entire sequence.

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.