Microsoft DirectX 9.0

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 Gm.dls file. However, you can use instruments from any DLS collection when playing a MIDI file.

First, load the collection and retrieve a pointer to the IDirectMusicCollection8 interface.

The following example function loads a collection by file name:

HRESULT LoadCollectionByName(
    IDirectMusicLoader8 *pILoader, 
    char *pszFileName, 
    IDirectMusicCollection8 **ppICollection)
  objDesc.dwSize = sizeof(DMUS_OBJECTDESC);
  objDesc.guidClass = CLSID_DirectMusicCollection;  
  objDesc.dwValidData = DMUS_OBJ_CLASS 
  hr = pILoader->GetObject(&objDesc, 
    (void **) ppICollection);
  return hr;

Next, you must associate the DLS data with the segment by calling IDirectMusicSegment8::SetParam, as shown in the following example:

HRESULT ConnectCollection(IDirectMusicSegment8* pSegment, 
                          IDirectMusicCollection8* pCollection)
  HRESULT hr = pSegment->SetParam(GUID_ConnectToDLSCollection,
      0xFFFFFFFF, DMUS_SEG_ALLTRACKS, 0, (void*)pCollection);
  return hr;

Finally, download the instruments in the collection to the performance or audiopath by calling IDirectMusicSegment8::Download.

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 continue to be played by the appropriate instruments in the GM collection.

See Also