Click here to Skip to main content
15,908,776 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRe: Read and write Pin
Rajesh R Subramanian2-May-07 23:12
professionalRajesh R Subramanian2-May-07 23:12 
AnswerRe: Read and write Pin
prathuraj2-May-07 23:19
prathuraj2-May-07 23:19 
GeneralRe: Read and write Pin
Rajesh R Subramanian2-May-07 23:20
professionalRajesh R Subramanian2-May-07 23:20 
GeneralRe: Read and write Pin
prathuraj2-May-07 23:21
prathuraj2-May-07 23:21 
GeneralRe: Read and write Pin
Rajesh R Subramanian2-May-07 23:34
professionalRajesh R Subramanian2-May-07 23:34 
QuestionHelp! about DirectSound? Pin
kcynic2-May-07 22:43
kcynic2-May-07 22:43 
QuestionRe: Help! about DirectSound? Pin
Mark Salsbery3-May-07 6:20
Mark Salsbery3-May-07 6:20 
AnswerRe: Help! about DirectSound? Pin
kcynic3-May-07 14:57
kcynic3-May-07 14:57 
I'm sorry.I will have a try to using 'pre' this time
CString strFile_in="sound.wav";
LPVOID lpPtr1=NULL;
LPVOID lpPtr2=NULL;
HRESULT hr;
DWORD dwLen1,dwLen2;
LPVOID lp_mMemory=NULL;//内存指针
LPVOID lpData=NULL;//media data point
LPWAVEFORMATEX lpWaveFormat=NULL;//wave format point
DWORD dwSize=0;//the size of the media file's data
CFile file;
DWORD dwFileSize;//the size of the media file
//open the wave media file
if(!file.Open(strFile_in,CFile::modeRead|CFile::shareDenyNone))
{
AfxMessageBox("Open the media file failed!");
return ;
}
dwFileSize=file.Seek(0L,CFile::end);//get the length of the media file
file.Seek(0L,CFile::begin);//locate the begin of the file
//allocate the memory
lp_mMemory=GlobalAlloc(GMEM_FIXED,dwFileSize);
if(lp_mMemory==NULL)
{
AfxMessageBox("Allocate memory failed!");
file.Close();
return ;
}
if(file.ReadHuge(lp_mMemory,dwFileSize)!=dwFileSize)
{
AfxMessageBox("Read file error!");
file.Close();
GlobalFree(lp_mMemory);//free the memory
return ;
}
file.Close();//the file is no longer useful in this program

LPDWORD lpdFile,lpdEnd;
DWORD dwRiff,dwType,dwLen;
lpdFile=(DWORD*)lp_mMemory;
dwRiff=*lpdFile++;
dwLen=*lpdFile++;
dwType=*lpdFile++;
if(dwRiff!=mmioFOURCC('R','I','F','F'))
{
AfxMessageBox("This is not a RIFF file!");
GlobalFree(lp_mMemory);
return ;
}
if(dwType!=mmioFOURCC('W','A','V','E'))
{
AfxMessageBox("This is not a WAVE file!");
GlobalFree(lp_mMemory);
return ;
}

//seek the format chunk,data chunk and get their length
lpdEnd=(DWORD*)((BYTE*)lp_mMemory+dwFileSize-4);
BOOL bEnd=FALSE;
while((lpdFile{
dwType=*lpdFile++;
dwLen=*lpdFile++;
switch(dwType)
{
case mmioFOURCC('f','m','t',' ')://如果是"fmt"标志
if(!lpWaveFormat)
{
if(dwLen {
AfxMessageBox("There is some wrong in the file struct!");
return ;
}
else
lpWaveFormat=(LPWAVEFORMATEX)lpdFile;
}
break;
case mmioFOURCC('d','a','t','a')://如果是"data"标志
if(!lpData||!dwSize)
{
lpData=(LPBYTE)lpdFile;
dwSize=dwLen;
if(lpWaveFormat)
bEnd=TRUE;
}
break;
}

lpdFile=(DWORD*)((BYTE*)lpdFile+((dwLen+1)&~1));
}

DSBUFFERDESC BufferDesc;
memset(&BufferDesc,0,sizeof(BufferDesc));
BufferDesc.lpwfxFormat=(LPWAVEFORMATEX)lpWaveFormat;
BufferDesc.dwSize=sizeof(DSBUFFERDESC);
BufferDesc.dwBufferBytes=dwSize;
BufferDesc.dwFlags=0;

// HRESULT hr;
LPDIRECTSOUND lpDirectSound;
hr=::DirectSoundCreate(0,&lpDirectSound,NULL);
if(hr!=DS_OK)
{
AfxMessageBox("Create DirectSound failed!");
GlobalFree(lp_mMemory);
return ;
}
//设置设备优先级为"Normal"
hr=lpDirectSound->SetCooperativeLevel(this->GetSafeHwnd(),DSSCL_NORMAL);
if(hr!=DS_OK)
{
AfxMessageBox("SetCooperativeLevel failed!");
}
lpDirectSound->Initialize(NULL);
//创建声音数据缓冲
LPDIRECTSOUNDBUFFER pSoundBuffer=NULL;
hr=lpDirectSound->CreateSoundBuffer(&BufferDesc,&pSoundBuffer,NULL);
if(hr==DS_OK)
{
hr=pSoundBuffer->Lock(0,dwSize,&lpPtr1,&dwLen1,&lpPtr2,&dwLen2,0);
if(hr==DS_OK)
{
memcpy(lpPtr1,lpData,dwLen1);
if(dwLen2>0)
{
BYTE* pData=(BYTE*)lpData+dwLen1;
lpData=(void*)pData;
memcpy(lpPtr2,lpData,dwLen2);
}
}
pSoundBuffer->Unlock(lpPtr1,dwLen1,lpPtr2,dwLen2);
DWORD dwFlags=0;
pSoundBuffer->Play(0,0,dwFlags);
}
else
{
AfxMessageBox("Create buffer failed!");
switch(hr)
{
case DSERR_ALLOCATED:
AfxMessageBox("DSERR_ALLOCATED ");
break;
case DSERR_BADFORMAT:
AfxMessageBox("DSERR_BADFORMAT ");
break;
case DSERR_INVALIDPARAM:
AfxMessageBox("DSERR_INVALIDPARAM ");
break;
case DSERR_NOAGGREGATION:
AfxMessageBox("DSERR_NOAGGREGATION ");
break;
case DSERR_OUTOFMEMORY :
AfxMessageBox("DSERR_OUTOFMEMORY ");
break;
case DSERR_UNINITIALIZED:
AfxMessageBox("DSERR_UNINITIALIZED");
break;
case DSERR_UNSUPPORTED :
AfxMessageBox("DSERR_UNSUPPORTED ");
break;
case DSERR_INVALIDCALL:
AfxMessageBox("DSERR_INVALIDCALL");
break;
}
}
GlobalFree(lp_mMemory);

GeneralRe: Help! about DirectSound? Pin
Mark Salsbery3-May-07 15:03
Mark Salsbery3-May-07 15:03 
GeneralRe: Help! about DirectSound? Pin
kcynic3-May-07 19:07
kcynic3-May-07 19:07 
GeneralRe: Help! about DirectSound? Pin
Mark Salsbery4-May-07 4:53
Mark Salsbery4-May-07 4:53 
GeneralRe: Help! about DirectSound? Pin
kcynic4-May-07 16:28
kcynic4-May-07 16:28 
GeneralRe: Help! about DirectSound? Pin
Mark Salsbery5-May-07 5:21
Mark Salsbery5-May-07 5:21 
Questionwhen and where to retrieve thread's return value Pin
neha.agarwal272-May-07 22:01
neha.agarwal272-May-07 22:01 
AnswerRe: when and where to retrieve thread's return value Pin
CPallini2-May-07 22:38
mveCPallini2-May-07 22:38 
QuestionSearch in vector? Pin
bosfan2-May-07 21:25
bosfan2-May-07 21:25 
AnswerRe: Search in vector? Pin
Stephen Hewitt2-May-07 21:32
Stephen Hewitt2-May-07 21:32 
GeneralRe: Search in vector? Pin
bosfan2-May-07 22:01
bosfan2-May-07 22:01 
GeneralRe: Search in vector? Pin
Stephen Hewitt2-May-07 22:08
Stephen Hewitt2-May-07 22:08 
GeneralRe: Search in vector? Pin
bosfan2-May-07 22:23
bosfan2-May-07 22:23 
GeneralRe: Search in vector? Pin
bosfan2-May-07 23:21
bosfan2-May-07 23:21 
AnswerRe: Search in vector? Pin
Arman S.2-May-07 21:34
Arman S.2-May-07 21:34 
GeneralRe: Search in vector? Pin
bosfan2-May-07 21:51
bosfan2-May-07 21:51 
QuestionCD+G format Pin
wasif_Muhammad2-May-07 21:23
wasif_Muhammad2-May-07 21:23 
QuestionHo to save word document programmaticaly? Pin
Banks K2-May-07 20:01
Banks K2-May-07 20:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.