Partager via


Changing the Tempo

The tempo of a performance dictates the conversion between the two types of time used in DirectMusic, which in turn controls the resolution of events to musical boundaries. (See Clock Time and Music Time.) The tempo track of the control segment usually manages the tempo, but it is also possible for an application to set the tempo dynamically.

There are two ways to change the tempo: by sending a message and by setting a track parameter on the control segment.

The following  example function sends a message to change the tempo, after disabling the tempo track so that it does not override the new tempo.

HRESULT ChangeTempoByMessage(IDirectMusicPerformance8* pPerformance, 
                    IDirectMusicSegment8* pSegment, 
                    double dblNewTempo)
{
   DMUS_TEMPO_PMSG* pTempoMsg;
   HRESULT hr;

   if (SUCCEEDED(hr = pSegment->SetParam(GUID_DisableTempo, 
          0xFFFF, 0, 0, NULL)))
   {
    if (SUCCEEDED(hr = pPerformance->AllocPMsg(
        sizeof(DMUS_TEMPO_PMSG), (DMUS_PMSG**)&pTempoMsg)))
    {
      // Cue the tempo event.
      ZeroMemory(pTempoMsg, sizeof(DMUS_TEMPO_PMSG));
      pTempoMsg->dwSize = sizeof(DMUS_TEMPO_PMSG);
      pTempoMsg->dblTempo = dblNewTempo;
      pTempoMsg->dwFlags = DMUS_PMSGF_REFTIME;
      pTempoMsg->dwType = DMUS_PMSGT_TEMPO;
      pPerformance->SendPMsg((DMUS_PMSG*)pTempoMsg);
    }
  }
  return hr;
}

If the performance has more than one audiopath, the message should be stamped for delivery to the correct audiopath. For more information, see Application-Created Messages.

The following example shows how to change the tempo parameter.

HRESULT ChangeTempoParameter(IDirectMusicSegment8* pSegment, double dblNewTempo)
{
  DMUS_TEMPO_PARAM Tempo;
  Tempo.dblTempo = dblNewTempo;
  HRESULT hr = pSegment->SetParam(GUID_TempoParam, 0xFFFF, 0, 0, &Tempo);
  return hr;
}

Note   DMUS_TEMPO_PARAM is declared in Dmusicf.h, which is not automatically included when you include Dmusici.h.

You can also change the master tempo, which adjusts the tempo set by any control segment.

See Also

© 2004 Microsoft Corporation. All rights reserved.