GameSounds.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //----------------------------------------------------------------------------
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright(C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: RTS3
  33. //
  34. // File name: GameSounds.cpp
  35. //
  36. // Created: 5/02/01
  37. //
  38. //----------------------------------------------------------------------------
  39. //----------------------------------------------------------------------------
  40. // Includes
  41. //----------------------------------------------------------------------------
  42. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  43. #include "Lib/Basetype.h"
  44. #include "Common/GameSounds.h"
  45. #include "Common/AudioEventInfo.h"
  46. #include "Common/AudioEventRTS.h"
  47. #include "Common/AudioRequest.h"
  48. #include "Common/Player.h"
  49. #include "Common/PlayerList.h"
  50. #include "GameLogic/PartitionManager.h"
  51. ///////////////////////////////////////////////////////////////////////////////////////////////////
  52. #ifdef _INTERNAL
  53. //#pragma optimize("", off)
  54. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  55. #endif
  56. //-------------------------------------------------------------------------------------------------
  57. SoundManager::SoundManager()
  58. {
  59. // nada to do
  60. }
  61. //-------------------------------------------------------------------------------------------------
  62. SoundManager::~SoundManager()
  63. {
  64. // nada to do
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. void SoundManager::init( void )
  68. {
  69. }
  70. //-------------------------------------------------------------------------------------------------
  71. void SoundManager::postProcessLoad()
  72. {
  73. // The AudioManager should actually be live now, so go ahead and get the info we need from it
  74. // here
  75. }
  76. //-------------------------------------------------------------------------------------------------
  77. void SoundManager::update( void )
  78. {
  79. }
  80. //-------------------------------------------------------------------------------------------------
  81. void SoundManager::reset( void )
  82. {
  83. m_numPlaying2DSamples = 0;
  84. m_numPlaying3DSamples = 0;
  85. }
  86. //-------------------------------------------------------------------------------------------------
  87. void SoundManager::loseFocus( void )
  88. {
  89. }
  90. //-------------------------------------------------------------------------------------------------
  91. void SoundManager::regainFocus( void )
  92. {
  93. }
  94. //-------------------------------------------------------------------------------------------------
  95. void SoundManager::setListenerPosition( const Coord3D *position )
  96. {
  97. }
  98. //-------------------------------------------------------------------------------------------------
  99. void SoundManager::setViewRadius( Real viewRadius )
  100. {
  101. }
  102. //-------------------------------------------------------------------------------------------------
  103. void SoundManager::setCameraAudibleDistance( Real audibleDistance )
  104. {
  105. }
  106. //-------------------------------------------------------------------------------------------------
  107. Real SoundManager::getCameraAudibleDistance( void )
  108. {
  109. return 1.0f;
  110. }
  111. //-------------------------------------------------------------------------------------------------
  112. void SoundManager::addAudioEvent(AudioEventRTS *eventToAdd)
  113. {
  114. if (m_num2DSamples == 0 && m_num3DSamples == 0) {
  115. m_num2DSamples = TheAudio->getNum2DSamples();
  116. m_num3DSamples = TheAudio->getNum3DSamples();
  117. }
  118. if (canPlayNow(eventToAdd)) {
  119. #ifdef INTENSIVE_AUDIO_DEBUG
  120. DEBUG_LOG((" - appended to request list with handle '%d'.\n", (UnsignedInt) eventToAdd->getPlayingHandle()));
  121. #endif
  122. AudioRequest *audioRequest = TheAudio->allocateAudioRequest( true );
  123. audioRequest->m_pendingEvent = eventToAdd;
  124. audioRequest->m_request = AR_Play;
  125. TheAudio->appendAudioRequest(audioRequest);
  126. } else {
  127. TheAudio->releaseAudioEventRTS(eventToAdd);
  128. }
  129. }
  130. //-------------------------------------------------------------------------------------------------
  131. void SoundManager::notifyOf2DSampleStart( void )
  132. {
  133. ++m_numPlaying2DSamples;
  134. }
  135. //-------------------------------------------------------------------------------------------------
  136. void SoundManager::notifyOf3DSampleStart( void )
  137. {
  138. ++m_numPlaying3DSamples;
  139. }
  140. //-------------------------------------------------------------------------------------------------
  141. void SoundManager::notifyOf2DSampleCompletion( void )
  142. {
  143. if (m_numPlaying2DSamples > 0) {
  144. --m_numPlaying2DSamples;
  145. }
  146. }
  147. //-------------------------------------------------------------------------------------------------
  148. void SoundManager::notifyOf3DSampleCompletion( void )
  149. {
  150. if (m_numPlaying3DSamples > 0) {
  151. --m_numPlaying3DSamples;
  152. }
  153. }
  154. //-------------------------------------------------------------------------------------------------
  155. Int SoundManager::getAvailableSamples( void )
  156. {
  157. return (m_num2DSamples - m_numPlaying2DSamples);
  158. }
  159. //-------------------------------------------------------------------------------------------------
  160. Int SoundManager::getAvailable3DSamples( void )
  161. {
  162. return (m_num3DSamples - m_numPlaying3DSamples);
  163. }
  164. //-------------------------------------------------------------------------------------------------
  165. AsciiString SoundManager::getFilenameForPlayFromAudioEvent( const AudioEventRTS *eventToGetFrom )
  166. {
  167. return AsciiString::TheEmptyString;
  168. }
  169. //-------------------------------------------------------------------------------------------------
  170. Bool SoundManager::canPlayNow( AudioEventRTS *event )
  171. {
  172. Bool retVal = false;
  173. // 1) Are we muted because we're beyond our maximum distance?
  174. // 2) Are we shrouded and this is a shroud sound?
  175. // 3) Are we violating our voice count or are we playing above the limit? (If so, stop now)
  176. // 4) is there an avaiable channel open?
  177. // 5) if not, then determine if there is anything of lower priority that we can kill
  178. // 6) if not, are we an interrupt-sound type?
  179. // if so, are there any sounds of our type playing right now that we can interrupt?
  180. // potentially here: Are there any sounds that are playing that are now beyond their distance?
  181. // if so, kill them and start our sound
  182. // if not, we're done. Can't play dude.
  183. if( event->isPositionalAudio() && !BitTest( event->getAudioEventInfo()->m_type, ST_GLOBAL) && event->getAudioEventInfo()->m_priority != AP_CRITICAL )
  184. {
  185. Coord3D distance = *TheAudio->getListenerPosition();
  186. const Coord3D *pos = event->getCurrentPosition();
  187. if (pos)
  188. {
  189. distance.sub(pos);
  190. if (distance.length() >= event->getAudioEventInfo()->m_maxDistance)
  191. {
  192. #ifdef INTENSIVE_AUDIO_DEBUG
  193. DEBUG_LOG(("- culled due to distance (%.2f).\n", distance.length()));
  194. #endif
  195. return false;
  196. }
  197. Int localPlayerNdx = ThePlayerList->getLocalPlayer()->getPlayerIndex();
  198. if( (event->getAudioEventInfo()->m_type & ST_SHROUDED) &&
  199. ThePartitionManager->getShroudStatusForPlayer(localPlayerNdx, pos) != CELLSHROUD_CLEAR )
  200. {
  201. #ifdef INTENSIVE_AUDIO_DEBUG
  202. DEBUG_LOG(("- culled due to shroud.\n"));
  203. #endif
  204. return false;
  205. }
  206. }
  207. }
  208. if (violatesVoice(event))
  209. {
  210. retVal = isInterrupting(event);
  211. if (retVal)
  212. {
  213. return true;
  214. }
  215. else
  216. {
  217. #ifdef INTENSIVE_AUDIO_DEBUG
  218. DEBUG_LOG(("- culled due to voice.\n"));
  219. #endif
  220. return false;
  221. }
  222. }
  223. if( TheAudio->doesViolateLimit( event ) )
  224. {
  225. #ifdef INTENSIVE_AUDIO_DEBUG
  226. DEBUG_LOG(("- culled due to limit.\n" ));
  227. #endif
  228. return false;
  229. }
  230. else if( isInterrupting( event ) )
  231. {
  232. return true;
  233. }
  234. if (event->isPositionalAudio())
  235. {
  236. if (m_numPlaying3DSamples < m_num3DSamples)
  237. {
  238. return true;
  239. }
  240. #ifdef INTENSIVE_AUDIO_DEBUG
  241. DEBUG_LOG(("- %d samples playing, %d samples available", m_numPlaying3DSamples, m_num3DSamples));
  242. #endif
  243. }
  244. else
  245. {
  246. // its a UI sound (and thus, 2-D)
  247. if (m_numPlaying2DSamples < m_num2DSamples)
  248. {
  249. return true;
  250. }
  251. }
  252. if (TheAudio->isPlayingLowerPriority(event))
  253. {
  254. return true;
  255. }
  256. if (isInterrupting(event))
  257. {
  258. retVal = TheAudio->isPlayingAlready(event);
  259. if (retVal)
  260. {
  261. return true;
  262. }
  263. else
  264. {
  265. #ifdef INTENSIVE_AUDIO_DEBUG
  266. DEBUG_LOG(("- culled due to no channels available and non-interrupting.\n" ));
  267. #endif
  268. return false;
  269. }
  270. }
  271. #ifdef INTENSIVE_AUDIO_DEBUG
  272. DEBUG_LOG(("culled due to unavailable channels"));
  273. #endif
  274. return false;
  275. }
  276. //-------------------------------------------------------------------------------------------------
  277. Bool SoundManager::violatesVoice( AudioEventRTS *event )
  278. {
  279. if (event->getAudioEventInfo()->m_type & ST_VOICE) {
  280. return (event->getObjectID() && TheAudio->isObjectPlayingVoice(event->getObjectID()));
  281. }
  282. return false;
  283. }
  284. //-------------------------------------------------------------------------------------------------
  285. Bool SoundManager::isInterrupting( AudioEventRTS *event )
  286. {
  287. return event->getAudioEventInfo()->m_control & AC_INTERRUPT;
  288. }