Solved

DirectSound - allocating buffers

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
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.

808 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