SoundScene.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWAudio *
  23. * *
  24. * $Archive:: /Commando/Code/WWAudio/SoundScene.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/18/02 2:50p $*
  29. * *
  30. * $Revision:: 34 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "soundscene.h"
  36. #include "soundcullobj.h"
  37. #include "logicalsound.h"
  38. #include "logicallistener.h"
  39. #include "chunkio.h"
  40. #include "persistfactory.h"
  41. #include "wwprofile.h"
  42. #include "threads.h"
  43. #include "wwmemlog.h"
  44. #include "systimer.h"
  45. DEFINE_AUTO_POOL(SoundSceneClass::AudibleInfoClass, 64);
  46. //////////////////////////////////////////////////////////////////////////////////
  47. // Generic constants
  48. //////////////////////////////////////////////////////////////////////////////////
  49. const int MAX_LOGICAL_LISTENER_UPDATES_PER_FRAME = 4;
  50. //////////////////////////////////////////////////////////////////////////////////
  51. // Save/Load constants
  52. //////////////////////////////////////////////////////////////////////////////////
  53. enum
  54. {
  55. CHUNKID_VARIABLES = 0x00000100,
  56. CHUNKID_STATIC_SOUNDS,
  57. CHUNKID_DYNAMIC_SOUNDS
  58. };
  59. enum
  60. {
  61. VARID_MIN_DIM = 0x01,
  62. VARID_MAX_DIM,
  63. };
  64. ////////////////////////////////////////////////////////////////////////////////////////////////
  65. //
  66. // SoundSceneClass
  67. //
  68. ////////////////////////////////////////////////////////////////////////////////////////////////
  69. SoundSceneClass::SoundSceneClass (void)
  70. : m_Listener (NULL),
  71. m_2ndListener (NULL),
  72. m_MinExtents (-500, -500, -500),
  73. m_MaxExtents (500, 500, 500),
  74. m_IsBatchMode (false)
  75. {
  76. WWMEMLOG(MEM_SOUND);
  77. m_Listener = new Listener3DClass;
  78. m_DynamicCullingSystem.Re_Partition (m_MinExtents, m_MaxExtents, 100.00F);
  79. m_LogicalCullingSystem.Re_Partition (m_MinExtents, m_MaxExtents, 100.00F);
  80. m_ListenerCullingSystem.Re_Partition (m_MinExtents, m_MaxExtents, 40.00F);
  81. m_StaticCullingSystem.Re_Partition ();
  82. return ;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. // ~SoundSceneClass
  87. //
  88. ////////////////////////////////////////////////////////////////////////////////////////////////
  89. SoundSceneClass::~SoundSceneClass (void)
  90. {
  91. REF_PTR_RELEASE (m_Listener);
  92. REF_PTR_RELEASE (m_2ndListener);
  93. return ;
  94. }
  95. ////////////////////////////////////////////////////////////////////////////////////////////////
  96. //
  97. // Re_Partition
  98. //
  99. ////////////////////////////////////////////////////////////////////////////////////////////////
  100. void
  101. SoundSceneClass::Re_Partition
  102. (
  103. const Vector3 &min_dimension,
  104. const Vector3 &max_dimension
  105. )
  106. {
  107. m_DynamicCullingSystem.Re_Partition (min_dimension, max_dimension, 100.00F);
  108. m_LogicalCullingSystem.Re_Partition (min_dimension, max_dimension, 100.00F);
  109. m_ListenerCullingSystem.Re_Partition (min_dimension, max_dimension, 40.00F);
  110. m_StaticCullingSystem.Re_Partition ();
  111. m_MinExtents = min_dimension;
  112. m_MaxExtents = max_dimension;
  113. return ;
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////////////////////
  116. //
  117. // Collect_Logical_Sounds
  118. //
  119. ////////////////////////////////////////////////////////////////////////////////////////////////
  120. void
  121. SoundSceneClass::Collect_Logical_Sounds (int listener_count)
  122. {
  123. WWPROFILE ("Collect_Logical_Sounds");
  124. uint32 timestamp = TIMEGETTIME ();
  125. //
  126. // Determine how many listeners to process
  127. //
  128. int count = listener_count;
  129. int max = MAX_LOGICAL_LISTENER_UPDATES_PER_FRAME;
  130. if ((count < 0) || (count > max)) {
  131. count = max;
  132. }
  133. PriorityMultiListIterator<LogicalListenerClass> priority_queue (&m_LogicalListeners);
  134. LogicalListenerClass *listener = NULL;
  135. //
  136. // Loop over as many of the listeners as we want to process this
  137. // frame.
  138. //
  139. for ( int total = 0;
  140. total < count && priority_queue.Process_Head (&listener);
  141. total ++)
  142. {
  143. LogicalListenerClass::Set_Oldest_Timestamp (listener->Get_Timestamp ());
  144. listener->Set_Timestamp (LogicalListenerClass::Get_New_Timestamp ());
  145. listener->On_Frame_Update ();
  146. //
  147. // Collect a list of the sounds this listener can hear.
  148. //
  149. Vector3 position = listener->Get_Position ();
  150. m_LogicalCullingSystem.Reset_Collection ();
  151. m_LogicalCullingSystem.Collect_Objects (position);
  152. //
  153. // Now loop through the list of sounds this listener can hear
  154. // and notify their callback.
  155. //
  156. SoundCullObjClass * cull_obj;
  157. for ( cull_obj = m_LogicalCullingSystem.Get_First_Collected_Object();
  158. cull_obj != NULL;
  159. cull_obj = m_LogicalCullingSystem.Get_Next_Collected_Object (cull_obj))
  160. {
  161. //
  162. // Get a pointer to the current 'cull-sound' object.
  163. //
  164. LogicalSoundClass *sound_obj = (LogicalSoundClass *)cull_obj->Peek_Sound_Obj ();
  165. //
  166. // Test this sound against the scale associated with the current listener to
  167. // see if the listener can really "hear" the sound.
  168. //
  169. const Vector3 &sound_pos = cull_obj->Get_Bounding_Box ().Center;
  170. Vector3 listener_pos = listener->Get_Position ();
  171. float dropoff_radius = sound_obj->Get_DropOff_Radius ();
  172. float scale = listener->Get_Effective_Scale ();
  173. float test_radius2 = (dropoff_radius * scale) * (dropoff_radius * scale);
  174. if ((listener_pos - sound_pos).Length2 () <= test_radius2) {
  175. //
  176. // Is the sound ready to notify?
  177. //
  178. if (sound_obj->Allow_Notify (timestamp)) {
  179. listener->On_Event (AudioCallbackClass::EVENT_LOGICAL_HEARD, (uint32)listener, (uint32)sound_obj);
  180. }
  181. }
  182. }
  183. }
  184. //
  185. // Loop through and remove any single shot sounds that have
  186. // been completely processed
  187. //
  188. MultiListIterator<LogicalSoundClass> single_shot_it (&m_SingleShotLogicalSounds);
  189. for (single_shot_it.First (); !single_shot_it.Is_Done (); single_shot_it.Next ()) {
  190. LogicalSoundClass *sound_obj = single_shot_it.Peek_Obj ();
  191. //
  192. // Remove this sound if its been completely processed
  193. //
  194. if (sound_obj->Get_Listener_Timestamp () <= LogicalListenerClass::Get_Oldest_Timestamp ()) {
  195. sound_obj->Remove_From_Scene ();
  196. single_shot_it.Remove_Current_Object ();
  197. single_shot_it.Prev ();
  198. }
  199. }
  200. return ;
  201. }
  202. ////////////////////////////////////////////////////////////////////////////////////////////////
  203. //
  204. // Collect_Audible_Sounds
  205. //
  206. ////////////////////////////////////////////////////////////////////////////////////////////////
  207. void
  208. SoundSceneClass::Collect_Audible_Sounds
  209. (
  210. Listener3DClass * listener,
  211. COLLECTED_SOUNDS &list
  212. )
  213. {
  214. WWPROFILE ("Collect_Audible_Sounds");
  215. //
  216. // Collect a list of the audible dynamic sounds
  217. //
  218. Vector3 listener_pos = listener->Get_Position ();
  219. m_DynamicCullingSystem.Reset_Collection ();
  220. m_DynamicCullingSystem.Collect_Objects (listener_pos);
  221. //
  222. // Collect a list of the audible static sounds
  223. //
  224. m_StaticCullingSystem.Reset_Collection ();
  225. m_StaticCullingSystem.Collect_Objects (listener_pos);
  226. //
  227. // Loop through all the dynamic sounds that are currently audible and make sure
  228. // they are 'really' audible. The culling systems just check bounding boxes
  229. // but we need to be able to check attenuation spheres.
  230. //
  231. SoundCullObjClass * cull_obj = NULL;
  232. for ( cull_obj = m_DynamicCullingSystem.Get_First_Collected_Object();
  233. cull_obj != NULL;
  234. cull_obj = m_DynamicCullingSystem.Get_Next_Collected_Object(cull_obj))
  235. {
  236. // Get a pointer to the current 'cull-sound' object
  237. AudibleSoundClass *sound_obj = (AudibleSoundClass *)cull_obj->Peek_Sound_Obj ();
  238. // Perform a quick sphere-cull check to make sure this
  239. // sound should really be audible
  240. Vector3 pos = sound_obj->Get_Position ();
  241. float radius = sound_obj->Get_DropOff_Radius ();
  242. float radius2 = radius * radius;
  243. float length2 = (pos - listener_pos).Length2 ();
  244. if (length2 <= radius2) {
  245. AudibleInfoClass *audible_info = new AudibleInfoClass (sound_obj, length2);
  246. list.Add (audible_info);
  247. //
  248. // Update this sound's runtime priority based on its distance
  249. // from the sound emitter.
  250. //
  251. float length = (pos - listener_pos).Quick_Length ();
  252. float priority = (length > 0) ? 1 - (length / radius) : 1.0F;
  253. sound_obj->Set_Runtime_Priority (priority);
  254. }
  255. }
  256. //
  257. // Loop through all the static sounds that are currently audible and make sure
  258. // they are 'really' audible. The culling systems just check bounding boxes
  259. // but we need to be able to check attenuation spheres.
  260. //
  261. for ( cull_obj = m_StaticCullingSystem.Get_First_Collected_Object();
  262. cull_obj != NULL;
  263. cull_obj = m_StaticCullingSystem.Get_Next_Collected_Object(cull_obj))
  264. {
  265. AudibleSoundClass *sound_obj = (AudibleSoundClass *)cull_obj->Peek_Sound_Obj ();
  266. // Perform a quick sphere-cull check to make sure this
  267. // sound should really be audible
  268. Vector3 pos = sound_obj->Get_Position ();
  269. float radius = sound_obj->Get_DropOff_Radius ();
  270. float radius2 = radius * radius;
  271. float length2 = (pos - listener_pos).Length2 ();
  272. if (length2 <= radius2) {
  273. AudibleInfoClass *audible_info = new AudibleInfoClass (sound_obj, length2);
  274. list.Add (audible_info);
  275. //
  276. // Update this sound's runtime priority based on its distance
  277. // from the sound emitter.
  278. //
  279. float length = (pos - listener_pos).Quick_Length ();
  280. float priority = (length > 0) ? 1 - (length / radius) : 1.0F;
  281. sound_obj->Set_Runtime_Priority (priority);
  282. }
  283. }
  284. return ;
  285. }
  286. ////////////////////////////////////////////////////////////////////////////////////////////////
  287. //
  288. // On_Frame_Update
  289. //
  290. // Note: This method could be made more efficient by using another data structure besides
  291. // linked lists. However the differece may be negligable due to the low density of 3D sounds
  292. // that are audible at once.
  293. //
  294. ////////////////////////////////////////////////////////////////////////////////////////////////
  295. void
  296. SoundSceneClass::On_Frame_Update (unsigned int milliseconds)
  297. {
  298. WWPROFILE ("On_Frame_Update");
  299. COLLECTED_SOUNDS auxiliary_sounds;
  300. COLLECTED_SOUNDS primary_sounds;
  301. //
  302. // First, collect any auxiliary sounds that are audible
  303. //
  304. if (m_2ndListener != NULL) {
  305. m_2ndListener->On_Frame_Update (milliseconds);
  306. Collect_Audible_Sounds (m_2ndListener, auxiliary_sounds);
  307. }
  308. //
  309. // Update the listener's position/velocity, etc
  310. //
  311. m_Listener->On_Frame_Update (milliseconds);
  312. //
  313. // Collect the primary sounds that are audible
  314. //
  315. Collect_Audible_Sounds (m_Listener, primary_sounds);
  316. //
  317. // Loop through the auxiliary sounds and make sure
  318. // sounds are only played once, either primary or auxiliary.
  319. //
  320. MultiListIterator<AudibleInfoClass> aux_iterator (&auxiliary_sounds);
  321. MultiListIterator<AudibleInfoClass> pri_iterator (&primary_sounds);
  322. AUDIBLE_SOUND_LIST audible_sounds;
  323. for (aux_iterator.First (); !aux_iterator.Is_Done (); aux_iterator.Next ()) {
  324. AudibleInfoClass *aux_info = aux_iterator.Peek_Obj ();
  325. //
  326. // Loop through all the primary sounds and remove any
  327. // that are 'overpowered' by the same sound in the
  328. // other listener.
  329. //
  330. bool found = false;
  331. for (pri_iterator.First (); !pri_iterator.Is_Done () && !found; pri_iterator.Next ()) {
  332. AudibleInfoClass *pri_info = pri_iterator.Peek_Obj ();
  333. //
  334. // Is this sound in both lists?
  335. //
  336. found = (aux_info->sound_obj == pri_info->sound_obj);
  337. if (found) {
  338. if (aux_info->distance2 < pri_info->distance2) {
  339. delete pri_info;
  340. primary_sounds.Remove (pri_info);
  341. } else {
  342. delete aux_info;
  343. auxiliary_sounds.Remove (aux_info);
  344. }
  345. }
  346. }
  347. }
  348. //
  349. // Add the primary audible sounds into the master list
  350. //
  351. for (pri_iterator.First (); !pri_iterator.Is_Done (); pri_iterator.Next ()) {
  352. AudibleInfoClass *pri_info = pri_iterator.Peek_Obj ();
  353. audible_sounds.Add (pri_info->sound_obj);
  354. //
  355. // Let the sound know what it's listener's position is
  356. //
  357. pri_info->sound_obj->Set_Listener_Transform (m_Listener->Get_Transform ());
  358. //
  359. // Free the audible info object
  360. //
  361. pri_iterator.Remove_Current_Object ();
  362. pri_iterator.Prev ();
  363. delete pri_info;
  364. }
  365. //
  366. // Add the auxiliary audible sounds into the master list
  367. //
  368. for (aux_iterator.First (); !aux_iterator.Is_Done (); aux_iterator.Next ()) {
  369. AudibleInfoClass *aux_info = aux_iterator.Peek_Obj ();
  370. audible_sounds.Add (aux_info->sound_obj);
  371. //
  372. // Let the sound know what it's listener's position is
  373. //
  374. aux_info->sound_obj->Set_Listener_Transform (m_2ndListener->Get_Transform ());
  375. //
  376. // Convert the sound to a Pseudo-3D sound that has
  377. // a 'tinny' filter applied to it.
  378. //
  379. /*aux_info.sound_obj->Convert_To_Filtered ();
  380. AudibleSoundClass *tinny_sound = aux_info.sound_obj->As_Converted_Format ();
  381. if (tinny_sound != NULL) {
  382. audible_sounds.Add (tinny_sound);
  383. }*/
  384. //
  385. // Free the audible info object
  386. //
  387. aux_iterator.Remove_Current_Object ();
  388. aux_iterator.Prev ();
  389. delete aux_info;
  390. }
  391. //
  392. // Loop through all the sounds that were audible last frame
  393. // and see if they are still audible this frame.
  394. //
  395. MultiListIterator<AudibleSoundClass> audible_iterator (&m_LastSoundsAudible);
  396. for (audible_iterator.First (); !audible_iterator.Is_Done (); audible_iterator.Next ()) {
  397. AudibleSoundClass *sound_obj = audible_iterator.Peek_Obj ();
  398. //
  399. // Is this sound still audible?
  400. //
  401. if (audible_sounds.Is_In_List (sound_obj)) {
  402. //
  403. // Make sure the sound is playing, then remove it from
  404. // the newly-audible list so we don't process it again
  405. //
  406. sound_obj->Cull_Sound (false);
  407. audible_sounds.Remove (sound_obj);
  408. } else {
  409. //
  410. // If the sound isn't audible any more then remove
  411. // it from the list
  412. //
  413. audible_iterator.Remove_Current_Object ();
  414. audible_iterator.Prev ();
  415. //
  416. // Make sure we cull the sound
  417. //
  418. WWASSERT(sound_obj != NULL);
  419. sound_obj->Cull_Sound (true);
  420. sound_obj->Set_Runtime_Priority (0);
  421. }
  422. }
  423. //
  424. // Loop through all the newly-audible sounds and
  425. // make sure they are playing.
  426. //
  427. MultiListIterator<AudibleSoundClass> newly_audible_it (&audible_sounds);
  428. for (newly_audible_it.First (); !newly_audible_it.Is_Done (); newly_audible_it.Next ()) {
  429. AudibleSoundClass *sound_obj = newly_audible_it.Peek_Obj ();
  430. //
  431. // Make sure the sound has a valid Miles handle (so it can make noise)
  432. //
  433. sound_obj->Cull_Sound (false);
  434. //
  435. // If this sound is still in the scene (it may have 'stopped'
  436. // while it was culled) then start playing it...
  437. //
  438. if (sound_obj->Is_In_Scene ()) {
  439. m_LastSoundsAudible.Add (sound_obj);
  440. }
  441. }
  442. return ;
  443. }
  444. ////////////////////////////////////////////////////////////////////////////////////////////////
  445. //
  446. // Add_Sound
  447. //
  448. ////////////////////////////////////////////////////////////////////////////////////////////////
  449. void
  450. SoundSceneClass::Add_Sound
  451. (
  452. AudibleSoundClass * sound_obj,
  453. bool start_playing
  454. )
  455. {
  456. WWPROFILE ("Add_Sound");
  457. WWMEMLOG(MEM_SOUND);
  458. WWASSERT (sound_obj != NULL);
  459. if (sound_obj != NULL && sound_obj->Is_In_Scene () == false) {
  460. bool cull_sound = true;
  461. // Create a wrapper object for the sound that we can use
  462. // with the different culling systems.
  463. SoundCullObjClass *cullable_sound = new SoundCullObjClass;
  464. cullable_sound->Set_Sound_Obj (sound_obj);
  465. sound_obj->Set_Cullable_Wrapper (cullable_sound);
  466. //
  467. // Add this object to the dynamic culling system
  468. //
  469. m_DynamicCullingSystem.Add_Object (cullable_sound);
  470. m_DynamicSounds.Add (cullable_sound);
  471. Update_Sound (cullable_sound);
  472. //
  473. // If the listener can hear this sound, then make sure
  474. // we start it off non-culled
  475. //
  476. if (m_IsBatchMode == false) {
  477. Vector3 listener_pos = m_Listener->Get_Position ();
  478. Vector3 sound_pos = sound_obj->Get_Position ();
  479. float radius = sound_obj->Get_DropOff_Radius ();
  480. float radius2 = radius * radius;
  481. if (((listener_pos - sound_pos).Length2 ()) < radius2) {
  482. cull_sound = false;
  483. m_LastSoundsAudible.Add (sound_obj);
  484. start_playing = true;
  485. sound_obj->Set_Listener_Transform (m_Listener->Get_Transform ());
  486. }
  487. }
  488. //
  489. // Make sure the sound is appropriately culled
  490. //
  491. sound_obj->Cull_Sound (cull_sound);
  492. //
  493. // Start the sound playing if requested
  494. //
  495. if (start_playing) {
  496. sound_obj->Play ();
  497. }
  498. }
  499. return ;
  500. }
  501. ////////////////////////////////////////////////////////////////////////////////////////////////
  502. //
  503. // Remove_Sound
  504. //
  505. // Note: This method should really be rewritten to use a different list type. Linked lists
  506. // are probably too inefficient (especially if we have 100s or 1000s of sounds)
  507. //
  508. ////////////////////////////////////////////////////////////////////////////////////////////////
  509. void
  510. SoundSceneClass::Remove_Sound
  511. (
  512. AudibleSoundClass *sound_obj,
  513. bool stop_playing
  514. )
  515. {
  516. WWPROFILE ("Remove_Sound");
  517. if (sound_obj == NULL) {
  518. return ;
  519. }
  520. //
  521. // Make sure we remove this sound from the list of last audible sounds.
  522. //
  523. if (m_LastSoundsAudible.Is_In_List (sound_obj)) {
  524. m_LastSoundsAudible.Remove (sound_obj);
  525. }
  526. //
  527. // Is this sound really in the scene?
  528. //
  529. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  530. if (cull_obj != NULL && m_DynamicSounds.Is_In_List (cull_obj)) {
  531. //
  532. // Stop playing the sound if necessary
  533. //
  534. if (stop_playing) {
  535. sound_obj->Stop ();
  536. }
  537. //
  538. // Flush the sound's cull-wrapper since we are removing it from the scene
  539. //
  540. sound_obj->Set_Cullable_Wrapper (NULL);
  541. //
  542. // Remove this sound from the dynamic culling system
  543. //
  544. m_DynamicCullingSystem.Remove_Object (cull_obj);
  545. m_DynamicSounds.Remove (cull_obj);
  546. //
  547. // Register the sound for deletion at an appropriate time
  548. //
  549. WWAudioThreadsClass::Add_Delayed_Release_Object (cull_obj);
  550. }
  551. return ;
  552. }
  553. ////////////////////////////////////////////////////////////////////////////////////////////////
  554. //
  555. // Add_Static_Sound
  556. //
  557. ////////////////////////////////////////////////////////////////////////////////////////////////
  558. void
  559. SoundSceneClass::Add_Static_Sound
  560. (
  561. AudibleSoundClass * sound_obj,
  562. bool start_playing
  563. )
  564. {
  565. WWPROFILE ("Add_Static_Sound");
  566. WWASSERT (sound_obj != NULL);
  567. if (sound_obj != NULL) {
  568. //
  569. // Check to see if this sound is already in the scene
  570. //
  571. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  572. if (cull_obj == NULL) {
  573. //
  574. // Create a wrapper object for the sound that we can use
  575. // with the different culling systems.
  576. //
  577. cull_obj = new SoundCullObjClass;
  578. cull_obj->Set_Sound_Obj (sound_obj);
  579. sound_obj->Set_Cullable_Wrapper (cull_obj);
  580. //
  581. // Add this object to the static culling system
  582. //
  583. m_StaticCullingSystem.Add_Object (cull_obj);
  584. m_StaticSounds.Add (cull_obj);
  585. Update_Sound (cull_obj);
  586. //
  587. // If the listener can hear this sound, then make sure
  588. // we start it off non-culled
  589. //
  590. bool cull_sound = true;
  591. if (m_IsBatchMode == false) {
  592. Vector3 listener_pos = m_Listener->Get_Position ();
  593. Vector3 sound_pos = sound_obj->Get_Position ();
  594. float radius = sound_obj->Get_DropOff_Radius ();
  595. float radius2 = radius * radius;
  596. if (((listener_pos - sound_pos).Length2 ()) < radius2) {
  597. cull_sound = false;
  598. m_LastSoundsAudible.Add (sound_obj);
  599. start_playing = true;
  600. sound_obj->Set_Listener_Transform (m_Listener->Get_Transform ());
  601. }
  602. }
  603. //
  604. // Make sure the sound is appropriately culled
  605. //
  606. sound_obj->Cull_Sound (cull_sound);
  607. //
  608. // Start the sound playing if requested
  609. //
  610. if (start_playing) {
  611. sound_obj->Play ();
  612. }
  613. //
  614. // Add a ref to the static sound object
  615. //
  616. //sound_obj->Add_Ref ();
  617. }
  618. }
  619. return ;
  620. }
  621. ////////////////////////////////////////////////////////////////////////////////////////////////
  622. //
  623. // Remove_Static_Sound
  624. //
  625. ////////////////////////////////////////////////////////////////////////////////////////////////
  626. void
  627. SoundSceneClass::Remove_Static_Sound
  628. (
  629. AudibleSoundClass * sound_obj,
  630. bool stop_playing
  631. )
  632. {
  633. WWPROFILE ("Remove_Static_Sound");
  634. if (sound_obj == NULL) {
  635. return ;
  636. }
  637. //
  638. // Make sure we remove this sound from the list of last audible sounds.
  639. //
  640. if (m_LastSoundsAudible.Is_In_List (sound_obj)) {
  641. m_LastSoundsAudible.Remove (sound_obj);
  642. }
  643. //
  644. // Is this sound really in the scene?
  645. //
  646. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  647. if (cull_obj != NULL && m_StaticSounds.Is_In_List (cull_obj)) {
  648. //
  649. // Stop playing the sound if necessary
  650. //
  651. if (stop_playing) {
  652. sound_obj->Stop ();
  653. }
  654. //
  655. // Flush the sound's cull-wrapper since we are removing it from the scene
  656. //
  657. sound_obj->Set_Cullable_Wrapper (NULL);
  658. //
  659. // Remove this sound from the static culling system
  660. //
  661. m_StaticCullingSystem.Remove_Object (cull_obj);
  662. m_StaticSounds.Remove (cull_obj);
  663. //
  664. // Register the sound for deletion at an appropriate time
  665. //
  666. WWAudioThreadsClass::Add_Delayed_Release_Object (cull_obj);
  667. }
  668. return ;
  669. }
  670. ////////////////////////////////////////////////////////////////////////////////////////////////
  671. //
  672. // Add_Logical_Sound
  673. //
  674. ////////////////////////////////////////////////////////////////////////////////////////////////
  675. void
  676. SoundSceneClass::Add_Logical_Sound
  677. (
  678. LogicalSoundClass * sound_obj,
  679. bool single_shot
  680. )
  681. {
  682. WWPROFILE ("Add_Logical_Sound");
  683. WWASSERT (sound_obj != NULL);
  684. if (sound_obj != NULL) {
  685. //
  686. // Check to make sure we don't add this sound twice
  687. //
  688. if (Is_Logical_Sound_In_Scene (sound_obj, single_shot) == false) {
  689. sound_obj->Set_Listener_Timestamp (LogicalListenerClass::Get_Newest_Timestamp ());
  690. //
  691. // Create a wrapper object for the sound that we can use
  692. // with the different culling systems.
  693. //
  694. SoundCullObjClass *cullable_sound = new SoundCullObjClass;
  695. cullable_sound->Set_Sound_Obj (sound_obj);
  696. sound_obj->Set_Cullable_Wrapper (cullable_sound);
  697. //
  698. // Add this object to the logical culling system
  699. //
  700. m_LogicalCullingSystem.Add_Object (cullable_sound);
  701. //
  702. // Add this sound to our current sounds list
  703. //
  704. if (single_shot) {
  705. m_SingleShotLogicalSounds.Add (sound_obj);
  706. } else {
  707. m_LogicalSounds.Add (sound_obj);
  708. }
  709. //
  710. // Keep a reference on this sound object
  711. //
  712. sound_obj->Add_Ref ();
  713. //
  714. // Make sure the cull-object has the most up-to-date information
  715. // about this sound object's bounding volume
  716. //
  717. Update_Sound (cullable_sound);
  718. }
  719. }
  720. return ;
  721. }
  722. ////////////////////////////////////////////////////////////////////////////////////////////////
  723. //
  724. // Remove_Logical_Sound
  725. //
  726. ////////////////////////////////////////////////////////////////////////////////////////////////
  727. void
  728. SoundSceneClass::Remove_Logical_Sound
  729. (
  730. LogicalSoundClass * sound_obj,
  731. bool single_shot,
  732. bool remove_from_list
  733. )
  734. {
  735. WWPROFILE ("Remove_Logical_Sound");
  736. if (sound_obj == NULL) {
  737. return ;
  738. }
  739. if (single_shot) {
  740. //
  741. // Only do this if the logical sound is really in our list
  742. //
  743. if (m_SingleShotLogicalSounds.Is_In_List (sound_obj)) {
  744. //
  745. // Remove this sound from logical sound list
  746. //
  747. if (remove_from_list) {
  748. m_SingleShotLogicalSounds.Remove (sound_obj);
  749. }
  750. //
  751. // Remove this sound from the culling system
  752. //
  753. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  754. m_LogicalCullingSystem.Remove_Object (cull_obj);
  755. //
  756. // Remove this sound obj's wrapper
  757. //
  758. sound_obj->Set_Cullable_Wrapper (NULL);
  759. WWAudioThreadsClass::Add_Delayed_Release_Object (cull_obj);
  760. //
  761. // Release our reference on this object
  762. //
  763. REF_PTR_RELEASE (sound_obj);
  764. }
  765. } else {
  766. //
  767. // Only do this if the logical sound is really in our list
  768. //
  769. if (m_LogicalSounds.Is_In_List (sound_obj)) {
  770. //
  771. // Remove this sound from logical sound list
  772. //
  773. if (remove_from_list) {
  774. m_LogicalSounds.Remove (sound_obj);
  775. }
  776. //
  777. // Remove this sound from the culling system
  778. //
  779. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  780. m_LogicalCullingSystem.Remove_Object (cull_obj);
  781. //
  782. // Remove this sound obj's wrapper
  783. //
  784. sound_obj->Set_Cullable_Wrapper (NULL);
  785. WWAudioThreadsClass::Add_Delayed_Release_Object (cull_obj);
  786. //
  787. // Release our reference on this object
  788. //
  789. REF_PTR_RELEASE (sound_obj);
  790. }
  791. }
  792. return ;
  793. }
  794. ////////////////////////////////////////////////////////////////////////////////////////////////
  795. //
  796. // Add_Logical_Listener
  797. //
  798. ////////////////////////////////////////////////////////////////////////////////////////////////
  799. void
  800. SoundSceneClass::Add_Logical_Listener (LogicalListenerClass *listener_obj)
  801. {
  802. WWPROFILE ("Add_Logical_Listener");
  803. WWASSERT (listener_obj != NULL);
  804. if (listener_obj != NULL) {
  805. //
  806. // Add the listener to the 'scene' if its in our list
  807. //
  808. if (m_LogicalListeners.Is_In_List (listener_obj) == false) {
  809. listener_obj->Set_Timestamp (LogicalListenerClass::Get_New_Timestamp ());
  810. m_LogicalListeners.Add_Tail (listener_obj);
  811. listener_obj->Add_Ref ();
  812. }
  813. }
  814. return ;
  815. }
  816. ////////////////////////////////////////////////////////////////////////////////////////////////
  817. //
  818. // Remove_Logical_Listener
  819. //
  820. ////////////////////////////////////////////////////////////////////////////////////////////////
  821. void
  822. SoundSceneClass::Remove_Logical_Listener (LogicalListenerClass *listener_obj)
  823. {
  824. WWPROFILE ("Remove_Logical_Listener");
  825. WWASSERT (listener_obj != NULL);
  826. if (listener_obj != NULL) {
  827. //
  828. // Remove the listener from the 'scene' if its in our list
  829. //
  830. if (m_LogicalListeners.Is_In_List (listener_obj)) {
  831. m_LogicalListeners.Remove (listener_obj);
  832. listener_obj->Release_Ref ();
  833. }
  834. }
  835. return ;
  836. }
  837. ////////////////////////////////////////////////////////////////////////////////////////////////
  838. //
  839. // Update_Sound
  840. //
  841. ////////////////////////////////////////////////////////////////////////////////////////////////
  842. void
  843. SoundSceneClass::Update_Sound (SoundCullObjClass *sound_obj)
  844. {
  845. if (sound_obj != NULL) {
  846. sound_obj->Set_Cull_Box(sound_obj->Get_Bounding_Box());
  847. }
  848. return ;
  849. }
  850. ////////////////////////////////////////////////////////////////////////////////////////////////
  851. //
  852. // Initialize
  853. //
  854. ////////////////////////////////////////////////////////////////////////////////////////////////
  855. void
  856. SoundSceneClass::Initialize (void)
  857. {
  858. m_Listener->Free_Miles_Handle ();
  859. m_Listener->Allocate_Miles_Handle ();
  860. return ;
  861. }
  862. ////////////////////////////////////////////////////////////////////////////////////////////////
  863. //
  864. // Is_Sound_In_Scene
  865. //
  866. ////////////////////////////////////////////////////////////////////////////////////////////////
  867. bool
  868. SoundSceneClass::Is_Sound_In_Scene (AudibleSoundClass *sound_obj, bool all)
  869. {
  870. bool retval = false;
  871. //
  872. // Try to find this sound's cull-object in either the static or dynamic
  873. // lists.
  874. //
  875. SoundCullObjClass *cull_obj = sound_obj->Peek_Cullable_Wrapper ();
  876. if (cull_obj != NULL) {
  877. retval = (m_DynamicSounds.Is_In_List (cull_obj) || m_StaticSounds.Is_In_List (cull_obj));
  878. }
  879. return retval;
  880. }
  881. ////////////////////////////////////////////////////////////////////////////////////////////////
  882. //
  883. // Is_Logical_Sound_In_Scene
  884. //
  885. ////////////////////////////////////////////////////////////////////////////////////////////////
  886. bool
  887. SoundSceneClass::Is_Logical_Sound_In_Scene
  888. (
  889. LogicalSoundClass * sound_obj,
  890. bool single_shot
  891. )
  892. {
  893. bool retval = false;
  894. //
  895. // Check to see if this sound is in either the continuous list or the single shot list.
  896. //
  897. if (single_shot == false) {
  898. retval = m_LogicalSounds.Is_In_List (sound_obj);
  899. } else {
  900. retval = m_SingleShotLogicalSounds.Is_In_List (sound_obj);
  901. }
  902. return retval;
  903. }
  904. ////////////////////////////////////////////////////////////////////////////////////////////////
  905. //
  906. // Save_Static
  907. //
  908. ////////////////////////////////////////////////////////////////////////////////////////////////
  909. bool
  910. SoundSceneClass::Save_Static (ChunkSaveClass &csave)
  911. {
  912. csave.Begin_Chunk (CHUNKID_VARIABLES);
  913. WRITE_MICRO_CHUNK (csave, VARID_MIN_DIM, m_MinExtents);
  914. WRITE_MICRO_CHUNK (csave, VARID_MAX_DIM, m_MaxExtents);
  915. csave.End_Chunk ();
  916. //
  917. // Save the list of static sounds that are currently in the scene
  918. //
  919. csave.Begin_Chunk (CHUNKID_STATIC_SOUNDS);
  920. Save_Static_Sounds (csave);
  921. csave.End_Chunk ();
  922. return true;
  923. }
  924. ////////////////////////////////////////////////////////////////////////////////////////////////
  925. //
  926. // Save_Static_Sounds
  927. //
  928. ////////////////////////////////////////////////////////////////////////////////////////////////
  929. void
  930. SoundSceneClass::Save_Static_Sounds (ChunkSaveClass &csave)
  931. {
  932. Re_Partition (m_MinExtents, m_MaxExtents);
  933. //
  934. // Loop over all the static sounds that are in the scene
  935. // and save each to its own chunk.
  936. //
  937. MultiListIterator<SoundCullObjClass> static_iterator (&m_StaticSounds);
  938. for (static_iterator.First (); !static_iterator.Is_Done (); static_iterator.Next ()) {
  939. SoundCullObjClass *cull_obj = static_iterator.Peek_Obj ();
  940. //
  941. // Get the sound from its cull object
  942. //
  943. AudibleSoundClass *sound_obj = (AudibleSoundClass *)cull_obj->Peek_Sound_Obj ();
  944. if (sound_obj != NULL) {
  945. //
  946. // Have the sound's factory save it
  947. //
  948. csave.Begin_Chunk (sound_obj->Get_Factory ().Chunk_ID ());
  949. sound_obj->Get_Factory ().Save (csave, sound_obj);
  950. csave.End_Chunk ();
  951. }
  952. }
  953. return ;
  954. }
  955. ////////////////////////////////////////////////////////////////////////////////////////////////
  956. //
  957. // Load_Static_Sounds
  958. //
  959. ////////////////////////////////////////////////////////////////////////////////////////////////
  960. void
  961. SoundSceneClass::Load_Static_Sounds (ChunkLoadClass &cload)
  962. {
  963. while (cload.Open_Chunk ()) {
  964. //
  965. // Load this sound from the chunk (if possible)
  966. //
  967. PersistFactoryClass *factory = SaveLoadSystemClass::Find_Persist_Factory (cload.Cur_Chunk_ID ());
  968. if (factory != NULL) {
  969. AudibleSoundClass *sound_obj = (AudibleSoundClass *)factory->Load (cload);
  970. if (sound_obj != NULL) {
  971. sound_obj->Add_To_Scene (true);
  972. REF_PTR_RELEASE (sound_obj);
  973. }
  974. }
  975. cload.Close_Chunk ();
  976. }
  977. return ;
  978. }
  979. ////////////////////////////////////////////////////////////////////////////////////////////////
  980. //
  981. // Load_Static
  982. //
  983. ////////////////////////////////////////////////////////////////////////////////////////////////
  984. bool
  985. SoundSceneClass::Load_Static (ChunkLoadClass &cload)
  986. {
  987. m_IsBatchMode = true;
  988. while (cload.Open_Chunk ()) {
  989. switch (cload.Cur_Chunk_ID ()) {
  990. case CHUNKID_STATIC_SOUNDS:
  991. Load_Static_Sounds (cload);
  992. break;
  993. case CHUNKID_VARIABLES:
  994. {
  995. //
  996. // Read all the variables from their micro-chunks
  997. //
  998. while (cload.Open_Micro_Chunk ()) {
  999. switch (cload.Cur_Micro_Chunk_ID ()) {
  1000. READ_MICRO_CHUNK (cload, VARID_MIN_DIM, m_MinExtents);
  1001. READ_MICRO_CHUNK (cload, VARID_MAX_DIM, m_MaxExtents);
  1002. }
  1003. cload.Close_Micro_Chunk ();
  1004. }
  1005. }
  1006. break;
  1007. }
  1008. cload.Close_Chunk ();
  1009. }
  1010. Re_Partition (m_MinExtents, m_MaxExtents);
  1011. On_Frame_Update (0);
  1012. m_IsBatchMode = false;
  1013. return true;
  1014. }
  1015. ////////////////////////////////////////////////////////////////////////////////////////////////
  1016. //
  1017. // Save_Dynamic
  1018. //
  1019. ////////////////////////////////////////////////////////////////////////////////////////////////
  1020. bool
  1021. SoundSceneClass::Save_Dynamic (ChunkSaveClass &csave)
  1022. {
  1023. return true;
  1024. }
  1025. ////////////////////////////////////////////////////////////////////////////////////////////////
  1026. //
  1027. // Load_Dynamic
  1028. //
  1029. ////////////////////////////////////////////////////////////////////////////////////////////////
  1030. bool
  1031. SoundSceneClass::Load_Dynamic (ChunkLoadClass &cload)
  1032. {
  1033. return true;
  1034. }
  1035. ////////////////////////////////////////////////////////////////////////////////////////////////
  1036. //
  1037. // Flush_Scene
  1038. //
  1039. ////////////////////////////////////////////////////////////////////////////////////////////////
  1040. void
  1041. SoundSceneClass::Flush_Scene (void)
  1042. {
  1043. RefMultiListClass<SoundCullObjClass> temp_static;
  1044. RefMultiListClass<SoundCullObjClass> temp_dynamic;
  1045. RefMultiListClass<LogicalSoundClass> temp_logical;
  1046. RefMultiListClass<LogicalSoundClass> temp_single_logical;
  1047. //
  1048. // Build temporary lists of the static and dynamic sounds
  1049. //
  1050. MultiListIterator<SoundCullObjClass> static_iterator (&m_StaticSounds);
  1051. for (static_iterator.First (); !static_iterator.Is_Done (); static_iterator.Next ()) {
  1052. SoundCullObjClass *cull_obj = static_iterator.Peek_Obj ();
  1053. temp_static.Add (cull_obj);
  1054. }
  1055. MultiListIterator<SoundCullObjClass> dynamic_iterator (&m_DynamicSounds);
  1056. for (dynamic_iterator.First (); !dynamic_iterator.Is_Done (); dynamic_iterator.Next ()) {
  1057. SoundCullObjClass *cull_obj = dynamic_iterator.Peek_Obj ();
  1058. temp_dynamic.Add (cull_obj);
  1059. }
  1060. MultiListIterator<LogicalSoundClass> logical_iterator (&m_LogicalSounds);
  1061. for (logical_iterator.First (); !logical_iterator.Is_Done (); logical_iterator.Next ()) {
  1062. LogicalSoundClass *logical_sound = logical_iterator.Peek_Obj ();
  1063. temp_logical.Add (logical_sound);
  1064. }
  1065. MultiListIterator<LogicalSoundClass> temp_single_iterator (&m_SingleShotLogicalSounds);
  1066. for (temp_single_iterator.First (); !temp_single_iterator.Is_Done (); temp_single_iterator.Next ()) {
  1067. LogicalSoundClass *logical_sound = temp_single_iterator.Peek_Obj ();
  1068. temp_single_logical.Add (logical_sound);
  1069. }
  1070. //
  1071. // Remove all the static sounds from the scene
  1072. //
  1073. RefMultiListIterator<SoundCullObjClass> temp_static_it (&temp_static);
  1074. for (temp_static_it.First (); !temp_static_it.Is_Done (); temp_static_it.Next ()) {
  1075. SoundCullObjClass *cull_obj = temp_static_it.Peek_Obj ();
  1076. AudibleSoundClass *sound_obj = (AudibleSoundClass *)cull_obj->Peek_Sound_Obj ();
  1077. if (sound_obj != NULL) {
  1078. Remove_Static_Sound (sound_obj);
  1079. }
  1080. }
  1081. //
  1082. // Remove all the dynamic sounds from the scene
  1083. //
  1084. RefMultiListIterator<SoundCullObjClass> temp_dynamic_it (&temp_dynamic);
  1085. for (temp_dynamic_it.First (); !temp_dynamic_it.Is_Done (); temp_dynamic_it.Next ()) {
  1086. SoundCullObjClass *cull_obj = temp_dynamic_it.Peek_Obj ();
  1087. AudibleSoundClass *sound_obj = (AudibleSoundClass *)cull_obj->Peek_Sound_Obj ();
  1088. if (sound_obj != NULL) {
  1089. Remove_Sound (sound_obj);
  1090. }
  1091. }
  1092. //
  1093. // Remove all the logical sounds from the scene
  1094. //
  1095. RefMultiListIterator<LogicalSoundClass> temp_logical_it (&temp_logical);
  1096. for (temp_logical_it.First (); !temp_logical_it.Is_Done (); temp_logical_it.Next ()) {
  1097. LogicalSoundClass *logical_sound = temp_logical_it.Peek_Obj ();
  1098. if (logical_sound != NULL) {
  1099. logical_sound->Remove_From_Scene ();
  1100. }
  1101. }
  1102. //
  1103. // Remove all the single shot logical sounds from the scene
  1104. //
  1105. RefMultiListIterator<LogicalSoundClass> temp_single_logical_it (&temp_single_logical);
  1106. for (temp_single_logical_it.First (); !temp_single_logical_it.Is_Done (); temp_single_logical_it.Next ()) {
  1107. LogicalSoundClass *logical_sound = temp_single_logical_it.Peek_Obj ();
  1108. if (logical_sound != NULL) {
  1109. logical_sound->Remove_From_Scene ();
  1110. }
  1111. }
  1112. return ;
  1113. }
  1114. ////////////////////////////////////////////////////////////////////////////////////////////////
  1115. //
  1116. // Set_2nd_Listener
  1117. //
  1118. ////////////////////////////////////////////////////////////////////////////////////////////////
  1119. void
  1120. SoundSceneClass::Set_2nd_Listener (Listener3DClass *listener)
  1121. {
  1122. if (m_2ndListener != NULL) {
  1123. m_2ndListener->On_Removed_From_Scene ();
  1124. }
  1125. if (listener != NULL) {
  1126. listener->On_Added_To_Scene ();
  1127. }
  1128. REF_PTR_SET (m_2ndListener, listener);
  1129. return ;
  1130. }