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