DirectSound play buffer notification (IDirectSoundNotify8)

IDirectSoundNotify8 is an interface to get notified on playback on capture audio buffer reaching certain position in the buffer. It is a must thing when implementing ring buffers with new data continuously added to the buffer for seamless playback (continuously copied from in case of capture).

This project is a minimalistic C++ sample code to illustrate the API. To initialize the DirectSound subsystem it is required to provide a window handle, which is created using ATL’s CWindowImpl (CMessageOnlyWindowImpl).

...
ATLENSURE_SUCCEEDED(DirectSoundCreate8(NULL, &pDirectSound8, NULL));
...
ATLENSURE_SUCCEEDED(pDirectSound8->SetCooperativeLevel(Window, DSSCL_PRIORITY));
...
ATLENSURE_SUCCEEDED(pDirectSound8->CreateSoundBuffer(&BufferDescriptor, &pDirectSoundBuffer, NULL));
...
CComQIPtr<IDirectSoundNotify8, &IID_IDirectSoundNotify8> pDirectSoundNotify8 = pDirectSoundBuffer;
...
ATLENSURE_SUCCEEDED(pDirectSoundNotify8->SetNotificationPositions(g_nPositionCount, pPositionNotify));
ATLENSURE_SUCCEEDED(pDirectSoundBuffer->Play(0, 0, DSBPLAY_LOOPING));

Application output:

BufferDescriptor.dwBufferBytes 128000
g_nPositionCount 8
pPositionNotify[0].dwOffset 15999
pPositionNotify[1].dwOffset 31999
pPositionNotify[2].dwOffset 47999
pPositionNotify[3].dwOffset 63999
pPositionNotify[4].dwOffset 79999
pPositionNotify[5].dwOffset 95999
pPositionNotify[6].dwOffset 111999
pPositionNotify[7].dwOffset 127999
nWaitResult 0
nWaitResult 1
nWaitResult 2
nWaitResult 3
nWaitResult 4
nWaitResult 5
nWaitResult 6
nWaitResult 7
nWaitResult 0
nWaitResult 1
nWaitResult 2
nWaitResult 3
nWaitResult 4
nWaitResult 5
nWaitResult 6
nWaitResult 7

A Visual C++ .NET 2008 source code is available from SVN.

See Also:

4 Replies to “DirectSound play buffer notification (IDirectSoundNotify8)”

  1. Hi alax

    I am trying to develop a DMO for decoding multi-channel audio . I follow the C:\Program Files\Microsoft DirectX 9.0 SDK (Summer 2004)\Samples\C++\DirectShow\DMO\DMOSample . Can I write an audio decoder DMO using this . can you pls send me some links / sample code for doing the same . I am beginner to this . I have developed a vide decoder using the above example . thats all .

    Thanks & Regards
    bsuhra .

  2. Hi alax ,

    Thanks for the reply .

    In case of video if I give inputStream flags as DMO_INPUT_STREAMF_SINGLE_SAMPLE_PER_BUFFER , i am supposed to get samples for one sec or one frame .? In case of video it is one frame . Is there any options using which we can get one frame per buffer

    Thanks & Regards
    bsuhra .

  3. Audio sample is a single value for one channel, and 1/sample-rate second duration, typically it is a 8- or 16-bit value. You definitely don’t want a single sample per buffer.

Leave a Reply