Jump to content

  • Log In with Google      Sign In   
  • Create Account


Direct Sound Help....


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 23 January 2002 - 04:00 AM

It''s very very confusing with all those descend ascend descend and ascend again.. chunks...fmt....data.... ahhhh!!! pls help. i just want to load a simple wave file.... but don''t want to use PlaySound().... plz... what''s all with this MMIOCKINFO... WAVEFORMATEX... then i think on the last part of the descend after the "data" chunk, you''ll create a pointer to a BYTE right? where you''ll use that data to put it in the mmioRead... then you use CopyMemory? well anyway... plz explain the process...

Sponsor:

#2 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 23 January 2002 - 11:41 PM

helllllppp!!!

explain briefly memcpy and CopyMemory... pls don''t say "copies character from one buffer to anther etc., ..." thanks!

#3 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 23 January 2002 - 11:41 PM

helllllppp!!!

explain briefly memcpy and CopyMemory... pls don''t say "copies character from one buffer to anther etc., ..." thanks!

#4 abdulla   Members   -  Reputation: 164

Like
Likes
Like

Posted 24 January 2002 - 12:44 AM

i believe CopyMemory is wrapper around memcpy, but memcpy is a really fast copy that copies from one buffer into another buffer the data directly

#5 Tracy   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 January 2002 - 04:59 AM

Method I've used to play .wav files with DirectSound (DX7):
      
//include dsound.h, dsound.lib, Mmsystem.h,

//and Winmm.lib in the project



//DirectSound Globals

//my structure for DirectSound

struct SOUND
{
//think of these as the sounds, the number of indexes in

//the array is the number of sounds i'm using

LPDIRECTSOUNDBUFFER lpDSSound[3];
//think of this as the sound card

LPDIRECTSOUND lpDS;
//buffer description

DSBUFFERDESC dsBD;
}dxSound;


//initSound

BOOL initSound(SOUND &dxSound,HWND hwnd)
{
//DirectSound value

DWORD dsval;

//DirectSound Create

dsval=DirectSoundCreate(NULL,&(dxSound.lpDS),NULL);
//handle errors

if(dsval!=DS_OK)
{
errStr=errSound;
return initFail(hwnd); //

}
//SetCooperativeLevel

dsval=dxSound.lpDS->SetCooperativeLevel(hwnd,
DSSCL_PRIORITY);
//handle errors

if(dsval!=DS_OK)
{
errStr=errSound;
return initFail(hwnd); //

}

//if initialization done (no problems)

return TRUE;
}
//initSound



//from the Cleanup() function

//clean up DirectSound

int i;
for(i=0;i<3;i++)
{
//clean up the sound buffers

if(dxSound.lpDSSound[i])
{
dxSound.lpDSSound[i]->Release();
dxSound.lpDSSound[i]=NULL;
}
}
//clean up lpDS and the structure

if(dxSound.lpDS)
{
dxSound.lpDS->Release();
dxSound.lpDS=NULL;
}
memset(&dxSound,0,sizeof(SOUND));


//call loadSound(), play .wav file

//where "be.wav" is "name of.wav"

//and the last number is "dxSound.lpDSSound[number]"

loadSound(dxSound,"be.wav",0); /**/
//play "dxSound.lpDSSound[buffer i'm playing]"

dxSound.lpDSSound[0]->Play(0,0,0);


//loadSound

//recieves the structure, the file, and

//the "buffer[number]" used

BOOL loadSound(SOUND &dxSound,char *file_name,int i)
{
HANDLE fileHandle = NULL;
HANDLE fileMapping = NULL;
PUCHAR filePointer = NULL;

//opening the file

fileHandle=CreateFile(file_name,GENERIC_READ,
FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE |
FILE_FLAG_SEQUENTIAL_SCAN,(HANDLE)NULL);
if(fileHandle==INVALID_HANDLE_VALUE)
return FALSE;

fileMapping=CreateFileMapping(fileHandle,NULL,
PAGE_READONLY,0,0,NULL);
if(fileMapping==NULL)
{
CloseHandle(fileHandle);
return FALSE;
}

filePointer=(PUCHAR)MapViewOfFile(fileMapping,FILE_MAP_READ,
0,0,0);
if(filePointer==NULL)
{
CloseHandle(fileHandle);
CloseHandle(fileMapping);
return FALSE;
}

WAVEFORMATEX *wavHeader=NULL;
DWORD wavLength=0;
BYTE *wavData=NULL;

if(parseWav(filePointer,&wavHeader, //call parseWav

&wavData,wavLength)==FALSE)
{
CloseHandle(fileHandle);
CloseHandle(fileMapping);
UnmapViewOfFile(filePointer);
return false;
}

if(dxSound.dsBD.dwSize!=0)
memset(&(dxSound.dsBD),0,sizeof(DSBUFFERDESC));
dxSound.dsBD.dwSize=sizeof(DSBUFFERDESC);
dxSound.dsBD.dwFlags=DSBCAPS_LOCSOFTWARE;
dxSound.dsBD.dwBufferBytes=wavLength;
dxSound.dsBD.lpwfxFormat=wavHeader;

DWORD result;

//Handle Sound Buffer

//i'm going to reload it

if(dxSound.lpDSSound[i])
{
dxSound.lpDSSound[i]->Release();
dxSound.lpDSSound[i]=NULL;
}

//create the sound buffer

result=dxSound.lpDS->CreateSoundBuffer(&(dxSound.dsBD),
&(dxSound.lpDSSound[i]),NULL);
if(result!=DS_OK)
{
CloseHandle(fileHandle);
CloseHandle(fileMapping);
UnmapViewOfFile(filePointer);
return FALSE;
}

BYTE *soundPointer=NULL;
DWORD spLength=0;

//Lock() it and write data to the buffer

result=dxSound.lpDSSound[i]->Lock(0,
dxSound.dsBD.dwBufferBytes,
(LPVOID*)&soundPointer,&spLength,
NULL,NULL,DSBLOCK_FROMWRITECURSOR);

if(result!=DS_OK)
{
CloseHandle(fileHandle);
CloseHandle(fileMapping);
UnmapViewOfFile(filePointer);
return FALSE;
}

memcpy(soundPointer,wavData,spLength);

//done writing data to the buffer, so Unlock() it

dxSound.lpDSSound[i]->Unlock((LPVOID*)soundPointer,
spLength,NULL,NULL);

//Done

CloseHandle(fileHandle);
CloseHandle(fileMapping);
UnmapViewOfFile(filePointer);

return TRUE;
}
//loadSound



//parseWav

//alot of the stuff here is MMIO API functions.

//.wav files are made up of chunks, containing a 4 character

//code (4-byte length field) and data.

//the MMIO API functions are used to handle this stuff (so include

//Mmsystem.h, and Winmm.lib).

//

//chunks:

//RIFF

// WAVE

// fmt

// data

//

//need to retrieve the format of the file and associated

//data for DirectSound.

BOOL parseWav(void *file,WAVEFORMATEX **wavHeader,
BYTE **wavData,DWORD &wavLength)
{
LPDWORD pFile;
LPDWORD pFileEnd;
DWORD length;
DWORD type;

pFile = (DWORD*)file;
if((DWORD)*pFile++ != mmioFOURCC('R','I','F','F'))
return FALSE;

length=*pFile++;
type=*pFile++;

if(type!=mmioFOURCC('W','A','V','E'))
return FALSE;

pFileEnd=(DWORD*)((BYTE*)pFile+length-4);

while(pFile<pFileEnd)
{
type=*pFile++;
length=*pFile++;

switch(type)
{
case mmioFOURCC('f','m','t',' '):
if(wavHeader && !*wavHeader)
{
*wavHeader = (WAVEFORMATEX*)pFile;
if(wavLength != 0)
return TRUE;
}
break;
case mmioFOURCC('d', 'a', 't', 'a'):
if((wavData&&!*wavData))
{
*wavData=(LPBYTE)pFile;
wavLength = length;
if(*wavHeader)
return TRUE;
}
break;
}

pFile=(DWORD*)((BYTE*)pFile+((length+1)&~1));

}

return FALSE;
}
//parseWav



"A man can't just sit around." 'Lawn Chair' Larry Walters (1982)

Edited by - Tracy on January 25, 2002 12:11:27 PM

#6 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 25 January 2002 - 05:58 AM

tracy: wait... i know where that thing comes from.... it''s from www.gametutorials.com right? hehe

abdulla: could you explain it then? and why is memcpy faster?

#7 Tracy   Members   -  Reputation: 122

Like
Likes
Like

Posted 25 January 2002 - 06:18 AM

Yeah.
I hadn''t done any programming with DirectSound and that was the only place where I found a tutorial on it. The only thing I changed was putting the sound buffers into an array (I think I might have changed it a little to work in a DirectDraw App, too).
Good tutorial, I think there''s another one there on DSound also.

"A man can''t just sit around." ''Lawn Chair'' Larry Walters (1982)

#8 mickey   Members   -  Reputation: 100

Like
Likes
Like

Posted 26 January 2002 - 08:40 PM

tracey: i think there''s a problem with that function, it can''t load any kind of bitmaps!

#9 Oluseyi   Staff Emeritus   -  Reputation: 1670

Like
Likes
Like

Posted 27 January 2002 - 06:41 AM

quote:
Original post by mickey
tracey: i think there''s a problem with that function, it can''t load any kind of bitmaps!

!



[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS