LOOPING SOUNDS
It's often very handy to be able to loop a sound. Before DirectMusic, the traditional way to loop a sound was to start it Playing and then check it every so often to see if it was still Playing. If it wasn't, you started it again.The biggest problem with that method is that to loop without a delay, you have to check up on the sound very often, on the order of 10 times per second. With one sound that may not seem like a big deal but with several, you can end up devouring a lot of CPU power.There's an easier way—just call the SetRepeats method of the segment interface. You can pass the DMUS_SEG_REPEAT_INFINITE flag to SetRepeats to tell DirectMusic to loop a segment infinitely. Or, you can pass in an integer specifying the number of times to loop.If you need finer control over looping, you can use the SetLoopPoints method of the segment. This allows you to specify where inside the segment the looping should begin and end.However, this doesn't operate as you might expect. Check out Figure 5.1. Even if you use SetLoopPoints to specify a starting and ending position, the entire segment will still Play. DirectMusic will Play from the start of the segment to the end loop point you specified, loop back to the start point you specified, loop for the number of times set by SetRepeats, and finally Play from the end of the loop point you set to the end of the segment. In other words, SetLoopPoints only applies during the loop; the segment still Plays itself into and out of the loop.
Figure 5.1: DirectMusic uses several different parameters to control looping of a segment.
If you want to control the actual start and end points, you need to use the segment interface's SetStartPoint and SetLength methods. By default, the loader sets the start point at the beginning of the segment, and the length to the length of the entire segment.
Tip | Of course, there are also Get methods corresponding to all of the methods that set looping parameters. |
Ch5p3_Looping illustrates how to loop a sound.