Microsoft DirectX 9.0

Pausing Segments

To pause a segment, you must ascertain the current play position before stopping the segment. The following example function returns the current play position in music time.

MUSIC_TIME GetTimeOffset(const MUSIC_TIME mtNow,           // From GetTime
                         const MUSIC_TIME mtStartTime,     // From GetStartTime
                         const MUSIC_TIME mtStartPoint,    // From GetStartPoint
                         const MUSIC_TIME mtLoopStart,     // From GetLoopPoints
                         const MUSIC_TIME mtLoopEnd,       // From GetLoopPoints
                         const MUSIC_TIME mtLength,        // From GetLength
                         const DWORD dwLoopRepeats)        // From GetRepeats
{
    // Convert mtNow from absolute time to an offset
    // from when the segment started playing.
 
    LONGLONG llOffset = mtNow - (mtStartTime - mtStartPoint);
 
    // If mtLoopEnd is not zero, set llLoopEnd to mtLoopEnd;
    // otherwise use the segment length.
 
    LONGLONG llLoopEnd = mtLoopEnd ? mtLoopEnd : mtLength;
 
    LONGLONG llLoopStart = mtLoopStart;
 
    // Adjust offset to take looping into account.
 
    if ((dwLoopRepeats != 0) &&  (llLoopStart < llLoopEnd) &&  (llLoopEnd > mtStartPoint))
    {
        if ((dwLoopRepeats != DMUS_SEG_REPEAT_INFINITE)
          &&  (llOffset > (llLoopStart + (llLoopEnd - llLoopStart) *(signed)dwLoopRepeats)))
        {
            llOffset -= (llLoopEnd - llLoopStart) * dwLoopRepeats;
        }
        else if (llOffset > llLoopStart)
        {
            llOffset = llLoopStart + (llOffset - llLoopStart) % (llLoopEnd - llLoopStart);
        }
    }

    llOffset = min(llOffset, LONG_MAX);  // LONG_MAX is defined in Limits.h.
    return long(llOffset);
}

To restart the segment at the correct position, pass the return value of the sample function to IDirectMusicSegment8::SetStartPoint before calling IDirectMusicPerformance8::PlaySegmentEx.

Note   The mtLength parameter of the example function will normally be 1 for segments loaded from WAV files. In this case, before calling SetStartPoint you must use IDirectMusicSegment8::SetLength to set the length of the segment to at least 1 tick more than the current offset. For more information, see IDirectMusicSegment8::SetStartPoint.