Creating the Performance

The manager of music playback is the performance object, which does most of the work of getting music from the source to the output buffer, including adding ports, assigning instruments to channels, downloading instrument data to the synthesizer, playing and stopping segments, dispatching messages, and managing tools and timing.

The following example creates a performance and obtains a pointer to the IDirectMusicPerformance interface:

IDirectMusicPerformance* pPerf;
 
if (FAILED(CoCreateInstance(
        CLSID_DirectMusicPerformance,
        NULL,
        CLSCTX_INPROC, 
        IID_IDirectMusicPerformance,
        (void**)&pPerf
    )))
{
    pPerf = NULL;
}
 

Once the performance is created, it must be initialized. An important part of initialization is the creation of a DirectMusic object. You can create DirectMusic by passing CLSID_DirectMusic to CoCreateInstance, and then passing the IDirectMusic interface pointer to IDirectMusicPerformance::Init, but it most cases it will be more convenient to have Init create DirectMusic. You can also choose whether or not to retrieve a pointer to the IDirectMusic interface, depending on how much control you need over ports and the master clock. If you intend to use only the default synthesizer and the default master clock, you probably don't need access to the methods of IDirectMusic; in this case, you would pass NULL to Init.

The following example initializes the performance, retrieves a pointer to IDirectMusic, and creates an IDirectSound interface initialized with the application window handle:

IDirectMusic* pDirectMusic; 
 
if (SUCCEEDED(pPerf->Init(&pDirectMusic,
        NULL,     // Create DirectSound object
        hWnd      // Application window handle
        )))
{
    // Performance initialized
}