FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
soundemitter.h
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 #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 
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) {
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) {
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 
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?
233  };
234 }
235 
236 #endif
void setGain(float gain)
Sets the gain of the emitter.
Definition: soundemitter.h:128
void reset(bool defaultall=false)
Reset the emitter, free all internal buffers.
bool isStereo() const
Tests if the audio data is stereo data or mono.
Definition: sounddecoder.h:92
void setCallback(const type_callback &cb)
Sets the callback to use when the STREAM has finished being played.
void pause()
Pauses playing the audio file.
Definition: soundemitter.h:118
uint32_t m_soundclipid
Definition: soundemitter.h:228
bool isStereo()
Tests if the audio data is stereo data or mono.
Definition: soundemitter.h:146
boost::function0< void > type_callback
Definition: soundemitter.h:48
void release()
Releases the emitter.
void setCursor(SoundPositionType type, float value)
Sets the cursor position in the audio file.
uint64_t getSampleRate()
Returns the sample rate.
Definition: soundemitter.h:164
Interface for events to be registered with TimeManager.
Definition: timeevent.h:48
void setSoundClip(SoundClipPtr soundclip)
Sets the sound clip to be used by this emitter.
SoundPositionType
Different types of audio-file positions.
Definition: soundclip.h:44
void attachSoundClip()
Internal function to attach a soundclip to the source.
void setPosition(float x, float y, float z)
Sets the position of the SoundEmitter in the virtual audio space.
Definition: soundemitter.h:206
void play()
Plays the associated audio file.
virtual uint64_t getDecodedLength() const =0
Returns the decoded length of the file in bytes.
int16_t getBitResolution() const
Returns the bit resolution.
Definition: sounddecoder.h:108
uint32_t getId() const
Returns the emitter-id.
Definition: soundemitter.h:55
type_callback m_callback
Definition: soundemitter.h:232
float getCursor(SoundPositionType type)
Returns the cursor position in the audio file.
SoundClipPtr m_soundclip
Definition: soundemitter.h:227
void setLooping(bool loop)
Sets the playing mode.
int16_t getBitResolution()
Returns the bit resolution.
Definition: soundemitter.h:155
SoundDecoder * getDecoder() const
Returns the attached decoder.
Definition: soundclip.h:132
uint64_t getSampleRate() const
Returns the sample rate.
Definition: sounddecoder.h:114
float getGain()
Returns the gain of the emitter.
Definition: soundemitter.h:136
virtual void updateEvent(uint32_t time)
Implementation of the pure virtual function from TimeEvent to update streaming.
void setVelocity(float x, float y, float z)
Sets the velocity of the SoundEmitter in the virtual audio space.
Definition: soundemitter.h:212
uint64_t getDuration()
Returns the duration of the sound clip in milliseconds.
Definition: soundemitter.h:183
The class for playing audio files.
Definition: soundemitter.h:46
void setPositioning(bool relative)
Sets Positioning-Type Default is false.
Definition: soundemitter.h:65
uint64_t getDecodedLength()
Returns the length of the decoded length in bytes.
Definition: soundemitter.h:173
SoundClipPtr getSoundClip()
Get the current sound clip used by this emitter.
Definition: soundemitter.h:85
SoundEmitter(SoundManager *manager, uint32_t uid)
SoundManager * m_manager
Definition: soundemitter.h:225
void stop()
Stops playing the audio file and rewinds to the beginning.
unsigned int uint32_t
Definition: core.h:40
void setRolloff(float rolloff)
Sets the AL_ROLEOFF_FACTOR.
Definition: soundemitter.h:73