Solved

DirectSound - allocating buffers

Posted on 1998-05-11
3
362 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

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

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 144
Find Visual Studio Tools 2 126
Problem with SqlConnection 4 184
Which Linux flavors will this run on? 6 102
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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