Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
78 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Initialization
 Loading And
 Playing Music


 Source code
 Printable version

 


Loading And Playing Music

Now comes the fun part. Although it may seem a bit daunting at first, loading a MIDI file via a DirectMusicLoader is much simpler than dealing with the Win32 multi-media functions. I’m not going to detail the code here. Instead, I’ll just give you the gist of what needs to be done. You can look at the source code that accompanies this article and read the comments to learn what the code does.

Loading a MIDI File

The first thing to do is to tell the loader where to look for the files and what type of files to look for. This is accomplished by a call to IDirectMusicLoader::SetSearchDirectory. Next, a DMUS_OBJDESC structure needs to be setup with values appropriate for MIDI files (again, all of this is in the example source). From that point, you just call IDirectMusicLoader::GetObject and pass a pointer to an IDirectMusicSegment object as one of the parameters. Finally, make a couple of calls to the Segment in order to set some parameters and "download" the instruments. Then you’re all done.

So, there’s really not much to it at all. I wish I could go into more detail with the source here, but space is sparse. So please make sure you read the comments in the example source thoroughly.

Playing Loaded Music

Playing a MIDI you have already loaded is quite simple. You do so by making a call to IDirectMusicPerformance::PlaySegment. Just pass a pointer to the IDirectMusicSgement you wish to play. There are several flags you can pass along, as well as a time to start playing. You usually will only want to use these flags if you’re dealing with music you created in DirectMusicProducer, which is a topic for another article. For now, just pass along 0’s.

The final parameter to PlaySegment is a pointer to an IDirectMusicSegmentState. This is important only if you need to track information about the Segment. For our purposes, it can just be NULL.

performance->PlaySegment(Segment_To_Play,0,0,NULL);

There are similar functions for stopping a Segment and to determine if a segment is playing. You can see examples of these functions in the accompanying source.

A Final Step

Okay. You’ve got everything loaded, your music is playing. Now you can quit the program and carry on, right? Wrong. The DirectMusic system must be shutdown.

Just like all DirectX objects, the DirectMusic components each have a Release method that should be called before exiting your application. You need to release the DirectMusicLoader you used, as well as the performance. However, the performance requires that you call IDirectMusicPerformance::CloseDown before releasing it.

Before you release the segments, you must first set a segment parameter called GUID_Unload on each segment you loaded. Again, you’ll have to peek at the source to see all of this in action, but it’s pretty self-explanatory.

Conclusion

Not all that difficult, right? You will find that after you have learned the basics, more and more of the DirectMusic API will begin to make sense to you. Be sure to study the example source carefully and you will be well on your way. If you wish to compile it, there are no additional libs you need to link to (other than the default libs MSVC sets up for you – don’t know about other compilers). Just create a new Win32 Application and compile.

Please report any errors you find in the article or the code to me at mdat71@thrunet.com.

That’s all folks. See you in Part Two.