Advertisement

Debug assertion error in DX-Sound

Started by May 13, 2000 11:23 AM
-1 comments, last by bosjoh 24 years, 7 months ago
I got directsound to work in my game. It can play waves easily, but a bug remains. When the game exits (Obviously in the closesound() function), a popup window appears, saying that a debug assertion failure appeared. I ask you kind folks to look at the code, especially those that have experience with debug errors. Maybe it''s just my system. You can download the code here. Or look at the source below:

//Handles DirectSound waveplaying

#include 

LPDIRECTSOUND dsound;
LPDIRECTSOUNDBUFFER soundbuf;
WAVEFORMATEX *wfx;
DSBUFFERDESC dsbdesc;
HMMIO mmiohandle;
MMCKINFO mmchunk;
MMCKINFO mmriffchunk;
//Direct Sound API''s

bool initsound(void);
void closesound(void);
bool loadwave(char *);
bool playwave(void);
void stopwave(void);
void loadwavefile(char *);
bool waveopenfile(char *);
bool ReadMMIO(void);
bool reset(void);
bool createstaticbuffer(void);
bool readwave(UINT,BYTE*,UINT*);
bool restorebuffer(void);
void closewave(void);
//Function prototypes

bool initsound(void){
   wfx=(WAVEFORMATEX *)malloc(sizeof(WAVEFORMATEX));
   if (DirectSoundCreate(NULL,&dsound,NULL)!=DS_OK) return(false);
   if (dsound->SetCooperativeLevel(windowhandle,DSSCL_PRIORITY)!=DS_OK) return(false);
   dsbdesc.dwSize=sizeof(dsbdesc);
   dsbdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
   dsbdesc.dwBufferBytes=0;
   dsbdesc.lpwfxFormat=NULL;
   if (dsound->CreateSoundBuffer(&dsbdesc,&soundbuf,NULL)!=DS_OK) return(false);
   wfx->wFormatTag=WAVE_FORMAT_PCM; 
   wfx->nChannels=2; 
   wfx->nSamplesPerSec=22050; 
   wfx->wBitsPerSample=16; 
   wfx->nBlockAlign=wfx->wBitsPerSample/8*wfx->nChannels;
   wfx->nAvgBytesPerSec=wfx->nSamplesPerSec*wfx->nBlockAlign;
   if (soundbuf->SetFormat(wfx)!=DS_OK) return(false);
   soundbuf->Release();
   return(true);
};

void closesound(void){
   stopwave();
   closewave();
   if (dsound!=NULL){
      if (soundbuf!=NULL) soundbuf->Release();
	  dsound->Release();
   };
   if (wfx!=NULL) free(wfx);
};

bool loadwave(char *filename){
   stopwave();
   closewave();
   loadwavefile(filename);
   return(true);
};

bool playwave(void){
   DWORD dwstatus;
   if (soundbuf==NULL) return(false);
   createstaticbuffer();
   if (soundbuf->GetStatus(&dwstatus)!=DS_OK) return(false);
   if (dwstatus&DSBSTATUS_PLAYING) soundbuf->SetCurrentPosition(0);
   else if (soundbuf->Play(0,0,0)!=DS_OK) return(false);
   return(true);
};

void stopwave(void){
   if (soundbuf==NULL) return;
   soundbuf->Stop();
   soundbuf->SetCurrentPosition(0L);
};

void loadwavefile(char *filename){
   if (!waveopenfile(filename)) return;
   if (!reset()) closewave();
};

bool waveopenfile(char *filename){
   mmiohandle=mmioOpen(filename,NULL,MMIO_ALLOCBUF/MMIO_READ);
   if (mmiohandle==NULL) return(false);
   if (!ReadMMIO()){
      mmioClose(mmiohandle,0);
      return(false);
   };
   return(true);
};

bool ReadMMIO(void){
   MMCKINFO ckIn;
   PCMWAVEFORMAT pcmWaveFormat;
   if (wfx!=NULL) free(wfx);
   if (mmioDescend(mmiohandle,&mmchunk,NULL,0)!=0) return(false);
   if (mmchunk.ckid!=FOURCC_RIFF // mmchunk.fccType!=mmioFOURCC(''W'',''A'',''V'',''E'')) return(false);
   ckIn.ckid=mmioFOURCC(''f'',''m'',''t'','' '');
   if (mmioDescend(mmiohandle,&ckIn,&mmchunk,MMIO_FINDCHUNK)!=0) return(false);
   if (ckIn.cksizecbSize=0;
   } else {
      WORD cbExtraBytes=0L;
      if (mmioRead(mmiohandle,(CHAR*)&cbExtraBytes,sizeof(WORD))!=sizeof(WORD)) return(false);
	  wfx=(WAVEFORMATEX *)malloc(sizeof(WAVEFORMATEX)+cbExtraBytes);
      if (wfx==NULL) return(false);
      memcpy(wfx,&pcmWaveFormat,sizeof(pcmWaveFormat));
      wfx->cbSize=cbExtraBytes;
      if (mmioRead(mmiohandle,(CHAR*)(((BYTE*)&(wfx->cbSize))+sizeof(WORD)),cbExtraBytes)!=cbExtraBytes) return(false);
   };
   if (mmioAscend(mmiohandle,&ckIn,0)!=0) return(false);
   return(true);
};

bool reset(){
   if (mmioSeek(mmiohandle,mmchunk.dwDataOffset+sizeof(FOURCC),SEEK_SET)==-1) return(false);
   mmriffchunk.ckid=mmioFOURCC(''d'',''a'',''t'',''a'');
   if (mmioDescend(mmiohandle,&mmriffchunk,&mmchunk,MMIO_FINDCHUNK)!=0) return(false);
   return(true);
};

bool createstaticbuffer(void){
   BYTE* pbWavData;
   UINT cbWavSize;
   INT nWaveFileSize;
   if (!reset()) return(false);
   nWaveFileSize=mmriffchunk.cksize;
   pbWavData=(BYTE *)malloc(nWaveFileSize);
   if (pbWavData==NULL) return(false);
   if (!readwave(nWaveFileSize,pbWavData,&cbWavSize)) return(false);           
   dsbdesc.dwSize=sizeof(DSBUFFERDESC);
   dsbdesc.dwFlags=DSBCAPS_CTRLPAN / DSBCAPS_CTRLVOLUME / DSBCAPS_CTRLFREQUENCY / DSBCAPS_STATIC;
   dsbdesc.dwBufferBytes=cbWavSize;
   dsbdesc.lpwfxFormat=wfx;
   if (soundbuf!=NULL) soundbuf->Release();
   if (dsound->CreateSoundBuffer(&dsbdesc,&soundbuf,NULL)!=DS_OK) return(false);
   VOID* pbData=NULL;
   VOID* pbData2=NULL;
   DWORD dwLength;
   DWORD dwLength2;
   if (!restorebuffer()) return(false); 
   if (soundbuf->Lock(0,dsbdesc.dwBufferBytes,&pbData,&dwLength,&pbData2,&dwLength2,0L)!=DS_OK) return(false);
   memcpy(pbData,pbWavData,dsbdesc.dwBufferBytes);
   soundbuf->Unlock(pbData,dsbdesc.dwBufferBytes,NULL,0);
   pbData=NULL;
   if (pbWavData!=NULL) free(pbWavData);
   return(true);
};

bool readwave(UINT cbRead,BYTE* pbDest,UINT* cbActualRead){
   MMIOINFO mmioinfoIn;
   *cbActualRead=0;
   if (mmioGetInfo(mmiohandle,&mmioinfoIn,0)!=0) return(false);
   UINT cbDataIn=cbRead;
   if (cbDataIn>mmriffchunk.cksize) cbDataIn=mmriffchunk.cksize;       
   mmriffchunk.cksize-=cbDataIn;
   for (DWORD cT=0;cTGetStatus(&dwStatus)!=DS_OK) return(false);
   if (dwStatus&DSBSTATUS_BUFFERLOST){
      do{
         if (soundbuf->Restore()==DSERR_BUFFERLOST) Sleep(10);
      }while(soundbuf->Restore());
   };
   return(true);
};

void closewave(void){
   mmioClose(mmiohandle,0);
};

This topic is closed to new replies.

Advertisement