DirectX 9 Audio Exposed: Interactive Audio Development

We already learned everything that we needed to know about loading and downloading a Segment from the previous two chapters. But there are a ton of options to explore in how we actually play, stop, and control a running instance of a Segment. For that, we look at the SegmentState object and the PlaySegmentEx() and StopEx() calls.

SegmentState

One very important feature of DirectMusic is the ability to play the same Segment multiple times concurrently. This is useful for sound effects. For example, you might want to use the same car engine sound on several cars that are driving at the same time. This is also very important with musical fragments when the score is built from multiple Segments.

If the only way to adjust a parameter of the playing Segment or stop its playback involved applying the operation to the original Segment object, things would be very bad since that would have to represent all instances of the playing Segment. We need something to represent each instance of the playing Segment. DirectMusic provides such a beast, called the SegmentState. You can request one of these when you play the Segment and then use it to specifically stop the Segment later, find out if it is still playing, and even access some of its control parameters. It is represented by the IDirectMusicSegmentState interface. We use this to stop a playing Segment in our programming example.

PlaySegmentEx()

Let's take a close look at PlaySegmentEx(), the method you call to actually play a Segment. We skip PlaySegment(), which is an older version from DirectX 7.0, as it is pretty much the same as PlaySegmentEx() with fewer options.

HRESULT hr = pPerformance->PlaySegmentEx( IUnknown * pSource, // Segment to play. WCHAR * pwzSegmentName, // Unused feature. IUnknown * pTransition, // Optional transition Segment. DWORD dwFlags, // Control flags. __int64 i64StartTime, // Optional start time. IDirectMusicSegmentState** ppSegmentState, // Optional Segment state to track // playing Segment. IUnknown * pFrom, // Optional Segment or AudioPath that stops when this starts. IUnknown * pAudioPath // AudioPath to play this Segment on. );

Typically, the three most important parameters are pSource, dwFlags, and pAudioPath. Pass the Segment you want to play to pSource, specify how you want it played with dwFlags, and determine which AudioPath it plays on with pAudioPath. (That is optional, too. You can pass NULL for the default AudioPath.)

Let's look at all the parameters in detail.

StopEx()

Often, you need to stop the Segment at some point. Again, there are two methods — the older Stop() and the newer StopEx(). Since StopEx() has all the functionality of Stop(), we only look at StopEx().

You have four choices for what you stop:

HRESULT hr = m_pPerformance->StopEx( IUnknown *pObjectToStop, // Segment, SegState, or AudioPath to stop. __int64 i64StopTime, // Optional stop time. DWORD dwFlags // Control flags. );

Категории