Magic_Game/core/Tool/Music.cpp

843 lines
17 KiB
C++
Raw Normal View History

2018-04-21 21:24:46 +08:00
#include "..\e2dtool.h"
#include "..\e2dmanager.h"
2018-04-27 17:07:47 +08:00
#include <map>
2018-02-01 22:07:44 +08:00
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if (p) { delete (p); (p)=nullptr; } }
#endif
#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p)=nullptr; } }
#endif
2018-02-28 19:17:15 +08:00
inline bool TraceError(wchar_t* sPrompt)
2018-02-01 22:07:44 +08:00
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo error: %s failed!", sPrompt);
2018-02-01 22:07:44 +08:00
return false;
}
2018-02-28 19:17:15 +08:00
inline bool TraceError(wchar_t* sPrompt, HRESULT hr)
2018-01-30 16:45:38 +08:00
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo error: %s (%#X)", sPrompt, hr);
2018-02-01 22:07:44 +08:00
return false;
}
static IXAudio2 * s_pXAudio2 = nullptr;
static IXAudio2MasteringVoice * s_pMasteringVoice = nullptr;
2018-04-27 17:07:47 +08:00
static float s_fMusicVolume = 1.0;
2018-04-27 17:07:47 +08:00
// <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD>
class MusicPlayer
{
2018-04-27 17:07:47 +08:00
public:
MusicPlayer();
2018-04-27 17:07:47 +08:00
virtual ~MusicPlayer();
2018-04-27 17:07:47 +08:00
bool open(
const e2d::String& filePath
);
bool MusicPlayer::open(
int resNameId,
const e2d::String& resType
2018-04-27 17:07:47 +08:00
);
2018-04-27 17:07:47 +08:00
bool play(
int nLoopCount = 0
);
2018-04-27 17:07:47 +08:00
void pause();
2018-04-27 17:07:47 +08:00
void resume();
2018-02-01 22:07:44 +08:00
2018-04-27 17:07:47 +08:00
void stop();
void close();
bool setVolume(
float fVolume
);
bool isPlaying() const;
protected:
bool _readMMIO();
bool _resetFile();
bool _read(
BYTE* pBuffer,
DWORD dwSizeToRead
);
bool _findMediaFileCch(
wchar_t* strDestPath,
int cchDest,
const wchar_t * strFilename
);
protected:
bool m_bOpened;
mutable bool m_bPlaying;
DWORD m_dwSize;
CHAR* m_pResourceBuffer;
BYTE* m_pbWaveData;
HMMIO m_hmmio;
MMCKINFO m_ck;
MMCKINFO m_ckRiff;
WAVEFORMATEX* m_pwfx;
IXAudio2SourceVoice* m_pSourceVoice;
};
typedef std::map<UINT, MusicPlayer *> MusicMap;
static MusicMap& GetMusicFileList()
{
static MusicMap s_MusicFileList;
return s_MusicFileList;
}
static MusicMap& GetMusicResList()
{
static MusicMap s_MusicResList;
return s_MusicResList;
}
2018-04-27 17:07:47 +08:00
MusicPlayer::MusicPlayer()
2018-03-02 23:49:57 +08:00
: m_bOpened(false)
, m_bPlaying(false)
, m_pwfx(nullptr)
, m_hmmio(nullptr)
, m_pResourceBuffer(nullptr)
, m_pbWaveData(nullptr)
, m_dwSize(0)
, m_pSourceVoice(nullptr)
{
}
2018-04-27 17:07:47 +08:00
MusicPlayer::~MusicPlayer()
{
2018-03-02 23:49:57 +08:00
close();
}
bool MusicPlayer::open(const e2d::String& filePath)
{
2018-02-01 22:07:44 +08:00
if (m_bOpened)
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo can be opened only once!");
return false;
2018-02-01 22:07:44 +08:00
}
if (filePath.isEmpty())
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo::open Invalid file name.");
2018-02-01 22:07:44 +08:00
return false;
}
if (!s_pXAudio2)
2018-02-01 22:07:44 +08:00
{
2018-04-23 00:05:57 +08:00
WARN_IF(true, "IXAudio2 nullptr pointer error!");
2018-02-01 22:07:44 +08:00
return false;
}
2018-02-01 22:07:44 +08:00
// <20><>λ wave <20>ļ<EFBFBD>
wchar_t pFilePath[MAX_PATH];
if (!_findMediaFileCch(pFilePath, MAX_PATH, filePath))
2018-02-01 22:07:44 +08:00
{
WARN_IF(true, "Failed to find media file: %s", pFilePath);
2018-02-01 22:07:44 +08:00
return false;
}
m_hmmio = mmioOpen(pFilePath, nullptr, MMIO_ALLOCBUF | MMIO_READ);
2018-02-01 22:07:44 +08:00
if (nullptr == m_hmmio)
{
2018-02-01 22:07:44 +08:00
return TraceError(L"mmioOpen");
}
2018-02-01 22:07:44 +08:00
if (!_readMMIO())
{
2018-02-01 22:07:44 +08:00
// <20><>ȡ<EFBFBD><C8A1> wave <20>ļ<EFBFBD>ʱ ReadMMIO <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>
mmioClose(m_hmmio, 0);
return TraceError(L"_readMMIO");
}
2018-02-01 22:07:44 +08:00
if (!_resetFile())
return TraceError(L"_resetFile");
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>wave <20>ļ<EFBFBD><C4BC>Ĵ<EFBFBD>С<EFBFBD><D0A1> m_ck.cksize
m_dwSize = m_ck.cksize;
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
m_pbWaveData = new BYTE[m_dwSize];
if (!_read(m_pbWaveData, m_dwSize))
{
2018-02-01 22:07:44 +08:00
TraceError(L"Failed to read WAV data");
SAFE_DELETE_ARRAY(m_pbWaveData);
return false;
}
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
HRESULT hr;
if (FAILED(hr = s_pXAudio2->CreateSourceVoice(&m_pSourceVoice, m_pwfx)))
2018-02-01 22:07:44 +08:00
{
2018-04-23 00:05:57 +08:00
TraceError(L"Create source voice error", hr);
2018-02-01 22:07:44 +08:00
SAFE_DELETE_ARRAY(m_pbWaveData);
return false;
}
m_bOpened = true;
m_bPlaying = false;
return true;
}
bool MusicPlayer::open(int resNameId, const e2d::String& resType)
{
HRSRC hResInfo;
HGLOBAL hResData;
DWORD dwSize;
void* pvRes;
if (m_bOpened)
{
WARN_IF(true, "MusicInfo can be opened only once!");
return false;
}
if (!s_pXAudio2)
{
WARN_IF(true, "IXAudio2 nullptr pointer error!");
return false;
}
// Loading it as a file failed, so try it as a resource
if (nullptr == (hResInfo = FindResourceW(HINST_THISCOMPONENT, MAKEINTRESOURCE(resNameId), resType)))
return TraceError(L"FindResource");
if (nullptr == (hResData = LoadResource(HINST_THISCOMPONENT, hResInfo)))
return TraceError(L"LoadResource");
if (0 == (dwSize = SizeofResource(HINST_THISCOMPONENT, hResInfo)))
return TraceError(L"SizeofResource");
if (nullptr == (pvRes = LockResource(hResData)))
return TraceError(L"LockResource");
m_pResourceBuffer = new CHAR[dwSize];
memcpy(m_pResourceBuffer, pvRes, dwSize);
MMIOINFO mmioInfo;
ZeroMemory(&mmioInfo, sizeof(mmioInfo));
mmioInfo.fccIOProc = FOURCC_MEM;
mmioInfo.cchBuffer = dwSize;
mmioInfo.pchBuffer = (CHAR*)m_pResourceBuffer;
m_hmmio = mmioOpen(nullptr, &mmioInfo, MMIO_ALLOCBUF | MMIO_READ);
if (!_readMMIO())
{
// ReadMMIO will fail if its an not a wave file
mmioClose(m_hmmio, 0);
return TraceError(L"ReadMMIO");
}
if (!_resetFile())
return TraceError(L"ResetFile");
// After the reset, the size of the wav file is m_ck.cksize so store it now
m_dwSize = m_ck.cksize;
// Read the sample data into memory
m_pbWaveData = new BYTE[m_dwSize];
if (!_read(m_pbWaveData, m_dwSize))
{
TraceError(L"Failed to read WAV data");
SAFE_DELETE_ARRAY(m_pbWaveData);
return false;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
HRESULT hr;
if (FAILED(hr = s_pXAudio2->CreateSourceVoice(&m_pSourceVoice, m_pwfx)))
{
TraceError(L"Create source voice error", hr);
SAFE_DELETE_ARRAY(m_pbWaveData);
return false;
}
m_bOpened = true;
m_bPlaying = false;
return true;
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::play(int nLoopCount)
{
2018-03-02 23:49:57 +08:00
if (!m_bOpened)
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo::play Failed: MusicInfo must be opened first!");
2018-03-02 23:49:57 +08:00
return false;
}
if (m_pSourceVoice == nullptr)
{
2018-04-27 17:07:47 +08:00
WARN_IF(true, "MusicInfo::play Failed: IXAudio2SourceVoice Null pointer exception!");
2018-03-02 23:49:57 +08:00
return false;
}
2018-02-01 22:07:44 +08:00
if (m_bPlaying)
{
stop();
}
2018-02-06 21:11:54 +08:00
nLoopCount = min(nLoopCount, XAUDIO2_LOOP_INFINITE - 1);
nLoopCount = (nLoopCount < 0) ? XAUDIO2_LOOP_INFINITE : nLoopCount;
2018-02-01 22:07:44 +08:00
// <20>ύ wave <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
XAUDIO2_BUFFER buffer = { 0 };
buffer.pAudioData = m_pbWaveData;
buffer.Flags = XAUDIO2_END_OF_STREAM;
buffer.AudioBytes = m_dwSize;
buffer.LoopCount = nLoopCount;
2018-03-02 23:49:57 +08:00
HRESULT hr;
2018-02-01 22:07:44 +08:00
if (FAILED(hr = m_pSourceVoice->SubmitSourceBuffer(&buffer)))
{
2018-04-23 00:05:57 +08:00
TraceError(L"Submitting source buffer error", hr);
2018-02-01 22:07:44 +08:00
m_pSourceVoice->DestroyVoice();
SAFE_DELETE_ARRAY(m_pbWaveData);
return false;
}
2018-02-01 22:07:44 +08:00
if (SUCCEEDED(hr = m_pSourceVoice->Start(0)))
{
m_bPlaying = true;
}
return SUCCEEDED(hr);
}
2018-04-27 17:07:47 +08:00
void MusicPlayer::pause()
{
2018-02-01 22:07:44 +08:00
if (m_pSourceVoice)
{
if (SUCCEEDED(m_pSourceVoice->Stop()))
{
m_bPlaying = false;
}
}
}
2018-04-27 17:07:47 +08:00
void MusicPlayer::resume()
{
2018-02-01 22:07:44 +08:00
if (m_pSourceVoice)
{
if (SUCCEEDED(m_pSourceVoice->Start()))
{
m_bPlaying = true;
}
}
}
2018-04-27 17:07:47 +08:00
void MusicPlayer::stop()
{
2018-02-01 22:07:44 +08:00
if (m_pSourceVoice)
{
if (SUCCEEDED(m_pSourceVoice->Stop()))
{
m_pSourceVoice->ExitLoop();
m_pSourceVoice->FlushSourceBuffers();
m_bPlaying = false;
}
}
}
2018-04-27 17:07:47 +08:00
void MusicPlayer::close()
{
2018-02-01 22:07:44 +08:00
if (m_pSourceVoice)
2018-03-02 23:49:57 +08:00
{
m_pSourceVoice->Stop();
m_pSourceVoice->FlushSourceBuffers();
m_pSourceVoice->DestroyVoice();
m_pSourceVoice = nullptr;
}
if (m_hmmio != nullptr)
{
mmioClose(m_hmmio, 0);
m_hmmio = nullptr;
}
SAFE_DELETE_ARRAY(m_pResourceBuffer);
SAFE_DELETE_ARRAY(m_pbWaveData);
SAFE_DELETE_ARRAY(m_pwfx);
m_bOpened = false;
m_bPlaying = false;
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::isPlaying() const
2018-03-02 23:49:57 +08:00
{
if (m_bOpened && m_pSourceVoice)
{
2018-02-01 22:07:44 +08:00
XAUDIO2_VOICE_STATE state;
m_pSourceVoice->GetState(&state);
2018-02-01 22:07:44 +08:00
if (state.BuffersQueued == 0)
{
m_bPlaying = false;
}
return m_bPlaying;
}
else
{
return false;
}
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::setVolume(float fVolume)
{
2018-02-01 22:07:44 +08:00
if (m_pSourceVoice)
{
2018-04-27 17:07:47 +08:00
return SUCCEEDED(m_pSourceVoice->SetVolume(fVolume));
2018-02-01 22:07:44 +08:00
}
return false;
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::_readMMIO()
2018-02-01 22:07:44 +08:00
{
MMCKINFO ckIn;
PCMWAVEFORMAT pcmWaveFormat;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
memset(&ckIn, 0, sizeof(ckIn));
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
m_pwfx = nullptr;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
if ((0 != mmioDescend(m_hmmio, &m_ckRiff, nullptr, 0)))
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// ȷ<><C8B7><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD> wave <20>ļ<EFBFBD>
if ((m_ckRiff.ckid != FOURCC_RIFF) ||
(m_ckRiff.fccType != mmioFOURCC('W', 'A', 'V', 'E')))
return TraceError(L"mmioFOURCC");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>в<EFBFBD><D0B2><EFBFBD> 'fmt' <20><>
ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (0 != mmioDescend(m_hmmio, &ckIn, &m_ckRiff, MMIO_FINDCHUNK))
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// 'fmt' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> PCMWAVEFORMAT һ<><D2BB><EFBFBD><EFBFBD>
if (ckIn.cksize < (LONG)sizeof(PCMWAVEFORMAT))
return TraceError(L"sizeof(PCMWAVEFORMAT)");
// <20><> 'fmt' <20><><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1> pcmWaveFormat <20><>
if (mmioRead(m_hmmio, (HPSTR)&pcmWaveFormat,
sizeof(pcmWaveFormat)) != sizeof(pcmWaveFormat))
return TraceError(L"mmioRead");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><><EFBFBD><EFBFBD> WAVEFORMATEX<45><58><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> PCM <20><>ʽ<EFBFBD><CABD><EFBFBD>ٶ<EFBFBD>ȡһ<C8A1><D2BB> WORD <20><>С
// <20><><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݾ<EFBFBD><DDBE>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
if (pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
m_pwfx = (WAVEFORMATEX*)new CHAR[sizeof(WAVEFORMATEX)];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
memcpy(m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat));
m_pwfx->cbSize = 0;
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
else
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĴ<DDB5>С
WORD cbExtraBytes = 0L;
if (mmioRead(m_hmmio, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD))
return TraceError(L"mmioRead");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
m_pwfx = (WAVEFORMATEX*)new CHAR[sizeof(WAVEFORMATEX) + cbExtraBytes];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
memcpy(m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat));
m_pwfx->cbSize = cbExtraBytes;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (mmioRead(m_hmmio, (CHAR*)(((BYTE*)&(m_pwfx->cbSize)) + sizeof(WORD)),
cbExtraBytes) != cbExtraBytes)
{
SAFE_DELETE(m_pwfx);
return TraceError(L"mmioRead");
}
}
if (0 != mmioAscend(m_hmmio, &ckIn, 0))
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
SAFE_DELETE(m_pwfx);
return TraceError(L"mmioAscend");
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::_resetFile()
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
// Seek to the data
if (-1 == mmioSeek(m_hmmio, m_ckRiff.dwDataOffset + sizeof(FOURCC),
SEEK_SET))
return TraceError(L"mmioSeek");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Search the input file for the 'data' chunk.
m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (0 != mmioDescend(m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK))
return TraceError(L"mmioDescend");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::_read(BYTE* pBuffer, DWORD dwSizeToRead)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
MMIOINFO mmioinfoIn; // current status of m_hmmio
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
if (0 != mmioGetInfo(m_hmmio, &mmioinfoIn, 0))
return TraceError(L"mmioGetInfo");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
UINT cbDataIn = dwSizeToRead;
if (cbDataIn > m_ck.cksize)
cbDataIn = m_ck.cksize;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
m_ck.cksize -= cbDataIn;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
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 TraceError(L"mmioAdvance");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
if (mmioinfoIn.pchNext == mmioinfoIn.pchEndRead)
return TraceError(L"mmioinfoIn.pchNext");
}
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Actual copy.
*((BYTE*)pBuffer + cT) = *((BYTE*)mmioinfoIn.pchNext);
mmioinfoIn.pchNext++;
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
if (0 != mmioSetInfo(m_hmmio, &mmioinfoIn, 0))
return TraceError(L"mmioSetInfo");
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
return true;
2018-01-30 16:45:38 +08:00
}
2018-04-27 17:07:47 +08:00
bool MusicPlayer::_findMediaFileCch(wchar_t* strDestPath, int cchDest, const wchar_t * strFilename)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
bool bFound = false;
2018-01-30 16:45:38 +08:00
2018-03-02 23:49:57 +08:00
if (nullptr == strFilename || nullptr == strDestPath || cchDest < 10)
2018-02-01 22:07:44 +08:00
return false;
// Get the exe name, and exe path
2018-02-28 19:17:15 +08:00
wchar_t strExePath[MAX_PATH] = { 0 };
wchar_t strExeName[MAX_PATH] = { 0 };
wchar_t* strLastSlash = nullptr;
2018-02-01 22:07:44 +08:00
GetModuleFileName(HINST_THISCOMPONENT, strExePath, MAX_PATH);
strExePath[MAX_PATH - 1] = 0;
strLastSlash = wcsrchr(strExePath, TEXT('\\'));
if (strLastSlash)
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
wcscpy_s(strExeName, MAX_PATH, &strLastSlash[1]);
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Chop the exe name from the exe path
*strLastSlash = 0;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Chop the .exe from the exe name
strLastSlash = wcsrchr(strExeName, TEXT('.'));
if (strLastSlash)
*strLastSlash = 0;
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
wcscpy_s(strDestPath, cchDest, strFilename);
if (GetFileAttributes(strDestPath) != 0xFFFFFFFF)
return true;
2018-01-30 16:45:38 +08:00
2018-02-01 22:07:44 +08:00
// Search all parent directories starting at .\ and using strFilename as the leaf name
2018-02-28 19:17:15 +08:00
wchar_t strLeafName[MAX_PATH] = { 0 };
2018-02-01 22:07:44 +08:00
wcscpy_s(strLeafName, MAX_PATH, strFilename);
2018-02-28 19:17:15 +08:00
wchar_t strFullPath[MAX_PATH] = { 0 };
wchar_t strFullFileName[MAX_PATH] = { 0 };
wchar_t strSearch[MAX_PATH] = { 0 };
wchar_t* strFilePart = nullptr;
2018-02-01 22:07:44 +08:00
GetFullPathName(L".", MAX_PATH, strFullPath, &strFilePart);
if (strFilePart == nullptr)
return false;
while (strFilePart != nullptr && *strFilePart != '\0')
2018-01-30 16:45:38 +08:00
{
2018-02-01 22:07:44 +08:00
swprintf_s(strFullFileName, MAX_PATH, L"%s\\%s", strFullPath, strLeafName);
if (GetFileAttributes(strFullFileName) != 0xFFFFFFFF)
{
wcscpy_s(strDestPath, cchDest, strFullFileName);
bFound = true;
break;
}
swprintf_s(strFullFileName, MAX_PATH, L"%s\\%s\\%s", strFullPath, strExeName, strLeafName);
if (GetFileAttributes(strFullFileName) != 0xFFFFFFFF)
{
wcscpy_s(strDestPath, cchDest, strFullFileName);
bFound = true;
break;
}
swprintf_s(strSearch, MAX_PATH, L"%s\\..", strFullPath);
GetFullPathName(strSearch, MAX_PATH, strFullPath, &strFilePart);
2018-01-30 16:45:38 +08:00
}
2018-02-01 22:07:44 +08:00
if (bFound)
return true;
// ʧ<><CAA7>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ϊ·<CEAA><C2B7><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>ͬʱҲ<CAB1><D2B2><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
wcscpy_s(strDestPath, cchDest, strFilename);
return false;
2018-01-30 16:45:38 +08:00
}
2018-04-01 23:08:11 +08:00
bool e2d::Music::preload(const String& filePath)
2018-04-27 17:07:47 +08:00
{
UINT nRet = filePath.getHashCode();
2018-04-27 17:07:47 +08:00
if (GetMusicFileList().end() != GetMusicFileList().find(nRet))
2018-04-27 17:07:47 +08:00
{
return true;
}
else
{
MusicPlayer * pPlayer = new (std::nothrow) MusicPlayer();
if (pPlayer->open(filePath))
2018-04-27 17:07:47 +08:00
{
pPlayer->setVolume(s_fMusicVolume);
GetMusicFileList().insert(std::pair<UINT, MusicPlayer *>(nRet, pPlayer));
return true;
}
else
{
delete pPlayer;
pPlayer = nullptr;
}
}
return false;
}
bool e2d::Music::preload(int resNameId, const String& resType)
{
if (GetMusicResList().end() != GetMusicResList().find(resNameId))
{
return true;
}
else
{
MusicPlayer * pPlayer = new (std::nothrow) MusicPlayer();
if (pPlayer->open(resNameId, resType))
{
pPlayer->setVolume(s_fMusicVolume);
GetMusicResList().insert(std::pair<UINT, MusicPlayer *>(resNameId, pPlayer));
2018-04-27 17:07:47 +08:00
return true;
}
else
{
delete pPlayer;
pPlayer = nullptr;
}
}
return false;
}
bool e2d::Music::play(const String& filePath, int nLoopCount)
2018-04-27 17:07:47 +08:00
{
if (Music::preload(filePath))
2018-04-27 17:07:47 +08:00
{
UINT nRet = filePath.getHashCode();
auto pMusic = GetMusicFileList()[nRet];
if (pMusic->play(nLoopCount))
{
return true;
}
}
return false;
}
bool e2d::Music::play(int resNameId, const String& resType, int nLoopCount)
{
if (Music::preload(resNameId, resType))
{
auto pMusic = GetMusicResList()[resNameId];
2018-04-27 17:07:47 +08:00
if (pMusic->play(nLoopCount))
{
return true;
}
}
return false;
}
void e2d::Music::pause(const String& filePath)
2018-04-27 17:07:47 +08:00
{
if (filePath.isEmpty())
2018-04-27 17:07:47 +08:00
return;
UINT nRet = filePath.getHashCode();
2018-04-27 17:07:47 +08:00
if (GetMusicFileList().end() != GetMusicFileList().find(nRet))
GetMusicFileList()[nRet]->pause();
}
void e2d::Music::pause(int resNameId, const String& resType)
{
if (GetMusicResList().end() != GetMusicResList().find(resNameId))
GetMusicResList()[resNameId]->pause();
2018-04-27 17:07:47 +08:00
}
void e2d::Music::resume(const String& filePath)
2018-04-27 17:07:47 +08:00
{
if (filePath.isEmpty())
2018-04-27 17:07:47 +08:00
return;
UINT nRet = filePath.getHashCode();
2018-04-27 17:07:47 +08:00
if (GetMusicFileList().end() != GetMusicFileList().find(nRet))
GetMusicFileList()[nRet]->resume();
}
void e2d::Music::resume(int resNameId, const String& resType)
{
if (GetMusicResList().end() != GetMusicResList().find(resNameId))
GetMusicResList()[resNameId]->pause();
2018-04-27 17:07:47 +08:00
}
void e2d::Music::stop(const String& filePath)
2018-04-27 17:07:47 +08:00
{
if (filePath.isEmpty())
2018-04-27 17:07:47 +08:00
return;
UINT nRet = filePath.getHashCode();
2018-04-27 17:07:47 +08:00
if (GetMusicFileList().end() != GetMusicFileList().find(nRet))
GetMusicFileList()[nRet]->stop();
}
void e2d::Music::stop(int resNameId, const String& resType)
{
if (GetMusicResList().end() != GetMusicResList().find(resNameId))
GetMusicResList()[resNameId]->stop();
2018-04-27 17:07:47 +08:00
}
bool e2d::Music::isPlaying(const String& filePath)
2018-04-27 17:07:47 +08:00
{
if (filePath.isEmpty())
2018-04-27 17:07:47 +08:00
return false;
UINT nRet = filePath.getHashCode();
2018-04-27 17:07:47 +08:00
if (GetMusicFileList().end() != GetMusicFileList().find(nRet))
return GetMusicFileList()[nRet]->isPlaying();
return false;
}
2018-04-27 17:07:47 +08:00
bool e2d::Music::isPlaying(int resNameId, const String& resType)
{
if (GetMusicResList().end() != GetMusicResList().find(resNameId))
return GetMusicResList()[resNameId]->isPlaying();
2018-04-27 17:07:47 +08:00
return false;
}
double e2d::Music::getVolume()
{
return s_fMusicVolume;
}
void e2d::Music::setVolume(double fVolume)
{
s_fMusicVolume = min(max(static_cast<float>(fVolume), -224), 224);
for (auto pair : GetMusicFileList())
2018-04-27 17:07:47 +08:00
{
pair.second->setVolume(s_fMusicVolume);
}
}
void e2d::Music::pauseAll()
{
for (auto pair : GetMusicFileList())
2018-04-27 17:07:47 +08:00
{
pair.second->pause();
}
}
void e2d::Music::resumeAll()
{
for (auto pair : GetMusicFileList())
2018-04-27 17:07:47 +08:00
{
pair.second->resume();
}
}
void e2d::Music::stopAll()
{
for (auto pair : GetMusicFileList())
2018-04-27 17:07:47 +08:00
{
pair.second->stop();
}
}
IXAudio2 * e2d::Music::getIXAudio2()
{
return s_pXAudio2;
}
IXAudio2MasteringVoice * e2d::Music::getIXAudio2MasteringVoice()
{
return s_pMasteringVoice;
}
bool e2d::Music::__init()
{
HRESULT hr;
if (FAILED(hr = XAudio2Create(&s_pXAudio2, 0)))
{
WARN_IF(true, "Failed to init XAudio2 engine");
return false;
}
if (FAILED(hr = s_pXAudio2->CreateMasteringVoice(&s_pMasteringVoice)))
{
WARN_IF(true, "Failed creating mastering voice");
e2d::SafeReleaseInterface(&s_pXAudio2);
return false;
}
return true;
}
void e2d::Music::__uninit()
{
for (auto pair : GetMusicFileList())
2018-04-27 17:07:47 +08:00
{
pair.second->close();
delete pair.second;
}
GetMusicFileList().clear();
2018-04-27 17:07:47 +08:00
if (s_pMasteringVoice)
{
s_pMasteringVoice->DestroyVoice();
}
e2d::SafeReleaseInterface(&s_pXAudio2);
}