MilesAudioManager.cpp 98 KB

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