GameAudio.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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: GameAudio.cpp
  35. //
  36. // Created: 5/01/01
  37. //
  38. //----------------------------------------------------------------------------
  39. //----------------------------------------------------------------------------
  40. // Includes
  41. //----------------------------------------------------------------------------
  42. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  43. #include "Common/GameAudio.h"
  44. #include "Common/AudioAffect.h"
  45. #include "Common/AudioEventInfo.h"
  46. #include "Common/AudioEventRTS.h"
  47. #include "Common/AudioHandleSpecialValues.h"
  48. #include "Common/AudioRequest.h"
  49. #include "Common/AudioSettings.h"
  50. #include "Common/FileSystem.h"
  51. #include "Common/GameEngine.h"
  52. #include "Common/GameMusic.h"
  53. #include "Common/GameSounds.h"
  54. #include "Common/MiscAudio.h"
  55. #include "Common/OSDisplay.h"
  56. #include "Common/Player.h"
  57. #include "Common/PlayerList.h"
  58. #include "Common/UserPreferences.h"
  59. #include "GameClient/ControlBar.h"
  60. #include "GameClient/Drawable.h"
  61. #include "GameClient/View.h"
  62. #include "GameLogic/GameLogic.h"
  63. #include "GameLogic/TerrainLogic.h"
  64. #include "WWMath/Matrix3D.h"
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. #ifdef _INTERNAL
  67. //#pragma optimize("", off)
  68. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  69. #endif
  70. static const char* TheSpeakerTypes[] =
  71. {
  72. "2 Speakers",
  73. "Headphones",
  74. "Surround Sound",
  75. "4 Speaker",
  76. "5.1 Surround",
  77. "7.1 Surround",
  78. NULL
  79. };
  80. static const Int TheSpeakerTypesCount = sizeof(TheSpeakerTypes) / sizeof(TheSpeakerTypes[0]);
  81. static void parseSpeakerType( INI *ini, void *instance, void *store, const void *userData );
  82. // Field Parse table for Audio Settings ///////////////////////////////////////////////////////////
  83. static const FieldParse audioSettingsFieldParseTable[] =
  84. {
  85. { "AudioRoot", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_audioRoot) },
  86. { "SoundsFolder", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_soundsFolder) },
  87. { "MusicFolder", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_musicFolder) },
  88. { "StreamingFolder", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_streamingFolder) },
  89. { "SoundsExtension", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_soundsExtension) },
  90. { "UseDigital", INI::parseBool, NULL, offsetof( AudioSettings, m_useDigital) },
  91. { "UseMidi", INI::parseBool, NULL, offsetof( AudioSettings, m_useMidi) },
  92. { "OutputRate", INI::parseInt, NULL, offsetof( AudioSettings, m_outputRate) },
  93. { "OutputBits", INI::parseInt, NULL, offsetof( AudioSettings, m_outputBits) },
  94. { "OutputChannels", INI::parseInt, NULL, offsetof( AudioSettings, m_outputChannels) },
  95. { "SampleCount2D", INI::parseInt, NULL, offsetof( AudioSettings, m_sampleCount2D) },
  96. { "SampleCount3D", INI::parseInt, NULL, offsetof( AudioSettings, m_sampleCount3D) },
  97. { "StreamCount", INI::parseInt, NULL, offsetof( AudioSettings, m_streamCount) },
  98. { "Preferred3DHW1", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_preferred3DProvider[0]) },
  99. { "Preferred3DHW2", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_preferred3DProvider[1]) },
  100. { "Preferred3DHW3", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_preferred3DProvider[2]) },
  101. { "Preferred3DHW4", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_preferred3DProvider[3]) },
  102. { "Preferred3DSW", INI::parseAsciiString, NULL, offsetof( AudioSettings, m_preferred3DProvider[4]) },
  103. { "Default2DSpeakerType", parseSpeakerType, NULL, offsetof( AudioSettings, m_defaultSpeakerType2D) },
  104. { "Default3DSpeakerType", parseSpeakerType, NULL, offsetof( AudioSettings, m_defaultSpeakerType3D) },
  105. { "MinSampleVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_minVolume) },
  106. { "GlobalMinRange", INI::parseInt, NULL, offsetof( AudioSettings, m_globalMinRange) },
  107. { "GlobalMaxRange", INI::parseInt, NULL, offsetof( AudioSettings, m_globalMaxRange) },
  108. { "TimeBetweenDrawableSounds", INI::parseDurationUnsignedInt, NULL, offsetof( AudioSettings, m_drawableAmbientFrames) },
  109. { "TimeToFadeAudio", INI::parseDurationUnsignedInt, NULL, offsetof( AudioSettings, m_fadeAudioFrames) },
  110. { "AudioFootprintInBytes",INI::parseUnsignedInt, NULL, offsetof( AudioSettings, m_maxCacheSize) },
  111. { "Relative2DVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_relative2DVolume ) },
  112. { "DefaultSoundVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_defaultSoundVolume) },
  113. { "Default3DSoundVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_default3DSoundVolume) },
  114. { "DefaultSpeechVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_defaultSpeechVolume) },
  115. { "DefaultMusicVolume", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_defaultMusicVolume) },
  116. { "MicrophoneDesiredHeightAboveTerrain", INI::parseReal, NULL, offsetof( AudioSettings, m_microphoneDesiredHeightAboveTerrain ) },
  117. { "MicrophoneMaxPercentageBetweenGroundAndCamera", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_microphoneMaxPercentageBetweenGroundAndCamera ) },
  118. { "ZoomMinDistance", INI::parseReal, NULL, offsetof( AudioSettings, m_zoomMinDistance ) },
  119. { "ZoomMaxDistance", INI::parseReal, NULL, offsetof( AudioSettings, m_zoomMaxDistance ) },
  120. { "ZoomSoundVolumePercentageAmount", INI::parsePercentToReal, NULL, offsetof( AudioSettings, m_zoomSoundVolumePercentageAmount ) },
  121. { NULL, NULL, NULL, NULL }
  122. };
  123. // Singleton TheAudio /////////////////////////////////////////////////////////////////////////////
  124. AudioManager *TheAudio = NULL;
  125. // AudioManager Device Independent functions //////////////////////////////////////////////////////
  126. AudioManager::AudioManager() :
  127. m_soundOn(TRUE),
  128. m_sound3DOn(TRUE),
  129. m_musicOn(TRUE),
  130. m_speechOn(TRUE),
  131. m_music(NULL),
  132. m_sound(NULL),
  133. m_surroundSpeakers(FALSE),
  134. m_hardwareAccel(FALSE),
  135. m_musicPlayingFromCD(FALSE)
  136. {
  137. // Added by Sadullah Nader
  138. m_adjustedVolumes.clear();
  139. m_audioRequests.clear();
  140. m_listenerPosition.zero();
  141. m_musicTracks.clear();
  142. m_musicVolume = 0.0f;
  143. m_sound3DVolume = 0.0f;
  144. m_soundVolume = 0.0f;
  145. m_speechVolume = 0.0f;
  146. m_systemMusicVolume = 0.0f;
  147. m_systemSound3DVolume = 0.0f;
  148. m_systemSoundVolume = 0.0f;
  149. m_systemSpeechVolume = 0.0f;
  150. m_volumeHasChanged = FALSE;
  151. //
  152. m_listenerOrientation.set(0.0, 1.0, 0.0);
  153. theAudioHandlePool = AHSV_FirstHandle;
  154. m_audioSettings = NEW AudioSettings;
  155. m_miscAudio = NEW MiscAudio;
  156. m_silentAudioEvent = NEW AudioEventRTS;
  157. m_savedValues = NULL;
  158. m_disallowSpeech = FALSE;
  159. }
  160. //-------------------------------------------------------------------------------------------------
  161. AudioManager::~AudioManager()
  162. {
  163. // cleanup all of the loaded AudioEventInfos
  164. AudioEventInfoHashIt it;
  165. for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
  166. AudioEventInfo *eventInfo = (*it).second;
  167. if (eventInfo) {
  168. eventInfo->deleteInstance();
  169. eventInfo = NULL;
  170. }
  171. }
  172. delete m_silentAudioEvent;
  173. m_silentAudioEvent = NULL;
  174. delete m_music;
  175. m_music = NULL;
  176. delete m_sound;
  177. m_sound = NULL;
  178. delete m_miscAudio;
  179. m_miscAudio = NULL;
  180. delete m_audioSettings;
  181. m_audioSettings = NULL;
  182. if (m_savedValues)
  183. delete [] m_savedValues;
  184. }
  185. //-------------------------------------------------------------------------------------------------
  186. void AudioManager::init()
  187. {
  188. INI ini;
  189. ini.load( AsciiString( "Data\\INI\\AudioSettings.ini" ), INI_LOAD_OVERWRITE, NULL);
  190. ini.load( AsciiString( "Data\\INI\\Default\\Music.ini" ), INI_LOAD_OVERWRITE, NULL );
  191. ini.load( AsciiString( "Data\\INI\\Music.ini" ), INI_LOAD_OVERWRITE, NULL );
  192. ini.load( AsciiString( "Data\\INI\\Default\\SoundEffects.ini" ), INI_LOAD_OVERWRITE, NULL );
  193. ini.load( AsciiString( "Data\\INI\\SoundEffects.ini" ), INI_LOAD_OVERWRITE, NULL );
  194. ini.load( AsciiString( "Data\\INI\\Default\\Speech.ini" ), INI_LOAD_OVERWRITE, NULL );
  195. ini.load( AsciiString( "Data\\INI\\Speech.ini" ), INI_LOAD_OVERWRITE, NULL );
  196. ini.load( AsciiString( "Data\\INI\\Default\\Voice.ini" ), INI_LOAD_OVERWRITE, NULL );
  197. ini.load( AsciiString( "Data\\INI\\Voice.ini" ), INI_LOAD_OVERWRITE, NULL );
  198. // do the miscellaneous sound files last so that we find the audioeventrts associated with the events.
  199. ini.load( AsciiString( "Data\\INI\\MiscAudio.ini" ), INI_LOAD_OVERWRITE, NULL);
  200. // determine if one of the music tracks exists. Since their now BIGd, one implies all.
  201. // If they don't exist, then attempt to load them from the CD.
  202. if (!isMusicAlreadyLoaded())
  203. {
  204. m_musicPlayingFromCD = TRUE;
  205. while (TRUE)
  206. {
  207. // @todo Unload any files from CD first. - jkmcd
  208. TheFileSystem->loadMusicFilesFromCD();
  209. if (isMusicAlreadyLoaded())
  210. {
  211. break;
  212. }
  213. // We loop infinitely on the splash screen if we don't allow breaking out of this loop.
  214. //#if !defined( _DEBUG ) && !defined( _INTERNAL )
  215. else
  216. {
  217. // Display the warning.
  218. if (OSDisplayWarningBox("GUI:InsertCDPrompt", "GUI:InsertCDMessage", OSDBT_OK | OSDBT_CANCEL, OSDOF_SYSTEMMODAL | OSDOF_EXCLAMATIONICON) == OSDBT_CANCEL) {
  219. //TheGameEngine->setQuitting(TRUE); // Can't do this to WorldBuilder
  220. break;
  221. }
  222. }
  223. //#endif
  224. }
  225. }
  226. m_music = NEW MusicManager;
  227. m_sound = NEW SoundManager;
  228. // Set our system volumes from the user's preferred settings, not the defaults.
  229. m_systemMusicVolume = getAudioSettings() ? getAudioSettings()->m_preferredMusicVolume : 0.55f;
  230. m_systemSoundVolume = getAudioSettings() ? getAudioSettings()->m_preferredSoundVolume : 0.75f;
  231. m_systemSound3DVolume = getAudioSettings() ? getAudioSettings()->m_preferred3DSoundVolume: 0.75f;
  232. m_systemSpeechVolume = getAudioSettings() ? getAudioSettings()->m_preferredSpeechVolume : 0.55f;
  233. m_scriptMusicVolume = 1.0f;
  234. m_scriptSoundVolume = 1.0f;
  235. m_scriptSound3DVolume = 1.0f;
  236. m_scriptSpeechVolume = 1.0f;
  237. m_musicVolume = m_systemMusicVolume;
  238. m_soundVolume = m_systemSoundVolume;
  239. m_sound3DVolume = m_systemSound3DVolume;
  240. m_speechVolume = m_systemSpeechVolume;
  241. }
  242. //-------------------------------------------------------------------------------------------------
  243. void AudioManager::postProcessLoad()
  244. {
  245. }
  246. //-------------------------------------------------------------------------------------------------
  247. void AudioManager::reset()
  248. {
  249. // clear out any adjusted volumes we might have set.
  250. m_adjustedVolumes.clear();
  251. // adjust the scripted volumes, and reset the
  252. m_scriptMusicVolume = 1.0f;
  253. m_scriptSoundVolume = 1.0f;
  254. m_scriptSound3DVolume = 1.0f;
  255. m_scriptSpeechVolume = 1.0f;
  256. // restore the final values to the
  257. m_musicVolume = m_systemMusicVolume;
  258. m_soundVolume = m_systemSoundVolume;
  259. m_sound3DVolume = m_systemSound3DVolume;
  260. m_speechVolume = m_systemSpeechVolume;
  261. m_disallowSpeech = FALSE;
  262. }
  263. //-------------------------------------------------------------------------------------------------
  264. void AudioManager::update()
  265. {
  266. Coord3D groundPos, microphonePos;
  267. TheTacticalView->getPosition( &groundPos );
  268. Real angle = TheTacticalView->getAngle();
  269. Matrix3D rot = Matrix3D::Identity;
  270. rot.Rotate_Z( angle );
  271. Vector3 forward( 0, 1, 0 );
  272. rot.mulVector3( forward );
  273. Real desiredHeight = TheAudio->getAudioSettings()->m_microphoneDesiredHeightAboveTerrain;
  274. Real maxPercentage = TheAudio->getAudioSettings()->m_microphoneMaxPercentageBetweenGroundAndCamera;
  275. Coord3D lookTo;
  276. lookTo.set(forward.X, forward.Y, forward.Z);
  277. //Kris: At this point, the microphone is calculated to be at the ground position where the camera is looking at.
  278. //Instead we want to move the microphone towards the camera. Hopefully, it'll be a desired altitude, but if it
  279. //gets too close to the camera (or even past it), that would be undesirable. Therefore, we have a backup method
  280. //of making sure we only go a certain percentage towards the camera or the desired height, whichever occurs first.
  281. Coord3D cameraPos = TheTacticalView->get3DCameraPosition();
  282. Coord3D groundToCameraVector;
  283. groundToCameraVector.set( &cameraPos );
  284. groundToCameraVector.sub( &groundPos );
  285. Real bestScaleFactor;
  286. if( cameraPos.z <= desiredHeight || groundToCameraVector.z <= 0.0f )
  287. {
  288. //Use the percentage calculation!
  289. bestScaleFactor = maxPercentage;
  290. }
  291. else
  292. {
  293. //Calculate the stopping position of the groundToCameraVector when we force z to be m_microphoneDesiredHeightAboveTerrain
  294. Real zScale = desiredHeight / groundToCameraVector.z;
  295. //Use the smallest of the two scale calculations
  296. bestScaleFactor = MIN( maxPercentage, zScale );
  297. }
  298. //Now apply the best scalar to the ground-to-camera vector.
  299. groundToCameraVector.scale( bestScaleFactor );
  300. //Set the microphone to be the ground position adjusted for terrain plus the vector we just calculated.
  301. groundPos.z = TheTerrainLogic->getGroundHeight( groundPos.x, groundPos.y );
  302. microphonePos.set( &groundPos );
  303. microphonePos.add( &groundToCameraVector );
  304. //Viola! A properly placed microphone.
  305. setListenerPosition( &microphonePos, &lookTo );
  306. //Now determine if we would like to boost the volume based on the camera being close to the microphone!
  307. Real maxBoostScalar = TheAudio->getAudioSettings()->m_zoomSoundVolumePercentageAmount;
  308. Real minDist = TheAudio->getAudioSettings()->m_zoomMinDistance;
  309. Real maxDist = TheAudio->getAudioSettings()->m_zoomMaxDistance;
  310. //We can't boost a sound above 100%, instead reduce the normal sound level.
  311. m_zoomVolume = 1.0f - maxBoostScalar;
  312. //Are we even using a boost?
  313. if( maxBoostScalar > 0.0f )
  314. {
  315. //How far away is the camera from the microphone?
  316. Coord3D vector = cameraPos;
  317. vector.sub( &microphonePos );
  318. Real dist = vector.length();
  319. if( dist < minDist )
  320. {
  321. //Max volume!
  322. m_zoomVolume = 1.0f;
  323. }
  324. else if( dist < maxDist )
  325. {
  326. //Determine what the boost amount will be.
  327. Real scalar = (dist - minDist) / (maxDist - minDist);
  328. m_zoomVolume = 1.0f - scalar * maxBoostScalar;
  329. }
  330. }
  331. set3DVolumeAdjustment( m_zoomVolume );
  332. }
  333. //-------------------------------------------------------------------------------------------------
  334. void AudioManager::getInfoForAudioEvent( const AudioEventRTS *eventToFindAndFill ) const
  335. {
  336. if (!eventToFindAndFill) {
  337. return;
  338. }
  339. if (eventToFindAndFill->getAudioEventInfo()) {
  340. // already done
  341. return;
  342. }
  343. eventToFindAndFill->setAudioEventInfo(findAudioEventInfo(eventToFindAndFill->getEventName()));
  344. }
  345. //-------------------------------------------------------------------------------------------------
  346. AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd)
  347. {
  348. if (eventToAdd->getEventName().isEmpty() || eventToAdd->getEventName() == AsciiString("NoSound")) {
  349. return AHSV_NoSound;
  350. }
  351. #ifdef INTENSIVE_AUDIO_DEBUG
  352. DEBUG_LOG(("AUDIO (%d): Received addAudioEvent('%s')", TheGameLogic->getFrame(), eventToAdd->getEventName().str()));
  353. #endif
  354. if (!eventToAdd->getAudioEventInfo()) {
  355. getInfoForAudioEvent(eventToAdd);
  356. if (!eventToAdd->getAudioEventInfo()) {
  357. DEBUG_CRASH(("No info for requested audio event '%s'\n", eventToAdd->getEventName().str()));
  358. return AHSV_Error;
  359. }
  360. }
  361. switch (eventToAdd->getAudioEventInfo()->m_soundType)
  362. {
  363. case AT_Music:
  364. if (!isOn(AudioAffect_Music))
  365. return AHSV_NoSound;
  366. break;
  367. case AT_SoundEffect:
  368. if (!isOn(AudioAffect_Sound) || !isOn(AudioAffect_Sound3D))
  369. return AHSV_NoSound;
  370. break;
  371. case AT_Streaming:
  372. if (!isOn(AudioAffect_Speech))
  373. return AHSV_NoSound;
  374. break;
  375. }
  376. // if we're currently playing uninterruptable speech, then disallow the addition of this sample
  377. if (getDisallowSpeech() && eventToAdd->getAudioEventInfo()->m_soundType == AT_Streaming) {
  378. return AHSV_NoSound;
  379. }
  380. AudioEventRTS *audioEvent = MSGNEW("AudioEventRTS") AudioEventRTS(*eventToAdd); // poolify
  381. audioEvent->setPlayingHandle( allocateNewHandle() );
  382. audioEvent->generateFilename(); // which file are we actually going to play?
  383. ((AudioEventRTS*)eventToAdd)->setPlayingAudioIndex( audioEvent->getPlayingAudioIndex() );
  384. audioEvent->generatePlayInfo(); // generate pitch shift and volume shift now as well
  385. std::list<std::pair<AsciiString, Real> >::iterator it;
  386. for (it = m_adjustedVolumes.begin(); it != m_adjustedVolumes.end(); ++it) {
  387. if (it->first == audioEvent->getEventName()) {
  388. audioEvent->setVolume(it->second);
  389. break;
  390. }
  391. }
  392. if (!audioEvent->getUninterruptable()) {
  393. if (!shouldPlayLocally(audioEvent)) {
  394. releaseAudioEventRTS(audioEvent);
  395. return AHSV_NotForLocal;
  396. }
  397. }
  398. // cull muted audio
  399. if (audioEvent->getVolume() < TheAudio->getAudioSettings()->m_minVolume) {
  400. #ifdef INTENSIVE_AUDIO_DEBUG
  401. DEBUG_LOG((" - culled due to muting (%d).\n", audioEvent->getVolume()));
  402. #endif
  403. releaseAudioEventRTS(audioEvent);
  404. return AHSV_Muted;
  405. }
  406. AudioType type = eventToAdd->getAudioEventInfo()->m_soundType;
  407. if (type == AT_Music)
  408. {
  409. m_music->addAudioEvent(audioEvent);
  410. }
  411. else
  412. {
  413. //Possible to nuke audioEvent inside.
  414. m_sound->addAudioEvent(audioEvent);
  415. }
  416. if( audioEvent )
  417. {
  418. return audioEvent->getPlayingHandle();
  419. }
  420. return AHSV_NoSound;
  421. }
  422. //-------------------------------------------------------------------------------------------------
  423. Bool AudioManager::isValidAudioEvent(const AudioEventRTS *eventToCheck) const
  424. {
  425. if (eventToCheck->getEventName().isEmpty()) {
  426. return false;
  427. }
  428. getInfoForAudioEvent(eventToCheck);
  429. return (eventToCheck->getAudioEventInfo() != NULL);
  430. }
  431. //-------------------------------------------------------------------------------------------------
  432. Bool AudioManager::isValidAudioEvent( AudioEventRTS *eventToCheck ) const
  433. {
  434. if( eventToCheck->getEventName().isEmpty() )
  435. {
  436. return false;
  437. }
  438. getInfoForAudioEvent( eventToCheck );
  439. return( eventToCheck->getAudioEventInfo() );
  440. }
  441. //-------------------------------------------------------------------------------------------------
  442. void AudioManager::addTrackName( const AsciiString& trackName )
  443. {
  444. m_musicTracks.push_back(trackName);
  445. }
  446. //-------------------------------------------------------------------------------------------------
  447. AsciiString AudioManager::nextTrackName(const AsciiString& currentTrack )
  448. {
  449. std::vector<AsciiString>::iterator it;
  450. for (it = m_musicTracks.begin(); it != m_musicTracks.end(); ++it) {
  451. if (*it == currentTrack) {
  452. break;
  453. }
  454. }
  455. if (it != m_musicTracks.end()) {
  456. ++it;
  457. }
  458. if (it == m_musicTracks.end()) {
  459. it = m_musicTracks.begin();
  460. if (it == m_musicTracks.end()) {
  461. return AsciiString::TheEmptyString;
  462. }
  463. }
  464. return *it;
  465. }
  466. //-------------------------------------------------------------------------------------------------
  467. AsciiString AudioManager::prevTrackName(const AsciiString& currentTrack )
  468. {
  469. std::vector<AsciiString>::reverse_iterator rit;
  470. for (rit = m_musicTracks.rbegin(); rit != m_musicTracks.rend(); ++rit) {
  471. if (*rit == currentTrack) {
  472. break;
  473. }
  474. }
  475. if (rit != m_musicTracks.rend()) {
  476. ++rit;
  477. }
  478. if (rit == m_musicTracks.rend()) {
  479. rit = m_musicTracks.rbegin();
  480. if (rit == m_musicTracks.rend()) {
  481. return AsciiString::TheEmptyString;
  482. }
  483. }
  484. return *rit;
  485. }
  486. //-------------------------------------------------------------------------------------------------
  487. void AudioManager::removeAudioEvent(AudioHandle audioEvent)
  488. {
  489. if (audioEvent == AHSV_StopTheMusic || audioEvent == AHSV_StopTheMusicFade) {
  490. m_music->removeAudioEvent(audioEvent);
  491. return;
  492. }
  493. if (audioEvent < AHSV_FirstHandle) {
  494. return;
  495. }
  496. AudioRequest *req = allocateAudioRequest( false );
  497. req->m_handleToInteractOn = audioEvent;
  498. req->m_request = AR_Stop;
  499. appendAudioRequest( req );
  500. }
  501. //-------------------------------------------------------------------------------------------------
  502. void AudioManager::setAudioEventEnabled( AsciiString eventToAffect, Bool enable )
  503. {
  504. setAudioEventVolumeOverride(eventToAffect, (enable ? -1.0f : 0.0f) );
  505. }
  506. //-------------------------------------------------------------------------------------------------
  507. void AudioManager::setAudioEventVolumeOverride( AsciiString eventToAffect, Real newVolume )
  508. {
  509. if (eventToAffect == AsciiString::TheEmptyString) {
  510. m_adjustedVolumes.clear();
  511. return;
  512. }
  513. // Find any playing audio events and adjust their volume accordingly.
  514. if (newVolume != -1.0f) {
  515. adjustVolumeOfPlayingAudio(eventToAffect, newVolume);
  516. }
  517. std::list<std::pair<AsciiString, Real> >::iterator it;
  518. for (it = m_adjustedVolumes.begin(); it != m_adjustedVolumes.end(); ++it) {
  519. if (it->first == eventToAffect) {
  520. if (newVolume == -1.0f) {
  521. m_adjustedVolumes.erase(it);
  522. return;
  523. } else {
  524. it->second = newVolume;
  525. return;
  526. }
  527. }
  528. }
  529. if (newVolume != -1.0f) {
  530. std::pair<AsciiString, Real> newPair;
  531. newPair.first = eventToAffect;
  532. newPair.second = newVolume;
  533. m_adjustedVolumes.push_front(newPair);
  534. }
  535. }
  536. //-------------------------------------------------------------------------------------------------
  537. void AudioManager::removeAudioEvent( AsciiString eventToRemove )
  538. {
  539. removePlayingAudio( eventToRemove );
  540. }
  541. //-------------------------------------------------------------------------------------------------
  542. void AudioManager::removeDisabledEvents()
  543. {
  544. removeAllDisabledAudio();
  545. }
  546. //-------------------------------------------------------------------------------------------------
  547. Bool AudioManager::isCurrentlyPlaying( AudioHandle audioEvent )
  548. {
  549. return true;
  550. }
  551. //-------------------------------------------------------------------------------------------------
  552. UnsignedInt AudioManager::translateSpeakerTypeToUnsignedInt( const AsciiString& speakerType )
  553. {
  554. for (UnsignedInt i = 0; TheSpeakerTypes[i]; ++i) {
  555. if (TheSpeakerTypes[i] == speakerType) {
  556. return i;
  557. }
  558. }
  559. return 0;
  560. }
  561. //-------------------------------------------------------------------------------------------------
  562. AsciiString AudioManager::translateUnsignedIntToSpeakerType( UnsignedInt speakerType )
  563. {
  564. if (speakerType >= TheSpeakerTypesCount) {
  565. return TheSpeakerTypes[0];
  566. }
  567. return TheSpeakerTypes[speakerType];
  568. }
  569. //-------------------------------------------------------------------------------------------------
  570. Bool AudioManager::isOn( AudioAffect whichToGet ) const
  571. {
  572. if (whichToGet & AudioAffect_Music) {
  573. return m_musicOn;
  574. } else if (whichToGet & AudioAffect_Sound) {
  575. return m_soundOn;
  576. } else if (whichToGet & AudioAffect_Sound3D) {
  577. return m_sound3DOn;
  578. }
  579. // Speech
  580. return m_speechOn;
  581. }
  582. //-------------------------------------------------------------------------------------------------
  583. void AudioManager::setOn( Bool turnOn, AudioAffect whichToAffect )
  584. {
  585. if (whichToAffect & AudioAffect_Music) {
  586. m_musicOn = turnOn;
  587. }
  588. if (whichToAffect & AudioAffect_Sound) {
  589. m_soundOn = turnOn;
  590. }
  591. if (whichToAffect & AudioAffect_Sound3D) {
  592. m_sound3DOn = turnOn;
  593. }
  594. if (whichToAffect & AudioAffect_Speech) {
  595. m_speechOn = turnOn;
  596. }
  597. }
  598. //-------------------------------------------------------------------------------------------------
  599. void AudioManager::setVolume( Real volume, AudioAffect whichToAffect )
  600. {
  601. if (whichToAffect & AudioAffect_Music) {
  602. if (whichToAffect & AudioAffect_SystemSetting) {
  603. m_systemMusicVolume = volume;
  604. } else {
  605. m_scriptMusicVolume = volume;
  606. }
  607. m_musicVolume = m_scriptMusicVolume * m_systemMusicVolume;
  608. }
  609. if (whichToAffect & AudioAffect_Sound) {
  610. if (whichToAffect & AudioAffect_SystemSetting) {
  611. m_systemSoundVolume = volume;
  612. } else {
  613. m_scriptSoundVolume = volume;
  614. }
  615. m_soundVolume = m_scriptSoundVolume * m_systemSoundVolume;
  616. }
  617. if (whichToAffect & AudioAffect_Sound3D) {
  618. if (whichToAffect & AudioAffect_SystemSetting) {
  619. m_systemSound3DVolume = volume;
  620. } else {
  621. m_scriptSound3DVolume = volume;
  622. }
  623. m_sound3DVolume = m_scriptSound3DVolume * m_systemSound3DVolume;
  624. }
  625. if (whichToAffect & AudioAffect_Speech) {
  626. if (whichToAffect & AudioAffect_SystemSetting) {
  627. m_systemSpeechVolume = volume;
  628. } else {
  629. m_scriptSpeechVolume = volume;
  630. }
  631. m_speechVolume = m_scriptSpeechVolume * m_systemSpeechVolume;
  632. }
  633. m_volumeHasChanged = true;
  634. }
  635. //-------------------------------------------------------------------------------------------------
  636. Real AudioManager::getVolume( AudioAffect whichToGet )
  637. {
  638. if (whichToGet & AudioAffect_Music) {
  639. return m_musicVolume;
  640. } else if (whichToGet & AudioAffect_Sound) {
  641. return m_soundVolume;
  642. } else if (whichToGet & AudioAffect_Sound3D) {
  643. return m_sound3DVolume;
  644. }
  645. // Speech
  646. return m_speechVolume;
  647. }
  648. //-------------------------------------------------------------------------------------------------
  649. void AudioManager::set3DVolumeAdjustment( Real volumeAdjustment )
  650. {
  651. m_sound3DVolume = volumeAdjustment * m_scriptSound3DVolume * m_systemSound3DVolume;
  652. // clamp
  653. if (m_sound3DVolume < 0.0f)
  654. m_sound3DVolume = 0.0f;
  655. if (m_sound3DVolume > 1.0f)
  656. m_sound3DVolume = 1.0f;
  657. m_volumeHasChanged = TRUE;
  658. }
  659. //-------------------------------------------------------------------------------------------------
  660. void AudioManager::setListenerPosition( const Coord3D *newListenerPos, const Coord3D *newListenerOrientation )
  661. {
  662. m_listenerPosition = *newListenerPos;
  663. m_listenerOrientation = *newListenerOrientation;
  664. }
  665. //-------------------------------------------------------------------------------------------------
  666. const Coord3D *AudioManager::getListenerPosition( void ) const
  667. {
  668. return &m_listenerPosition;
  669. }
  670. //-------------------------------------------------------------------------------------------------
  671. AudioRequest *AudioManager::allocateAudioRequest( Bool useAudioEvent )
  672. {
  673. AudioRequest *audioReq = newInstance(AudioRequest);
  674. audioReq->m_usePendingEvent = useAudioEvent;
  675. audioReq->m_requiresCheckForSample = false;
  676. return audioReq;
  677. }
  678. //-------------------------------------------------------------------------------------------------
  679. void AudioManager::releaseAudioRequest( AudioRequest *requestToRelease )
  680. {
  681. if (requestToRelease) {
  682. requestToRelease->deleteInstance();
  683. }
  684. }
  685. //-------------------------------------------------------------------------------------------------
  686. void AudioManager::appendAudioRequest( AudioRequest *m_request )
  687. {
  688. m_audioRequests.push_back(m_request);
  689. }
  690. //-------------------------------------------------------------------------------------------------
  691. void AudioManager::processRequestList( void )
  692. {
  693. }
  694. //-------------------------------------------------------------------------------------------------
  695. AudioEventInfo *AudioManager::newAudioEventInfo( AsciiString audioName )
  696. {
  697. AudioEventInfo *eventInfo = findAudioEventInfo(audioName);
  698. if (eventInfo) {
  699. DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd\n", audioName.str()));
  700. return eventInfo;
  701. }
  702. m_allAudioEventInfo[audioName] = newInstance(AudioEventInfo);
  703. return m_allAudioEventInfo[audioName];
  704. }
  705. //-------------------------------------------------------------------------------------------------
  706. AudioEventInfo *AudioManager::findAudioEventInfo( AsciiString eventName ) const
  707. {
  708. AudioEventInfoHash::const_iterator it;
  709. it = m_allAudioEventInfo.find(eventName);
  710. if (it == m_allAudioEventInfo.end()) {
  711. return NULL;
  712. }
  713. return (*it).second;
  714. }
  715. //-------------------------------------------------------------------------------------------------
  716. const AudioSettings *AudioManager::getAudioSettings( void ) const
  717. {
  718. return m_audioSettings;
  719. }
  720. //-------------------------------------------------------------------------------------------------
  721. AudioSettings *AudioManager::friend_getAudioSettings( void )
  722. {
  723. return m_audioSettings;
  724. }
  725. //-------------------------------------------------------------------------------------------------
  726. const MiscAudio *AudioManager::getMiscAudio( void ) const
  727. {
  728. return m_miscAudio;
  729. }
  730. //-------------------------------------------------------------------------------------------------
  731. MiscAudio *AudioManager::friend_getMiscAudio( void )
  732. {
  733. return m_miscAudio;
  734. }
  735. //-------------------------------------------------------------------------------------------------
  736. const FieldParse *AudioManager::getFieldParseTable( void ) const
  737. {
  738. return audioSettingsFieldParseTable;
  739. }
  740. //-------------------------------------------------------------------------------------------------
  741. void AudioManager::refreshCachedVariables()
  742. {
  743. m_hardwareAccel = isCurrentProviderHardwareAccelerated();
  744. m_surroundSpeakers = isCurrentSpeakerTypeSurroundSound();
  745. }
  746. //-------------------------------------------------------------------------------------------------
  747. Real AudioManager::getAudioLengthMS( const AudioEventRTS *event )
  748. {
  749. if (!event->getAudioEventInfo()) {
  750. getInfoForAudioEvent(event);
  751. if (!event->getAudioEventInfo()) {
  752. return 0.0f;
  753. }
  754. }
  755. AudioEventRTS tmpEvent = *event;
  756. tmpEvent.generateFilename();
  757. tmpEvent.generatePlayInfo();
  758. return getFileLengthMS(tmpEvent.getAttackFilename()) +
  759. getFileLengthMS(tmpEvent.getFilename()) +
  760. getFileLengthMS(tmpEvent.getDecayFilename());
  761. }
  762. //-------------------------------------------------------------------------------------------------
  763. Bool AudioManager::isMusicAlreadyLoaded(void) const
  764. {
  765. const AudioEventInfo *musicToLoad = NULL;
  766. AudioEventInfoHash::const_iterator it;
  767. for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
  768. if (it->second) {
  769. const AudioEventInfo *aet = it->second;
  770. if (aet->m_soundType == AT_Music) {
  771. musicToLoad = aet;
  772. }
  773. }
  774. }
  775. if (!musicToLoad) {
  776. return FALSE;
  777. }
  778. AudioEventRTS aud;
  779. aud.setAudioEventInfo(musicToLoad);
  780. aud.generateFilename();
  781. AsciiString astr = aud.getFilename();
  782. return (TheFileSystem->doesFileExist(astr.str()));
  783. }
  784. //-------------------------------------------------------------------------------------------------
  785. void AudioManager::findAllAudioEventsOfType( AudioType audioType, std::vector<AudioEventInfo*>& allEvents )
  786. {
  787. AudioEventInfoHashIt it;
  788. for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
  789. AudioEventInfo *aud = (*it).second;
  790. if (aud->m_soundType == audioType) {
  791. allEvents.push_back(aud);
  792. }
  793. }
  794. }
  795. //-------------------------------------------------------------------------------------------------
  796. Bool AudioManager::isCurrentProviderHardwareAccelerated()
  797. {
  798. for (Int i = 0; i < MAX_HW_PROVIDERS; ++i) {
  799. if (getProviderName(getSelectedProvider()) == TheAudio->getAudioSettings()->m_preferred3DProvider[i]) {
  800. return TRUE;
  801. }
  802. }
  803. return FALSE;
  804. }
  805. //-------------------------------------------------------------------------------------------------
  806. Bool AudioManager::isCurrentSpeakerTypeSurroundSound()
  807. {
  808. return (getSpeakerType() == TheAudio->getAudioSettings()->m_defaultSpeakerType3D);
  809. }
  810. //-------------------------------------------------------------------------------------------------
  811. Bool AudioManager::shouldPlayLocally(const AudioEventRTS *audioEvent)
  812. {
  813. Player *localPlayer = ThePlayerList->getLocalPlayer();
  814. if( !localPlayer->isPlayerActive() )
  815. {
  816. //We are dead, thus are observing. Get the player we are observing. It's
  817. //possible that we're not looking at any player, therefore it can be NULL.
  818. localPlayer = TheControlBar->getObserverLookAtPlayer();
  819. }
  820. const AudioEventInfo *ei = audioEvent->getAudioEventInfo();
  821. // Music should always play locally.
  822. if (ei->m_soundType == AT_Music) {
  823. return TRUE;
  824. }
  825. if (!BitTest(ei->m_type, (ST_PLAYER | ST_ALLIES | ST_ENEMIES | ST_EVERYONE))) {
  826. DEBUG_CRASH(("No player restrictions specified for '%s'. Using Everyone.\n", ei->m_audioName.str()));
  827. return TRUE;
  828. }
  829. if (BitTest(ei->m_type, ST_EVERYONE)) {
  830. return TRUE;
  831. }
  832. Player *owningPlayer = ThePlayerList->getNthPlayer(audioEvent->getPlayerIndex());
  833. if (BitTest(ei->m_type, ST_PLAYER) && BitTest(ei->m_type, ST_UI) && owningPlayer == NULL) {
  834. DEBUG_ASSERTCRASH(!TheGameLogic->isInGameLogicUpdate(), ("Playing %s sound -- player-based UI sound without specifying a player.\n"));
  835. return TRUE;
  836. }
  837. if (owningPlayer == NULL) {
  838. DEBUG_CRASH(("Sound '%s' expects an owning player, but the audio event that created it didn't specify one.\n", ei->m_audioName.str()));
  839. return FALSE;
  840. }
  841. if( !localPlayer )
  842. {
  843. return FALSE;
  844. }
  845. const Team *localTeam = localPlayer->getDefaultTeam();
  846. if (localTeam == NULL) {
  847. return FALSE;
  848. }
  849. if (BitTest(ei->m_type, ST_PLAYER)) {
  850. return owningPlayer == localPlayer;
  851. }
  852. if (BitTest(ei->m_type, ST_ALLIES)) {
  853. // We have to also check that the owning player isn't the local player, because PLAYER
  854. // wasn't specified, or we wouldn't have gotten here.
  855. return (owningPlayer != localPlayer) && owningPlayer->getRelationship(localTeam) == ALLIES;
  856. }
  857. if (BitTest(ei->m_type, ST_ENEMIES)) {
  858. return owningPlayer->getRelationship(localTeam) == ENEMIES;
  859. }
  860. return FALSE;
  861. }
  862. //-------------------------------------------------------------------------------------------------
  863. AudioHandle AudioManager::allocateNewHandle( void )
  864. {
  865. // note, intenionally a post increment rather than a pre increment.
  866. return theAudioHandlePool++;
  867. }
  868. //-------------------------------------------------------------------------------------------------
  869. void AudioManager::releaseAudioEventRTS( AudioEventRTS *eventToRelease )
  870. {
  871. if( eventToRelease )
  872. {
  873. delete eventToRelease;
  874. eventToRelease = NULL;
  875. }
  876. }
  877. //-------------------------------------------------------------------------------------------------
  878. void AudioManager::loseFocus( void )
  879. {
  880. DEBUG_ASSERTLOG(m_savedValues == NULL, ("AudioManager::loseFocus() - leak - jkmcd\n"));
  881. // In this case, make all the audio go silent.
  882. m_savedValues = NEW Real[NUM_VOLUME_TYPES];
  883. m_savedValues[0] = m_systemMusicVolume;
  884. m_savedValues[1] = m_systemSoundVolume;
  885. m_savedValues[2] = m_systemSound3DVolume;
  886. m_savedValues[3] = m_systemSpeechVolume;
  887. // Now, set them all to 0.
  888. setVolume(0.0f, (AudioAffect) (AudioAffect_All | AudioAffect_SystemSetting));
  889. }
  890. //-------------------------------------------------------------------------------------------------
  891. void AudioManager::regainFocus( void )
  892. {
  893. if (!m_savedValues) {
  894. return;
  895. }
  896. // We got focus back. Restore the previous audio values.
  897. setVolume(m_savedValues[0], (AudioAffect) (AudioAffect_Music | AudioAffect_SystemSetting));
  898. setVolume(m_savedValues[1], (AudioAffect) (AudioAffect_Sound | AudioAffect_SystemSetting));
  899. setVolume(m_savedValues[2], (AudioAffect) (AudioAffect_Sound3D | AudioAffect_SystemSetting));
  900. setVolume(m_savedValues[3], (AudioAffect) (AudioAffect_Speech | AudioAffect_SystemSetting));
  901. // Now, blow away the old volumes.
  902. delete [] m_savedValues;
  903. m_savedValues = NULL;
  904. }
  905. //-------------------------------------------------------------------------------------------------
  906. //-------------------------------------------------------------------------------------------------
  907. //-------------------------------------------------------------------------------------------------
  908. void INI::parseAudioSettingsDefinition( INI *ini )
  909. {
  910. ini->initFromINI(TheAudio->friend_getAudioSettings(), TheAudio->getFieldParseTable());
  911. // time to override the volume settings, default 3-D provider, and speaker setup with the
  912. // ones stored from the prefs
  913. OptionPreferences prefs;
  914. TheAudio->setPreferredProvider(prefs.getPreferred3DProvider());
  915. TheAudio->setPreferredSpeaker(prefs.getSpeakerType());
  916. Real relative2DVolume = TheAudio->getAudioSettings()->m_relative2DVolume;
  917. relative2DVolume = MIN( 1.0f, MAX( -1.0f, relative2DVolume ) );
  918. TheAudio->friend_getAudioSettings()->m_preferredSoundVolume = prefs.getSoundVolume() / 100.0f;
  919. TheAudio->friend_getAudioSettings()->m_preferred3DSoundVolume = prefs.get3DSoundVolume() / 100.0f;
  920. TheAudio->friend_getAudioSettings()->m_preferredSpeechVolume = prefs.getSpeechVolume() / 100.0f;
  921. TheAudio->friend_getAudioSettings()->m_preferredMusicVolume = prefs.getMusicVolume() / 100.0f;
  922. }
  923. //-------------------------------------------------------------------------------------------------
  924. //-------------------------------------------------------------------------------------------------
  925. //-------------------------------------------------------------------------------------------------
  926. //-------------------------------------------------------------------------------------------------
  927. void parseSpeakerType( INI *ini, void *instance, void *store, const void* userData )
  928. {
  929. AsciiString str;
  930. ini->parseAsciiString( ini, instance, &str, userData );
  931. (*(UnsignedInt*)store) = TheAudio->translateSpeakerTypeToUnsignedInt(str);
  932. }