mirror of https://github.com/axmolengine/axmol.git
Merge pull request #3203 from dumganhar/rafaelx-openal-deletebuffers
Merge PR https://github.com/cocos2d/cocos2d-x/pull/3147
This commit is contained in:
commit
ae015a71e1
|
@ -63,9 +63,11 @@ static inline unsigned int getHashCodeByString(const char *key)
|
|||
}
|
||||
|
||||
/**
|
||||
@class SimpleAudioEngine
|
||||
@brief offer a VERY simple interface to play background music & sound effect
|
||||
*/
|
||||
@class SimpleAudioEngine
|
||||
@brief Offers a VERY simple interface to play background music & sound effects.
|
||||
@note Make sure to call SimpleAudioEngine::end() when the sound engine is not needed anymore
|
||||
to release allocated resources.
|
||||
*/
|
||||
|
||||
class EXPORT_DLL SimpleAudioEngine : public TypeInfo
|
||||
{
|
||||
|
@ -91,7 +93,7 @@ public:
|
|||
|
||||
/**
|
||||
@brief Preload background music
|
||||
@param pszFilePath The path of the background music file,or the FileName of T_SoundResInfo
|
||||
@param pszFilePath The path of the background music file.
|
||||
*/
|
||||
void preloadBackgroundMusic(const char* pszFilePath);
|
||||
|
||||
|
@ -110,59 +112,68 @@ public:
|
|||
|
||||
/**
|
||||
@brief Pause playing background music
|
||||
*/
|
||||
*/
|
||||
void pauseBackgroundMusic();
|
||||
|
||||
/**
|
||||
@brief Resume playing background music
|
||||
*/
|
||||
*/
|
||||
void resumeBackgroundMusic();
|
||||
|
||||
/**
|
||||
@brief Rewind playing background music
|
||||
*/
|
||||
*/
|
||||
void rewindBackgroundMusic();
|
||||
|
||||
/**
|
||||
@brief Indicates whether any background music can be played or not.
|
||||
@return <i>true</i> if background music can be played, otherwise <i>false</i>.
|
||||
*/
|
||||
bool willPlayBackgroundMusic();
|
||||
|
||||
/**
|
||||
@brief Whether the background music is playing
|
||||
@return If is playing return true,or return false
|
||||
@brief Indicates whether the background music is playing
|
||||
@return <i>true</i> if the background music is playing, otherwise <i>false</i>
|
||||
*/
|
||||
bool isBackgroundMusicPlaying();
|
||||
|
||||
//
|
||||
// properties
|
||||
//
|
||||
|
||||
/**
|
||||
@brief The volume of the background music max value is 1.0,the min value is 0.0
|
||||
@brief The volume of the background music within the range of 0.0 as the minimum and 1.0 as the maximum.
|
||||
*/
|
||||
float getBackgroundMusicVolume();
|
||||
|
||||
/**
|
||||
@brief set the volume of background music
|
||||
@param volume must be in 0.0~1.0
|
||||
@brief Set the volume of background music
|
||||
@param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
|
||||
*/
|
||||
void setBackgroundMusicVolume(float volume);
|
||||
|
||||
/**
|
||||
@brief The volume of the effects max value is 1.0,the min value is 0.0
|
||||
@brief The volume of the effects within the range of 0.0 as the minimum and 1.0 as the maximum.
|
||||
*/
|
||||
float getEffectsVolume();
|
||||
|
||||
/**
|
||||
@brief set the volume of sound effecs
|
||||
@param volume must be in 0.0~1.0
|
||||
@brief Set the volume of sound effects
|
||||
@param volume must be within the range of 0.0 as the minimum and 1.0 as the maximum.
|
||||
*/
|
||||
void setEffectsVolume(float volume);
|
||||
|
||||
//
|
||||
// for sound effects
|
||||
|
||||
/**
|
||||
@brief Play sound effect with a file path, pitch, pan and gain
|
||||
@param pszFilePath The path of the effect file,or the FileName of T_SoundResInfo
|
||||
@param bLoop Whether to loop the effect playing, default value is false
|
||||
@brief Play sound effect with a file path, pitch, pan and gain
|
||||
@param pszFilePath The path of the effect file.
|
||||
@param bLoop Determines whether to loop the effect playing or not. The default value is false.
|
||||
@param pitch Frequency, normal value is 1.0. Will also change effect play time.
|
||||
@param pan Stereo effect, in range [-1..1] where -1 enables only left channel.
|
||||
@param gain Volume, normal value is 1. Works for range [0..1].
|
||||
@param pan Stereo effect, in the range of [-1..1] where -1 enables only left channel.
|
||||
@param gain Volume, in the range of [0..1]. The normal value is 1.
|
||||
@return the OpenAL source id
|
||||
|
||||
@note Full support is under development, now there are limitations:
|
||||
- no pitch effect on Samsung Galaxy S2 with OpenSL backend enabled;
|
||||
|
@ -208,14 +219,14 @@ public:
|
|||
|
||||
/**
|
||||
@brief preload a compressed audio file
|
||||
@details the compressed audio will be decode to wave, then write into an
|
||||
internal buffer in SimpleaudioEngine
|
||||
@details the compressed audio will be decoded to wave, then written into an internal buffer in SimpleAudioEngine
|
||||
@param pszFilePath The path of the effect file
|
||||
*/
|
||||
void preloadEffect(const char* pszFilePath);
|
||||
|
||||
/**
|
||||
@brief unload the preloaded effect from internal buffer
|
||||
@param[in] pszFilePath The path of the effect file,or the FileName of T_SoundResInfo
|
||||
@param pszFilePath The path of the effect file
|
||||
*/
|
||||
void unloadEffect(const char* pszFilePath);
|
||||
};
|
||||
|
|
|
@ -44,171 +44,184 @@ using namespace std;
|
|||
|
||||
namespace CocosDenshion
|
||||
{
|
||||
struct soundData {
|
||||
ALuint buffer;
|
||||
ALuint source;
|
||||
bool isLooped;
|
||||
struct soundData {
|
||||
ALuint buffer;
|
||||
ALuint source;
|
||||
bool isLooped;
|
||||
float pitch;
|
||||
float pan;
|
||||
float gain;
|
||||
};
|
||||
};
|
||||
|
||||
typedef map<string, soundData *> EffectsMap;
|
||||
EffectsMap s_effects;
|
||||
typedef map<string, soundData *> EffectsMap;
|
||||
EffectsMap s_effects;
|
||||
|
||||
typedef enum {
|
||||
PLAYING,
|
||||
STOPPED,
|
||||
PAUSED,
|
||||
} playStatus;
|
||||
typedef enum {
|
||||
PLAYING,
|
||||
STOPPED,
|
||||
PAUSED,
|
||||
} playStatus;
|
||||
|
||||
static float s_volume = 1.0f;
|
||||
static float s_effectVolume = 1.0f;
|
||||
static float s_volume = 1.0f;
|
||||
static float s_effectVolume = 1.0f;
|
||||
|
||||
struct backgroundMusicData {
|
||||
ALuint buffer;
|
||||
ALuint source;
|
||||
};
|
||||
typedef map<string, backgroundMusicData *> BackgroundMusicsMap;
|
||||
BackgroundMusicsMap s_backgroundMusics;
|
||||
struct backgroundMusicData {
|
||||
ALuint buffer;
|
||||
ALuint source;
|
||||
};
|
||||
typedef map<string, backgroundMusicData *> BackgroundMusicsMap;
|
||||
BackgroundMusicsMap s_backgroundMusics;
|
||||
|
||||
static ALuint s_backgroundSource = AL_NONE;
|
||||
static ALuint s_backgroundSource = AL_NONE;
|
||||
|
||||
static SimpleAudioEngine *s_engine = 0;
|
||||
static SimpleAudioEngine *s_engine = nullptr;
|
||||
|
||||
static int checkALError(const char *funcName)
|
||||
{
|
||||
int err = alGetError();
|
||||
static int checkALError(const char *funcName)
|
||||
{
|
||||
int err = alGetError();
|
||||
|
||||
if (err != AL_NO_ERROR)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case AL_INVALID_NAME:
|
||||
fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName);
|
||||
break;
|
||||
if (err != AL_NO_ERROR)
|
||||
{
|
||||
switch (err)
|
||||
{
|
||||
case AL_INVALID_NAME:
|
||||
fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName);
|
||||
break;
|
||||
|
||||
case AL_INVALID_ENUM:
|
||||
fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName);
|
||||
break;
|
||||
case AL_INVALID_ENUM:
|
||||
fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName);
|
||||
break;
|
||||
|
||||
case AL_INVALID_VALUE:
|
||||
fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName);
|
||||
break;
|
||||
case AL_INVALID_VALUE:
|
||||
fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName);
|
||||
break;
|
||||
|
||||
case AL_INVALID_OPERATION:
|
||||
fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName);
|
||||
break;
|
||||
case AL_INVALID_OPERATION:
|
||||
fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName);
|
||||
break;
|
||||
|
||||
case AL_OUT_OF_MEMORY:
|
||||
fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
case AL_OUT_OF_MEMORY:
|
||||
fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
static void stopBackground(bool bReleaseData)
|
||||
{
|
||||
alSourceStop(s_backgroundSource);
|
||||
// The background music might have been already stopped
|
||||
// Stop request can come from
|
||||
// - stopBackgroundMusic(..)
|
||||
// - end(..)
|
||||
if (s_backgroundSource != AL_NONE)
|
||||
alSourceStop(s_backgroundSource);
|
||||
|
||||
if (bReleaseData)
|
||||
{
|
||||
for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||
{
|
||||
if (it->second->source == s_backgroundSource)
|
||||
{
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("stopBackground:alDeleteBuffers");
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("stopBackground:alDeleteSources");
|
||||
delete it->second;
|
||||
s_backgroundMusics.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bReleaseData)
|
||||
{
|
||||
for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||
{
|
||||
if (it->second->source == s_backgroundSource)
|
||||
{
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("stopBackground:alDeleteSources");
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("stopBackground:alDeleteBuffers");
|
||||
delete it->second;
|
||||
s_backgroundMusics.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s_backgroundSource = AL_NONE;
|
||||
s_backgroundSource = AL_NONE;
|
||||
}
|
||||
|
||||
static void setBackgroundVolume(float volume)
|
||||
{
|
||||
alSourcef(s_backgroundSource, AL_GAIN, volume);
|
||||
alSourcef(s_backgroundSource, AL_GAIN, volume);
|
||||
}
|
||||
|
||||
SimpleAudioEngine::SimpleAudioEngine()
|
||||
{
|
||||
alutInit(0, 0);
|
||||
SimpleAudioEngine::SimpleAudioEngine()
|
||||
{
|
||||
alutInit(0, 0);
|
||||
#ifdef ENABLE_MPG123
|
||||
mpg123_init();
|
||||
#endif
|
||||
checkALError("SimpleAudioEngine:alutInit");
|
||||
OpenALDecoder::installDecoders();
|
||||
}
|
||||
}
|
||||
|
||||
SimpleAudioEngine::~SimpleAudioEngine()
|
||||
{
|
||||
SimpleAudioEngine::~SimpleAudioEngine()
|
||||
{
|
||||
#ifdef ENABLE_MPG123
|
||||
mpg123_exit();
|
||||
#endif
|
||||
alutExit();
|
||||
}
|
||||
|
||||
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
||||
{
|
||||
if (!s_engine)
|
||||
s_engine = new SimpleAudioEngine();
|
||||
|
||||
return s_engine;
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::end()
|
||||
{
|
||||
checkALError("end:init");
|
||||
|
||||
// clear all the sounds
|
||||
EffectsMap::const_iterator end = s_effects.end();
|
||||
for (EffectsMap::iterator it = s_effects.begin(); it != end; it++)
|
||||
{
|
||||
alSourceStop(it->second->source);
|
||||
checkALError("end:alSourceStop");
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("end:alDeleteSources");
|
||||
delete it->second;
|
||||
}
|
||||
s_effects.clear();
|
||||
|
||||
// and the background too
|
||||
stopBackground(true);
|
||||
|
||||
for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||
{
|
||||
alSourceStop(it->second->source);
|
||||
checkALError("end:alSourceStop");
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("end:alDeleteSources");
|
||||
delete it->second;
|
||||
}
|
||||
s_backgroundMusics.clear();
|
||||
alutExit();
|
||||
}
|
||||
|
||||
//
|
||||
// background audio
|
||||
//
|
||||
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
||||
{
|
||||
if (!s_engine)
|
||||
s_engine = new SimpleAudioEngine();
|
||||
|
||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||
if (it == s_backgroundMusics.end())
|
||||
{
|
||||
return s_engine;
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::end()
|
||||
{
|
||||
checkALError("end:init");
|
||||
|
||||
// clear all the sound effects
|
||||
EffectsMap::const_iterator end = s_effects.end();
|
||||
for (auto it = s_effects.begin(); it != end; ++it)
|
||||
{
|
||||
alSourceStop(it->second->source);
|
||||
checkALError("end:alSourceStop");
|
||||
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("end:alDeleteSources");
|
||||
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
|
||||
delete it->second;
|
||||
}
|
||||
s_effects.clear();
|
||||
|
||||
// and the background music too
|
||||
stopBackground(true);
|
||||
|
||||
for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||
{
|
||||
alSourceStop(it->second->source);
|
||||
checkALError("end:alSourceStop");
|
||||
|
||||
alDeleteSources(1, &it->second->source);
|
||||
checkALError("end:alDeleteSources");
|
||||
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
|
||||
delete it->second;
|
||||
}
|
||||
s_backgroundMusics.clear();
|
||||
|
||||
CC_SAFE_DELETE(s_engine);
|
||||
}
|
||||
|
||||
//
|
||||
// background audio
|
||||
//
|
||||
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||
if (it == s_backgroundMusics.end())
|
||||
{
|
||||
ALuint buffer = AL_NONE;
|
||||
bool success = false;
|
||||
OpenALFile file;
|
||||
|
@ -224,75 +237,97 @@ namespace CocosDenshion
|
|||
success = decoders[i]->decode(file, buffer);
|
||||
file.clear();
|
||||
|
||||
ALuint source = AL_NONE;
|
||||
alGenSources(1, &source);
|
||||
checkALError("preloadBackgroundMusic:alGenSources");
|
||||
ALuint source = AL_NONE;
|
||||
alGenSources(1, &source);
|
||||
checkALError("preloadBackgroundMusic:alGenSources");
|
||||
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
checkALError("preloadBackgroundMusic:alSourcei");
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
checkALError("preloadBackgroundMusic:alSourcei");
|
||||
|
||||
backgroundMusicData* data = new backgroundMusicData();
|
||||
data->buffer = buffer;
|
||||
data->source = source;
|
||||
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
|
||||
}
|
||||
}
|
||||
backgroundMusicData* data = new backgroundMusicData();
|
||||
data->buffer = buffer;
|
||||
data->source = source;
|
||||
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
||||
{
|
||||
if (s_backgroundSource != AL_NONE)
|
||||
stopBackgroundMusic(false);
|
||||
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
||||
{
|
||||
// If there is already a background music source we stop it first
|
||||
if (s_backgroundSource != AL_NONE)
|
||||
stopBackgroundMusic(false);
|
||||
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||
if (it == s_backgroundMusics.end())
|
||||
{
|
||||
preloadBackgroundMusic(fullPath.c_str());
|
||||
it = s_backgroundMusics.find(fullPath);
|
||||
}
|
||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||
if (it == s_backgroundMusics.end())
|
||||
{
|
||||
preloadBackgroundMusic(fullPath.c_str());
|
||||
it = s_backgroundMusics.find(fullPath);
|
||||
}
|
||||
|
||||
if (it != s_backgroundMusics.end())
|
||||
{
|
||||
s_backgroundSource = it->second->source;
|
||||
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
||||
alSourcePlay(s_backgroundSource);
|
||||
checkALError("playBackgroundMusic:alSourcePlay");
|
||||
}
|
||||
}
|
||||
if (it != s_backgroundMusics.end())
|
||||
{
|
||||
s_backgroundSource = it->second->source;
|
||||
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
||||
setBackgroundVolume(s_volume);
|
||||
alSourcePlay(s_backgroundSource);
|
||||
checkALError("playBackgroundMusic:alSourcePlay");
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
||||
{
|
||||
// If there is no source, then there is nothing that can be stopped
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
stopBackground(bReleaseData);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::pauseBackgroundMusic()
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(s_backgroundSource);
|
||||
checkALError("pauseBackgroundMusic:alSourcePause");
|
||||
}
|
||||
void SimpleAudioEngine::pauseBackgroundMusic()
|
||||
{
|
||||
// If there is no source, then there is nothing that can be paused
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
void SimpleAudioEngine::resumeBackgroundMusic()
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(s_backgroundSource);
|
||||
checkALError("resumeBackgroundMusic:alSourcePlay");
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(s_backgroundSource);
|
||||
|
||||
checkALError("pauseBackgroundMusic:alSourcePause");
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::resumeBackgroundMusic()
|
||||
{
|
||||
// If there is no source, then there is nothing that can be resumed
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(s_backgroundSource);
|
||||
|
||||
checkALError("resumeBackgroundMusic:alSourcePlay");
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::rewindBackgroundMusic()
|
||||
{
|
||||
// If there is no source, then there is nothing that can be rewinded
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
// Rewind and prevent the last state the source had
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
alSourceRewind(s_backgroundSource);
|
||||
|
||||
if (state == AL_PLAYING)
|
||||
{
|
||||
alSourcePlay(s_backgroundSource);
|
||||
|
@ -305,113 +340,127 @@ namespace CocosDenshion
|
|||
checkALError("rewindBackgroundMusic:alSourceRewind");
|
||||
}
|
||||
|
||||
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
||||
{
|
||||
// We are able to play background music
|
||||
// if we have a valid background source
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return false;
|
||||
|
||||
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
||||
{
|
||||
ALint play_status;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status);
|
||||
return (alIsSource(s_backgroundSource) == AL_TRUE ? true : false);
|
||||
}
|
||||
|
||||
return (play_status == AL_PLAYING);
|
||||
}
|
||||
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
||||
{
|
||||
// If there is no source, then there is nothing that is playing
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return false;
|
||||
|
||||
float SimpleAudioEngine::getBackgroundMusicVolume()
|
||||
{
|
||||
return s_volume;
|
||||
}
|
||||
ALint play_status;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status);
|
||||
checkALError("isBackgroundMusicPlaying:alGetSourcei");
|
||||
|
||||
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
||||
{
|
||||
if (s_volume != volume && volume >= -0.0001 && volume <= 1.0001)
|
||||
{
|
||||
s_volume = volume;
|
||||
return (play_status == AL_PLAYING);
|
||||
}
|
||||
|
||||
setBackgroundVolume(volume);
|
||||
}
|
||||
}
|
||||
float SimpleAudioEngine::getBackgroundMusicVolume()
|
||||
{
|
||||
return s_volume;
|
||||
}
|
||||
|
||||
//
|
||||
// Effect audio (using OpenAL)
|
||||
//
|
||||
float SimpleAudioEngine::getEffectsVolume()
|
||||
{
|
||||
return s_effectVolume;
|
||||
}
|
||||
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
||||
{
|
||||
if (s_volume != volume && volume >= -0.0001 && volume <= 1.0001)
|
||||
{
|
||||
s_volume = volume;
|
||||
|
||||
void SimpleAudioEngine::setEffectsVolume(float volume)
|
||||
{
|
||||
if (volume != s_effectVolume)
|
||||
{
|
||||
EffectsMap::const_iterator end = s_effects.end();
|
||||
for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
|
||||
// No source, no background music, no volume adjustment
|
||||
if (s_backgroundSource != AL_NONE)
|
||||
{
|
||||
setBackgroundVolume(volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Effect audio (using OpenAL)
|
||||
//
|
||||
float SimpleAudioEngine::getEffectsVolume()
|
||||
{
|
||||
return s_effectVolume;
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::setEffectsVolume(float volume)
|
||||
{
|
||||
if (volume != s_effectVolume)
|
||||
{
|
||||
EffectsMap::const_iterator end = s_effects.end();
|
||||
for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
|
||||
{
|
||||
alSourcef(it->second->source, AL_GAIN, volume * it->second->gain);
|
||||
}
|
||||
}
|
||||
|
||||
s_effectVolume = volume;
|
||||
}
|
||||
}
|
||||
s_effectVolume = volume;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,
|
||||
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,
|
||||
float pitch, float pan, float gain)
|
||||
{
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
preloadEffect(fullPath.c_str());
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
preloadEffect(fullPath.c_str());
|
||||
|
||||
// let's try again
|
||||
iter = s_effects.find(fullPath);
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// let's try again
|
||||
iter = s_effects.find(fullPath);
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
checkALError("playEffect:init");
|
||||
checkALError("playEffect:init");
|
||||
|
||||
soundData &d = *iter->second;
|
||||
d.isLooped = bLoop;
|
||||
d.isLooped = bLoop;
|
||||
d.pitch = pitch;
|
||||
d.pan = pan;
|
||||
d.gain = gain;
|
||||
alSourcei(d.source, AL_LOOPING, d.isLooped ? AL_TRUE : AL_FALSE);
|
||||
alSourcei(d.source, AL_LOOPING, d.isLooped ? AL_TRUE : AL_FALSE);
|
||||
alSourcef(d.source, AL_GAIN, s_effectVolume * d.gain);
|
||||
alSourcef(d.source, AL_PITCH, d.pitch);
|
||||
float sourcePosAL[] = {d.pan, 0.0f, 0.0f};//Set position - just using left and right panning
|
||||
alSourcefv(d.source, AL_POSITION, sourcePosAL);
|
||||
alSourcePlay(d.source);
|
||||
checkALError("playEffect:alSourcePlay");
|
||||
alSourcePlay(d.source);
|
||||
checkALError("playEffect:alSourcePlay");
|
||||
|
||||
return d.source;
|
||||
return d.source;
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
||||
{
|
||||
alSourceStop(nSoundId);
|
||||
checkALError("stopEffect:alSourceStop");
|
||||
}
|
||||
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
||||
{
|
||||
alSourceStop(nSoundId);
|
||||
checkALError("stopEffect:alSourceStop");
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
|
||||
// check if we have this already
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
ALuint buffer = AL_NONE;
|
||||
ALuint source = AL_NONE;
|
||||
soundData *data = new soundData;
|
||||
// check if we have this already
|
||||
if (iter == s_effects.end())
|
||||
{
|
||||
ALuint buffer = AL_NONE;
|
||||
ALuint source = AL_NONE;
|
||||
soundData *data = new soundData;
|
||||
|
||||
checkALError("preloadEffect:init");
|
||||
OpenALFile file;
|
||||
|
@ -428,108 +477,108 @@ namespace CocosDenshion
|
|||
success = decoders[i]->decode(file, buffer);
|
||||
file.clear();
|
||||
|
||||
alGenSources(1, &source);
|
||||
alGenSources(1, &source);
|
||||
|
||||
if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
|
||||
{
|
||||
alDeleteBuffers(1, &buffer);
|
||||
return;
|
||||
}
|
||||
if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
|
||||
{
|
||||
alDeleteBuffers(1, &buffer);
|
||||
return;
|
||||
}
|
||||
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
checkALError("preloadEffect:alSourcei");
|
||||
alSourcei(source, AL_BUFFER, buffer);
|
||||
checkALError("preloadEffect:alSourcei");
|
||||
|
||||
data->isLooped = false;
|
||||
data->buffer = buffer;
|
||||
data->source = source;
|
||||
data->isLooped = false;
|
||||
data->buffer = buffer;
|
||||
data->source = source;
|
||||
data->pitch = 1.0;
|
||||
data->pan = 0.0;
|
||||
data->gain = 1.0;
|
||||
|
||||
s_effects.insert(EffectsMap::value_type(fullPath, data));
|
||||
}
|
||||
}
|
||||
s_effects.insert(EffectsMap::value_type(fullPath, data));
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
||||
{
|
||||
// Changing file path to full path
|
||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||
|
||||
if (iter != s_effects.end())
|
||||
{
|
||||
checkALError("unloadEffect:init");
|
||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||
|
||||
alSourceStop(iter->second->source);
|
||||
checkALError("unloadEffect:alSourceStop");
|
||||
if (iter != s_effects.end())
|
||||
{
|
||||
checkALError("unloadEffect:init");
|
||||
|
||||
alDeleteSources(1, &iter->second->source);
|
||||
checkALError("unloadEffect:DeletSources");
|
||||
alSourceStop(iter->second->source);
|
||||
checkALError("unloadEffect:alSourceStop");
|
||||
|
||||
alDeleteBuffers(1, &iter->second->buffer);
|
||||
checkALError("unloadEffect:alDeleteBuffers");
|
||||
delete iter->second;
|
||||
alDeleteSources(1, &iter->second->source);
|
||||
checkALError("unloadEffect:DeletSources");
|
||||
|
||||
s_effects.erase(iter);
|
||||
}
|
||||
}
|
||||
alDeleteBuffers(1, &iter->second->buffer);
|
||||
checkALError("unloadEffect:alDeleteBuffers");
|
||||
delete iter->second;
|
||||
|
||||
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(nSoundId);
|
||||
checkALError("pauseEffect:alSourcePause");
|
||||
}
|
||||
s_effects.erase(iter);
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::pauseAllEffects()
|
||||
{
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
ALint state;
|
||||
while (iter != s_effects.end())
|
||||
{
|
||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(iter->second->source);
|
||||
checkALError("pauseAllEffects:alSourcePause");
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(nSoundId);
|
||||
checkALError("pauseEffect:alSourcePause");
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(nSoundId);
|
||||
checkALError("resumeEffect:alSourcePlay");
|
||||
}
|
||||
void SimpleAudioEngine::pauseAllEffects()
|
||||
{
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
ALint state;
|
||||
while (iter != s_effects.end())
|
||||
{
|
||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
alSourcePause(iter->second->source);
|
||||
checkALError("pauseAllEffects:alSourcePause");
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::resumeAllEffects()
|
||||
{
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
ALint state;
|
||||
while (iter != s_effects.end())
|
||||
{
|
||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(iter->second->source);
|
||||
checkALError("resumeAllEffects:alSourcePlay");
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
||||
{
|
||||
ALint state;
|
||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(nSoundId);
|
||||
checkALError("resumeEffect:alSourcePlay");
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::resumeAllEffects()
|
||||
{
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
ALint state;
|
||||
while (iter != s_effects.end())
|
||||
{
|
||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PAUSED)
|
||||
alSourcePlay(iter->second->source);
|
||||
checkALError("resumeAllEffects:alSourcePlay");
|
||||
++iter;
|
||||
}
|
||||
}
|
||||
|
||||
void SimpleAudioEngine::stopAllEffects()
|
||||
{
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
EffectsMap::iterator iter = s_effects.begin();
|
||||
|
||||
if (iter != s_effects.end())
|
||||
{
|
||||
checkALError("stopAllEffects:init");
|
||||
alSourceStop(iter->second->source);
|
||||
checkALError("stopAllEffects:alSourceStop");
|
||||
}
|
||||
if (iter != s_effects.end())
|
||||
{
|
||||
checkALError("stopAllEffects:init");
|
||||
alSourceStop(iter->second->source);
|
||||
checkALError("stopAllEffects:alSourceStop");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -246,7 +246,7 @@ void CocosDenshionTest::onExit()
|
|||
{
|
||||
Layer::onExit();
|
||||
|
||||
SimpleAudioEngine::getInstance()->end();
|
||||
SimpleAudioEngine::end();
|
||||
}
|
||||
|
||||
void CocosDenshionTest::addButtons()
|
||||
|
|
Loading…
Reference in New Issue