FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
soundmanager.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2011 by the FIFE team *
3  * http://www.fifengine.net *
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/vfs.h"
33 #include "util/log/logger.h"
34 #include "util/base/exception.h"
35 
36 #include "soundmanager.h"
37 #include "soundemitter.h"
38 
39 namespace FIFE {
40  static Logger _log(LM_AUDIO);
41 
42  SoundManager::SoundManager() : m_context(0),
43  m_device(0),
44  m_mutevol(0),
45  m_volume(1.0) {
46  }
47 
48  SoundManager::~SoundManager() {
49 
50  // free all soundemitters
51 
52  for (std::vector<SoundEmitter*>::iterator it = m_emittervec.begin(), it_end = m_emittervec.end(); it != it_end; ++it) {
53  if ((*it) != NULL) {
54  delete (*it);
55  }
56  }
57 
58  m_emittervec.clear();
59 
60  if (m_device) {
61  alcDestroyContext(m_context);
62  alcCloseDevice(m_device);
63  m_device = NULL;
64  }
65 
66  if (alcGetError(NULL) != ALC_NO_ERROR) {
67  FL_ERR(_log, LMsg() << "error closing openal device");
68  }
69  }
70 
71  void SoundManager::init() {
72  m_device = alcOpenDevice(NULL);
73 
74  if (!m_device || alcGetError(m_device) != ALC_NO_ERROR) {
75  FL_ERR(_log, LMsg() << "Could not open audio device - deactivating audio module");
76  m_device = NULL;
77  return;
78  }
79 
80  m_context = alcCreateContext(m_device, NULL);
81  if (!m_context || alcGetError(m_device) != ALC_NO_ERROR) {
82  FL_ERR(_log, LMsg() << "Couldn't create audio context - deactivating audio module");
83  m_device = NULL;
84  return;
85  }
86 
87  alcMakeContextCurrent(m_context);
88  if (alcGetError(m_device) != ALC_NO_ERROR) {
89  FL_ERR(_log, LMsg() << "Couldn't change current audio context - deactivating audio module");
90  m_device = NULL;
91  return;
92  }
93 
94  // set listener position
95  alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
96  ALfloat vec1[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
97  alListenerfv(AL_ORIENTATION, vec1);
98 
99  // set volume
100  alListenerf(AL_GAIN, m_volume);
101  }
102 
103  SoundEmitter* SoundManager::getEmitter(uint32_t emitterid) const{
104  return m_emittervec.at(emitterid);
105  }
106 
107  SoundEmitter* SoundManager::createEmitter() {
108  SoundEmitter* ptr = new SoundEmitter(this, m_emittervec.size());
109  m_emittervec.push_back(ptr);
110  return ptr;
111  }
112 
113  void SoundManager::releaseEmitter(uint32_t emitterid) {
114  SoundEmitter** ptr = &m_emittervec.at(emitterid);
115  delete *ptr;
116  *ptr = NULL;
117  }
118 } //FIFE