FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
soundemitter.h
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 #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 "vfs/raw/rawdata.h"
37 #include "util/time/timeevent.h"
38 
39 #include "soundclip.h"
40 
41 namespace FIFE {
42 
43  class SoundManager;
44  class SoundClipPool;
45 
48  class SoundEmitter : private TimeEvent {
49  public:
50  typedef boost::function0<void> type_callback;
51 
52  SoundEmitter(SoundManager* manager, SoundClipPool* pool, unsigned int uid);
53  ~SoundEmitter();
54 
57  unsigned int getId() const{
58  return m_emitterid;
59  }
60 
67  void setPositioning(bool relative) {
68  alSourcei(m_source, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
69  }
70 
75  void setRolloff(float rolloff) {
76  alSourcef (m_source, AL_ROLLOFF_FACTOR, rolloff);
77  }
78 
82  void setSoundClip(unsigned int sound_id);
83 
89  void setCallback(const type_callback& cb);
90 
95  void reset(bool defaultall = false);
96 
99  void release();
100 
103  void setLooping(bool loop);
104 
107  void play();
108 
111  void stop();
112 
115  void pause() {
116  if (m_soundclip) {
117  alSourcePause(m_source);
118  }
119  }
120 
125  void setGain(float gain) {
126  alSourcef(m_source, AL_GAIN, gain);
127  }
128 
133  float getGain() {
134  float tmp;
135  alGetSourcef(m_source, AL_GAIN, &tmp);
136  return tmp;
137  }
138 
143  bool isStereo() const{
144  if (m_soundclip) {
145  return m_soundclip->getDecoder()->isStereo();
146  }
147  return false;
148  }
149 
152  short getBitResolution() const {
153  if (m_soundclip) {
154  return m_soundclip->getDecoder()->getBitResolution();
155  }
156  return 0;
157  }
158 
161  unsigned long getSampleRate() const{
162  if (m_soundclip) {
163  return m_soundclip->getDecoder()->getSampleRate();
164  }
165  return 0;
166  }
167 
170  unsigned long getDecodedLength() const{
171  if (m_soundclip) {
172  return m_soundclip->getDecoder()->getDecodedLength();
173 
174  }
175  return 0;
176  }
177 
180  unsigned long getDuration() const{
181  if (m_soundclip) {
182  double samplerate = static_cast<double>(getSampleRate()) / 1000.0; //convert to milliseconds
183  double bitres = static_cast<double>(getBitResolution());
184  double size = static_cast<double>(getDecodedLength()) * 8.0; //convert to bits
185  double stereo = (isStereo() ? 2.0 : 1.0);
186  double time = ( size / (samplerate * bitres) ) / stereo;
187 
188  return static_cast<unsigned long>(time);
189  }
190  return 0;
191  }
192 
195  void setCursor(SoundPositionType type, float value);
196 
199  float getCursor(SoundPositionType type);
200 
203  void setPosition(float x, float y, float z) {
204  alSource3f(m_source, AL_POSITION, x, y, z);
205  }
206 
209  void setVelocity(float x, float y, float z) {
210  alSource3f(m_source, AL_VELOCITY, x, y, z);
211  }
212 
213  private:
216  virtual void updateEvent(unsigned long time);
217 
220  void attachSoundClip();
221 
222  SoundManager* m_manager;
223  SoundClipPool* m_pool;
224  ALuint m_source; // The openAL-source
225  SoundClip* m_soundclip; // the attached soundclip
226  unsigned int m_soundclipid;// id of the attached soundclip
227  unsigned int m_streamid; // the id of the stream
228  unsigned int m_emitterid; // the emitter-id
229  bool m_loop; // loop?
230  type_callback m_callback;
231  };
232 }
233 
234 #endif