axmol/CocosDenshion/bada/SimpleAudioEngine.cpp

598 lines
14 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "SimpleAudioEngine.h"
2011-10-18 18:10:53 +08:00
#include "CCAudioOut.h"
#include <FBase.h>
#include <FMedia.h>
#include <FSystem.h>
#include <string>
#include <map>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
using namespace Osp::Media;
using namespace Osp::System;
using namespace Osp::Base::Runtime;
using namespace Osp::Base::Collection;
#define BREAK_IF(cond) if (cond) break;
namespace CocosDenshion {
#define MAX_BUFFER_SIZE 2520 // 840 byte
2011-10-18 18:10:53 +08:00
typedef map<unsigned int, CCAudioOut*> EffectList;
typedef pair<unsigned int ,CCAudioOut*> Effect;
static SimpleAudioEngine *s_pSharedAudioEngine = NULL;
static Player *s_pBackPlayer = NULL;
static EffectList s_List;
static float s_fBackgroundMusicVolume = 1.0f;
static float s_fEffectsVolume = 1.0f;
static bool s_bWillPlayBackgroundMusic = false;
static bool s_bBackgroundMusicPaused = false;
2011-11-07 14:19:46 +08:00
static string s_strResourcePath = "/Res/";
static unsigned int _Hash(const char *key)
{
unsigned int len = strlen(key);
const char *end=key+len;
unsigned int hash;
for (hash = 0; key < end; key++)
{
hash *= 16777619;
hash ^= (unsigned int) (unsigned char) toupper(*key);
}
return (hash);
}
static string fullPathFromRelativePath(const char *pszRelativePath)
{
string strRet="";
int len = strlen(pszRelativePath);
if (pszRelativePath == NULL || len <= 0)
{
return strRet;
}
if (len > 1 && pszRelativePath[0] == '/')
{
strRet = pszRelativePath;
}
else
{
strRet = s_strResourcePath;
strRet += pszRelativePath;
}
return strRet;
}
class MyPlayerEventListener :
public IPlayerEventListener
{
public:
/**
* Notifies that audio/video content was opened asynchronously.
*
* @param[in] r The cause of the error
* @exception E_SUCCESS The method was successful.
* @exception E_SYSTEM A system error occurred.
* @exception E_CONNECTION_FAILED Network connection failed.
* @exception E_UNSUPPORTED_FORMAT The specified format is not supported.
* @exception E_UNSUPPORTED_CODEC The specified codec is not supported.
* @exception E_OUT_OF_MEMORY Insufficient memory.
* @see Player::OpenFile(), Player::OpenUrl(), Player::OpenBuffer()
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerOpened( result r )
{
AppLog("OnPlayerOpened result = %d", r);
2011-09-28 22:53:51 +08:00
}
/**
* Notifies that the Player has reached the end of the clip.
*
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerEndOfClip(void)
{
AppLog("OnPlayerEndOfClip");
2011-09-28 22:53:51 +08:00
}
/**
* Notifies that the position of the audio/video content was moved asynchronously.
*
* @exception E_SUCCESS The method was successful.
* @exception E_SYSTEM A system error occurred.
* @see Player::SeekTo()
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerSeekCompleted( result r )
{
AppLog("OnPlayerSeekCompleted result = %d", r);
2011-09-28 22:53:51 +08:00
}
/**
* Notifies that streaming data is being buffered.
*
* @param[in] percent The percentage of buffering completed
* @see Player::OpenUrl()
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerBuffering(int percent)
{
AppLog("OnPlayerBuffering percent = %d%", percent);
2011-09-28 22:53:51 +08:00
}
/**
* Notifies that an error has occurred while the Player is working.
*
* @param[in] r A player error reason of type ::PlayerErrorReason
* @remark While playing streaming media, the player might throw an error like ::PLAYER_ERROR_CONNECTION_LOST @n
* ::PLAYER_ERROR_STREAMING_TIMEOUT, ::PLAYER_ERROR_TRANSPORT or ::PLAYER_ERROR_SERVER. @n
* If the content includes invalid data, ::PLAYER_ERROR_INVALID_DATA may occur.
* @see PlayerErrorReason
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerErrorOccurred( PlayerErrorReason r )
{
AppLog("OnPlayerErrorOccurred PlayerErrorReason = %d", r);
2011-09-28 22:53:51 +08:00
}
/**
* Notifies that the Player is being interrupted by a task of higher priority than Player.
*
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerInterrupted(void)
{
AppLog("OnPlayerInterrupted");
2011-09-28 22:53:51 +08:00
//Insert your code here
if (s_pBackPlayer->GetState() == PLAYER_STATE_PLAYING)
s_pBackPlayer->Pause();
}
/**
* Notifies that the interrupting Player has been released.
*
*/
2011-09-28 22:53:51 +08:00
virtual void OnPlayerReleased(void)
{
AppLog("OnPlayerReleased");
2011-09-28 22:53:51 +08:00
//Insert your code here
if (s_pBackPlayer->GetState() != PLAYER_STATE_PLAYING)
s_pBackPlayer->Play();
}
};
static MyPlayerEventListener s_playerListener;
static void closeMediaPlayer(Player*& pPlayer)
{
if (pPlayer != NULL)
{
PlayerState nowState = pPlayer->GetState();
if( nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_PAUSED )
{
pPlayer->Stop();
pPlayer->Close();
}
else if(nowState == PLAYER_STATE_OPENED || nowState == PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_STOPPED )
{
pPlayer->Close();
}
}
}
static bool openMediaPlayer(Player*& pPlayer, const char* pszFilePath)
{
bool bRet = false;
result r = E_FAILURE;
do
{
closeMediaPlayer(pPlayer);
if (pPlayer == NULL)
{
pPlayer = new Player();
r = pPlayer->Construct(s_playerListener, null);
if (IsFailed(r))
{
AppLog("player construct fails, pszFilePath = %s", pszFilePath);
delete pPlayer;
pPlayer = NULL;
break;
}
}
string strFilePath = fullPathFromRelativePath(pszFilePath);
// OpenFile must use synchronous param, for after that it will playing.
r = pPlayer->OpenFile(strFilePath.c_str(), false);
if (IsFailed(r))
{
AppLog("Open (%s) fails\n", strFilePath.c_str());
delete pPlayer;
pPlayer = NULL;
break;
}
else
{
bRet = true;
}
}
while (0);
SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(s_fBackgroundMusicVolume);
return bRet;
}
SimpleAudioEngine::SimpleAudioEngine()
{
}
SimpleAudioEngine::~SimpleAudioEngine()
{
for (EffectList::iterator it = s_List.begin(); it != s_List.end(); ++it)
{
it->second->Stop();
delete it->second;
}
s_List.clear();
closeMediaPlayer(s_pBackPlayer);
delete s_pBackPlayer;
s_pBackPlayer = NULL;
}
SimpleAudioEngine* SimpleAudioEngine::sharedEngine()
{
if (s_pSharedAudioEngine == NULL)
{
s_pSharedAudioEngine = new SimpleAudioEngine;
}
return s_pSharedAudioEngine;
}
void SimpleAudioEngine::end()
{
if (s_pSharedAudioEngine)
{
delete s_pSharedAudioEngine;
s_pSharedAudioEngine = NULL;
}
}
void SimpleAudioEngine::setResource(const char* pszZipFileName)
{
}
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
{
openMediaPlayer(s_pBackPlayer, pszFilePath);
}
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
{
result r = E_FAILURE;
bool bRet = false;
bRet = openMediaPlayer(s_pBackPlayer, pszFilePath);
2011-10-29 14:47:13 +08:00
if (bRet)
2011-09-28 22:53:51 +08:00
{
s_pBackPlayer->SetLooping(bLoop);
2011-09-28 22:53:51 +08:00
r = s_pBackPlayer->Play();
}
}
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
{
s_bBackgroundMusicPaused = false;
if (s_pBackPlayer != NULL && PLAYER_STATE_PLAYING == s_pBackPlayer->GetState())
{
s_pBackPlayer->Stop();
}
}
void SimpleAudioEngine::pauseBackgroundMusic()
{
if (s_pBackPlayer != NULL && PLAYER_STATE_PLAYING == s_pBackPlayer->GetState())
{
s_bBackgroundMusicPaused = true;
s_pBackPlayer->Pause();
}
}
void SimpleAudioEngine::resumeBackgroundMusic()
{
if (s_pBackPlayer != NULL && s_bBackgroundMusicPaused && PLAYER_STATE_PLAYING != s_pBackPlayer->GetState())
{
s_bBackgroundMusicPaused = false;
s_pBackPlayer->Play();
}
}
void SimpleAudioEngine::rewindBackgroundMusic()
{
stopBackgroundMusic();
if (s_pBackPlayer != NULL)
{
if (PLAYER_STATE_PLAYING != s_pBackPlayer->GetState())
{
result r = s_pBackPlayer->Play();
if (IsFailed(r))
{
AppLog("ERROR: %s", GetErrorMessage(r));
}
}
}
}
bool SimpleAudioEngine::willPlayBackgroundMusic()
{
return s_bWillPlayBackgroundMusic;
}
bool SimpleAudioEngine::isBackgroundMusicPlaying()
{
bool bRet = false;
if (s_pBackPlayer != NULL && s_pBackPlayer->GetState() == PLAYER_STATE_PLAYING)
{
bRet = true;
}
return bRet;
}
// properties
float SimpleAudioEngine::getBackgroundMusicVolume()
{
return s_fBackgroundMusicVolume;
}
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
{
if (volume > 1.0f)
{
volume = 1.0f;
}
else if (volume < 0.0f)
{
volume = 0.0f;
}
if (s_pBackPlayer != NULL)
{
s_pBackPlayer->SetVolume((int) (volume * 99));
if (volume > 0.0f && s_pBackPlayer->GetVolume() == 0)
{
s_pBackPlayer->SetVolume(1);
}
}
s_fBackgroundMusicVolume = volume;
}
float SimpleAudioEngine::getEffectsVolume()
{
return s_fEffectsVolume;
}
void SimpleAudioEngine::setEffectsVolume(float volume)
{
if (volume > 1.0f)
{
volume = 1.0f;
}
else if (volume < 0.0f)
{
volume = 0.0f;
}
s_fEffectsVolume = volume;
}
// for sound effects
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop/* = false*/)
{
result r = E_FAILURE;
string strFilePath = fullPathFromRelativePath(pszFilePath);
unsigned int nRet = _Hash(strFilePath.c_str());
preloadEffect(pszFilePath);
EffectList::iterator p = s_List.find(nRet);
if (p != s_List.end())
{
if (NULL == p->second)
{
AppLog("CCAudioOut instance must not be NULL, id = %d", p->first);
return 0;
}
p->second->SetVolume((int) (s_fEffectsVolume * 99));
2011-10-18 18:10:53 +08:00
int volume = p->second->GetVolume();
2011-10-18 18:10:53 +08:00
if (s_fEffectsVolume > 0.0f && volume == 0)
{
p->second->SetVolume(1);
}
2011-10-18 18:10:53 +08:00
if (AUDIOOUT_STATE_PLAYING == p->second->GetState())
{
return nRet; // Stop waste a lot of time, so just return.
2011-11-29 09:27:44 +08:00
//r = p->second->Stop();
}
2011-10-20 13:49:01 +08:00
if (s_fEffectsVolume > 0.0f)
{
r = p->second->Play(bLoop);
2011-10-20 13:49:01 +08:00
}
2011-10-18 18:10:53 +08:00
if (IsFailed(r))
{
AppLog("play effect fails, error code = %d", r);
}
}
return nRet;
}
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
{
EffectList::iterator it = s_List.find(nSoundId);
if (it != s_List.end())
{
if (it->second != NULL)
{
it->second->Stop();
}
}
}
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
{
EffectList::iterator it = s_List.find(nSoundId);
if (it != s_List.end())
{
if (it->second != NULL)
{
it->second->Pause();
}
}
}
void SimpleAudioEngine::pauseAllEffects()
{
for (EffectList::iterator it = s_List.begin(); it != s_List.end(); ++it)
{
if (it->second != NULL)
{
it->second->Pause();
}
}
}
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
{
EffectList::iterator it = s_List.find(nSoundId);
if (it != s_List.end())
{
if (it->second != NULL)
{
it->second->Resume();
}
}
}
void SimpleAudioEngine::resumeAllEffects()
{
for (EffectList::iterator it = s_List.begin(); it != s_List.end(); ++it)
{
if (it->second != NULL)
{
it->second->Resume();
}
}
}
2011-10-29 14:47:13 +08:00
void SimpleAudioEngine::stopAllEffects()
{
for (EffectList::iterator it = s_List.begin(); it != s_List.end(); ++it)
{
if (it->second != NULL)
{
it->second->Stop();
}
2011-10-29 14:47:13 +08:00
}
}
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
{
int nRet = 0;
2011-10-18 18:10:53 +08:00
CCAudioOut* pEffectPlayer = NULL;
do
{
BREAK_IF(! pszFilePath);
string strFilePath = fullPathFromRelativePath(pszFilePath);
nRet = _Hash(strFilePath.c_str());
BREAK_IF(s_List.end() != s_List.find(nRet));
2011-11-13 00:57:54 +08:00
//AppLog("not find effect, create it...");
2011-10-18 18:10:53 +08:00
if (s_List.size() >= 64)
{
// get the first effect, and remove it form list
AppLog("effect preload more than 64, delete the first effect");
pEffectPlayer = s_List.begin()->second;
2011-10-18 18:10:53 +08:00
pEffectPlayer->Finalize();
s_List.erase(s_List.begin());
}
if (NULL == pEffectPlayer)
{
pEffectPlayer = new CCAudioOut();
}
2011-10-18 18:10:53 +08:00
pEffectPlayer->Initialize(strFilePath.c_str());
s_List.insert(Effect(nRet, pEffectPlayer));
} while (0);
}
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
{
string strFilePath = fullPathFromRelativePath(pszFilePath);
unsigned int nID = _Hash(strFilePath.c_str());
EffectList::iterator p = s_List.find(nID);
if (p != s_List.end())
{
if (p->second != NULL)
{
delete p->second;
p->second = NULL;
AppLog("delete CCAudioOut, id = %d", nID);
}
else
{
AppLog("CCAudioOut instance is NULL, id = %d", nID);
}
s_List.erase(p);
}
}
} // end of namespace CocosDenshion