Sign in to follow this  
felix1000

[Question(Solved)]Problem with DirectSound that makes no sound come out

Recommended Posts

felix1000    122
I am creating a Test Program to test DirectSound8 in DirectX9, if success i will put it into my project. This program is successfully playing sound with using sine format to generate the short[] buffer, therefore, now i try to read a wave file and play it now. The CWaveFile class is found in some website and he said it is copied out from DirectX example. because the class need BYTE[] to store data, so i change my buffer from short[] to BYTE[]. I called function to read data to buffer, fill buffer to DirectSound buffer without error. but, no sound come out from my speaker. I tried many way to find out error, I find out that the read function of CWaveFile return -2147221008 to me, so i try to printf the error in before fail return but nothing printed. and all DX debug function did not triggered... therefore, i really don't know how to solve the problem.... Please, help me T_T. thank you very much. I have made some changes in creating Buffer description to match the need to load wave file. DSBUFFERDESC setBufferDescription(){ DSBUFFERDESC dsbdesc; memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_STATIC; //dsbdesc.dwBufferBytes = 44100 * 2; // it is because the buffer is short[44100*2] before dsbdesc.dwBufferBytes = wavefile->GetSize(); dsbdesc.lpwfxFormat = &waveformat; return dsbdesc; } It is the related code i think in the main program: HRESULT fillSoundBuffer(){ LPVOID lpvWrite; DWORD dwLength; HRESULT hr = secondarybuffer->Lock(0, 0, &lpvWrite, &dwLength, NULL, NULL, DSBLOCK_ENTIREBUFFER); if(SUCCEEDED(hr)){ memcpy(lpvWrite, buffer, dwLength); hr = secondarybuffer->Unlock(lpvWrite,dwLength,NULL,NULL); } return hr; } bool openWaveFile(){ HRESULT hr; wavefile->Open((LPTSTR)(LPCTSTR)L"K:\\Project3\\CodeTesting\\drumloop.wav", &waveformat, WAVEFILE_READ); waveformat = *wavefile->GetFormat(); buffer = new BYTE[wavefile->GetSize()]; hr = wavefile->Read(buffer, wavefile->GetSize(), 0); return true; } It is the related code i think in class CWaveFile: HRESULT CWaveFile::Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags ) { HRESULT hr = 0; m_dwFlags = dwFlags; m_bIsReadingFromMemory = FALSE; if( m_dwFlags == WAVEFILE_READ ) { if( strFileName == NULL ) return E_INVALIDARG; SAFE_DELETE_ARRAY( m_pwfx ); m_hmmio = mmioOpen( strFileName, NULL, MMIO_ALLOCBUF | MMIO_READ ); if( NULL == m_hmmio ) { HRSRC hResInfo; HGLOBAL hResData; DWORD dwSize; VOID* pvRes; // Loading it as a file failed, so try it as a resource if( NULL == ( hResInfo = FindResource( NULL, strFileName, TEXT("WAVE") ) ) ) { if( NULL == ( hResInfo = FindResource( NULL, strFileName, TEXT("WAV") ) ) ) return DXTRACE_ERR_MSGBOX( TEXT("FindResource"), E_FAIL ); } if( NULL == ( hResData = LoadResource( NULL, hResInfo ) ) ) return DXTRACE_ERR_MSGBOX( TEXT("LoadResource"), E_FAIL ); if( 0 == ( dwSize = SizeofResource( NULL, hResInfo ) ) ) return DXTRACE_ERR_MSGBOX( TEXT("SizeofResource"), E_FAIL ); if( NULL == ( pvRes = LockResource( hResData ) ) ) //return 100000; return DXTRACE_ERR_MSGBOX( TEXT("LockResource"), E_FAIL ); CHAR* pData = new CHAR[ dwSize ]; memcpy( pData, pvRes, dwSize ); MMIOINFO mmioInfo; ZeroMemory( &mmioInfo, sizeof(mmioInfo) ); mmioInfo.fccIOProc = FOURCC_MEM; mmioInfo.cchBuffer = dwSize; mmioInfo.pchBuffer = (CHAR*) pData; m_hmmio = mmioOpen( NULL, &mmioInfo, MMIO_ALLOCBUF | MMIO_READ ); } if( FAILED( hr = ReadMMIO() ) ) { // ReadMMIO will fail if its an not a wave file mmioClose( m_hmmio, 0 ); return DXTRACE_ERR_MSGBOX( TEXT("ReadMMIO"), hr ); } if( FAILED( hr = ResetFile() ) ) return DXTRACE_ERR_MSGBOX( TEXT("ResetFile"), hr ); // After the reset, the size of the wav file is m_ck.cksize so store it now m_dwSize = m_ck.cksize; } else { m_hmmio = mmioOpen( strFileName, NULL, MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_CREATE ); if( NULL == m_hmmio ) return DXTRACE_ERR_MSGBOX( TEXT("mmioOpen"), E_FAIL ); if( FAILED( hr = WriteMMIO( pwfx ) ) ) { mmioClose( m_hmmio, 0 ); return DXTRACE_ERR_MSGBOX( TEXT("WriteMMIO"), hr ); } if( FAILED( hr = ResetFile() ) ) return DXTRACE_ERR_MSGBOX( TEXT("ResetFile"), hr ); } return hr; } HRESULT CWaveFile::Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead ) { if( m_bIsReadingFromMemory ) { if( m_pbDataCur == NULL ) printf("%s", CO_E_NOTINITIALIZED); return CO_E_NOTINITIALIZED; if( pdwSizeRead != NULL ) *pdwSizeRead = 0; if( (BYTE*)(m_pbDataCur + dwSizeToRead) > (BYTE*)(m_pbData + m_ulDataSize) ) { dwSizeToRead = m_ulDataSize - (DWORD)(m_pbDataCur - m_pbData); } CopyMemory( pBuffer, m_pbDataCur, dwSizeToRead ); if( pdwSizeRead != NULL ) *pdwSizeRead = dwSizeToRead; return S_OK; } else { MMIOINFO mmioinfoIn; // current status of m_hmmio if( m_hmmio == NULL ) printf("%s", CO_E_NOTINITIALIZED); return CO_E_NOTINITIALIZED; if( pBuffer == NULL || pdwSizeRead == NULL ) printf("%s", E_INVALIDARG); return E_INVALIDARG; if( pdwSizeRead != NULL ) *pdwSizeRead = 0; if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) ) return DXTRACE_ERR_MSGBOX( TEXT("mmioGetInfo"), E_FAIL ); UINT cbDataIn = dwSizeToRead; if( cbDataIn > m_ck.cksize ) cbDataIn = m_ck.cksize; m_ck.cksize -= cbDataIn; for( DWORD cT = 0; cT < cbDataIn; cT++ ) { // Copy the bytes from the io to the buffer. if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead ) { if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) ) return DXTRACE_ERR_MSGBOX( TEXT("mmioAdvance"), E_FAIL ); if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead ) return DXTRACE_ERR_MSGBOX( TEXT("mmioinfoIn.pchNext"), E_FAIL ); } // Actual copy. *((BYTE*)pBuffer+cT) = *((BYTE*)mmioinfoIn.pchNext); mmioinfoIn.pchNext++; } if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) ) return DXTRACE_ERR_MSGBOX( TEXT("mmioSetInfo"), E_FAIL ); if( pdwSizeRead != NULL ) *pdwSizeRead = cbDataIn; return S_OK; } } DWORD CWaveFile::GetSize() { return m_dwSize; } The main program is so long(many printf@@), so i don't paste all here some function is not pasted here those are no change from my success build using sine generated data to play: if (::createSoundObject() != DS_OK){ ... } ... if (!openWaveFile()){ ... } bufferdesc = setBufferDescription(); ... if ((hr = createSecondarySoundBuffer()) != DS_OK){ ... } if ((hr = ::fillSoundBuffer()) != DS_OK){ ... } secondarybuffer->SetCurrentPosition(0); secondarybuffer->Play(0,0,DSBPLAY_LOOPING); [Edited by - felix1000 on March 24, 2008 9:32:29 AM]

Share this post


Link to post
Share on other sites
Buckeye    10747
By the way, -2147221008 is CO_E_NOTINITIIALIZED.

CO_E_NOTINITIALIZED is a number, not a string so printf("%s",<number>) won't work.

You can check HRESULT error codes using DXGetErrorString8(hr).

Share this post


Link to post
Share on other sites
felix1000    122
^_^ thank you very much!!!
i have solved the problem!!

My DirectSound can come out sound now !!!

really thank you very much

although there are many very stupid bug in my program.

but Buckeye suggested me using DXGetErrorString really help me so much

although i found the buffer size i set cannot fully load all the sound data into DirectSound, but at least i can hear some sound now!!!

thank you very much too all people reading so long question with many stupid mistake, also, i need to say sorry to post out a source code that have so many mistake.

thank you very much

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this