2010-10-18 15:07:55 +08:00
|
|
|
#include "SoundDataManager.h"
|
2010-10-21 10:21:24 +08:00
|
|
|
#include "FileUtils.h"
|
|
|
|
#include "SoundPlayer.h"
|
2010-10-18 15:07:55 +08:00
|
|
|
|
|
|
|
#define BREAK_IF(cond) if (cond) break;
|
|
|
|
|
2010-12-15 18:28:18 +08:00
|
|
|
namespace CocosDenshion {
|
|
|
|
|
2010-10-18 15:07:55 +08:00
|
|
|
SoundDataManager::SoundDataManager()
|
|
|
|
: m_pEffects(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SoundDataManager::~SoundDataManager()
|
|
|
|
{
|
|
|
|
removeAllEffects();
|
|
|
|
}
|
|
|
|
|
2011-01-14 17:42:37 +08:00
|
|
|
void SoundDataManager::loadSoundData(const char* pszFilePath)
|
2010-10-18 15:07:55 +08:00
|
|
|
{
|
2011-01-14 17:42:37 +08:00
|
|
|
do
|
2010-10-18 15:07:55 +08:00
|
|
|
{
|
2011-01-14 17:42:37 +08:00
|
|
|
BREAK_IF(! FileUtils::isFileExisted(pszFilePath));
|
2010-10-18 15:07:55 +08:00
|
|
|
|
2011-01-14 17:42:37 +08:00
|
|
|
// if we have loaded the file before,break
|
|
|
|
tEffectElement *pElement = NULL;
|
|
|
|
HASH_FIND_STR(m_pEffects, pszFilePath, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-10-18 15:07:55 +08:00
|
|
|
|
2011-01-14 17:42:37 +08:00
|
|
|
// load the file data
|
|
|
|
unsigned long nBufferSize = 0;
|
|
|
|
unsigned char* buffer = FileUtils::getFileData(pszFilePath, "rb", &nBufferSize);
|
|
|
|
BREAK_IF(!buffer || nBufferSize <= 0);
|
2010-10-18 15:07:55 +08:00
|
|
|
|
2011-01-14 17:42:37 +08:00
|
|
|
// add the data to hash map
|
|
|
|
pElement = (tEffectElement*)calloc(sizeof(*pElement), 1);
|
|
|
|
pElement->pDataBuffer = buffer;
|
|
|
|
pElement->nDataSize = nBufferSize;
|
|
|
|
strcpy(pElement->FileName, pszFilePath);
|
|
|
|
HASH_ADD_STR(m_pEffects, FileName, pElement);
|
|
|
|
} while (0);
|
2010-10-18 15:07:55 +08:00
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
tEffectElement* SoundDataManager::getSoundData(const char* pFileName)
|
2010-10-18 15:07:55 +08:00
|
|
|
{
|
|
|
|
tEffectElement* pElement = NULL;
|
2011-01-11 16:41:06 +08:00
|
|
|
HASH_FIND_STR(m_pEffects, pFileName, pElement);
|
2010-10-18 15:07:55 +08:00
|
|
|
|
|
|
|
return pElement;
|
|
|
|
}
|
|
|
|
|
2011-01-11 16:41:06 +08:00
|
|
|
void SoundDataManager::unloadEffect(const char* pFileName)
|
2010-10-18 15:07:55 +08:00
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
tEffectElement* pElement = NULL;
|
2011-01-11 16:41:06 +08:00
|
|
|
HASH_FIND_STR(m_pEffects, pFileName, pElement);
|
2010-10-18 15:07:55 +08:00
|
|
|
BREAK_IF(!pElement);
|
|
|
|
|
|
|
|
delete [] (pElement->pDataBuffer);
|
|
|
|
HASH_DEL(m_pEffects, pElement);
|
|
|
|
free(pElement);
|
|
|
|
} while (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundDataManager::removeAllEffects()
|
|
|
|
{
|
|
|
|
for (tEffectElement *pElement = m_pEffects; pElement != NULL; )
|
|
|
|
{
|
2011-01-11 16:41:06 +08:00
|
|
|
std::string fName = pElement->FileName;
|
2010-10-18 15:07:55 +08:00
|
|
|
pElement = (tEffectElement*)pElement->hh.next;
|
2011-01-11 16:41:06 +08:00
|
|
|
unloadEffect(fName.c_str());
|
2010-10-18 15:07:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 18:28:18 +08:00
|
|
|
} // end of namespace CocosDenshion
|