DirectX SDK

Step 3: Play the MIDI File

[C++]

This topic pertains only to DirectX for Visual Basic. See DirectMusic C/C++ Tutorials.

[Visual Basic]

Now that you have a DirectMusicSegment object representing the MIDI file, you need to do a little more preparation before you can play it.

First, let the segment know that it represents a standard MIDI file. Although the segment can be played without this step, certain details of playback might not be handled properly. The following code performs this step for files with the conventional MIDI file extension:

If StrConv(Right(FileName, 4), vbLowerCase) = ".mid" Then
    Call objSeg.SetStandardMidiFile 
End If

Second, ensure that the DLS data for the instruments is downloaded to the port. The simplest way to do this is to turn on automatic downloading for the entire performance, as might be done in the setup code, as follows:

Call gObjDMPerformance.SetMasterAutoDownload(True)

The disadvantage of this method is that the instruments are unloaded from the port as soon as the segment stops, and must then be downloaded again. If your application is playing different segments in rapid succession, this behavior might cause unacceptable delays. For greater control, you can download the instruments each time a segment is about to be played. The instruments remain on the port until you choose to unload them, or until the performance is closed down. Any redundant downloads are ignored.

Call objSeg.Download(gObjDMPerformance)

Now, the segment can be played:

Call gObjDMPerformance.PlaySegment(objSeg, 0, 0)

If you want to track the state of playback (for example, to be able to pause and resume), you can save the return value of DirectMusicPerformance.PlaySegment in a DirectMusicSegmentState object:

Dim objSegState as DirectMusicSegmentState
Set objSegState = gObjDMPerformance.PlaySegment(gObjPrimarySeg, 0, 0)