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);
|
||||
|
||||
|
@ -123,46 +125,55 @@ public:
|
|||
*/
|
||||
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
|
||||
@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);
|
||||
};
|
||||
|
|
|
@ -74,7 +74,7 @@ namespace CocosDenshion
|
|||
|
||||
static ALuint s_backgroundSource = AL_NONE;
|
||||
|
||||
static SimpleAudioEngine *s_engine = 0;
|
||||
static SimpleAudioEngine *s_engine = nullptr;
|
||||
|
||||
static int checkALError(const char *funcName)
|
||||
{
|
||||
|
@ -111,18 +111,23 @@ namespace CocosDenshion
|
|||
|
||||
static void stopBackground(bool bReleaseData)
|
||||
{
|
||||
// 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)
|
||||
for (auto 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");
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("stopBackground:alDeleteBuffers");
|
||||
delete it->second;
|
||||
s_backgroundMusics.erase(it);
|
||||
break;
|
||||
|
@ -168,34 +173,42 @@ namespace CocosDenshion
|
|||
{
|
||||
checkALError("end:init");
|
||||
|
||||
// clear all the sounds
|
||||
// clear all the sound effects
|
||||
EffectsMap::const_iterator end = s_effects.end();
|
||||
for (EffectsMap::iterator it = s_effects.begin(); it != end; it++)
|
||||
for (auto 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");
|
||||
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
|
||||
delete it->second;
|
||||
}
|
||||
s_effects.clear();
|
||||
|
||||
// and the background too
|
||||
// and the background music too
|
||||
stopBackground(true);
|
||||
|
||||
for (BackgroundMusicsMap::iterator it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||
for (auto 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");
|
||||
|
||||
alDeleteBuffers(1, &it->second->buffer);
|
||||
checkALError("end:alDeleteBuffers");
|
||||
|
||||
delete it->second;
|
||||
}
|
||||
s_backgroundMusics.clear();
|
||||
|
||||
CC_SAFE_DELETE(s_engine);
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -240,6 +253,7 @@ namespace CocosDenshion
|
|||
|
||||
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);
|
||||
|
||||
|
@ -257,6 +271,7 @@ namespace CocosDenshion
|
|||
{
|
||||
s_backgroundSource = it->second->source;
|
||||
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
||||
setBackgroundVolume(s_volume);
|
||||
alSourcePlay(s_backgroundSource);
|
||||
checkALError("playBackgroundMusic:alSourcePlay");
|
||||
}
|
||||
|
@ -264,6 +279,10 @@ namespace CocosDenshion
|
|||
|
||||
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
||||
{
|
||||
// If there is no source, then there is nothing that can be stopped
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
ALint state;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||
if (state == AL_PLAYING)
|
||||
|
@ -272,27 +291,43 @@ namespace CocosDenshion
|
|||
|
||||
void SimpleAudioEngine::pauseBackgroundMusic()
|
||||
{
|
||||
// If there is no source, then there is nothing that can be paused
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return;
|
||||
|
||||
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);
|
||||
|
@ -307,13 +342,23 @@ namespace CocosDenshion
|
|||
|
||||
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
||||
{
|
||||
return true;
|
||||
// We are able to play background music
|
||||
// if we have a valid background source
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return false;
|
||||
|
||||
return (alIsSource(s_backgroundSource) == AL_TRUE ? true : false);
|
||||
}
|
||||
|
||||
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
||||
{
|
||||
// If there is no source, then there is nothing that is playing
|
||||
if (s_backgroundSource == AL_NONE)
|
||||
return false;
|
||||
|
||||
ALint play_status;
|
||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status);
|
||||
checkALError("isBackgroundMusicPlaying:alGetSourcei");
|
||||
|
||||
return (play_status == AL_PLAYING);
|
||||
}
|
||||
|
@ -329,9 +374,13 @@ namespace CocosDenshion
|
|||
{
|
||||
s_volume = volume;
|
||||
|
||||
// No source, no background music, no volume adjustment
|
||||
if (s_backgroundSource != AL_NONE)
|
||||
{
|
||||
setBackgroundVolume(volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Effect audio (using OpenAL)
|
||||
|
|
|
@ -246,7 +246,7 @@ void CocosDenshionTest::onExit()
|
|||
{
|
||||
Layer::onExit();
|
||||
|
||||
SimpleAudioEngine::getInstance()->end();
|
||||
SimpleAudioEngine::end();
|
||||
}
|
||||
|
||||
void CocosDenshionTest::addButtons()
|
||||
|
|
Loading…
Reference in New Issue