Platform SDK: DirectX

Step 2: Create the Performance

[Visual Basic]

This tutorial pertains only to applications written in C++. See DirectMusic Visual Basic Tutorials.

[C++]

The central object of any DirectMusic application is the performance, which manages the playback of segments. It is created by using the COM CoCreateInstance function, as in the following sample function:

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

You can use this function to initialize a global performance pointer that will be used in later steps:

IDirectMusicPerformance* g_pPerf = CreatePerformance();
if (g_pPerf == NULL)
{
    // Failure -- performance not created
}
 

Once the performance has been created, you need to initialize it by calling the IDirectMusicPerformance::Init method. The method creates a DirectMusic object to manage the default port. Because you don't need to access the IDirectMusic methods directly, you don't need to retrieve a pointer to it, so you pass NULL as the first parameter to Init. You also pass NULL as the IDirectSound pointer and as the window handle, so that DirectMusic will create a DirectSound object and pass the current focus window to it when setting the cooperative level.

if (FAILED(g_pPerf->Init(NULL, NULL, NULL)))
{
    // Failure -- performance not initialized
};
 

Now you need to add a port to the performance. Calling the IDirectMusicPerformance::AddPort method with a NULL parameter automatically adds the default port (normally the Microsoft Software Synthesizer) with one channel group, and assigns PChannels 0-15 to the group's MIDI channels.

if (FAILED(pPerf->AddPort(NULL)))
{
    // Failure -- port not initialized
}

Next: Step 3: Create the Loader