simple_source.cpp
Go to the documentation of this file.00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "stdsound.h"
00025
00026 #include "simple_source.h"
00027 #include "driver/buffer.h"
00028 #include "driver/source.h"
00029 #include "mixing_track.h"
00030 #include "simple_sound.h"
00031 #include "clustered_sound.h"
00032
00033 using namespace NLMISC;
00034
00035
00036 namespace NLSOUND
00037 {
00038
00039
00040
00041
00042
00043 CSimpleSource::CSimpleSource( CSimpleSound *simpleSound, bool spawn, TSpawnEndCallback cb, void *cbUserParam, NL3D::CCluster *cluster)
00044 : CSourceCommon(simpleSound, spawn, cb, cbUserParam, cluster),
00045 _Track(NULL), _PlayMuted(false)
00046 {
00047 nlassert(simpleSound != 0);
00048 _Sound = simpleSound;
00049
00050
00051 _Alpha = _Sound->getAlpha();
00052 }
00053
00054
00055
00056
00057
00058 CSimpleSource::~CSimpleSource()
00059 {
00060 if (_Playing)
00061 stop();
00062
00063
00064 CAudioMixerUser::instance()->removeEvents(this);
00065 }
00066
00067 CSimpleSound *CSimpleSource::getSimpleSound()
00068 {
00069 return _Sound;
00070 }
00071
00072 uint32 CSimpleSource::getTime()
00073 {
00074 if (_Track && _Track->DrvSource)
00075 {
00076 return _Track->DrvSource->getTime();
00077 }
00078 else
00079 return 0;
00080 }
00081
00082
00083 IBuffer *CSimpleSource::getBuffer()
00084 {
00085 return _Sound->getBuffer();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 void CSimpleSource::setLooping( bool l )
00095 {
00096 CSourceCommon::setLooping(l);
00097 if ( _Track != NULL )
00098 {
00099 _Track->DrvSource->setLooping( l );
00100 }
00101 }
00102
00103
00104 CVector CSimpleSource::getVirtualPos() const
00105 {
00106 if (getCluster() != 0)
00107 {
00108
00109 const CClusteredSound::CClusterSoundStatus *css = CAudioMixerUser::instance()->getClusteredSound()->getClusterSoundStatus(getCluster());
00110 if (css != 0)
00111 {
00112
00113 float dist = (css->Position - getPos()).norm();
00114 CVector vpos(CAudioMixerUser::instance()->getListenPosVector() + css->Direction * (css->Dist + dist));
00115 vpos = _Position * (1-css->PosAlpha) + vpos*(css->PosAlpha);
00116 return vpos;
00117 }
00118 }
00119
00120 return _Position;
00121 }
00122
00123
00124
00125
00126
00127 void CSimpleSource::play()
00128 {
00129
00130
00131 CAudioMixerUser *mixer = CAudioMixerUser::instance();
00132
00133
00134
00135
00136 if (_Sound->getBuffer() == 0
00137 || !_Sound->getBuffer()->isBufferLoaded()
00138 || (mixer->getListenPosVector() - _Position).sqrnorm() > _Sound->getMaxDistance()*_Sound->getMaxDistance())
00139 {
00140
00141 if (_Spawn)
00142 {
00143 if (_SpawnEndCb != 0)
00144 _SpawnEndCb(this, _CbUserParam);
00145
00146 delete this;
00147 }
00148
00149 return;
00150 }
00151
00152
00153
00154
00155 if (_Track == 0)
00156 _Track = mixer->getFreeTrack(this);
00157 if (_Track != 0)
00158 {
00159
00160 _Track->DrvSource->setStaticBuffer(_Sound->getBuffer());
00161
00162
00163 _Track->DrvSource->setPos( getVirtualPos(), false);
00164 if ( ! _Sound->getBuffer()->isStereo() )
00165 {
00166 _Track->DrvSource->setMinMaxDistances( _Sound->getMinDistance(), _Sound->getMaxDistance(), false );
00167 setDirection( _Direction );
00168 _Track->DrvSource->setVelocity( _Velocity );
00169 }
00170 _Track->DrvSource->setGain( _Gain );
00171 _Track->DrvSource->setSourceRelativeMode( _RelativeMode );
00172 _Track->DrvSource->setLooping( _Looping );
00173 _Track->DrvSource->setPitch( _Pitch );
00174 _Track->DrvSource->setAlpha( _Alpha );
00175
00176
00177 bool play = _Track->DrvSource->play();
00178
00179 nlassert(play);
00180
00181 }
00182 else
00183 {
00184 if (_Priority == HighestPri)
00185 {
00186
00187 mixer->addSourceWaitingForPlay(this);
00188 return;
00189 }
00190
00191 mixer->addEvent(this, CTime::getLocalTime()+_Sound->getDuration());
00192 _PlayMuted = true;
00193 mixer->incPlayingSourceMuted();
00194
00195 }
00196
00197 CSourceCommon::play();
00198
00199 }
00200
00201 void CSimpleSource::onEvent()
00202 {
00203
00204
00205 if (!_Playing)
00206 return;
00207
00208
00209 _PlayMuted = false;
00210 CAudioMixerUser::instance()->decPlayingSourceMuted();
00211
00212 stop();
00213 }
00214
00215
00216
00217
00218 void CSimpleSource::stop()
00219 {
00220
00221
00222 if (!_Playing)
00223 return;
00224
00225 if (_Track != 0)
00226 {
00227
00228 _Track->DrvSource->stop();
00229 _Track->DrvSource->setStaticBuffer(0);
00230 CAudioMixerUser::instance()->freeTrack(_Track);
00231 _Track = 0;
00232 }
00233 else if (_PlayMuted)
00234 {
00235
00236 CAudioMixerUser::instance()->decPlayingSourceMuted();
00237 CAudioMixerUser::instance()->removeEvents(this);
00238 }
00239
00240
00241 CSourceCommon::stop();
00242
00243 if (_Spawn)
00244 {
00245 if ( _SpawnEndCb != 0 )
00246 {
00247 _SpawnEndCb( this, _CbUserParam);
00248 }
00249
00250 delete this;
00251 }
00252 }
00253
00254
00255
00256
00257
00258
00259 void CSimpleSource::setPos( const NLMISC::CVector& pos )
00260 {
00261 CSourceCommon::setPos(pos);
00262
00263
00264 if ( _Track != NULL )
00265 {
00266
00267 _Track->DrvSource->setPos( getVirtualPos() );
00268 }
00269 }
00270
00271
00272
00273
00274
00275 void CSimpleSource::setVelocity( const NLMISC::CVector& vel )
00276 {
00277 CSourceCommon::setVelocity(vel);
00278
00279
00280 if ( _Track != NULL )
00281 {
00282
00283 _Track->DrvSource->setVelocity( vel );
00284 }
00285 }
00286
00287
00288
00289
00290
00291 void CSimpleSource::setDirection( const NLMISC::CVector& dir )
00292 {
00293 CSourceCommon::setDirection(dir);
00294
00295
00296 if ( _Track != NULL )
00297 {
00298 if ( ! _Sound->getBuffer()->isStereo() )
00299 {
00300 static bool coneset = false;
00301 if ( dir.isNull() )
00302 {
00303 _Track->DrvSource->setCone( float(Pi*2), float(Pi*2), 1.0f );
00304 _Track->DrvSource->setDirection( CVector::I );
00305 coneset = false;
00306 }
00307 else
00308 {
00309
00310 {
00311 _Track->DrvSource->setCone( _Sound->getConeInnerAngle(), _Sound->getConeOuterAngle(), _Sound->getConeOuterGain() );
00312 coneset = true;
00313 }
00314 _Track->DrvSource->setDirection( dir );
00315 }
00316 }
00317 }
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327 void CSimpleSource::setGain( float gain )
00328 {
00329 CSourceCommon::setGain(gain);
00330
00331
00332 if ( _Track != NULL )
00333 {
00334 _Track->DrvSource->setGain( gain );
00335 }
00336 }
00337
00338 void CSimpleSource::setRelativeGain( float gain )
00339 {
00340 CSourceCommon::setRelativeGain(gain);
00341
00342
00343 if ( _Track != NULL )
00344 {
00345 _Track->DrvSource->setGain( _Gain );
00346 }
00347 }
00348
00349
00350
00351
00352
00353 void CSimpleSource::setPitch( float pitch )
00354 {
00355 CSourceCommon::setPitch(pitch);
00356
00357
00358 if ( _Track != NULL )
00359 {
00360 _Track->DrvSource->setPitch( pitch );
00361 }
00362 }
00363
00364
00365
00366
00367
00368 void CSimpleSource::setSourceRelativeMode( bool mode )
00369 {
00370 CSourceCommon::setSourceRelativeMode(mode);
00371
00372
00373 if ( _Track != NULL )
00374 {
00375 _Track->DrvSource->setSourceRelativeMode( mode );
00376 }
00377 }
00378
00379
00380
00381
00382 bool CSimpleSource::isPlaying()
00383 {
00384 return _Playing;
00385 }
00386
00387
00388 }