Question

I have been trying to set the wave format in my program for quite some time now with no success...

it always returns false at the directsound->setFormat method... can someone pls help me out?

Posted below is the code im working on

bool SoundClass::InitializeDirectSound(HWND HANDLE)
{
HRESULT hr;
DSBUFFERDESC BufferDesc;
WAVEFORMATEX WaveFormat;

hr = DirectSoundCreate8(NULL,&m_pDirectSound,NULL);
if(FAILED(hr))
{
    return false;
}
hr = m_pDirectSound->SetCooperativeLevel(HANDLE,DSSCL_PRIORITY);
if(FAILED(hr))
{
    return false;
}

      //setup primary buffer desc

BufferDesc.dwSize = sizeof(DSBUFFERDESC);
BufferDesc.dwBufferBytes = 0;
BufferDesc.dwFlags =  DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
BufferDesc.guid3DAlgorithm = GUID_NULL;
BufferDesc.dwReserved = 0;
BufferDesc.lpwfxFormat = NULL;

      // Get control of the primary sound buffer on the default sound device.

hr = m_pDirectSound->CreateSoundBuffer(&BufferDesc,&m_pPrimarySoundBuffer,NULL);
if(FAILED(hr))
{
    return false;
}

      //setup the format of primary buffer
      // In this case it is a .WAV file recorded at 44,100 samples per second in 16-bit stereo (cd audio format).

WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
WaveFormat.nSamplesPerSec = 44,100;
WaveFormat.wBitsPerSample = 16;
WaveFormat.nChannels = 2;
WaveFormat.nBlockAlign =(WaveFormat.wBitsPerSample / 8) * WaveFormat.nChannels;
WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
WaveFormat.cbSize = 0;

      //set the primary buffer format to the described waveformat(it always returns false here)

hr = m_pPrimarySoundBuffer->SetFormat(&WaveFormat);
if(FAILED(hr))
{
    return false;
}
return true;
}
Was it helpful?

Solution

Unless it's a typo, the following line is a possible problem:

WaveFormat.nSamplesPerSec = 44,100;

It should be

WaveFormat.nSamplesPerSec = 44100L;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top