2011-08-19 14:43:19 +08:00
|
|
|
/*
|
|
|
|
* FmodAudioPlayer.cpp
|
|
|
|
*
|
|
|
|
* Created on: Aug 18, 2011
|
|
|
|
* Author: laschweinski
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FmodAudioPlayer.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#define szMusicSuffix "|"
|
|
|
|
|
|
|
|
namespace CocosDenshion {
|
|
|
|
|
|
|
|
FmodAudioPlayer* FmodAudioPlayer::sharedPlayer() {
|
2012-04-19 14:35:52 +08:00
|
|
|
static FmodAudioPlayer s_SharedPlayer;
|
|
|
|
return &s_SharedPlayer;
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 19:10:50 +08:00
|
|
|
void ERRCHECKWITHEXIT(FMOD_RESULT result) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (result != FMOD_OK) {
|
|
|
|
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
|
|
|
|
// exit(-1);
|
|
|
|
}
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 19:10:50 +08:00
|
|
|
bool ERRCHECK(FMOD_RESULT result) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (result != FMOD_OK) {
|
|
|
|
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-10-22 19:10:50 +08:00
|
|
|
}
|
|
|
|
|
2011-08-19 14:43:19 +08:00
|
|
|
FmodAudioPlayer::FmodAudioPlayer() :
|
2012-04-19 14:35:52 +08:00
|
|
|
pMusic(0), pBGMChannel(0), iSoundChannelCount(0) {
|
|
|
|
init();
|
2011-08-25 19:04:29 +08:00
|
|
|
}
|
|
|
|
|
2011-10-22 19:10:50 +08:00
|
|
|
void FmodAudioPlayer::init() {
|
2012-04-19 14:35:52 +08:00
|
|
|
//init
|
|
|
|
FMOD_RESULT result;
|
|
|
|
FMOD::ChannelGroup *masterChannelGroup;
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int version;
|
|
|
|
/*
|
|
|
|
Create a System object and initialize.
|
|
|
|
*/
|
|
|
|
result = FMOD::System_Create(&pSystem);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = pSystem->setOutput(FMOD_OUTPUTTYPE_ALSA);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = pSystem->init(32, FMOD_INIT_NORMAL, 0);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = pSystem->createChannelGroup("Channel Group", &pChannelGroup);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = pSystem->getMasterChannelGroup(&masterChannelGroup);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = masterChannelGroup->addGroup(pChannelGroup);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
mapEffectSound.clear();
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::close() {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD_RESULT result;
|
|
|
|
//BGM
|
|
|
|
if (pBGMChannel != NULL) {
|
|
|
|
result = pBGMChannel->stop();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
pBGMChannel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pMusic != NULL) {
|
|
|
|
result = pMusic->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
pMusic = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = pChannelGroup->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
sMusicPath.clear();
|
|
|
|
|
|
|
|
result = pSystem->close();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
result = pSystem->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
|
|
|
|
init();
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FmodAudioPlayer::~FmodAudioPlayer() {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD_RESULT result;
|
|
|
|
//BGM
|
|
|
|
if (pBGMChannel != NULL) {
|
|
|
|
result = pBGMChannel->stop();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pMusic != NULL) {
|
|
|
|
result = pMusic->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = pChannelGroup->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
|
|
|
|
result = pSystem->close();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
result = pSystem->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// BGM
|
|
|
|
void FmodAudioPlayer::preloadBackgroundMusic(const char* pszFilePath) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD_RESULT result;
|
|
|
|
pSystem->update();
|
|
|
|
string sNewMusicPath = string(pszFilePath) + szMusicSuffix;
|
|
|
|
if (pMusic && sNewMusicPath != sMusicPath) {
|
|
|
|
//release old
|
|
|
|
result = pMusic->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
sMusicPath = sNewMusicPath;
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-08-19 14:43:19 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
result = pSystem->createSound(pszFilePath, FMOD_LOOP_NORMAL, 0, &pMusic);
|
|
|
|
ERRCHECK(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::playBackgroundMusic(const char* pszFilePath, bool bLoop) {
|
2012-04-19 14:35:52 +08:00
|
|
|
pSystem->update();
|
|
|
|
if (pMusic == NULL) {
|
|
|
|
//did not load it
|
|
|
|
//load the new music
|
|
|
|
FMOD_RESULT result = pSystem->createSound(pszFilePath, FMOD_LOOP_NORMAL,
|
|
|
|
0, &pMusic);
|
|
|
|
if (!ERRCHECK(result)) {
|
|
|
|
sMusicPath = string(pszFilePath) + szMusicSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
string sNewMusicPath = string(pszFilePath) + szMusicSuffix;
|
|
|
|
if (pBGMChannel) {
|
|
|
|
pBGMChannel->stop();
|
|
|
|
pBGMChannel = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sNewMusicPath != sMusicPath) {
|
|
|
|
|
|
|
|
pMusic->release();
|
|
|
|
//load the new music
|
|
|
|
FMOD_RESULT result = pSystem->createSound(pszFilePath,
|
|
|
|
FMOD_LOOP_NORMAL, 0, &pMusic);
|
|
|
|
|
|
|
|
if (!ERRCHECK(result)) {
|
|
|
|
sMusicPath = sNewMusicPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FMOD_RESULT result = pSystem->playSound(FMOD_CHANNEL_FREE, pMusic, true,
|
|
|
|
&pBGMChannel);
|
|
|
|
if (!ERRCHECK(result)) {
|
|
|
|
pBGMChannel->setLoopCount((bLoop) ? -1 : 1);
|
|
|
|
result = pBGMChannel->setPaused(false);
|
|
|
|
}
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::stopBackgroundMusic(bool bReleaseData) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD_RESULT result;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
if (pBGMChannel == NULL || pMusic == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bReleaseData) {
|
|
|
|
result = pBGMChannel->stop();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
result = pMusic->release();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
pBGMChannel = 0;
|
|
|
|
pMusic = 0;
|
|
|
|
} else {
|
|
|
|
result = pBGMChannel->stop();
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
pBGMChannel = 0;
|
|
|
|
}
|
|
|
|
sMusicPath.clear();
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::pauseBackgroundMusic() {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->setPaused(true);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::resumeBackgroundMusic() {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->setPaused(false);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::rewindBackgroundMusic() {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->setPosition(0, FMOD_TIMEUNIT_MS);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FmodAudioPlayer::willPlayBackgroundMusic() {
|
2012-04-19 14:35:52 +08:00
|
|
|
pSystem->update();
|
|
|
|
return false; //do it according to win
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FmodAudioPlayer::isBackgroundMusicPlaying() {
|
2012-04-19 14:35:52 +08:00
|
|
|
bool bPlaying;
|
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->isPlaying(&bPlaying);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
return bPlaying;
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float FmodAudioPlayer::getBackgroundMusicVolume() {
|
2012-04-19 14:35:52 +08:00
|
|
|
float fVolumn;
|
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->getVolume(&fVolumn);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
|
|
|
return fVolumn;
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::setBackgroundMusicVolume(float volume) {
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pBGMChannel == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pBGMChannel->setVolume(volume);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
//~BGM
|
|
|
|
|
|
|
|
// for sound effects
|
|
|
|
float FmodAudioPlayer::getEffectsVolume() {
|
2012-04-19 14:35:52 +08:00
|
|
|
float fVolumn;
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pChannelGroup->getVolume(&fVolumn);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::setEffectsVolume(float volume) {
|
2012-04-19 14:35:52 +08:00
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pChannelGroup->setVolume(volume);
|
|
|
|
ERRCHECKWITHEXIT(result);
|
2011-08-19 14:43:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int FmodAudioPlayer::playEffect(const char* pszFilePath, bool bLoop) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
FMOD::Sound* pSound = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<string, FMOD::Sound*>::iterator l_it = mapEffectSound.find(
|
|
|
|
string(pszFilePath));
|
|
|
|
if (l_it == mapEffectSound.end()) {
|
|
|
|
//no load it yet
|
|
|
|
preloadEffect(pszFilePath);
|
|
|
|
l_it = mapEffectSound.find(string(pszFilePath));
|
|
|
|
}
|
|
|
|
pSound = l_it->second;
|
|
|
|
if (pSound==NULL){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FMOD_RESULT result = pSystem->playSound(FMOD_CHANNEL_FREE, pSound, true,
|
|
|
|
&pChannel);
|
|
|
|
|
|
|
|
if (ERRCHECK(result)) {
|
|
|
|
printf("sound effect in %s could not be played", pszFilePath);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pChannel->setChannelGroup(pChannelGroup);
|
|
|
|
|
|
|
|
//set its loop
|
|
|
|
pChannel->setLoopCount((bLoop) ? -1 : 1);
|
|
|
|
result = pChannel->setPaused(false);
|
|
|
|
|
|
|
|
mapEffectSoundChannel[iSoundChannelCount] = pChannel;
|
|
|
|
return iSoundChannelCount++;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return 0;
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::stopEffect(unsigned int nSoundId) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.find(nSoundId);
|
|
|
|
if (l_it == mapEffectSoundChannel.end()) {
|
|
|
|
//no play yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//stop the channel;
|
|
|
|
pChannel->stop();
|
|
|
|
|
|
|
|
//delete from the map;
|
|
|
|
mapEffectSoundChannel.erase(nSoundId);
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
2011-11-25 14:16:24 +08:00
|
|
|
void FmodAudioPlayer::pauseEffect(unsigned int uSoundId) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.find(uSoundId);
|
|
|
|
if (l_it == mapEffectSoundChannel.end()) {
|
|
|
|
//no play yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//pause the channel;
|
|
|
|
pChannel->setPaused(true);
|
2011-11-25 14:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::pauseAllEffects() {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.begin();
|
|
|
|
|
|
|
|
for (; l_it != mapEffectSoundChannel.end(); l_it++) {
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//pause the channel;
|
|
|
|
pChannel->setPaused(true);
|
|
|
|
}
|
2011-11-25 14:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::resumeEffect(unsigned int uSoundId) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.find(uSoundId);
|
|
|
|
if (l_it == mapEffectSoundChannel.end()) {
|
|
|
|
//no play yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//resume the channel;
|
|
|
|
pChannel->setPaused(false);
|
2011-11-25 14:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::resumeAllEffects() {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.begin();
|
|
|
|
|
|
|
|
for (; l_it != mapEffectSoundChannel.end(); l_it++) {
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//resume the channel;
|
|
|
|
pChannel->setPaused(false);
|
|
|
|
}
|
2011-11-25 14:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::stopAllEffects() {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Channel* pChannel;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<unsigned int, FMOD::Channel*>::iterator l_it =
|
|
|
|
mapEffectSoundChannel.begin();
|
|
|
|
|
|
|
|
for (; l_it != mapEffectSoundChannel.end(); l_it++) {
|
|
|
|
pChannel = l_it->second;
|
|
|
|
//resume the channel;
|
|
|
|
pChannel->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
mapEffectSoundChannel.clear();
|
2011-11-25 14:16:24 +08:00
|
|
|
}
|
|
|
|
|
2011-08-19 14:43:19 +08:00
|
|
|
void FmodAudioPlayer::preloadEffect(const char* pszFilePath) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Sound* pLoadSound;
|
|
|
|
|
|
|
|
pSystem->update();
|
|
|
|
FMOD_RESULT result = pSystem->createSound(pszFilePath, FMOD_LOOP_NORMAL, 0,
|
|
|
|
&pLoadSound);
|
|
|
|
if (ERRCHECK(result)){
|
|
|
|
printf("sound effect in %s could not be preload", pszFilePath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mapEffectSound[string(pszFilePath)] = pLoadSound;
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FmodAudioPlayer::unloadEffect(const char* pszFilePath) {
|
2012-04-19 14:35:52 +08:00
|
|
|
FMOD::Sound* pSound;
|
|
|
|
pSystem->update();
|
|
|
|
|
|
|
|
map<string, FMOD::Sound*>::iterator l_it = mapEffectSound.find(
|
|
|
|
string(pszFilePath));
|
|
|
|
if (l_it == mapEffectSound.end()) {
|
|
|
|
//no load yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pSound = l_it->second;
|
|
|
|
|
|
|
|
//release the sound;
|
|
|
|
pSound->release();
|
|
|
|
|
|
|
|
//delete from the map
|
|
|
|
mapEffectSound.erase(string(pszFilePath));
|
2011-08-19 14:43:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//~for sound effects
|
|
|
|
|
|
|
|
} /* namespace CocosDenshion */
|