Share via


Play Motif Sample

The Play Motif sample demonstrates how a motif played as a secondary segment can be aligned to the rhythm of the primary segment in various ways.

Path

Source: (SDK root)\Samples\C++\DirectMusic\PlayMotif

Executable: (SDK root)\Samples\C++\DirectMusic\Bin

User's Guide

Play the default segment, or load another DirectMusic segment based on a style that contains motifs. Select one of the patterns in the list box and one of the Align Option buttons, and then click Play Motif. Note how the motif does not begin playing until an appropriate boundary in the primary segment has been reached.

Programming Notes

The Play Motif sample is very similar in form to the Play Audio Sample.

When loading the file, Play Motif performs the same steps as Play Audio, with the following additions in the LoadSegmentFile function:

  1. Loops through styles in the segment, searching it for motifs. It calls the CMusicSegment::GetStyle method in the sample framework, which in turn calls IDirectMusicSegment8::GetParam, passing GUID_IDirectMusicStyle and an incrementing index to get each of the styles in turn. The method fails when there are no more styles.
  2. For each style, calls IDirectMusicStyle8::EnumMotif, passing an incrementing motif index. This call retrieves the motif name at that index. When the call returns S_FALSE, there are no more motifs in the style.
  3. Passes the motif name to IDirectMusicStyle8::GetMotif to get an IDirectMusicSegment8 interface pointer to the motif, and stores this as data associated with the item in the list box.

When Play Motif is clicked, the OnPlayMotif function performs the following tasks:

  1. Retrieves the desired alignment option from the interface.
  2. Gets the selected motif from the listbox, along with its MOTIF_NODE item data. The MOTIF_NODE structure keeps a count of the number of plays currently occurring, as well as a pointer to the IDirectMusicSegment interface of the motif.
  3. Calls IDirectMusicPerformance8::PlaySegment, passing the motif's IDirectMusicSegment and flags, including DMUS_SEGF_SECONDARY and any alignment option.

DirectMusic notifications are handled much as in Play Audio, but this application also takes note of any motif starting or stopping, and updates the play count in the MOTIF_NODE structure. If the play count is greater than zero, then it updates the user interface to show that the motif is playing.

See Also

© 2004 Microsoft Corporation. All rights reserved.