2010-09-29 17:29:43 +08:00
|
|
|
#include "SimpleAudioEngine.h"
|
2010-10-21 10:21:24 +08:00
|
|
|
#include "SoundPlayer.h"
|
|
|
|
#include "SoundDataManager.h"
|
2010-11-11 11:18:58 +08:00
|
|
|
#include "TSoundPlayer.h"
|
2010-12-11 11:08:03 +08:00
|
|
|
#include "FileUtils.h"
|
2010-09-29 17:29:43 +08:00
|
|
|
|
|
|
|
#define BREAK_IF(cond) if (cond) break;
|
|
|
|
|
2010-12-15 18:28:18 +08:00
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
static SimpleAudioEngine *s_pSharedAudioEngine = NULL;
|
2010-10-21 10:21:24 +08:00
|
|
|
static SoundDataManager *s_pDataManager = NULL;
|
|
|
|
static SoundPlayer *s_pBackPlayer = NULL;
|
2010-11-11 11:18:58 +08:00
|
|
|
static TSoundPlayer *s_pEffectPlayer = NULL;
|
2010-10-21 10:21:24 +08:00
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
static std::string s_strBackMusicName = "";
|
2011-01-22 16:22:26 +08:00
|
|
|
static float s_fBackgroundMusicVolume = 1.0f;
|
|
|
|
static float s_fEffectsVolume = 1.0f;
|
2011-01-11 16:41:06 +08:00
|
|
|
static bool s_bWillPlayBackgroundMusic = false;
|
2010-10-21 14:44:02 +08:00
|
|
|
|
2010-09-29 17:29:43 +08:00
|
|
|
SimpleAudioEngine::SimpleAudioEngine()
|
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
if (s_pEffectPlayer)
|
2010-10-21 10:21:24 +08:00
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
delete s_pEffectPlayer;
|
2010-10-21 10:21:24 +08:00
|
|
|
}
|
2010-11-11 11:18:58 +08:00
|
|
|
s_pEffectPlayer = new TSoundPlayer();
|
2010-10-21 10:21:24 +08:00
|
|
|
|
|
|
|
if (s_pBackPlayer)
|
|
|
|
{
|
|
|
|
delete s_pBackPlayer;
|
|
|
|
}
|
|
|
|
s_pBackPlayer = new SoundPlayer();
|
2011-01-22 16:22:26 +08:00
|
|
|
setBackgroundMusicVolume(s_fBackgroundMusicVolume);
|
2010-10-21 10:21:24 +08:00
|
|
|
|
|
|
|
if (s_pDataManager)
|
|
|
|
{
|
|
|
|
delete s_pDataManager;
|
|
|
|
}
|
|
|
|
s_pDataManager = new SoundDataManager();
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::~SimpleAudioEngine()
|
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
if (s_pEffectPlayer)
|
|
|
|
{
|
|
|
|
delete s_pEffectPlayer;
|
|
|
|
s_pEffectPlayer = NULL;
|
|
|
|
}
|
2010-10-16 16:25:09 +08:00
|
|
|
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
delete s_pBackPlayer;
|
|
|
|
s_pBackPlayer = NULL;
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
2010-10-18 10:34:09 +08:00
|
|
|
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pDataManager)
|
2010-10-18 10:34:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
delete s_pDataManager;
|
|
|
|
s_pDataManager = NULL;
|
2010-10-18 10:34:09 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2010-11-16 15:12:09 +08:00
|
|
|
SimpleAudioEngine* SimpleAudioEngine::sharedEngine()
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
if (s_pSharedAudioEngine == NULL)
|
2010-10-18 10:34:09 +08:00
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
s_pSharedAudioEngine = new SimpleAudioEngine;
|
2010-10-18 10:34:09 +08:00
|
|
|
}
|
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
return s_pSharedAudioEngine;
|
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
void SimpleAudioEngine::end()
|
2010-11-11 11:18:58 +08:00
|
|
|
{
|
|
|
|
if (s_pSharedAudioEngine)
|
|
|
|
{
|
|
|
|
delete s_pSharedAudioEngine;
|
|
|
|
s_pSharedAudioEngine = NULL;
|
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-25 14:26:26 +08:00
|
|
|
void SimpleAudioEngine::setResource(const char* pszZipFileName, const char* pszResPath)
|
2010-12-11 11:08:03 +08:00
|
|
|
{
|
2011-01-18 18:21:06 +08:00
|
|
|
FileUtils::setResource(pszResPath, pszZipFileName);
|
2010-12-11 11:08:03 +08:00
|
|
|
}
|
|
|
|
|
2010-09-29 17:29:43 +08:00
|
|
|
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
|
|
|
{
|
2010-10-13 14:18:03 +08:00
|
|
|
int nTimes = 1;
|
2010-09-29 17:29:43 +08:00
|
|
|
if (bLoop)
|
|
|
|
{
|
2010-10-13 14:18:03 +08:00
|
|
|
nTimes = -1;
|
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
s_strBackMusicName = pszFilePath;
|
|
|
|
s_pDataManager->loadSoundData(pszFilePath);
|
|
|
|
tEffectElement* pElement = s_pDataManager->getSoundData(pszFilePath);
|
2010-10-13 14:18:03 +08:00
|
|
|
|
2010-10-18 15:07:55 +08:00
|
|
|
if (pElement)
|
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
s_pBackPlayer->PlaySoundFromMem(pElement->pDataBuffer, pElement->nDataSize, pElement->FileName, nTimes);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 14:37:11 +08:00
|
|
|
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
s_pBackPlayer->Stop();
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
2010-11-30 14:37:11 +08:00
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
if (bReleaseData && s_strBackMusicName.length())
|
2010-11-30 14:37:11 +08:00
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
s_pDataManager->unloadEffect(s_strBackMusicName.c_str());
|
|
|
|
s_strBackMusicName = "";
|
2010-11-30 14:37:11 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
s_pBackPlayer->Pause();
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
s_pBackPlayer->Resume();
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::rewindBackgroundMusic()
|
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
s_pBackPlayer->Rewind();
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
|
|
|
{
|
2010-10-21 14:44:02 +08:00
|
|
|
return s_bWillPlayBackgroundMusic;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
|
|
|
{
|
2010-10-16 16:25:09 +08:00
|
|
|
bool bRet = false;
|
|
|
|
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2010-10-21 10:21:24 +08:00
|
|
|
bRet = s_pBackPlayer->IsPlaying();
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// properties
|
2011-01-21 15:14:07 +08:00
|
|
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
return s_fBackgroundMusicVolume;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:14:07 +08:00
|
|
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
if (volume > 1.0f)
|
2010-10-21 14:44:02 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
volume = 1.0f;
|
2010-10-21 14:44:02 +08:00
|
|
|
}
|
2011-01-22 16:22:26 +08:00
|
|
|
else if (volume < 0.0f)
|
2010-10-21 14:44:02 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
volume = 0.0f;
|
2010-10-21 14:44:02 +08:00
|
|
|
}
|
|
|
|
|
2010-10-21 10:21:24 +08:00
|
|
|
if (s_pBackPlayer)
|
2010-10-16 16:25:09 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
s_pBackPlayer->SetVolumeValue((Int32) (volume * 100));
|
2010-10-16 16:25:09 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 16:22:26 +08:00
|
|
|
s_fBackgroundMusicVolume = volume;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:14:07 +08:00
|
|
|
float SimpleAudioEngine::getEffectsVolume()
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
return s_fEffectsVolume;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:14:07 +08:00
|
|
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
if (volume > 1.0f)
|
2010-10-21 14:44:02 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
volume = 1.0f;
|
2010-10-21 14:44:02 +08:00
|
|
|
}
|
2011-01-22 16:22:26 +08:00
|
|
|
else if (volume < 0.0f)
|
2010-10-21 14:44:02 +08:00
|
|
|
{
|
2011-01-22 16:22:26 +08:00
|
|
|
volume = 0.0f;
|
2010-10-21 14:44:02 +08:00
|
|
|
}
|
|
|
|
|
2011-01-22 16:22:26 +08:00
|
|
|
s_fEffectsVolume = volume;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for sound effects
|
2011-01-11 16:41:06 +08:00
|
|
|
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
preloadEffect(pszFilePath);
|
|
|
|
int nRet = -1;
|
2010-09-29 17:29:43 +08:00
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
do
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
tEffectElement* pElement = s_pDataManager->getSoundData(pszFilePath);
|
|
|
|
BREAK_IF(! pElement);
|
|
|
|
|
|
|
|
TSoundPlayParameter soundParam;
|
|
|
|
soundParam.pSoundData = pElement->pDataBuffer;
|
|
|
|
soundParam.dataLen = pElement->nDataSize;
|
|
|
|
soundParam.dataType = SOUND_TYPE_WAVE;
|
2011-01-22 16:22:26 +08:00
|
|
|
soundParam.volume = (int) (0xFFFF * s_fEffectsVolume);
|
2011-01-11 16:41:06 +08:00
|
|
|
|
|
|
|
nRet = s_pEffectPlayer->Play(soundParam);
|
|
|
|
} while (0);
|
2010-09-29 17:29:43 +08:00
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
return (unsigned int) nRet;
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2010-11-11 11:18:58 +08:00
|
|
|
do
|
2010-10-13 14:18:03 +08:00
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
int nID = (int) nSoundId;
|
2010-11-11 11:18:58 +08:00
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
BREAK_IF(nID < 0);
|
|
|
|
s_pEffectPlayer->Stop(nID);
|
2010-11-11 11:18:58 +08:00
|
|
|
} while (0);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-18 18:21:06 +08:00
|
|
|
s_pDataManager->loadSoundData(pszFilePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
s_pDataManager->loadSoundData(pszFilePath);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
2010-09-29 17:29:43 +08:00
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
s_pDataManager->unloadEffect(pszFilePath);
|
2010-09-29 17:29:43 +08:00
|
|
|
}
|
|
|
|
|
2011-01-21 15:14:07 +08:00
|
|
|
// void SimpleAudioEngine::unloadEffectAll()
|
|
|
|
// {
|
|
|
|
// s_pDataManager->removeAllEffects();
|
|
|
|
// }
|
2010-12-15 18:28:18 +08:00
|
|
|
|
|
|
|
} // end of namespace CocosDenshion
|