Microsoft DirectX 9.0

Step 1: Initialize

The following instructions are needed for any application that uses the DirectMusic API. Including Dmusici.h also causes the other necessary header files for DirectMusic and DirectSound to be included.

#define INITGUID
#include <dmusici.h>

The tutorial uses three interface pointers, which are declared as follows:

IDirectMusicLoader8*  g_pLoader           = NULL;
IDirectMusicPerformance8* g_pPerformance  = NULL;
IDirectMusicSegment8*   g_pSegment        = NULL;

All the code in this simple application is included in the WinMain function. The application has no main window, so it can proceed straight to the creation of COM and two objects: the loader and the performance:.

INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, 
  LPSTR pCmdLine, INT nCmdShow )
{
  CoInitialize(NULL);
  
  CoCreateInstance(CLSID_DirectMusicLoader, NULL, 
       CLSCTX_INPROC, IID_IDirectMusicLoader8,
       (void**)&g_pLoader);

  CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
       CLSCTX_INPROC, IID_IDirectMusicPerformance8,
       (void**)&g_pPerformance );

The next step is to initialize the performance and the synthesizer. The IDirectMusicPerformance8::InitAudio method performs the following tasks:

In the tutorial, the call to InitAudio is very simple:

  g_pPerformance->InitAudio( 
    NULL,              // IDirectMusic interface not needed.
    NULL,              // IDirectSound interface not needed.
    NULL,              // Window handle.
    DMUS_APATH_SHARED_STEREOPLUSREVERB,  // Default audiopath type.
    64,                // Number of performance channels.
    DMUS_AUDIOF_ALL,   // Features on synthesizer.
    NULL               // Audio parameters; use defaults.
  );

Next: Step 2: Load a File