Solved

DirectSound - allocating buffers

Posted on 1998-05-11
3
355 Views
Last Modified: 2012-08-14
This code creates the DirectSound object and Sets the cooperative level to exclusive.  Then it tries to allocate two buffers - neither of these succeeds and they return with on DS_OUTOFMEMORY error (even when only trying to allocate 1K, 172K or 10M.  Probably just missing something simple.

Thx

  DSBUFFERDESC BufferDesc;
  WAVEFORMATEX WaveFormat;

  // Create DirectSound object
  ErrCo = DirectSoundCreate(NULL, &ProgramData.DS, NULL);
  if (ErrCo != DS_OK) {
    Caption = "Fatal Error DirectSoundCreate: " + IntToStr(ErrCo & 0x2ff);
    return;
  }

  // Set Cooperative Level (exclusive)
  ErrCo = ProgramData.DS->SetCooperativeLevel(Handle, DSSCL_EXCLUSIVE);
  if (ErrCo != DS_OK) {
    Caption = "Fatal Error SetCooperativeLevel: " + IntToStr(ErrCo & 0x2ff);
    return;
  }

  // Create sound buffers
  WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
  WaveFormat.nChannels = 2;
  WaveFormat.nSamplesPerSec = 44100;
  WaveFormat.wBitsPerSample = 16;
  WaveFormat.nBlockAlign = WaveFormat.nChannels * WaveFormat.wBitsPerSample / 8;
  WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
  WaveFormat.cbSize = 0;

  BufferDesc.dwSize = sizeof(BufferDesc);
  BufferDesc.dwFlags = DSBCAPS_CTRLFREQUENCY  | DSBCAPS_CTRLPOSITIONNOTIFY
                       | DSBCAPS_CTRLVOLUME
                       | DSBCAPS_GETCURRENTPOSITION2
                       | DSBCAPS_LOCSOFTWARE;
  BufferDesc.dwBufferBytes = 8*1024; //1*WaveFormat.nAvgBytesPerSec;
  BufferDesc.lpwfxFormat = &WaveFormat;

  ErrCo // First
   = ProgramData.DS->CreateSoundBuffer(&BufferDesc, &ProgramData.SoundBuffChannel1, NULL);
  if (ErrCo != DS_OK) {
    Caption = "Fatal Error CreateSoundBuffer (1): " + IntToStr(ErrCo & 0x2ff);
    return;
  }

  ErrCo // Second
   = ProgramData.DS->CreateSoundBuffer(&BufferDesc, &ProgramData.SoundBuffChannel2, NULL);
  if (ErrCo != DS_OK) {
    Caption = "Fatal Error CreateSoundBuffer (2): " + IntToStr(ErrCo & 0x2ff);
    return;
  }
0
Comment
Question by:Odie
3 Comments
 

Accepted Solution

by:
econde earned 50 total points
ID: 1175671
1 - Are you sure DirectSound is working fine in your computer ?. Try runnig DS examples and check the values of BufferDesc
before calling CreateSoundBuffer.

2 - Try this before the call :

  memset(&DSBufferDesc, 0, sizeof(DSBUFFERDESC)); // Zero it out.
0
 
LVL 1

Author Comment

by:Odie
ID: 1175672
Zeroing it out did the trick.
0
 

Expert Comment

by:jeffmcclintock
ID: 4398026
Yes you forgot to set DSBufferDesc.dwReserved = 0
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question