Reducing Latency

The latency in DirectMusic consists of two major components, system-dependent latency and latency padding.

System-dependent latency is calculated dynamically by approximating the minimum amount of write-ahead data required for the given system configuration. This behavior is automatic and not controllable by the application. The absolute minimum system-dependent latency is 4 milliseconds.

Latency padding ranges from 0 to 100 milliseconds, and is configurable by the application. This value is added to the system-dependent latency and may be used to mitigate glitching problems. To ensure maximum compatibility with older systems, latency padding is set to 55 milliseconds by default. Most applications do not need to increase the latency padding.

Decreasing the latency padding allows applications to take advantage of the low-latency capabilities of modern hardware. However, doing so comes at the risk of glitching on some systems. Only applications that truly need the lowest latency possible should reduce the latency padding value.

Changing the latency padding value is accomplished by setting the GUID_DMUS_PROP_WriteLatency property on the port. The following example code demonstrates the use of this property.

HRESULT SetLatency (IDirectMusicPort8 *pDMPort, DWORD dwLatency)
{
    IKsControl* pKSControl;
    HRESULT hr;

    // Query for IKsControl. All ports that support properties provide this interface.
    hr = pDMPort->QueryInterface(IID_IKsControl, (void**)&pKSControl);
    if (SUCCEEDED(hr)) {
        KSPROPERTY KSProperty;
        ULONG      ulDummy;
        ZeroMemory(&KSProperty, sizeof(KSProperty));
        KSProperty.Set   = GUID_DMUS_PROP_WriteLatency;
        KSProperty.Flags = KSPROPERTY_TYPE_SET;
        hr = pKSControl->KsProperty(&KSProperty, sizeof(KSProperty),
                (LPVOID)&dwLatency, sizeof(dwLatency), &ulDummy);
        pKSControl->Release();
    }
    return hr;
}

To attain the absolute minimum latency, applications must also reduce the wakeup interval of the DirectMusic realtime thread. This is achieved by setting the GUID_DMUS_PROP_WritePeriod property on the port.

See Also

© 2004 Microsoft Corporation. All rights reserved.