Sign in to follow this  

[Question]No sound come out with DirectSound8

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am creating a Test Program to test DirectSound8 in DirectX9, if success i will make it into my project. This program is successfully play 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 try 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 23, 2008 1:29:09 PM]

Share this post


Link to post
Share on other sites

This topic is 3550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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