GameAudio.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. if ( ! has3DSensitiveStreamsPlaying() )
  658. m_volumeHasChanged = TRUE;
  659. }
  660. //-------------------------------------------------------------------------------------------------
  661. void AudioManager::setListenerPosition( const Coord3D *newListenerPos, const Coord3D *newListenerOrientation )
  662. {
  663. m_listenerPosition = *newListenerPos;
  664. m_listenerOrientation = *newListenerOrientation;
  665. }
  666. //-------------------------------------------------------------------------------------------------
  667. const Coord3D *AudioManager::getListenerPosition( void ) const
  668. {
  669. return &m_listenerPosition;
  670. }
  671. //-------------------------------------------------------------------------------------------------
  672. AudioRequest *AudioManager::allocateAudioRequest( Bool useAudioEvent )
  673. {
  674. AudioRequest *audioReq = newInstance(AudioRequest);
  675. audioReq->m_usePendingEvent = useAudioEvent;
  676. audioReq->m_requiresCheckForSample = false;
  677. return audioReq;
  678. }
  679. //-------------------------------------------------------------------------------------------------
  680. void AudioManager::releaseAudioRequest( AudioRequest *requestToRelease )
  681. {
  682. if (requestToRelease) {
  683. requestToRelease->deleteInstance();
  684. }
  685. }
  686. //-------------------------------------------------------------------------------------------------
  687. void AudioManager::appendAudioRequest( AudioRequest *m_request )
  688. {
  689. m_audioRequests.push_back(m_request);
  690. }
  691. //-------------------------------------------------------------------------------------------------
  692. // Remove all pending audio requests
  693. void AudioManager::removeAllAudioRequests( void )
  694. {
  695. std::list<AudioRequest*>::iterator it;
  696. for ( it = m_audioRequests.begin(); it != m_audioRequests.end(); it++ ) {
  697. releaseAudioRequest( *it );
  698. }
  699. m_audioRequests.clear();
  700. }
  701. //-------------------------------------------------------------------------------------------------
  702. void AudioManager::processRequestList( void )
  703. {
  704. }
  705. //-------------------------------------------------------------------------------------------------
  706. AudioEventInfo *AudioManager::newAudioEventInfo( AsciiString audioName )
  707. {
  708. AudioEventInfo *eventInfo = findAudioEventInfo(audioName);
  709. if (eventInfo) {
  710. DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd\n", audioName.str()));
  711. return eventInfo;
  712. }
  713. m_allAudioEventInfo[audioName] = newInstance(AudioEventInfo);
  714. return m_allAudioEventInfo[audioName];
  715. }
  716. //-------------------------------------------------------------------------------------------------
  717. // Add an AudioEventInfo structure allocated elsewhere to the audio event list
  718. void AudioManager::addAudioEventInfo( AudioEventInfo * newEvent )
  719. {
  720. // Warning: Don't try to copy the structure. It may be a derived class
  721. AudioEventInfo *eventInfo = findAudioEventInfo( newEvent->m_audioName );
  722. if (eventInfo)
  723. {
  724. DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd\n", newEvent->m_audioName.str()));
  725. *eventInfo = *newEvent;
  726. }
  727. else
  728. {
  729. m_allAudioEventInfo[newEvent->m_audioName] = newEvent;
  730. }
  731. }
  732. //-------------------------------------------------------------------------------------------------
  733. AudioEventInfo *AudioManager::findAudioEventInfo( AsciiString eventName ) const
  734. {
  735. AudioEventInfoHash::const_iterator it;
  736. it = m_allAudioEventInfo.find(eventName);
  737. if (it == m_allAudioEventInfo.end()) {
  738. return NULL;
  739. }
  740. return (*it).second;
  741. }
  742. //-------------------------------------------------------------------------------------------------
  743. // Remove all AudioEventInfo's with the m_isLevelSpecific flag
  744. void AudioManager::removeLevelSpecificAudioEventInfos(void)
  745. {
  746. AudioEventInfoHash::iterator it = m_allAudioEventInfo.begin();
  747. while ( it != m_allAudioEventInfo.end() )
  748. {
  749. AudioEventInfoHash::iterator next = it; // Make sure erase doesn't cause problems
  750. next++;
  751. if ( it->second->isLevelSpecific() )
  752. {
  753. it->second->deleteInstance();
  754. m_allAudioEventInfo.erase( it );
  755. }
  756. it = next;
  757. }
  758. }
  759. //-------------------------------------------------------------------------------------------------
  760. const AudioSettings *AudioManager::getAudioSettings( void ) const
  761. {
  762. return m_audioSettings;
  763. }
  764. //-------------------------------------------------------------------------------------------------
  765. AudioSettings *AudioManager::friend_getAudioSettings( void )
  766. {
  767. return m_audioSettings;
  768. }
  769. //-------------------------------------------------------------------------------------------------
  770. const MiscAudio *AudioManager::getMiscAudio( void ) const
  771. {
  772. return m_miscAudio;
  773. }
  774. //-------------------------------------------------------------------------------------------------
  775. MiscAudio *AudioManager::friend_getMiscAudio( void )
  776. {
  777. return m_miscAudio;
  778. }
  779. //-------------------------------------------------------------------------------------------------
  780. const FieldParse *AudioManager::getFieldParseTable( void ) const
  781. {
  782. return audioSettingsFieldParseTable;
  783. }
  784. //-------------------------------------------------------------------------------------------------
  785. void AudioManager::refreshCachedVariables()
  786. {
  787. m_hardwareAccel = isCurrentProviderHardwareAccelerated();
  788. m_surroundSpeakers = isCurrentSpeakerTypeSurroundSound();
  789. }
  790. //-------------------------------------------------------------------------------------------------
  791. Real AudioManager::getAudioLengthMS( const AudioEventRTS *event )
  792. {
  793. if (!event->getAudioEventInfo()) {
  794. getInfoForAudioEvent(event);
  795. if (!event->getAudioEventInfo()) {
  796. return 0.0f;
  797. }
  798. }
  799. AudioEventRTS tmpEvent = *event;
  800. tmpEvent.generateFilename();
  801. tmpEvent.generatePlayInfo();
  802. return getFileLengthMS(tmpEvent.getAttackFilename()) +
  803. getFileLengthMS(tmpEvent.getFilename()) +
  804. getFileLengthMS(tmpEvent.getDecayFilename());
  805. }
  806. //-------------------------------------------------------------------------------------------------
  807. Bool AudioManager::isMusicAlreadyLoaded(void) const
  808. {
  809. const AudioEventInfo *musicToLoad = NULL;
  810. AudioEventInfoHash::const_iterator it;
  811. for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
  812. if (it->second) {
  813. const AudioEventInfo *aet = it->second;
  814. if (aet->m_soundType == AT_Music) {
  815. musicToLoad = aet;
  816. }
  817. }
  818. }
  819. if (!musicToLoad) {
  820. return FALSE;
  821. }
  822. AudioEventRTS aud;
  823. aud.setAudioEventInfo(musicToLoad);
  824. aud.generateFilename();
  825. AsciiString astr = aud.getFilename();
  826. return (TheFileSystem->doesFileExist(astr.str()));
  827. }
  828. //-------------------------------------------------------------------------------------------------
  829. void AudioManager::findAllAudioEventsOfType( AudioType audioType, std::vector<AudioEventInfo*>& allEvents )
  830. {
  831. AudioEventInfoHashIt it;
  832. for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
  833. AudioEventInfo *aud = (*it).second;
  834. if (aud->m_soundType == audioType) {
  835. allEvents.push_back(aud);
  836. }
  837. }
  838. }
  839. //-------------------------------------------------------------------------------------------------
  840. Bool AudioManager::isCurrentProviderHardwareAccelerated()
  841. {
  842. for (Int i = 0; i < MAX_HW_PROVIDERS; ++i) {
  843. if (getProviderName(getSelectedProvider()) == TheAudio->getAudioSettings()->m_preferred3DProvider[i]) {
  844. return TRUE;
  845. }
  846. }
  847. return FALSE;
  848. }
  849. //-------------------------------------------------------------------------------------------------
  850. Bool AudioManager::isCurrentSpeakerTypeSurroundSound()
  851. {
  852. return (getSpeakerType() == TheAudio->getAudioSettings()->m_defaultSpeakerType3D);
  853. }
  854. //-------------------------------------------------------------------------------------------------
  855. Bool AudioManager::shouldPlayLocally(const AudioEventRTS *audioEvent)
  856. {
  857. Player *localPlayer = ThePlayerList->getLocalPlayer();
  858. if( !localPlayer->isPlayerActive() )
  859. {
  860. //We are dead, thus are observing. Get the player we are observing. It's
  861. //possible that we're not looking at any player, therefore it can be NULL.
  862. localPlayer = TheControlBar->getObserverLookAtPlayer();
  863. }
  864. const AudioEventInfo *ei = audioEvent->getAudioEventInfo();
  865. // Music should always play locally.
  866. if (ei->m_soundType == AT_Music) {
  867. return TRUE;
  868. }
  869. if (!BitTest(ei->m_type, (ST_PLAYER | ST_ALLIES | ST_ENEMIES | ST_EVERYONE))) {
  870. DEBUG_CRASH(("No player restrictions specified for '%s'. Using Everyone.\n", ei->m_audioName.str()));
  871. return TRUE;
  872. }
  873. if (BitTest(ei->m_type, ST_EVERYONE)) {
  874. return TRUE;
  875. }
  876. Player *owningPlayer = ThePlayerList->getNthPlayer(audioEvent->getPlayerIndex());
  877. if (BitTest(ei->m_type, ST_PLAYER) && BitTest(ei->m_type, ST_UI) && owningPlayer == NULL) {
  878. DEBUG_ASSERTCRASH(!TheGameLogic->isInGameLogicUpdate(), ("Playing %s sound -- player-based UI sound without specifying a player.\n"));
  879. return TRUE;
  880. }
  881. if (owningPlayer == NULL) {
  882. DEBUG_CRASH(("Sound '%s' expects an owning player, but the audio event that created it didn't specify one.\n", ei->m_audioName.str()));
  883. return FALSE;
  884. }
  885. if( !localPlayer )
  886. {
  887. return FALSE;
  888. }
  889. const Team *localTeam = localPlayer->getDefaultTeam();
  890. if (localTeam == NULL) {
  891. return FALSE;
  892. }
  893. if (BitTest(ei->m_type, ST_PLAYER)) {
  894. return owningPlayer == localPlayer;
  895. }
  896. if (BitTest(ei->m_type, ST_ALLIES)) {
  897. // We have to also check that the owning player isn't the local player, because PLAYER
  898. // wasn't specified, or we wouldn't have gotten here.
  899. return (owningPlayer != localPlayer) && owningPlayer->getRelationship(localTeam) == ALLIES;
  900. }
  901. if (BitTest(ei->m_type, ST_ENEMIES)) {
  902. return owningPlayer->getRelationship(localTeam) == ENEMIES;
  903. }
  904. return FALSE;
  905. }
  906. //-------------------------------------------------------------------------------------------------
  907. AudioHandle AudioManager::allocateNewHandle( void )
  908. {
  909. // note, intenionally a post increment rather than a pre increment.
  910. return theAudioHandlePool++;
  911. }
  912. //-------------------------------------------------------------------------------------------------
  913. void AudioManager::releaseAudioEventRTS( AudioEventRTS *eventToRelease )
  914. {
  915. if( eventToRelease )
  916. {
  917. delete eventToRelease;
  918. eventToRelease = NULL;
  919. }
  920. }
  921. //-------------------------------------------------------------------------------------------------
  922. void AudioManager::loseFocus( void )
  923. {
  924. DEBUG_ASSERTLOG(m_savedValues == NULL, ("AudioManager::loseFocus() - leak - jkmcd\n"));
  925. // In this case, make all the audio go silent.
  926. m_savedValues = NEW Real[NUM_VOLUME_TYPES];
  927. m_savedValues[0] = m_systemMusicVolume;
  928. m_savedValues[1] = m_systemSoundVolume;
  929. m_savedValues[2] = m_systemSound3DVolume;
  930. m_savedValues[3] = m_systemSpeechVolume;
  931. // Now, set them all to 0.
  932. setVolume(0.0f, (AudioAffect) (AudioAffect_All | AudioAffect_SystemSetting));
  933. }
  934. //-------------------------------------------------------------------------------------------------
  935. void AudioManager::regainFocus( void )
  936. {
  937. if (!m_savedValues) {
  938. return;
  939. }
  940. // We got focus back. Restore the previous audio values.
  941. setVolume(m_savedValues[0], (AudioAffect) (AudioAffect_Music | AudioAffect_SystemSetting));
  942. setVolume(m_savedValues[1], (AudioAffect) (AudioAffect_Sound | AudioAffect_SystemSetting));
  943. setVolume(m_savedValues[2], (AudioAffect) (AudioAffect_Sound3D | AudioAffect_SystemSetting));
  944. setVolume(m_savedValues[3], (AudioAffect) (AudioAffect_Speech | AudioAffect_SystemSetting));
  945. // Now, blow away the old volumes.
  946. delete [] m_savedValues;
  947. m_savedValues = NULL;
  948. }
  949. //-------------------------------------------------------------------------------------------------
  950. //-------------------------------------------------------------------------------------------------
  951. //-------------------------------------------------------------------------------------------------
  952. void INI::parseAudioSettingsDefinition( INI *ini )
  953. {
  954. ini->initFromINI(TheAudio->friend_getAudioSettings(), TheAudio->getFieldParseTable());
  955. // time to override the volume settings, default 3-D provider, and speaker setup with the
  956. // ones stored from the prefs
  957. OptionPreferences prefs;
  958. TheAudio->setPreferredProvider(prefs.getPreferred3DProvider());
  959. TheAudio->setPreferredSpeaker(prefs.getSpeakerType());
  960. Real relative2DVolume = TheAudio->getAudioSettings()->m_relative2DVolume;
  961. relative2DVolume = MIN( 1.0f, MAX( -1.0f, relative2DVolume ) );
  962. TheAudio->friend_getAudioSettings()->m_preferredSoundVolume = prefs.getSoundVolume() / 100.0f;
  963. TheAudio->friend_getAudioSettings()->m_preferred3DSoundVolume = prefs.get3DSoundVolume() / 100.0f;
  964. TheAudio->friend_getAudioSettings()->m_preferredSpeechVolume = prefs.getSpeechVolume() / 100.0f;
  965. TheAudio->friend_getAudioSettings()->m_preferredMusicVolume = prefs.getMusicVolume() / 100.0f;
  966. }
  967. //-------------------------------------------------------------------------------------------------
  968. //-------------------------------------------------------------------------------------------------
  969. //-------------------------------------------------------------------------------------------------
  970. //-------------------------------------------------------------------------------------------------
  971. void parseSpeakerType( INI *ini, void *instance, void *store, const void* userData )
  972. {
  973. AsciiString str;
  974. ini->parseAsciiString( ini, instance, &str, userData );
  975. (*(UnsignedInt*)store) = TheAudio->translateSpeakerTypeToUnsignedInt(str);
  976. }