SoundPseudo3D.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. ** Command & Conquer Generals(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/SoundPseudo3D.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/13/01 12:09p $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "soundpseudo3d.h"
  36. #include "wwaudio.h"
  37. #include "soundscene.h"
  38. #include "utils.h"
  39. #include "soundchunkids.h"
  40. #include "persistfactory.h"
  41. #include "soundhandle.h"
  42. //////////////////////////////////////////////////////////////////////////////////
  43. // Static factories
  44. //////////////////////////////////////////////////////////////////////////////////
  45. SimplePersistFactoryClass<SoundPseudo3DClass, CHUNKID_PSEUDO_SOUND3D> _PseudoSound3DPersistFactory;
  46. ////////////////////////////////////////////////////////////////////////////////////////////////
  47. //
  48. // SoundPseudo3DClass
  49. //
  50. ////////////////////////////////////////////////////////////////////////////////////////////////
  51. SoundPseudo3DClass::SoundPseudo3DClass (void)
  52. {
  53. return ;
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////
  56. //
  57. // SoundPseudo3DClass
  58. //
  59. ////////////////////////////////////////////////////////////////////////////////////////////////
  60. SoundPseudo3DClass::SoundPseudo3DClass (const SoundPseudo3DClass &src)
  61. : Sound3DClass (src)
  62. {
  63. (*this) = src;
  64. return ;
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////////////////////
  67. //
  68. // ~SoundPseudo3DClass
  69. //
  70. ////////////////////////////////////////////////////////////////////////////////////////////////
  71. SoundPseudo3DClass::~SoundPseudo3DClass (void)
  72. {
  73. Free_Miles_Handle ();
  74. return ;
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////
  77. //
  78. // operator=
  79. //
  80. ////////////////////////////////////////////////////////////////////////////////////////////////
  81. const SoundPseudo3DClass &
  82. SoundPseudo3DClass::operator= (const SoundPseudo3DClass &src)
  83. {
  84. // Call the base class
  85. Sound3DClass::operator= (src);
  86. return (*this);
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////
  89. //
  90. // Set_Miles_Handle
  91. //
  92. ////////////////////////////////////////////////////////////////////////////////////////////////
  93. void
  94. SoundPseudo3DClass::Set_Miles_Handle (MILES_HANDLE handle)
  95. {
  96. AudibleSoundClass::Set_Miles_Handle (handle);
  97. return ;
  98. }
  99. ////////////////////////////////////////////////////////////////////////////////////////////////
  100. //
  101. // Initialize_Miles_Handle
  102. //
  103. ////////////////////////////////////////////////////////////////////////////////////////////////
  104. void
  105. SoundPseudo3DClass::Initialize_Miles_Handle (void)
  106. {
  107. AudibleSoundClass::Initialize_Miles_Handle ();
  108. Update_Pseudo_Volume ();
  109. return ;
  110. }
  111. ////////////////////////////////////////////////////////////////////////////////////////////////
  112. //
  113. // Update_Pseudo_Volume
  114. //
  115. ////////////////////////////////////////////////////////////////////////////////////////////////
  116. void
  117. SoundPseudo3DClass::Update_Pseudo_Volume (float distance)
  118. {
  119. MMSLockClass lock;
  120. //
  121. // Only do this if the sound is really playing
  122. //
  123. if (m_SoundHandle != NULL) {
  124. float volume_mod = Determine_Real_Volume ();
  125. float max_distance = Get_DropOff_Radius ();
  126. float min_distance = Get_Max_Vol_Radius ();
  127. float delta = max_distance - min_distance;
  128. // Determine a normalized volume from the position
  129. float volume = 1.0F;
  130. if (distance > min_distance) {
  131. volume = 1.0F - ((distance - min_distance) / delta);
  132. volume = min (volume, 1.0F);
  133. volume = max (volume, 0.0F);
  134. }
  135. // Multiply the 'max' volume with the calculated volume
  136. volume = volume * volume_mod;
  137. //
  138. // Pass the volume on
  139. //
  140. m_SoundHandle->Set_Sample_Volume (int(volume * 127.0F));
  141. }
  142. return ;
  143. }
  144. ////////////////////////////////////////////////////////////////////////////////////////////////
  145. //
  146. // Update_Pseudo_Volume
  147. //
  148. ////////////////////////////////////////////////////////////////////////////////////////////////
  149. void
  150. SoundPseudo3DClass::Update_Pseudo_Volume (void)
  151. {
  152. MMSLockClass lock;
  153. // Only do this if the sound is really playing
  154. if (m_SoundHandle != NULL) {
  155. //
  156. // Find the difference in the sound position and its listener's position
  157. //
  158. Vector3 sound_pos = m_ListenerTransform.Get_Translation () - m_Transform.Get_Translation ();
  159. float distance = sound_pos.Quick_Length ();
  160. //
  161. // Determine a normalized volume from the position
  162. //
  163. Update_Pseudo_Volume (distance);
  164. }
  165. return ;
  166. }
  167. ////////////////////////////////////////////////////////////////////////////////////////////////
  168. //
  169. // Update_Pseudo_Pan
  170. //
  171. ////////////////////////////////////////////////////////////////////////////////////////////////
  172. void
  173. SoundPseudo3DClass::Update_Pseudo_Pan (void)
  174. {
  175. MMSLockClass lock;
  176. //
  177. // Only do this if the sound is really playing
  178. //
  179. if (m_SoundHandle != NULL) {
  180. //
  181. // Transform the sound's position into 'listener-space'
  182. //
  183. Vector3 sound_pos = m_Transform.Get_Translation ();
  184. Vector3 rel_sound_pos;
  185. Matrix3D::Inverse_Transform_Vector (m_ListenerTransform, sound_pos, &rel_sound_pos);
  186. //
  187. // Calculate a normalized pan from 0 (hard left) to 1.0F (hard right)
  188. //
  189. float angle = WWMath::Atan2 (rel_sound_pos.Y, rel_sound_pos.X);
  190. float pan = -WWMath::Fast_Sin (angle);
  191. pan = (pan / 2.0F) + 0.5F;
  192. //
  193. // Pass the pan on
  194. //
  195. m_SoundHandle->Set_Sample_Pan (S32(pan * 127.0F));
  196. }
  197. return ;
  198. }
  199. ////////////////////////////////////////////////////////////////////////////////////////////////
  200. //
  201. // Allocate_Miles_Handle
  202. //
  203. ////////////////////////////////////////////////////////////////////////////////////////////////
  204. void
  205. SoundPseudo3DClass::Allocate_Miles_Handle (void)
  206. {
  207. AudibleSoundClass::Allocate_Miles_Handle ();
  208. return ;
  209. }
  210. ////////////////////////////////////////////////////////////////////////////////////////////////
  211. //
  212. // Free_Miles_Handle
  213. //
  214. ////////////////////////////////////////////////////////////////////////////////////////////////
  215. void
  216. SoundPseudo3DClass::Free_Miles_Handle (void)
  217. {
  218. AudibleSoundClass::Free_Miles_Handle ();
  219. return ;
  220. }
  221. ////////////////////////////////////////////////////////////////////////////////////////////////
  222. //
  223. // On_Frame_Update
  224. //
  225. ////////////////////////////////////////////////////////////////////////////////////////////////
  226. bool
  227. SoundPseudo3DClass::On_Frame_Update (unsigned int milliseconds)
  228. {
  229. // If necessary, update the volume based on the distance
  230. // from the listener
  231. if (m_SoundHandle != NULL) {
  232. Update_Pseudo_Volume ();
  233. Update_Pseudo_Pan ();
  234. }
  235. // Allow the base Sound3DClass to process this call
  236. return Sound3DClass::On_Frame_Update ();
  237. }
  238. ////////////////////////////////////////////////////////////////////////////////////////////////
  239. //
  240. // Get_Factory
  241. //
  242. ////////////////////////////////////////////////////////////////////////////////////////////////
  243. const PersistFactoryClass &
  244. SoundPseudo3DClass::Get_Factory (void) const
  245. {
  246. return _PseudoSound3DPersistFactory;
  247. }