Solved

DirectSound - allocating buffers

Posted on 1998-05-11
3
346 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
Comment Utility
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
Comment Utility
Zeroing it out did the trick.
0
 

Expert Comment

by:jeffmcclintock
Comment Utility
Yes you forgot to set DSBufferDesc.dwReserved = 0
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now