Playing a MIDI File with Custom Instruments

By default, when you play a MIDI file the instruments used are those in the Roland GM/GS Sound Set, contained in the file Gm.dls. However, you can play a MIDI file using instruments from any collection.

To do so, first load the collection as described under Loading a Collection, obtaining a pointer to the IDirectMusicCollection interface. Then call IDirectMusicSegment::SetParam on the MIDI segment to establish a connection between the segment and the collection.

The following sample shows how the connection is made:

/* Assume that pSegment was created from a MIDI file
   and that pLoadedCollection is a valid IDirectMusicCollection 
   pointer. */
 
HRESULT hr = pSegment->SetParam(GUID_ConnectToDLSCollection,
        0xFFFFFFFF, 0, 0,
        (void*)pLoadedCollection);
 

Note  When a custom collection is attached to a MIDI segment, the connection to the GM collection is not broken. For example, suppose you load a collection containing a single instrument that has a patch number of 12, and connect this to the segment. MIDI channels with any patch number other than 12 will continue to be played by the appropriate instruments in the GM collection.

As with any other collection, instruments to be used in playing a MIDI file must be downloaded before the segment is played, unless automatic downloading has been enabled. (For more information on automatic downloading, see Setting and Retrieving Global Parameters and Downloading and Unloading Bands.) If instruments are not being downloaded automatically, you need to download them by calling IDirectMusicSegment::SetParam, as in the following example:

/* pSegment is an IDirectMusicSegment pointer, and pPerformance 
   is a valid pointer to IDirectMusicPerformance. */
 
pSegment->SetParam(GUID_Download -1, 0, 0, (void *) pPerformance);
 

For more information on downloading by using SetParam, see Setting and Retrieving Track Parameters.