mirror of https://github.com/axmolengine/axmol.git
issue #2415: Formating codes in SimpleAudioEngineOpenAL.cpp.
This commit is contained in:
parent
a60c3eaa20
commit
8c182d257d
|
@ -44,184 +44,184 @@ using namespace std;
|
||||||
|
|
||||||
namespace CocosDenshion
|
namespace CocosDenshion
|
||||||
{
|
{
|
||||||
struct soundData {
|
struct soundData {
|
||||||
ALuint buffer;
|
ALuint buffer;
|
||||||
ALuint source;
|
ALuint source;
|
||||||
bool isLooped;
|
bool isLooped;
|
||||||
float pitch;
|
float pitch;
|
||||||
float pan;
|
float pan;
|
||||||
float gain;
|
float gain;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef map<string, soundData *> EffectsMap;
|
typedef map<string, soundData *> EffectsMap;
|
||||||
EffectsMap s_effects;
|
EffectsMap s_effects;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
PLAYING,
|
PLAYING,
|
||||||
STOPPED,
|
STOPPED,
|
||||||
PAUSED,
|
PAUSED,
|
||||||
} playStatus;
|
} playStatus;
|
||||||
|
|
||||||
static float s_volume = 1.0f;
|
static float s_volume = 1.0f;
|
||||||
static float s_effectVolume = 1.0f;
|
static float s_effectVolume = 1.0f;
|
||||||
|
|
||||||
struct backgroundMusicData {
|
struct backgroundMusicData {
|
||||||
ALuint buffer;
|
ALuint buffer;
|
||||||
ALuint source;
|
ALuint source;
|
||||||
};
|
};
|
||||||
typedef map<string, backgroundMusicData *> BackgroundMusicsMap;
|
typedef map<string, backgroundMusicData *> BackgroundMusicsMap;
|
||||||
BackgroundMusicsMap s_backgroundMusics;
|
BackgroundMusicsMap s_backgroundMusics;
|
||||||
|
|
||||||
static ALuint s_backgroundSource = AL_NONE;
|
static ALuint s_backgroundSource = AL_NONE;
|
||||||
|
|
||||||
static SimpleAudioEngine *s_engine = nullptr;
|
static SimpleAudioEngine *s_engine = nullptr;
|
||||||
|
|
||||||
static int checkALError(const char *funcName)
|
static int checkALError(const char *funcName)
|
||||||
{
|
{
|
||||||
int err = alGetError();
|
int err = alGetError();
|
||||||
|
|
||||||
if (err != AL_NO_ERROR)
|
if (err != AL_NO_ERROR)
|
||||||
{
|
{
|
||||||
switch (err)
|
switch (err)
|
||||||
{
|
{
|
||||||
case AL_INVALID_NAME:
|
case AL_INVALID_NAME:
|
||||||
fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName);
|
fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AL_INVALID_ENUM:
|
case AL_INVALID_ENUM:
|
||||||
fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName);
|
fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AL_INVALID_VALUE:
|
case AL_INVALID_VALUE:
|
||||||
fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName);
|
fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AL_INVALID_OPERATION:
|
case AL_INVALID_OPERATION:
|
||||||
fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName);
|
fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AL_OUT_OF_MEMORY:
|
case AL_OUT_OF_MEMORY:
|
||||||
fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName);
|
fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stopBackground(bool bReleaseData)
|
static void stopBackground(bool bReleaseData)
|
||||||
{
|
{
|
||||||
// The background music might have been already stopped
|
// The background music might have been already stopped
|
||||||
// Stop request can come from
|
// Stop request can come from
|
||||||
// - stopBackgroundMusic(..)
|
// - stopBackgroundMusic(..)
|
||||||
// - end(..)
|
// - end(..)
|
||||||
if (s_backgroundSource != AL_NONE)
|
if (s_backgroundSource != AL_NONE)
|
||||||
alSourceStop(s_backgroundSource);
|
alSourceStop(s_backgroundSource);
|
||||||
|
|
||||||
if (bReleaseData)
|
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)
|
if (it->second->source == s_backgroundSource)
|
||||||
{
|
{
|
||||||
alDeleteSources(1, &it->second->source);
|
alDeleteSources(1, &it->second->source);
|
||||||
checkALError("stopBackground:alDeleteSources");
|
checkALError("stopBackground:alDeleteSources");
|
||||||
alDeleteBuffers(1, &it->second->buffer);
|
alDeleteBuffers(1, &it->second->buffer);
|
||||||
checkALError("stopBackground:alDeleteBuffers");
|
checkALError("stopBackground:alDeleteBuffers");
|
||||||
delete it->second;
|
delete it->second;
|
||||||
s_backgroundMusics.erase(it);
|
s_backgroundMusics.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s_backgroundSource = AL_NONE;
|
s_backgroundSource = AL_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setBackgroundVolume(float volume)
|
static void setBackgroundVolume(float volume)
|
||||||
{
|
{
|
||||||
alSourcef(s_backgroundSource, AL_GAIN, volume);
|
alSourcef(s_backgroundSource, AL_GAIN, volume);
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleAudioEngine::SimpleAudioEngine()
|
SimpleAudioEngine::SimpleAudioEngine()
|
||||||
{
|
{
|
||||||
alutInit(0, 0);
|
alutInit(0, 0);
|
||||||
#ifdef ENABLE_MPG123
|
#ifdef ENABLE_MPG123
|
||||||
mpg123_init();
|
mpg123_init();
|
||||||
#endif
|
#endif
|
||||||
checkALError("SimpleAudioEngine:alutInit");
|
checkALError("SimpleAudioEngine:alutInit");
|
||||||
OpenALDecoder::installDecoders();
|
OpenALDecoder::installDecoders();
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleAudioEngine::~SimpleAudioEngine()
|
SimpleAudioEngine::~SimpleAudioEngine()
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_MPG123
|
#ifdef ENABLE_MPG123
|
||||||
mpg123_exit();
|
mpg123_exit();
|
||||||
#endif
|
#endif
|
||||||
alutExit();
|
alutExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
||||||
{
|
{
|
||||||
if (!s_engine)
|
if (!s_engine)
|
||||||
s_engine = new SimpleAudioEngine();
|
s_engine = new SimpleAudioEngine();
|
||||||
|
|
||||||
return s_engine;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleAudioEngine::end()
|
return s_engine;
|
||||||
{
|
}
|
||||||
checkALError("end:init");
|
|
||||||
|
|
||||||
// clear all the sound effects
|
void SimpleAudioEngine::end()
|
||||||
EffectsMap::const_iterator end = s_effects.end();
|
{
|
||||||
for (auto it = s_effects.begin(); it != end; ++it)
|
checkALError("end:init");
|
||||||
{
|
|
||||||
alSourceStop(it->second->source);
|
|
||||||
checkALError("end:alSourceStop");
|
|
||||||
|
|
||||||
alDeleteSources(1, &it->second->source);
|
// clear all the sound effects
|
||||||
checkALError("end:alDeleteSources");
|
EffectsMap::const_iterator end = s_effects.end();
|
||||||
|
for (auto it = s_effects.begin(); it != end; ++it)
|
||||||
|
{
|
||||||
|
alSourceStop(it->second->source);
|
||||||
|
checkALError("end:alSourceStop");
|
||||||
|
|
||||||
alDeleteBuffers(1, &it->second->buffer);
|
alDeleteSources(1, &it->second->source);
|
||||||
checkALError("end:alDeleteBuffers");
|
checkALError("end:alDeleteSources");
|
||||||
|
|
||||||
delete it->second;
|
alDeleteBuffers(1, &it->second->buffer);
|
||||||
}
|
checkALError("end:alDeleteBuffers");
|
||||||
s_effects.clear();
|
|
||||||
|
|
||||||
// and the background music too
|
delete it->second;
|
||||||
stopBackground(true);
|
}
|
||||||
|
s_effects.clear();
|
||||||
|
|
||||||
for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
// and the background music too
|
||||||
{
|
stopBackground(true);
|
||||||
alSourceStop(it->second->source);
|
|
||||||
checkALError("end:alSourceStop");
|
|
||||||
|
|
||||||
alDeleteSources(1, &it->second->source);
|
for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
||||||
checkALError("end:alDeleteSources");
|
{
|
||||||
|
alSourceStop(it->second->source);
|
||||||
|
checkALError("end:alSourceStop");
|
||||||
|
|
||||||
alDeleteBuffers(1, &it->second->buffer);
|
alDeleteSources(1, &it->second->source);
|
||||||
checkALError("end:alDeleteBuffers");
|
checkALError("end:alDeleteSources");
|
||||||
|
|
||||||
delete it->second;
|
alDeleteBuffers(1, &it->second->buffer);
|
||||||
}
|
checkALError("end:alDeleteBuffers");
|
||||||
s_backgroundMusics.clear();
|
|
||||||
|
delete it->second;
|
||||||
|
}
|
||||||
|
s_backgroundMusics.clear();
|
||||||
|
|
||||||
CC_SAFE_DELETE(s_engine);
|
CC_SAFE_DELETE(s_engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// background audio
|
// background audio
|
||||||
//
|
//
|
||||||
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
||||||
{
|
{
|
||||||
// Changing file path to full path
|
// Changing file path to full path
|
||||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||||
|
|
||||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||||
if (it == s_backgroundMusics.end())
|
if (it == s_backgroundMusics.end())
|
||||||
{
|
{
|
||||||
ALuint buffer = AL_NONE;
|
ALuint buffer = AL_NONE;
|
||||||
bool success = false;
|
bool success = false;
|
||||||
OpenALFile file;
|
OpenALFile file;
|
||||||
|
@ -237,48 +237,48 @@ namespace CocosDenshion
|
||||||
success = decoders[i]->decode(file, buffer);
|
success = decoders[i]->decode(file, buffer);
|
||||||
file.clear();
|
file.clear();
|
||||||
|
|
||||||
ALuint source = AL_NONE;
|
ALuint source = AL_NONE;
|
||||||
alGenSources(1, &source);
|
alGenSources(1, &source);
|
||||||
checkALError("preloadBackgroundMusic:alGenSources");
|
checkALError("preloadBackgroundMusic:alGenSources");
|
||||||
|
|
||||||
alSourcei(source, AL_BUFFER, buffer);
|
alSourcei(source, AL_BUFFER, buffer);
|
||||||
checkALError("preloadBackgroundMusic:alSourcei");
|
checkALError("preloadBackgroundMusic:alSourcei");
|
||||||
|
|
||||||
backgroundMusicData* data = new backgroundMusicData();
|
backgroundMusicData* data = new backgroundMusicData();
|
||||||
data->buffer = buffer;
|
data->buffer = buffer;
|
||||||
data->source = source;
|
data->source = source;
|
||||||
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
|
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop)
|
||||||
{
|
{
|
||||||
// If there is already a background music source we stop it first
|
// If there is already a background music source we stop it first
|
||||||
if (s_backgroundSource != AL_NONE)
|
if (s_backgroundSource != AL_NONE)
|
||||||
stopBackgroundMusic(false);
|
stopBackgroundMusic(false);
|
||||||
|
|
||||||
// Changing file path to full path
|
// Changing file path to full path
|
||||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||||
|
|
||||||
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
||||||
if (it == s_backgroundMusics.end())
|
if (it == s_backgroundMusics.end())
|
||||||
{
|
{
|
||||||
preloadBackgroundMusic(fullPath.c_str());
|
preloadBackgroundMusic(fullPath.c_str());
|
||||||
it = s_backgroundMusics.find(fullPath);
|
it = s_backgroundMusics.find(fullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it != s_backgroundMusics.end())
|
if (it != s_backgroundMusics.end())
|
||||||
{
|
{
|
||||||
s_backgroundSource = it->second->source;
|
s_backgroundSource = it->second->source;
|
||||||
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
||||||
setBackgroundVolume(s_volume);
|
setBackgroundVolume(s_volume);
|
||||||
alSourcePlay(s_backgroundSource);
|
alSourcePlay(s_backgroundSource);
|
||||||
checkALError("playBackgroundMusic:alSourcePlay");
|
checkALError("playBackgroundMusic:alSourcePlay");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData)
|
||||||
{
|
{
|
||||||
// If there is no source, then there is nothing that can be stopped
|
// If there is no source, then there is nothing that can be stopped
|
||||||
if (s_backgroundSource == AL_NONE)
|
if (s_backgroundSource == AL_NONE)
|
||||||
return;
|
return;
|
||||||
|
@ -287,33 +287,33 @@ namespace CocosDenshion
|
||||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||||
if (state == AL_PLAYING)
|
if (state == AL_PLAYING)
|
||||||
stopBackground(bReleaseData);
|
stopBackground(bReleaseData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::pauseBackgroundMusic()
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
||||||
{
|
{
|
||||||
// If there is no source, then there is nothing that can be paused
|
// If there is no source, then there is nothing that can be paused
|
||||||
if (s_backgroundSource == AL_NONE)
|
if (s_backgroundSource == AL_NONE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ALint state;
|
ALint state;
|
||||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||||
if (state == AL_PLAYING)
|
if (state == AL_PLAYING)
|
||||||
alSourcePause(s_backgroundSource);
|
alSourcePause(s_backgroundSource);
|
||||||
|
|
||||||
checkALError("pauseBackgroundMusic:alSourcePause");
|
checkALError("pauseBackgroundMusic:alSourcePause");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::resumeBackgroundMusic()
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
||||||
{
|
{
|
||||||
// If there is no source, then there is nothing that can be resumed
|
// If there is no source, then there is nothing that can be resumed
|
||||||
if (s_backgroundSource == AL_NONE)
|
if (s_backgroundSource == AL_NONE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ALint state;
|
ALint state;
|
||||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
||||||
if (state == AL_PAUSED)
|
if (state == AL_PAUSED)
|
||||||
alSourcePlay(s_backgroundSource);
|
alSourcePlay(s_backgroundSource);
|
||||||
|
|
||||||
checkALError("resumeBackgroundMusic:alSourcePlay");
|
checkALError("resumeBackgroundMusic:alSourcePlay");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -340,40 +340,38 @@ namespace CocosDenshion
|
||||||
checkALError("rewindBackgroundMusic:alSourceRewind");
|
checkALError("rewindBackgroundMusic:alSourceRewind");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
||||||
{
|
{
|
||||||
// We are able to play background music
|
// We are able to play background music
|
||||||
// if we have a valid background source
|
// if we have a valid background source
|
||||||
if (s_backgroundSource == AL_NONE)
|
if (s_backgroundSource == AL_NONE)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return (alIsSource(s_backgroundSource) == AL_TRUE ? true : false);
|
return (alIsSource(s_backgroundSource) == AL_TRUE ? true : false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
bool SimpleAudioEngine::isBackgroundMusicPlaying()
|
||||||
{
|
{
|
||||||
// If there is no source, then there is nothing that is playing
|
// If there is no source, then there is nothing that is playing
|
||||||
if (s_backgroundSource == AL_NONE)
|
if (s_backgroundSource == AL_NONE)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
ALint play_status;
|
ALint play_status;
|
||||||
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status);
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status);
|
||||||
checkALError("isBackgroundMusicPlaying:alGetSourcei");
|
checkALError("isBackgroundMusicPlaying:alGetSourcei");
|
||||||
|
|
||||||
return (play_status == AL_PLAYING);
|
return (play_status == AL_PLAYING);
|
||||||
}
|
}
|
||||||
|
|
||||||
float SimpleAudioEngine::getBackgroundMusicVolume()
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
||||||
{
|
{
|
||||||
return s_volume;
|
return s_volume;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
||||||
{
|
{
|
||||||
|
if (s_volume != volume && volume >= -0.0001 && volume <= 1.0001)
|
||||||
|
{
|
||||||
if (s_volume != volume && volume >= -0.0001 && volume <= 1.0001)
|
|
||||||
{
|
|
||||||
s_volume = volume;
|
s_volume = volume;
|
||||||
|
|
||||||
// No source, no background music, no volume adjustment
|
// No source, no background music, no volume adjustment
|
||||||
|
@ -381,88 +379,88 @@ namespace CocosDenshion
|
||||||
{
|
{
|
||||||
setBackgroundVolume(volume);
|
setBackgroundVolume(volume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Effect audio (using OpenAL)
|
// Effect audio (using OpenAL)
|
||||||
//
|
//
|
||||||
float SimpleAudioEngine::getEffectsVolume()
|
float SimpleAudioEngine::getEffectsVolume()
|
||||||
{
|
{
|
||||||
return s_effectVolume;
|
return s_effectVolume;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::setEffectsVolume(float volume)
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
||||||
{
|
{
|
||||||
if (volume != s_effectVolume)
|
if (volume != s_effectVolume)
|
||||||
{
|
{
|
||||||
EffectsMap::const_iterator end = s_effects.end();
|
EffectsMap::const_iterator end = s_effects.end();
|
||||||
for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
|
for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
|
||||||
{
|
{
|
||||||
alSourcef(it->second->source, AL_GAIN, volume * it->second->gain);
|
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)
|
float pitch, float pan, float gain)
|
||||||
{
|
{
|
||||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
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())
|
if (iter == s_effects.end())
|
||||||
{
|
{
|
||||||
preloadEffect(fullPath.c_str());
|
preloadEffect(fullPath.c_str());
|
||||||
|
|
||||||
// let's try again
|
// let's try again
|
||||||
iter = s_effects.find(fullPath);
|
iter = s_effects.find(fullPath);
|
||||||
if (iter == s_effects.end())
|
if (iter == s_effects.end())
|
||||||
{
|
{
|
||||||
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
|
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkALError("playEffect:init");
|
checkALError("playEffect:init");
|
||||||
|
|
||||||
soundData &d = *iter->second;
|
soundData &d = *iter->second;
|
||||||
d.isLooped = bLoop;
|
d.isLooped = bLoop;
|
||||||
d.pitch = pitch;
|
d.pitch = pitch;
|
||||||
d.pan = pan;
|
d.pan = pan;
|
||||||
d.gain = gain;
|
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_GAIN, s_effectVolume * d.gain);
|
||||||
alSourcef(d.source, AL_PITCH, d.pitch);
|
alSourcef(d.source, AL_PITCH, d.pitch);
|
||||||
float sourcePosAL[] = {d.pan, 0.0f, 0.0f};//Set position - just using left and right panning
|
float sourcePosAL[] = {d.pan, 0.0f, 0.0f};//Set position - just using left and right panning
|
||||||
alSourcefv(d.source, AL_POSITION, sourcePosAL);
|
alSourcefv(d.source, AL_POSITION, sourcePosAL);
|
||||||
alSourcePlay(d.source);
|
alSourcePlay(d.source);
|
||||||
checkALError("playEffect:alSourcePlay");
|
checkALError("playEffect:alSourcePlay");
|
||||||
|
|
||||||
return d.source;
|
return d.source;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
||||||
{
|
{
|
||||||
alSourceStop(nSoundId);
|
alSourceStop(nSoundId);
|
||||||
checkALError("stopEffect:alSourceStop");
|
checkALError("stopEffect:alSourceStop");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
||||||
{
|
{
|
||||||
// Changing file path to full path
|
// Changing file path to full path
|
||||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
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
|
// check if we have this already
|
||||||
if (iter == s_effects.end())
|
if (iter == s_effects.end())
|
||||||
{
|
{
|
||||||
ALuint buffer = AL_NONE;
|
ALuint buffer = AL_NONE;
|
||||||
ALuint source = AL_NONE;
|
ALuint source = AL_NONE;
|
||||||
soundData *data = new soundData;
|
soundData *data = new soundData;
|
||||||
|
|
||||||
checkALError("preloadEffect:init");
|
checkALError("preloadEffect:init");
|
||||||
OpenALFile file;
|
OpenALFile file;
|
||||||
|
@ -479,108 +477,108 @@ namespace CocosDenshion
|
||||||
success = decoders[i]->decode(file, buffer);
|
success = decoders[i]->decode(file, buffer);
|
||||||
file.clear();
|
file.clear();
|
||||||
|
|
||||||
alGenSources(1, &source);
|
alGenSources(1, &source);
|
||||||
|
|
||||||
if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
|
if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
|
||||||
{
|
{
|
||||||
alDeleteBuffers(1, &buffer);
|
alDeleteBuffers(1, &buffer);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
alSourcei(source, AL_BUFFER, buffer);
|
alSourcei(source, AL_BUFFER, buffer);
|
||||||
checkALError("preloadEffect:alSourcei");
|
checkALError("preloadEffect:alSourcei");
|
||||||
|
|
||||||
data->isLooped = false;
|
data->isLooped = false;
|
||||||
data->buffer = buffer;
|
data->buffer = buffer;
|
||||||
data->source = source;
|
data->source = source;
|
||||||
data->pitch = 1.0;
|
data->pitch = 1.0;
|
||||||
data->pan = 0.0;
|
data->pan = 0.0;
|
||||||
data->gain = 1.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)
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
||||||
{
|
{
|
||||||
// Changing file path to full path
|
// Changing file path to full path
|
||||||
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
||||||
|
|
||||||
EffectsMap::iterator iter = s_effects.find(fullPath);
|
|
||||||
|
|
||||||
if (iter != s_effects.end())
|
EffectsMap::iterator iter = s_effects.find(fullPath);
|
||||||
{
|
|
||||||
checkALError("unloadEffect:init");
|
|
||||||
|
|
||||||
alSourceStop(iter->second->source);
|
if (iter != s_effects.end())
|
||||||
checkALError("unloadEffect:alSourceStop");
|
{
|
||||||
|
checkALError("unloadEffect:init");
|
||||||
|
|
||||||
alDeleteSources(1, &iter->second->source);
|
alSourceStop(iter->second->source);
|
||||||
checkALError("unloadEffect:DeletSources");
|
checkALError("unloadEffect:alSourceStop");
|
||||||
|
|
||||||
alDeleteBuffers(1, &iter->second->buffer);
|
alDeleteSources(1, &iter->second->source);
|
||||||
checkALError("unloadEffect:alDeleteBuffers");
|
checkALError("unloadEffect:DeletSources");
|
||||||
delete iter->second;
|
|
||||||
|
|
||||||
s_effects.erase(iter);
|
alDeleteBuffers(1, &iter->second->buffer);
|
||||||
}
|
checkALError("unloadEffect:alDeleteBuffers");
|
||||||
}
|
delete iter->second;
|
||||||
|
|
||||||
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
s_effects.erase(iter);
|
||||||
{
|
}
|
||||||
ALint state;
|
}
|
||||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
|
||||||
if (state == AL_PLAYING)
|
|
||||||
alSourcePause(nSoundId);
|
|
||||||
checkALError("pauseEffect:alSourcePause");
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleAudioEngine::pauseAllEffects()
|
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
||||||
{
|
{
|
||||||
EffectsMap::iterator iter = s_effects.begin();
|
ALint state;
|
||||||
ALint state;
|
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||||
while (iter != s_effects.end())
|
if (state == AL_PLAYING)
|
||||||
{
|
alSourcePause(nSoundId);
|
||||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
checkALError("pauseEffect:alSourcePause");
|
||||||
if (state == AL_PLAYING)
|
}
|
||||||
alSourcePause(iter->second->source);
|
|
||||||
checkALError("pauseAllEffects:alSourcePause");
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
void SimpleAudioEngine::pauseAllEffects()
|
||||||
{
|
{
|
||||||
ALint state;
|
EffectsMap::iterator iter = s_effects.begin();
|
||||||
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
ALint state;
|
||||||
if (state == AL_PAUSED)
|
while (iter != s_effects.end())
|
||||||
alSourcePlay(nSoundId);
|
{
|
||||||
checkALError("resumeEffect:alSourcePlay");
|
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
||||||
}
|
if (state == AL_PLAYING)
|
||||||
|
alSourcePause(iter->second->source);
|
||||||
|
checkALError("pauseAllEffects:alSourcePause");
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SimpleAudioEngine::resumeAllEffects()
|
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
||||||
{
|
{
|
||||||
EffectsMap::iterator iter = s_effects.begin();
|
ALint state;
|
||||||
ALint state;
|
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
||||||
while (iter != s_effects.end())
|
if (state == AL_PAUSED)
|
||||||
{
|
alSourcePlay(nSoundId);
|
||||||
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
checkALError("resumeEffect:alSourcePlay");
|
||||||
if (state == AL_PAUSED)
|
}
|
||||||
alSourcePlay(iter->second->source);
|
|
||||||
checkALError("resumeAllEffects:alSourcePlay");
|
void SimpleAudioEngine::resumeAllEffects()
|
||||||
++iter;
|
{
|
||||||
}
|
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()
|
void SimpleAudioEngine::stopAllEffects()
|
||||||
{
|
{
|
||||||
EffectsMap::iterator iter = s_effects.begin();
|
EffectsMap::iterator iter = s_effects.begin();
|
||||||
|
|
||||||
if (iter != s_effects.end())
|
if (iter != s_effects.end())
|
||||||
{
|
{
|
||||||
checkALError("stopAllEffects:init");
|
checkALError("stopAllEffects:init");
|
||||||
alSourceStop(iter->second->source);
|
alSourceStop(iter->second->source);
|
||||||
checkALError("stopAllEffects:alSourceStop");
|
checkALError("stopAllEffects:alSourceStop");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue