reconstruct the SimpleAudioEngine
This commit is contained in:
natural-law 2010-10-21 02:21:24 +00:00
parent 7be5cca13a
commit 27f125cc77
15 changed files with 135 additions and 155 deletions

View File

@ -131,7 +131,7 @@ bool HelloWorld::init()
// Start up the background music
SimpleAudioEngine::getSharedEngine()->SetBackgroundMusicVolume(40);
SimpleAudioEngine::getSharedEngine()->playBackgroundMusic(SoundResInfo[0].FileName.c_str(), true);
SimpleAudioEngine::getSharedEngine()->playBackgroundMusic(SoundResInfo[0].FileName, true);
return true;
}
@ -226,7 +226,7 @@ void HelloWorld::ccTouchesEnded(NSSet* touches, UIEvent* event)
// Play a sound!
// SimpleAudioEngine::getSharedEngine()->SetEffectsVolume(30);
// SimpleAudioEngine::getSharedEngine()->playEffect(SoundResInfo[1].FileName.c_str());
// SimpleAudioEngine::getSharedEngine()->playEffect(SoundResInfo[1].FileName);
// Determine where we wish to shoot the projectile to
float realX = winSize.width + (projectile->getContentSize().width / 2);
@ -257,7 +257,7 @@ HelloWorld::HelloWorld()
,_projectiles(NULL)
,_projectilesDestroyed(0)
{
SimpleAudioEngine::getSharedEngine()->preloadEffect(SoundResInfo[1].FileName.c_str());
SimpleAudioEngine::getSharedEngine()->preloadEffect(SoundResInfo[1].FileName);
}
// on "dealloc" you need to release all your retained objects

View File

@ -2,7 +2,7 @@
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="CocosDenshion"
Name="CocosDenshion_uphone"
ProjectGUID="{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}"
RootNamespace="CocosDenshion"
Keyword="Win32Proj"
@ -168,46 +168,6 @@
<References>
</References>
<Files>
<Filter
Name="SimpleAudioEngine"
>
<File
RelativePath=".\SimpleAudioEngine\SimpleAudioEngine.cpp"
>
</File>
</Filter>
<Filter
Name="Platform"
>
<File
RelativePath=".\Platform\platform.h"
>
</File>
<Filter
Name="uPhone"
>
<File
RelativePath=".\Platform\uPhone\FileUtils.h"
>
</File>
<File
RelativePath=".\Platform\uPhone\ResourceHandle.cpp"
>
</File>
<File
RelativePath=".\Platform\uPhone\ResourceHandle.h"
>
</File>
<File
RelativePath=".\Platform\uPhone\SoundPlayer.cpp"
>
</File>
<File
RelativePath=".\Platform\uPhone\SoundPlayer.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="include"
>
@ -215,20 +175,44 @@
RelativePath=".\include\SimpleAudioEngine.h"
>
</File>
<File
RelativePath=".\include\SoundDataManager.cpp"
>
</File>
<File
RelativePath=".\include\SoundDataManager.h"
>
</File>
</Filter>
<Filter
Name="Support"
Name="source"
>
<File
RelativePath=".\Support\uthash.h"
RelativePath=".\uphone\FileUtils.h"
>
</File>
<File
RelativePath=".\uphone\ResourceHandle.cpp"
>
</File>
<File
RelativePath=".\uphone\ResourceHandle.h"
>
</File>
<File
RelativePath=".\uphone\SimpleAudioEngine.cpp"
>
</File>
<File
RelativePath=".\uphone\SoundDataManager.cpp"
>
</File>
<File
RelativePath=".\uphone\SoundDataManager.h"
>
</File>
<File
RelativePath=".\uphone\SoundPlayer.cpp"
>
</File>
<File
RelativePath=".\uphone\SoundPlayer.h"
>
</File>
<File
RelativePath=".\uphone\uthash.h"
>
</File>
</Filter>

View File

@ -1,14 +0,0 @@
#ifndef __DENSHION_PLATFORM_H__
#define __DENSHION_PLATFORM_H__
#if defined(_TRANZDA_VM_) || defined(CCX_UNDER_UPHONE)
#define _PLATFORM_UPHONE
#endif
#ifdef _PLATFORM_UPHONE
#include "uPhone/SoundPlayer.h"
#include "uPhone/FileUtils.h"
#include "uPhone/ResourceHandle.h"
#endif
#endif

View File

@ -25,21 +25,20 @@ THE SOFTWARE.
#ifndef _SIMPLE_AUDIO_ENGINE_H_
#define _SIMPLE_AUDIO_ENGINE_H_
#include "../Platform/platform.h"
#include "SoundDataManager.h"
#include <vector>
#include "../Export.h"
typedef struct _tResourceInfo
{
const char* FileName;
int nResID;
} T_SoundResInfo;
/*!***************************************************************************
@class SimpleAudioEngine
@brief offer a VERY simple interface to play background music & sound effect
*****************************************************************************/
class EXPORT_DLL SimpleAudioEngine
{
public:
typedef std::vector<SoundPlayer*> PlayerArray;
typedef PlayerArray::iterator PlayerArrayIterator;
public:
SimpleAudioEngine();
~SimpleAudioEngine();
@ -70,7 +69,6 @@ public:
int GetEffectsVolume();
void SetEffectsVolume(int volume);
// for sound effects
int playEffect(const char* pszFilePath);
void stopEffect(int nSoundId);
@ -99,17 +97,10 @@ public:
*****************************************************************************/
void playPreloadedEffect(int nSoundId);
void removeAllEffectPlayers();
protected:
int m_nBackgroundMusicVolume;
int m_nEffectsVolume;
bool m_bWillPlayBackgroundMusic;
SoundDataManager* m_pDataManager;
SoundPlayer* m_pBackPlayer;
PlayerArray* m_pEffectPlayers;
};
#endif // _SIMPLE_AUDIO_ENGINE_H_

View File

@ -1,33 +1,72 @@
#include "SimpleAudioEngine.h"
#include "SoundPlayer.h"
#include "SoundDataManager.h"
#include <vector>
typedef std::vector<SoundPlayer*> PlayerArray;
typedef PlayerArray::iterator PlayerArrayIterator;
#define BREAK_IF(cond) if (cond) break;
static SimpleAudioEngine *s_pSharedAudioEngie = NULL;
static PlayerArray *s_pEffectPlayers = NULL;
static SoundDataManager *s_pDataManager = NULL;
static SoundPlayer *s_pBackPlayer = NULL;
void removeAllEffectPlayers()
{
PlayerArrayIterator iter;
for (iter = s_pEffectPlayers->begin(); iter != s_pEffectPlayers->end(); ++iter)
{
if (*iter)
{
delete *iter;
}
}
s_pEffectPlayers->clear();
delete s_pEffectPlayers;
}
SimpleAudioEngine::SimpleAudioEngine()
: m_nBackgroundMusicVolume(100)
, m_nEffectsVolume(100)
, m_bWillPlayBackgroundMusic(false)
{
m_pEffectPlayers = new PlayerArray();
m_pBackPlayer = new SoundPlayer();
m_pDataManager = new SoundDataManager();
if (s_pEffectPlayers)
{
removeAllEffectPlayers();
}
s_pEffectPlayers = new PlayerArray();
if (s_pBackPlayer)
{
delete s_pBackPlayer;
}
s_pBackPlayer = new SoundPlayer();
if (s_pDataManager)
{
delete s_pDataManager;
}
s_pDataManager = new SoundDataManager();
}
SimpleAudioEngine::~SimpleAudioEngine()
{
removeAllEffectPlayers();
if (m_pBackPlayer)
if (s_pBackPlayer)
{
delete m_pBackPlayer;
m_pBackPlayer = NULL;
delete s_pBackPlayer;
s_pBackPlayer = NULL;
}
if (m_pDataManager)
if (s_pDataManager)
{
delete m_pDataManager;
m_pDataManager = NULL;
delete s_pDataManager;
s_pDataManager = NULL;
}
}
@ -49,44 +88,44 @@ void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
nTimes = -1;
}
int nSoundID = m_pDataManager->loadSoundData(pszFilePath);
tEffectElement* pElement = m_pDataManager->getSoundData(nSoundID);
int nSoundID = s_pDataManager->loadSoundData(pszFilePath);
tEffectElement* pElement = s_pDataManager->getSoundData(nSoundID);
if (pElement)
{
m_pBackPlayer->PlaySoundFromMem(pElement->pDataBuffer, pElement->nDataSize, pElement->FileName, nTimes);
s_pBackPlayer->PlaySoundFromMem(pElement->pDataBuffer, pElement->nDataSize, pElement->FileName, nTimes);
}
}
void SimpleAudioEngine::stopBackgroundMusic()
{
if (m_pBackPlayer)
if (s_pBackPlayer)
{
m_pBackPlayer->Stop();
s_pBackPlayer->Stop();
}
}
void SimpleAudioEngine::pauseBackgroundMusic()
{
if (m_pBackPlayer)
if (s_pBackPlayer)
{
m_pBackPlayer->Pause();
s_pBackPlayer->Pause();
}
}
void SimpleAudioEngine::resumeBackgroundMusic()
{
if (m_pBackPlayer)
if (s_pBackPlayer)
{
m_pBackPlayer->Resume();
s_pBackPlayer->Resume();
}
}
void SimpleAudioEngine::rewindBackgroundMusic()
{
if (m_pBackPlayer)
if (s_pBackPlayer)
{
m_pBackPlayer->Rewind();
s_pBackPlayer->Rewind();
}
}
@ -99,9 +138,9 @@ bool SimpleAudioEngine::isBackgroundMusicPlaying()
{
bool bRet = false;
if (m_pBackPlayer)
if (s_pBackPlayer)
{
bRet = m_pBackPlayer->IsPlaying();
bRet = s_pBackPlayer->IsPlaying();
}
return bRet;
@ -115,9 +154,9 @@ int SimpleAudioEngine::GetBackgroundMusicVolume()
void SimpleAudioEngine::SetBackgroundMusicVolume(int volume)
{
if (m_pBackPlayer)
if (s_pBackPlayer)
{
m_pBackPlayer->SetVolumeValue(volume);
s_pBackPlayer->SetVolumeValue(volume);
}
m_nBackgroundMusicVolume = volume;
@ -132,7 +171,7 @@ void SimpleAudioEngine::SetEffectsVolume(int volume)
{
PlayerArrayIterator iter;
for (iter = m_pEffectPlayers->begin(); iter != m_pEffectPlayers->end(); ++iter)
for (iter = s_pEffectPlayers->begin(); iter != s_pEffectPlayers->end(); ++iter)
{
if (*iter)
{
@ -163,7 +202,7 @@ void SimpleAudioEngine::stopEffect(int nSoundId)
// find the players are playing the effect
PlayerArrayIterator iter;
for (iter = m_pEffectPlayers->begin(); iter != m_pEffectPlayers->end(); ++iter)
for (iter = s_pEffectPlayers->begin(); iter != s_pEffectPlayers->end(); ++iter)
{
if ((*iter) && pPlayer->GetCurrentSoundID() == nSoundId)
{
@ -174,27 +213,27 @@ void SimpleAudioEngine::stopEffect(int nSoundId)
int SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
return m_pDataManager->loadSoundData(pszFilePath);
return s_pDataManager->loadSoundData(pszFilePath);
}
void SimpleAudioEngine::unloadEffect(int nSoundId)
{
m_pDataManager->unloadEffect(nSoundId);
s_pDataManager->unloadEffect(nSoundId);
}
void SimpleAudioEngine::playPreloadedEffect(int nSoundId)
{
do
{
tEffectElement* pElement = m_pDataManager->getSoundData(nSoundId);
tEffectElement* pElement = s_pDataManager->getSoundData(nSoundId);
BREAK_IF(! pElement);
SoundPlayer* pPlayer = NULL;
bool bLoaded = false;
// find the not playing player in m_pEffectPlayers
// find the not playing player in s_pEffectPlayers
PlayerArrayIterator iter;
for (iter = m_pEffectPlayers->begin(); iter != m_pEffectPlayers->end(); ++iter)
for (iter = s_pEffectPlayers->begin(); iter != s_pEffectPlayers->end(); ++iter)
{
if ((*iter) && !(*iter)->IsPlaying())
{
@ -211,7 +250,7 @@ void SimpleAudioEngine::playPreloadedEffect(int nSoundId)
if (!pPlayer)
{
pPlayer = new SoundPlayer();
m_pEffectPlayers->push_back(pPlayer);
s_pEffectPlayers->push_back(pPlayer);
// set the player volume
pPlayer->SetVolumeValue(m_nEffectsVolume);
@ -224,34 +263,18 @@ void SimpleAudioEngine::playPreloadedEffect(int nSoundId)
}
else
{
pPlayer->PlaySoundFromMem(pElement->pDataBuffer, pElement->nDataSize, pElement->FileName.c_str());
pPlayer->PlaySoundFromMem(pElement->pDataBuffer, pElement->nDataSize, pElement->FileName);
pPlayer->SetCurrentSoundID(nSoundId);
}
} while (0);
}
void SimpleAudioEngine::removeAllEffectPlayers()
{
PlayerArrayIterator iter;
for (iter = m_pEffectPlayers->begin(); iter != m_pEffectPlayers->end(); ++iter)
{
if (*iter)
{
delete *iter;
}
}
m_pEffectPlayers->clear();
delete m_pEffectPlayers;
}
void SimpleAudioEngine::setSoundResInfo(const T_SoundResInfo ResInfo[], int nCount)
{
m_pDataManager->setSoundResInfo(ResInfo, nCount);
s_pDataManager->setSoundResInfo(ResInfo, nCount);
}
void SimpleAudioEngine::setResourceEntry(const void* pResEntry)
{
m_pDataManager->setResEntry(pResEntry);
s_pDataManager->setResEntry(pResEntry);
}

View File

@ -1,4 +1,6 @@
#include "SoundDataManager.h"
#include "FileUtils.h"
#include "SoundPlayer.h"
#define BREAK_IF(cond) if (cond) break;

View File

@ -1,21 +1,16 @@
#ifndef _SOUND_DATA_MANAGER_H_
#define _SOUND_DATA_MANAGER_H_
#include "../Platform/platform.h"
#include "ResourceHandle.h"
#include <map>
#include "../Support/uthash.h"
typedef struct
{
std::string FileName;
int nResID;
} T_SoundResInfo;
#include "uthash.h"
#include "SimpleAudioEngine.h"
typedef struct _hashElement
{
int nSoundID;
unsigned char* pDataBuffer;
std::string FileName;
const char* FileName;
int nDataSize;
UT_hash_handle hh;
} tEffectElement;

View File

@ -70,13 +70,13 @@ void SoundPlayer::PlaySoundFile(const char* pFileName, Int32 nTimes)
}
}
void SoundPlayer::PlaySoundFromMem(UInt8* pData, Int32 nSize, std::string FileName, Int32 nTimes)
void SoundPlayer::PlaySoundFromMem(UInt8* pData, Int32 nSize, const char* FileName, Int32 nTimes)
{
if (m_pMediaFile)
{
Int32 nRet = m_pMediaFile->SetContent(pData, nSize);
if (FileName.empty())
if (! strlen(FileName))
{
// 没有指定文件名,按照 .wav 格式解析
const TUChar ExtendName[] = {'.', 'w', 'a', 'v', 0};
@ -87,7 +87,7 @@ void SoundPlayer::PlaySoundFromMem(UInt8* pData, Int32 nSize, std::string FileNa
else
{
// 使用指定的文件名
TUString::StrGBToUnicode(m_fileName, (const Char*)(FileName.c_str()));
TUString::StrGBToUnicode(m_fileName, (const Char*)(FileName));
}
m_pMediaFile->SetName(m_fileName);

View File

@ -4,10 +4,9 @@
#include "TG3.h"
#include "TCOM_MediaPlayer_Method.h"
#include "TCOM_Generic_DataType.h"
#include "../../Export.h"
#include <string>
class EXPORT_DLL SoundPlayer
class SoundPlayer
{
public:
SoundPlayer();
@ -31,7 +30,7 @@ public:
@warning pFileName
pFileName NULL .wav
*/
void PlaySoundFromMem(UInt8* pData, Int32 nSize, std::string pFileName = "", Int32 nTimes = 1);
void PlaySoundFromMem(UInt8* pData, Int32 nSize, const char* pFileName = "", Int32 nTimes = 1);
/**
@brief

View File

@ -19,8 +19,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "chipmunk", "chipmunk\chipmu
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Box2D", "Box2D\Box2D.vcproj", "{83C75293-906B-432E-80B9-A0041043E380}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CocosDenshion", "CocosDenshion\CocosDenshion.vcproj", "{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestAudioEngine", "TestAudioEngine\TestAudioEngine.vcproj", "{93D51450-73EC-48FA-9CFB-2095757678B1}"
ProjectSection(ProjectDependencies) = postProject
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0} = {AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}
@ -37,6 +35,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Cocos2dSimpleGame", "Cocos2
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0} = {AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CocosDenshion_uphone", "CocosDenshion\CocosDenshion-uphone.vcproj", "{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@ -63,10 +63,6 @@ Global
{83C75293-906B-432E-80B9-A0041043E380}.Debug|Win32.Build.0 = Debug|Win32
{83C75293-906B-432E-80B9-A0041043E380}.Release|Win32.ActiveCfg = Release|Win32
{83C75293-906B-432E-80B9-A0041043E380}.Release|Win32.Build.0 = Release|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Debug|Win32.ActiveCfg = Debug|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Debug|Win32.Build.0 = Debug|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Release|Win32.ActiveCfg = Release|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Release|Win32.Build.0 = Release|Win32
{93D51450-73EC-48FA-9CFB-2095757678B1}.Debug|Win32.ActiveCfg = Debug|Win32
{93D51450-73EC-48FA-9CFB-2095757678B1}.Debug|Win32.Build.0 = Debug|Win32
{93D51450-73EC-48FA-9CFB-2095757678B1}.Release|Win32.ActiveCfg = Release|Win32
@ -79,6 +75,10 @@ Global
{ADD7B173-FF61-45DA-BDE0-3E41600D21B0}.Debug|Win32.Build.0 = Debug|Win32
{ADD7B173-FF61-45DA-BDE0-3E41600D21B0}.Release|Win32.ActiveCfg = Release|Win32
{ADD7B173-FF61-45DA-BDE0-3E41600D21B0}.Release|Win32.Build.0 = Release|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Debug|Win32.ActiveCfg = Debug|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Debug|Win32.Build.0 = Debug|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Release|Win32.ActiveCfg = Release|Win32
{AB17438B-7848-4FD6-AF6E-A6A91F82C6F0}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE