Step 1: Initialize

DirectMusic

Microsoft DirectX 9.0 SDK Update (Summer 2004)

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:

  • Creates a DirectMusic and a DirectSound object. In most cases you don't need an interface to those objects, and you can pass NULL in the first two parameters.
  • Associates an application window with the DirectSound object. Normally the handle of the main application window is passed as the third parameter, but the tutorial application doesn't have a window, so it passes NULL instead.
  • Sets up a default audiopath of a standard type. The tutorial requests a path of type DMUS_APATH_SHARED_STEREOPLUSREVERB, which is suitable for music.
  • Allocates a number of performance channels to the audiopath. WAV files require only a single performance channel, and MIDI files require up to 16. Segments created in DirectMusic Producer might need more. No harm is done by asking for extra channels.
  • Specifies capabilities and resources of the synthesizer. This can be done in one of two ways: by setting flags or by supplying a DMUS_AUDIOPARAMS structure with more detailed information. Most applications set the DMUS_AUDIOF_ALL flag and let DirectMusic create the synthesizer with default parameters.

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


© 2004 Microsoft Corporation. All rights reserved.