Magic_Game/Easy2D/Tool/MusicUtils.cpp

417 lines
7.8 KiB
C++
Raw Normal View History

2017-09-27 17:56:28 +08:00
#include "..\easy2d.h"
2017-09-10 23:56:52 +08:00
#include <mmsystem.h>
#pragma comment(lib , "winmm.lib")
2017-09-27 17:56:28 +08:00
#include <Digitalv.h>
2017-09-10 23:56:52 +08:00
#include <map>
////////////////////////////////////////////////////////////////////
// MciPlayer
////////////////////////////////////////////////////////////////////
class MciPlayer
{
public:
MciPlayer();
~MciPlayer();
2017-09-20 14:52:50 +08:00
void close();
void open(TString pFileName, UINT uId);
2017-09-20 14:52:50 +08:00
void play(bool bLoop = false);
void pause();
void resume();
void stop();
void rewind();
void setVolume(float volume);
bool isPlaying();
UINT getSoundID();
2017-09-10 23:56:52 +08:00
private:
2017-09-20 14:52:50 +08:00
void _sendCommand(int nCommand, DWORD_PTR param1 = 0, DWORD_PTR parma2 = 0);
2017-09-10 23:56:52 +08:00
2017-09-20 14:52:50 +08:00
MCIDEVICEID m_dev;
UINT m_nSoundID;
bool m_bPlaying;
bool m_bLoop;
TString m_sExt;
2017-09-10 23:56:52 +08:00
};
2017-09-20 14:52:50 +08:00
MciPlayer::MciPlayer() :
m_dev(0L),
m_nSoundID(0),
m_bPlaying(false),
m_bLoop(false),
m_sExt(_T(""))
2017-09-10 23:56:52 +08:00
{
}
MciPlayer::~MciPlayer()
{
2017-09-20 14:52:50 +08:00
close(); // <20>رղ<D8B1><D5B2><EFBFBD><EFBFBD><EFBFBD>
2017-09-10 23:56:52 +08:00
}
void MciPlayer::open(TString pFileName, UINT uId)
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>ļ<EFBFBD>
if (pFileName.empty() || !PathFileExists(pFileName.c_str())) return;
// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>׺<EFBFBD><D7BA>
m_sExt = FileUtils::getFileExtension(pFileName);
// ֹͣ<CDA3><D6B9>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
close();
2017-09-10 23:56:52 +08:00
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD> MCI_OPEN_PARMS <20><><EFBFBD><EFBFBD>
2017-09-10 23:56:52 +08:00
MCI_OPEN_PARMS mciOpen = { 0 };
mciOpen.lpstrDeviceType = (LPCTSTR)-1; // device ID for "all devices"
2017-09-10 23:56:52 +08:00
mciOpen.lpstrElementName = pFileName.c_str();
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
MCIERROR mciError;
2017-09-10 23:56:52 +08:00
mciError = mciSendCommand(0, MCI_OPEN, MCI_OPEN_ELEMENT, reinterpret_cast<DWORD_PTR>(&mciOpen));
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
2017-09-10 23:56:52 +08:00
if (mciError) return;
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>Ϣ
m_dev = mciOpen.wDeviceID;
m_nSoundID = uId;
m_bPlaying = false;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MciPlayer::play(bool bLoop)
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
2017-09-10 23:56:52 +08:00
{
return;
}
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>ò<EFBFBD><C3B2>Ų<EFBFBD><C5B2><EFBFBD>
2017-09-10 23:56:52 +08:00
MCI_PLAY_PARMS mciPlay = { 0 };
2017-09-20 14:52:50 +08:00
MCIERROR s_mciError;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
s_mciError = mciSendCommand(m_dev, MCI_PLAY, MCI_FROM | (bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay));
// δ<><CEB4><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> m_bPlaying Ϊ true
2017-09-10 23:56:52 +08:00
if (!s_mciError)
{
2017-09-20 14:52:50 +08:00
m_bPlaying = true;
m_bLoop = bLoop;
2017-09-10 23:56:52 +08:00
}
}
2017-09-20 14:52:50 +08:00
void MciPlayer::close()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
if (m_bPlaying)
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
stop();
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
// <20>ر<EFBFBD><D8B1>
if (m_dev)
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
_sendCommand(MCI_CLOSE);
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
// <20>ָ<EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
m_dev = 0;
m_bPlaying = false;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MciPlayer::pause()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
_sendCommand(MCI_PAUSE);
m_bPlaying = false;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MciPlayer::resume()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (m_sExt == _T(".mid"))
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// midi <20><>֧<EFBFBD><D6A7> MCI_RESUME <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧʹ<D3A6><CAB9> MCI_FROM <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC><CEBB>
// <20><>ȡ MCI ״̬
2017-09-10 23:56:52 +08:00
MCI_STATUS_PARMS mciStatusParms;
mciStatusParms.dwItem = MCI_STATUS_POSITION;
2017-09-20 14:52:50 +08:00
_sendCommand(MCI_STATUS, MCI_STATUS_ITEM, reinterpret_cast<DWORD_PTR>(&mciStatusParms));
// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
MCI_PLAY_PARMS mciPlayParms;
mciPlayParms.dwFrom = (DWORD)mciStatusParms.dwReturn;
2017-09-20 14:52:50 +08:00
_sendCommand(MCI_PLAY, MCI_FROM, reinterpret_cast<DWORD_PTR>(&mciPlayParms));
2017-09-10 23:56:52 +08:00
}
else
{
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
_sendCommand(MCI_RESUME);
m_bPlaying = true;
2017-09-10 23:56:52 +08:00
}
}
2017-09-20 14:52:50 +08:00
void MciPlayer::stop()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD>
_sendCommand(MCI_STOP);
m_bPlaying = false;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MciPlayer::rewind()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20>豸Ϊ<E8B1B8><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
2017-09-10 23:56:52 +08:00
{
return;
}
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>λ<EFBFBD><CEBB>
mciSendCommand(m_dev, MCI_SEEK, MCI_SEEK_TO_START, 0);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-09-10 23:56:52 +08:00
MCI_PLAY_PARMS mciPlay = { 0 };
2017-09-20 14:52:50 +08:00
m_bPlaying = mciSendCommand(m_dev, MCI_PLAY, (m_bLoop ? MCI_DGV_PLAY_REPEAT : 0), reinterpret_cast<DWORD_PTR>(&mciPlay)) ? false : true;
}
void MciPlayer::setVolume(float volume)
{
volume = min(max(volume, 0), 1);
MCI_DGV_SETAUDIO_PARMS mciSetAudioPara = { 0 };
mciSetAudioPara.dwItem = MCI_DGV_SETAUDIO_VOLUME;
mciSetAudioPara.dwValue = DWORD(1000 * volume);
mciSendCommand(m_dev, MCI_SETAUDIO, MCI_DGV_SETAUDIO_VALUE | MCI_DGV_SETAUDIO_ITEM, (DWORD_PTR)&mciSetAudioPara);
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
bool MciPlayer::isPlaying()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
return m_bPlaying;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
UINT MciPlayer::getSoundID()
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
return m_nSoundID;
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MciPlayer::_sendCommand(int nCommand, DWORD_PTR param1, DWORD_PTR parma2)
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>豸ʱ<E8B1B8><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2><EFBFBD>
if (!m_dev)
2017-09-10 23:56:52 +08:00
{
return;
}
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><E8B1B8><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>
mciSendCommand(m_dev, nCommand, param1, parma2);
2017-09-10 23:56:52 +08:00
}
////////////////////////////////////////////////////////////////////
// MusicUtils
////////////////////////////////////////////////////////////////////
typedef std::map<unsigned int, MciPlayer *> MusicList;
typedef std::pair<unsigned int, MciPlayer *> Music;
static unsigned int _Hash(TString key);
2017-09-10 23:56:52 +08:00
2017-09-20 14:52:50 +08:00
static MusicList& getMciPlayerList()
2017-09-10 23:56:52 +08:00
{
static MusicList s_List;
return s_List;
}
2017-09-20 14:52:50 +08:00
static MciPlayer& getBgMciPlayer()
2017-09-10 23:56:52 +08:00
{
static MciPlayer s_Music;
return s_Music;
}
void MusicUtils::end()
{
2017-09-20 14:52:50 +08:00
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
getBgMciPlayer().close();
// ֹͣ<CDA3><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (auto& iter : getMciPlayerList())
2017-09-10 23:56:52 +08:00
{
SafeDelete(iter.second);
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
getMciPlayerList().clear();
2017-09-10 23:56:52 +08:00
return;
}
2017-09-16 23:09:11 +08:00
void MusicUtils::setVolume(float volume)
{
2017-09-20 14:52:50 +08:00
// <20><><EFBFBD>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
getBgMciPlayer().setVolume(volume);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (auto& iter : getMciPlayerList())
{
iter.second->setVolume(volume);
}
}
void MusicUtils::setVolume(TString pszFilePath, float volume)
2017-09-20 14:52:50 +08:00
{
unsigned int nRet = ::_Hash(pszFilePath);
MusicList::iterator p = getMciPlayerList().find(nRet);
if (p != getMciPlayerList().end())
{
p->second->setVolume(volume);
}
2017-09-16 23:09:11 +08:00
}
void MusicUtils::playBackgroundMusic(TString pszFilePath, bool bLoop)
2017-09-10 23:56:52 +08:00
{
if (pszFilePath.empty())
{
return;
}
2017-09-20 14:52:50 +08:00
getBgMciPlayer().open(pszFilePath, ::_Hash(pszFilePath));
getBgMciPlayer().play(bLoop);
2017-09-10 23:56:52 +08:00
}
void MusicUtils::stopBackgroundMusic(bool bReleaseData)
{
if (bReleaseData)
{
2017-09-20 14:52:50 +08:00
getBgMciPlayer().close();
2017-09-10 23:56:52 +08:00
}
else
{
2017-09-20 14:52:50 +08:00
getBgMciPlayer().stop();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::pauseBackgroundMusic()
{
2017-09-20 14:52:50 +08:00
getBgMciPlayer().pause();
2017-09-10 23:56:52 +08:00
}
void MusicUtils::resumeBackgroundMusic()
{
2017-09-20 14:52:50 +08:00
getBgMciPlayer().resume();
2017-09-10 23:56:52 +08:00
}
void MusicUtils::rewindBackgroundMusic()
{
2017-09-20 14:52:50 +08:00
getBgMciPlayer().rewind();
2017-09-10 23:56:52 +08:00
}
bool MusicUtils::isBackgroundMusicPlaying()
{
2017-09-20 14:52:50 +08:00
return getBgMciPlayer().isPlaying();
2017-09-10 23:56:52 +08:00
}
2017-09-20 14:52:50 +08:00
void MusicUtils::setBackgroundMusicVolume(float volume)
{
getBgMciPlayer().setVolume(volume);
}
2017-09-10 23:56:52 +08:00
unsigned int MusicUtils::playMusic(TString pszFilePath, bool bLoop)
2017-09-10 23:56:52 +08:00
{
unsigned int nRet = ::_Hash(pszFilePath);
preloadMusic(pszFilePath);
2017-09-20 14:52:50 +08:00
MusicList::iterator p = getMciPlayerList().find(nRet);
if (p != getMciPlayerList().end())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
p->second->play(bLoop);
2017-09-10 23:56:52 +08:00
}
return nRet;
}
void MusicUtils::stopMusic(unsigned int nSoundId)
{
2017-09-20 14:52:50 +08:00
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
p->second->stop();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::preloadMusic(TString pszFilePath)
2017-09-10 23:56:52 +08:00
{
if (pszFilePath.empty()) return;
int nRet = ::_Hash(pszFilePath);
2017-09-20 14:52:50 +08:00
if (getMciPlayerList().end() != getMciPlayerList().find(nRet)) return;
2017-09-10 23:56:52 +08:00
2017-09-20 14:52:50 +08:00
getMciPlayerList().insert(Music(nRet, new MciPlayer()));
MciPlayer * pPlayer = getMciPlayerList()[nRet];
pPlayer->open(pszFilePath, nRet);
2017-09-10 23:56:52 +08:00
2017-09-20 14:52:50 +08:00
if (nRet == pPlayer->getSoundID()) return;
2017-09-10 23:56:52 +08:00
delete pPlayer;
2017-09-20 14:52:50 +08:00
getMciPlayerList().erase(nRet);
2017-09-10 23:56:52 +08:00
nRet = 0;
}
void MusicUtils::pauseMusic(unsigned int nSoundId)
{
2017-09-20 14:52:50 +08:00
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
p->second->pause();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::pauseAllMusics()
{
2017-09-20 14:52:50 +08:00
for (auto& iter : getMciPlayerList())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
iter.second->pause();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::resumeMusic(unsigned int nSoundId)
{
2017-09-20 14:52:50 +08:00
MusicList::iterator p = getMciPlayerList().find(nSoundId);
if (p != getMciPlayerList().end())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
p->second->resume();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::resumeAllMusics()
{
2017-09-20 14:52:50 +08:00
for (auto& iter : getMciPlayerList())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
iter.second->resume();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::stopAllMusics()
{
2017-09-20 14:52:50 +08:00
for (auto& iter : getMciPlayerList())
2017-09-10 23:56:52 +08:00
{
2017-09-20 14:52:50 +08:00
iter.second->stop();
2017-09-10 23:56:52 +08:00
}
}
void MusicUtils::unloadMusic(LPCTSTR pszFilePath)
{
unsigned int nID = ::_Hash(pszFilePath);
2017-09-20 14:52:50 +08:00
MusicList::iterator p = getMciPlayerList().find(nID);
if (p != getMciPlayerList().end())
2017-09-10 23:56:52 +08:00
{
SafeDelete(p->second);
2017-09-20 14:52:50 +08:00
getMciPlayerList().erase(nID);
2017-09-10 23:56:52 +08:00
}
}
unsigned int _Hash(TString key)
2017-09-10 23:56:52 +08:00
{
unsigned int len = unsigned(key.size());
unsigned int hash = 0;
for (unsigned i = 0; i < len; i++)
{
hash *= 16777619;
hash ^= (unsigned int)(unsigned char)toupper(key[i]);
}
return (hash);
}