FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
soundmanager.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // Platform specific includes
25 
26 // 3rd party library includes
27 
28 // FIFE includes
29 // These includes are split up in two parts, separated by one empty line
30 // First block: files included from the FIFE root src directory
31 // Second block: files included from the same folder
32 #include "vfs/raw/rawdata.h"
33 #include "vfs/vfs.h"
34 #include "util/log/logger.h"
35 #include "util/base/exception.h"
36 
37 #include "soundmanager.h"
38 #include "soundemitter.h"
39 #include "soundclippool.h"
40 
41 namespace FIFE {
42  static Logger _log(LM_AUDIO);
43 
44  SoundManager::SoundManager(SoundClipPool* pool) : m_context(0),
45  m_device(0),
46  m_pool(pool),
47  m_mutevol(0),
48  m_volume(1.0) {
49  }
50 
51  SoundManager::~SoundManager() {
52 
53  // free all soundemitters
54 
55  for (std::vector<SoundEmitter*>::iterator it = m_emittervec.begin(), it_end = m_emittervec.end(); it != it_end; ++it) {
56  if ((*it) != NULL) {
57  delete (*it);
58  }
59  }
60 
61  m_emittervec.clear();
62 
63  if (m_device) {
64  alcDestroyContext(m_context);
65  alcCloseDevice(m_device);
66  m_device = NULL;
67  }
68 
69  if (alcGetError(NULL) != ALC_NO_ERROR) {
70  FL_ERR(_log, LMsg() << "error closing openal device");
71  }
72  }
73 
74  void SoundManager::init() {
75  m_device = alcOpenDevice(NULL);
76 
77  if (!m_device || alcGetError(m_device) != ALC_NO_ERROR) {
78  FL_ERR(_log, LMsg() << "Could not open audio device - deactivating audio module");
79  m_device = NULL;
80  return;
81  }
82 
83  m_context = alcCreateContext(m_device, NULL);
84  if (!m_context || alcGetError(m_device) != ALC_NO_ERROR) {
85  FL_ERR(_log, LMsg() << "Couldn't create audio context - deactivating audio module");
86  m_device = NULL;
87  return;
88  }
89 
90  alcMakeContextCurrent(m_context);
91  if (alcGetError(m_device) != ALC_NO_ERROR) {
92  FL_ERR(_log, LMsg() << "Couldn't change current audio context - deactivating audio module");
93  m_device = NULL;
94  return;
95  }
96 
97  // set listener position
98  alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
99  ALfloat vec1[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
100  alListenerfv(AL_ORIENTATION, vec1);
101 
102  // set volume
103  alListenerf(AL_GAIN, m_volume);
104  }
105 
106  SoundEmitter* SoundManager::getEmitter(unsigned int emitterid) const{
107  return m_emittervec.at(emitterid);
108  }
109 
110  SoundEmitter* SoundManager::createEmitter() {
111  SoundEmitter* ptr = new SoundEmitter(this, m_pool, m_emittervec.size());
112  m_emittervec.push_back(ptr);
113  return ptr;
114  }
115 
116  void SoundManager::releaseEmitter(unsigned int emitterid) {
117  SoundEmitter** ptr = &m_emittervec.at(emitterid);
118  delete *ptr;
119  *ptr = NULL;
120  }
121 } //FIFE