2013-06-22 04:01:00 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2013-07-22 11:36:20 +08:00
|
|
|
#include "SimpleAudioEngine.h"
|
2013-06-22 04:01:00 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
|
|
|
#include <AL/alut.h>
|
|
|
|
#include "OpenALDecoder.h"
|
|
|
|
|
|
|
|
#ifdef ENABLE_MPG123
|
|
|
|
#include <mpg123.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
namespace CocosDenshion {
|
|
|
|
|
|
|
|
struct soundData {
|
|
|
|
ALuint buffer;
|
|
|
|
ALuint source;
|
|
|
|
bool isLooped;
|
|
|
|
float pitch;
|
|
|
|
float pan;
|
|
|
|
float gain;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef map<string, soundData *> EffectsMap;
|
|
|
|
EffectsMap s_effects;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PLAYING,
|
|
|
|
STOPPED,
|
|
|
|
PAUSED,
|
|
|
|
} playStatus;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
static ALuint s_backgroundSource = AL_NONE;
|
|
|
|
|
|
|
|
static SimpleAudioEngine *s_engine = nullptr;
|
|
|
|
|
|
|
|
static int checkALError(const char *funcName)
|
2013-06-22 04:01:00 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
int err = alGetError();
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (err != AL_NO_ERROR)
|
|
|
|
{
|
|
|
|
switch (err)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
case AL_INVALID_NAME:
|
|
|
|
fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName);
|
|
|
|
break;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
case AL_INVALID_ENUM:
|
|
|
|
fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName);
|
|
|
|
break;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
case AL_INVALID_VALUE:
|
|
|
|
fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName);
|
|
|
|
break;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
case AL_INVALID_OPERATION:
|
|
|
|
fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName);
|
|
|
|
break;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
case AL_OUT_OF_MEMORY:
|
|
|
|
fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName);
|
|
|
|
break;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
return err;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
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 (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
if (it->second->source == s_backgroundSource)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteSources(1, &it->second->source);
|
|
|
|
checkALError("stopBackground:alDeleteSources");
|
|
|
|
alDeleteBuffers(1, &it->second->buffer);
|
|
|
|
checkALError("stopBackground:alDeleteBuffers");
|
|
|
|
delete it->second;
|
|
|
|
s_backgroundMusics.erase(it);
|
|
|
|
break;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
s_backgroundSource = AL_NONE;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
static void setBackgroundVolume(float volume)
|
|
|
|
{
|
|
|
|
alSourcef(s_backgroundSource, AL_GAIN, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleAudioEngine::SimpleAudioEngine()
|
|
|
|
{
|
|
|
|
alutInit(0, 0);
|
2013-06-22 04:01:00 +08:00
|
|
|
#ifdef ENABLE_MPG123
|
2013-07-22 11:36:20 +08:00
|
|
|
mpg123_init();
|
2013-06-22 04:01:00 +08:00
|
|
|
#endif
|
2013-07-22 11:36:20 +08:00
|
|
|
checkALError("SimpleAudioEngine:alutInit");
|
|
|
|
OpenALDecoder::installDecoders();
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
SimpleAudioEngine::~SimpleAudioEngine()
|
|
|
|
{
|
2013-06-22 04:01:00 +08:00
|
|
|
#ifdef ENABLE_MPG123
|
2013-07-22 11:36:20 +08:00
|
|
|
mpg123_exit();
|
2013-06-22 04:01:00 +08:00
|
|
|
#endif
|
2013-07-22 11:36:20 +08:00
|
|
|
alutExit();
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
SimpleAudioEngine* SimpleAudioEngine::getInstance()
|
|
|
|
{
|
|
|
|
if (!s_engine)
|
|
|
|
s_engine = new SimpleAudioEngine();
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
return s_engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::end()
|
|
|
|
{
|
|
|
|
checkALError("end:init");
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// clear all the sound effects
|
|
|
|
EffectsMap::const_iterator end = s_effects.end();
|
|
|
|
for (auto it = s_effects.begin(); it != end; ++it)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourceStop(it->second->source);
|
|
|
|
checkALError("end:alSourceStop");
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteSources(1, &it->second->source);
|
|
|
|
checkALError("end:alDeleteSources");
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteBuffers(1, &it->second->buffer);
|
|
|
|
checkALError("end:alDeleteBuffers");
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
s_effects.clear();
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// and the background music too
|
|
|
|
stopBackground(true);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
for (auto it = s_backgroundMusics.begin(); it != s_backgroundMusics.end(); ++it)
|
|
|
|
{
|
|
|
|
alSourceStop(it->second->source);
|
|
|
|
checkALError("end:alSourceStop");
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteSources(1, &it->second->source);
|
|
|
|
checkALError("end:alDeleteSources");
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteBuffers(1, &it->second->buffer);
|
|
|
|
checkALError("end:alDeleteBuffers");
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
s_backgroundMusics.clear();
|
2013-07-14 23:27:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
CC_SAFE_DELETE(s_engine);
|
|
|
|
}
|
2013-07-22 10:32:48 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
//
|
|
|
|
// background audio
|
|
|
|
//
|
|
|
|
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
// Changing file path to full path
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
|
|
|
if (it == s_backgroundMusics.end())
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
ALuint buffer = AL_NONE;
|
|
|
|
bool success = false;
|
|
|
|
OpenALFile file;
|
|
|
|
file.debugName = pszFilePath;
|
|
|
|
file.file = fopen(fullPath.c_str(), "rb");
|
|
|
|
if (!file.file) {
|
|
|
|
fprintf(stderr, "Cannot read file: '%s'\n", fullPath.data());
|
|
|
|
return;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
const std::vector<OpenALDecoder *> &decoders = OpenALDecoder::getDecoders();
|
|
|
|
for (size_t i = 0, n = decoders.size(); !success && i < n; ++i)
|
|
|
|
success = decoders[i]->decode(file, buffer);
|
|
|
|
file.clear();
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
ALuint source = AL_NONE;
|
|
|
|
alGenSources(1, &source);
|
|
|
|
checkALError("preloadBackgroundMusic:alGenSources");
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcei(source, AL_BUFFER, buffer);
|
|
|
|
checkALError("preloadBackgroundMusic:alSourcei");
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
backgroundMusicData* data = new backgroundMusicData();
|
|
|
|
data->buffer = buffer;
|
|
|
|
data->source = source;
|
|
|
|
s_backgroundMusics.insert(BackgroundMusicsMap::value_type(fullPath, data));
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
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);
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// Changing file path to full path
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
BackgroundMusicsMap::const_iterator it = s_backgroundMusics.find(fullPath);
|
|
|
|
if (it == s_backgroundMusics.end())
|
|
|
|
{
|
|
|
|
preloadBackgroundMusic(fullPath.c_str());
|
|
|
|
it = s_backgroundMusics.find(fullPath);
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (it != s_backgroundMusics.end())
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
s_backgroundSource = it->second->source;
|
|
|
|
alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE);
|
|
|
|
setBackgroundVolume(s_volume);
|
|
|
|
alSourcePlay(s_backgroundSource);
|
|
|
|
checkALError("playBackgroundMusic:alSourcePlay");
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
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)
|
|
|
|
stopBackground(bReleaseData);
|
|
|
|
}
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::pauseBackgroundMusic()
|
|
|
|
{
|
|
|
|
// If there is no source, then there is nothing that can be paused
|
|
|
|
if (s_backgroundSource == AL_NONE)
|
|
|
|
return;
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
ALint state;
|
|
|
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
|
|
|
if (state == AL_PLAYING)
|
|
|
|
alSourcePause(s_backgroundSource);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
checkALError("pauseBackgroundMusic:alSourcePause");
|
|
|
|
}
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::resumeBackgroundMusic()
|
|
|
|
{
|
|
|
|
// If there is no source, then there is nothing that can be resumed
|
|
|
|
if (s_backgroundSource == AL_NONE)
|
|
|
|
return;
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
ALint state;
|
|
|
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
|
|
|
if (state == AL_PAUSED)
|
|
|
|
alSourcePlay(s_backgroundSource);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
checkALError("resumeBackgroundMusic:alSourcePlay");
|
|
|
|
}
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::rewindBackgroundMusic()
|
|
|
|
{
|
|
|
|
// If there is no source, then there is nothing that can be rewinded
|
|
|
|
if (s_backgroundSource == AL_NONE)
|
|
|
|
return;
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// Rewind and prevent the last state the source had
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &state);
|
|
|
|
alSourceRewind(s_backgroundSource);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (state == AL_PLAYING)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcePlay(s_backgroundSource);
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
else if (state == AL_PAUSED)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcePlay(s_backgroundSource);
|
|
|
|
alSourcePause(s_backgroundSource);
|
|
|
|
}
|
|
|
|
checkALError("rewindBackgroundMusic:alSourceRewind");
|
|
|
|
}
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
bool SimpleAudioEngine::willPlayBackgroundMusic()
|
|
|
|
{
|
|
|
|
// We are able to play background music
|
|
|
|
// if we have a valid background source
|
|
|
|
if (s_backgroundSource == AL_NONE)
|
|
|
|
return false;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
return (alIsSource(s_backgroundSource) == AL_TRUE ? true : false);
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
float SimpleAudioEngine::getBackgroundMusicVolume()
|
|
|
|
{
|
|
|
|
return s_volume;
|
|
|
|
}
|
2013-07-19 07:00:24 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
|
|
|
|
{
|
|
|
|
if (s_volume != volume && volume >= -0.0001 && volume <= 1.0001)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
s_volume = volume;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// No source, no background music, no volume adjustment
|
|
|
|
if (s_backgroundSource != AL_NONE)
|
|
|
|
{
|
|
|
|
setBackgroundVolume(volume);
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
//
|
|
|
|
// Effect audio (using OpenAL)
|
|
|
|
//
|
|
|
|
float SimpleAudioEngine::getEffectsVolume()
|
|
|
|
{
|
|
|
|
return s_effectVolume;
|
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::setEffectsVolume(float volume)
|
|
|
|
{
|
|
|
|
if (volume != s_effectVolume)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
EffectsMap::const_iterator end = s_effects.end();
|
|
|
|
for (EffectsMap::const_iterator it = s_effects.begin(); it != end; it++)
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcef(it->second->source, AL_GAIN, volume * it->second->gain);
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
|
|
|
|
s_effectVolume = volume;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
unsigned int SimpleAudioEngine::playEffect(const char* pszFilePath, bool bLoop,
|
|
|
|
float pitch, float pan, float gain)
|
|
|
|
{
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
EffectsMap::iterator iter = s_effects.find(fullPath);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (iter == s_effects.end())
|
|
|
|
{
|
|
|
|
preloadEffect(fullPath.c_str());
|
|
|
|
|
|
|
|
// let's try again
|
|
|
|
iter = s_effects.find(fullPath);
|
2013-07-22 10:43:23 +08:00
|
|
|
if (iter == s_effects.end())
|
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
fprintf(stderr, "could not find play sound %s\n", fullPath.c_str());
|
|
|
|
return -1;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
}
|
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
checkALError("playEffect:init");
|
|
|
|
|
|
|
|
soundData &d = *iter->second;
|
|
|
|
d.isLooped = bLoop;
|
|
|
|
d.pitch = pitch;
|
|
|
|
d.pan = pan;
|
|
|
|
d.gain = gain;
|
|
|
|
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");
|
|
|
|
|
|
|
|
return d.source;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::stopEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
alSourceStop(nSoundId);
|
|
|
|
checkALError("stopEffect:alSourceStop");
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::preloadEffect(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
// Changing file path to full path
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
EffectsMap::iterator iter = s_effects.find(fullPath);
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
// 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;
|
|
|
|
file.debugName = pszFilePath;
|
|
|
|
file.file = fopen(fullPath.c_str(), "rb");
|
|
|
|
if (!file.file) {
|
|
|
|
fprintf(stderr, "Cannot read file: '%s'\n", fullPath.data());
|
|
|
|
return;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
bool success = false;
|
|
|
|
const std::vector<OpenALDecoder *> &decoders = OpenALDecoder::getDecoders();
|
|
|
|
for (size_t i = 0, n = decoders.size(); !success && i < n; ++i)
|
|
|
|
success = decoders[i]->decode(file, buffer);
|
|
|
|
file.clear();
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alGenSources(1, &source);
|
|
|
|
|
|
|
|
if (checkALError("preloadEffect:alGenSources") != AL_NO_ERROR)
|
|
|
|
{
|
|
|
|
alDeleteBuffers(1, &buffer);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcei(source, AL_BUFFER, buffer);
|
|
|
|
checkALError("preloadEffect:alSourcei");
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
data->isLooped = false;
|
|
|
|
data->buffer = buffer;
|
|
|
|
data->source = source;
|
|
|
|
data->pitch = 1.0;
|
|
|
|
data->pan = 0.0;
|
|
|
|
data->gain = 1.0;
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
s_effects.insert(EffectsMap::value_type(fullPath, data));
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::unloadEffect(const char* pszFilePath)
|
|
|
|
{
|
|
|
|
// Changing file path to full path
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(pszFilePath);
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
EffectsMap::iterator iter = s_effects.find(fullPath);
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (iter != s_effects.end())
|
|
|
|
{
|
|
|
|
checkALError("unloadEffect:init");
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourceStop(iter->second->source);
|
|
|
|
checkALError("unloadEffect:alSourceStop");
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteSources(1, &iter->second->source);
|
|
|
|
checkALError("unloadEffect:DeletSources");
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
alDeleteBuffers(1, &iter->second->buffer);
|
|
|
|
checkALError("unloadEffect:alDeleteBuffers");
|
|
|
|
delete iter->second;
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
s_effects.erase(iter);
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleAudioEngine::pauseEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
|
|
|
if (state == AL_PLAYING)
|
|
|
|
alSourcePause(nSoundId);
|
|
|
|
checkALError("pauseEffect:alSourcePause");
|
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::pauseAllEffects()
|
|
|
|
{
|
|
|
|
EffectsMap::iterator iter = s_effects.begin();
|
|
|
|
ALint state;
|
|
|
|
while (iter != s_effects.end())
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
2013-07-22 10:43:23 +08:00
|
|
|
if (state == AL_PLAYING)
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcePause(iter->second->source);
|
|
|
|
checkALError("pauseAllEffects:alSourcePause");
|
|
|
|
++iter;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::resumeEffect(unsigned int nSoundId)
|
|
|
|
{
|
|
|
|
ALint state;
|
|
|
|
alGetSourcei(nSoundId, AL_SOURCE_STATE, &state);
|
|
|
|
if (state == AL_PAUSED)
|
|
|
|
alSourcePlay(nSoundId);
|
|
|
|
checkALError("resumeEffect:alSourcePlay");
|
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::resumeAllEffects()
|
|
|
|
{
|
|
|
|
EffectsMap::iterator iter = s_effects.begin();
|
|
|
|
ALint state;
|
|
|
|
while (iter != s_effects.end())
|
2013-07-22 10:43:23 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
alGetSourcei(iter->second->source, AL_SOURCE_STATE, &state);
|
2013-07-22 10:43:23 +08:00
|
|
|
if (state == AL_PAUSED)
|
2013-07-22 11:36:20 +08:00
|
|
|
alSourcePlay(iter->second->source);
|
|
|
|
checkALError("resumeAllEffects:alSourcePlay");
|
|
|
|
++iter;
|
2013-07-22 10:43:23 +08:00
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
}
|
2013-07-22 10:43:23 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
void SimpleAudioEngine::stopAllEffects()
|
|
|
|
{
|
|
|
|
EffectsMap::iterator iter = s_effects.begin();
|
2013-06-22 04:01:00 +08:00
|
|
|
|
2013-07-22 11:36:20 +08:00
|
|
|
if (iter != s_effects.end())
|
2013-06-22 04:01:00 +08:00
|
|
|
{
|
2013-07-22 11:36:20 +08:00
|
|
|
checkALError("stopAllEffects:init");
|
|
|
|
alSourceStop(iter->second->source);
|
|
|
|
checkALError("stopAllEffects:alSourceStop");
|
2013-06-22 04:01:00 +08:00
|
|
|
}
|
|
|
|
}
|
2013-07-22 11:36:20 +08:00
|
|
|
|
|
|
|
} // namespace CocosDenshion {
|