Allowing DirectMusic to create and manage the DirectSound object works for applications that are not using DirectSound independently. However, if your application is using DirectSound to play wave data from a source other than the DirectMusic synthesizer, you have to ensure that the same IDirectSound interface is used by DirectMusic. You must create the DirectSound object first, then pass the interface pointer to DirectMusic. This can be done in the call to IDirectMusicPerformance::Init, as in the following example, where pPerf is the IDirectMusicPerformance and pDS is a pointer to IDirectSound:
pPerf->Init(NULL, pDS, NULL);
Note When an IDirectSound pointer is passed to IDirectMusicPerformance::Init, the third parameter, the window handle, is ignored. The application is responsible for setting the controlling window by calling IDirectSound::SetCooperativeLevel.
It is strongly recommended that you set the DSSCL_PRIORITY cooperative level for any DirectSound object to be used with DirectMusic. If you set the DSSCL_NORMAL cooperative level, DirectMusic cannot upgrade the primary buffer format.
If you create the DirectMusic object by using CoCreateInstance, rather than letting the performance do it for you, you must also explicitly link it to an existing IDirectSound interface. This is done by using the IDirectMusic::SetDirectSound method.
You can also use the IDirectMusicPort::SetDirectSound method to assign a different DirectSound object to a port. You would do this when different ports were on different audio devices, each represented by its own IDirectSound interface. The DirectSound object for a port cannot be changed once the port has been activated.
When a port that requires DirectSound is activated, it takes the first available IDirectSound interface pointer from the following list:
When DirectSound is in emulation mode, it has exclusive use of the audio device. A well-behaved application will release DirectSound whenever it loses the focus, in order to allow other applications to use the device. Typically this is done in response to a WM_ACTIVATE message.
If DirectMusic created the DirectSound object, it will automatically release the object when all ports are deactivated (and create a new one when the first port using DirectMusic is reactivated). However, if you created the DirectSound object yourself, you are responsible for releasing it. This can be done by using the IDirectMusic::SetDirectSound or IDirectMusicPort::SetDirectSound method, passing NULL as the pDirectSound parameter.