DirectSound API Reference

The DirectSound API provides a low-level interface for managing sound hardware and playing back audio on Windows. It offers fine-grained control over audio mixing, effects, and spatialization, making it a powerful tool for game development and audio-intensive applications.

Key Features and Concepts

Core Interfaces

The DirectSound API is largely object-oriented, relying on COM interfaces. The most fundamental interface is:

IDirectSound8

This is the primary interface for interacting with DirectSound. You obtain it by calling DirectSoundCreate8.

Other important interfaces include:

Common Functions

Some frequently used functions include:

Example Snippet (Creating a Secondary Buffer)


    IDirectSound8* pDS = NULL;
    IDirectSoundBuffer8* pDsbPrimary = NULL;
    IDirectSoundBuffer8* pDsbSecondary = NULL;
    DSBUFFERDESC dsbd;
    WAVEFORMATEX wfx;

    // Initialize DirectSound
    DirectSoundCreate8(NULL, &pDS, NULL);
    pDS->SetCooperativeLevel(GetForegroundWindow(), DSSCL_PRIORITY);

    // Set up the primary buffer description
    ZeroMemory(&dsbd, sizeof(dsbd));
    dsbd.dwSize = sizeof(dsbd);
    dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
    dsbd.dwBufferBytes = 0;
    dsbd.lpwfxFormat = NULL;

    // Create the primary buffer
    pDS->CreateSoundBuffer(&dsbd, &pDsbPrimary, NULL);

    // Set up the secondary buffer description
    ZeroMemory(&dsbd, sizeof(dsbd));
    dsbd.dwSize = sizeof(dsbd);
    dsbd.dwFlags = 0; // No special flags for now
    dsbd.dwBufferBytes = /* size of your audio data */;
    dsbd.lpwfxFormat = &wfx; // Your WAVEFORMATEX

    // Create the secondary buffer
    pDS->CreateSoundBuffer(&dsbd, &pDsbSecondary, NULL);

    // ... Load audio data into pDsbSecondary ...
    // ... Then call pDsbSecondary->Play() ...

    // Clean up
    if (pDsbSecondary) pDsbSecondary->Release();
    if (pDsbPrimary) pDsbPrimary->Release();
    if (pDS) pDS->Release();
        

DirectSound is part of the legacy DirectX SDK. For modern Windows audio development, consider using the WASAPI (Windows Audio Session API), which offers more flexibility and better integration with the Windows audio subsystem.

« Previous: Concepts | Next: Interfaces »