00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "vfs/vfs.h"
00033 #include "util/log/logger.h"
00034 #include "util/base/exception.h"
00035
00036 #include "soundmanager.h"
00037 #include "soundemitter.h"
00038
00039 namespace FIFE {
00040 static Logger _log(LM_AUDIO);
00041
00042 SoundManager::SoundManager() : m_context(0),
00043 m_device(0),
00044 m_mutevol(0),
00045 m_volume(1.0) {
00046 }
00047
00048 SoundManager::~SoundManager() {
00049
00050
00051
00052 for (std::vector<SoundEmitter*>::iterator it = m_emittervec.begin(), it_end = m_emittervec.end(); it != it_end; ++it) {
00053 if ((*it) != NULL) {
00054 delete (*it);
00055 }
00056 }
00057
00058 m_emittervec.clear();
00059
00060 if (m_device) {
00061 alcDestroyContext(m_context);
00062 alcCloseDevice(m_device);
00063 m_device = NULL;
00064 }
00065
00066 if (alcGetError(NULL) != ALC_NO_ERROR) {
00067 FL_ERR(_log, LMsg() << "error closing openal device");
00068 }
00069 }
00070
00071 void SoundManager::init() {
00072 m_device = alcOpenDevice(NULL);
00073
00074 if (!m_device || alcGetError(m_device) != ALC_NO_ERROR) {
00075 FL_ERR(_log, LMsg() << "Could not open audio device - deactivating audio module");
00076 m_device = NULL;
00077 return;
00078 }
00079
00080 m_context = alcCreateContext(m_device, NULL);
00081 if (!m_context || alcGetError(m_device) != ALC_NO_ERROR) {
00082 FL_ERR(_log, LMsg() << "Couldn't create audio context - deactivating audio module");
00083 m_device = NULL;
00084 return;
00085 }
00086
00087 alcMakeContextCurrent(m_context);
00088 if (alcGetError(m_device) != ALC_NO_ERROR) {
00089 FL_ERR(_log, LMsg() << "Couldn't change current audio context - deactivating audio module");
00090 m_device = NULL;
00091 return;
00092 }
00093
00094
00095 alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
00096 ALfloat vec1[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
00097 alListenerfv(AL_ORIENTATION, vec1);
00098
00099
00100 alListenerf(AL_GAIN, m_volume);
00101 }
00102
00103 SoundEmitter* SoundManager::getEmitter(uint32_t emitterid) const{
00104 return m_emittervec.at(emitterid);
00105 }
00106
00107 SoundEmitter* SoundManager::createEmitter() {
00108 SoundEmitter* ptr = new SoundEmitter(this, m_emittervec.size());
00109 m_emittervec.push_back(ptr);
00110 return ptr;
00111 }
00112
00113 void SoundManager::releaseEmitter(uint32_t emitterid) {
00114 SoundEmitter** ptr = &m_emittervec.at(emitterid);
00115 delete *ptr;
00116 *ptr = NULL;
00117 }
00118 }