MilesAudioManager.cpp 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343
  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. // FILE: MilesAudioManager.cpp
  24. /*---------------------------------------------------------------------------*/
  25. /* EA Pacific */
  26. /* Confidential Information */
  27. /* Copyright (C) 2001 - All Rights Reserved */
  28. /* DO NOT DISTRIBUTE */
  29. /*---------------------------------------------------------------------------*/
  30. /* Project: RTS3 */
  31. /* File name: MilesAudioManager.cpp */
  32. /* Created: John K. McDonald, Jr., 3/21/2002 */
  33. /* Desc: This is the implementation for the MilesAudioManager, which */
  34. /* interfaces with the Miles Sound System. */
  35. /* Revision History: */
  36. /* 7/18/2002 : Initial creation */
  37. /*---------------------------------------------------------------------------*/
  38. #include <dsound.h>
  39. #include "Lib/Basetype.h"
  40. #include "MilesAudioDevice/MilesAudioManager.h"
  41. #include "Common/AudioAffect.h"
  42. #include "Common/AudioHandleSpecialValues.h"
  43. #include "Common/AudioRequest.h"
  44. #include "Common/AudioSettings.h"
  45. #include "Common/AsciiString.h"
  46. #include "Common/AudioEventInfo.h"
  47. #include "Common/FileSystem.h"
  48. #include "Common/GameCommon.h"
  49. #include "Common/GameSounds.h"
  50. #include "Common/CRCDebug.h"
  51. #include "Common/GlobalData.h"
  52. #include "Common/ScopedMutex.h"
  53. #include "GameClient/DebugDisplay.h"
  54. #include "GameClient/Drawable.h"
  55. #include "GameClient/GameClient.h"
  56. #include "GameClient/VideoPlayer.h"
  57. #include "GameClient/View.h"
  58. #include "GameLogic/GameLogic.h"
  59. #include "GameLogic/TerrainLogic.h"
  60. #include "Common/File.h"
  61. #ifdef _INTERNAL
  62. //#pragma optimize("", off)
  63. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  64. #endif
  65. enum { INFINITE_LOOP_COUNT = 1000000 };
  66. // Callback functions useful for Miles ////////////////////////////////////////////////////////////
  67. static void AILCALLBACK setSampleCompleted( HSAMPLE sampleCompleted );
  68. static void AILCALLBACK set3DSampleCompleted( H3DSAMPLE sample3DCompleted );
  69. static void AILCALLBACK setStreamCompleted( HSTREAM streamCompleted );
  70. static U32 AILCALLBACK streamingFileOpen(char const *fileName, U32 *file_handle);
  71. static void AILCALLBACK streamingFileClose(U32 fileHandle);
  72. static S32 AILCALLBACK streamingFileSeek(U32 fileHandle, S32 offset, U32 type);
  73. static U32 AILCALLBACK streamingFileRead(U32 fileHandle, void *buffer, U32 bytes);
  74. //-------------------------------------------------------------------------------------------------
  75. MilesAudioManager::MilesAudioManager() :
  76. m_providerCount(0),
  77. m_selectedProvider(PROVIDER_ERROR),
  78. m_selectedSpeakerType(0),
  79. m_lastProvider(PROVIDER_ERROR),
  80. m_digitalHandle(NULL),
  81. m_num2DSamples(0),
  82. m_num3DSamples(0),
  83. m_numStreams(0),
  84. m_delayFilter(NULL),
  85. m_binkHandle(NULL),
  86. m_pref3DProvider(AsciiString::TheEmptyString),
  87. m_prefSpeaker(AsciiString::TheEmptyString)
  88. {
  89. m_audioCache = NEW AudioFileCache;
  90. }
  91. //-------------------------------------------------------------------------------------------------
  92. MilesAudioManager::~MilesAudioManager()
  93. {
  94. DEBUG_ASSERTCRASH(m_binkHandle == NULL, ("Leaked a Bink handle. Chuybregts"));
  95. releaseHandleForBink();
  96. closeDevice();
  97. delete m_audioCache;
  98. DEBUG_ASSERTCRASH(this == TheAudio, ("Umm...\n"));
  99. TheAudio = NULL;
  100. }
  101. //-------------------------------------------------------------------------------------------------
  102. #if defined(_DEBUG) || defined(_INTERNAL)
  103. AudioHandle MilesAudioManager::addAudioEvent( const AudioEventRTS *eventToAdd )
  104. {
  105. if (TheGlobalData->m_preloadReport) {
  106. if (!eventToAdd->getEventName().isEmpty()) {
  107. m_allEventsLoaded.insert(eventToAdd->getEventName());
  108. }
  109. }
  110. return AudioManager::addAudioEvent(eventToAdd);
  111. }
  112. #endif
  113. #if defined(_DEBUG) || defined(_INTERNAL)
  114. //-------------------------------------------------------------------------------------------------
  115. void MilesAudioManager::audioDebugDisplay(DebugDisplayInterface *dd, void *, FILE *fp )
  116. {
  117. std::list<PlayingAudio *>::iterator it;
  118. static char buffer[128] = { 0 };
  119. if (buffer[0] == 0) {
  120. AIL_MSS_version(buffer, 128);
  121. }
  122. Coord3D lookPos;
  123. TheTacticalView->getPosition( &lookPos );
  124. lookPos.z = TheTerrainLogic->getGroundHeight( lookPos.x, lookPos.y );
  125. const Coord3D *mikePos = TheAudio->getListenerPosition();
  126. Coord3D distanceVector = TheTacticalView->get3DCameraPosition();
  127. distanceVector.sub( mikePos );
  128. Int now = TheGameLogic->getFrame();
  129. static Int lastCheck = now;
  130. const Int frames = 60;
  131. static Int latency = 0;
  132. static Int worstLatency = 0;
  133. if( lastCheck + frames <= now )
  134. {
  135. latency = AIL_get_timer_highest_delay();
  136. if( latency > worstLatency )
  137. {
  138. worstLatency = latency;
  139. }
  140. lastCheck = now;
  141. }
  142. if( dd )
  143. {
  144. dd->printf("Miles Sound System version: %s ", buffer);
  145. dd->printf("Memory Usage : %d/%d\n", m_audioCache->getCurrentlyUsedSize(), m_audioCache->getMaxSize());
  146. dd->printf("Sound: %s ", (isOn(AudioAffect_Sound) ? "Yes" : "No"));
  147. dd->printf("3DSound: %s ", (isOn(AudioAffect_Sound3D) ? "Yes" : "No"));
  148. dd->printf("Speech: %s ", (isOn(AudioAffect_Speech) ? "Yes" : "No"));
  149. dd->printf("Music: %s\n", (isOn(AudioAffect_Music) ? "Yes" : "No"));
  150. dd->printf("Channels Available: ");
  151. dd->printf("%d Sounds ", m_sound->getAvailableSamples());
  152. dd->printf("%d(%d) 3D Sounds\n", m_sound->getAvailable3DSamples(), m_available3DSamples.size() );
  153. dd->printf("Volume: ");
  154. dd->printf("Sound: %d ", REAL_TO_INT(m_soundVolume * 100.0f));
  155. dd->printf("3DSound: %d ", REAL_TO_INT(m_sound3DVolume * 100.0f));
  156. dd->printf("Speech: %d ", REAL_TO_INT(m_speechVolume * 100.0f));
  157. dd->printf("Music: %d\n", REAL_TO_INT(m_musicVolume * 100.0f));
  158. dd->printf("Current 3D Provider: %s ",
  159. TheAudio->getProviderName(m_selectedProvider).str());
  160. dd->printf("Current Speaker Type: %s\n", TheAudio->translateUnsignedIntToSpeakerType(TheAudio->getSpeakerType()).str());
  161. dd->printf( "Looking at: (%d,%d,%d) -- Microphone at: (%d,%d,%d)\n",
  162. (Int)lookPos.x, (Int)lookPos.y, (Int)lookPos.z, (Int)mikePos->x, (Int)mikePos->y, (Int)mikePos->z );
  163. dd->printf( "Camera distance from microphone: %d -- Zoom Volume: %d%%\n",
  164. (Int)distanceVector.length(), (Int)(TheAudio->getZoomVolume()*100.0f) );
  165. dd->printf( "Worst latency: %d -- Current latency: %d\n", worstLatency, latency );
  166. dd->printf("-----------------------------------------------------------\n");
  167. dd->printf("Playing Audio\n");
  168. }
  169. if( fp )
  170. {
  171. fprintf( fp, "Miles Sound System version: %s ", buffer );
  172. fprintf( fp, "Memory Usage : %d/%d\n", m_audioCache->getCurrentlyUsedSize(), m_audioCache->getMaxSize() );
  173. fprintf( fp, "Sound: %s ", (isOn(AudioAffect_Sound) ? "Yes" : "No") );
  174. fprintf( fp, "3DSound: %s ", (isOn(AudioAffect_Sound3D) ? "Yes" : "No") );
  175. fprintf( fp, "Speech: %s ", (isOn(AudioAffect_Speech) ? "Yes" : "No") );
  176. fprintf( fp, "Music: %s\n", (isOn(AudioAffect_Music) ? "Yes" : "No") );
  177. fprintf( fp, "Channels Available: " );
  178. fprintf( fp, "%d Sounds ", m_sound->getAvailableSamples() );
  179. fprintf( fp, "%d 3D Sounds\n", m_sound->getAvailable3DSamples() );
  180. fprintf( fp, "Volume: ");
  181. fprintf( fp, "Sound: %d ", REAL_TO_INT(m_soundVolume * 100.0f) );
  182. fprintf( fp, "3DSound: %d ", REAL_TO_INT(m_sound3DVolume * 100.0f) );
  183. fprintf( fp, "Speech: %d ", REAL_TO_INT(m_speechVolume * 100.0f) );
  184. fprintf( fp, "Music: %d\n", REAL_TO_INT(m_musicVolume * 100.0f) );
  185. fprintf( fp, "Current 3D Provider: %s ", TheAudio->getProviderName(m_selectedProvider).str());
  186. fprintf( fp, "Current Speaker Type: %s\n", TheAudio->translateUnsignedIntToSpeakerType(TheAudio->getSpeakerType()).str());
  187. fprintf( fp, "Looking at: (%d,%d,%d) -- Microphone at: (%d,%d,%d)\n",
  188. (Int)lookPos.x, (Int)lookPos.y, (Int)lookPos.z, (Int)mikePos->x, (Int)mikePos->y, (Int)mikePos->z );
  189. fprintf( fp, "Camera distance from microphone: %d -- Zoom Volume: %d%%\n",
  190. (Int)distanceVector.length(), (Int)(TheAudio->getZoomVolume()*100.0f) );
  191. fprintf( fp, "-----------------------------------------------------------\n" );
  192. fprintf( fp, "Playing Audio\n" );
  193. }
  194. PlayingAudio *playing = NULL;
  195. Int channel;
  196. Int channelCount;
  197. Real volume = 0.0f;
  198. AsciiString filenameNoSlashes;
  199. const Int maxChannels = 64;
  200. PlayingAudio *playingArray[maxChannels] = { NULL };
  201. // 2-D Sounds
  202. if( dd )
  203. {
  204. dd->printf("-----------------------------------------------------Sounds\n");
  205. channelCount = TheAudio->getNum2DSamples();
  206. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  207. playing = *it;
  208. if (!playing) {
  209. continue;
  210. }
  211. playingArray[AIL_sample_user_data(playing->m_sample, 0)] = playing;
  212. }
  213. for (Int i = 1; i <= maxChannels && i <= channelCount; ++i) {
  214. playing = playingArray[i];
  215. if (!playing) {
  216. dd->printf("%d: Silence\n", i);
  217. continue;
  218. }
  219. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  220. filenameNoSlashes = filenameNoSlashes.reverseFind('\\') + 1;
  221. // Calculate Sample volume
  222. volume = 100.0f;
  223. volume *= getEffectiveVolume(playing->m_audioEventRTS);
  224. dd->printf("%2d: %-20s - (%s) Volume: %d (2D)\n", i, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT(volume));
  225. playingArray[i] = NULL;
  226. }
  227. }
  228. if( fp )
  229. {
  230. fprintf( fp, "-----------------------------------------------------Sounds\n" );
  231. channelCount = TheAudio->getNum2DSamples();
  232. channel = 1;
  233. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it )
  234. {
  235. playing = *it;
  236. if( !playing )
  237. {
  238. continue;
  239. }
  240. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  241. filenameNoSlashes = filenameNoSlashes.reverseFind( '\\' ) + 1;
  242. // Calculate Sample volume
  243. volume = 100.0f;
  244. volume *= getEffectiveVolume( playing->m_audioEventRTS );
  245. fprintf( fp, "%2d: %-20s - (%s) Volume: %d (2D)\n", channel++, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT( volume ) );
  246. }
  247. for( int i = channel; i <= channelCount; ++i )
  248. {
  249. fprintf( fp, "%d: Silence\n", i );
  250. }
  251. }
  252. const Coord3D *microphonePos = TheAudio->getListenerPosition();
  253. // Now 3D Sounds
  254. if( dd )
  255. {
  256. dd->printf("--------------------------------------------------3D Sounds\n");
  257. channelCount = TheAudio->getNum3DSamples();
  258. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  259. playing = *it;
  260. if (!playing) {
  261. continue;
  262. }
  263. playingArray[AIL_3D_user_data(playing->m_3DSample, 0)] = playing;
  264. }
  265. for (Int i = 1; i <= maxChannels && i <= channelCount; ++i)
  266. {
  267. playing = playingArray[i];
  268. if (!playing)
  269. {
  270. dd->printf("%d: Silence\n", i);
  271. continue;
  272. }
  273. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  274. filenameNoSlashes = filenameNoSlashes.reverseFind('\\') + 1;
  275. // Calculate Sample volume
  276. volume = 100.0f;
  277. volume *= getEffectiveVolume(playing->m_audioEventRTS);
  278. Real dist = -1.0f;
  279. const Coord3D *pos = playing->m_audioEventRTS->getPosition();
  280. char distStr[32];
  281. if( pos )
  282. {
  283. Coord3D vector = *microphonePos;
  284. vector.sub( pos );
  285. dist = vector.length();
  286. sprintf( distStr, "%d", REAL_TO_INT( dist ) );
  287. }
  288. else
  289. {
  290. sprintf( distStr, "???" );
  291. }
  292. char str[32];
  293. switch( playing->m_audioEventRTS->getOwnerType() )
  294. {
  295. case OT_Positional:
  296. sprintf( str, "(3D)" );
  297. break;
  298. case OT_Object:
  299. sprintf( str, "(3DObj)" );
  300. break;
  301. case OT_Drawable:
  302. sprintf( str, "(3DDraw)" );
  303. break;
  304. case OT_Dead:
  305. sprintf( str, "(3DDead)" );
  306. break;
  307. }
  308. dd->printf("%2d: %-20s - (%s) Volume: %d, Dist: %s, %s\n",
  309. i, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT(volume), distStr, str );
  310. playingArray[i] = NULL;
  311. }
  312. }
  313. if( fp )
  314. {
  315. fprintf( fp, "--------------------------------------------------3D Sounds\n" );
  316. channelCount = TheAudio->getNum3DSamples();
  317. channel = 1;
  318. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it )
  319. {
  320. playing = *it;
  321. if( !playing )
  322. {
  323. continue;
  324. }
  325. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  326. filenameNoSlashes = filenameNoSlashes.reverseFind('\\') + 1;
  327. // Calculate Sample volume
  328. volume = 100.0f;
  329. volume *= getEffectiveVolume( playing->m_audioEventRTS );
  330. fprintf( fp, "%2d: %-24s - (%s) Volume: %d \n", channel++, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT( volume ) );
  331. }
  332. for( int i = channel; i <= channelCount; ++i )
  333. {
  334. fprintf( fp, "%2d: Silence\n", i );
  335. }
  336. }
  337. // Now Streams
  338. if( dd )
  339. {
  340. dd->printf("----------------------------------------------------Streams\n");
  341. channelCount = TheAudio->getNumStreams();
  342. channel = 1;
  343. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  344. playing = *it;
  345. if (!playing) {
  346. continue;
  347. }
  348. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  349. filenameNoSlashes = filenameNoSlashes.reverseFind('\\') + 1;
  350. // Calculate Sample volume
  351. volume = 100.0f;
  352. volume *= getEffectiveVolume(playing->m_audioEventRTS);
  353. dd->printf("%2d: %-24s - (%s) Volume: %d (Stream)\n", channel++, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT(volume));
  354. }
  355. for ( int i = channel; i <= channelCount; ++i) {
  356. dd->printf("%2d: Silence\n", i);
  357. }
  358. dd->printf("===========================================================\n");
  359. }
  360. if( fp )
  361. {
  362. fprintf( fp, "----------------------------------------------------Streams\n" );
  363. channelCount = TheAudio->getNumStreams();
  364. channel = 1;
  365. for( it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it )
  366. {
  367. playing = *it;
  368. if( !playing )
  369. {
  370. continue;
  371. }
  372. filenameNoSlashes = playing->m_audioEventRTS->getFilename();
  373. filenameNoSlashes = filenameNoSlashes.reverseFind('\\') + 1;
  374. // Calculate Sample volume
  375. volume = 100.0f;
  376. volume *= getEffectiveVolume(playing->m_audioEventRTS);
  377. fprintf( fp, "%2d: %-24s - (%s) Volume: %d (Stream)\n", channel++, playing->m_audioEventRTS->getEventName().str(), filenameNoSlashes.str(), REAL_TO_INT( volume ) );
  378. }
  379. for( int i = channel; i <= channelCount; ++i )
  380. {
  381. fprintf( fp, "%2d: Silence\n", i );
  382. }
  383. fprintf( fp, "===========================================================\n" );
  384. }
  385. }
  386. #endif
  387. //-------------------------------------------------------------------------------------------------
  388. void MilesAudioManager::init()
  389. {
  390. AudioManager::init();
  391. #ifdef INTENSE_DEBUG
  392. DEBUG_LOG(("Sound has temporarily been disabled in debug builds only. jkmcd\n"));
  393. // for now, _DEBUG builds only should have no sound. ask jkmcd or srj about this.
  394. return;
  395. #endif
  396. // We should now know how many samples we want to load
  397. openDevice();
  398. m_audioCache->setMaxSize(getAudioSettings()->m_maxCacheSize);
  399. // Now, set the file callbacks to load the streams from Biggie files
  400. AIL_set_file_callbacks(streamingFileOpen, streamingFileClose, streamingFileSeek, streamingFileRead);
  401. }
  402. //-------------------------------------------------------------------------------------------------
  403. void MilesAudioManager::postProcessLoad()
  404. {
  405. AudioManager::postProcessLoad();
  406. }
  407. //-------------------------------------------------------------------------------------------------
  408. void MilesAudioManager::reset()
  409. {
  410. #if defined(_DEBUG) || defined(_INTERNAL)
  411. dumpAllAssetsUsed();
  412. m_allEventsLoaded.clear();
  413. #endif
  414. AudioManager::reset();
  415. stopAllAudioImmediately();
  416. }
  417. //-------------------------------------------------------------------------------------------------
  418. void MilesAudioManager::update()
  419. {
  420. AudioManager::update();
  421. setDeviceListenerPosition();
  422. processRequestList();
  423. processPlayingList();
  424. processFadingList();
  425. processStoppedList();
  426. }
  427. //-------------------------------------------------------------------------------------------------
  428. void MilesAudioManager::stopAudio( AudioAffect which )
  429. {
  430. // All we really need to do is:
  431. // 1) Remove the EOS callback.
  432. // 2) Stop the sample, (so that when we later unload it, bad stuff doesn't happen)
  433. // 3) Set the status to stopped, so that when we next process the playing list, we will
  434. // correctly clean up the sample.
  435. std::list<PlayingAudio *>::iterator it;
  436. PlayingAudio *playing = NULL;
  437. if (BitTest(which, AudioAffect_Sound)) {
  438. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  439. playing = *it;
  440. if (playing) {
  441. AIL_register_EOS_callback(playing->m_sample, NULL);
  442. AIL_stop_sample(playing->m_sample);
  443. playing->m_status = PS_Stopped;
  444. }
  445. }
  446. }
  447. if (BitTest(which, AudioAffect_Sound3D)) {
  448. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  449. playing = *it;
  450. if (playing) {
  451. AIL_register_3D_EOS_callback(playing->m_3DSample, NULL);
  452. AIL_stop_3D_sample(playing->m_3DSample);
  453. playing->m_status = PS_Stopped;
  454. }
  455. }
  456. }
  457. if (BitTest(which, AudioAffect_Speech | AudioAffect_Music)) {
  458. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  459. playing = *it;
  460. if (playing) {
  461. if (playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  462. if (!BitTest(which, AudioAffect_Music)) {
  463. continue;
  464. }
  465. } else {
  466. if (!BitTest(which, AudioAffect_Speech)) {
  467. continue;
  468. }
  469. }
  470. AIL_register_stream_callback(playing->m_stream, NULL);
  471. AIL_pause_stream(playing->m_stream, 1);
  472. playing->m_status = PS_Stopped;
  473. }
  474. }
  475. }
  476. }
  477. //-------------------------------------------------------------------------------------------------
  478. void MilesAudioManager::pauseAudio( AudioAffect which )
  479. {
  480. std::list<PlayingAudio *>::iterator it;
  481. PlayingAudio *playing = NULL;
  482. if (BitTest(which, AudioAffect_Sound)) {
  483. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  484. playing = *it;
  485. if (playing) {
  486. AIL_stop_sample(playing->m_sample);
  487. }
  488. }
  489. }
  490. if (BitTest(which, AudioAffect_Sound3D)) {
  491. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  492. playing = *it;
  493. if (playing) {
  494. AIL_stop_3D_sample(playing->m_3DSample);
  495. }
  496. }
  497. }
  498. if (BitTest(which, AudioAffect_Speech | AudioAffect_Music)) {
  499. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  500. playing = *it;
  501. if (playing) {
  502. if (playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  503. if (!BitTest(which, AudioAffect_Music)) {
  504. continue;
  505. }
  506. } else {
  507. if (!BitTest(which, AudioAffect_Speech)) {
  508. continue;
  509. }
  510. }
  511. AIL_pause_stream(playing->m_stream, 1);
  512. }
  513. }
  514. }
  515. //Get rid of PLAY audio requests when pausing audio.
  516. std::list<AudioRequest*>::iterator ait;
  517. for (ait = m_audioRequests.begin(); ait != m_audioRequests.end(); /* empty */)
  518. {
  519. AudioRequest *req = (*ait);
  520. if( req && req->m_request == AR_Play )
  521. {
  522. req->deleteInstance();
  523. ait = m_audioRequests.erase(ait);
  524. }
  525. else
  526. {
  527. ait++;
  528. }
  529. }
  530. }
  531. //-------------------------------------------------------------------------------------------------
  532. void MilesAudioManager::resumeAudio( AudioAffect which )
  533. {
  534. std::list<PlayingAudio *>::iterator it;
  535. PlayingAudio *playing = NULL;
  536. if (BitTest(which, AudioAffect_Sound)) {
  537. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  538. playing = *it;
  539. if (playing) {
  540. AIL_resume_sample(playing->m_sample);
  541. }
  542. }
  543. }
  544. if (BitTest(which, AudioAffect_Sound3D)) {
  545. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  546. playing = *it;
  547. if (playing) {
  548. AIL_resume_3D_sample(playing->m_3DSample);
  549. }
  550. }
  551. }
  552. if (BitTest(which, AudioAffect_Speech | AudioAffect_Music)) {
  553. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  554. playing = *it;
  555. if (playing) {
  556. if (playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  557. if (!BitTest(which, AudioAffect_Music)) {
  558. continue;
  559. }
  560. } else {
  561. if (!BitTest(which, AudioAffect_Speech)) {
  562. continue;
  563. }
  564. }
  565. AIL_pause_stream(playing->m_stream, 0);
  566. }
  567. }
  568. }
  569. }
  570. //-------------------------------------------------------------------------------------------------
  571. void MilesAudioManager::pauseAmbient( Bool shouldPause )
  572. {
  573. }
  574. //-------------------------------------------------------------------------------------------------
  575. void MilesAudioManager::stopAllAmbientsBy( Object *obj )
  576. {
  577. }
  578. //-------------------------------------------------------------------------------------------------
  579. void MilesAudioManager::stopAllAmbientsBy( Drawable *draw )
  580. {
  581. }
  582. //-------------------------------------------------------------------------------------------------
  583. void MilesAudioManager::playAudioEvent( AudioEventRTS *event )
  584. {
  585. #ifdef INTENSIVE_AUDIO_DEBUG
  586. DEBUG_LOG(("MILES (%d) - Processing play request: %d (%s)", TheGameLogic->getFrame(), event->getPlayingHandle(), event->getEventName().str()));
  587. #endif
  588. const AudioEventInfo *info = event->getAudioEventInfo();
  589. if (!info) {
  590. return;
  591. }
  592. std::list<PlayingAudio *>::iterator it;
  593. PlayingAudio *playing = NULL;
  594. AudioHandle handleToKill = event->getHandleToKill();
  595. AsciiString fileToPlay = event->getFilename();
  596. PlayingAudio *audio = allocatePlayingAudio();
  597. switch(info->m_soundType)
  598. {
  599. case AT_Music:
  600. case AT_Streaming:
  601. {
  602. #ifdef INTENSIVE_AUDIO_DEBUG
  603. DEBUG_LOG(("- Stream\n"));
  604. #endif
  605. if ((info->m_soundType == AT_Streaming) && event->getUninterruptable()) {
  606. stopAllSpeech();
  607. }
  608. Real curVolume = 1.0;
  609. if (info->m_soundType == AT_Music) {
  610. curVolume = m_musicVolume;
  611. } else {
  612. curVolume = m_speechVolume;
  613. }
  614. curVolume *= event->getVolume();
  615. Bool foundSoundToReplace = false;
  616. if (handleToKill) {
  617. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  618. playing = (*it);
  619. if (!playing) {
  620. continue;
  621. }
  622. if (playing->m_audioEventRTS && playing->m_audioEventRTS->getPlayingHandle() == handleToKill)
  623. {
  624. //Release this streaming channel immediately because we are going to play another sound in it's place.
  625. releasePlayingAudio(playing);
  626. m_playingStreams.erase(it);
  627. foundSoundToReplace = true;
  628. break;
  629. }
  630. }
  631. }
  632. HSTREAM stream;
  633. if (!handleToKill || foundSoundToReplace) {
  634. stream = AIL_open_stream(m_digitalHandle, fileToPlay.str(), 0);
  635. } else {
  636. stream = NULL;
  637. }
  638. // Put this on here, so that the audio event RTS will be cleaned up regardless.
  639. audio->m_audioEventRTS = event;
  640. audio->m_stream = stream;
  641. audio->m_type = PAT_Stream;
  642. if (stream) {
  643. if ((info->m_soundType == AT_Streaming) && event->getUninterruptable()) {
  644. setDisallowSpeech(TRUE);
  645. }
  646. AIL_set_stream_volume_pan(stream, curVolume, 0.5f);
  647. playStream(event, stream);
  648. m_playingStreams.push_back(audio);
  649. audio = NULL;
  650. }
  651. break;
  652. }
  653. case AT_SoundEffect:
  654. {
  655. #ifdef INTENSIVE_AUDIO_DEBUG
  656. DEBUG_LOG(("- Sound"));
  657. #endif
  658. if (event->isPositionalAudio()) {
  659. // Sounds that are non-global are positional 3-D sounds. Deal with them accordingly
  660. #ifdef INTENSIVE_AUDIO_DEBUG
  661. DEBUG_LOG((" Positional"));
  662. #endif
  663. Bool foundSoundToReplace = false;
  664. if (handleToKill)
  665. {
  666. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  667. playing = (*it);
  668. if (!playing) {
  669. continue;
  670. }
  671. if( playing->m_audioEventRTS && playing->m_audioEventRTS->getPlayingHandle() == handleToKill )
  672. {
  673. //Release this 3D sound channel immediately because we are going to play another sound in it's place.
  674. releasePlayingAudio(playing);
  675. m_playing3DSounds.erase(it);
  676. foundSoundToReplace = true;
  677. break;
  678. }
  679. }
  680. }
  681. H3DSAMPLE sample3D;
  682. if( !handleToKill || foundSoundToReplace )
  683. {
  684. sample3D = getFirst3DSample( event );
  685. if( !sample3D )
  686. {
  687. //If we don't have an available sample, kill the lowest priority assuming we have one that is lower
  688. //than the sound we are trying to add. One possibility for strangeness is when an interrupt sound
  689. //that wants to kill a handle to replace it, it's possible that another request already killed it,
  690. //in which case we need to attempt to find another sound to kill.
  691. if( killLowestPrioritySoundImmediately( event ) )
  692. {
  693. sample3D = getFirst3DSample( event );
  694. }
  695. }
  696. }
  697. else
  698. {
  699. sample3D = NULL;
  700. }
  701. // Push it onto the list of playing things
  702. audio->m_audioEventRTS = event;
  703. audio->m_3DSample = sample3D;
  704. audio->m_file = NULL;
  705. audio->m_type = PAT_3DSample;
  706. m_playing3DSounds.push_back(audio);
  707. if (sample3D) {
  708. audio->m_file = playSample3D(event, sample3D);
  709. m_sound->notifyOf3DSampleStart();
  710. }
  711. if( !audio->m_file )
  712. {
  713. m_playing3DSounds.pop_back();
  714. #ifdef INTENSIVE_AUDIO_DEBUG
  715. DEBUG_LOG((" Killed (no handles available)\n"));
  716. #endif
  717. }
  718. else
  719. {
  720. audio = NULL;
  721. #ifdef INTENSIVE_AUDIO_DEBUG
  722. DEBUG_LOG((" Playing.\n"));
  723. #endif
  724. }
  725. }
  726. else
  727. {
  728. // UI sounds are always 2-D. All other sounds should be Positional
  729. // Unit acknowledgement, etc, falls into the UI category of sound.
  730. Bool foundSoundToReplace = false;
  731. if (handleToKill) {
  732. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  733. playing = (*it);
  734. if (!playing) {
  735. continue;
  736. }
  737. if (playing->m_audioEventRTS && playing->m_audioEventRTS->getPlayingHandle() == handleToKill)
  738. {
  739. //Release this 2D sound channel immediately because we are going to play another sound in it's place.
  740. releasePlayingAudio(playing);
  741. m_playingSounds.erase(it);
  742. foundSoundToReplace = true;
  743. break;
  744. }
  745. }
  746. }
  747. HSAMPLE sample;
  748. if( !handleToKill || foundSoundToReplace )
  749. {
  750. sample = getFirst2DSample(event);
  751. if( !sample )
  752. {
  753. //If we don't have an available sample, kill the lowest priority assuming we have one that is lower
  754. //than the sound we are trying to add. One possibility for strangeness is when an interrupt sound
  755. //that wants to kill a handle to replace it, it's possible that another request already killed it,
  756. //in which case we need to attempt to find another sound to kill.
  757. if( killLowestPrioritySoundImmediately( event ) )
  758. {
  759. sample = getFirst2DSample( event );
  760. }
  761. }
  762. }
  763. else
  764. {
  765. sample = NULL;
  766. }
  767. // Push it onto the list of playing things
  768. audio->m_audioEventRTS = event;
  769. audio->m_sample = sample;
  770. audio->m_file = NULL;
  771. audio->m_type = PAT_Sample;
  772. m_playingSounds.push_back(audio);
  773. if (sample) {
  774. audio->m_file = playSample(event, sample);
  775. m_sound->notifyOf2DSampleStart();
  776. }
  777. if (!audio->m_file) {
  778. #ifdef INTENSIVE_AUDIO_DEBUG
  779. DEBUG_LOG((" Killed (no handles available)\n"));
  780. #endif
  781. m_playingSounds.pop_back();
  782. } else {
  783. audio = NULL;
  784. }
  785. #ifdef INTENSIVE_AUDIO_DEBUG
  786. DEBUG_LOG((" Playing.\n"));
  787. #endif
  788. }
  789. break;
  790. }
  791. }
  792. // If we were able to successfully play audio, then we set it to NULL above. (And it will be freed
  793. // later. However, if audio is non-NULL at this point, then it must be freed.
  794. if (audio) {
  795. releasePlayingAudio(audio);
  796. }
  797. }
  798. //-------------------------------------------------------------------------------------------------
  799. void MilesAudioManager::stopAudioEvent( AudioHandle handle )
  800. {
  801. #ifdef INTENSIVE_AUDIO_DEBUG
  802. DEBUG_LOG(("MILES (%d) - Processing stop request: %d\n", TheGameLogic->getFrame(), handle));
  803. #endif
  804. std::list<PlayingAudio *>::iterator it;
  805. if ( handle == AHSV_StopTheMusic || handle == AHSV_StopTheMusicFade ) {
  806. // for music, just find the currently playing music stream and kill it.
  807. for ( it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it ) {
  808. PlayingAudio *audio = (*it);
  809. if (!audio) {
  810. continue;
  811. }
  812. if( audio->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music )
  813. {
  814. if( handle == AHSV_StopTheMusicFade )
  815. {
  816. m_fadingAudio.push_back(audio);
  817. }
  818. else
  819. {
  820. //m_stoppedAudio.push_back(audio);
  821. releasePlayingAudio( audio );
  822. }
  823. m_playingStreams.erase(it);
  824. break;
  825. }
  826. }
  827. }
  828. for ( it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it ) {
  829. PlayingAudio *audio = (*it);
  830. if (!audio) {
  831. continue;
  832. }
  833. if (audio->m_audioEventRTS->getPlayingHandle() == handle) {
  834. // found it
  835. audio->m_requestStop = true;
  836. notifyOfAudioCompletion((UnsignedInt)(audio->m_stream), PAT_Stream);
  837. break;
  838. }
  839. }
  840. for ( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it ) {
  841. PlayingAudio *audio = (*it);
  842. if (!audio) {
  843. continue;
  844. }
  845. if (audio->m_audioEventRTS->getPlayingHandle() == handle) {
  846. audio->m_requestStop = true;
  847. break;
  848. }
  849. }
  850. for ( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it ) {
  851. PlayingAudio *audio = (*it);
  852. if (!audio) {
  853. continue;
  854. }
  855. if (audio->m_audioEventRTS->getPlayingHandle() == handle) {
  856. #ifdef INTENSIVE_AUDIO_DEBUG
  857. DEBUG_LOG((" (%s)\n", audio->m_audioEventRTS->getEventName()));
  858. #endif
  859. audio->m_requestStop = true;
  860. break;
  861. }
  862. }
  863. }
  864. //-------------------------------------------------------------------------------------------------
  865. void MilesAudioManager::killAudioEventImmediately( AudioHandle audioEvent )
  866. {
  867. //First look for it in the request list.
  868. std::list<AudioRequest*>::iterator ait;
  869. for( ait = m_audioRequests.begin(); ait != m_audioRequests.end(); ait++ )
  870. {
  871. AudioRequest *req = (*ait);
  872. if( req && req->m_request == AR_Play && req->m_handleToInteractOn == audioEvent )
  873. {
  874. req->deleteInstance();
  875. ait = m_audioRequests.erase(ait);
  876. return;
  877. }
  878. }
  879. //Look for matching 3D sound to kill
  880. std::list<PlayingAudio *>::iterator it;
  881. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); it++ )
  882. {
  883. PlayingAudio *audio = (*it);
  884. if( !audio )
  885. {
  886. continue;
  887. }
  888. if( audio->m_audioEventRTS->getPlayingHandle() == audioEvent )
  889. {
  890. releasePlayingAudio( audio );
  891. m_playing3DSounds.erase( it );
  892. return;
  893. }
  894. }
  895. //Look for matching 2D sound to kill
  896. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); it++ )
  897. {
  898. PlayingAudio *audio = (*it);
  899. if( !audio )
  900. {
  901. continue;
  902. }
  903. if( audio->m_audioEventRTS->getPlayingHandle() == audioEvent )
  904. {
  905. releasePlayingAudio( audio );
  906. m_playingSounds.erase( it );
  907. return;
  908. }
  909. }
  910. //Look for matching steaming sound to kill
  911. for( it = m_playingStreams.begin(); it != m_playingStreams.end(); it++ )
  912. {
  913. PlayingAudio *audio = (*it);
  914. if( !audio )
  915. {
  916. continue;
  917. }
  918. if( audio->m_audioEventRTS->getPlayingHandle() == audioEvent )
  919. {
  920. releasePlayingAudio( audio );
  921. m_playingStreams.erase( it );
  922. return;
  923. }
  924. }
  925. }
  926. //-------------------------------------------------------------------------------------------------
  927. void MilesAudioManager::pauseAudioEvent( AudioHandle handle )
  928. {
  929. // pause audio
  930. }
  931. //-------------------------------------------------------------------------------------------------
  932. void *MilesAudioManager::loadFileForRead( AudioEventRTS *eventToLoadFrom )
  933. {
  934. return m_audioCache->openFile(eventToLoadFrom);
  935. }
  936. //-------------------------------------------------------------------------------------------------
  937. void MilesAudioManager::closeFile( void *fileRead )
  938. {
  939. m_audioCache->closeFile(fileRead);
  940. }
  941. //-------------------------------------------------------------------------------------------------
  942. PlayingAudio *MilesAudioManager::allocatePlayingAudio( void )
  943. {
  944. PlayingAudio *aud = NEW PlayingAudio; // poolify
  945. aud->m_status = PS_Playing;
  946. return aud;
  947. }
  948. //-------------------------------------------------------------------------------------------------
  949. void MilesAudioManager::releaseMilesHandles( PlayingAudio *release )
  950. {
  951. switch (release->m_type)
  952. {
  953. case PAT_Sample:
  954. {
  955. if (release->m_sample) {
  956. AIL_register_EOS_callback(release->m_sample, NULL);
  957. AIL_stop_sample(release->m_sample);
  958. m_availableSamples.push_back(release->m_sample);
  959. }
  960. break;
  961. }
  962. case PAT_3DSample:
  963. {
  964. if (release->m_3DSample) {
  965. AIL_register_3D_EOS_callback(release->m_3DSample, NULL);
  966. AIL_stop_3D_sample(release->m_3DSample);
  967. m_available3DSamples.push_back(release->m_3DSample);
  968. }
  969. break;
  970. }
  971. case PAT_Stream:
  972. {
  973. if (release->m_stream) {
  974. AIL_register_stream_callback(release->m_stream, NULL);
  975. AIL_close_stream(release->m_stream);
  976. }
  977. break;
  978. }
  979. }
  980. release->m_type = PAT_INVALID;
  981. }
  982. //-------------------------------------------------------------------------------------------------
  983. void MilesAudioManager::releasePlayingAudio( PlayingAudio *release )
  984. {
  985. if (release->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_SoundEffect) {
  986. if (release->m_type == PAT_Sample) {
  987. if (release->m_sample) {
  988. m_sound->notifyOf2DSampleCompletion();
  989. }
  990. } else {
  991. if (release->m_3DSample) {
  992. m_sound->notifyOf3DSampleCompletion();
  993. }
  994. }
  995. }
  996. releaseMilesHandles(release); // forces stop of this audio
  997. closeFile( release->m_file );
  998. if (release->m_cleanupAudioEventRTS) {
  999. releaseAudioEventRTS(release->m_audioEventRTS);
  1000. }
  1001. delete release;
  1002. release = NULL;
  1003. }
  1004. //-------------------------------------------------------------------------------------------------
  1005. void MilesAudioManager::stopAllAudioImmediately( void )
  1006. {
  1007. std::list<PlayingAudio *>::iterator it;
  1008. PlayingAudio *playing;
  1009. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ) {
  1010. playing = *it;
  1011. if (!playing) {
  1012. continue;
  1013. }
  1014. releasePlayingAudio(playing);
  1015. it = m_playingSounds.erase(it);
  1016. }
  1017. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ) {
  1018. playing = *it;
  1019. if (!playing) {
  1020. continue;
  1021. }
  1022. releasePlayingAudio(playing);
  1023. it = m_playing3DSounds.erase(it);
  1024. }
  1025. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ) {
  1026. playing = (*it);
  1027. if (!playing) {
  1028. continue;
  1029. }
  1030. releasePlayingAudio(playing);
  1031. it = m_playingStreams.erase(it);
  1032. }
  1033. std::list<HAUDIO>::iterator hit;
  1034. for (hit = m_audioForcePlayed.begin(); hit != m_audioForcePlayed.end(); ++hit) {
  1035. if (*hit) {
  1036. AIL_quick_unload(*hit);
  1037. }
  1038. }
  1039. m_audioForcePlayed.clear();
  1040. }
  1041. //-------------------------------------------------------------------------------------------------
  1042. void MilesAudioManager::freeAllMilesHandles( void )
  1043. {
  1044. // First, we need to ensure that we don't have any sample handles open. To that end, we must stop
  1045. // all of our currently playing audio.
  1046. stopAllAudioImmediately();
  1047. // Walks through the available 2-D and 3-D handles and releases them
  1048. std::list<HSAMPLE>::iterator it;
  1049. for ( it = m_availableSamples.begin(); it != m_availableSamples.end(); /* empty */ ) {
  1050. HSAMPLE sample = *it;
  1051. AIL_release_sample_handle(sample);
  1052. it = m_availableSamples.erase(it);
  1053. }
  1054. m_num2DSamples = 0;
  1055. std::list<H3DSAMPLE>::iterator it3D;
  1056. for ( it3D = m_available3DSamples.begin(); it3D != m_available3DSamples.end(); /* empty */ ) {
  1057. H3DSAMPLE sample3D = *it3D;
  1058. AIL_release_3D_sample_handle(sample3D);
  1059. it3D = m_available3DSamples.erase(it3D);
  1060. }
  1061. m_num3DSamples = 0;
  1062. m_numStreams = 0;
  1063. }
  1064. //-------------------------------------------------------------------------------------------------
  1065. HSAMPLE MilesAudioManager::getFirst2DSample( AudioEventRTS *event )
  1066. {
  1067. if (m_availableSamples.begin() != m_availableSamples.end()) {
  1068. HSAMPLE retSample = *m_availableSamples.begin();
  1069. m_availableSamples.erase(m_availableSamples.begin());
  1070. return (retSample);
  1071. }
  1072. // Find the first sample of lower priority than my augmented priority that is interruptable and take its handle
  1073. return NULL;
  1074. }
  1075. //-------------------------------------------------------------------------------------------------
  1076. H3DSAMPLE MilesAudioManager::getFirst3DSample( AudioEventRTS *event )
  1077. {
  1078. if (m_available3DSamples.begin() != m_available3DSamples.end()) {
  1079. H3DSAMPLE retSample = *m_available3DSamples.begin();
  1080. m_available3DSamples.erase(m_available3DSamples.begin());
  1081. return (retSample);
  1082. }
  1083. // Find the first sample of lower priority than my augmented priority that is interruptable and take its handle
  1084. return NULL;
  1085. }
  1086. //-------------------------------------------------------------------------------------------------
  1087. void MilesAudioManager::adjustPlayingVolume( PlayingAudio *audio )
  1088. {
  1089. Real desiredVolume = audio->m_audioEventRTS->getVolume() * audio->m_audioEventRTS->getVolumeShift();
  1090. Real pan;
  1091. if (audio->m_type == PAT_Sample) {
  1092. AIL_sample_volume_pan(audio->m_sample, NULL, &pan);
  1093. AIL_set_sample_volume_pan(audio->m_sample, m_soundVolume * desiredVolume, pan);
  1094. } else if (audio->m_type == PAT_3DSample) {
  1095. AIL_set_3D_sample_volume(audio->m_3DSample, m_sound3DVolume * desiredVolume);
  1096. } else if (audio->m_type == PAT_Stream) {
  1097. AIL_stream_volume_pan(audio->m_stream, NULL, &pan);
  1098. if (audio->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music ) {
  1099. AIL_set_stream_volume_pan(audio->m_stream, m_musicVolume * desiredVolume, pan);
  1100. } else {
  1101. AIL_set_stream_volume_pan(audio->m_stream, m_speechVolume * desiredVolume, pan);
  1102. }
  1103. }
  1104. }
  1105. //-------------------------------------------------------------------------------------------------
  1106. void MilesAudioManager::stopAllSpeech( void )
  1107. {
  1108. std::list<PlayingAudio *>::iterator it;
  1109. PlayingAudio *playing;
  1110. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ) {
  1111. playing = (*it);
  1112. if (!playing) {
  1113. continue;
  1114. }
  1115. if (playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Streaming) {
  1116. releasePlayingAudio(playing);
  1117. it = m_playingStreams.erase(it);
  1118. } else {
  1119. ++it;
  1120. }
  1121. }
  1122. }
  1123. //-------------------------------------------------------------------------------------------------
  1124. void MilesAudioManager::initFilters( HSAMPLE sample, const AudioEventRTS *event )
  1125. {
  1126. // set the sample volume
  1127. Real volume = event->getVolume() * event->getVolumeShift() * m_soundVolume;
  1128. AIL_set_sample_volume_pan(sample, volume, 0.5f);
  1129. // pitch shift
  1130. Real pitchShift = event->getPitchShift();
  1131. if (pitchShift == 0.0f) {
  1132. DEBUG_CRASH(("Invalid Pitch shift in sound: '%s'", event->getEventName().str()) );
  1133. } else {
  1134. AIL_set_sample_playback_rate(sample, REAL_TO_INT(AIL_sample_playback_rate(sample) * pitchShift));
  1135. }
  1136. // set up delay filter, if applicable
  1137. if (event->getDelay() > 0.0f) {
  1138. Real value;
  1139. value = event->getDelay();
  1140. AIL_set_sample_processor(sample, DP_FILTER, m_delayFilter);
  1141. AIL_set_filter_sample_preference(sample, "Mono Delay Time", &value);
  1142. value = 0.0;
  1143. AIL_set_filter_sample_preference(sample, "Mono Delay", &value);
  1144. AIL_set_filter_sample_preference(sample, "Mono Delay Mix", &value);
  1145. }
  1146. }
  1147. //-------------------------------------------------------------------------------------------------
  1148. void MilesAudioManager::initFilters3D( H3DSAMPLE sample, const AudioEventRTS *event, const Coord3D *pos )
  1149. {
  1150. // set the sample volume
  1151. Real volume = event->getVolume() * event->getVolumeShift() * m_sound3DVolume;
  1152. AIL_set_3D_sample_volume(sample, volume);
  1153. // pitch shift
  1154. Real pitchShift = event->getPitchShift();
  1155. if (pitchShift == 0.0f) {
  1156. DEBUG_CRASH(("Invalid Pitch shift in sound: '%s'", event->getEventName().str()) );
  1157. } else {
  1158. AIL_set_3D_sample_playback_rate(sample, REAL_TO_INT(AIL_3D_sample_playback_rate(sample) * pitchShift));
  1159. }
  1160. // Low pass filter
  1161. if (event->getAudioEventInfo()->m_lowPassFreq > 0 && !isOnScreen(pos) ) {
  1162. AIL_set_3D_sample_occlusion(sample, 1.0f - event->getAudioEventInfo()->m_lowPassFreq);
  1163. }
  1164. }
  1165. //-------------------------------------------------------------------------------------------------
  1166. void MilesAudioManager::nextMusicTrack( void )
  1167. {
  1168. AsciiString trackName;
  1169. std::list<PlayingAudio *>::iterator it;
  1170. PlayingAudio *playing;
  1171. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1172. playing = *it;
  1173. if (playing && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1174. trackName = playing->m_audioEventRTS->getEventName();
  1175. }
  1176. }
  1177. // Stop currently playing music
  1178. TheAudio->removeAudioEvent(AHSV_StopTheMusic);
  1179. trackName = nextTrackName(trackName);
  1180. AudioEventRTS newTrack(trackName);
  1181. TheAudio->addAudioEvent(&newTrack);
  1182. }
  1183. //-------------------------------------------------------------------------------------------------
  1184. void MilesAudioManager::prevMusicTrack( void )
  1185. {
  1186. AsciiString trackName;
  1187. std::list<PlayingAudio *>::iterator it;
  1188. PlayingAudio *playing;
  1189. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1190. playing = *it;
  1191. if (playing && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1192. trackName = playing->m_audioEventRTS->getEventName();
  1193. }
  1194. }
  1195. // Stop currently playing music
  1196. TheAudio->removeAudioEvent(AHSV_StopTheMusic);
  1197. trackName = prevTrackName(trackName);
  1198. AudioEventRTS newTrack(trackName);
  1199. TheAudio->addAudioEvent(&newTrack);
  1200. }
  1201. //-------------------------------------------------------------------------------------------------
  1202. Bool MilesAudioManager::isMusicPlaying( void ) const
  1203. {
  1204. std::list<PlayingAudio *>::const_iterator it;
  1205. PlayingAudio *playing;
  1206. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1207. playing = *it;
  1208. if (playing && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1209. return TRUE;
  1210. }
  1211. }
  1212. return FALSE;
  1213. }
  1214. //-------------------------------------------------------------------------------------------------
  1215. Bool MilesAudioManager::hasMusicTrackCompleted( const AsciiString& trackName, Int numberOfTimes ) const
  1216. {
  1217. std::list<PlayingAudio *>::const_iterator it;
  1218. PlayingAudio *playing;
  1219. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1220. playing = *it;
  1221. if (playing && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1222. if (playing->m_audioEventRTS->getEventName() == trackName) {
  1223. if (INFINITE_LOOP_COUNT - AIL_stream_loop_count(playing->m_stream) >= numberOfTimes) {
  1224. return TRUE;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. return FALSE;
  1230. }
  1231. //-------------------------------------------------------------------------------------------------
  1232. AsciiString MilesAudioManager::getMusicTrackName( void ) const
  1233. {
  1234. // First check the requests. If there's one there, then report that as the currently playing track.
  1235. std::list<AudioRequest *>::const_iterator ait;
  1236. for (ait = m_audioRequests.begin(); ait != m_audioRequests.end(); ++ait) {
  1237. if ((*ait)->m_request != AR_Play) {
  1238. continue;
  1239. }
  1240. if (!(*ait)->m_usePendingEvent) {
  1241. continue;
  1242. }
  1243. if ((*ait)->m_pendingEvent->getAudioEventInfo()->m_soundType == AT_Music) {
  1244. return (*ait)->m_pendingEvent->getEventName();
  1245. }
  1246. }
  1247. std::list<PlayingAudio *>::const_iterator it;
  1248. PlayingAudio *playing;
  1249. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1250. playing = *it;
  1251. if (playing && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1252. return playing->m_audioEventRTS->getEventName();
  1253. }
  1254. }
  1255. return AsciiString::TheEmptyString;
  1256. }
  1257. //-------------------------------------------------------------------------------------------------
  1258. void MilesAudioManager::openDevice( void )
  1259. {
  1260. if (!TheGlobalData->m_audioOn) {
  1261. return;
  1262. }
  1263. AIL_set_redist_directory("MSS\\");
  1264. AIL_startup();
  1265. Int retval = 0;
  1266. // AIL_quick_startup should be replaced later with a call to actually pick which device to use, etc
  1267. const AudioSettings *audioSettings = getAudioSettings();
  1268. m_selectedSpeakerType = TheAudio->translateSpeakerTypeToUnsignedInt(m_prefSpeaker);
  1269. retval = AIL_quick_startup(audioSettings->m_useDigital, audioSettings->m_useMidi, audioSettings->m_outputRate, audioSettings->m_outputBits, audioSettings->m_outputChannels);
  1270. // Quick handles tells us where to store the various devices. For now, we're only interested in the digital handle.
  1271. AIL_quick_handles(&m_digitalHandle, NULL, NULL);
  1272. if (retval) {
  1273. buildProviderList();
  1274. } else {
  1275. // if we couldn't initialize any devices, turn sound off (fail silently)
  1276. setOn( false, AudioAffect_All );
  1277. }
  1278. selectProvider(TheAudio->getProviderIndex(m_pref3DProvider));
  1279. // Now that we're all done, update the cached variables so that everything is in sync.
  1280. TheAudio->refreshCachedVariables();
  1281. if (!isValidProvider()) {
  1282. return;
  1283. }
  1284. initDelayFilter();
  1285. }
  1286. //-------------------------------------------------------------------------------------------------
  1287. void MilesAudioManager::closeDevice( void )
  1288. {
  1289. freeAllMilesHandles();
  1290. unselectProvider();
  1291. AIL_shutdown();
  1292. }
  1293. //-------------------------------------------------------------------------------------------------
  1294. Bool MilesAudioManager::isCurrentlyPlaying( AudioHandle handle )
  1295. {
  1296. std::list<PlayingAudio *>::iterator it;
  1297. PlayingAudio *playing;
  1298. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  1299. playing = *it;
  1300. if (playing && playing->m_audioEventRTS->getPlayingHandle() == handle) {
  1301. return true;
  1302. }
  1303. }
  1304. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  1305. playing = *it;
  1306. if (playing && playing->m_audioEventRTS->getPlayingHandle() == handle) {
  1307. return true;
  1308. }
  1309. }
  1310. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1311. playing = *it;
  1312. if (playing && playing->m_audioEventRTS->getPlayingHandle() == handle) {
  1313. return true;
  1314. }
  1315. }
  1316. // if something is requested, it is also considered playing
  1317. std::list<AudioRequest *>::iterator ait;
  1318. AudioRequest *req = NULL;
  1319. for (ait = m_audioRequests.begin(); ait != m_audioRequests.end(); ++ait) {
  1320. req = *ait;
  1321. if (req && req->m_usePendingEvent && req->m_pendingEvent->getPlayingHandle() == handle) {
  1322. return true;
  1323. }
  1324. }
  1325. return false;
  1326. }
  1327. //-------------------------------------------------------------------------------------------------
  1328. void MilesAudioManager::notifyOfAudioCompletion( UnsignedInt audioCompleted, UnsignedInt flags )
  1329. {
  1330. PlayingAudio *playing = findPlayingAudioFrom(audioCompleted, flags);
  1331. if (!playing) {
  1332. DEBUG_CRASH(("Audio has completed playing, but we can't seem to find it. - jkmcd"));
  1333. return;
  1334. }
  1335. if (getDisallowSpeech() && playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Streaming) {
  1336. setDisallowSpeech(FALSE);
  1337. }
  1338. if (playing->m_audioEventRTS->getAudioEventInfo()->m_control & AC_LOOP) {
  1339. if (playing->m_audioEventRTS->getNextPlayPortion() == PP_Attack) {
  1340. playing->m_audioEventRTS->setNextPlayPortion(PP_Sound);
  1341. }
  1342. if (playing->m_audioEventRTS->getNextPlayPortion() == PP_Sound) {
  1343. // First, decrease the loop count.
  1344. playing->m_audioEventRTS->decreaseLoopCount();
  1345. // Now, try to start the next loop
  1346. if (startNextLoop(playing)) {
  1347. return;
  1348. }
  1349. }
  1350. }
  1351. playing->m_audioEventRTS->advanceNextPlayPortion();
  1352. if (playing->m_audioEventRTS->getNextPlayPortion() != PP_Done) {
  1353. if (playing->m_type == PAT_Sample) {
  1354. closeFile(playing->m_file); // close it so as not to leak it.
  1355. playing->m_file = playSample(playing->m_audioEventRTS, playing->m_sample);
  1356. // If we don't have a file now, then we should drop to the stopped status so that
  1357. // We correctly close this handle.
  1358. if (playing->m_file) {
  1359. return;
  1360. }
  1361. } else if (playing->m_type == PAT_3DSample) {
  1362. closeFile(playing->m_file); // close it so as not to leak it.
  1363. playing->m_file = playSample3D(playing->m_audioEventRTS, playing->m_3DSample);
  1364. // If we don't have a file now, then we should drop to the stopped status so that
  1365. // We correctly close this handle.
  1366. if (playing->m_file) {
  1367. return;
  1368. }
  1369. }
  1370. }
  1371. if (playing->m_type == PAT_Stream) {
  1372. if (playing->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_Music) {
  1373. playStream(playing->m_audioEventRTS, playing->m_stream);
  1374. return;
  1375. }
  1376. }
  1377. playing->m_status = PS_Stopped; // it will be cleaned up on the next frame update
  1378. }
  1379. //-------------------------------------------------------------------------------------------------
  1380. PlayingAudio *MilesAudioManager::findPlayingAudioFrom( UnsignedInt audioCompleted, UnsignedInt flags )
  1381. {
  1382. std::list<PlayingAudio *>::iterator it;
  1383. PlayingAudio *playing;
  1384. if (flags == PAT_Sample) {
  1385. HSAMPLE sample = (HSAMPLE) audioCompleted;
  1386. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  1387. playing = *it;
  1388. if (playing && playing->m_sample == sample) {
  1389. return playing;
  1390. }
  1391. }
  1392. }
  1393. if (flags == PAT_3DSample) {
  1394. H3DSAMPLE sample3D = (H3DSAMPLE) audioCompleted;
  1395. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  1396. playing = *it;
  1397. if (playing && playing->m_3DSample == sample3D) {
  1398. return playing;
  1399. }
  1400. }
  1401. }
  1402. if (flags == PAT_Stream) {
  1403. HSTREAM stream = (HSTREAM) audioCompleted;
  1404. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1405. playing = *it;
  1406. if (playing && playing->m_stream == stream) {
  1407. return playing;
  1408. }
  1409. }
  1410. }
  1411. return NULL;
  1412. }
  1413. //-------------------------------------------------------------------------------------------------
  1414. UnsignedInt MilesAudioManager::getProviderCount( void ) const
  1415. {
  1416. return m_providerCount;
  1417. }
  1418. //-------------------------------------------------------------------------------------------------
  1419. AsciiString MilesAudioManager::getProviderName( UnsignedInt providerNum ) const
  1420. {
  1421. if (isOn(AudioAffect_Sound3D) && providerNum < m_providerCount) {
  1422. return m_provider3D[providerNum].name;
  1423. }
  1424. return AsciiString::TheEmptyString;
  1425. }
  1426. //-------------------------------------------------------------------------------------------------
  1427. UnsignedInt MilesAudioManager::getProviderIndex( AsciiString providerName ) const
  1428. {
  1429. for (UnsignedInt i = 0; i < m_providerCount; ++i) {
  1430. if (providerName == m_provider3D[i].name) {
  1431. return i;
  1432. }
  1433. }
  1434. return PROVIDER_ERROR;
  1435. }
  1436. //-------------------------------------------------------------------------------------------------
  1437. void MilesAudioManager::selectProvider( UnsignedInt providerNdx )
  1438. {
  1439. if (!isOn(AudioAffect_Sound3D))
  1440. {
  1441. return;
  1442. }
  1443. if (providerNdx == m_selectedProvider)
  1444. {
  1445. return;
  1446. }
  1447. if (isValidProvider())
  1448. {
  1449. freeAllMilesHandles();
  1450. unselectProvider();
  1451. }
  1452. LPDIRECTSOUND lpDirectSoundInfo;
  1453. AIL_get_DirectSound_info( NULL, (void**)&lpDirectSoundInfo, NULL );
  1454. Bool useDolby = FALSE;
  1455. if( lpDirectSoundInfo )
  1456. {
  1457. DWORD speakerConfig;
  1458. lpDirectSoundInfo->GetSpeakerConfig( &speakerConfig );
  1459. switch( DSSPEAKER_CONFIG( speakerConfig ) )
  1460. {
  1461. case DSSPEAKER_DIRECTOUT:
  1462. m_selectedSpeakerType = AIL_3D_2_SPEAKER;
  1463. break;
  1464. case DSSPEAKER_MONO:
  1465. m_selectedSpeakerType = AIL_3D_2_SPEAKER;
  1466. break;
  1467. case DSSPEAKER_STEREO:
  1468. m_selectedSpeakerType = AIL_3D_2_SPEAKER;
  1469. break;
  1470. case DSSPEAKER_HEADPHONE:
  1471. m_selectedSpeakerType = AIL_3D_HEADPHONE;
  1472. useDolby = TRUE;
  1473. break;
  1474. case DSSPEAKER_QUAD:
  1475. m_selectedSpeakerType = AIL_3D_4_SPEAKER;
  1476. useDolby = TRUE;
  1477. break;
  1478. case DSSPEAKER_SURROUND:
  1479. m_selectedSpeakerType = AIL_3D_SURROUND;
  1480. useDolby = TRUE;
  1481. break;
  1482. case DSSPEAKER_5POINT1:
  1483. m_selectedSpeakerType = AIL_3D_51_SPEAKER;
  1484. useDolby = TRUE;
  1485. break;
  1486. case DSSPEAKER_7POINT1:
  1487. m_selectedSpeakerType = AIL_3D_71_SPEAKER;
  1488. useDolby = TRUE;
  1489. break;
  1490. }
  1491. }
  1492. Bool success = FALSE;
  1493. if( useDolby )
  1494. {
  1495. providerNdx = getProviderIndex( "Dolby Surround" );
  1496. }
  1497. else
  1498. {
  1499. providerNdx = getProviderIndex( "Miles Fast 2D Positional Audio" );
  1500. }
  1501. success = AIL_open_3D_provider( m_provider3D[providerNdx].id ) == 0;
  1502. //if (providerNdx < m_providerCount)
  1503. //{
  1504. // failed = AIL_open_3D_provider(m_provider3D[providerNdx].id);
  1505. //}
  1506. if( !success )
  1507. {
  1508. m_selectedProvider = PROVIDER_ERROR;
  1509. // try to select a failsafe
  1510. providerNdx = getProviderIndex( "Miles Fast 2D Positional Audio" );
  1511. success = AIL_open_3D_provider( m_provider3D[providerNdx].id ) == 0;
  1512. }
  1513. if ( success )
  1514. {
  1515. m_selectedProvider = providerNdx;
  1516. initSamplePools();
  1517. createListener();
  1518. setSpeakerType(m_selectedSpeakerType);
  1519. if (TheVideoPlayer)
  1520. {
  1521. TheVideoPlayer->notifyVideoPlayerOfNewProvider(TRUE);
  1522. }
  1523. }
  1524. }
  1525. //-------------------------------------------------------------------------------------------------
  1526. void MilesAudioManager::unselectProvider( void )
  1527. {
  1528. if (!(isOn(AudioAffect_Sound3D) && isValidProvider())) {
  1529. return;
  1530. }
  1531. if (TheVideoPlayer) {
  1532. TheVideoPlayer->notifyVideoPlayerOfNewProvider(FALSE);
  1533. }
  1534. AIL_close_3D_listener(m_listener);
  1535. m_listener = NULL;
  1536. AIL_close_3D_provider(m_provider3D[m_selectedProvider].id);
  1537. m_lastProvider = m_selectedProvider;
  1538. m_selectedProvider = PROVIDER_ERROR;
  1539. }
  1540. //-------------------------------------------------------------------------------------------------
  1541. UnsignedInt MilesAudioManager::getSelectedProvider( void ) const
  1542. {
  1543. return m_selectedProvider;
  1544. }
  1545. //-------------------------------------------------------------------------------------------------
  1546. void MilesAudioManager::setSpeakerType( UnsignedInt speakerType )
  1547. {
  1548. if (!isValidProvider()) {
  1549. return;
  1550. }
  1551. AIL_set_3D_speaker_type( m_provider3D[m_selectedProvider].id, speakerType );
  1552. m_selectedSpeakerType = speakerType;
  1553. }
  1554. //-------------------------------------------------------------------------------------------------
  1555. UnsignedInt MilesAudioManager::getSpeakerType( void )
  1556. {
  1557. if (!isValidProvider()) {
  1558. return 0;
  1559. }
  1560. return m_selectedSpeakerType;
  1561. }
  1562. //-------------------------------------------------------------------------------------------------
  1563. UnsignedInt MilesAudioManager::getNum2DSamples( void ) const
  1564. {
  1565. return m_num2DSamples;
  1566. }
  1567. //-------------------------------------------------------------------------------------------------
  1568. UnsignedInt MilesAudioManager::getNum3DSamples( void ) const
  1569. {
  1570. return m_num3DSamples;
  1571. }
  1572. //-------------------------------------------------------------------------------------------------
  1573. UnsignedInt MilesAudioManager::getNumStreams( void ) const
  1574. {
  1575. return m_numStreams;
  1576. }
  1577. //-------------------------------------------------------------------------------------------------
  1578. Bool MilesAudioManager::doesViolateLimit( AudioEventRTS *event ) const
  1579. {
  1580. Int limit = event->getAudioEventInfo()->m_limit;
  1581. if (limit == 0) {
  1582. return false;
  1583. }
  1584. Int totalCount = 0;
  1585. Int totalRequestCount = 0;
  1586. std::list<PlayingAudio *>::const_iterator it;
  1587. if (!event->isPositionalAudio()) {
  1588. // 2-D
  1589. for ( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it ) {
  1590. if ((*it)->m_audioEventRTS->getEventName() == event->getEventName()) {
  1591. if (totalCount == 0) {
  1592. // This is the oldest audio of this type playing.
  1593. event->setHandleToKill((*it)->m_audioEventRTS->getPlayingHandle());
  1594. }
  1595. ++totalCount;
  1596. }
  1597. }
  1598. } else {
  1599. // 3-D
  1600. for ( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it ) {
  1601. if ((*it)->m_audioEventRTS->getEventName() == event->getEventName()) {
  1602. if (totalCount == 0) {
  1603. // This is the oldest audio of this type playing.
  1604. event->setHandleToKill((*it)->m_audioEventRTS->getPlayingHandle());
  1605. }
  1606. ++totalCount;
  1607. }
  1608. }
  1609. }
  1610. // Also check the request list in case we've requested to play this sound.
  1611. std::list<AudioRequest*>::const_iterator arIt;
  1612. for (arIt = m_audioRequests.begin(); arIt != m_audioRequests.end(); ++arIt) {
  1613. AudioRequest *req = (*arIt);
  1614. if (req == NULL) {
  1615. continue;
  1616. }
  1617. if( req->m_usePendingEvent )
  1618. {
  1619. if( req->m_pendingEvent->getEventName() == event->getEventName() )
  1620. {
  1621. totalRequestCount++;
  1622. totalCount++;
  1623. }
  1624. }
  1625. }
  1626. //If our event is an interrupting type, then normally we would always add it. The exception is when we have requested
  1627. //multiple sounds in the same frame and those requests violate the limit. Because we don't have any "old" sounds to
  1628. //remove in the case of an interrupt, we need to catch it early and prevent the sound from being added if we already
  1629. //reached the limit
  1630. if( event->getAudioEventInfo()->m_control & AC_INTERRUPT )
  1631. {
  1632. if( totalRequestCount < limit )
  1633. {
  1634. Int totalPlayingCount = totalCount - totalRequestCount;
  1635. if( totalRequestCount + totalPlayingCount < limit )
  1636. {
  1637. //We aren't exceeding the actual limit, then clear the kill handle.
  1638. event->setHandleToKill(0);
  1639. return false;
  1640. }
  1641. //We are exceeding the limit - the kill handle will kill the
  1642. //oldest playing sound to enforce the actual limit.
  1643. return false;
  1644. }
  1645. }
  1646. if( totalCount < limit )
  1647. {
  1648. event->setHandleToKill(0);
  1649. return false;
  1650. }
  1651. return true;
  1652. }
  1653. //-------------------------------------------------------------------------------------------------
  1654. Bool MilesAudioManager::isPlayingAlready( AudioEventRTS *event ) const
  1655. {
  1656. std::list<PlayingAudio *>::const_iterator it;
  1657. if (!event->isPositionalAudio()) {
  1658. // 2-D
  1659. for ( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it ) {
  1660. if ((*it)->m_audioEventRTS->getEventName() == event->getEventName()) {
  1661. return true;
  1662. }
  1663. }
  1664. } else {
  1665. // 3-D
  1666. for ( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it ) {
  1667. if ((*it)->m_audioEventRTS->getEventName() == event->getEventName()) {
  1668. return true;
  1669. }
  1670. }
  1671. }
  1672. return false;
  1673. }
  1674. //-------------------------------------------------------------------------------------------------
  1675. Bool MilesAudioManager::isObjectPlayingVoice( UnsignedInt objID ) const
  1676. {
  1677. if (objID == 0) {
  1678. return false;
  1679. }
  1680. std::list<PlayingAudio *>::const_iterator it;
  1681. // 2-D
  1682. for ( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it ) {
  1683. if ((*it)->m_audioEventRTS->getObjectID() == objID && (*it)->m_audioEventRTS->getAudioEventInfo()->m_type & ST_VOICE) {
  1684. return true;
  1685. }
  1686. }
  1687. // 3-D
  1688. for ( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it ) {
  1689. if ((*it)->m_audioEventRTS->getObjectID() == objID && (*it)->m_audioEventRTS->getAudioEventInfo()->m_type & ST_VOICE) {
  1690. return true;
  1691. }
  1692. }
  1693. return false;
  1694. }
  1695. //-------------------------------------------------------------------------------------------------
  1696. AudioEventRTS* MilesAudioManager::findLowestPrioritySound( AudioEventRTS *event )
  1697. {
  1698. AudioPriority priority = event->getAudioEventInfo()->m_priority;
  1699. if( priority == AP_LOWEST )
  1700. {
  1701. //If the event we pass in is the lowest priority, don't bother checking because
  1702. //there is nothing lower priority than lowest.
  1703. return NULL;
  1704. }
  1705. AudioEventRTS *lowestPriorityEvent = NULL;
  1706. AudioPriority lowestPriority;
  1707. std::list<PlayingAudio *>::const_iterator it;
  1708. if( event->isPositionalAudio() )
  1709. {
  1710. //3D
  1711. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it )
  1712. {
  1713. AudioEventRTS *itEvent = (*it)->m_audioEventRTS;
  1714. AudioPriority itPriority = itEvent->getAudioEventInfo()->m_priority;
  1715. if( itPriority < priority )
  1716. {
  1717. if( !lowestPriorityEvent || lowestPriority > itPriority )
  1718. {
  1719. lowestPriorityEvent = itEvent;
  1720. lowestPriority = itPriority;
  1721. if( lowestPriority == AP_LOWEST )
  1722. {
  1723. return lowestPriorityEvent;
  1724. }
  1725. }
  1726. }
  1727. }
  1728. }
  1729. else
  1730. {
  1731. //2D
  1732. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it )
  1733. {
  1734. AudioEventRTS *itEvent = (*it)->m_audioEventRTS;
  1735. AudioPriority itPriority = itEvent->getAudioEventInfo()->m_priority;
  1736. if( itPriority < priority )
  1737. {
  1738. if( !lowestPriorityEvent || lowestPriority > itPriority )
  1739. {
  1740. lowestPriorityEvent = itEvent;
  1741. lowestPriority = itPriority;
  1742. if( lowestPriority == AP_LOWEST )
  1743. {
  1744. return lowestPriorityEvent;
  1745. }
  1746. }
  1747. }
  1748. }
  1749. }
  1750. return lowestPriorityEvent;
  1751. }
  1752. //-------------------------------------------------------------------------------------------------
  1753. Bool MilesAudioManager::isPlayingLowerPriority( AudioEventRTS *event ) const
  1754. {
  1755. //We don't actually want to do anything to this CONST function. Remember, we're
  1756. //just checking to see if there is a lower priority sound.
  1757. AudioPriority priority = event->getAudioEventInfo()->m_priority;
  1758. if( priority == AP_LOWEST )
  1759. {
  1760. //If the event we pass in is the lowest priority, don't bother checking because
  1761. //there is nothing lower priority than lowest.
  1762. return false;
  1763. }
  1764. std::list<PlayingAudio *>::const_iterator it;
  1765. if (!event->isPositionalAudio()) {
  1766. // 2-D
  1767. for ( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it ) {
  1768. if ((*it)->m_audioEventRTS->getAudioEventInfo()->m_priority < priority) {
  1769. //event->setHandleToKill((*it)->m_audioEventRTS->getPlayingHandle());
  1770. return true;
  1771. }
  1772. }
  1773. } else {
  1774. // 3-D
  1775. for ( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it ) {
  1776. if ((*it)->m_audioEventRTS->getAudioEventInfo()->m_priority < priority) {
  1777. //event->setHandleToKill((*it)->m_audioEventRTS->getPlayingHandle());
  1778. return true;
  1779. }
  1780. }
  1781. }
  1782. return false;
  1783. }
  1784. //-------------------------------------------------------------------------------------------------
  1785. Bool MilesAudioManager::killLowestPrioritySoundImmediately( AudioEventRTS *event )
  1786. {
  1787. //Actually, we want to kill the LOWEST PRIORITY SOUND, not the first "lower" priority
  1788. //sound we find, because it could easily be
  1789. AudioEventRTS *lowestPriorityEvent = findLowestPrioritySound( event );
  1790. if( lowestPriorityEvent )
  1791. {
  1792. std::list<PlayingAudio *>::iterator it;
  1793. if( event->isPositionalAudio() )
  1794. {
  1795. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it )
  1796. {
  1797. PlayingAudio *playing = (*it);
  1798. if( !playing )
  1799. {
  1800. continue;
  1801. }
  1802. if( playing->m_audioEventRTS && playing->m_audioEventRTS == lowestPriorityEvent )
  1803. {
  1804. //Release this 3D sound channel immediately because we are going to play another sound in it's place.
  1805. releasePlayingAudio( playing );
  1806. m_playing3DSounds.erase( it );
  1807. return TRUE;
  1808. }
  1809. }
  1810. }
  1811. else
  1812. {
  1813. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it )
  1814. {
  1815. PlayingAudio *playing = (*it);
  1816. if( !playing )
  1817. {
  1818. continue;
  1819. }
  1820. if( playing->m_audioEventRTS && playing->m_audioEventRTS == lowestPriorityEvent )
  1821. {
  1822. //Release this 3D sound channel immediately because we are going to play another sound in it's place.
  1823. releasePlayingAudio( playing );
  1824. m_playing3DSounds.erase( it );
  1825. return TRUE;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. return FALSE;
  1831. }
  1832. //-------------------------------------------------------------------------------------------------
  1833. void MilesAudioManager::adjustVolumeOfPlayingAudio(AsciiString eventName, Real newVolume)
  1834. {
  1835. Real pan;
  1836. std::list<PlayingAudio *>::iterator it;
  1837. PlayingAudio *playing = NULL;
  1838. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ++it) {
  1839. playing = *it;
  1840. if (playing && playing->m_audioEventRTS->getEventName() == eventName) {
  1841. // Adjust it
  1842. playing->m_audioEventRTS->setVolume(newVolume);
  1843. Real desiredVolume = playing->m_audioEventRTS->getVolume() * playing->m_audioEventRTS->getVolumeShift();
  1844. AIL_sample_volume_pan(playing->m_sample, NULL, &pan);
  1845. AIL_set_sample_volume_pan(playing->m_sample, desiredVolume, pan);
  1846. }
  1847. }
  1848. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ++it) {
  1849. playing = *it;
  1850. if (playing && playing->m_audioEventRTS->getEventName() == eventName) {
  1851. // Adjust it
  1852. playing->m_audioEventRTS->setVolume(newVolume);
  1853. Real desiredVolume = playing->m_audioEventRTS->getVolume() * playing->m_audioEventRTS->getVolumeShift();
  1854. AIL_set_3D_sample_volume(playing->m_3DSample, desiredVolume);
  1855. }
  1856. }
  1857. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it) {
  1858. playing = *it;
  1859. if (playing && playing->m_audioEventRTS->getEventName() == eventName) {
  1860. // Adjust it
  1861. playing->m_audioEventRTS->setVolume(newVolume);
  1862. Real desiredVolume = playing->m_audioEventRTS->getVolume() * playing->m_audioEventRTS->getVolumeShift();
  1863. AIL_stream_volume_pan(playing->m_stream, NULL, &pan);
  1864. AIL_set_stream_volume_pan(playing->m_stream, desiredVolume, pan);
  1865. }
  1866. }
  1867. }
  1868. //-------------------------------------------------------------------------------------------------
  1869. void MilesAudioManager::removePlayingAudio( AsciiString eventName )
  1870. {
  1871. std::list<PlayingAudio *>::iterator it;
  1872. PlayingAudio *playing = NULL;
  1873. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); )
  1874. {
  1875. playing = *it;
  1876. if( playing && playing->m_audioEventRTS->getEventName() == eventName )
  1877. {
  1878. releasePlayingAudio( playing );
  1879. it = m_playingSounds.erase(it);
  1880. }
  1881. else
  1882. {
  1883. it++;
  1884. }
  1885. }
  1886. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); )
  1887. {
  1888. playing = *it;
  1889. if( playing && playing->m_audioEventRTS->getEventName() == eventName )
  1890. {
  1891. releasePlayingAudio( playing );
  1892. it = m_playing3DSounds.erase(it);
  1893. }
  1894. else
  1895. {
  1896. it++;
  1897. }
  1898. }
  1899. for( it = m_playingStreams.begin(); it != m_playingStreams.end(); )
  1900. {
  1901. playing = *it;
  1902. if( playing && playing->m_audioEventRTS->getEventName() == eventName )
  1903. {
  1904. releasePlayingAudio( playing );
  1905. it = m_playingStreams.erase(it);
  1906. }
  1907. else
  1908. {
  1909. it++;
  1910. }
  1911. }
  1912. }
  1913. //-------------------------------------------------------------------------------------------------
  1914. void MilesAudioManager::removeAllDisabledAudio()
  1915. {
  1916. std::list<PlayingAudio *>::iterator it;
  1917. PlayingAudio *playing = NULL;
  1918. for( it = m_playingSounds.begin(); it != m_playingSounds.end(); )
  1919. {
  1920. playing = *it;
  1921. if( playing && playing->m_audioEventRTS->getVolume() == 0.0f )
  1922. {
  1923. releasePlayingAudio( playing );
  1924. it = m_playingSounds.erase(it);
  1925. }
  1926. else
  1927. {
  1928. it++;
  1929. }
  1930. }
  1931. for( it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); )
  1932. {
  1933. playing = *it;
  1934. if( playing && playing->m_audioEventRTS->getVolume() == 0.0f )
  1935. {
  1936. releasePlayingAudio( playing );
  1937. it = m_playing3DSounds.erase(it);
  1938. }
  1939. else
  1940. {
  1941. it++;
  1942. }
  1943. }
  1944. for( it = m_playingStreams.begin(); it != m_playingStreams.end(); )
  1945. {
  1946. playing = *it;
  1947. if( playing && playing->m_audioEventRTS->getVolume() == 0.0f )
  1948. {
  1949. releasePlayingAudio( playing );
  1950. it = m_playingStreams.erase(it);
  1951. }
  1952. else
  1953. {
  1954. it++;
  1955. }
  1956. }
  1957. }
  1958. //-------------------------------------------------------------------------------------------------
  1959. void MilesAudioManager::processRequestList( void )
  1960. {
  1961. std::list<AudioRequest*>::iterator it;
  1962. for (it = m_audioRequests.begin(); it != m_audioRequests.end(); /* empty */) {
  1963. AudioRequest *req = (*it);
  1964. if (req == NULL) {
  1965. continue;
  1966. }
  1967. if (!shouldProcessRequestThisFrame(req)) {
  1968. adjustRequest(req);
  1969. ++it;
  1970. continue;
  1971. }
  1972. if (!req->m_requiresCheckForSample || checkForSample(req)) {
  1973. processRequest(req);
  1974. }
  1975. req->deleteInstance();
  1976. it = m_audioRequests.erase(it);
  1977. }
  1978. }
  1979. //-------------------------------------------------------------------------------------------------
  1980. void MilesAudioManager::processPlayingList( void )
  1981. {
  1982. // There are two types of processing we have to do here.
  1983. // 1. Move the item to the stopped list if it has become stopped.
  1984. // 2. Update the position of the audio if it is positional
  1985. std::list<PlayingAudio *>::iterator it;
  1986. PlayingAudio *playing;
  1987. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); /* empty */) {
  1988. playing = (*it);
  1989. if (!playing)
  1990. {
  1991. it = m_playingSounds.erase(it);
  1992. continue;
  1993. }
  1994. if (playing->m_status == PS_Stopped)
  1995. {
  1996. //m_stoppedAudio.push_back(playing);
  1997. releasePlayingAudio( playing );
  1998. it = m_playingSounds.erase(it);
  1999. }
  2000. else
  2001. {
  2002. if (m_volumeHasChanged)
  2003. {
  2004. adjustPlayingVolume(playing);
  2005. }
  2006. ++it;
  2007. }
  2008. }
  2009. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); )
  2010. {
  2011. playing = (*it);
  2012. if (!playing)
  2013. {
  2014. it = m_playing3DSounds.erase(it);
  2015. continue;
  2016. }
  2017. if (playing->m_status == PS_Stopped)
  2018. {
  2019. //m_stoppedAudio.push_back(playing);
  2020. releasePlayingAudio( playing );
  2021. it = m_playing3DSounds.erase(it);
  2022. }
  2023. else
  2024. {
  2025. if (m_volumeHasChanged)
  2026. {
  2027. adjustPlayingVolume(playing);
  2028. }
  2029. const Coord3D *pos = getCurrentPositionFromEvent(playing->m_audioEventRTS);
  2030. if (pos)
  2031. {
  2032. if( playing->m_audioEventRTS->isDead() )
  2033. {
  2034. stopAudioEvent( playing->m_audioEventRTS->getPlayingHandle() );
  2035. it++;
  2036. continue;
  2037. }
  2038. else
  2039. {
  2040. Real volForConsideration = getEffectiveVolume(playing->m_audioEventRTS);
  2041. volForConsideration /= (m_sound3DVolume > 0.0f ? m_soundVolume : 1.0f);
  2042. Bool playAnyways = BitTest( playing->m_audioEventRTS->getAudioEventInfo()->m_type, ST_GLOBAL) || playing->m_audioEventRTS->getAudioEventInfo()->m_priority == AP_CRITICAL;
  2043. if( volForConsideration < m_audioSettings->m_minVolume && !playAnyways )
  2044. {
  2045. // don't want to get an additional callback for this sample
  2046. AIL_register_3D_EOS_callback(playing->m_3DSample, NULL);
  2047. //m_stoppedAudio.push_back(playing);
  2048. releasePlayingAudio( playing );
  2049. it = m_playing3DSounds.erase(it);
  2050. continue;
  2051. }
  2052. else
  2053. {
  2054. Real x = pos->x;
  2055. Real y = pos->y;
  2056. Real z = pos->z;
  2057. AIL_set_3D_position( playing->m_3DSample, x, y, z );
  2058. }
  2059. }
  2060. }
  2061. else
  2062. {
  2063. AIL_register_3D_EOS_callback(playing->m_3DSample, NULL);
  2064. //m_stoppedAudio.push_back(playing);
  2065. releasePlayingAudio( playing );
  2066. it = m_playing3DSounds.erase(it);
  2067. continue;
  2068. }
  2069. ++it;
  2070. }
  2071. }
  2072. for (it = m_playingStreams.begin(); it != m_playingStreams.end(); ) {
  2073. playing = (*it);
  2074. if (!playing)
  2075. {
  2076. it = m_playingStreams.erase(it);
  2077. continue;
  2078. }
  2079. if (playing->m_status == PS_Stopped)
  2080. {
  2081. //m_stoppedAudio.push_back(playing);
  2082. releasePlayingAudio( playing );
  2083. it = m_playingStreams.erase(it);
  2084. }
  2085. else
  2086. {
  2087. if (m_volumeHasChanged)
  2088. {
  2089. adjustPlayingVolume(playing);
  2090. }
  2091. ++it;
  2092. }
  2093. }
  2094. if (m_volumeHasChanged) {
  2095. m_volumeHasChanged = false;
  2096. }
  2097. }
  2098. //-------------------------------------------------------------------------------------------------
  2099. void MilesAudioManager::processFadingList( void )
  2100. {
  2101. std::list<PlayingAudio *>::iterator it;
  2102. PlayingAudio *playing;
  2103. for (it = m_fadingAudio.begin(); it != m_fadingAudio.end(); /* emtpy */) {
  2104. playing = *it;
  2105. if (!playing) {
  2106. continue;
  2107. }
  2108. if (playing->m_framesFaded >= getAudioSettings()->m_fadeAudioFrames) {
  2109. playing->m_status = PS_Stopped;
  2110. playing->m_requestStop = true;
  2111. //m_stoppedAudio.push_back(playing);
  2112. releasePlayingAudio( playing );
  2113. it = m_fadingAudio.erase(it);
  2114. continue;
  2115. }
  2116. ++playing->m_framesFaded;
  2117. Real volume = getEffectiveVolume(playing->m_audioEventRTS);
  2118. volume *= (1.0f - 1.0f * playing->m_framesFaded / getAudioSettings()->m_fadeAudioFrames);
  2119. switch(playing->m_type)
  2120. {
  2121. case PAT_Sample:
  2122. {
  2123. AIL_set_sample_volume_pan(playing->m_sample, volume, 0.5f);
  2124. break;
  2125. }
  2126. case PAT_3DSample:
  2127. {
  2128. AIL_set_3D_sample_volume(playing->m_3DSample, volume);
  2129. break;
  2130. }
  2131. case PAT_Stream:
  2132. {
  2133. AIL_set_stream_volume_pan(playing->m_stream, volume, 0.5f);
  2134. break;
  2135. }
  2136. }
  2137. ++it;
  2138. }
  2139. }
  2140. //-------------------------------------------------------------------------------------------------
  2141. void MilesAudioManager::processStoppedList( void )
  2142. {
  2143. std::list<PlayingAudio *>::iterator it;
  2144. PlayingAudio *playing;
  2145. for (it = m_stoppedAudio.begin(); it != m_stoppedAudio.end(); /* emtpy */) {
  2146. playing = *it;
  2147. if (playing) {
  2148. releasePlayingAudio(playing);
  2149. }
  2150. it = m_stoppedAudio.erase(it);
  2151. }
  2152. }
  2153. //-------------------------------------------------------------------------------------------------
  2154. Bool MilesAudioManager::shouldProcessRequestThisFrame( AudioRequest *req ) const
  2155. {
  2156. if (!req->m_usePendingEvent) {
  2157. return true;
  2158. }
  2159. if (req->m_pendingEvent->getDelay() < MSEC_PER_LOGICFRAME_REAL) {
  2160. return true;
  2161. }
  2162. return false;
  2163. }
  2164. //-------------------------------------------------------------------------------------------------
  2165. void MilesAudioManager::adjustRequest( AudioRequest *req )
  2166. {
  2167. if (!req->m_usePendingEvent) {
  2168. return;
  2169. }
  2170. req->m_pendingEvent->decrementDelay(MSEC_PER_LOGICFRAME_REAL);
  2171. req->m_requiresCheckForSample = true;
  2172. }
  2173. //-------------------------------------------------------------------------------------------------
  2174. Bool MilesAudioManager::checkForSample( AudioRequest *req )
  2175. {
  2176. if (!req->m_usePendingEvent) {
  2177. return true;
  2178. }
  2179. if (req->m_pendingEvent->getAudioEventInfo()->m_type != AT_SoundEffect) {
  2180. return true;
  2181. }
  2182. return m_sound->canPlayNow(req->m_pendingEvent);
  2183. }
  2184. //-------------------------------------------------------------------------------------------------
  2185. void MilesAudioManager::setHardwareAccelerated(Bool accel)
  2186. {
  2187. // Extends
  2188. Bool retEarly = (accel == m_hardwareAccel);
  2189. AudioManager::setHardwareAccelerated(accel);
  2190. if (retEarly) {
  2191. return;
  2192. }
  2193. if (m_hardwareAccel) {
  2194. for (Int i = 0; i < MAX_HW_PROVIDERS; ++i) {
  2195. UnsignedInt providerNdx = TheAudio->getProviderIndex(TheAudio->getAudioSettings()->m_preferred3DProvider[i]);
  2196. TheAudio->selectProvider(providerNdx);
  2197. if (getSelectedProvider() == providerNdx) {
  2198. return;
  2199. }
  2200. }
  2201. }
  2202. // set it false
  2203. AudioManager::setHardwareAccelerated(FALSE);
  2204. UnsignedInt providerNdx = TheAudio->getProviderIndex(TheAudio->getAudioSettings()->m_preferred3DProvider[MAX_HW_PROVIDERS]);
  2205. TheAudio->selectProvider(providerNdx);
  2206. }
  2207. //-------------------------------------------------------------------------------------------------
  2208. void MilesAudioManager::setSpeakerSurround(Bool surround)
  2209. {
  2210. // Extends
  2211. Bool retEarly = (surround == m_surroundSpeakers);
  2212. AudioManager::setSpeakerSurround(surround);
  2213. if (retEarly) {
  2214. return;
  2215. }
  2216. UnsignedInt speakerType;
  2217. if (m_surroundSpeakers) {
  2218. speakerType = TheAudio->getAudioSettings()->m_defaultSpeakerType3D;
  2219. } else {
  2220. speakerType = TheAudio->getAudioSettings()->m_defaultSpeakerType2D;
  2221. }
  2222. TheAudio->setSpeakerType(speakerType);
  2223. }
  2224. //-------------------------------------------------------------------------------------------------
  2225. Real MilesAudioManager::getFileLengthMS( AsciiString strToLoad ) const
  2226. {
  2227. if (strToLoad.isEmpty()) {
  2228. return 0.0f;
  2229. }
  2230. // Load it as a stream to get the file info without actually opening the file.
  2231. HSTREAM stream = AIL_open_stream(m_digitalHandle, strToLoad.str(), 0);
  2232. if (!stream) {
  2233. return 0.0f;
  2234. }
  2235. long retVal;
  2236. AIL_stream_ms_position(stream, &retVal, NULL);
  2237. // Now close the stream
  2238. AIL_close_stream(stream);
  2239. return INT_TO_REAL(retVal);
  2240. }
  2241. //-------------------------------------------------------------------------------------------------
  2242. void MilesAudioManager::closeAnySamplesUsingFile( const void *fileToClose )
  2243. {
  2244. std::list<PlayingAudio *>::iterator it;
  2245. PlayingAudio *playing;
  2246. for (it = m_playingSounds.begin(); it != m_playingSounds.end(); ) {
  2247. playing = *it;
  2248. if (!playing) {
  2249. continue;
  2250. }
  2251. if (playing->m_file == fileToClose) {
  2252. releasePlayingAudio(playing);
  2253. it = m_playingSounds.erase(it);
  2254. } else {
  2255. ++it;
  2256. }
  2257. }
  2258. for (it = m_playing3DSounds.begin(); it != m_playing3DSounds.end(); ) {
  2259. playing = *it;
  2260. if (!playing) {
  2261. continue;
  2262. }
  2263. if (playing->m_file == fileToClose) {
  2264. releasePlayingAudio(playing);
  2265. it = m_playing3DSounds.erase(it);
  2266. } else {
  2267. ++it;
  2268. }
  2269. }
  2270. }
  2271. //-------------------------------------------------------------------------------------------------
  2272. void MilesAudioManager::setDeviceListenerPosition( void )
  2273. {
  2274. if (m_listener) {
  2275. AIL_set_3D_orientation(m_listener, m_listenerOrientation.x, m_listenerOrientation.y, m_listenerOrientation.z, 0, 0, -1);
  2276. Real x = m_listenerPosition.x;
  2277. Real y = m_listenerPosition.y;
  2278. Real z = m_listenerPosition.z;
  2279. AIL_set_3D_position( m_listener, x, y, z );
  2280. }
  2281. }
  2282. //-------------------------------------------------------------------------------------------------
  2283. const Coord3D *MilesAudioManager::getCurrentPositionFromEvent( AudioEventRTS *event )
  2284. {
  2285. if (!event->isPositionalAudio()) {
  2286. return NULL;
  2287. }
  2288. return event->getCurrentPosition();
  2289. }
  2290. //-------------------------------------------------------------------------------------------------
  2291. Bool MilesAudioManager::isOnScreen( const Coord3D *pos ) const
  2292. {
  2293. static ICoord2D dummy;
  2294. // WorldToScreen will return True if the point is onscreen and false if it is offscreen.
  2295. return TheTacticalView->worldToScreen(pos, &dummy);
  2296. }
  2297. //-------------------------------------------------------------------------------------------------
  2298. Real MilesAudioManager::getEffectiveVolume(AudioEventRTS *event) const
  2299. {
  2300. Real volume = 1.0f;
  2301. volume *= (event->getVolume() * event->getVolumeShift());
  2302. if (event->getAudioEventInfo()->m_soundType == AT_Music)
  2303. {
  2304. volume *= m_musicVolume;
  2305. }
  2306. else if (event->getAudioEventInfo()->m_soundType == AT_Streaming)
  2307. {
  2308. volume *= m_speechVolume;
  2309. }
  2310. else
  2311. {
  2312. if (event->isPositionalAudio())
  2313. {
  2314. volume *= m_sound3DVolume;
  2315. Coord3D distance = m_listenerPosition;
  2316. const Coord3D *pos = event->getCurrentPosition();
  2317. if (pos)
  2318. {
  2319. distance.sub(pos);
  2320. Real objMinDistance;
  2321. Real objMaxDistance;
  2322. if (event->getAudioEventInfo()->m_type & ST_GLOBAL)
  2323. {
  2324. objMinDistance = TheAudio->getAudioSettings()->m_globalMinRange;
  2325. objMaxDistance = TheAudio->getAudioSettings()->m_globalMaxRange;
  2326. }
  2327. else
  2328. {
  2329. objMinDistance = event->getAudioEventInfo()->m_minDistance;
  2330. objMaxDistance = event->getAudioEventInfo()->m_maxDistance;
  2331. }
  2332. Real objDistance = distance.length();
  2333. if( objDistance > objMinDistance )
  2334. {
  2335. volume *= 1 / (objDistance / objMinDistance);
  2336. }
  2337. if( objDistance >= objMaxDistance )
  2338. {
  2339. volume = 0.0f;
  2340. }
  2341. //else if( objDistance > objMinDistance )
  2342. //{
  2343. // volume *= 1.0f - (objDistance - objMinDistance) / (objMaxDistance - objMinDistance);
  2344. //}
  2345. }
  2346. }
  2347. else
  2348. {
  2349. volume *= m_soundVolume;
  2350. }
  2351. }
  2352. return volume;
  2353. }
  2354. //-------------------------------------------------------------------------------------------------
  2355. Bool MilesAudioManager::startNextLoop( PlayingAudio *looping )
  2356. {
  2357. closeFile(looping->m_file);
  2358. looping->m_file = NULL;
  2359. if (looping->m_requestStop) {
  2360. return false;
  2361. }
  2362. if (looping->m_audioEventRTS->hasMoreLoops()) {
  2363. // generate a new filename, and test to see whether we can play with it now
  2364. looping->m_audioEventRTS->generateFilename();
  2365. if (looping->m_audioEventRTS->getDelay() > MSEC_PER_LOGICFRAME_REAL) {
  2366. // fake it out so that this sound appears done, but also so that it will not
  2367. // delete the sound on completion (which would suck)
  2368. looping->m_cleanupAudioEventRTS = false;
  2369. looping->m_requestStop = true;
  2370. looping->m_status = PS_Stopped;
  2371. AudioRequest *req = allocateAudioRequest(true);
  2372. req->m_pendingEvent = looping->m_audioEventRTS;
  2373. req->m_requiresCheckForSample = true;
  2374. appendAudioRequest(req);
  2375. return true;
  2376. }
  2377. if (looping->m_type == PAT_3DSample) {
  2378. looping->m_file = playSample3D(looping->m_audioEventRTS, looping->m_3DSample);
  2379. } else {
  2380. looping->m_file = playSample(looping->m_audioEventRTS, looping->m_sample);
  2381. }
  2382. return looping->m_file != NULL;
  2383. }
  2384. return false;
  2385. }
  2386. //-------------------------------------------------------------------------------------------------
  2387. void MilesAudioManager::playStream( AudioEventRTS *event, HSTREAM stream )
  2388. {
  2389. // Force it to the beginning
  2390. if (event->getAudioEventInfo()->m_soundType == AT_Music) {
  2391. AIL_set_stream_loop_count(stream, INFINITE_LOOP_COUNT);
  2392. }
  2393. AIL_register_stream_callback(stream, setStreamCompleted);
  2394. AIL_start_stream(stream);
  2395. if (event->getAudioEventInfo()->m_soundType == AT_Music) {
  2396. // Need to stop/fade out the old music here.
  2397. }
  2398. }
  2399. //-------------------------------------------------------------------------------------------------
  2400. void *MilesAudioManager::playSample( AudioEventRTS *event, HSAMPLE sample )
  2401. {
  2402. AIL_init_sample(sample);
  2403. // Prep any sort of filtering, etc, here
  2404. AIL_register_EOS_callback(sample, setSampleCompleted);
  2405. initFilters(sample, event);
  2406. // Load the file in
  2407. void *fileBuffer = NULL;
  2408. fileBuffer = loadFileForRead(event);
  2409. if (fileBuffer) {
  2410. AIL_set_sample_file(sample, fileBuffer, 0);
  2411. // Start playback
  2412. AIL_start_sample(sample);
  2413. }
  2414. return fileBuffer;
  2415. }
  2416. //-------------------------------------------------------------------------------------------------
  2417. void *MilesAudioManager::playSample3D( AudioEventRTS *event, H3DSAMPLE sample3D )
  2418. {
  2419. const Coord3D *pos = getCurrentPositionFromEvent(event);
  2420. if (pos) {
  2421. // Load the file in
  2422. void *fileBuffer = loadFileForRead(event);
  2423. if (fileBuffer) {
  2424. AIL_set_3D_sample_file(sample3D, fileBuffer);
  2425. // Prep any sort of filtering, etc, here
  2426. AIL_register_3D_EOS_callback(sample3D, set3DSampleCompleted);
  2427. // Set the position values of the sample here
  2428. if (event->getAudioEventInfo()->m_type & ST_GLOBAL) {
  2429. AIL_set_3D_sample_distances(sample3D, TheAudio->getAudioSettings()->m_globalMinRange, TheAudio->getAudioSettings()->m_globalMaxRange );
  2430. } else {
  2431. AIL_set_3D_sample_distances(sample3D, event->getAudioEventInfo()->m_minDistance, event->getAudioEventInfo()->m_maxDistance );
  2432. }
  2433. // Set the position of the sample here
  2434. Real x = pos->x;
  2435. Real y = pos->y;
  2436. Real z = pos->z;
  2437. AIL_set_3D_position( sample3D, x, y, z );
  2438. initFilters3D(sample3D, event, pos);
  2439. // Start playback
  2440. AIL_start_3D_sample(sample3D);
  2441. }
  2442. return fileBuffer;
  2443. }
  2444. return NULL;
  2445. }
  2446. //-------------------------------------------------------------------------------------------------
  2447. void MilesAudioManager::buildProviderList( void )
  2448. {
  2449. HPROENUM next = HPROENUM_FIRST;
  2450. char *name;
  2451. UnsignedInt index = 0;
  2452. while (index < MAXPROVIDERS && AIL_enumerate_3D_providers(&next, &m_provider3D[index].id, &name)) {
  2453. m_provider3D[index].name.set(name); // set it to the AsciiString
  2454. ++index;
  2455. }
  2456. m_providerCount = index;
  2457. }
  2458. //-------------------------------------------------------------------------------------------------
  2459. void MilesAudioManager::createListener( void )
  2460. {
  2461. if (!(isOn(AudioAffect_Sound3D) && isValidProvider())) {
  2462. return;
  2463. }
  2464. m_listener = AIL_open_3D_listener(m_provider3D[m_selectedProvider].id);
  2465. // initial listener position will be (0, 0, 0)
  2466. }
  2467. //-------------------------------------------------------------------------------------------------
  2468. void MilesAudioManager::initDelayFilter( void )
  2469. {
  2470. if (m_delayFilter != NULL) {
  2471. return;
  2472. }
  2473. char* filterName;
  2474. HPROENUM enumFLTs = HPROENUM_FIRST;
  2475. HPROVIDER currentProvider;
  2476. while (AIL_enumerate_filters(&enumFLTs, &currentProvider, &filterName )) {
  2477. if (strcmp(filterName,"Mono Delay Filter") == 0) {
  2478. m_delayFilter = currentProvider;
  2479. break;
  2480. }
  2481. }
  2482. }
  2483. //-------------------------------------------------------------------------------------------------
  2484. Bool MilesAudioManager::isValidProvider( void )
  2485. {
  2486. return (m_selectedProvider < m_providerCount);
  2487. }
  2488. //-------------------------------------------------------------------------------------------------
  2489. void MilesAudioManager::initSamplePools( void )
  2490. {
  2491. if (!(isOn(AudioAffect_Sound3D) && isValidProvider())) {
  2492. return;
  2493. }
  2494. int i = 0;
  2495. for (i = 0; i < getAudioSettings()->m_sampleCount2D; ++i) {
  2496. HSAMPLE sample = AIL_allocate_sample_handle(m_digitalHandle);
  2497. DEBUG_ASSERTCRASH(sample, ("Couldn't get %d 2D samples\n", i + 1));
  2498. if (sample) {
  2499. AIL_init_sample(sample);
  2500. AIL_set_sample_user_data(sample, 0, i + 1);
  2501. m_availableSamples.push_back(sample);
  2502. ++m_num2DSamples;
  2503. }
  2504. }
  2505. for (i = 0; i < getAudioSettings()->m_sampleCount3D; ++i) {
  2506. H3DSAMPLE sample = AIL_allocate_3D_sample_handle(m_provider3D[m_selectedProvider].id);
  2507. DEBUG_ASSERTCRASH(sample, ("Couldn't get %d 3D samples\n", i + 1));
  2508. if (sample) {
  2509. AIL_set_3D_user_data(sample, 0, i + 1);
  2510. m_available3DSamples.push_back(sample);
  2511. ++m_num3DSamples;
  2512. }
  2513. }
  2514. // Streams are basically free, so we can just allocate the appropriate number
  2515. m_numStreams = getAudioSettings()->m_streamCount;
  2516. }
  2517. //-------------------------------------------------------------------------------------------------
  2518. void MilesAudioManager::processRequest( AudioRequest *req )
  2519. {
  2520. switch (req->m_request)
  2521. {
  2522. case AR_Play:
  2523. {
  2524. playAudioEvent(req->m_pendingEvent);
  2525. break;
  2526. }
  2527. case AR_Pause:
  2528. {
  2529. pauseAudioEvent(req->m_handleToInteractOn);
  2530. break;
  2531. }
  2532. case AR_Stop:
  2533. {
  2534. stopAudioEvent(req->m_handleToInteractOn);
  2535. break;
  2536. }
  2537. }
  2538. }
  2539. //-------------------------------------------------------------------------------------------------
  2540. void *MilesAudioManager::getHandleForBink( void )
  2541. {
  2542. if (m_binkHandle == NULL) {
  2543. PlayingAudio *aud = allocatePlayingAudio();
  2544. aud->m_audioEventRTS = NEW AudioEventRTS("BinkHandle"); // poolify
  2545. getInfoForAudioEvent(aud->m_audioEventRTS);
  2546. aud->m_sample = getFirst2DSample(aud->m_audioEventRTS);
  2547. aud->m_type = PAT_Sample;
  2548. if (!aud->m_sample) {
  2549. releasePlayingAudio(aud);
  2550. return NULL;
  2551. }
  2552. m_binkHandle = aud;
  2553. }
  2554. AILLPDIRECTSOUND lpDS;
  2555. AIL_get_DirectSound_info(m_binkHandle->m_sample, &lpDS, NULL);
  2556. return lpDS;
  2557. }
  2558. //-------------------------------------------------------------------------------------------------
  2559. void MilesAudioManager::releaseHandleForBink( void )
  2560. {
  2561. if (m_binkHandle) {
  2562. releasePlayingAudio(m_binkHandle);
  2563. m_binkHandle = NULL;
  2564. }
  2565. }
  2566. //-------------------------------------------------------------------------------------------------
  2567. void MilesAudioManager::friend_forcePlayAudioEventRTS(const AudioEventRTS* eventToPlay)
  2568. {
  2569. if (!eventToPlay->getAudioEventInfo()) {
  2570. getInfoForAudioEvent(eventToPlay);
  2571. if (!eventToPlay->getAudioEventInfo()) {
  2572. DEBUG_CRASH(("No info for forced audio event '%s'\n", eventToPlay->getEventName().str()));
  2573. return;
  2574. }
  2575. }
  2576. switch (eventToPlay->getAudioEventInfo()->m_soundType)
  2577. {
  2578. case AT_Music:
  2579. if (!isOn(AudioAffect_Music))
  2580. return;
  2581. break;
  2582. case AT_SoundEffect:
  2583. if (!isOn(AudioAffect_Sound) || !isOn(AudioAffect_Sound3D))
  2584. return;
  2585. break;
  2586. case AT_Streaming:
  2587. if (!isOn(AudioAffect_Speech))
  2588. return;
  2589. break;
  2590. }
  2591. AudioEventRTS event = *eventToPlay;
  2592. event.generateFilename();
  2593. event.generatePlayInfo();
  2594. std::list<std::pair<AsciiString, Real> >::iterator it;
  2595. for (it = m_adjustedVolumes.begin(); it != m_adjustedVolumes.end(); ++it) {
  2596. if (it->first == event.getEventName()) {
  2597. event.setVolume(it->second);
  2598. break;
  2599. }
  2600. }
  2601. AsciiString fileToPlay = event.getFilename();
  2602. HAUDIO haud = AIL_quick_load_and_play(fileToPlay.str(), 1, 0);
  2603. // Even though the event type is not Speech, this is used only for mission briefings, so use the
  2604. // speech slider to adjust the volume.
  2605. // Get the volume from the event, and pass 0.5 to play the audio in the middle. (0.0 is full left, 1.0 is full right)
  2606. AIL_quick_set_volume(haud, event.getVolume() * getVolume(AudioAffect_Speech), 0.5);
  2607. m_audioForcePlayed.push_back(haud);
  2608. }
  2609. //-------------------------------------------------------------------------------------------------
  2610. //-------------------------------------------------------------------------------------------------
  2611. //-------------------------------------------------------------------------------------------------
  2612. void AILCALLBACK setSampleCompleted( HSAMPLE sampleCompleted )
  2613. {
  2614. TheAudio->notifyOfAudioCompletion((UnsignedInt) sampleCompleted, PAT_Sample);
  2615. }
  2616. //-------------------------------------------------------------------------------------------------
  2617. void AILCALLBACK set3DSampleCompleted( H3DSAMPLE sample3DCompleted )
  2618. {
  2619. TheAudio->notifyOfAudioCompletion((UnsignedInt) sample3DCompleted, PAT_3DSample);
  2620. }
  2621. //-------------------------------------------------------------------------------------------------
  2622. void AILCALLBACK setStreamCompleted( HSTREAM streamCompleted )
  2623. {
  2624. TheAudio->notifyOfAudioCompletion((UnsignedInt) streamCompleted, PAT_Stream);
  2625. }
  2626. //-------------------------------------------------------------------------------------------------
  2627. U32 AILCALLBACK streamingFileOpen(char const *fileName, U32 *file_handle)
  2628. {
  2629. #if defined(_DEBUG) || defined(_INTERNAL)
  2630. if (sizeof(U32) != sizeof(File*)) {
  2631. RELEASE_CRASH(("streamingFileOpen - This function requires work in order to compile on non 32-bit platforms.\n"));
  2632. }
  2633. #endif
  2634. (*file_handle) = (U32) TheFileSystem->openFile(fileName, File::READ | File::STREAMING);
  2635. return ((*file_handle) != 0);
  2636. }
  2637. //-------------------------------------------------------------------------------------------------
  2638. void AILCALLBACK streamingFileClose(U32 fileHandle)
  2639. {
  2640. ((File*) fileHandle)->close();
  2641. }
  2642. //-------------------------------------------------------------------------------------------------
  2643. S32 AILCALLBACK streamingFileSeek(U32 fileHandle, S32 offset, U32 type)
  2644. {
  2645. return ((File*) fileHandle)->seek(offset, (File::seekMode) type);
  2646. }
  2647. //-------------------------------------------------------------------------------------------------
  2648. U32 AILCALLBACK streamingFileRead(U32 file_handle, void *buffer, U32 bytes)
  2649. {
  2650. return ((File*) file_handle)->read(buffer, bytes);
  2651. }
  2652. //-------------------------------------------------------------------------------------------------
  2653. //-------------------------------------------------------------------------------------------------
  2654. //-------------------------------------------------------------------------------------------------
  2655. AudioFileCache::AudioFileCache() : m_maxSize(0), m_currentlyUsedSize(0), m_mutexName("AudioFileCacheMutex")
  2656. {
  2657. m_mutex = CreateMutex(NULL, FALSE, m_mutexName);
  2658. }
  2659. //-------------------------------------------------------------------------------------------------
  2660. AudioFileCache::~AudioFileCache()
  2661. {
  2662. {
  2663. ScopedMutex mut(m_mutex);
  2664. // Free all the samples that are open.
  2665. OpenFilesHashIt it;
  2666. for ( it = m_openFiles.begin(); it != m_openFiles.end(); ++it ) {
  2667. if (it->second.m_openCount > 0) {
  2668. DEBUG_CRASH(("Sample '%s' is still playing, and we're trying to quit.\n", it->second.m_eventInfo->m_audioName.str()));
  2669. }
  2670. releaseOpenAudioFile(&it->second);
  2671. // Don't erase it from the map, cause it makes this whole process way more complicated, and
  2672. // we're about to go away anyways.
  2673. }
  2674. }
  2675. CloseHandle(m_mutex);
  2676. }
  2677. //-------------------------------------------------------------------------------------------------
  2678. void *AudioFileCache::openFile( AudioEventRTS *eventToOpenFrom )
  2679. {
  2680. // Protect the entire openFile function
  2681. ScopedMutex mut(m_mutex);
  2682. AsciiString strToFind;
  2683. switch (eventToOpenFrom->getNextPlayPortion())
  2684. {
  2685. case PP_Attack:
  2686. strToFind = eventToOpenFrom->getAttackFilename();
  2687. break;
  2688. case PP_Sound:
  2689. strToFind = eventToOpenFrom->getFilename();
  2690. break;
  2691. case PP_Decay:
  2692. strToFind = eventToOpenFrom->getDecayFilename();
  2693. break;
  2694. case PP_Done:
  2695. return NULL;
  2696. }
  2697. OpenFilesHash::iterator it;
  2698. it = m_openFiles.find(strToFind);
  2699. if (it != m_openFiles.end()) {
  2700. ++it->second.m_openCount;
  2701. return it->second.m_file;
  2702. }
  2703. // Couldn't find the file, so actually open it.
  2704. File *file = TheFileSystem->openFile(strToFind.str());
  2705. if (!file) {
  2706. DEBUG_ASSERTLOG(strToFind.isEmpty(), ("Missing Audio File: '%s'\n", strToFind.str()));
  2707. return NULL;
  2708. }
  2709. UnsignedInt fileSize = file->size();
  2710. char* buffer = file->readEntireAndClose();
  2711. OpenAudioFile openedAudioFile;
  2712. openedAudioFile.m_eventInfo = eventToOpenFrom->getAudioEventInfo();
  2713. AILSOUNDINFO soundInfo;
  2714. AIL_WAV_info(buffer, &soundInfo);
  2715. if (eventToOpenFrom->isPositionalAudio()) {
  2716. if (soundInfo.channels > 1) {
  2717. DEBUG_CRASH(("Requested Positional Play of audio '%s', but it is in stereo.", strToFind.str()));
  2718. delete [] buffer;
  2719. return NULL;
  2720. }
  2721. }
  2722. if (soundInfo.format == WAVE_FORMAT_IMA_ADPCM) {
  2723. void *decompressFileBuffer;
  2724. U32 newFileSize;
  2725. AIL_decompress_ADPCM(&soundInfo, &decompressFileBuffer, &newFileSize);
  2726. fileSize = newFileSize;
  2727. openedAudioFile.m_compressed = TRUE;
  2728. delete [] buffer;
  2729. openedAudioFile.m_file = decompressFileBuffer;
  2730. openedAudioFile.m_soundInfo = soundInfo;
  2731. openedAudioFile.m_openCount = 1;
  2732. } else if (soundInfo.format == WAVE_FORMAT_PCM) {
  2733. openedAudioFile.m_compressed = FALSE;
  2734. openedAudioFile.m_file = buffer;
  2735. openedAudioFile.m_soundInfo = soundInfo;
  2736. openedAudioFile.m_openCount = 1;
  2737. } else {
  2738. DEBUG_CRASH(("Unexpected compression type in '%s'\n", strToFind.str()));
  2739. // prevent leaks
  2740. delete [] buffer;
  2741. return NULL;
  2742. }
  2743. openedAudioFile.m_fileSize = fileSize;
  2744. m_currentlyUsedSize += openedAudioFile.m_fileSize;
  2745. if (m_currentlyUsedSize > m_maxSize) {
  2746. // We need to free some samples, or we're not going to be able to play this sound.
  2747. if (!freeEnoughSpaceForSample(openedAudioFile)) {
  2748. m_currentlyUsedSize -= openedAudioFile.m_fileSize;
  2749. releaseOpenAudioFile(&openedAudioFile);
  2750. return NULL;
  2751. }
  2752. }
  2753. m_openFiles[strToFind] = openedAudioFile;
  2754. return openedAudioFile.m_file;
  2755. }
  2756. //-------------------------------------------------------------------------------------------------
  2757. void AudioFileCache::closeFile( void *fileToClose )
  2758. {
  2759. if (!fileToClose) {
  2760. return;
  2761. }
  2762. // Protect the entire closeFile function
  2763. ScopedMutex mut(m_mutex);
  2764. OpenFilesHash::iterator it;
  2765. for ( it = m_openFiles.begin(); it != m_openFiles.end(); ++it ) {
  2766. if ( it->second.m_file == fileToClose ) {
  2767. --it->second.m_openCount;
  2768. return;
  2769. }
  2770. }
  2771. }
  2772. //-------------------------------------------------------------------------------------------------
  2773. void AudioFileCache::setMaxSize( UnsignedInt size )
  2774. {
  2775. // Protect the function, in case we're trying to use this value elsewhere.
  2776. ScopedMutex mut(m_mutex);
  2777. m_maxSize = size;
  2778. }
  2779. //-------------------------------------------------------------------------------------------------
  2780. void AudioFileCache::releaseOpenAudioFile( OpenAudioFile *fileToRelease )
  2781. {
  2782. if (fileToRelease->m_openCount > 0) {
  2783. // This thing needs to be terminated IMMEDIATELY.
  2784. TheAudio->closeAnySamplesUsingFile(fileToRelease->m_file);
  2785. }
  2786. if (fileToRelease->m_file) {
  2787. if (fileToRelease->m_compressed) {
  2788. // Files read in via AIL_decompress_ADPCM must be freed with AIL_mem_free_lock.
  2789. AIL_mem_free_lock(fileToRelease->m_file);
  2790. } else {
  2791. // Otherwise, we read it, we own it, blow it away.
  2792. delete [] fileToRelease->m_file;
  2793. }
  2794. fileToRelease->m_file = NULL;
  2795. fileToRelease->m_eventInfo = NULL;
  2796. }
  2797. }
  2798. //-------------------------------------------------------------------------------------------------
  2799. Bool AudioFileCache::freeEnoughSpaceForSample(const OpenAudioFile& sampleThatNeedsSpace)
  2800. {
  2801. Int spaceRequired = m_currentlyUsedSize - m_maxSize;
  2802. Int runningTotal = 0;
  2803. std::list<AsciiString> filesToClose;
  2804. // First, search for any samples that have ref counts of 0. They are low-hanging fruit, and
  2805. // should be considered immediately.
  2806. OpenFilesHashIt it;
  2807. for (it = m_openFiles.begin(); it != m_openFiles.end(); ++it) {
  2808. if (it->second.m_openCount == 0) {
  2809. // This is said low-hanging fruit.
  2810. filesToClose.push_back(it->first);
  2811. runningTotal += it->second.m_fileSize;
  2812. if (runningTotal >= spaceRequired) {
  2813. break;
  2814. }
  2815. }
  2816. }
  2817. // If we don't have enough space yet, then search through the events who have a count of 1 or more
  2818. // and who are lower priority than this sound.
  2819. // Mical said that at this point, sounds shouldn't care if other sounds are interruptable or not.
  2820. // Kill any files of lower priority necessary to clear our the buffer.
  2821. if (runningTotal < spaceRequired) {
  2822. for (it = m_openFiles.begin(); it != m_openFiles.end(); ++it) {
  2823. if (it->second.m_openCount > 0) {
  2824. if (it->second.m_eventInfo->m_priority < sampleThatNeedsSpace.m_eventInfo->m_priority) {
  2825. filesToClose.push_back(it->first);
  2826. runningTotal += it->second.m_fileSize;
  2827. if (runningTotal >= spaceRequired) {
  2828. break;
  2829. }
  2830. }
  2831. }
  2832. }
  2833. }
  2834. // We weren't able to find enough sounds to truncate. Therefore, this sound is not going to play.
  2835. if (runningTotal < spaceRequired) {
  2836. return FALSE;
  2837. }
  2838. std::list<AsciiString>::iterator ait;
  2839. for (ait = filesToClose.begin(); ait != filesToClose.end(); ++ait) {
  2840. OpenFilesHashIt itToErase = m_openFiles.find(*ait);
  2841. if (itToErase != m_openFiles.end()) {
  2842. releaseOpenAudioFile(&itToErase->second);
  2843. m_currentlyUsedSize -= itToErase->second.m_fileSize;
  2844. m_openFiles.erase(itToErase);
  2845. }
  2846. }
  2847. return TRUE;
  2848. }
  2849. #if defined(_DEBUG) || defined(_INTERNAL)
  2850. //-------------------------------------------------------------------------------------------------
  2851. void MilesAudioManager::dumpAllAssetsUsed()
  2852. {
  2853. if (!TheGlobalData->m_preloadReport) {
  2854. return;
  2855. }
  2856. // Dump all the audio assets we've used.
  2857. FILE *logfile=fopen("PreloadedAssets.txt","a+"); //append to log
  2858. if (!logfile)
  2859. return;
  2860. std::list<AsciiString> missingEvents;
  2861. std::list<AsciiString> usedFiles;
  2862. std::list<AsciiString>::iterator lit;
  2863. fprintf(logfile, "\nAudio Asset Report - BEGIN\n");
  2864. {
  2865. SetAsciiStringIt it;
  2866. std::vector<AsciiString>::iterator asIt;
  2867. for (it = m_allEventsLoaded.begin(); it != m_allEventsLoaded.end(); ++it) {
  2868. AsciiString astr = *it;
  2869. AudioEventInfo *aei = findAudioEventInfo(astr);
  2870. if (!aei) {
  2871. missingEvents.push_back(astr);
  2872. continue;
  2873. }
  2874. for (asIt = aei->m_attackSounds.begin(); asIt != aei->m_attackSounds.end(); ++asIt) {
  2875. usedFiles.push_back(*asIt);
  2876. }
  2877. for (asIt = aei->m_sounds.begin(); asIt != aei->m_sounds.end(); ++asIt) {
  2878. usedFiles.push_back(*asIt);
  2879. }
  2880. for (asIt = aei->m_decaySounds.begin(); asIt != aei->m_decaySounds.end(); ++asIt) {
  2881. usedFiles.push_back(*asIt);
  2882. }
  2883. if (!aei->m_filename.isEmpty()) {
  2884. usedFiles.push_back(aei->m_filename);
  2885. }
  2886. }
  2887. fprintf(logfile, "\nEvents Requested that are missing information - BEGIN\n");
  2888. for (lit = missingEvents.begin(); lit != missingEvents.end(); ++lit) {
  2889. fprintf(logfile, "%s\n", (*lit).str());
  2890. }
  2891. fprintf(logfile, "\nEvents Requested that are missing information - END\n");
  2892. fprintf(logfile, "\nFiles Used - BEGIN\n");
  2893. for (lit = usedFiles.begin(); lit != usedFiles.end(); ++lit) {
  2894. fprintf(logfile, "%s\n", (*lit).str());
  2895. }
  2896. fprintf(logfile, "\nFiles Used - END\n");
  2897. }
  2898. fprintf(logfile, "\nAudio Asset Report - END\n");
  2899. fclose(logfile);
  2900. logfile = NULL;
  2901. }
  2902. #endif