DirectSound CreateSoundBuffer Problem

Started by
2 comments, last by Xangis 18 years ago
I'm having a bit of trouble with the following DirectSound initialization code. I'm trying to create the primary buffer and set the format (secondary buffers will be created later). In the call to CreateSoundBuffer at the end, I get a failure the ever-so-uninformative E_INVALIDARG error message. Any clues as to what I might be doing wrong?

    m_parentWindow = (HWND)parentWindow;
    HRESULT				hr;
    LPDIRECTSOUNDBUFFER primaryBuffer;
    int count;

    // Initialize COM
    if( FAILED( hr = CoInitialize( NULL )) )
    {
        return false;
    }

    // Create IDirectSound using the primary sound device
    if( FAILED( hr = DirectSoundCreate( NULL, &m_pDS, NULL ) ) )
    {
	MessageBox( NULL, DXGetErrorString8(hr), "DirectSoundCreate Error", MB_OK );
        return false;
    }

    // Set coop level to DSSCL_PRIORITY
    if( FAILED( hr = m_pDS->SetCooperativeLevel( m_parentWindow, DSSCL_PRIORITY ) ) )
    {
	MessageBox( NULL, DXGetErrorString8(hr), "DirectSound SetCooperativeLevel Error", MB_OK );
        return false;
    }

    // Primary buffer and format settings
    WAVEFORMATEX		wfx;
    DSBUFFERDESC		dsbd;
    ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
    dsbd.dwSize        = sizeof(DSBUFFERDESC);
    dsbd.dwFlags       = DSBCAPS_PRIMARYBUFFER | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
    // Bytes must be 0 for a primary buffer.
    dsbd.dwBufferBytes = 0;
    dsbd.dwReserved = 0;
    dsbd.guid3DAlgorithm = GUID_NULL;
    // Set primary buffer format to 44.1kHz and 16-bit stereo output.
    ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
    wfx.wFormatTag      = WAVE_FORMAT_PCM;
    wfx.nSamplesPerSec =  44100;
    wfx.wBitsPerSample = 16;
    wfx.nChannels = 2;
    wfx.nBlockAlign = wfx.nChannels * ( wfx.wBitsPerSample / 8 );
    wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

    dsbd.lpwfxFormat = &wfx;

    if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &primaryBuffer, NULL ) ) )
    {
	MessageBox( NULL, DXGetErrorString8(hr), "DirectSound CreateSoundBuffer Error", MB_OK );
        return false;
    }

Advertisement
Random guess: Can primary buffers have those flags on them (DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY)?

If you install the debug runtimes, and put the debug slider up to maxiumum, you should get D3D telling you what argument is invalid and why.
From the docs:

Quote:From MSDN:
lpwfxFormat
Address of a WAVEFORMATEX or WAVEFORMATEXTENSIBLE structure specifying the waveform format for the buffer. This value must be NULL for primary buffers.

I was originally setting lpwfxFormat to NULL and tried actually setting values for it as a test - but it was the same result either way.

It turns out that CreateSoundBuffer didn't like my adding anything at all to DSBCAPS_PRIMARYBUFFER (I had originally tried it without the DSBCAPS_CTRLFREQUENCY, DSBCAPS_CTRLVOLUME, and DSBCAPS_CTRLFREQUENCY). The problem, however, was my adding DSBCAPS_GLOBALFOCUS.

So, working corrections are:

dsbd.lpwfxFormat = NULL;
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;

And for the secondary buffer, I use:

dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;

And everything works out as I had originally intended. Thanks to Evil Steve for that tip about the debug slider and thanks to Dave Hunt for your post - to be exact it was part of the solution. :)

This topic is closed to new replies.

Advertisement