Code:
HANDLE hfile=CreateFile("boom.wave",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//set up buffer description
DSBUFFERDESC dsbd;
memset(&dsbd,0,sizeof(DSBUFFERDESC));
//size
dsbd.dwSize=sizeof(DSBUFFERDESC);
//flags
dsbd.dwFlags=DSBCAPS_LOCSOFTWARE;
char Buffer[5];
Buffer[4]=0;
//read length
DWORD dwNumRead;
//length variable
DWORD dwLength;
DWORD dwDataLength;
//data buffer
UCHAR* ucTemp;
UCHAR* ucData;
LPWAVEFORMATEX lpWfx;
/*
BOOL ReadFile(
HANDLE hFile, // handle of file to read
LPVOID lpBuffer, // address of buffer that receives data
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // address of number of bytes read
LPOVERLAPPED lpOverlapped // address of structure for data
);
*/
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"RIFF"
ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of file
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//"WAVE"
bool done=false;
while(!done)
{
ReadFile(hfile,Buffer,4,&dwNumRead,NULL);//chunk header
ReadFile(hfile,&dwLength,sizeof(dwLength),&dwNumRead,NULL);//length of chunk
ucTemp=new UCHAR[dwLength];
ReadFile(hfile,ucTemp,dwLength,&dwNumRead,NULL);
//dependin on the chunk header, do something
if(strcmp("fmt ",Buffer)==0)// if the chunk contains the world "fmt "
{
//clear out format
memset(lpWfx,0,sizeof(WAVEFORMATEX));
//memcpy (&destination, &source, lenghtofbytes)
//copy from buffer
memcpy(lpWfx,ucTemp,dwLength);
}
if(strcmp("data",Buffer)==0)
{
//data chunk
//allocate data buffer
//copy length
dwDataLength=dwLength;
//copy buffer
memcpy(ucData,ucTemp,dwDataLength);
//we are done, and need no more chunks
done=true;
}
delete ucTemp;
}
//close the file
CloseHandle(hfile);
dsbd.dwBufferBytes=dwLength;
bullets->CreateSoundBuffer(&dsbd,&lpdsb, NULL);
DWORD buflen,buflen2;
void* bufptr;
lpdsb->Lock(0,0,&bufptr,&buflen,NULL,&buflen2,DSBLOCK_ENTIREBUFFER);
//copy from wave loader to sound buffer
memcpy(bufptr,ucData,dwLength);
lpdsb->Unlock(bufptr,buflen,NULL,buflen2);