Solved

DirectSound - allocating buffers

Posted on 1998-05-11
3
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

695 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