Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Source.cpp
Go to the documentation of this file.
1 //
2 // C++ Implementation: Audio::Source
3 //
4 
5 #include "Source.h"
6 #include "SourceListener.h"
7 #include "config.h"
8 #include "utils.h"
9 
10 #include <math.h>
11 
12 namespace Audio {
13 
14  Source::Source(SharedPtr<Sound> sound, bool _looping) throw() :
15  soundPtr(sound),
16 
17  // Some safe defaults
18  position(0,0,0),
19  direction(0,0,1),
20  velocity(0,0,0),
21 
22  cosAngleRange(-1,-1),
23 
24  radius(1),
25 
26  pfRadiusRatios(1,1),
27  referenceFreqs(250,5000),
28 
29  gain(1),
30 
31  lastKnownPlayingTime(0),
32  lastKnownPlayingTimeTime( getRealTime() )
33  {
34  // Some default flags
35  setLooping(_looping);
36  setRelative(false);
37  setAttenuated(true);
38  }
39 
41  {
42  }
43 
45  {
46  lastKnownPlayingTime = timestamp;
47  lastKnownPlayingTimeTime = getRealTime();
48  return timestamp;
49  }
50 
52  {
53  dirty.setAll();
54  startPlayingImpl( setLastKnownPlayingTime(start) );
55  }
56 
57  void Source::stopPlaying() throw()
58  {
59  // Pause first to stop the renderable
60  pausePlaying();
62  }
63 
64  void Source::pausePlaying() throw()
65  {
66  if (rendererDataPtr.get() && isActive()) {
67  try {
69  } catch(Exception e) { }
70 
71  // Must notify the listener, if any
72  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantPlayEvents())
73  sourceListenerPtr->onPrePlay(*this, false);
74 
75  rendererDataPtr->stopPlaying();
76 
77  // Must notify the listener, if any
78  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantPlayEvents())
79  sourceListenerPtr->onPostPlay(*this, false);
80  }
81  }
82 
84  {
85  if (rendererDataPtr.get() && isPlaying() && !isActive()) {
86  // Must notify the listener, if any
87  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantPlayEvents())
88  sourceListenerPtr->onPrePlay(*this, true);
89 
90  rendererDataPtr->startPlaying( getWouldbePlayingTime() );
91 
92  // Must notify the listener, if any
93  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantPlayEvents())
94  sourceListenerPtr->onPrePlay(*this, true);
95 
96  }
97  }
98 
100  {
101  try {
102  if (rendererDataPtr.get() && isActive())
103  return rendererDataPtr->getPlayingTime();
104  else
105  return lastKnownPlayingTime;
106  } catch(Exception e) {
107  return lastKnownPlayingTime;
108  }
109  }
110 
112  {
113  try {
114  if (rendererDataPtr.get() && isActive())
115  return rendererDataPtr->getPlayingTime();
116  } catch(Exception e) { }
118  }
119 
120  bool Source::isPlaying() const throw()
121  {
122  try {
123  return isPlayingImpl();
124  } catch(Exception e) {
125  return false;
126  }
127  }
128 
129  bool Source::isActive() const throw()
130  {
131  try {
132  return rendererDataPtr.get() && rendererDataPtr->isPlaying();
133  } catch(Exception e) {
134  return false;
135  }
136  }
137 
139  {
142  }
143 
145  {
146  cosAngleRange.min = Scalar(cos(r.min));
147  cosAngleRange.max = Scalar(cos(r.max));
148  dirty.attributes = 1;
149  }
150 
151  void Source::updateRenderable(int flags, const Listener& sceneListener)
152  throw()
153  {
154  if (rendererDataPtr.get()) {
155  int oflags = flags;
156 
157  if (!dirty.attributes)
158  flags &= ~RenderableSource::UPDATE_ATTRIBUTES;
159  if (!dirty.gain)
160  flags &= ~RenderableSource::UPDATE_GAIN;
161 
162  // Must always update location... listeners might move around.
164 
165  // Must nofity listener, if any
166  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantUpdateEvents())
167  sourceListenerPtr->onUpdate(*this, flags);
168 
169  rendererDataPtr->update(flags, sceneListener);
170 
171  if (oflags == RenderableSource::UPDATE_ALL) {
172  dirty.reset();
173  } else {
175  dirty.location = 0;
177  dirty.attributes = 0;
178  if (flags & RenderableSource::UPDATE_GAIN)
179  dirty.gain = 0;
180  }
181 
182  switch(flags) {
184  dirty.reset();
185  break;
188  dirty.attributes = 0;
190  dirty.location = 0;
191  };
192  }
193  }
194 
195  void Source::setRenderable(SharedPtr<RenderableSource> ptr)
196  throw()
197  {
198  // Notify at/detachment to listener, if any
199  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantAttachEvents())
200  sourceListenerPtr->onPreAttach(*this, ptr.get() != 0);
201 
202  rendererDataPtr = ptr;
203 
204  // Notify at/detachment to listener, if any
205  if (sourceListenerPtr.get() != 0 && sourceListenerPtr->wantAttachEvents())
206  sourceListenerPtr->onPostAttach(*this, ptr.get() != 0);
207  }
208 
210  throw(Exception)
211  {
212  if (rendererDataPtr.get() && isPlaying() && isActive()) {
213  rendererDataPtr->seek(time);
214  } else {
215  setLastKnownPlayingTime(time);
216  }
217  }
218 
219 };