FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
soundmanager.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // Platform specific includes
00025 
00026 // 3rd party library includes
00027 
00028 // FIFE includes
00029 // These includes are split up in two parts, separated by one empty line
00030 // First block: files included from the FIFE root src directory
00031 // Second block: files included from the same folder
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         // free all soundemitters
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         // set listener position
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         // set volume
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 } //FIFE