sfxSource.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/sfxSource.h"
  23. #include "sfx/sfxSystem.h"
  24. #include "sfx/sfxTrack.h"
  25. #include "sfx/sfxTypes.h"
  26. #include "sfx/sfxDescription.h"
  27. #include "sfx/sfxModifier.h"
  28. #include "console/engineAPI.h"
  29. #include "math/mRandom.h"
  30. #include "math/mEase.h"
  31. //#define DEBUG_SPEW
  32. IMPLEMENT_CONOBJECT( SFXSource );
  33. ConsoleDocClass( SFXSource,
  34. "@brief Playback controller for a sound source.\n\n"
  35. "All sound playback is driven by SFXSources. Each such source represents an independent playback controller "
  36. "that directly or indirectly affects sound output.\n\n"
  37. "While this class itself is instantiable, such an instance will not by itself emit any sound. This is the "
  38. "responsibility of its subclasses. Note, however, that none of these subclasses must be instantiated directly "
  39. "but must instead be instantiated indirectly through the SFX interface.\n\n"
  40. "@section SFXSource_playonce Play-Once Sources\n\n"
  41. "Often, a sound source need only exist for the duration of the sound it is playing. In this case "
  42. "so-called \"play-once\" sources simplify the bookkeeping involved by leaving the deletion of "
  43. "sources that have expired their playtime to the sound system.\n\n"
  44. "Play-once sources can be created in either of two ways:\n"
  45. "- sfxPlayOnce(): Directly create a play-once source from a SFXTrack or file.\n"
  46. "- sfxDeleteWhenStopped(): Retroactively turn any source into a play-once source that will automatically "
  47. "be deleted when moving into stopped state.\n\n"
  48. "@see sfxPlayOnce\n"
  49. "@see sfxDeleteWhenStopped\n\n"
  50. "@section SFXSource_hierarchies Source Hierarchies\n\n"
  51. "Source are arranged into playback hierarchies where a parent source will scale some of the properties of its "
  52. "children and also hand on any play(), pause(), and stop() commands to them. This allows to easily group sounds "
  53. "into logical units that can then be operated on as a whole.\n\n"
  54. "An example of this is the segregation of sounds according to their use in the game. Volume levels of background "
  55. "music, in-game sound effects, and character voices will usually be controlled independently and putting their sounds "
  56. "into different hierarchies allows to achieve that easily.\n\n"
  57. "The source properties that are scaled by parent values are:\n"
  58. "- volume,\n"
  59. "- pitch, and\n"
  60. "- priority\n\n"
  61. "This means that if a parent has a volume of 0.5, the child will play at half the effective volume it would otherwise "
  62. "have.\n\n"
  63. "Additionally, parents affect the playback state of their children:\n\n"
  64. "- A parent that is in stopped state will force all its direct and indirect children into stopped state.\n"
  65. "- A parent that is in paused state will force all its direct and indirect children that are playing into paused state. However, "
  66. "children that are in stopped state will not be affected.\n"
  67. "- A parent that is in playing state will not affect the playback state of its children.\n\n"
  68. "Each source maintains a state that is wants to be in which may differ from the state that is enforced on it by its "
  69. "parent. If a parent changes its states in a way that allows a child to move into its desired state, the child will do "
  70. "so.\n\n"
  71. "For logically grouping sources, instantiate the SFXSource class directly and make other sources children to it. A "
  72. "source thus instantiated will not effect any real sound output on its own but will influence the sound output of its "
  73. "direct and indirect children.\n\n"
  74. "@note Be aware that the property values used to scale child property values are the @b effective values. For example, "
  75. "the value used to scale the volume of a child is the @b effective volume of the parent, i.e. the volume after fades, "
  76. "distance attenuation, etc. has been applied.\n\n"
  77. "@see SFXDescription::sourceGroup\n"
  78. "@section SFXSource_volume Volume Attenuation\n\n"
  79. "During its lifetime, the volume of a source will be continually updated. This update process always progresses "
  80. "in a fixed set of steps to compute the final effective volume of the source based on the base volume level "
  81. "that was either assigned from the SFXDescription associated with the source (SFXDescription::volume) or manually "
  82. "set by the user. The process of finding a source's final effective volume is called \"volume attenuation\". The "
  83. "steps involved in attenuating a source's volume are (in order):\n"
  84. "<dl>\n"
  85. "<dt>Fading</dt>\n"
  86. "<dd>If the source currently has a fade-effect applied, the volume is interpolated along the currently active fade curve.</dd>\n"
  87. "<dt>Modulation</dt>\n"
  88. "<dd>If the source is part of a hierarchy, it's volume is scaled according to the effective volume of its parent.</dd>\n"
  89. "<dt>Distance Attenuation</dt>\n"
  90. "<dd>If the source is a 3D sound source, then the volume is interpolated according to the distance model in effect and "
  91. "current listener position and orientation (see @ref SFX_3d).</dd>\n"
  92. "</dl>\n\n"
  93. "@see SFXDescription::volume\n"
  94. "@see SFXDescription::is3d\n"
  95. "@section SFXSource_fades Volume Fades\n\n"
  96. "To ease-in and ease-out playback of a sound, fade effects may be applied to sources. A fade will either go from "
  97. "zero volume to full effective volume (fade-in) or from full effective volume to zero volume (fade-out).\n\n"
  98. "Fading is coupled to the play(), pause(), and stop() methods as well as to loop iterations when SFXDescription::fadeLoops "
  99. "is true for the source. play() and the start of a loop iteration will trigger a fade-in whereas pause(), stop() and the "
  100. "end of loop iterations will trigger fade-outs.\n\n"
  101. "For looping sources, if SFXDescription::fadeLoops is false, only the initial play() will trigger a fade-in and no further "
  102. "fading will be applied to loop iterations.\n\n"
  103. "By default, the fade durations will be governed by the SFXDescription::fadeInTime and SFXDescription::fadeOutTime properties "
  104. "of the SFXDescription attached to the source. However, these may be overridden on a per-source basis by setting fade times "
  105. "explicitly with setFadeTimes(). Additionally, the set values may be overridden for individual play(), pause(), and stop() "
  106. "calls by supplying appropriate fadeInTime/fadeOutTime parameters.\n\n"
  107. "By default, volume will interpolate linearly during fades. However, custom interpolation curves can be assigned through the "
  108. "SFXDescription::fadeInEase and SFXDescription::fadeOutTime properties.\n\n"
  109. "@see SFXDescription::fadeInTime\n"
  110. "@see SFXDescription::fadeOutTime\n"
  111. "@see SFXDescription::fadeInEase\n"
  112. "@see SFXDescription::fadeOutEase\n"
  113. "@see SFXDescription::fadeLoops\n"
  114. "@section SFXSource_cones Sound Cones\n\n"
  115. "@see SFXDescription::coneInsideAngle\n"
  116. "@see SFXDescription::coneOutsideAngle\n"
  117. "@see SFXDescription::coneOutsideVolume\n"
  118. "@section SFXSource_doppler Doppler Effect\n\n"
  119. "@see sfxGetDopplerFactor\n"
  120. "@see sfxSetDopplerFactor\n"
  121. "@see SFXAmbience::dopplerFactor\n"
  122. "@section SFXSource_markers Playback Markers\n\n"
  123. "Playback markers allow to attach notification triggers to specific playback positions. Once the "
  124. "play cursor crosses a position for which a marker is defined, the #onMarkerPassed callback will "
  125. "be triggered on the SFXSource thus allowing to couple script logic to .\n\n"
  126. "Be aware that the precision with which marker callbacks are triggered are bound by global source "
  127. "update frequency. Thus there may be a delay between the play cursor actually passing a marker position "
  128. "and the callback being triggered.\n\n"
  129. "@ingroup SFX\n"
  130. );
  131. IMPLEMENT_CALLBACK( SFXSource, onStatusChange, void, ( SFXStatus newStatus ), ( newStatus ),
  132. "Called when the playback status of the source changes.\n"
  133. "@param newStatus The new playback status." );
  134. IMPLEMENT_CALLBACK( SFXSource, onParameterValueChange, void, ( SFXParameter* parameter ), ( parameter ),
  135. "Called when a parameter attached to the source changes value.\n"
  136. "This callback will be triggered before the value change has actually been applied to the source.\n"
  137. "@param parameter The parameter that has changed value.\n"
  138. "@note This is also triggered when the parameter is first attached to the source." );
  139. //-----------------------------------------------------------------------------
  140. SFXSource::SFXSource()
  141. : mStatus( SFXStatusStopped ),
  142. mSavedStatus( SFXStatusNull ),
  143. mStatusCallback( NULL ),
  144. mDescription( NULL ),
  145. mVolume( 1.f ),
  146. mPreFadeVolume( 1.f ),
  147. mFadedVolume( 1.f ),
  148. mModulativeVolume( 1.f ),
  149. mPreAttenuatedVolume( 1.f ),
  150. mAttenuatedVolume( 1.f ),
  151. mPriority( 0 ),
  152. mModulativePriority( 1.f ),
  153. mEffectivePriority( 0 ),
  154. mPitch( 1.f ),
  155. mModulativePitch( 1.f ),
  156. mEffectivePitch( 1.f ),
  157. mTransform( true ),
  158. mVelocity( 0, 0, 0 ),
  159. mMinDistance( 1 ),
  160. mMaxDistance( 100 ),
  161. mConeInsideAngle( 360 ),
  162. mConeOutsideAngle( 360 ),
  163. mConeOutsideVolume( 1 ),
  164. mDistToListener( 0.f ),
  165. mTransformScattered( false ),
  166. mFadeInTime( 0.f ),
  167. mFadeOutTime( 0.f ),
  168. mFadeInPoint( -1.f ),
  169. mFadeOutPoint( -1.f ),
  170. mFadeSegmentType( FadeSegmentNone ),
  171. mFadeSegmentEase( NULL ),
  172. mFadeSegmentStartPoint( 0.f ),
  173. mFadeSegmentEndPoint( 0.f ),
  174. mSavedFadeTime( -1.f ),
  175. mPlayStartTick( 0 )
  176. {
  177. VECTOR_SET_ASSOCIATION( mParameters );
  178. }
  179. //-----------------------------------------------------------------------------
  180. SFXSource::SFXSource( SFXTrack* track, SFXDescription* description )
  181. : mStatus( SFXStatusStopped ),
  182. mSavedStatus( SFXStatusNull ),
  183. mStatusCallback( NULL ),
  184. mTrack( track ),
  185. mDescription( description ),
  186. mVolume( 1.f ),
  187. mPreFadeVolume( 1.f ),
  188. mFadedVolume( 1.f ),
  189. mModulativeVolume( 1.f ),
  190. mPreAttenuatedVolume( 1.f ),
  191. mAttenuatedVolume( 1.f ),
  192. mPriority( 0 ),
  193. mModulativePriority( 1.f ),
  194. mEffectivePriority( 0 ),
  195. mPitch( 1.f ),
  196. mModulativePitch( 1.f ),
  197. mEffectivePitch( 1.f ),
  198. mTransform( true ),
  199. mVelocity( 0, 0, 0 ),
  200. mMinDistance( 1 ),
  201. mMaxDistance( 100 ),
  202. mConeInsideAngle( 360 ),
  203. mConeOutsideAngle( 360 ),
  204. mConeOutsideVolume( 1 ),
  205. mDistToListener( 0.f ),
  206. mTransformScattered( false ),
  207. mFadeInTime( 0.f ),
  208. mFadeOutTime( 0.f ),
  209. mFadeInPoint( -1.f ),
  210. mFadeOutPoint( -1.f ),
  211. mFadeSegmentType( FadeSegmentNone ),
  212. mFadeSegmentEase( NULL ),
  213. mFadeSegmentStartPoint( 0.f ),
  214. mFadeSegmentEndPoint( 0.f ),
  215. mSavedFadeTime( -1.f ),
  216. mPlayStartTick( 0 )
  217. {
  218. VECTOR_SET_ASSOCIATION( mParameters );
  219. if( !description && track )
  220. mDescription = track->getDescription();
  221. // Make sure we get notified when our datablocks go away
  222. // so we can kill this source.
  223. if( mTrack != NULL )
  224. deleteNotify( mTrack );
  225. deleteNotify( mDescription );
  226. }
  227. //-----------------------------------------------------------------------------
  228. SFXSource::~SFXSource()
  229. {
  230. // Disconnect from any remaining parameters.
  231. while( !mParameters.empty() )
  232. {
  233. mParameters.last()->getEventSignal().remove( this, &SFXSource::_onParameterEvent );
  234. mParameters.decrement();
  235. }
  236. }
  237. //-----------------------------------------------------------------------------
  238. void SFXSource::initPersistFields()
  239. {
  240. addGroup( "Sound" );
  241. addProtectedField( "description", TypeSFXDescriptionName, Offset( mDescription, SFXSource ),
  242. &_setDescription, &_getDescription,
  243. "The playback configuration that determines the initial sound properties and setup.\n"
  244. "Any SFXSource must have an associated SFXDescription." );
  245. addField( "statusCallback", TypeString, Offset( mStatusCallback, SFXSource ),
  246. "Name of function to call when the status of the source changes.\n\n"
  247. "The source that had its status changed is passed as the first argument to the function and the "
  248. "new status of the source is passed as the second argument." );
  249. endGroup( "Sound" );
  250. Parent::initPersistFields();
  251. }
  252. //-----------------------------------------------------------------------------
  253. bool SFXSource::processArguments( S32 argc, ConsoleValue *argv )
  254. {
  255. // Don't allow subclasses of this to be created via script. Force
  256. // usage of the SFXSystem functions.
  257. if( typeid( *this ) != typeid( SFXSource ) )
  258. {
  259. Con::errorf( ConsoleLogEntry::Script, "Use sfxCreateSource, sfxPlay, or sfxPlayOnce!" );
  260. return false;
  261. }
  262. else
  263. return Parent::processArguments( argc, argv );
  264. }
  265. //-----------------------------------------------------------------------------
  266. bool SFXSource::onAdd()
  267. {
  268. if( !Parent::onAdd() )
  269. return false;
  270. // Make sure we have a description.
  271. if( !mDescription )
  272. {
  273. Con::errorf( "SFXSource::onAdd() - no description set on source %i (%s)", getId(), getName() );
  274. return false;
  275. }
  276. // Set our initial properties.
  277. _setVolume( mDescription->mVolume );
  278. _setPitch( mDescription->mPitch );
  279. _setPriority( mDescription->mPriority );
  280. _setFadeTimes( mDescription->mFadeInTime, mDescription->mFadeOutTime );
  281. _setMinMaxDistance( mDescription->mMinDistance, mDescription->mMaxDistance );
  282. _setCone( F32( mDescription->mConeInsideAngle ),
  283. F32( mDescription->mConeOutsideAngle ),
  284. mDescription->mConeOutsideVolume );
  285. // Add the parameters from the description.
  286. for( U32 i = 0; i < SFXDescription::MaxNumParameters; ++ i )
  287. {
  288. StringTableEntry name = mDescription->mParameters[ i ];
  289. if( name && name[ 0 ] )
  290. _addParameter( name );
  291. }
  292. // Add the parameters from the track.
  293. if( mTrack != NULL )
  294. for( U32 i = 0; i < SFXTrack::MaxNumParameters; ++ i )
  295. {
  296. StringTableEntry name = mTrack->getParameter( i );
  297. if( name && name[ 0 ] )
  298. _addParameter( name );
  299. }
  300. // Add us to the description's source group.
  301. if( mDescription->mSourceGroup )
  302. mDescription->mSourceGroup->addObject( this );
  303. // Add to source set.
  304. if( Sim::getSFXSourceSet() )
  305. Sim::getSFXSourceSet()->addObject( this );
  306. // Register with SFX system.
  307. SFX->_onAddSource( this );
  308. return true;
  309. }
  310. //-----------------------------------------------------------------------------
  311. void SFXSource::onRemove()
  312. {
  313. stop();
  314. // Let the system know.
  315. SFX->_onRemoveSource( this );
  316. #ifdef DEBUG_SPEW
  317. Platform::outputDebugString( "[SFXSource] removed source '%i'", getId() );
  318. #endif
  319. Parent::onRemove();
  320. }
  321. //-----------------------------------------------------------------------------
  322. void SFXSource::onDeleteNotify( SimObject* object )
  323. {
  324. if( object == mTrack )
  325. {
  326. deleteObject();
  327. return;
  328. }
  329. Parent::onDeleteNotify( object );
  330. }
  331. //-----------------------------------------------------------------------------
  332. bool SFXSource::acceptsAsChild( SimObject* object )
  333. {
  334. return ( dynamic_cast< SFXSource* >( object ) != NULL );
  335. }
  336. //-----------------------------------------------------------------------------
  337. void SFXSource::onGroupAdd()
  338. {
  339. Parent::onGroupAdd();
  340. SFXSource* source = dynamic_cast< SFXSource* >( getGroup() );
  341. if( source )
  342. {
  343. if( source != mDescription->mSourceGroup )
  344. mFlags.set( CustomGroupFlag );
  345. else
  346. mFlags.clear( CustomGroupFlag );
  347. }
  348. //DRTODO: sync up playback state
  349. }
  350. //-----------------------------------------------------------------------------
  351. SFXSource* SFXSource::getSourceGroup() const
  352. {
  353. return dynamic_cast< SFXSource* >( getGroup() );
  354. }
  355. //-----------------------------------------------------------------------------
  356. F32 SFXSource::getElapsedPlayTime() const
  357. {
  358. return F32( mPlayTimer.getPosition() ) / 1000.f;
  359. }
  360. //-----------------------------------------------------------------------------
  361. F32 SFXSource::getElapsedPlayTimeCurrentCycle() const
  362. {
  363. // In this base class, we can't make assumptions about total playtimes
  364. // and thus cannot clamp the playtimer into range for the current cycle.
  365. // This needs to be done by subclasses.
  366. return F32( mPlayTimer.getPosition() ) / 1000.f;
  367. }
  368. //-----------------------------------------------------------------------------
  369. F32 SFXSource::getTotalPlayTime() const
  370. {
  371. return Float_Inf;
  372. }
  373. //-----------------------------------------------------------------------------
  374. void SFXSource::play( F32 fadeInTime )
  375. {
  376. SFXStatus status = getStatus();
  377. // Return if the source is already playing.
  378. if( status == SFXStatusPlaying )
  379. {
  380. // Revert fade-out if there is one.
  381. if( mFadeSegmentType != FadeSegmentNone && mFadeSegmentType != FadeSegmentPlay )
  382. {
  383. // Let easing curve remain in place. Otherwise we would have to
  384. // search the fade-in's easing curve for the point matching the
  385. // current fade volume in order to prevent volume pops.
  386. mFadeSegmentType = FadeSegmentPlay;
  387. }
  388. return;
  389. }
  390. // First check the parent source. If it is not playing,
  391. // only save our non-inherited state and return.
  392. SFXSource* sourceGroup = getSourceGroup();
  393. if( sourceGroup != NULL && !sourceGroup->isPlaying() )
  394. {
  395. mSavedStatus = SFXStatusPlaying;
  396. mSavedFadeTime = fadeInTime;
  397. return;
  398. }
  399. // Add fades, if required.
  400. if( fadeInTime == -1.f )
  401. fadeInTime = mFadeInTime;
  402. if( status == SFXStatusPaused && fadeInTime > 0.f )
  403. {
  404. // Source is paused. Set up temporary fade-in segment.
  405. mFadeSegmentEase = &mDescription->mFadeInEase;
  406. mFadeSegmentType = FadeSegmentPlay;
  407. mFadeSegmentStartPoint = getElapsedPlayTimeCurrentCycle();
  408. mFadeSegmentEndPoint = mFadeSegmentStartPoint + fadeInTime;
  409. }
  410. else if( fadeInTime > 0.f )
  411. {
  412. mFadeInPoint = fadeInTime;
  413. // Add fade-out if play-time of the source is finite and
  414. // if it is either not looping or has fading enabled on loops.
  415. F32 totalPlayTime = getTotalPlayTime();
  416. if( !mIsInf_F( totalPlayTime ) && mDescription->mFadeOutTime > 0.f && ( !mDescription->mIsLooping || mDescription->mFadeLoops ) )
  417. {
  418. mFadeOutPoint = totalPlayTime - mDescription->mFadeOutTime;
  419. // If there's an intersection between fade-in and fade-out, move them
  420. // to the midpoint.
  421. if( mFadeOutPoint < mFadeInPoint )
  422. {
  423. F32 midPoint = mFadeOutPoint + ( mFadeInPoint - mFadeOutPoint / 2 );
  424. mFadeInPoint = midPoint;
  425. mFadeOutPoint = midPoint;
  426. }
  427. }
  428. }
  429. else
  430. {
  431. mFadeInPoint = -1.f;
  432. mFadeOutPoint = -1.f;
  433. }
  434. // Start playback.
  435. if( status != SFXStatusPaused )
  436. {
  437. mPlayStartTick = Platform::getVirtualMilliseconds();
  438. mPlayTimer.reset();
  439. }
  440. _setStatus( SFXStatusPlaying );
  441. _play();
  442. #ifdef DEBUG_SPEW
  443. Platform::outputDebugString( "[SFXSource] Started playback of source '%i'", getId() );
  444. #endif
  445. }
  446. //-----------------------------------------------------------------------------
  447. void SFXSource::pause( F32 fadeOutTime )
  448. {
  449. SFXStatus status = getStatus();
  450. if( status != SFXStatusPlaying )
  451. return;
  452. // Pause playback.
  453. if( fadeOutTime == -1.f )
  454. fadeOutTime = mFadeOutTime;
  455. if( fadeOutTime > 0.f )
  456. _setupFadeOutSegment( FadeSegmentPause, fadeOutTime );
  457. else
  458. {
  459. _setStatus( SFXStatusPaused );
  460. _pause();
  461. mFadeSegmentType = FadeSegmentNone;
  462. mPreFadeVolume = mVolume;
  463. #ifdef DEBUG_SPEW
  464. Platform::outputDebugString( "[SFXSource] Paused playback of source '%i'", getId() );
  465. #endif
  466. }
  467. }
  468. //-----------------------------------------------------------------------------
  469. void SFXSource::stop( F32 fadeOutTime )
  470. {
  471. SFXStatus status = getStatus();
  472. if( status == SFXStatusStopped )
  473. return;
  474. if( status == SFXStatusPaused )
  475. {
  476. _setStatus( SFXStatusStopped );
  477. _stop();
  478. return;
  479. }
  480. if( fadeOutTime == -1.f )
  481. fadeOutTime = mFadeOutTime;
  482. if( fadeOutTime > 0.f )
  483. _setupFadeOutSegment( FadeSegmentStop, fadeOutTime );
  484. else
  485. {
  486. _setStatus( SFXStatusStopped );
  487. _stop();
  488. mFadeSegmentType = FadeSegmentNone;
  489. mPreFadeVolume = mVolume;
  490. #ifdef DEBUG_SPEW
  491. Platform::outputDebugString( "[SFXSource] Stopped playback of source '%i'", getId() );
  492. #endif
  493. }
  494. }
  495. //-----------------------------------------------------------------------------
  496. void SFXSource::update()
  497. {
  498. if( !isPlaying() )
  499. return;
  500. _update();
  501. // Update our modifiers, if any.
  502. for( ModifierList::Iterator iter = mModifiers.begin();
  503. iter != mModifiers.end(); )
  504. {
  505. ModifierList::Iterator next = iter;
  506. next ++;
  507. if( !( *iter )->update() )
  508. {
  509. delete *iter;
  510. mModifiers.erase( iter );
  511. }
  512. iter = next;
  513. }
  514. }
  515. //-----------------------------------------------------------------------------
  516. void SFXSource::_play()
  517. {
  518. mPlayTimer.start();
  519. // Resume playback of children that want to be in
  520. // playing state.
  521. for( iterator iter = begin(); iter != end(); ++ iter )
  522. {
  523. SFXSource* source = dynamic_cast< SFXSource* >( *iter );
  524. if( source && source->mSavedStatus == SFXStatusPlaying )
  525. source->play( source->mSavedFadeTime );
  526. }
  527. }
  528. //-----------------------------------------------------------------------------
  529. void SFXSource::_pause()
  530. {
  531. // Pause playing child sources.
  532. for( iterator iter = begin(); iter != end(); ++ iter )
  533. {
  534. SFXSource* source = dynamic_cast< SFXSource* >( *iter );
  535. if( source && source->isPlaying() )
  536. {
  537. source->pause( 0.f );
  538. // Save info for resuming playback.
  539. source->mSavedStatus = SFXStatusPlaying;
  540. source->mSavedFadeTime = 0.f;
  541. }
  542. }
  543. mPlayTimer.pause();
  544. }
  545. //-----------------------------------------------------------------------------
  546. void SFXSource::_stop()
  547. {
  548. // Stop all child sources.
  549. for( iterator iter = begin(); iter != end(); ++ iter )
  550. {
  551. SFXSource* source = dynamic_cast< SFXSource* >( *iter );
  552. if( source )
  553. source->stop( 0.f );
  554. }
  555. mPlayTimer.stop();
  556. }
  557. //-----------------------------------------------------------------------------
  558. void SFXSource::_update()
  559. {
  560. /// Update our modulated properties.
  561. _updateVolume( SFX->getListener().getTransform() );
  562. _updatePitch();
  563. _updatePriority();
  564. }
  565. //-----------------------------------------------------------------------------
  566. bool SFXSource::_setDescription( void* obj, const char* index, const char* data )
  567. {
  568. SFXSource* source = reinterpret_cast< SFXSource* >( obj );
  569. source->mDescription = EngineUnmarshallData< SFXDescription* >()( data );
  570. if( !source->mDescription )
  571. {
  572. Con::errorf( "SFXSource::_setDescription - No SFXDescription '%s'", data );
  573. return false;
  574. }
  575. source->notifyDescriptionChanged();
  576. return false;
  577. }
  578. //-----------------------------------------------------------------------------
  579. const char* SFXSource::_getDescription( void* obj, const char* data )
  580. {
  581. SFXSource* source = reinterpret_cast< SFXSource* >( obj );
  582. SFXDescription* description = source->mDescription;
  583. if( !description )
  584. return "";
  585. return description->getName();
  586. }
  587. //-----------------------------------------------------------------------------
  588. void SFXSource::_setStatus( SFXStatus status )
  589. {
  590. if( mStatus == status )
  591. return;
  592. mStatus = status;
  593. // Clear saved status.
  594. mSavedStatus = SFXStatusNull;
  595. // Do the callback if we have it.
  596. if( mStatusCallback && mStatusCallback[0] )
  597. {
  598. const char* statusString = SFXStatusToString( mStatus );
  599. Con::executef( mStatusCallback, getIdString(), statusString );
  600. }
  601. else if( getNamespace() )
  602. onStatusChange_callback( status );
  603. }
  604. //-----------------------------------------------------------------------------
  605. void SFXSource::_updateVolume( const MatrixF& listener )
  606. {
  607. if (!mDescription)
  608. return;
  609. // Handle fades (compute mFadedVolume).
  610. mFadedVolume = mPreFadeVolume;
  611. if( mFadeSegmentType != FadeSegmentNone )
  612. {
  613. // We have a temporary fade segment.
  614. F32 elapsed;
  615. if( mDescription->mIsLooping && !mDescription->mFadeLoops )
  616. elapsed = getElapsedPlayTime();
  617. else
  618. elapsed = getElapsedPlayTimeCurrentCycle();
  619. if( elapsed < mFadeSegmentEndPoint )
  620. {
  621. const F32 duration = mFadeSegmentEndPoint - mFadeSegmentStartPoint;
  622. if( mFadeSegmentType == FadeSegmentPlay )
  623. {
  624. const F32 time = elapsed - mFadeSegmentStartPoint;
  625. mFadedVolume = mFadeSegmentEase->getValue
  626. ( time, 0.f, mPreFadeVolume, duration );
  627. }
  628. else
  629. {
  630. // If the end-point to the ease functions is 0,
  631. // we'll always get out the start point. Thus do the whole
  632. // thing backwards here.
  633. const F32 time = mFadeSegmentEndPoint - elapsed;
  634. mFadedVolume = mFadeSegmentEase->getValue
  635. ( time, 0.f, mPreFadeVolume, duration );
  636. }
  637. }
  638. else
  639. {
  640. // The fade segment has played. Remove it.
  641. switch( mFadeSegmentType )
  642. {
  643. case FadeSegmentStop:
  644. stop( 0.f );
  645. break;
  646. case FadeSegmentPause:
  647. pause( 0.f );
  648. break;
  649. case FadeSegmentPlay: // Nothing to do.
  650. default:
  651. break;
  652. }
  653. mFadeSegmentType = FadeSegmentNone;
  654. mPreFadeVolume = mVolume;
  655. }
  656. }
  657. else if( mFadeInPoint != -1 || mFadeOutPoint != -1 )
  658. {
  659. F32 elapsed;
  660. if( mDescription->mIsLooping && !mDescription->mFadeLoops )
  661. elapsed = getElapsedPlayTime();
  662. else
  663. elapsed = getElapsedPlayTimeCurrentCycle();
  664. // Check for fade-in.
  665. if( mFadeInPoint != -1 )
  666. {
  667. if( elapsed < mFadeInPoint )
  668. mFadedVolume = mDescription->mFadeInEase.getValue( elapsed, 0.f, mPreFadeVolume, mFadeInPoint );
  669. else if( mDescription->mIsLooping && !mDescription->mFadeLoops )
  670. {
  671. // Deactivate fade-in so we don't see it on further loops.
  672. mFadeInPoint = -1;
  673. }
  674. }
  675. // Check for fade-out.
  676. if( mFadeOutPoint != -1 && elapsed > mFadeOutPoint )
  677. {
  678. const F32 totalPlayTime = getTotalPlayTime();
  679. const F32 duration = totalPlayTime - mFadeOutPoint;
  680. const F32 time = totalPlayTime - elapsed;
  681. mFadedVolume = mDescription->mFadeOutEase.getValue( time, 0.f, mPreFadeVolume, duration );
  682. }
  683. }
  684. // Compute the pre-attenuated volume.
  685. mPreAttenuatedVolume =
  686. mFadedVolume
  687. * mModulativeVolume;
  688. SFXSource* group = getSourceGroup();
  689. if( group )
  690. mPreAttenuatedVolume *= group->getAttenuatedVolume();
  691. if( !is3d() )
  692. {
  693. mDistToListener = 0.0f;
  694. mAttenuatedVolume = mPreAttenuatedVolume;
  695. }
  696. else
  697. {
  698. // For 3D sounds, compute distance attenuation.
  699. Point3F pos, lpos;
  700. mTransform.getColumn( 3, &pos );
  701. listener.getColumn( 3, &lpos );
  702. mDistToListener = ( pos - lpos ).len();
  703. mAttenuatedVolume = SFXDistanceAttenuation(
  704. SFX->getDistanceModel(),
  705. mMinDistance,
  706. mMaxDistance,
  707. mDistToListener,
  708. mPreAttenuatedVolume,
  709. SFX->getRolloffFactor() );
  710. }
  711. }
  712. //-----------------------------------------------------------------------------
  713. void SFXSource::_updatePitch()
  714. {
  715. if (!mDescription)
  716. return;
  717. mEffectivePitch = mModulativePitch * mPitch;
  718. }
  719. //-----------------------------------------------------------------------------
  720. void SFXSource::_updatePriority()
  721. {
  722. if (!mDescription)
  723. return;
  724. mEffectivePriority = mPriority * mModulativePriority;
  725. SFXSource* group = getSourceGroup();
  726. if( group )
  727. mEffectivePriority *= group->getEffectivePriority();
  728. }
  729. //-----------------------------------------------------------------------------
  730. void SFXSource::setTransform( const MatrixF& transform )
  731. {
  732. mTransform = transform;
  733. }
  734. //-----------------------------------------------------------------------------
  735. void SFXSource::setVelocity( const VectorF& velocity )
  736. {
  737. mVelocity = velocity;
  738. }
  739. //-----------------------------------------------------------------------------
  740. void SFXSource::setFadeTimes( F32 fadeInTime, F32 fadeOutTime )
  741. {
  742. _setFadeTimes( fadeInTime, fadeOutTime );
  743. if( mFadeInTime >= 0.f || mFadeOutTime >= 0.f )
  744. mFlags.set( CustomFadeFlag );
  745. }
  746. //-----------------------------------------------------------------------------
  747. void SFXSource::_setFadeTimes( F32 fadeInTime, F32 fadeOutTime )
  748. {
  749. if( fadeInTime >= 0.f )
  750. mFadeInTime = getMax( 0.f, fadeInTime );
  751. else
  752. mFadeInTime = mDescription->mFadeInTime;
  753. if( fadeOutTime >= 0.f )
  754. mFadeOutTime = getMax( 0.f, fadeOutTime );
  755. else
  756. mFadeOutTime = mDescription->mFadeOutTime;
  757. }
  758. //-----------------------------------------------------------------------------
  759. void SFXSource::_setupFadeOutSegment( FadeSegmentType type, F32 fadeOutTime )
  760. {
  761. // Get the current faded volume as the start volume so
  762. // that we correctly fade when starting in a fade.
  763. _updateVolume( SFX->getListener().getTransform() );
  764. mPreFadeVolume = mFadedVolume;
  765. mFadeSegmentEase = &mDescription->mFadeOutEase;
  766. mFadeSegmentType = type;
  767. if( mDescription->mIsLooping && mDescription->mFadeLoops )
  768. {
  769. mFadeSegmentStartPoint = getElapsedPlayTimeCurrentCycle();
  770. mFadeSegmentEndPoint = getMin( mFadeSegmentStartPoint + fadeOutTime, getTotalPlayTime() );
  771. }
  772. else
  773. {
  774. mFadeSegmentStartPoint = getElapsedPlayTime();
  775. mFadeSegmentEndPoint = mFadeSegmentStartPoint + fadeOutTime;
  776. }
  777. }
  778. //-----------------------------------------------------------------------------
  779. void SFXSource::_setMinMaxDistance( F32 min, F32 max )
  780. {
  781. mMinDistance = getMax( 0.0f, min );
  782. mMaxDistance = getMax( mMinDistance, max );
  783. }
  784. //-----------------------------------------------------------------------------
  785. void SFXSource::_setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume )
  786. {
  787. mConeInsideAngle = mClampF( innerAngle, 0.0, 360.0 );
  788. mConeOutsideAngle = mClampF( outerAngle, mConeInsideAngle, 360.0 );
  789. mConeOutsideVolume = mClampF( outerVolume, 0.0, 1.0 );
  790. }
  791. //-----------------------------------------------------------------------------
  792. void SFXSource::_setVolume( F32 volume )
  793. {
  794. mVolume = mClampF( volume, 0.f, 1.f );
  795. mPreFadeVolume = mVolume;
  796. _updateVolume( SFX->getListener( 0 ).getTransform() );
  797. }
  798. //-----------------------------------------------------------------------------
  799. void SFXSource::setModulativeVolume( F32 value )
  800. {
  801. mModulativeVolume = mClampF( value, 0.f, 1.f );
  802. _updateVolume( SFX->getListener( 0 ).getTransform() );
  803. }
  804. //-----------------------------------------------------------------------------
  805. void SFXSource::_setPitch( F32 pitch )
  806. {
  807. mPitch = mClampF( pitch, 0.001f, 2.0f );
  808. _updatePitch();
  809. }
  810. //-----------------------------------------------------------------------------
  811. void SFXSource::setModulativePitch( F32 value )
  812. {
  813. mModulativePitch = mClampF( value, 0.001f, 2.0f );
  814. _updatePitch();
  815. }
  816. //-----------------------------------------------------------------------------
  817. void SFXSource::_setPriority( F32 priority )
  818. {
  819. mPriority = priority;
  820. _updatePriority();
  821. }
  822. //-----------------------------------------------------------------------------
  823. void SFXSource::setModulativePriority( F32 value )
  824. {
  825. mModulativePriority = value;
  826. _updatePriority();
  827. }
  828. //-----------------------------------------------------------------------------
  829. SFXTrack* SFXSource::getTrack() const
  830. {
  831. return mTrack;
  832. }
  833. //-----------------------------------------------------------------------------
  834. void SFXSource::notifyDescriptionChanged()
  835. {
  836. if( !mFlags.test( CustomVolumeFlag ) )
  837. _setVolume( mDescription->mVolume );
  838. if( !mFlags.test( CustomPitchFlag ) )
  839. _setPitch( mDescription->mPitch );
  840. if( !mFlags.test( CustomPriorityFlag ) )
  841. _setPriority( mDescription->mPriority );
  842. if( !mFlags.test( CustomRadiusFlag ) )
  843. _setMinMaxDistance( mDescription->mMinDistance, mDescription->mMaxDistance );
  844. if( !mFlags.test( CustomConeFlag ) )
  845. _setCone( mDescription->mConeInsideAngle, mDescription->mConeOutsideAngle, mDescription->mConeOutsideVolume );
  846. if( !mFlags.test( CustomFadeFlag ) )
  847. _setFadeTimes( mDescription->mFadeInTime, mDescription->mFadeOutTime );
  848. if( !mFlags.test( CustomGroupFlag ) && mDescription->mSourceGroup != NULL && getGroup() != mDescription->mSourceGroup )
  849. mDescription->mSourceGroup->addObject( this );
  850. }
  851. //-----------------------------------------------------------------------------
  852. void SFXSource::notifyTrackChanged()
  853. {
  854. //RDTODO
  855. }
  856. //-----------------------------------------------------------------------------
  857. template< class T >
  858. void SFXSource::_clearModifiers()
  859. {
  860. for( ModifierList::Iterator iter = mModifiers.begin();
  861. iter != mModifiers.end(); )
  862. {
  863. ModifierList::Iterator next = iter;
  864. next ++;
  865. if( dynamic_cast< T* >( *iter ) )
  866. {
  867. delete *iter;
  868. mModifiers.erase( iter );
  869. }
  870. iter = next;
  871. }
  872. }
  873. //-----------------------------------------------------------------------------
  874. void SFXSource::addModifier( SFXModifier* modifier )
  875. {
  876. mModifiers.pushBack( modifier );
  877. }
  878. //-----------------------------------------------------------------------------
  879. void SFXSource::addMarker( const String& name, F32 pos )
  880. {
  881. addModifier( new SFXMarkerModifier( this, name, pos ) );
  882. }
  883. //-----------------------------------------------------------------------------
  884. void SFXSource::addParameter( SFXParameter* parameter )
  885. {
  886. for( U32 i = 0; i < mParameters.size(); ++ i )
  887. if( mParameters[ i ] == parameter )
  888. return;
  889. mParameters.push_back( parameter );
  890. parameter->getEventSignal().notify( this, &SFXSource::_onParameterEvent );
  891. // Trigger an initial value-changed event so the
  892. // current parameter value becomes effective.
  893. _onParameterEvent( parameter, SFXParameterEvent_ValueChanged );
  894. }
  895. //-----------------------------------------------------------------------------
  896. void SFXSource::removeParameter( SFXParameter* parameter )
  897. {
  898. for( U32 i = 0; i < mParameters.size(); ++ i )
  899. if( mParameters[ i ] == parameter )
  900. {
  901. mParameters[ i ]->getEventSignal().remove( this, &SFXSource::_onParameterEvent );
  902. mParameters.erase( i );
  903. break;
  904. }
  905. }
  906. //-----------------------------------------------------------------------------
  907. void SFXSource::_addParameter( StringTableEntry name )
  908. {
  909. SFXParameter* parameter = dynamic_cast< SFXParameter* >(
  910. Sim::getSFXParameterGroup()->findObjectByInternalName( name )
  911. );
  912. if( !parameter )
  913. {
  914. Con::errorf( "SFXSource::_addParameter - no parameter called '%s'", name );
  915. return;
  916. }
  917. addParameter( parameter );
  918. }
  919. //-----------------------------------------------------------------------------
  920. void SFXSource::_onParameterEvent( SFXParameter* parameter, SFXParameterEvent event )
  921. {
  922. switch( event )
  923. {
  924. case SFXParameterEvent_ValueChanged:
  925. onParameterValueChange_callback( parameter );
  926. switch( parameter->getChannel() )
  927. {
  928. case SFXChannelStatus:
  929. {
  930. F32 value = parameter->getValue();
  931. if( value >= F32( SFXStatusPlaying ) && value <= F32( SFXStatusStopped ) )
  932. {
  933. SFXStatus status = ( SFXStatus ) U32( value );
  934. switch( status )
  935. {
  936. case SFXStatusPlaying: play(); break;
  937. case SFXStatusPaused: pause(); break;
  938. case SFXStatusStopped: stop(); break;
  939. default:
  940. break;
  941. }
  942. }
  943. break;
  944. }
  945. case SFXChannelVolume:
  946. setVolume( parameter->getValue() );
  947. break;
  948. case SFXChannelPitch:
  949. setPitch( parameter->getValue() );
  950. break;
  951. case SFXChannelPriority:
  952. setPriority( parameter->getValue() );
  953. break;
  954. case SFXChannelMinDistance:
  955. setMinMaxDistance( parameter->getValue(), mMaxDistance );
  956. break;
  957. case SFXChannelMaxDistance:
  958. setMinMaxDistance( mMinDistance, parameter->getValue() );
  959. break;
  960. case SFXChannelPositionX:
  961. {
  962. Point3F position = mTransform.getPosition();
  963. position.x = parameter->getValue();
  964. mTransform.setPosition( position );
  965. setTransform( mTransform );
  966. break;
  967. }
  968. case SFXChannelPositionY:
  969. {
  970. Point3F position = mTransform.getPosition();
  971. position.y = parameter->getValue();
  972. mTransform.setPosition( position );
  973. setTransform( mTransform );
  974. break;
  975. }
  976. case SFXChannelPositionZ:
  977. {
  978. Point3F position = mTransform.getPosition();
  979. position.z = parameter->getValue();
  980. mTransform.setPosition( position );
  981. setTransform( mTransform );
  982. break;
  983. }
  984. case SFXChannelRotationX:
  985. {
  986. EulerF angles = mTransform.toEuler();
  987. angles.x = parameter->getValue();
  988. MatrixF transform( angles );
  989. transform.setPosition( mTransform.getPosition() );
  990. setTransform( transform );
  991. break;
  992. }
  993. case SFXChannelRotationY:
  994. {
  995. EulerF angles = mTransform.toEuler();
  996. angles.y = parameter->getValue();
  997. MatrixF transform( angles );
  998. transform.setPosition( mTransform.getPosition() );
  999. setTransform( transform );
  1000. break;
  1001. }
  1002. case SFXChannelRotationZ:
  1003. {
  1004. EulerF angles = mTransform.toEuler();
  1005. angles.z = parameter->getValue();
  1006. MatrixF transform( angles );
  1007. transform.setPosition( mTransform.getPosition() );
  1008. setTransform( transform );
  1009. break;
  1010. }
  1011. case SFXChannelVelocityX:
  1012. {
  1013. mVelocity.x = parameter->getValue();
  1014. setVelocity( mVelocity );
  1015. break;
  1016. }
  1017. case SFXChannelVelocityY:
  1018. {
  1019. mVelocity.y = parameter->getValue();
  1020. setVelocity( mVelocity );
  1021. break;
  1022. }
  1023. case SFXChannelVelocityZ:
  1024. {
  1025. mVelocity.z = parameter->getValue();
  1026. setVelocity( mVelocity );
  1027. break;
  1028. }
  1029. default:
  1030. break;
  1031. }
  1032. break;
  1033. case SFXParameterEvent_Deleted:
  1034. removeParameter( parameter );
  1035. break;
  1036. }
  1037. }
  1038. //-----------------------------------------------------------------------------
  1039. void SFXSource::_scatterTransform()
  1040. {
  1041. if( mDescription )
  1042. {
  1043. Point3F position = mTransform.getPosition();
  1044. for( U32 i = 0; i < 3; ++ i )
  1045. {
  1046. F32 scatterDist = mDescription->mScatterDistance[ i ];
  1047. if( scatterDist != 0.f )
  1048. position[ 0 ] += gRandGen.randF( - scatterDist, scatterDist );
  1049. }
  1050. mTransform.setPosition( position );
  1051. }
  1052. mTransformScattered = true;
  1053. }
  1054. //=============================================================================
  1055. // Console Methods.
  1056. //=============================================================================
  1057. // MARK: ---- Console Methods ----
  1058. //-----------------------------------------------------------------------------
  1059. DefineEngineMethod( SFXSource, play, void, ( F32 fadeInTime ), ( -1.0f ),
  1060. "Start playback of the source.\n"
  1061. "If the sound data for the source has not yet been fully loaded, there will be a delay after calling "
  1062. "play and playback will start after the data has become available.\n\n"
  1063. "@param fadeInTime Seconds for the sound to reach full volume. If -1, the SFXDescription::fadeInTime "
  1064. "set in the source's associated description is used. Pass 0 to disable a fade-in effect that may "
  1065. "be configured on the description." )
  1066. {
  1067. object->play( fadeInTime );
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. DefineEngineMethod( SFXSource, stop, void, ( F32 fadeOutTime ), ( -1.0f ),
  1071. "Stop playback of the source.\n"
  1072. "@param fadeOutTime Seconds for the sound to fade down to zero volume. If -1, the SFXDescription::fadeOutTime "
  1073. "set in the source's associated description is used. Pass 0 to disable a fade-out effect that may be "
  1074. "configured on the description.\n"
  1075. "Be aware that if a fade-out effect is used, the source will not immediately transtion to stopped state but "
  1076. "will rather remain in playing state until the fade-out time has expired." )
  1077. {
  1078. object->stop( fadeOutTime );
  1079. }
  1080. //-----------------------------------------------------------------------------
  1081. DefineEngineMethod( SFXSource, pause, void, ( F32 fadeOutTime ), ( -1.0f ),
  1082. "Pause playback of the source.\n"
  1083. "@param fadeOutTime Seconds for the sound to fade down to zero volume. If -1, the SFXDescription::fadeOutTime "
  1084. "set in the source's associated description is used. Pass 0 to disable a fade-out effect that may be "
  1085. "configured on the description.\n"
  1086. "Be aware that if a fade-out effect is used, the source will not immediately to paused state but will "
  1087. "rather remain in playing state until the fade-out time has expired.." )
  1088. {
  1089. object->pause( fadeOutTime );
  1090. }
  1091. //-----------------------------------------------------------------------------
  1092. DefineEngineMethod( SFXSource, isPlaying, bool, (),,
  1093. "Test whether the source is currently playing.\n"
  1094. "@return True if the source is in playing state, false otherwise.\n\n"
  1095. "@see play\n"
  1096. "@see getStatus\n"
  1097. "@see SFXStatus" )
  1098. {
  1099. return object->isPlaying();
  1100. }
  1101. //-----------------------------------------------------------------------------
  1102. DefineEngineMethod( SFXSource, isPaused, bool, (),,
  1103. "Test whether the source is currently paused.\n"
  1104. "@return True if the source is in paused state, false otherwise.\n\n"
  1105. "@see pause\n"
  1106. "@see getStatus\n"
  1107. "@see SFXStatus" )
  1108. {
  1109. return object->isPaused();
  1110. }
  1111. //-----------------------------------------------------------------------------
  1112. DefineEngineMethod( SFXSource, isStopped, bool, (),,
  1113. "Test whether the source is currently stopped.\n"
  1114. "@return True if the source is in stopped state, false otherwise.\n\n"
  1115. "@see stop\n"
  1116. "@see getStatus\n"
  1117. "@see SFXStatus" )
  1118. {
  1119. return object->isStopped();
  1120. }
  1121. //-----------------------------------------------------------------------------
  1122. DefineEngineMethod( SFXSource, getStatus, SFXStatus, (),,
  1123. "Get the current playback status.\n"
  1124. "@return Te current playback status\n" )
  1125. {
  1126. return object->getStatus();
  1127. }
  1128. //-----------------------------------------------------------------------------
  1129. DefineEngineMethod( SFXSource, getVolume, F32, (),,
  1130. "Get the current base volume level of the source.\n"
  1131. "This is not the final effective volume that the source is playing at but rather the starting "
  1132. "volume level before source group modulation, fades, or distance-based volume attenuation are applied.\n\n"
  1133. "@return The current base volume level.\n\n"
  1134. "@see setVolume\n"
  1135. "@see SFXDescription::volume\n\n"
  1136. "@ref SFXSource_volume" )
  1137. {
  1138. return object->getVolume();
  1139. }
  1140. //-----------------------------------------------------------------------------
  1141. DefineEngineMethod( SFXSource, setVolume, void, ( F32 volume ),,
  1142. "Set the base volume level for the source.\n"
  1143. "This volume will be the starting point for source group volume modulation, fades, and distance-based "
  1144. "volume attenuation.\n\n"
  1145. "@param volume The new base volume level for the source. Must be 0>=volume<=1.\n\n"
  1146. "@see getVolume\n\n"
  1147. "@ref SFXSource_volume" )
  1148. {
  1149. object->setVolume( volume );
  1150. }
  1151. //-----------------------------------------------------------------------------
  1152. DefineEngineMethod( SFXSource, getAttenuatedVolume, F32, (),,
  1153. "Get the final effective volume level of the source.\n\n"
  1154. "This method returns the volume level as it is after source group volume modulation, fades, and distance-based "
  1155. "volume attenuation have been applied to the base volume level.\n\n"
  1156. "@return The effective volume of the source.\n\n"
  1157. "@ref SFXSource_volume" )
  1158. {
  1159. return object->getAttenuatedVolume();
  1160. }
  1161. //-----------------------------------------------------------------------------
  1162. DefineEngineMethod( SFXSource, getFadeInTime, F32, (),,
  1163. "Get the fade-in time set on the source.\n"
  1164. "This will initially be SFXDescription::fadeInTime.\n\n"
  1165. "@return The fade-in time set on the source in seconds.\n\n"
  1166. "@see SFXDescription::fadeInTime\n\n"
  1167. "@ref SFXSource_fades" )
  1168. {
  1169. return object->getFadeInTime();
  1170. }
  1171. //-----------------------------------------------------------------------------
  1172. DefineEngineMethod( SFXSource, getFadeOutTime, F32, (),,
  1173. "Get the fade-out time set on the source.\n"
  1174. "This will initially be SFXDescription::fadeOutTime.\n\n"
  1175. "@return The fade-out time set on the source in seconds.\n\n"
  1176. "@see SFXDescription::fadeOutTime\n\n"
  1177. "@ref SFXSource_fades" )
  1178. {
  1179. return object->getFadeOutTime();
  1180. }
  1181. //-----------------------------------------------------------------------------
  1182. DefineEngineMethod( SFXSource, setFadeTimes, void, ( F32 fadeInTime, F32 fadeOutTime ),,
  1183. "Set the fade time parameters of the source.\n"
  1184. "@param fadeInTime The new fade-in time in seconds.\n"
  1185. "@param fadeOutTime The new fade-out time in seconds.\n\n"
  1186. "@see SFXDescription::fadeInTime\n"
  1187. "@see SFXDescription::fadeOutTime\n\n"
  1188. "@ref SFXSource_fades" )
  1189. {
  1190. object->setFadeTimes( fadeInTime, fadeOutTime );
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. DefineEngineMethod( SFXSource, getPitch, F32, (),,
  1194. "Get the pitch scale of the source.\n"
  1195. "Pitch determines the playback speed of the source (default: 1).\n\n"
  1196. "@return The current pitch scale factor of the source.\n\n"
  1197. "@see setPitch\n"
  1198. "@see SFXDescription::pitch" )
  1199. {
  1200. return object->getPitch();
  1201. }
  1202. //-----------------------------------------------------------------------------
  1203. DefineEngineMethod( SFXSource, setPitch, void, ( F32 pitch ),,
  1204. "Set the pitch scale of the source.\n"
  1205. "Pitch determines the playback speed of the source (default: 1).\n\n"
  1206. "@param pitch The new pitch scale factor.\n\n"
  1207. "@see getPitch\n"
  1208. "@see SFXDescription::pitch" )
  1209. {
  1210. object->setPitch( pitch );
  1211. }
  1212. //-----------------------------------------------------------------------------
  1213. // Need an overload here as we can't use a default parameter to signal omission of the direction argument
  1214. // and we need to properly detect the omission to leave the currently set direction on the source as is.
  1215. /* LukasPJ: For now, just use the setTransform with strings as parameters.
  1216. DEFINE_CALLIN( fnSFXSoure_setTransform1, setTransform, SFXSource, void, ( SFXSource* source, const VectorF& position ),,,
  1217. "Set the position of the source's 3D sound.\n\n"
  1218. "@param position The new position in world space.\n"
  1219. "@note This method has no effect if the source is not a 3D source." )
  1220. {
  1221. MatrixF mat = source->getTransform();
  1222. mat.setPosition( position );
  1223. source->setTransform( mat );
  1224. }
  1225. DEFINE_CALLIN( fnSFXSoure_setTransform2, setTransform, SFXSource, void, ( SFXSource* source, const VectorF& position, const VectorF& direction ),,,
  1226. "Set the position and orientation of the source's 3D sound.\n\n"
  1227. "@param position The new position in world space.\n"
  1228. "@param direction The forward vector." )
  1229. {
  1230. MatrixF mat = source->getTransform();
  1231. mat.setPosition( position );
  1232. mat.setColumn( 1, direction );
  1233. source->setTransform( mat );
  1234. }
  1235. */
  1236. // Console interop version.
  1237. static ConsoleDocFragment _sSetTransform1(
  1238. "Set the position of the source's 3D sound.\n\n"
  1239. "@param position The new position in world space.\n"
  1240. "@note This method has no effect if the source is not a 3D source.",
  1241. "SFXSource",
  1242. "void setTransform( Point3F position )"
  1243. );
  1244. static ConsoleDocFragment _sSetTransform2(
  1245. "Set the position and orientation of the source's 3D sound.\n\n"
  1246. "@param position The new position in world space.\n"
  1247. "@param direction The forward vector.",
  1248. "SFXSource",
  1249. "void setTransform( Point3F position, Point3F direction )"
  1250. );
  1251. DefineEngineMethod( SFXSource, setTransform, void, ( const char * position, const char * direction ), ( "" ),
  1252. "( vector position [, vector direction ] ) "
  1253. "Set the position and orientation of a 3D sound source.\n"
  1254. "@hide" )
  1255. {
  1256. MatrixF mat = object->getTransform();
  1257. if(String::compare( position , "")!=0 )
  1258. {
  1259. Point3F pos;
  1260. dSscanf( position, "%g %g %g", &pos.x, &pos.y, &pos.z );
  1261. mat.setPosition( pos );
  1262. }
  1263. if(String::compare( direction ,"")!=0 )
  1264. {
  1265. Point3F dir;
  1266. dSscanf( direction, "%g %g %g", &dir.x, &dir.y, &dir.z );
  1267. mat.setColumn( 1, dir );
  1268. }
  1269. object->setTransform( mat );
  1270. }
  1271. //-----------------------------------------------------------------------------
  1272. DefineEngineMethod( SFXSource, setCone, void, ( F32 innerAngle, F32 outerAngle, F32 outsideVolume ),,
  1273. "Set up the 3D volume cone for the source.\n\n"
  1274. "@param innerAngle Angle of the inner sound cone in degrees (@ref SFXDescription::coneInsideAngle). Must be 0<=innerAngle<=360.\n"
  1275. "@param outerAngle Angle of the outer sound cone in degrees (@ref SFXDescription::coneOutsideAngle). Must be 0<=outerAngle<=360.\n"
  1276. "@param outsideVolume Volume scale factor outside of outer cone (@ref SFXDescription::coneOutsideVolume). Must be 0<=outsideVolume<=1.\n"
  1277. "@note This method has no effect on the source if the source is not 3D.\n\n" )
  1278. {
  1279. bool isValid = true;
  1280. if( innerAngle < 0.0 || innerAngle > 360.0 )
  1281. {
  1282. Con::errorf( "SFXSource.setCone() - 'innerAngle' must be between 0 and 360" );
  1283. isValid = false;
  1284. }
  1285. if( outerAngle < 0.0 || outerAngle > 360.0 )
  1286. {
  1287. Con::errorf( "SFXSource.setCone() - 'outerAngle' must be between 0 and 360" );
  1288. isValid = false;
  1289. }
  1290. if( outsideVolume < 0.0 || outsideVolume > 1.0 )
  1291. {
  1292. Con::errorf( "SFXSource.setCone() - 'outsideVolume' must be between 0 and 1" );
  1293. isValid = false;
  1294. }
  1295. if( !isValid )
  1296. return;
  1297. object->setCone( innerAngle, outerAngle, outsideVolume );
  1298. }
  1299. //-----------------------------------------------------------------------------
  1300. DefineEngineMethod( SFXSource, getParameterCount, S32, (),,
  1301. "Get the number of SFXParameters that are attached to the source.\n"
  1302. "@return The number of parameters attached to the source.\n\n"
  1303. "@tsexample\n"
  1304. "// Print the name ofo each parameter attached to %source.\n"
  1305. "%numParams = %source.getParameterCount();\n"
  1306. "for( %i = 0; %i < %numParams; %i ++ )\n"
  1307. " echo( %source.getParameter( %i ).getParameterName() );\n"
  1308. "@endtsexample\n\n"
  1309. "@see getParameter\n"
  1310. "@see addParameter\n" )
  1311. {
  1312. return object->getNumParameters();
  1313. }
  1314. //-----------------------------------------------------------------------------
  1315. DefineEngineMethod( SFXSource, addParameter, void, ( SFXParameter* parameter ),,
  1316. "Attach @a parameter to the source,\n\n"
  1317. "Once attached, the source will react to value changes of the given @a parameter. Attaching a parameter "
  1318. "will also trigger an initial read-out of the parameter's current value.\n\n"
  1319. "@param parameter The parameter to attach to the source." )
  1320. {
  1321. if( parameter )
  1322. object->addParameter( parameter );
  1323. }
  1324. //-----------------------------------------------------------------------------
  1325. DefineEngineMethod( SFXSource, removeParameter, void, ( SFXParameter* parameter ),,
  1326. "Detach @a parameter from the source.\n\n"
  1327. "Once detached, the source will no longer react to value changes of the given @a parameter.\n\n"
  1328. "If the parameter is not attached to the source, the method will do nothing.\n\n"
  1329. "@param parameter The parameter to detach from the source.\n" )
  1330. {
  1331. if( parameter )
  1332. object->removeParameter( parameter );
  1333. }
  1334. //-----------------------------------------------------------------------------
  1335. DefineEngineMethod( SFXSource, getParameter, SFXParameter*, ( S32 index ),,
  1336. "Get the parameter at the given index.\n"
  1337. "@param index Index of the parameter to fetch. Must be 0<=index<=getParameterCount().\n"
  1338. "@return The parameter at the given @a index or null if @a index is out of range.\n\n"
  1339. "@tsexample\n"
  1340. "// Print the name ofo each parameter attached to %source.\n"
  1341. "%numParams = %source.getParameterCount();\n"
  1342. "for( %i = 0; %i < %numParams; %i ++ )\n"
  1343. " echo( %source.getParameter( %i ).getParameterName() );\n"
  1344. "@endtsexample\n\n"
  1345. "@see getParameterCount" )
  1346. {
  1347. if( index >= object->getNumParameters() )
  1348. {
  1349. Con::errorf( "SFXSource::getParameter - index out of range: %i", index );
  1350. return 0;
  1351. }
  1352. return object->getParameter( index );
  1353. }
  1354. //-----------------------------------------------------------------------------
  1355. DefineEngineMethod( SFXSource, addMarker, void, ( String name, F32 pos ),,
  1356. "Add a notification marker called @a name at @a pos seconds of playback.\n\n"
  1357. "@param name Symbolic name for the marker that will be passed to the onMarkerPassed() callback.\n"
  1358. "@param pos Playback position in seconds when the notification should trigger. Note that this is a soft limit and there "
  1359. "may be a delay between the play cursor actually passing the position and the callback being triggered.\n\n"
  1360. "@note For looped sounds, the marker will trigger on each iteration.\n\n"
  1361. "@tsexample\n"
  1362. "// Create a new source.\n"
  1363. "$source = sfxCreateSource( AudioMusicLoop2D, \"art/sound/backgroundMusic\" );\n"
  1364. "\n"
  1365. "// Assign a class to the source.\n"
  1366. "$source.class = \"BackgroundMusic\";\n"
  1367. "\n"
  1368. "// Add a playback marker at one minute into playback.\n"
  1369. "$source.addMarker( \"first\", 60 );\n"
  1370. "\n"
  1371. "// Define the callback function. This function will be called when the playback position passes the one minute mark.\n"
  1372. "function BackgroundMusic::onMarkerPassed( %this, %markerName )\n"
  1373. "{\n"
  1374. " if( %markerName $= \"first\" )\n"
  1375. " echo( \"Playback has passed the 60 seconds mark.\" );\n"
  1376. "}\n"
  1377. "\n"
  1378. "// Play the sound.\n"
  1379. "$source.play();\n"
  1380. "@endtsexample" )
  1381. {
  1382. object->addMarker( name, pos );
  1383. }