FIFE  2008.0
soundemitter.h
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 #ifndef FIFE_SOUNDEMITTER_H_
23 #define FIFE_SOUNDEMITTER_H_
24 
25 // Standard C++ library includes
26 
27 // Platform specific includes
28 
29 // 3rd party library includes
30 #include <boost/function.hpp>
31 
32 // FIFE includes
33 // These includes are split up in two parts, separated by one empty line
34 // First block: files included from the FIFE root src directory
35 // Second block: files included from the same folder
36 #include "util/time/timeevent.h"
37 
38 #include "soundclip.h"
39 
40 namespace FIFE {
41 
42  class SoundManager;
43 
46  class SoundEmitter : private TimeEvent {
47  public:
48  typedef boost::function0<void> type_callback;
49 
50  SoundEmitter(SoundManager* manager, uint32_t uid);
51  ~SoundEmitter();
52 
55  uint32_t getId() const{
56  return m_emitterid;
57  }
58 
65  void setPositioning(bool relative) {
66  alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
67  }
68 
73  void setRolloff(float rolloff) {
74  alSourcef (m_source, AL_ROLLOFF_FACTOR, rolloff);
75  }
76 
80  void setSoundClip(SoundClipPtr soundclip);
81 
85  SoundClipPtr getSoundClip() { return m_soundclip; };
86 
92  void setCallback(const type_callback& cb);
93 
98  void reset(bool defaultall = false);
99 
102  void release();
103 
106  void setLooping(bool loop);
107 
110  void play();
111 
114  void stop();
115 
118  void pause() {
119  if (m_soundclip) {
120  alSourcePause(m_source);
121  }
122  }
123 
128  void setGain(float gain) {
129  alSourcef(m_source, AL_GAIN, gain);
130  }
131 
136  float getGain() {
137  float tmp;
138  alGetSourcef(m_source, AL_GAIN, &tmp);
139  return tmp;
140  }
141 
146  bool isStereo() {
147  if (m_soundclip) {
148  return m_soundclip->getDecoder()->isStereo();
149  }
150  return false;
151  }
152 
155  int16_t getBitResolution() {
156  if (m_soundclip) {
157  return m_soundclip->getDecoder()->getBitResolution();
158  }
159  return 0;
160  }
161 
164  uint64_t getSampleRate() {
165  if (m_soundclip) {
166  return m_soundclip->getDecoder()->getSampleRate();
167  }
168  return 0;
169  }
170 
173  uint64_t getDecodedLength() {
174  if (m_soundclip) {
175  return m_soundclip->getDecoder()->getDecodedLength();
176 
177  }
178  return 0;
179  }
180 
183  uint64_t getDuration() {
184  if (m_soundclip) {
185  double samplerate = static_cast<double>(getSampleRate()) / 1000.0; //convert to milliseconds
186  double bitres = static_cast<double>(getBitResolution());
187  double size = static_cast<double>(getDecodedLength()) * 8.0; //convert to bits
188  double stereo = (isStereo() ? 2.0 : 1.0);
189  double time = ( size / (samplerate * bitres) ) / stereo;
190 
191  return static_cast<uint64_t>(time);
192  }
193  return 0;
194  }
195 
198  void setCursor(SoundPositionType type, float value);
199 
202  float getCursor(SoundPositionType type);
203 
206  void setPosition(float x, float y, float z) {
207  alSource3f(m_source, AL_POSITION, x, y, z);
208  }
209 
212  void setVelocity(float x, float y, float z) {
213  alSource3f(m_source, AL_VELOCITY, x, y, z);
214  }
215 
216  private:
219  virtual void updateEvent(uint32_t time);
220 
223  void attachSoundClip();
224 
225  SoundManager* m_manager;
226  ALuint m_source; // The openAL-source
227  SoundClipPtr m_soundclip; // the attached soundclip
228  uint32_t m_soundclipid;// id of the attached soundclip
229  uint32_t m_streamid; // the id of the stream
230  uint32_t m_emitterid; // the emitter-id
231  bool m_loop; // loop?
232  type_callback m_callback;
233  };
234 }
235 
236 #endif
void setGain(float gain)
Definition: soundemitter.h:128
void reset(bool defaultall=false)
void setCallback(const type_callback &cb)
void setCursor(SoundPositionType type, float value)
uint64_t getSampleRate()
Definition: soundemitter.h:164
void setSoundClip(SoundClipPtr soundclip)
SoundPositionType
Definition: soundclip.h:44
void setPosition(float x, float y, float z)
Definition: soundemitter.h:206
uint32_t getId() const
Definition: soundemitter.h:55
float getCursor(SoundPositionType type)
void setLooping(bool loop)
int16_t getBitResolution()
Definition: soundemitter.h:155
SoundDecoder * getDecoder() const
Definition: soundclip.h:131
void setVelocity(float x, float y, float z)
Definition: soundemitter.h:212
uint64_t getDuration()
Definition: soundemitter.h:183
void setPositioning(bool relative)
Definition: soundemitter.h:65
uint64_t getDecodedLength()
Definition: soundemitter.h:173
SoundClipPtr getSoundClip()
Definition: soundemitter.h:85
void setRolloff(float rolloff)
Definition: soundemitter.h:73
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39