HRESULT FrkSound::LoadAudio(const tchar* audioPath) { HRESULT result; CWaveFile audioObject; result = audioObject.Open(LPTSTR(audioPath), 0, WAVEFILE_READ); if (!FAILED(result)) { m_hBufferDescription.dwBufferBytes = audioObject.GetSize(); m_hBufferDescription.lpwfxFormat = audioObject.m_pwfx; result = m_hAudioHandler->CreateSoundBuffer(&m_hBufferDescription, &m_hSoundBuffer, 0); VOID* pointerToLockBuffer = 0; DWORD lockSize = 0; result = result | (m_hSoundBuffer)->Lock(0, AUDIO_BUFFER_SIZE, &pointerToLockBuffer, &lockSize, 0, 0, DSBLOCK_ENTIREBUFFER); if (!FAILED(result)) { DWORD readedData = 0; audioObject.ResetFile(); result = audioObject.Read((BYTE*)pointerToLockBuffer, lockSize, &readedData); if (!FAILED(result)) { (m_hSoundBuffer)->Unlock(pointerToLockBuffer, lockSize, 0, 0); } } } audioObject.Close(); return result; }
// ----------------------------------------------- // Load am thanh vao buffer // ----------------------------------------------- HRESULT Sound::loadAudio(const char* audioPath_) { HRESULT result; CWaveFile audioObject; result = audioObject.Open(LPTSTR(audioPath_), 0, 1); if (!FAILED(result)) { _bufferDescription.dwBufferBytes = audioObject.GetSize(); _bufferDescription.lpwfxFormat = audioObject.m_pwfx; result = _audioHandler->CreateSoundBuffer(&_bufferDescription, &_soundBuffer, 0); VOID* pointerToLockedBuffer = 0; DWORD lockedSize = 0; result = result | (_soundBuffer)->Lock(0, AUDIO_BUFFER_SIZE, &pointerToLockedBuffer, &lockedSize, 0, 0, DSBLOCK_ENTIREBUFFER); if (!FAILED(result)) { DWORD readedData = 0; audioObject.ResetFile(); result = audioObject.Read((BYTE*)pointerToLockedBuffer, lockedSize, &readedData); if (!FAILED(result)) { (_soundBuffer)->Unlock(pointerToLockedBuffer, lockedSize, 0, 0); } } } audioObject.Close(); return result; }
bool DXSoundBuffer::LoadWaveToSoundBuffer(std::string wavFilename) { CWaveFile *wavFile; HRESULT hr; wavFile = new CWaveFile(); wavFile->Open((char*)wavFilename.c_str(), NULL, WAVEFILE_READ); if (wavFile->GetSize() == 0) { OutputDebugString("[SoundBuffer.cpp] Cannot init. DXSoundBuffer::LoadWaveToSoundBuffer(std::string wavFilename)"); return false; } DSBUFFERDESC dsbd; ZeroMemory(&dsbd, sizeof(DSBUFFERDESC)); dsbd.dwSize = sizeof(DSBUFFERDESC); //dsbd.dwFlags = 0; dsbd.dwFlags = DSBCAPS_CTRLVOLUME; dsbd.dwBufferBytes = wavFile->GetSize(); dsbd.guid3DAlgorithm = GUID_NULL; dsbd.lpwfxFormat = wavFile->m_pwfx; hr = _pDS->CreateSoundBuffer(&dsbd, &_soundBuffer, NULL); if FAILED(hr) { OutputDebugString("[SoundBuffer.cpp] Cannot init. DXSoundBuffer::LoadWaveToSoundBuffer(std::string wavFilename)"); return false; } VOID* pDSLockedBuffer = NULL; // Pointer to locked buffer memory DWORD dwDSLockedBufferSize = 0; // Size of the locked DirectSound buffer DWORD dwWavDataRead = 0; // Amount of data read from the wav file hr = _soundBuffer->Lock(0, wavFile->GetSize(), &pDSLockedBuffer, &dwDSLockedBufferSize, NULL, NULL, 0L); if FAILED(hr) return false; // Reset the wave file to the beginning wavFile->ResetFile(); // Read the wave file hr = wavFile->Read((BYTE*)pDSLockedBuffer, dwDSLockedBufferSize, &dwWavDataRead); // Check to make sure that this was successful if FAILED(hr) return false; // Check to make sure the wav file is not empty if (dwWavDataRead == 0) { // Wav is blank, so just fill with silence FillMemory((BYTE*)pDSLockedBuffer, dwDSLockedBufferSize, (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0)); } else if (dwWavDataRead < dwDSLockedBufferSize) { // Don't repeat the wav file, just fill in silence FillMemory((BYTE*)pDSLockedBuffer + dwWavDataRead, dwDSLockedBufferSize - dwWavDataRead, (BYTE)(wavFile->m_pwfx->wBitsPerSample == 8 ? 128 : 0)); } // Unlock the buffer, we don't need it anymore. _soundBuffer->Unlock(pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0); // Clean up delete wavFile; return true; }