FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
soundmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 {
43  static Logger _log(LM_AUDIO);
44 
45  SoundManager::SoundManager() : m_context(0),
46  m_device(0),
47  m_mutevol(0),
48  m_volume(1.0) {
49  }
50 
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 
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 
107  return m_emittervec.at(emitterid);
108  }
109 
111  SoundEmitter* ptr = new SoundEmitter(this, m_emittervec.size());
112  m_emittervec.push_back(ptr);
113  return ptr;
114  }
115 
117  SoundEmitter** ptr = &m_emittervec.at(emitterid);
118  delete *ptr;
119  *ptr = NULL;
120  }
121 } //FIFE
Helper class to create log strings out from separate parts Usage: LMsg(&quot;some text&quot;) &lt;&lt; variable &lt;&lt; &quot;...
Definition: logger.h:82
SoundEmitter * createEmitter()
Returns a pointer to an allocated emitter-instance.
static Logger _log(LM_AUDIO)
ALCcontext * m_context
Definition: soundmanager.h:128
#define FL_ERR(logger, msg)
Definition: logger.h:73
void init()
Initializes the audio system.
ALCdevice * m_device
Definition: soundmanager.h:129
The class for playing audio files.
Definition: soundemitter.h:46
std::vector< SoundEmitter * > m_emittervec
Definition: soundmanager.h:127
unsigned int uint32_t
Definition: core.h:40
void releaseEmitter(uint32_t emitterid)
Release an emitter-instance given by emitter-id.
SoundEmitter * getEmitter(uint32_t emitterid) const
Returns a pointer to an emitter-instance given by emitterid.