effectsound.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /**
  2. \brief Wrapper class for managing sound through SEffectManager
  3. */
  4. class EffectSound : Effect
  5. {
  6. /** \name Event invokers
  7. * ScriptInvonkers for certain events
  8. */
  9. //@{
  10. ref ScriptInvoker Event_OnSoundWaveStarted = new ScriptInvoker();
  11. ref ScriptInvoker Event_OnSoundWaveEnded = new ScriptInvoker();
  12. ref ScriptInvoker Event_OnSoundFadeInStopped = new ScriptInvoker();
  13. ref ScriptInvoker Event_OnSoundFadeOutStarted = new ScriptInvoker();
  14. //@}
  15. /** \name Sound objects and structures
  16. * Objects and structures for the sound
  17. */
  18. //@{
  19. protected ref SoundParams m_SoundParams;
  20. protected ref SoundObjectBuilder m_SoundObjectBuilder;
  21. protected ref SoundObject m_SoundObject;
  22. protected AbstractWave m_SoundWaveObject;
  23. //@}
  24. /** \name Generic data
  25. * Generic data for the sound
  26. */
  27. //@{
  28. protected WaveKind m_SoundWaveKind;
  29. protected string m_SoundSetName;
  30. protected bool m_SoundLoop;
  31. protected bool m_SetEnvVariables;
  32. protected bool m_SoundAutodestroy;
  33. protected bool m_SoundWaveIsPlaying;
  34. protected float m_SoundWaveLenght;
  35. protected float m_SoundWaveVolume;
  36. protected float m_SoundWaveVolumeMax;
  37. protected float m_SoundWaveTime;
  38. protected int m_SoundDoppler;
  39. //@}
  40. /** \name Fading data
  41. * Data for fadein/fadeout for the sound
  42. */
  43. //@{
  44. protected bool m_SoundWaveStarting;
  45. protected bool m_SoundWaveStopping;
  46. protected bool m_SoundFadedOut;
  47. protected float m_SoundFadeInDuration;
  48. protected float m_SoundFadeOutStartTime;
  49. protected float m_SoundFadeOutDuration;
  50. protected float m_SoundFadeOutInitVolume;
  51. //@}
  52. /**
  53. \brief ctor
  54. */
  55. void EffectSound()
  56. {
  57. m_SoundWaveKind = WaveKind.WAVEEFFECTEX;
  58. m_SoundWaveVolume = 0;
  59. m_SoundWaveVolumeMax = 1;
  60. m_SoundAutodestroy = false;
  61. m_SoundWaveStopping = false;
  62. m_SoundFadedOut = false;
  63. m_SoundDoppler = -1;
  64. }
  65. /**
  66. \brief dtor
  67. */
  68. void ~EffectSound()
  69. {
  70. }
  71. /**
  72. \brief init
  73. */
  74. override void InitEffect()
  75. {
  76. super.InitEffect();
  77. // These will be called by the sound events
  78. Event_OnStarted.Remove(Event_OnEffectStarted);
  79. Event_OnStopped.Remove(Event_OnEffectEnded);
  80. }
  81. /**
  82. \brief Override when getting debug information
  83. */
  84. override string GetDebugName()
  85. {
  86. string identifier;
  87. if (m_SoundSetName != "")
  88. {
  89. identifier = m_SoundSetName;
  90. }
  91. else
  92. {
  93. identifier = "NO_SOUNDSET";
  94. }
  95. return string.Format("%1:%2", super.GetDebugName(), identifier);
  96. }
  97. /** \name EffectType
  98. Information about what type of effect the Effect is, without the need for casting
  99. */
  100. //@{
  101. /**
  102. \brief Get what type of effect the Effect is
  103. \return \p EffectType What type of effect the Effect is
  104. */
  105. override EffectType GetEffectType()
  106. {
  107. return EffectType.SOUND;
  108. }
  109. /**
  110. \brief Check whether the Effect is EffectSound without casting
  111. \return \p bool Whether the Effect is EffectSound
  112. */
  113. override bool IsSound()
  114. {
  115. return true;
  116. }
  117. //@}
  118. /** \name Playback
  119. * Methods to Play/Stop sound
  120. * Generally, SEffectManager.PlaySound methods are used instead of SoundPlay
  121. */
  122. //@{
  123. /**
  124. \brief Plays sound
  125. \param params \p SoundParams Sound Parameters for the sound
  126. \return \p bool Whether the sound will start playing
  127. */
  128. bool SoundPlayEx(out SoundParams params)
  129. {
  130. super.Start();
  131. if (m_SoundSetName != "")
  132. {
  133. vector position = GetCurrentLocalPosition();
  134. if ( SoundLoadEx(params) )
  135. {
  136. if (m_SetEnvVariables && m_SoundParams)
  137. {
  138. m_SoundObjectBuilder.AddEnvSoundVariables(GetPosition());
  139. m_SoundObject = m_SoundObjectBuilder.BuildSoundObject();
  140. m_SoundObject.SetKind( m_SoundWaveKind );
  141. m_SoundObject.SetParent( m_ParentObject, m_PivotIndex );
  142. }
  143. if ( m_SoundObject )
  144. {
  145. SetCurrentLocalPosition(position, false);
  146. m_SoundWaveObject = GetGame().GetSoundScene().Play3D( m_SoundObject, m_SoundObjectBuilder );
  147. if ( !m_SoundWaveObject )
  148. return false;
  149. // Wait for header to be loaded before asking for its length, else we block the main thread
  150. if (m_SoundWaveObject.IsHeaderLoaded())
  151. ValidateSoundWave();
  152. else
  153. m_SoundWaveObject.GetEvents().Event_OnSoundWaveHeaderLoaded.Insert(ValidateSoundWave);
  154. return true;
  155. }
  156. else
  157. {
  158. SoundError("m_SoundObject is null.");
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. /**
  165. \brief Plays sound
  166. \return \p bool Whether the sound will start playing
  167. */
  168. bool SoundPlay()
  169. {
  170. SoundParams params;
  171. return SoundPlayEx(params);
  172. }
  173. /**
  174. \brief Plays sound
  175. */
  176. override void Start()
  177. {
  178. SoundPlay();
  179. }
  180. /**
  181. \brief Stops sound
  182. \note Will not be immediately if a SoundFadeOutDuration was set
  183. */
  184. void SoundStop()
  185. {
  186. super.Stop();
  187. if ( IsSoundPlaying() )
  188. {
  189. if ( m_SoundFadeOutDuration > 0 && !m_SoundWaveStopping )
  190. {
  191. m_SoundWaveStopping = true;
  192. m_SoundFadedOut = false;
  193. m_SoundWaveStarting = false;
  194. m_SoundFadeOutStartTime = m_SoundWaveTime;
  195. }
  196. else
  197. {
  198. m_SoundWaveObject.Stop();
  199. }
  200. }
  201. else if (!IsPendingDeletion()) // not about to be destroyed
  202. {
  203. SoundReset();
  204. }
  205. }
  206. /**
  207. \brief Stops sound
  208. */
  209. override void Stop()
  210. {
  211. SoundStop();
  212. }
  213. /**
  214. \brief Resets EffectSound
  215. */
  216. protected void SoundReset()
  217. {
  218. m_IsPlaying = false;
  219. m_SoundWaveIsPlaying = false;
  220. m_SoundWaveStopping = false;
  221. m_SoundFadedOut = false;
  222. m_SoundWaveVolume = m_SoundWaveVolumeMax;
  223. m_SoundWaveTime = 0;
  224. m_SoundFadeOutInitVolume = 0;
  225. m_SoundFadeOutStartTime = 0;
  226. if ( m_SoundWaveObject )
  227. {
  228. m_SoundWaveObject.Stop();
  229. m_SoundWaveObject.SetVolumeRelative( m_SoundWaveVolumeMax );
  230. }
  231. }
  232. /**
  233. \brief Get whether EffectSound is currently playing
  234. \return \p bool Whether EffectSound is currently playing
  235. */
  236. bool IsSoundPlaying()
  237. {
  238. return m_SoundWaveIsPlaying;
  239. }
  240. /**
  241. \brief Returns true when the effect is playing, false otherwise
  242. */
  243. override bool IsPlaying()
  244. {
  245. return IsSoundPlaying(); // Just in case, as that's what used to be the actual way to check
  246. }
  247. //@}
  248. /** \name Sound load
  249. Methods regarding the loading of the sound, used in SoundPlay
  250. */
  251. //@{
  252. /**
  253. \brief Loads in the sound when it is requested for playing through 'SoundPlayEx'
  254. \param params \p SoundParams Possibility of passing in an already existing SoundParams, else one will get created
  255. */
  256. bool SoundLoadEx(out SoundParams params)
  257. {
  258. if ( !m_SoundParams || !m_SoundParams.IsValid() )
  259. {
  260. if (!params)
  261. {
  262. params = new SoundParams( m_SoundSetName );
  263. }
  264. //Print("SoundLoad is loading..");
  265. m_SoundParams = params;
  266. if ( !m_SoundParams.IsValid() )
  267. {
  268. SoundError("Invalid sound set.");
  269. return false;
  270. }
  271. m_SoundObjectBuilder = new SoundObjectBuilder( m_SoundParams );
  272. if (m_SetEnvVariables)
  273. {
  274. m_SoundObjectBuilder.AddEnvSoundVariables(GetPosition());
  275. }
  276. m_SoundObject = m_SoundObjectBuilder.BuildSoundObject();
  277. if ( m_SoundObject )
  278. {
  279. m_SoundObject.SetKind( m_SoundWaveKind );
  280. m_SoundObject.SetParent( m_ParentObject, m_PivotIndex );
  281. }
  282. else
  283. {
  284. SoundError("m_SoundObject is null.");
  285. }
  286. }
  287. else
  288. {
  289. //Print("SoundLoad is loaded.");
  290. }
  291. return true;
  292. }
  293. /**
  294. \brief Loads in the sound when it is requested for playing
  295. \note Legacy, backwards compatibility
  296. */
  297. bool SoundLoad()
  298. {
  299. SoundParams params;
  300. return SoundLoadEx(params);
  301. }
  302. /**
  303. \brief Helper for checking if params are valid
  304. */
  305. bool IsSoundValid()
  306. {
  307. return m_SoundParams.IsValid();
  308. }
  309. /**
  310. \brief Gets called to fill in the necessary data when the header has finished loading
  311. \note Called from 'SoundPlayEx'
  312. */
  313. protected void ValidateSoundWave()
  314. {
  315. if (!m_SoundWaveObject)
  316. {
  317. ErrorEx(string.Format("%1 SoundWaveObject is null. SoundSet: %2", this.ToString(), m_SoundSetName));
  318. return;
  319. }
  320. m_SoundWaveLenght = m_SoundWaveObject.GetLength();
  321. if ( SoundWaveValidation() )
  322. {
  323. if ( m_SoundFadeInDuration > 0 )
  324. {
  325. m_SoundWaveObject.SetVolumeRelative( 0 );
  326. m_SoundFadeOutStartTime = m_SoundWaveLenght - m_SoundFadeInDuration;
  327. }
  328. SetSoundLoop( m_SoundLoop );
  329. m_SoundWaveStarting = true;
  330. AbstractWaveEvents events = m_SoundWaveObject.GetEvents();
  331. events.Event_OnSoundWaveStarted.Insert( Event_OnSoundWaveStarted );
  332. events.Event_OnSoundWaveEnded.Insert( Event_OnSoundWaveEnded );
  333. UpdateEvents();
  334. }
  335. else
  336. {
  337. m_SoundWaveObject.Stop();
  338. }
  339. }
  340. /**
  341. \brief Validation of fade settings
  342. \note Called from 'ValidateSoundWave'
  343. */
  344. protected bool SoundWaveValidation()
  345. {
  346. bool valid = true;
  347. if ( m_SoundFadeInDuration > GetSoundWaveLength() )
  348. {
  349. SoundError("FadeIn is longer than sound wave length.");
  350. valid = false;
  351. }
  352. if ( m_SoundFadeOutDuration > GetSoundWaveLength() )
  353. {
  354. SoundError("FadeOut is longer than sound wave length.");
  355. valid = false;
  356. }
  357. if ( m_SoundFadeOutDuration + m_SoundFadeInDuration > GetSoundWaveLength() )
  358. {
  359. SoundError("FadeIn & FadeOut are longer than sound wave length.");
  360. valid = false;
  361. }
  362. return valid;
  363. }
  364. /**
  365. \brief Enables the frame event on the EffectSound
  366. \note Called from 'ValidateSoundWave' when all is successful
  367. \note So this is effectively enabling frame event on all valid EffectSound
  368. */
  369. protected void UpdateEvents()
  370. {
  371. if ( m_SoundWaveObject )
  372. {
  373. SetEnableEventFrame(true);
  374. }
  375. else
  376. {
  377. SetEnableEventFrame(false);
  378. }
  379. }
  380. //@}
  381. /** \name Events
  382. Various events that can be overriden for custom behaviour
  383. */
  384. //@{
  385. /**
  386. \brief Event called on frame when enabled by SetEnableEventFrame(true)
  387. \note Is always enabled on sound
  388. \note Called from SEffectManager.Event_OnFrameUpdate in MissionGameplay.OnUpdate
  389. \param time_delta \p float Time passed since the previous frame
  390. */
  391. override void Event_OnFrameUpdate(float time_delta)
  392. {
  393. if ( IsSoundPlaying() )
  394. {
  395. if (m_SoundDoppler != -1)
  396. {
  397. m_SoundWaveObject.SetDoppler(m_SoundDoppler);
  398. }
  399. // FadeIn
  400. if ( m_SoundWaveStarting )
  401. {
  402. if ( m_SoundFadeInDuration > 0 )
  403. {
  404. SetSoundVolume( GetSoundVolume() + (time_delta / m_SoundFadeInDuration) );
  405. if ( GetSoundVolume() >= m_SoundWaveVolumeMax )
  406. {
  407. Event_OnSoundFadeInStopped();
  408. SetSoundVolume( m_SoundWaveVolumeMax );
  409. m_SoundWaveStarting = false;
  410. }
  411. }
  412. else
  413. {
  414. SetSoundVolume( m_SoundWaveVolumeMax );
  415. m_SoundWaveStarting = false;
  416. }
  417. }
  418. // FadeOut
  419. if ( m_SoundWaveStopping )
  420. {
  421. if ( m_SoundFadeOutDuration > 0 )
  422. {
  423. if ( m_SoundFadeOutInitVolume == 0 )
  424. {
  425. m_SoundFadeOutInitVolume = GetSoundVolume();
  426. Event_OnSoundFadeOutStarted();
  427. }
  428. SetSoundVolume( GetSoundVolume() - (m_SoundFadeOutInitVolume / m_SoundFadeOutDuration * time_delta) );
  429. }
  430. else
  431. {
  432. SetSoundVolume( 0 );
  433. }
  434. if ( GetSoundVolume() <= 0 )
  435. {
  436. if ( m_SoundWaveObject )
  437. {
  438. m_SoundWaveObject.Stop();
  439. m_SoundWaveStopping = false;
  440. m_SoundFadedOut = true;
  441. }
  442. }
  443. }
  444. // Counting timer here because loop play
  445. m_SoundWaveTime += time_delta;
  446. }
  447. }
  448. /**
  449. \brief Event called from SEffectManager when the Effect is registered
  450. \note Should only ever be called by SEffectManager!
  451. \param id \p int ID registered in SEffectManager
  452. */
  453. override void Event_OnRegistered(int id)
  454. {
  455. super.Event_OnRegistered(id);
  456. Event_OnSoundWaveEnded.Insert( SEffectManager.Event_OnSoundWaveEnded );
  457. }
  458. /**
  459. \brief Event called from SEffectManager when the Effect is unregistered
  460. \note Should only ever be called by SEffectManager!
  461. */
  462. override void Event_OnUnregistered()
  463. {
  464. super.Event_OnUnregistered();
  465. Event_OnSoundWaveEnded.Remove( SEffectManager.Event_OnSoundWaveEnded );
  466. }
  467. /**
  468. \brief Event called when sound starts playing
  469. \note Inserted into events of m_SoundWaveObject
  470. */
  471. void Event_OnSoundWaveStarted()
  472. {
  473. m_SoundWaveIsPlaying = true;
  474. Event_OnSoundWaveStarted.Invoke(this);
  475. Event_OnEffectStarted();
  476. }
  477. /**
  478. \brief Event called when sound stops playing
  479. \note Inserted into events of m_SoundWaveObject
  480. */
  481. void Event_OnSoundWaveEnded()
  482. {
  483. m_SoundWaveIsPlaying = false;
  484. Event_OnSoundWaveEnded.Invoke(this);
  485. Event_OnEffectEnded();
  486. }
  487. /**
  488. \brief Event called when sound fade in stops
  489. \note Called from Event_OnFrameUpdate
  490. */
  491. void Event_OnSoundFadeInStopped()
  492. {
  493. Event_OnSoundFadeInStopped.Invoke(this);
  494. }
  495. /**
  496. \brief Event called when sound fade out starts
  497. \note Called from Event_OnFrameUpdate
  498. */
  499. void Event_OnSoundFadeOutStarted()
  500. {
  501. Event_OnSoundFadeOutStarted.Invoke(this);
  502. }
  503. //@}
  504. /** \name AutoDestroy
  505. Methods regarding automatic cleanup on stop
  506. */
  507. //@{
  508. /**
  509. \brief Sets whether Effect automatically cleans up when it stops
  510. \note This means that it will be unregistered from SEffectManager as well
  511. \param auto_destroy \p bool Whether Effect automatically cleans up when it stops
  512. */
  513. override void SetAutodestroy(bool auto_destroy)
  514. {
  515. super.SetAutodestroy(auto_destroy);
  516. m_SoundAutodestroy = auto_destroy;
  517. }
  518. /**
  519. \brief Get whether Effect automatically cleans up when it stops
  520. \return \p bool Whether Effect automatically cleans up when it stops
  521. */
  522. override bool IsAutodestroy()
  523. {
  524. return IsSoundAutodestroy();
  525. }
  526. /**
  527. \brief Sets whether EffectSound automatically cleans up when sound stops
  528. \param auto_destroy \p bool Whether EffectSound automatically cleans up when sound stops
  529. */
  530. void SetSoundAutodestroy(bool auto_destroy)
  531. {
  532. SetAutodestroy(auto_destroy);
  533. }
  534. /**
  535. \brief Get whether EffectSound automatically cleans up when sound stops
  536. \return \p bool Whether EffectSound automatically cleans up when sound stops
  537. */
  538. bool IsSoundAutodestroy()
  539. {
  540. return m_SoundAutodestroy;
  541. }
  542. override bool CanDestroy()
  543. {
  544. return m_SoundFadeOutDuration <= 0 || !m_SoundWaveIsPlaying;
  545. }
  546. //@}
  547. /** \name Generic API
  548. Setters and getters for generic data and properties
  549. */
  550. //@{
  551. /**
  552. \brief Set parent for the sound to follow
  553. \param parent_obj \p Object The parent for the sound to follow
  554. */
  555. override void SetParent(Object parent_obj, int pivot)
  556. {
  557. super.SetParent(parent_obj, pivot); // ...
  558. if (m_SoundObject)
  559. {
  560. m_SoundObject.SetParent(parent_obj, pivot);
  561. }
  562. }
  563. /**
  564. \brief Get parent for the EffectSound
  565. \return \p Object The parent for the EffectSound
  566. */
  567. override Object GetParent()
  568. {
  569. if (m_SoundObject)
  570. return Object.Cast(m_SoundObject.GetParent());
  571. else
  572. return super.GetParent();
  573. }
  574. /**
  575. \brief Get parent pivot of the Effect, only valid when there is some GetParent
  576. \warning Only gets the cached variable
  577. \return \p int The parent pivot of the Effect
  578. */
  579. override int GetPivotIndex()
  580. {
  581. if (m_SoundObject)
  582. return m_SoundObject.GetHierarchyPivot();
  583. else
  584. return super.GetPivotIndex();
  585. }
  586. /**
  587. \brief Get parent for the EffectSound
  588. \note There is no real parenting with sound, so the setters and getters for parents do the exact same
  589. \return \p Object The parent for the EffectSound
  590. */
  591. override Object GetCurrentParent()
  592. {
  593. if (m_SoundObject)
  594. return Object.Cast(m_SoundObject.GetParent());
  595. else
  596. return super.GetParent(); // Yes, intentionally this one
  597. }
  598. /**
  599. \brief Set the world position of the managed sound
  600. \param pos \p vector The world position of the managed sound
  601. \param updateCached \p bool Whether to update the cached variable
  602. */
  603. override void SetCurrentPosition( vector pos, bool updateCached = true )
  604. {
  605. super.SetCurrentPosition(pos, updateCached);
  606. if (m_SoundObject)
  607. {
  608. Object parent = GetParent();
  609. if (parent)
  610. pos = parent.WorldToModel(pos);
  611. m_SoundObject.SetPosition(pos);
  612. }
  613. }
  614. /**
  615. \brief Get the current world position of the managed sound
  616. \return \p vector The current world position of the managed sound
  617. */
  618. override vector GetCurrentPosition()
  619. {
  620. if (m_SoundObject)
  621. return m_SoundObject.GetPosition();
  622. if (m_ParentObject)
  623. return m_ParentObject.ModelToWorld(GetPosition());
  624. return GetPosition();
  625. }
  626. /**
  627. \brief Set the current local position of the managed sound
  628. \param pos \p vector The current local position for the managed sound
  629. \param updateCached \p bool Whether to update the cached variable
  630. */
  631. override void SetCurrentLocalPosition( vector pos, bool updateCached = true )
  632. {
  633. super.SetCurrentLocalPosition(pos, updateCached);
  634. if (m_SoundObject)
  635. {
  636. m_SoundObject.SetPosition(pos);
  637. }
  638. }
  639. /**
  640. \brief Get the current local position of the managed sound
  641. \return \p vector The current local position of the managed sound
  642. */
  643. override vector GetCurrentLocalPosition()
  644. {
  645. Object parent = GetParent();
  646. if (m_SoundObject)
  647. {
  648. //TODO(kumarjac): Create and expose 'SoundObject.GetLocalPosition'
  649. if (parent)
  650. return parent.WorldToModel(m_SoundObject.GetPosition());
  651. else
  652. return m_SoundObject.GetPosition();
  653. }
  654. else
  655. {
  656. if (parent)
  657. return GetLocalPosition();
  658. else
  659. return GetPosition();
  660. }
  661. return vector.Zero;
  662. }
  663. /**
  664. \brief Set WaveKind for the sound
  665. \note Needs to be set before playing
  666. \param wave_kind \p WaveKind The WaveKind for the sound
  667. */
  668. void SetSoundWaveKind(WaveKind wave_kind)
  669. {
  670. m_SoundWaveKind = wave_kind;
  671. }
  672. /**
  673. \brief Set soundset for the sound
  674. \note Needs to be set before playing
  675. \param snd \p string Name of the soundset to play
  676. */
  677. void SetSoundSet(string snd)
  678. {
  679. m_SoundSetName = snd;
  680. }
  681. /**
  682. \brief Get soundset for the sound
  683. \return \p string Name of the soundset
  684. */
  685. string GetSoundSet()
  686. {
  687. return m_SoundSetName;
  688. }
  689. /**
  690. \brief Set if the sound loops
  691. \param loop \p bool Whether the sound should loop
  692. */
  693. void SetSoundLoop(bool loop)
  694. {
  695. m_SoundLoop = loop;
  696. if ( m_SoundWaveObject )
  697. m_SoundWaveObject.Loop( loop );
  698. }
  699. /**
  700. \brief Sets whether AddEnvSoundVariables needs to be called during Loading
  701. \param setEnvVariables \p bool Whether AddEnvSoundVariables is called
  702. */
  703. void SetEnviromentVariables(bool setEnvVariables)
  704. {
  705. m_SetEnvVariables = setEnvVariables;
  706. }
  707. /**
  708. \brief Get the sound wave length
  709. \note Legacy, exists for backwards compatibility
  710. \return \p float The sound wave length
  711. */
  712. float GetSoundWaveLenght()
  713. {
  714. return GetSoundWaveLength();
  715. }
  716. /**
  717. \brief Get the sound wave length
  718. \return \p float The sound wave length
  719. */
  720. float GetSoundWaveLength()
  721. {
  722. return m_SoundWaveLenght;
  723. }
  724. /**
  725. \brief Set the RELATIVE volume for the sound
  726. \param volume \p float The relative volume for the sound
  727. */
  728. void SetSoundVolume(float volume)
  729. {
  730. m_SoundWaveVolume = volume;
  731. if ( m_SoundWaveObject )
  732. m_SoundWaveObject.SetVolumeRelative( volume );
  733. }
  734. /**
  735. \brief Get the RELATIVE volume set by 'SetSoundVolume'
  736. \return \p float The relative volume for the sound set by 'SetSoundVolume'
  737. */
  738. float GetSoundVolume()
  739. {
  740. return m_SoundWaveVolume;
  741. }
  742. /**
  743. \brief Set the sound max volume
  744. \warning Seems to purely be used for fade in effect, rather than really setting the max volume...
  745. \warning Adjusts the current volume to this value as well
  746. \param volume \p float The maximum volume for the sound
  747. */
  748. void SetSoundMaxVolume(float volume)
  749. {
  750. m_SoundWaveVolumeMax = volume;
  751. if ( m_SoundWaveObject )
  752. m_SoundWaveObject.SetVolumeRelative( m_SoundWaveVolume );
  753. }
  754. /**
  755. \brief Get the time since EffectSound started playing
  756. \warning May not reflect the actual time of the sound, as it can start at negative time to simulate speed of sound
  757. \return \p float The time since EffectSound started playing
  758. */
  759. float GetSoundWaveTime()
  760. {
  761. return m_SoundWaveTime;
  762. }
  763. /**
  764. \brief Set the sound fade in duration
  765. \param fade_in \p float The fade in duration
  766. */
  767. void SetSoundFadeIn(float fade_in)
  768. {
  769. m_SoundFadeInDuration = fade_in;
  770. }
  771. /**
  772. \brief Set the sound fade out duration
  773. \param fade_out \p float The fade out duration
  774. */
  775. void SetSoundFadeOut(float fade_out)
  776. {
  777. m_SoundFadeOutDuration = fade_out;
  778. }
  779. /**
  780. \brief Set if the sound has the doppler effect enabled
  781. \param setDoppler \p float If the doppler effect is enabled
  782. */
  783. void SetDoppler(bool setDoppler)
  784. {
  785. //! bool is a fancy int, ensure the bool is 0 or 1 and don't allow -1 here since resetting isn't supported
  786. m_SoundDoppler = 0;
  787. if (setDoppler)
  788. {
  789. m_SoundDoppler = 1;
  790. }
  791. }
  792. //@}
  793. /**
  794. \brief Helper for throwing sound errors
  795. */
  796. protected void SoundError(string err_msg)
  797. {
  798. ErrorEx(string.Format("%1: SoundSetName: '%2' :: %3", this, m_SoundSetName, err_msg));
  799. }
  800. }