Jump to content
  • Advertisement
Sign in to follow this  
Ruudje

CreateSoundBuffer problem

This topic is 4962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, havent been around for a long time, but i've got myself a problem. My code compiles fine, but the program crashes during the loading of the wave-files, so I guess I;'m addressing the wrong memory, but I cant find what i'm doing wrong here. My idea is this: I have 1 directory with x wavefiles in it. I pass this dir-name to a function along with a pointer to an array of soundbuffers to be. This idea already works for me with bitmaps, but cant' get it working for waves. Some code: // this variable is global: LPDIRECTSOUNDBUFFER *Sound; // the function to load a dir: bool LoadSoundDir(LPCSTR Directory_To_Load, LPDIRECTSOUNDBUFFER *& Pointer_To_Sound_Array) { ... // This works fine, tested etc, also proven in the bitmap-loader: Pointer_To_Sound_Array = new LPDIRECTSOUNDBUFFER[n_Count]; ... // The point on which the program crashes is this line: if(lpDS->CreateSoundBuffer(&dsbdDesc, &Pointer_To_Sound_Array, NULL) != DS_OK) { ... } ... } // this is how i call the function if(!LoadSoundDir("../data/Sounds/", Sound)) The entire idea is that i can do the following: - Load the dir Sounds into the array Sound - in the program use it like Sound[3]->Play(...); Just like i did with my graphics - Load the dir Player - Display sprite 1 by e.g. Draw(Player[0]); Any help would be greatly appreciated Tnx in advance

Share this post


Link to post
Share on other sites
Advertisement

bool LoadSoundDir(LPCSTR Directory_To_Load, LPDIRECTSOUNDBUFFER *& Pointer_To_Sound_Array)
{
int i = 0, n_DATCount = 0;
char filename[255];
bool b_Status = true;

CWave * sounds = 0;

_finddata_t c_file;

char findcommand[255];
sprintf(findcommand, "%s*.wav", Directory_To_Load);

long hFile = _findfirst(findcommand, &c_file);

if(hFile != -1)
{
n_DATCount++;

while(_findnext(hFile, &c_file) == 0)
n_DATCount++;
}
else
b_Status = false; // NO SOUNDS TO USE???

_findclose(hFile);

sounds = new CWave[n_DATCount];

hFile = _findfirst(findcommand, &c_file);

if(hFile != -1)
{
sprintf(filename, "%s%s", Directory_To_Load, c_file.name);
sounds[0].Create(filename);

while(_findnext(hFile, &c_file) == 0)
{
sprintf(filename, "%s%s", Directory_To_Load, c_file.name);
sounds[++i].Create(filename);
}
}

_findclose(hFile);

// CWaves to LPDSBuffers

Pointer_To_Sound_Array = new LPDIRECTSOUNDBUFFER[n_DATCount];

for(i = 0; i < n_DATCount; i++)
{
DWORD dwDataLen = sounds.GetDataLen();
WAVEFORMATEX wfFormat;
sounds.GetFormat(wfFormat);

DSBUFFERDESC dsbdDesc;
ZeroMemory(&dsbdDesc, sizeof(DSBUFFERDESC));
dsbdDesc.dwSize = sizeof(DSBUFFERDESC);
dsbdDesc.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_STATIC;
dsbdDesc.dwBufferBytes = dwDataLen;
dsbdDesc.lpwfxFormat = &wfFormat;

if(lpDS->CreateSoundBuffer(&dsbdDesc, &Pointer_To_Sound_Array, NULL) != DS_OK)
b_Status = false;

unsigned char *pDSBuffData;

if(Pointer_To_Sound_Array->Lock(0, dwDataLen, (void**)&pDSBuffData, &dwDataLen, NULL, 0, 0) != DS_OK)
b_Status = false;

dwDataLen = sounds.GetData(pDSBuffData, dwDataLen);

if(Pointer_To_Sound_Array->Unlock(pDSBuffData, dwDataLen, NULL, 0) != DS_OK)
b_Status = false;
}

delete [] sounds;

return b_Status;
}


It's prob. not at its most efficient, but hey, its only executed while loading..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!