soundmanager.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
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/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         // free all soundemitters
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         // set listener position
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         // set volume
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 } //FIFE