WWAudio.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWAudio *
  23. * *
  24. * $Archive:: /Commando/Code/WWAudio/WWAudio.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/14/01 2:27p $*
  29. * *
  30. * $Revision:: 52 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "always.h"
  36. #include <Windows.H>
  37. #include "WWAudio.H"
  38. #include "WWDebug.H"
  39. #include "Utils.H"
  40. #include "RealCRC.H"
  41. #include "SoundBuffer.H"
  42. #include "AudibleSound.H"
  43. #include "Sound3D.H"
  44. #include "RawFile.H"
  45. #include "WW3D.H"
  46. #include "SoundScene.H"
  47. #include "SoundPseudo3D.H"
  48. #include "FFactory.H"
  49. #include "Registry.H"
  50. #include "Threads.H"
  51. #include "LogicalSound.h"
  52. #include "LogicalListener.h"
  53. #include "definitionclassids.h"
  54. #include "wwmemlog.h"
  55. #include "wwprofile.h"
  56. #ifdef G_CODE_BASE
  57. #include "..\wwlib\argv.h"
  58. #endif
  59. ////////////////////////////////////////////////////////////////////////////////////////////////
  60. // Static member initialization
  61. ////////////////////////////////////////////////////////////////////////////////////////////////
  62. WWAudioClass *WWAudioClass::_theInstance = NULL;
  63. HANDLE WWAudioClass::_TimerSyncEvent = NULL;
  64. ////////////////////////////////////////////////////////////////////////////////////////////////
  65. // Registry value names
  66. ////////////////////////////////////////////////////////////////////////////////////////////////
  67. const char *VALUE_NAME_IS_STEREO = "stereo";
  68. const char *VALUE_NAME_BITS = "bits";
  69. const char *VALUE_NAME_HERTZ = "hertz";
  70. const char *VALUE_NAME_DEVICE_NAME = "device name";
  71. const char *VALUE_NAME_MUSIC_ENABLED = "music enabled";
  72. const char *VALUE_NAME_SOUND_ENABLED = "sound enabled";
  73. const char *VALUE_NAME_MUSIC_VOL = "music volume";
  74. const char *VALUE_NAME_SOUND_VOL = "sound volume";
  75. ////////////////////////////////////////////////////////////////////////////////////////////////
  76. // Local inlines
  77. ////////////////////////////////////////////////////////////////////////////////////////////////
  78. __inline bool
  79. WWAudioClass::Is_OK_To_Give_Handle (const AudibleSoundClass &sound_obj)
  80. {
  81. bool is_ok = false;
  82. AudibleSoundClass::SOUND_TYPE type = sound_obj.Get_Type ();
  83. if (((type == AudibleSoundClass::TYPE_SOUND_EFFECT) && m_AreSoundEffectsEnabled) ||
  84. ((type == AudibleSoundClass::TYPE_MUSIC) && m_IsMusicEnabled)) {
  85. is_ok = true;
  86. }
  87. return is_ok;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////
  90. //
  91. // WWAudioClass
  92. //
  93. ////////////////////////////////////////////////////////////////////////////////////////////////
  94. WWAudioClass::WWAudioClass (void)
  95. : m_Driver2D (NULL),
  96. m_Driver3D (NULL),
  97. m_PlaybackRate (44100),
  98. m_PlaybackBits (16),
  99. m_PlaybackStereo (true),
  100. m_ReverbFilter (INVALID_MILES_HANDLE),
  101. m_UpdateTimer (-1),
  102. m_Driver3DPseudo (NULL),
  103. m_MusicVolume (DEF_MUSIC_VOL),
  104. m_SoundVolume (DEF_SFX_VOL),
  105. m_MaxCacheSize (DEF_CACHE_SIZE * 1024),
  106. m_CurrentCacheSize (0),
  107. m_Max2DSamples (DEF_2D_SAMPLE_COUNT),
  108. m_Max3DSamples (DEF_3D_SAMPLE_COUNT),
  109. m_Max2DBufferSize (DEF_MAX_2D_BUFFER_SIZE),
  110. m_Max3DBufferSize (DEF_MAX_3D_BUFFER_SIZE),
  111. m_SoundScene (NULL),
  112. m_IsMusicEnabled (true),
  113. m_AreSoundEffectsEnabled (true),
  114. m_FileFactory (NULL),
  115. m_EffectsLevel (0),
  116. m_ReverbRoomType (ENVIRONMENT_GENERIC)
  117. {
  118. ::InitializeCriticalSection (&MMSLockClass::_MSSLockCriticalSection);
  119. //
  120. // Start Miles Sound System
  121. //
  122. AIL_startup ();
  123. _theInstance = this;
  124. _TimerSyncEvent = ::CreateEvent (NULL, TRUE, FALSE, "WWAUDIO_TIMER_SYNC");
  125. //
  126. // Set some default values
  127. //
  128. Set_Sound_Effects_Volume ();
  129. Set_Music_Volume ();
  130. // Create a new sound scene to manage our 3D sounds...
  131. m_SoundScene = W3DNEW SoundSceneClass;
  132. return;
  133. }
  134. ////////////////////////////////////////////////////////////////////////////////////////////////
  135. //
  136. // ~WWAudioClass
  137. //
  138. ////////////////////////////////////////////////////////////////////////////////////////////////
  139. WWAudioClass::~WWAudioClass (void)
  140. {
  141. //
  142. // Make sure the delayed-release thread is terminated
  143. // before we exit (otherwise the process will crash).
  144. //
  145. WWAudioThreadsClass::End_Delayed_Release_Thread ();
  146. Shutdown ();
  147. _theInstance = NULL;
  148. ::CloseHandle(_TimerSyncEvent);
  149. _TimerSyncEvent = NULL;
  150. ::DeleteCriticalSection (&MMSLockClass::_MSSLockCriticalSection);
  151. //
  152. // Free the list of logical "types".
  153. //
  154. Reset_Logical_Types ();
  155. return;
  156. }
  157. ////////////////////////////////////////////////////////////////////////////////////////////////
  158. //
  159. // Flush_Cache
  160. //
  161. ////////////////////////////////////////////////////////////////////////////////////////////////
  162. void
  163. WWAudioClass::Flush_Cache (void)
  164. {
  165. // Loop through all the hash indicies
  166. for (int hash_index = 0; hash_index < MAX_CACHE_HASH; hash_index ++) {
  167. // Loop through all the buffers at this hash index and free them all
  168. for (int index = 0; index < m_CachedBuffers[hash_index].Count (); index ++) {
  169. CACHE_ENTRY_STRUCT &info = m_CachedBuffers[hash_index][index];
  170. // Free the buffer data
  171. SAFE_FREE (info.string_id);
  172. REF_PTR_RELEASE (info.buffer);
  173. }
  174. // Remove all the entries for this hash index
  175. m_CachedBuffers[hash_index].Delete_All ();
  176. }
  177. m_CurrentCacheSize = 0;
  178. return;
  179. }
  180. ////////////////////////////////////////////////////////////////////////////////////////////////
  181. //
  182. // Open_2D_Device
  183. //
  184. ////////////////////////////////////////////////////////////////////////////////////////////////
  185. WWAudioClass::DRIVER_TYPE_2D
  186. WWAudioClass::Open_2D_Device (LPWAVEFORMAT format)
  187. {
  188. MMSLockClass lock;
  189. //
  190. // Store the playback settings for future reference
  191. //
  192. m_PlaybackRate = format->nSamplesPerSec;
  193. m_PlaybackBits = (format->nAvgBytesPerSec << 3) / (format->nChannels * format->nSamplesPerSec);
  194. m_PlaybackStereo = bool(format->nChannels > 1);
  195. //
  196. // Assume we will open the DirectSound driver
  197. //
  198. DRIVER_TYPE_2D type = DRIVER2D_DSOUND;
  199. // First close the current 2D device and take
  200. // all the sound handles away from the sound objects.
  201. Close_2D_Device ();
  202. AIL_set_preference (AIL_LOCK_PROTECTION, NO);
  203. // Try to use DirectSound if possible
  204. S32 success = ::AIL_set_preference (DIG_USE_WAVEOUT, FALSE);
  205. WWASSERT (success == AIL_NO_ERROR);
  206. // Open the driver
  207. success = ::AIL_waveOutOpen (&m_Driver2D, NULL, 0, format);
  208. // Do we need to switch from direct sound to waveout?
  209. if ((success == AIL_NO_ERROR) &&
  210. (m_Driver2D != NULL) &&
  211. (m_Driver2D->emulated_ds == TRUE)) {
  212. ::AIL_waveOutClose (m_Driver2D);
  213. success = 2;
  214. WWDEBUG_SAY (("WWAudio: Detected 2D DirectSound emulation, switching to WaveOut.\r\n"));
  215. }
  216. // If we couldn't open the direct sound device, then use the
  217. // default wave out device
  218. if (success != AIL_NO_ERROR) {
  219. // Try to use the default wave out driver
  220. success = ::AIL_set_preference (DIG_USE_WAVEOUT, TRUE);
  221. WWASSERT (success == AIL_NO_ERROR);
  222. // Open the driver
  223. success = ::AIL_waveOutOpen (&m_Driver2D, NULL, 0, format);
  224. WWASSERT (success == AIL_NO_ERROR);
  225. type = (success == AIL_NO_ERROR) ? DRIVER2D_WAVEOUT : DRIVER2D_ERROR;
  226. }
  227. // Allocate all the available handles if we were successful
  228. if (success == AIL_NO_ERROR) {
  229. Allocate_2D_Handles ();
  230. ReAssign_2D_Handles ();
  231. } else {
  232. Close_2D_Device ();
  233. WWDEBUG_SAY (("WWAudio: Error initializing 2D device.\r\n"));
  234. }
  235. // Return the opened device type
  236. return type;
  237. }
  238. ////////////////////////////////////////////////////////////////////////////////////////////////
  239. //
  240. // Open_2D_Device
  241. //
  242. ////////////////////////////////////////////////////////////////////////////////////////////////
  243. WWAudioClass::DRIVER_TYPE_2D
  244. WWAudioClass::Open_2D_Device
  245. (
  246. bool stereo,
  247. int bits,
  248. int hertz
  249. )
  250. {
  251. // Build a wave format structure from the params
  252. PCMWAVEFORMAT wave_format = { 0 };
  253. wave_format.wf.wFormatTag = WAVE_FORMAT_PCM;
  254. wave_format.wf.nChannels = stereo ? 2 : 1;
  255. wave_format.wf.nSamplesPerSec = hertz;
  256. wave_format.wf.nAvgBytesPerSec = (wave_format.wf.nChannels * wave_format.wf.nSamplesPerSec * bits) >> 3;
  257. wave_format.wf.nBlockAlign = (wave_format.wf.nChannels * bits) >> 3;
  258. wave_format.wBitsPerSample = bits;
  259. DRIVER_TYPE_2D type = DRIVER2D_ERROR;
  260. while (((type = Open_2D_Device ((LPWAVEFORMAT)&wave_format)) == DRIVER2D_ERROR) &&
  261. (wave_format.wf.nSamplesPerSec >= 11025)) {
  262. //
  263. // Cut the playback rate in half and try again
  264. //
  265. wave_format.wf.nSamplesPerSec = wave_format.wf.nSamplesPerSec >> 1;
  266. wave_format.wf.nAvgBytesPerSec = (wave_format.wf.nChannels * wave_format.wf.nSamplesPerSec * bits) >> 3;
  267. wave_format.wf.nBlockAlign = (wave_format.wf.nChannels * bits) >> 3;
  268. }
  269. // Pass this structure onto the function that actually opens the device
  270. return type;
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////////////////////
  273. //
  274. // Close_2D_Device
  275. //
  276. ////////////////////////////////////////////////////////////////////////////////////////////////
  277. bool
  278. WWAudioClass::Close_2D_Device (void)
  279. {
  280. MMSLockClass lock;
  281. bool retval = false;
  282. //
  283. // Free any 2D sound handles
  284. //
  285. Remove_2D_Sound_Handles ();
  286. Release_2D_Handles ();
  287. //
  288. // Do we have an open driver handle to close?
  289. //
  290. if (m_Driver2D != NULL) {
  291. //
  292. // Close the driver
  293. //
  294. ::AIL_waveOutClose (m_Driver2D);
  295. m_Driver2D = NULL;
  296. retval = true;
  297. }
  298. //
  299. // Return the true/false result code
  300. //
  301. return retval;
  302. }
  303. ////////////////////////////////////////////////////////////////////////////////////////////////
  304. //
  305. // Get_Sound_Buffer
  306. //
  307. ////////////////////////////////////////////////////////////////////////////////////////////////
  308. SoundBufferClass *
  309. WWAudioClass::Get_Sound_Buffer (const char *filename, bool is_3d)
  310. {
  311. //
  312. // Try to find the buffer in our cache, otherwise create a new buffer.
  313. //
  314. SoundBufferClass *buffer = Find_Cached_Buffer (filename);
  315. if (buffer == NULL) {
  316. FileClass *file = Get_File (filename);
  317. if (file != NULL && file->Is_Available ()) {
  318. buffer = Create_Sound_Buffer (*file, filename, is_3d);
  319. } else {
  320. static int count = 0;
  321. if ( ++count < 10 ) {
  322. WWDEBUG_SAY(( "Sound \"%s\" not found\r\n", filename ));
  323. }
  324. }
  325. Return_File (file);
  326. }
  327. return buffer;
  328. }
  329. ////////////////////////////////////////////////////////////////////////////////////////////////
  330. //
  331. // Get_Sound_Buffer
  332. //
  333. ////////////////////////////////////////////////////////////////////////////////////////////////
  334. SoundBufferClass *
  335. WWAudioClass::Get_Sound_Buffer (FileClass &file, const char *string_id, bool is_3d)
  336. {
  337. WWMEMLOG(MEM_SOUND);
  338. //
  339. // Try to find the buffer in our cache, otherwise create a new buffer.
  340. //
  341. SoundBufferClass *buffer = Find_Cached_Buffer (string_id);
  342. if (buffer == NULL) {
  343. buffer = Create_Sound_Buffer (file, string_id, is_3d);
  344. }
  345. return buffer;
  346. }
  347. ////////////////////////////////////////////////////////////////////////////////////////////////
  348. //
  349. // Find_Cached_Buffer
  350. //
  351. ////////////////////////////////////////////////////////////////////////////////////////////////
  352. SoundBufferClass *
  353. WWAudioClass::Find_Cached_Buffer (const char *string_id)
  354. {
  355. SoundBufferClass *sound_buffer = NULL;
  356. // Param OK?
  357. WWASSERT (string_id != NULL);
  358. if (string_id != NULL) {
  359. //
  360. // Determine which index in our hash table to use
  361. //
  362. int hash_index = ::CRC_Stringi (string_id) & CACHE_HASH_MASK;
  363. //
  364. // Loop through all the buffers at this hash index and try to find
  365. // one that matches the requested name
  366. //
  367. for (int index = 0; index < m_CachedBuffers[hash_index].Count (); index ++) {
  368. //
  369. // Is this the sound buffer we were looking for?
  370. //
  371. CACHE_ENTRY_STRUCT &info = m_CachedBuffers[hash_index][index];
  372. if (::lstrcmpi (info.string_id, string_id) == 0) {
  373. sound_buffer = info.buffer;
  374. sound_buffer->Add_Ref ();
  375. break;
  376. }
  377. }
  378. }
  379. //
  380. // Return a pointer to the cached sound buffer
  381. //
  382. return sound_buffer;
  383. }
  384. ////////////////////////////////////////////////////////////////////////////////////////////////
  385. //
  386. // Free_Cache_Space
  387. //
  388. ////////////////////////////////////////////////////////////////////////////////////////////////
  389. bool
  390. WWAudioClass::Free_Cache_Space (int bytes)
  391. {
  392. int bytes_freed = 0;
  393. // Loop through all the hash indicies
  394. for (int hash_index = 0;
  395. (hash_index < MAX_CACHE_HASH) && (bytes_freed < bytes);
  396. hash_index ++) {
  397. // Loop through all the buffers at this hash index
  398. for (int index = 0;
  399. (index < m_CachedBuffers[hash_index].Count ()) && (bytes_freed < bytes);
  400. index ++) {
  401. // Can we free this cached buffer?
  402. CACHE_ENTRY_STRUCT &info = m_CachedBuffers[hash_index][index];
  403. if ((info.buffer != NULL) && (info.buffer->Num_Refs () == 1)) {
  404. // Add the size of this buffer to our count of bytes freed
  405. bytes_freed += info.buffer->Get_Raw_Length ();
  406. // Free the buffer data
  407. SAFE_FREE (info.string_id);
  408. REF_PTR_RELEASE (info.buffer);
  409. // Remove this entry from the hash table
  410. m_CachedBuffers[hash_index].Delete (index);
  411. index --;
  412. }
  413. }
  414. }
  415. // Make sure to recompute out current cache size
  416. m_CurrentCacheSize -= bytes_freed;
  417. WWASSERT (m_CurrentCacheSize >= 0);
  418. // Return true if we freed enough bytes in the cache
  419. return (bytes_freed >= bytes);
  420. }
  421. ////////////////////////////////////////////////////////////////////////////////////////////////
  422. //
  423. // Cache_Buffer
  424. //
  425. ////////////////////////////////////////////////////////////////////////////////////////////////
  426. bool
  427. WWAudioClass::Cache_Buffer
  428. (
  429. SoundBufferClass *buffer,
  430. const char *string_id
  431. )
  432. {
  433. // Assume failure
  434. bool retval = false;
  435. // Params OK?
  436. WWASSERT (buffer != NULL);
  437. WWASSERT (string_id != NULL);
  438. if ((buffer != NULL) &&
  439. (string_id != NULL) &&
  440. (buffer->Get_Raw_Length () < (U32)(m_MaxCacheSize / 2))) {
  441. // Attempt to free space in the cache (if needed)
  442. int space_needed = (m_CurrentCacheSize + buffer->Get_Raw_Length ()) - (int)m_MaxCacheSize;
  443. if (space_needed > 0) {
  444. Free_Cache_Space (space_needed);
  445. }
  446. // Do we have enough space in the cache for this buffer?
  447. space_needed = (m_CurrentCacheSize + buffer->Get_Raw_Length ()) - (int)m_MaxCacheSize;
  448. if (space_needed <= 0) {
  449. // Determine which index in our hash table to use
  450. int hash_index = ::CRC_Stringi (string_id) & CACHE_HASH_MASK;
  451. //
  452. // Add this buffer to the hash table at the given index.
  453. // Note: The assignment operator caused by the Add call
  454. // will add a reference to the sound buffer.
  455. //
  456. CACHE_ENTRY_STRUCT info;
  457. info.string_id = (char *)string_id;
  458. info.buffer = buffer;
  459. m_CachedBuffers[hash_index].Add (info);
  460. // Update our current cache size
  461. m_CurrentCacheSize += buffer->Get_Raw_Length ();
  462. retval = true;
  463. }
  464. }
  465. // Return the true/false result code
  466. return retval;
  467. }
  468. ////////////////////////////////////////////////////////////////////////////////////////////////
  469. //
  470. // Create_Sound_Buffer
  471. //
  472. ////////////////////////////////////////////////////////////////////////////////////////////////
  473. SoundBufferClass *
  474. WWAudioClass::Create_Sound_Buffer
  475. (
  476. FileClass & file,
  477. const char *string_id,
  478. bool is_3d
  479. )
  480. {
  481. SoundBufferClass *sound_buffer = NULL;
  482. //
  483. // Determine how large this buffer can be
  484. //
  485. int max_size = is_3d ? m_Max3DBufferSize : m_Max2DBufferSize;
  486. //
  487. // Create a streaming sound buffer object if the
  488. // file is too large to preload.
  489. //
  490. if (file.Size () > max_size) {
  491. sound_buffer = W3DNEW StreamSoundBufferClass;
  492. } else {
  493. sound_buffer = W3DNEW SoundBufferClass;
  494. }
  495. SET_REF_OWNER(sound_buffer);
  496. //
  497. // Create a new sound buffer from the provided file
  498. //
  499. bool success = sound_buffer->Load_From_File (file);
  500. sound_buffer->Set_Filename (string_id);
  501. WWASSERT (success);
  502. // If we were successful in creating the sound buffer, then
  503. // try to cache it as well, otherwise free the buffer and return NULL.
  504. if (success && (string_id != NULL)) {
  505. Cache_Buffer (sound_buffer, string_id);
  506. } else if (success == false) {
  507. REF_PTR_RELEASE (sound_buffer);
  508. }
  509. // Return a pointer to the new sound buffer
  510. return sound_buffer;
  511. }
  512. ////////////////////////////////////////////////////////////////////////////////////////////////
  513. //
  514. // Create_Sound_Buffer
  515. //
  516. ////////////////////////////////////////////////////////////////////////////////////////////////
  517. SoundBufferClass *
  518. WWAudioClass::Create_Sound_Buffer
  519. (
  520. unsigned char * file_image,
  521. unsigned long bytes,
  522. const char * string_id,
  523. bool is_3d
  524. )
  525. {
  526. // Create a new sound buffer from the provided file
  527. SoundBufferClass *sound_buffer = W3DNEW SoundBufferClass;
  528. SET_REF_OWNER(sound_buffer);
  529. //
  530. // Initialize the sound from this piece of memory
  531. //
  532. bool success = sound_buffer->Load_From_Memory (file_image, bytes);
  533. sound_buffer->Set_Filename (string_id);
  534. WWASSERT (success);
  535. // If we were successful in creating the sound buffer, then
  536. // try to cache it as well, otherwise free the buffer and return NULL.
  537. if (success && (string_id != NULL)) {
  538. Cache_Buffer (sound_buffer, string_id);
  539. } else if (success == false) {
  540. REF_PTR_RELEASE (sound_buffer);
  541. }
  542. // Return a pointer to the new sound buffer
  543. return sound_buffer;
  544. }
  545. ////////////////////////////////////////////////////////////////////////////////////////////////
  546. //
  547. // Create_Sound_Effect
  548. //
  549. ////////////////////////////////////////////////////////////////////////////////////////////////
  550. AudibleSoundClass *
  551. WWAudioClass::Create_Sound_Effect (FileClass &file, const char *string_id)
  552. {
  553. // Create a new sound object
  554. AudibleSoundClass *sound_obj = W3DNEW AudibleSoundClass;
  555. if (Is_Disabled () == false) {
  556. // Try to find the buffer in our cache, otherwise create a new buffer.
  557. SoundBufferClass *buffer = Get_Sound_Buffer (file, string_id, false);
  558. // Pass the actual sound data onto the sound object
  559. sound_obj->Set_Buffer (buffer);
  560. REF_PTR_RELEASE (buffer);
  561. }
  562. // Return a pointer to the sound effect
  563. return sound_obj;
  564. }
  565. ////////////////////////////////////////////////////////////////////////////////////////////////
  566. //
  567. // Create_Sound_Effect
  568. //
  569. ////////////////////////////////////////////////////////////////////////////////////////////////
  570. AudibleSoundClass *
  571. WWAudioClass::Create_Sound_Effect (const char *filename)
  572. {
  573. // Assume failure
  574. AudibleSoundClass *sound_obj = NULL;
  575. if (Is_Disabled () == false) {
  576. // Param OK?
  577. WWASSERT (filename != NULL);
  578. if (filename != NULL) {
  579. // Create a file object and pass it onto the appropriate function
  580. FileClass *file = Get_File (filename);
  581. if (file && file->Is_Available()) {
  582. sound_obj = Create_Sound_Effect (*file, filename);
  583. } else {
  584. WWDEBUG_SAY(( "Sound %s not found\r\n", filename ));
  585. }
  586. Return_File (file);
  587. }
  588. }
  589. // Return a pointer to the sound effect
  590. return sound_obj;
  591. }
  592. ////////////////////////////////////////////////////////////////////////////////////////////////
  593. //
  594. // Create_Sound_Effect
  595. //
  596. ////////////////////////////////////////////////////////////////////////////////////////////////
  597. AudibleSoundClass *
  598. WWAudioClass::Create_Sound_Effect
  599. (
  600. const char * string_id,
  601. unsigned char *raw_wave_data,
  602. unsigned long bytes
  603. )
  604. {
  605. // Create a new sound object
  606. AudibleSoundClass *sound_obj = W3DNEW AudibleSoundClass;
  607. if (Is_Disabled () == false) {
  608. // Try to find the buffer in our cache, otherwise create a new buffer.
  609. SoundBufferClass *buffer = Find_Cached_Buffer (string_id);
  610. if (buffer == NULL) {
  611. buffer = Create_Sound_Buffer (raw_wave_data, bytes, string_id, false);
  612. }
  613. // Pass the actual sound data onto the sound object
  614. sound_obj->Set_Buffer (buffer);
  615. REF_PTR_RELEASE (buffer);
  616. }
  617. // Return a pointer to the sound effect
  618. return sound_obj;
  619. }
  620. /////////////////////////////////////////////////////////////////////////////////
  621. //
  622. // Create_3D_Sound
  623. //
  624. ////////////////////////////////////////////////////////////////////////////////////////////////
  625. Sound3DClass *
  626. WWAudioClass::Create_3D_Sound (FileClass &file, const char *string_id, int classid_hint)
  627. {
  628. Sound3DClass *sound_obj = NULL;
  629. if (Is_Disabled () == false) {
  630. // Try to find the buffer in our cache, otherwise create a new buffer.
  631. SoundBufferClass *buffer = Get_Sound_Buffer (file, string_id, true);
  632. //
  633. // What type of sound object should we create? A true 3D sound or one of
  634. // our pseudo-3d sounds? (volume and panning only)
  635. //
  636. if ( classid_hint == CLASSID_PSEUDO3D ||
  637. Validate_3D_Sound_Buffer (buffer) == false)
  638. {
  639. sound_obj = W3DNEW SoundPseudo3DClass;
  640. sound_obj->Set_Buffer (buffer);
  641. } else if (buffer != NULL) {
  642. sound_obj = W3DNEW Sound3DClass;
  643. sound_obj->Set_Buffer (buffer);
  644. }
  645. REF_PTR_RELEASE (buffer);
  646. }
  647. // Return a pointer to the sound effect
  648. return sound_obj;
  649. }
  650. ////////////////////////////////////////////////////////////////////////////////////////////////
  651. //
  652. // Create_3D_Sound
  653. //
  654. ////////////////////////////////////////////////////////////////////////////////////////////////
  655. Sound3DClass *
  656. WWAudioClass::Create_3D_Sound
  657. (
  658. const char * filename,
  659. int classid_hint
  660. )
  661. {
  662. WWMEMLOG(MEM_SOUND);
  663. // Assume failure
  664. Sound3DClass *sound_obj = NULL;
  665. if (Is_Disabled () == false) {
  666. // Param OK?
  667. WWASSERT (filename != NULL);
  668. if (filename != NULL) {
  669. // Try to find the buffer in our cache, otherwise create a new buffer.
  670. SoundBufferClass *buffer = Get_Sound_Buffer (filename, true);
  671. //
  672. // What type of sound object should we create? A true 3D sound or one of
  673. // our pseudo-3d sounds? (volume and panning only)
  674. //
  675. if ( classid_hint == CLASSID_PSEUDO3D ||
  676. Validate_3D_Sound_Buffer (buffer) == false)
  677. {
  678. sound_obj = W3DNEW SoundPseudo3DClass;
  679. sound_obj->Set_Buffer (buffer);
  680. } else if (buffer != NULL) {
  681. sound_obj = W3DNEW Sound3DClass;
  682. sound_obj->Set_Buffer (buffer);
  683. } else {
  684. static int count = 0;
  685. if ( ++count < 10 ) {
  686. WWDEBUG_SAY(( "Sound File not Found \"%s\"\r\n", filename ));
  687. }
  688. }
  689. REF_PTR_RELEASE (buffer);
  690. }
  691. }
  692. // Return a pointer to the sound effect
  693. return sound_obj;
  694. }
  695. ////////////////////////////////////////////////////////////////////////////////////////////////
  696. //
  697. // Create_3D_Sound
  698. //
  699. ////////////////////////////////////////////////////////////////////////////////////////////////
  700. Sound3DClass *
  701. WWAudioClass::Create_3D_Sound
  702. (
  703. const char * string_id,
  704. unsigned char * raw_wave_data,
  705. unsigned long bytes,
  706. int classid_hint
  707. )
  708. {
  709. Sound3DClass *sound_obj = NULL;
  710. if (Is_Disabled () == false) {
  711. //
  712. // Try to find the buffer in our cache, otherwise create a new buffer.
  713. //
  714. SoundBufferClass *buffer = Find_Cached_Buffer (string_id);
  715. if (buffer == NULL) {
  716. buffer = Create_Sound_Buffer (raw_wave_data, bytes, string_id, true);
  717. }
  718. //
  719. // What type of sound object should we create? A true 3D sound or one of
  720. // our pseudo-3d sounds? (volume and panning only)
  721. //
  722. if ( classid_hint == CLASSID_PSEUDO3D ||
  723. Validate_3D_Sound_Buffer (buffer) == false)
  724. {
  725. sound_obj = W3DNEW SoundPseudo3DClass;
  726. sound_obj->Set_Buffer (buffer);
  727. } else if (buffer != NULL) {
  728. sound_obj = W3DNEW Sound3DClass;
  729. sound_obj->Set_Buffer (buffer);
  730. }
  731. REF_PTR_RELEASE (buffer);
  732. }
  733. // Return a pointer to the sound effect
  734. return sound_obj;
  735. }
  736. ////////////////////////////////////////////////////////////////////////////////////////////////
  737. //
  738. // Create_Sound
  739. //
  740. ////////////////////////////////////////////////////////////////////////////////////////////////
  741. AudibleSoundClass *
  742. WWAudioClass::Create_Sound
  743. (
  744. int definition_id,
  745. RefCountClass *user_obj,
  746. uint32 user_data,
  747. int classid_hint
  748. )
  749. {
  750. AudibleSoundClass *sound = NULL;
  751. //
  752. // Find the definition
  753. //
  754. DefinitionClass *definition = DefinitionMgrClass::Find_Definition (definition_id);
  755. if (definition != NULL ) {
  756. //
  757. // Make sure this is really a sound definition
  758. //
  759. WWASSERT (definition->Get_Class_ID () == CLASSID_SOUND);
  760. if (definition->Get_Class_ID () == CLASSID_SOUND) {
  761. AudibleSoundDefinitionClass *sound_def = reinterpret_cast<AudibleSoundDefinitionClass *> (definition);
  762. //
  763. // Create an instance of the sound
  764. //
  765. sound = sound_def->Create_Sound (classid_hint);
  766. if (sound != NULL) {
  767. sound->Set_User_Data (user_obj, user_data);
  768. }
  769. }
  770. }
  771. return sound;
  772. }
  773. ////////////////////////////////////////////////////////////////////////////////////////////////
  774. //
  775. // Create_Sound
  776. //
  777. ////////////////////////////////////////////////////////////////////////////////////////////////
  778. AudibleSoundClass *
  779. WWAudioClass::Create_Sound
  780. (
  781. const char * def_name,
  782. RefCountClass *user_obj,
  783. uint32 user_data,
  784. int classid_hint
  785. )
  786. {
  787. AudibleSoundClass *sound = NULL;
  788. //
  789. // Find the definition
  790. //
  791. DefinitionClass *definition = DefinitionMgrClass::Find_Typed_Definition (def_name, CLASSID_SOUND, true);
  792. if (definition != NULL ) {
  793. //
  794. // Make sure this is really a sound definition
  795. //
  796. WWASSERT (definition->Get_Class_ID () == CLASSID_SOUND);
  797. if (definition->Get_Class_ID () == CLASSID_SOUND) {
  798. AudibleSoundDefinitionClass *sound_def = reinterpret_cast<AudibleSoundDefinitionClass *> (definition);
  799. //
  800. // Create an instance of the sound
  801. //
  802. sound = sound_def->Create_Sound (classid_hint);
  803. if (sound != NULL) {
  804. sound->Set_User_Data (user_obj, user_data);
  805. }
  806. }
  807. }
  808. return sound;
  809. }
  810. ////////////////////////////////////////////////////////////////////////////////////////////////
  811. //
  812. // Create_Continuous_Sound
  813. //
  814. ////////////////////////////////////////////////////////////////////////////////////////////////
  815. AudibleSoundClass *
  816. WWAudioClass::Create_Continuous_Sound
  817. (
  818. int definition_id,
  819. RefCountClass *user_obj,
  820. uint32 user_data,
  821. int classid_hint
  822. )
  823. {
  824. //
  825. // Create an instance of the sound and play it
  826. //
  827. AudibleSoundClass *sound = Create_Sound (definition_id, user_obj, user_data, classid_hint);
  828. if (sound != NULL) {
  829. if (sound->Get_Loop_Count () != INFINITE_LOOPS) {
  830. WWDEBUG_SAY (("Audio Error: Creating a continuous sound with a finite loop count!\r\n"));
  831. }
  832. }
  833. return sound;
  834. }
  835. ////////////////////////////////////////////////////////////////////////////////////////////////
  836. //
  837. // Create_Instant_Sound
  838. //
  839. ////////////////////////////////////////////////////////////////////////////////////////////////
  840. int
  841. WWAudioClass::Create_Instant_Sound
  842. (
  843. int definition_id,
  844. const Matrix3D & tm,
  845. RefCountClass * user_obj,
  846. uint32 user_data,
  847. int classid_hint
  848. )
  849. {
  850. int sound_id = 0;
  851. //
  852. // Create an instance of the sound and play it
  853. //
  854. AudibleSoundClass *sound = Create_Sound (definition_id, user_obj, user_data, classid_hint);
  855. if (sound != NULL) {
  856. if (sound->Get_Loop_Count () == INFINITE_LOOPS) {
  857. WWDEBUG_SAY (("Audio Error: Creating an instant sound %s with an infinite loop count!\r\n",sound->Get_Definition()->Get_Name()));
  858. }
  859. sound_id = sound->Get_ID ();
  860. sound->Set_Transform (tm);
  861. sound->Add_To_Scene ();
  862. sound->Release_Ref ();
  863. }
  864. return sound_id;
  865. }
  866. ////////////////////////////////////////////////////////////////////////////////////////////////
  867. //
  868. // Create_Continuous_Sound
  869. //
  870. ////////////////////////////////////////////////////////////////////////////////////////////////
  871. AudibleSoundClass *
  872. WWAudioClass::Create_Continuous_Sound
  873. (
  874. const char * def_name,
  875. RefCountClass *user_obj,
  876. uint32 user_data,
  877. int classid_hint
  878. )
  879. {
  880. //
  881. // Create an instance of the sound and play it
  882. //
  883. AudibleSoundClass *sound = Create_Sound (def_name, user_obj, user_data, classid_hint);
  884. if (sound != NULL) {
  885. if (sound->Get_Loop_Count () != INFINITE_LOOPS) {
  886. WWDEBUG_SAY (("Audio Error: Creating a continuous sound with a finite loop count!\r\n"));
  887. }
  888. }
  889. return sound;
  890. }
  891. ////////////////////////////////////////////////////////////////////////////////////////////////
  892. //
  893. // Create_Instant_Sound
  894. //
  895. ////////////////////////////////////////////////////////////////////////////////////////////////
  896. int
  897. WWAudioClass::Create_Instant_Sound
  898. (
  899. const char * def_name,
  900. const Matrix3D & tm,
  901. RefCountClass * user_obj,
  902. uint32 user_data,
  903. int classid_hint
  904. )
  905. {
  906. int sound_id = 0;
  907. //
  908. // Create an instance of the sound and play it
  909. //
  910. AudibleSoundClass *sound = Create_Sound (def_name, user_obj, user_data, classid_hint);
  911. if (sound != NULL) {
  912. if (sound->Get_Loop_Count () == INFINITE_LOOPS) {
  913. WWDEBUG_SAY (("Audio Error: Creating an instant sound %s with an infinite loop count!\r\n",sound->Get_Definition()->Get_Name()));
  914. }
  915. sound_id = sound->Get_ID ();
  916. sound->Set_Transform (tm);
  917. sound->Add_To_Scene ();
  918. sound->Release_Ref ();
  919. }
  920. return sound_id;
  921. }
  922. ////////////////////////////////////////////////////////////////////////////////////////////////
  923. //
  924. // Flush_Playlist
  925. //
  926. ////////////////////////////////////////////////////////////////////////////////////////////////
  927. void
  928. WWAudioClass::Flush_Playlist (void)
  929. {
  930. // First make sure to free any completed sounds
  931. Free_Completed_Sounds ();
  932. // Loop through all the entries in the playlist
  933. for (int index = 0; index < m_Playlist.Count (); index ++) {
  934. AudibleSoundClass *sound_obj = m_Playlist[index];
  935. REF_PTR_RELEASE (sound_obj);
  936. }
  937. // Free the list structure
  938. m_Playlist.Delete_All ();
  939. return;
  940. }
  941. ////////////////////////////////////////////////////////////////////////////////////////////////
  942. //
  943. // Free_Completed_Sounds
  944. //
  945. ////////////////////////////////////////////////////////////////////////////////////////////////
  946. void
  947. WWAudioClass::Free_Completed_Sounds (void)
  948. {
  949. if (m_CompletedSounds.Count () > 0) {
  950. // Loop through all the entries in the completed sounds list
  951. for (int index = 0; index < m_CompletedSounds.Count (); index ++) {
  952. AudibleSoundClass *sound_obj = m_CompletedSounds[index];
  953. WWASSERT(sound_obj != NULL); //TSS 05/24/99
  954. // Remove this sound from the playlist
  955. bool found = false;
  956. for (int play_index = 0; (play_index < m_Playlist.Count ()) && !found; play_index ++) {
  957. if (m_Playlist[play_index] == sound_obj) {
  958. // Free our hold on this sound object
  959. m_Playlist.Delete (play_index);
  960. REF_PTR_RELEASE (sound_obj);
  961. found = true;
  962. }
  963. }
  964. }
  965. // Free the list structure
  966. m_CompletedSounds.Delete_All ();
  967. // Try to give a play-handle back to a sound that was priority-bumped.
  968. Reprioritize_Playlist ();
  969. }
  970. return;
  971. }
  972. ////////////////////////////////////////////////////////////////////////////////////////////////
  973. //
  974. // Get_Playlist_Entry
  975. //
  976. ////////////////////////////////////////////////////////////////////////////////////////////////
  977. AudibleSoundClass *
  978. WWAudioClass::Get_Playlist_Entry (int index) const
  979. {
  980. AudibleSoundClass *sound_obj = NULL;
  981. // Params OK?
  982. WWASSERT (index >= 0 && index < m_Playlist.Count ());
  983. if ((index >= 0) && (index < m_Playlist.Count ())) {
  984. m_Playlist[index]->Add_Ref ();
  985. m_Playlist[index];
  986. }
  987. // Return a pointer to the sound object
  988. return sound_obj;
  989. }
  990. ////////////////////////////////////////////////////////////////////////////////////////////
  991. //
  992. // Add_To_Playlist
  993. //
  994. ////////////////////////////////////////////////////////////////////////////////////////////////
  995. bool
  996. WWAudioClass::Add_To_Playlist (AudibleSoundClass *sound)
  997. {
  998. // Assume failure
  999. bool retval = false;
  1000. WWASSERT (sound != NULL);
  1001. if (sound != NULL) {
  1002. // Loop through all the entries in the playlist
  1003. bool already_added = false;
  1004. for (int index = 0; (index < m_Playlist.Count ()) && (already_added == false); index ++) {
  1005. already_added = (sound == m_Playlist[index]);
  1006. }
  1007. // Add this sound to our playlist
  1008. if (already_added == false) {
  1009. sound->Add_Ref ();
  1010. m_Playlist.Add (sound);
  1011. }
  1012. }
  1013. // Return the true/false result code
  1014. return retval;
  1015. }
  1016. ////////////////////////////////////////////////////////////////////////////////////////////
  1017. //
  1018. // Remove_From_Playlist
  1019. //
  1020. ////////////////////////////////////////////////////////////////////////////////////////////////
  1021. bool
  1022. WWAudioClass::Remove_From_Playlist (AudibleSoundClass *sound_obj)
  1023. {
  1024. // Assume failure
  1025. bool retval = false;
  1026. WWASSERT (sound_obj != NULL);
  1027. if (sound_obj != NULL) {
  1028. // Loop through all the entries in the playlist
  1029. for (int index = 0; (index < m_Playlist.Count ()) && (retval == false); index ++) {
  1030. // Is this the entry we are looking for?
  1031. if (sound_obj == m_Playlist[index]) {
  1032. // Add this sound to the 'completed' list
  1033. m_CompletedSounds.Add (sound_obj);
  1034. retval = true;
  1035. }
  1036. }
  1037. //
  1038. // Notify any callbacks that this sound is ending...
  1039. //
  1040. if (sound_obj->Get_Loop_Count () != INFINITE_LOOPS) {
  1041. for (index = 0; index < m_EOSCallbackList.Count (); index ++) {
  1042. uint32 user_data = NULL;
  1043. LPFNEOSCALLBACK callback = m_EOSCallbackList.Get_Callback (index, &user_data);
  1044. if (callback != NULL) {
  1045. (*callback) (sound_obj, user_data);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. // Return the true/false result code
  1051. return retval;
  1052. }
  1053. ////////////////////////////////////////////////////////////////////////////////////////////
  1054. //
  1055. // Is_Sound_In_Playlist
  1056. //
  1057. ////////////////////////////////////////////////////////////////////////////////////////////////
  1058. bool
  1059. WWAudioClass::Is_Sound_In_Playlist (AudibleSoundClass *sound_obj)
  1060. {
  1061. // Assume failure
  1062. bool retval = false;
  1063. // Loop through all the entries in the playlist
  1064. for (int index = 0; (index < m_Playlist.Count ()) && (retval == false); index ++) {
  1065. if (sound_obj == m_Playlist[index]) {
  1066. retval = true;
  1067. }
  1068. }
  1069. // Return the true/false result code
  1070. return retval;
  1071. }
  1072. ////////////////////////////////////////////////////////////////////////////////////////////
  1073. //
  1074. // Reprioritize_Playlist
  1075. //
  1076. ////////////////////////////////////////////////////////////////////////////////////////////////
  1077. void
  1078. WWAudioClass::Reprioritize_Playlist (void)
  1079. {
  1080. AudibleSoundClass *sound_to_get_handle = NULL;
  1081. float hightest_priority = 0;
  1082. // Loop through all the entries in the playlist
  1083. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1084. // Is this the highest priority without a miles handle?
  1085. AudibleSoundClass *sound_obj = m_Playlist[index];
  1086. if ((sound_obj->Get_Miles_Handle () == NULL) &&
  1087. (sound_obj->Is_Sound_Culled () == false) &&
  1088. (sound_obj->Get_Priority () > hightest_priority))
  1089. {
  1090. // This is now the highest priority sound effect without
  1091. // a play-handle.
  1092. sound_to_get_handle = sound_obj;
  1093. hightest_priority = sound_obj->Get_Priority ();
  1094. }
  1095. }
  1096. // Get a new handle for this sound if necessary
  1097. if (sound_to_get_handle != NULL) {
  1098. sound_to_get_handle->Allocate_Miles_Handle ();
  1099. }
  1100. return;
  1101. }
  1102. ////////////////////////////////////////////////////////////////////////////////////////////
  1103. //
  1104. // On_Frame_Update
  1105. //
  1106. ////////////////////////////////////////////////////////////////////////////////////////////////
  1107. void
  1108. WWAudioClass::On_Frame_Update (unsigned int milliseconds)
  1109. {
  1110. //
  1111. // Free any sounds we completed last frame
  1112. //
  1113. Free_Completed_Sounds ();
  1114. //
  1115. // Calculate the time in ms since the last frame
  1116. //
  1117. unsigned int time_delta = milliseconds;
  1118. if (time_delta == 0) {
  1119. time_delta = WW3D::Get_Frame_Time ();
  1120. }
  1121. if (m_SoundScene != NULL) {
  1122. m_SoundScene->On_Frame_Update (milliseconds);
  1123. m_SoundScene->Collect_Logical_Sounds ();
  1124. }
  1125. //
  1126. // Loop through all the entries in the playlist
  1127. //
  1128. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1129. //
  1130. // Update this sound object
  1131. //
  1132. AudibleSoundClass *sound_obj = m_Playlist[index];
  1133. sound_obj->On_Frame_Update (time_delta);
  1134. }
  1135. return;
  1136. }
  1137. ////////////////////////////////////////////////////////////////////////////////////////////
  1138. //
  1139. // Release_2D_Handles
  1140. //
  1141. ////////////////////////////////////////////////////////////////////////////////////////////////
  1142. void
  1143. WWAudioClass::Release_2D_Handles (void)
  1144. {
  1145. MMSLockClass lock;
  1146. // Release our hold on all the samples we've allocated
  1147. for (int index = 0; index < m_2DSampleHandles.Count (); index ++) {
  1148. HSAMPLE sample = m_2DSampleHandles[index];
  1149. if (sample != NULL) {
  1150. ::AIL_release_sample_handle (sample);
  1151. }
  1152. }
  1153. m_2DSampleHandles.Delete_All ();
  1154. return;
  1155. }
  1156. ////////////////////////////////////////////////////////////////////////////////////////////
  1157. //
  1158. // Allocate_2D_Handles
  1159. //
  1160. ////////////////////////////////////////////////////////////////////////////////////////////////
  1161. void
  1162. WWAudioClass::Allocate_2D_Handles (void)
  1163. {
  1164. MMSLockClass lock;
  1165. // Start fresh
  1166. Release_2D_Handles ();
  1167. if (m_Driver2D != NULL) {
  1168. // Attempt to allocate our share of 2D sample handles
  1169. for (int index = 0; index < m_Max2DSamples; index ++) {
  1170. HSAMPLE sample = ::AIL_allocate_sample_handle (m_Driver2D);
  1171. if (sample != NULL) {
  1172. ::AIL_set_sample_user_data (sample, INFO_OBJECT_PTR, NULL);
  1173. m_2DSampleHandles.Add (sample);
  1174. }
  1175. }
  1176. // Record our actual number of available 2D sample handles
  1177. m_Max2DSamples = m_2DSampleHandles.Count ();
  1178. }
  1179. return;
  1180. }
  1181. ////////////////////////////////////////////////////////////////////////////////////////////
  1182. //
  1183. // Get_2D_Sample
  1184. //
  1185. ////////////////////////////////////////////////////////////////////////////////////////////
  1186. HSAMPLE
  1187. WWAudioClass::Get_2D_Sample (const AudibleSoundClass &sound_obj)
  1188. {
  1189. if (Is_OK_To_Give_Handle (sound_obj) == false) {
  1190. return (HSAMPLE)INVALID_MILES_HANDLE;
  1191. }
  1192. MMSLockClass lock;
  1193. float lowest_priority = sound_obj.Get_Priority ();
  1194. float lowest_runtime_priority = sound_obj.Get_Runtime_Priority ();
  1195. AudibleSoundClass *lowest_pri_sound = NULL;
  1196. HSAMPLE lowest_pri_sample = NULL;
  1197. HSAMPLE free_sample = (HSAMPLE)INVALID_MILES_HANDLE;
  1198. // Loop through all the available sample handles and try to find
  1199. // one that isn't being used to play a sound.
  1200. bool found = false;
  1201. for (int index = 0; (index < m_2DSampleHandles.Count ()) && !found; index ++) {
  1202. HSAMPLE sample = m_2DSampleHandles[index];
  1203. if (sample != NULL) {
  1204. // Get a pointer to the object that is currently using this sample
  1205. AudibleSoundClass *sound_obj = (AudibleSoundClass *)::AIL_sample_user_data (sample, INFO_OBJECT_PTR);
  1206. if (sound_obj == NULL) {
  1207. // Return this sample handle to the caller
  1208. free_sample = sample;
  1209. found = true;
  1210. } else {
  1211. //
  1212. // Determine if this sound's priority is lesser then the sound we want to play.
  1213. // This is done by comparing both the designer-specified priority and the current
  1214. // runtime priority (which is calculated by distance to the listener).
  1215. //
  1216. float priority = sound_obj->Get_Priority ();
  1217. float runtime_priority = sound_obj->Get_Runtime_Priority ();
  1218. if ( (priority < lowest_priority) ||
  1219. (priority == lowest_priority && runtime_priority <= lowest_runtime_priority))
  1220. {
  1221. lowest_priority = priority;
  1222. lowest_pri_sound = sound_obj;
  1223. lowest_pri_sample = sample;
  1224. lowest_runtime_priority = runtime_priority;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. // Steal the sample handle from the lower priority
  1230. // sound and return the handle to the caller.
  1231. if ((found == false) && (lowest_pri_sound != NULL)) {
  1232. lowest_pri_sound->Free_Miles_Handle ();
  1233. free_sample = lowest_pri_sample;
  1234. }
  1235. // Return the free sample handle if we found one
  1236. return free_sample;
  1237. }
  1238. ////////////////////////////////////////////////////////////////////////////////////////////
  1239. //
  1240. // Get_3D_Sample
  1241. //
  1242. ////////////////////////////////////////////////////////////////////////////////////////////////
  1243. H3DSAMPLE
  1244. WWAudioClass::Get_3D_Sample (const Sound3DClass &sound_obj)
  1245. {
  1246. if (Is_OK_To_Give_Handle (sound_obj) == false) {
  1247. return (H3DSAMPLE)INVALID_MILES_HANDLE;
  1248. }
  1249. MMSLockClass lock;
  1250. float lowest_priority = sound_obj.Get_Priority ();
  1251. float lowest_runtime_priority = sound_obj.Get_Runtime_Priority ();
  1252. AudibleSoundClass *lowest_pri_sound = NULL;
  1253. H3DSAMPLE lowest_pri_sample = NULL;
  1254. H3DSAMPLE free_sample = (H3DSAMPLE)INVALID_MILES_HANDLE;
  1255. // Loop through all the available sample handles and try to find
  1256. // one that isn't being used to play a sound.
  1257. bool found = false;
  1258. for (int index = 0; (index < m_3DSampleHandles.Count ()) && !found; index ++) {
  1259. H3DSAMPLE sample = m_3DSampleHandles[index];
  1260. if (sample != NULL) {
  1261. // Get a pointer to the object that is currently using this sample
  1262. AudibleSoundClass *sound_obj = (AudibleSoundClass *)::AIL_3D_object_user_data (sample, INFO_OBJECT_PTR);
  1263. if (sound_obj == NULL) {
  1264. // Return this sample handle to the caller
  1265. free_sample = sample;
  1266. found = true;
  1267. } else {
  1268. //
  1269. // Determine if this sound's priority is lesser then the sound we want to play.
  1270. // This is done by comparing both the designer-specified priority and the current
  1271. // runtime priority (which is calculated by distance to the listener).
  1272. //
  1273. float priority = sound_obj->Get_Priority ();
  1274. float runtime_priority = sound_obj->Get_Runtime_Priority ();
  1275. if ( (priority < lowest_priority) ||
  1276. (priority == lowest_priority && runtime_priority <= lowest_runtime_priority))
  1277. {
  1278. lowest_priority = priority;
  1279. lowest_pri_sound = sound_obj;
  1280. lowest_pri_sample = sample;
  1281. lowest_runtime_priority = runtime_priority;
  1282. }
  1283. }
  1284. }
  1285. }
  1286. // Steal the sample handle from the lower priority
  1287. // sound and return the handle to the caller.
  1288. if ((found == false) && (lowest_pri_sound != NULL)) {
  1289. lowest_pri_sound->Free_Miles_Handle ();
  1290. free_sample = lowest_pri_sample;
  1291. }
  1292. // Return the free sample handle if we found one
  1293. return free_sample;
  1294. }
  1295. ////////////////////////////////////////////////////////////////////////////////////////////
  1296. //
  1297. // Get_Listener_Handle
  1298. //
  1299. ////////////////////////////////////////////////////////////////////////////////////////////////
  1300. H3DPOBJECT
  1301. WWAudioClass::Get_Listener_Handle (void)
  1302. {
  1303. MMSLockClass lock;
  1304. return ::AIL_3D_open_listener (m_Driver3D);
  1305. }
  1306. ////////////////////////////////////////////////////////////////////////////////////////////
  1307. //
  1308. // Build_3D_Driver_List
  1309. //
  1310. ////////////////////////////////////////////////////////////////////////////////////////////////
  1311. void
  1312. WWAudioClass::Build_3D_Driver_List (void)
  1313. {
  1314. MMSLockClass lock;
  1315. HPROENUM next = HPROENUM_FIRST;
  1316. HPROVIDER provider = NULL;
  1317. char *name = NULL;
  1318. while (::AIL_enumerate_3D_providers (&next, &provider, &name) > 0) {
  1319. // Can we successfully open this provider?
  1320. if (::AIL_open_3D_provider (provider) == M3D_NOERR) {
  1321. DRIVER_INFO_STRUCT *info = W3DNEW DRIVER_INFO_STRUCT;
  1322. info->driver = provider;
  1323. info->name = ::strdup (name);
  1324. m_Driver3DList.Add (info);
  1325. ::AIL_close_3D_provider (provider);
  1326. } else {
  1327. char *error_info = ::AIL_last_error ();
  1328. WWDEBUG_SAY (("WWAudio: Unable to open %s.\r\n", name));
  1329. WWDEBUG_SAY (("WWAudio: Reason %s.\r\n", error_info));
  1330. }
  1331. }
  1332. // Attempt to select one of the known drivers (in the following order).
  1333. if ((Select_3D_Device (DRIVER3D_A3D) == false) &&
  1334. (Select_3D_Device (DRIVER3D_EAX) == false) &&
  1335. (Select_3D_Device (DRIVER3D_D3DSOUND) == false) &&
  1336. (Select_3D_Device (DRIVER3D_DOLBY) == false)) {
  1337. // Couldn't select a known driver, so just use the first possible.
  1338. if (m_Driver3DList.Count () > 0) {
  1339. Select_3D_Device ((int)0);
  1340. //DRIVER_INFO_STRUCT *info = m_Driver3DList[0];
  1341. //m_Driver3D = info->driver;
  1342. }
  1343. }
  1344. return;
  1345. }
  1346. ////////////////////////////////////////////////////////////////////////////////////////////
  1347. //
  1348. // Free_3D_Driver_List
  1349. //
  1350. ////////////////////////////////////////////////////////////////////////////////////////////////
  1351. void
  1352. WWAudioClass::Free_3D_Driver_List (void)
  1353. {
  1354. MMSLockClass lock;
  1355. //
  1356. // Remove all the handles
  1357. //
  1358. Remove_3D_Sound_Handles ();
  1359. Release_3D_Handles ();
  1360. //
  1361. // Loop through all the driver entries and free them all
  1362. //
  1363. for (int index = 0; index < m_Driver3DList.Count (); index ++) {
  1364. DRIVER_INFO_STRUCT *info = m_Driver3DList[index];
  1365. if (info != NULL) {
  1366. //
  1367. // Free the information we have stored with this driver
  1368. //
  1369. if (info->name != NULL) {
  1370. ::free (info->name);
  1371. }
  1372. delete info;
  1373. }
  1374. }
  1375. if (m_Driver3D != NULL) {
  1376. ::AIL_close_3D_provider (m_Driver3D);
  1377. m_Driver3D = NULL;
  1378. }
  1379. //
  1380. // Clear the list
  1381. //
  1382. m_Driver3DList.Delete_All ();
  1383. return;
  1384. }
  1385. ////////////////////////////////////////////////////////////////////////////////////////////
  1386. //
  1387. // Select_3D_Device
  1388. //
  1389. ////////////////////////////////////////////////////////////////////////////////////////////////
  1390. bool
  1391. WWAudioClass::Select_3D_Device (const char *device_name)
  1392. {
  1393. bool retval = false;
  1394. //
  1395. // Loop through all the drivers until we've found the one we want
  1396. //
  1397. for (int index = 0; index < m_Driver3DList.Count (); index ++) {
  1398. DRIVER_INFO_STRUCT *info = m_Driver3DList[index];
  1399. if (info != NULL) {
  1400. //
  1401. // Is this the device we were looking for?
  1402. //
  1403. if (::lstrcmpi (info->name, device_name) == 0) {
  1404. retval = Select_3D_Device (device_name, info->driver);
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. return retval;
  1410. }
  1411. ////////////////////////////////////////////////////////////////////////////////////////////
  1412. //
  1413. // Select_3D_Device
  1414. //
  1415. ////////////////////////////////////////////////////////////////////////////////////////////////
  1416. bool
  1417. WWAudioClass::Select_3D_Device (const char *device_name, HPROVIDER provider)
  1418. {
  1419. bool retval = false;
  1420. if ((provider != NULL) && (provider != m_Driver3D)) {
  1421. //
  1422. // Remove all the handles
  1423. //
  1424. Remove_3D_Sound_Handles ();
  1425. Release_3D_Handles ();
  1426. //
  1427. // Close the previous driver if needs be
  1428. //
  1429. if (m_Driver3D != NULL) {
  1430. ::AIL_close_3D_provider (m_Driver3D);
  1431. m_Driver3D = NULL;
  1432. }
  1433. //
  1434. // Select this device and re-allocate all handles
  1435. //
  1436. if (::AIL_open_3D_provider (provider) == M3D_NOERR) {
  1437. m_Driver3D = provider;
  1438. m_SoundScene->Initialize ();
  1439. Allocate_3D_Handles ();
  1440. AIL_set_3D_speaker_type (provider, AIL_3D_2_SPEAKER);
  1441. //
  1442. // Adjust the effects level to 1.0 if this is an EAX based driver
  1443. //
  1444. StringClass lower_name = device_name;
  1445. ::strlwr (lower_name.Peek_Buffer ());
  1446. if (::strstr (device_name, "eax") != 0) {
  1447. m_EffectsLevel = 1.0F;
  1448. } else {
  1449. m_EffectsLevel = 0.0F;
  1450. }
  1451. }
  1452. retval = true;
  1453. }
  1454. // Return true if we successfully selected the device
  1455. return retval;
  1456. }
  1457. ////////////////////////////////////////////////////////////////////////////////////////////
  1458. //
  1459. // Select_3D_Device
  1460. //
  1461. ////////////////////////////////////////////////////////////////////////////////////////////////
  1462. bool
  1463. WWAudioClass::Select_3D_Device (int index)
  1464. {
  1465. bool retval = false;
  1466. //
  1467. // Index valid?
  1468. //
  1469. if ((index >= 0) && (index < m_Driver3DList.Count ())) {
  1470. Select_3D_Device (m_Driver3DList[index]->name, m_Driver3DList[index]->driver);
  1471. WWDEBUG_SAY (("WWAudio: Selecting 3D sound device: %s.\r\n", m_Driver3DList[index]->name));
  1472. retval = true;
  1473. }
  1474. //
  1475. // Return true if we successfully selected the device
  1476. //
  1477. return retval;
  1478. }
  1479. ////////////////////////////////////////////////////////////////////////////////////////////
  1480. //
  1481. // Select_3D_Device
  1482. //
  1483. ////////////////////////////////////////////////////////////////////////////////////////////////
  1484. bool
  1485. WWAudioClass::Select_3D_Device (DRIVER_TYPE_3D type)
  1486. {
  1487. // Return true if we successfully selected the device
  1488. return Select_3D_Device (Find_3D_Device (type));
  1489. }
  1490. ////////////////////////////////////////////////////////////////////////////////////////////
  1491. //
  1492. // Find_3D_Device
  1493. //
  1494. ////////////////////////////////////////////////////////////////////////////////////////////////
  1495. int
  1496. WWAudioClass::Find_3D_Device (DRIVER_TYPE_3D type)
  1497. {
  1498. // Determine which substring to search for in the
  1499. // name of the driver.
  1500. const char *sub_string = "RSX";
  1501. switch (type) {
  1502. case DRIVER3D_D3DSOUND:
  1503. sub_string = "DirectSound";
  1504. break;
  1505. case DRIVER3D_EAX:
  1506. sub_string = "EAX";
  1507. break;
  1508. case DRIVER3D_A3D:
  1509. sub_string = "A3D";
  1510. break;
  1511. case DRIVER3D_PSEUDO:
  1512. sub_string = "Fast";
  1513. break;
  1514. case DRIVER3D_DOLBY:
  1515. sub_string = "Dolby";
  1516. break;
  1517. }
  1518. // Loop through all the driver entries and free them all
  1519. int driver_index = -1;
  1520. for (int index = 0; (index < m_Driver3DList.Count ()) && (driver_index == -1); index ++) {
  1521. DRIVER_INFO_STRUCT *info = m_Driver3DList[index];
  1522. if ((info != NULL) && (info->name != NULL)) {
  1523. // Is this the driver we were looking for?
  1524. if (::strstr (info->name, sub_string) != NULL) {
  1525. driver_index = index;
  1526. }
  1527. }
  1528. }
  1529. // Return -1 if not found, otherwise the 0 based index
  1530. return driver_index;
  1531. }
  1532. ////////////////////////////////////////////////////////////////////////////////////////////
  1533. //
  1534. // Allocate_3D_Handles
  1535. //
  1536. ////////////////////////////////////////////////////////////////////////////////////////////////
  1537. void
  1538. WWAudioClass::Allocate_3D_Handles (void)
  1539. {
  1540. MMSLockClass lock;
  1541. // Start fresh
  1542. Release_3D_Handles ();
  1543. if (m_Driver3D != NULL) {
  1544. // Attempt to allocate our share of 3D sample handles
  1545. for (int index = 0; index < m_Max3DSamples; index ++) {
  1546. H3DSAMPLE sample = ::AIL_allocate_3D_sample_handle (m_Driver3D);
  1547. if (sample != NULL) {
  1548. ::AIL_set_3D_object_user_data (sample, INFO_OBJECT_PTR, NULL);
  1549. m_3DSampleHandles.Add (sample);
  1550. }
  1551. }
  1552. }
  1553. return;
  1554. }
  1555. ////////////////////////////////////////////////////////////////////////////////////////////
  1556. //
  1557. // Release_3D_Handles
  1558. //
  1559. ////////////////////////////////////////////////////////////////////////////////////////////////
  1560. void
  1561. WWAudioClass::Release_3D_Handles (void)
  1562. {
  1563. MMSLockClass lock;
  1564. //
  1565. // Release our hold on all the samples we've allocated
  1566. //
  1567. for (int index = 0; index < m_3DSampleHandles.Count (); index ++) {
  1568. H3DSAMPLE sample = m_3DSampleHandles[index];
  1569. if (sample != NULL) {
  1570. ::AIL_release_3D_sample_handle (sample);
  1571. }
  1572. }
  1573. m_3DSampleHandles.Delete_All ();
  1574. return;
  1575. }
  1576. ////////////////////////////////////////////////////////////////////////////////////////////
  1577. //
  1578. // Validate_3D_Sound_Buffer
  1579. //
  1580. ////////////////////////////////////////////////////////////////////////////////////////////////
  1581. bool
  1582. WWAudioClass::Validate_3D_Sound_Buffer (SoundBufferClass *buffer)
  1583. {
  1584. bool retval = false;
  1585. //
  1586. // 3D sound buffer MUST be uncompressed mono WAV data
  1587. //
  1588. if ((buffer != NULL) &&
  1589. (buffer->Get_Channels () == 1) &&
  1590. (buffer->Get_Type () == WAVE_FORMAT_PCM) &&
  1591. (buffer->Is_Streaming () == false))
  1592. {
  1593. retval = true;
  1594. }
  1595. // Return a true/false result code
  1596. return retval;
  1597. }
  1598. ////////////////////////////////////////////////////////////////////////////////////////////
  1599. //
  1600. // ReAssign_2D_Handles
  1601. //
  1602. ////////////////////////////////////////////////////////////////////////////////////////////////
  1603. void
  1604. WWAudioClass::ReAssign_2D_Handles (void)
  1605. {
  1606. // Loop through all the entries in the playlist
  1607. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1608. AudibleSoundClass *sound_obj = m_Playlist[index];
  1609. // If this is a 2D sound effect, then force it to 'get' a new
  1610. // sound handle.
  1611. if ((sound_obj->Get_Class_ID () == CLASSID_2D) ||
  1612. (sound_obj->Get_Class_ID () == CLASSID_PSEUDO3D) ||
  1613. (sound_obj->Get_Class_ID () == CLASSID_2DTRIGGER))
  1614. {
  1615. sound_obj->Free_Miles_Handle ();
  1616. sound_obj->Allocate_Miles_Handle ();
  1617. }
  1618. }
  1619. return;
  1620. }
  1621. ////////////////////////////////////////////////////////////////////////////////////////////
  1622. //
  1623. // ReAssign_3D_Handles
  1624. //
  1625. ////////////////////////////////////////////////////////////////////////////////////////////////
  1626. void
  1627. WWAudioClass::ReAssign_3D_Handles (void)
  1628. {
  1629. // Loop through all the entries in the playlist
  1630. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1631. AudibleSoundClass *sound_obj = m_Playlist[index];
  1632. // If this is a 3D sound effect, then force it to 'get' a new
  1633. // sound handle.
  1634. if (sound_obj->Get_Class_ID () == CLASSID_3D) {
  1635. sound_obj->Free_Miles_Handle ();
  1636. sound_obj->Allocate_Miles_Handle ();
  1637. }
  1638. }
  1639. return;
  1640. }
  1641. ////////////////////////////////////////////////////////////////////////////////////////////
  1642. //
  1643. // Remove_2D_Sound_Handles
  1644. //
  1645. ////////////////////////////////////////////////////////////////////////////////////////////////
  1646. void
  1647. WWAudioClass::Remove_2D_Sound_Handles (void)
  1648. {
  1649. // Loop through all the entries in the playlist
  1650. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1651. AudibleSoundClass *sound_obj = m_Playlist[index];
  1652. // If this is a 2D sound effect, then take its handle away
  1653. if (sound_obj->Get_Class_ID () == CLASSID_2D) {
  1654. sound_obj->Free_Miles_Handle ();
  1655. }
  1656. }
  1657. return;
  1658. }
  1659. ////////////////////////////////////////////////////////////////////////////////////////////
  1660. //
  1661. // Remove_3D_Sound_Handles
  1662. //
  1663. ////////////////////////////////////////////////////////////////////////////////////////////////
  1664. void
  1665. WWAudioClass::Remove_3D_Sound_Handles (void)
  1666. {
  1667. // Loop through all the entries in the playlist
  1668. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1669. AudibleSoundClass *sound_obj = m_Playlist[index];
  1670. // If this is a 3D sound effect, then force take its handle away
  1671. if (sound_obj->Get_Class_ID () == CLASSID_3D) {
  1672. sound_obj->Free_Miles_Handle ();
  1673. }
  1674. }
  1675. return;
  1676. }
  1677. ////////////////////////////////////////////////////////////////////////////////////////////
  1678. //
  1679. // Set_Sound_Effects_Volume
  1680. //
  1681. ////////////////////////////////////////////////////////////////////////////////////////////////
  1682. void
  1683. WWAudioClass::Set_Sound_Effects_Volume (float volume)
  1684. {
  1685. m_SoundVolume = volume;
  1686. m_SoundVolume = min (1.0F, m_SoundVolume);
  1687. m_SoundVolume = max (0.0F, m_SoundVolume);
  1688. // Update all the currently playing 'Sound Effects' to
  1689. // reflect this new volume
  1690. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1691. AudibleSoundClass *sound_obj = m_Playlist[index];
  1692. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_SOUND_EFFECT) {
  1693. sound_obj->Update_Volume ();
  1694. }
  1695. }
  1696. return;
  1697. }
  1698. ////////////////////////////////////////////////////////////////////////////////////////////
  1699. //
  1700. // Set_Music_Volume
  1701. //
  1702. ////////////////////////////////////////////////////////////////////////////////////////////////
  1703. void
  1704. WWAudioClass::Set_Music_Volume (float volume)
  1705. {
  1706. m_MusicVolume = volume;
  1707. m_MusicVolume = min (1.0F, m_MusicVolume);
  1708. m_MusicVolume = max (0.0F, m_MusicVolume);
  1709. // Update all currently playing music to
  1710. // reflect this new volume
  1711. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1712. AudibleSoundClass *sound_obj = m_Playlist[index];
  1713. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_MUSIC) {
  1714. sound_obj->Update_Volume ();
  1715. }
  1716. }
  1717. return;
  1718. }
  1719. ////////////////////////////////////////////////////////////////////////////////////////////
  1720. //
  1721. // Is_Disabled
  1722. //
  1723. ////////////////////////////////////////////////////////////////////////////////////////////////
  1724. bool
  1725. WWAudioClass::Is_Disabled (void) const
  1726. {
  1727. static bool _firsttime = true;
  1728. static bool _disabled = false;
  1729. if (_firsttime) {
  1730. _firsttime = false;
  1731. #ifdef G_CODE_BASE
  1732. //
  1733. // Use command line arguement.
  1734. //
  1735. ArgvClass argv;
  1736. if (argv.Find("-NOAUDIO")) {
  1737. _disabled = true;
  1738. }
  1739. #endif
  1740. //
  1741. // Read the disabled key from the registry
  1742. //
  1743. RegistryClass registry ("SOFTWARE\\Westwood\\WWAudio");
  1744. if (registry.Is_Valid ()) {
  1745. if (registry.Get_Int ("Disabled", 0) == 1) {
  1746. _disabled = true;
  1747. WWDEBUG_SAY (("WWAudio: Audio system disabled in registry.\r\n"));
  1748. }
  1749. }
  1750. }
  1751. return _disabled;
  1752. }
  1753. ////////////////////////////////////////////////////////////////////////////////////////////
  1754. //
  1755. // Initialize
  1756. //
  1757. ////////////////////////////////////////////////////////////////////////////////////////////////
  1758. void
  1759. WWAudioClass::Initialize (const char *registry_subkey_name)
  1760. {
  1761. WWMEMLOG(MEM_SOUND);
  1762. if (Is_Disabled () == false) {
  1763. //
  1764. // Initialize the audio system from the registry settings
  1765. //
  1766. Load_From_Registry (registry_subkey_name);
  1767. //
  1768. // Grab the first (and only) filter for use with our 'tinny' effect.
  1769. //
  1770. HPROENUM next = HPROENUM_FIRST;
  1771. char *name = NULL;
  1772. if (::AIL_enumerate_filters (&next, &m_ReverbFilter, &name) == 0) {
  1773. m_ReverbFilter = INVALID_MILES_HANDLE;
  1774. }
  1775. }
  1776. //
  1777. // Register the file callbacks so we can support streaming from MIX files...
  1778. //
  1779. ::AIL_set_file_callbacks (File_Open_Callback, File_Close_Callback,
  1780. File_Seek_Callback, File_Read_Callback);
  1781. return ;
  1782. }
  1783. ////////////////////////////////////////////////////////////////////////////////////////////
  1784. //
  1785. // Initialize
  1786. //
  1787. ////////////////////////////////////////////////////////////////////////////////////////////////
  1788. void
  1789. WWAudioClass::Initialize
  1790. (
  1791. bool stereo,
  1792. int bits,
  1793. int hertz
  1794. )
  1795. {
  1796. // Open the default 2D device, then build a list of 3D
  1797. // devices and open the default.
  1798. if (Is_Disabled () == false) {
  1799. Open_2D_Device (stereo, bits, hertz);
  1800. Build_3D_Driver_List ();
  1801. //
  1802. // Grab the first (and only) filter for use with our 'tinny' effect.
  1803. //
  1804. HPROENUM next = HPROENUM_FIRST;
  1805. char *name = NULL;
  1806. if (::AIL_enumerate_filters (&next, &m_ReverbFilter, &name) == 0) {
  1807. m_ReverbFilter = INVALID_MILES_HANDLE;
  1808. }
  1809. }
  1810. return;
  1811. }
  1812. ////////////////////////////////////////////////////////////////////////////////////////////
  1813. //
  1814. // Shutdown
  1815. //
  1816. ////////////////////////////////////////////////////////////////////////////////////////////////
  1817. void
  1818. WWAudioClass::Shutdown (void)
  1819. {
  1820. //
  1821. // If there is a timer running, then stop the timer...
  1822. //
  1823. if (m_UpdateTimer != -1) {
  1824. // Kill the timer
  1825. ::AIL_stop_timer (m_UpdateTimer);
  1826. ::AIL_release_timer_handle (m_UpdateTimer);
  1827. m_UpdateTimer = -1;
  1828. // Wait for the timer callback function to end
  1829. ::WaitForSingleObject (_TimerSyncEvent, 20000);
  1830. ::CloseHandle (_TimerSyncEvent);
  1831. _TimerSyncEvent = NULL;
  1832. }
  1833. //
  1834. // Stop all sounds from playing
  1835. //
  1836. Flush_Playlist ();
  1837. if (m_SoundScene != NULL) {
  1838. m_SoundScene->Flush_Scene ();
  1839. }
  1840. //
  1841. // Free all our cached sound buffers
  1842. //
  1843. Flush_Cache ();
  1844. //
  1845. // Close-out our hold on any driver resources
  1846. //
  1847. Remove_2D_Sound_Handles ();
  1848. Remove_3D_Sound_Handles ();
  1849. Release_2D_Handles ();
  1850. Release_3D_Handles ();
  1851. Free_3D_Driver_List ();
  1852. SAFE_DELETE (m_SoundScene);
  1853. Close_2D_Device ();
  1854. //
  1855. // Shutdown Miles Sound System
  1856. //
  1857. ::AIL_shutdown ();
  1858. return;
  1859. }
  1860. ////////////////////////////////////////////////////////////////////////////////////////////
  1861. //
  1862. // Register_EOS_Callback
  1863. //
  1864. ////////////////////////////////////////////////////////////////////////////////////////////////
  1865. void
  1866. WWAudioClass::Register_EOS_Callback (LPFNEOSCALLBACK callback, DWORD user_param)
  1867. {
  1868. m_EOSCallbackList.Add_Callback (callback, user_param);
  1869. return;
  1870. }
  1871. ////////////////////////////////////////////////////////////////////////////////////////////
  1872. //
  1873. // UnRegister_EOS_Callback
  1874. //
  1875. ////////////////////////////////////////////////////////////////////////////////////////////////
  1876. void
  1877. WWAudioClass::UnRegister_EOS_Callback (LPFNEOSCALLBACK callback)
  1878. {
  1879. m_EOSCallbackList.Remove_Callback (callback);
  1880. return;
  1881. }
  1882. ////////////////////////////////////////////////////////////////////////////////////////////
  1883. //
  1884. // Register_Text_Callback
  1885. //
  1886. ////////////////////////////////////////////////////////////////////////////////////////////////
  1887. void
  1888. WWAudioClass::Register_Text_Callback (LPFNTEXTCALLBACK callback, DWORD user_param)
  1889. {
  1890. m_TextCallbackList.Add_Callback (callback, user_param);
  1891. return;
  1892. }
  1893. ////////////////////////////////////////////////////////////////////////////////////////////
  1894. //
  1895. // UnRegister_Text_Callback
  1896. //
  1897. ////////////////////////////////////////////////////////////////////////////////////////////////
  1898. void
  1899. WWAudioClass::UnRegister_Text_Callback (LPFNTEXTCALLBACK callback)
  1900. {
  1901. m_TextCallbackList.Remove_Callback (callback);
  1902. return;
  1903. }
  1904. ////////////////////////////////////////////////////////////////////////////////////////////
  1905. //
  1906. // Fire_Text_Callback
  1907. //
  1908. ////////////////////////////////////////////////////////////////////////////////////////////////
  1909. void
  1910. WWAudioClass::Fire_Text_Callback (AudibleSoundClass *sound_obj, const StringClass &text)
  1911. {
  1912. if (text.Get_Length () > 0) {
  1913. //
  1914. // Loop over all the text-callbacks that have been registered
  1915. //
  1916. for (int index = 0; index < m_TextCallbackList.Count (); index ++) {
  1917. uint32 user_data = 0L;
  1918. LPFNTEXTCALLBACK callback = m_TextCallbackList.Get_Callback (index, &user_data);
  1919. if (callback != NULL) {
  1920. //
  1921. // Fire the notification
  1922. //
  1923. (*callback) (sound_obj, text, user_data);
  1924. }
  1925. }
  1926. }
  1927. return ;
  1928. }
  1929. ////////////////////////////////////////////////////////////////////////////////////////////
  1930. //
  1931. // Allow_Sound_Effects
  1932. //
  1933. ////////////////////////////////////////////////////////////////////////////////////////////////
  1934. void
  1935. WWAudioClass::Allow_Sound_Effects (bool onoff)
  1936. {
  1937. // Is the state changing?
  1938. if (m_AreSoundEffectsEnabled != onoff) {
  1939. m_AreSoundEffectsEnabled = onoff;
  1940. // Update all the currently playing 'Sound Effects' to
  1941. // reflect this new state.
  1942. if (m_AreSoundEffectsEnabled) {
  1943. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1944. AudibleSoundClass *sound_obj = m_Playlist[index];
  1945. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_SOUND_EFFECT) {
  1946. sound_obj->Allocate_Miles_Handle ();
  1947. }
  1948. }
  1949. } else {
  1950. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1951. AudibleSoundClass *sound_obj = m_Playlist[index];
  1952. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_SOUND_EFFECT) {
  1953. sound_obj->Free_Miles_Handle ();
  1954. }
  1955. }
  1956. }
  1957. }
  1958. return;
  1959. }
  1960. ////////////////////////////////////////////////////////////////////////////////////////////
  1961. //
  1962. // Allow_Music
  1963. //
  1964. ////////////////////////////////////////////////////////////////////////////////////////////////
  1965. void
  1966. WWAudioClass::Allow_Music (bool onoff)
  1967. {
  1968. // Is the state changing?
  1969. if (m_IsMusicEnabled != onoff) {
  1970. m_IsMusicEnabled = onoff;
  1971. // Update all the currently playing 'music tracks' to
  1972. // reflect this new state.
  1973. if (m_IsMusicEnabled) {
  1974. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1975. AudibleSoundClass *sound_obj = m_Playlist[index];
  1976. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_MUSIC) {
  1977. sound_obj->Stop (false);
  1978. sound_obj->Play ();
  1979. }
  1980. }
  1981. } else {
  1982. for (int index = 0; index < m_Playlist.Count (); index ++) {
  1983. AudibleSoundClass *sound_obj = m_Playlist[index];
  1984. if (sound_obj->Get_Type () == AudibleSoundClass::TYPE_MUSIC) {
  1985. sound_obj->Free_Miles_Handle ();
  1986. }
  1987. }
  1988. }
  1989. }
  1990. return;
  1991. }
  1992. ////////////////////////////////////////////////////////////////////////////////////////////
  1993. //
  1994. // Simple_Play_2D_Sound_Effect
  1995. //
  1996. ////////////////////////////////////////////////////////////////////////////////////////////////
  1997. bool
  1998. WWAudioClass::Simple_Play_2D_Sound_Effect
  1999. (
  2000. const char *filename,
  2001. float priority,
  2002. float volume
  2003. )
  2004. {
  2005. bool retval = false;
  2006. AudibleSoundClass *sound = Create_Sound_Effect (filename);
  2007. if (sound != NULL) {
  2008. sound->Set_Priority (priority);
  2009. sound->Set_Loop_Count (1);
  2010. sound->Set_Volume(volume);
  2011. sound->Play ();
  2012. sound->Release_Ref ();
  2013. sound = NULL;
  2014. retval = true;
  2015. }
  2016. return retval;
  2017. }
  2018. ////////////////////////////////////////////////////////////////////////////////////////////
  2019. //
  2020. // Simple_Play_2D_Sound_Effect
  2021. //
  2022. ////////////////////////////////////////////////////////////////////////////////////////////////
  2023. bool
  2024. WWAudioClass::Simple_Play_2D_Sound_Effect
  2025. (
  2026. FileClass &file,
  2027. float priority,
  2028. float volume
  2029. )
  2030. {
  2031. bool retval = false;
  2032. AudibleSoundClass *sound = Create_Sound_Effect (file);
  2033. if (sound != NULL) {
  2034. sound->Set_Priority (priority);
  2035. sound->Set_Loop_Count (1);
  2036. sound->Set_Volume(volume);
  2037. sound->Play ();
  2038. sound->Release_Ref ();
  2039. sound = NULL;
  2040. retval = true;
  2041. }
  2042. return retval;
  2043. }
  2044. ////////////////////////////////////////////////////////////////////////////////////////////
  2045. //
  2046. // Get_File
  2047. //
  2048. ////////////////////////////////////////////////////////////////////////////////////////////////
  2049. FileClass *
  2050. WWAudioClass::Get_File (LPCTSTR filename)
  2051. {
  2052. FileClass *file = NULL;
  2053. if (m_FileFactory != NULL) {
  2054. file = m_FileFactory->Get_File (filename);
  2055. } else {
  2056. file = _TheFileFactory->Get_File(filename);
  2057. }
  2058. // Return a pointer to the file
  2059. return file;
  2060. }
  2061. ////////////////////////////////////////////////////////////////////////////////////////////
  2062. //
  2063. // Return_File
  2064. //
  2065. ////////////////////////////////////////////////////////////////////////////////////////////////
  2066. void
  2067. WWAudioClass::Return_File (FileClass *file)
  2068. {
  2069. if (m_FileFactory != NULL) {
  2070. m_FileFactory->Return_File (file);
  2071. } else {
  2072. SAFE_DELETE (file);
  2073. }
  2074. return;
  2075. }
  2076. ////////////////////////////////////////////////////////////////////////////////////////////
  2077. //
  2078. // Create_Logical_Sound
  2079. //
  2080. ////////////////////////////////////////////////////////////////////////////////////////////
  2081. LogicalSoundClass *
  2082. WWAudioClass::Create_Logical_Sound (void)
  2083. {
  2084. return W3DNEW LogicalSoundClass;
  2085. }
  2086. ////////////////////////////////////////////////////////////////////////////////////////////
  2087. //
  2088. // Create_Logical_Listener
  2089. //
  2090. ////////////////////////////////////////////////////////////////////////////////////////////
  2091. LogicalListenerClass *
  2092. WWAudioClass::Create_Logical_Listener (void)
  2093. {
  2094. return W3DNEW LogicalListenerClass;
  2095. }
  2096. ////////////////////////////////////////////////////////////////////////////////////////////
  2097. //
  2098. // Add_Logical_Type
  2099. //
  2100. ////////////////////////////////////////////////////////////////////////////////////////////
  2101. void
  2102. WWAudioClass::Add_Logical_Type (int id, LPCTSTR display_name)
  2103. {
  2104. m_LogicalTypes.Add (LOGICAL_TYPE_STRUCT (id, display_name));
  2105. return ;
  2106. }
  2107. ////////////////////////////////////////////////////////////////////////////////////////////
  2108. //
  2109. // Reset_Logical_Types
  2110. //
  2111. ////////////////////////////////////////////////////////////////////////////////////////////
  2112. void
  2113. WWAudioClass::Reset_Logical_Types (void)
  2114. {
  2115. m_LogicalTypes.Delete_All ();
  2116. return ;
  2117. }
  2118. ////////////////////////////////////////////////////////////////////////////////////////////
  2119. //
  2120. // Get_Logical_Type
  2121. //
  2122. ////////////////////////////////////////////////////////////////////////////////////////////
  2123. int
  2124. WWAudioClass::Get_Logical_Type (int index, StringClass &name)
  2125. {
  2126. int type_id = 0;
  2127. WWASSERT (index >= 0 && index < m_LogicalTypes.Count ());
  2128. if (index >= 0 && index < m_LogicalTypes.Count ()) {
  2129. type_id = m_LogicalTypes[index].id;
  2130. name = m_LogicalTypes[index].display_name;
  2131. }
  2132. return type_id;
  2133. }
  2134. ////////////////////////////////////////////////////////////////////////////////////////////
  2135. //
  2136. // Find_Sound_Object
  2137. //
  2138. ////////////////////////////////////////////////////////////////////////////////////////////
  2139. SoundSceneObjClass *
  2140. WWAudioClass::Find_Sound_Object (uint32 sound_obj_id)
  2141. {
  2142. SoundSceneObjClass *sound_obj = NULL;
  2143. //
  2144. // Lookup the sound object and return it to the caller
  2145. //
  2146. int index = 0;
  2147. if (SoundSceneObjClass::Find_Sound_Object (sound_obj_id, &index)) {
  2148. sound_obj = SoundSceneObjClass::m_GlobalSoundList[index];
  2149. }
  2150. return sound_obj;
  2151. }
  2152. ////////////////////////////////////////////////////////////////////////////////////////////
  2153. //
  2154. // Load_From_Registry
  2155. //
  2156. ////////////////////////////////////////////////////////////////////////////////////////////
  2157. bool
  2158. WWAudioClass::Load_From_Registry (const char *subkey_name)
  2159. {
  2160. bool retval = true;
  2161. StringClass device_name;
  2162. bool is_stereo = true;
  2163. int bits = 16;
  2164. int hertz = 44100;
  2165. //
  2166. // Load the settings from the registry
  2167. //
  2168. if (Load_From_Registry (subkey_name, device_name, is_stereo, bits, hertz,
  2169. m_AreSoundEffectsEnabled, m_IsMusicEnabled, m_SoundVolume, m_MusicVolume))
  2170. {
  2171. //
  2172. // Close any open devices
  2173. //
  2174. Free_3D_Driver_List ();
  2175. Close_2D_Device ();
  2176. //
  2177. // Open the 2D device as specified
  2178. //
  2179. Open_2D_Device (is_stereo, bits, hertz);
  2180. //
  2181. // Find and open the 3D device specified
  2182. //
  2183. Build_3D_Driver_List ();
  2184. Select_3D_Device (device_name);
  2185. retval = true;
  2186. }
  2187. return retval;
  2188. }
  2189. ////////////////////////////////////////////////////////////////////////////////////////////
  2190. //
  2191. // Load_From_Registry
  2192. //
  2193. ////////////////////////////////////////////////////////////////////////////////////////////
  2194. bool
  2195. WWAudioClass::Load_From_Registry
  2196. (
  2197. const char * subkey_name,
  2198. StringClass & device_name,
  2199. bool & is_stereo,
  2200. int & bits,
  2201. int & hertz,
  2202. bool & sound_enabled,
  2203. bool & music_enabled,
  2204. float & sound_volume,
  2205. float & music_volume
  2206. )
  2207. {
  2208. bool retval = false;
  2209. //
  2210. // Attempt to open the registry key
  2211. //
  2212. RegistryClass registry (subkey_name);
  2213. if (registry.Is_Valid ()) {
  2214. //
  2215. // Read the device name into a string object
  2216. //
  2217. char temp_buffer[256] = { 0 };
  2218. registry.Get_String (VALUE_NAME_DEVICE_NAME, temp_buffer, sizeof (temp_buffer));
  2219. device_name = temp_buffer;
  2220. //
  2221. // Read the 2D settings
  2222. //
  2223. is_stereo = (registry.Get_Int (VALUE_NAME_IS_STEREO, true) == 1);
  2224. bits = registry.Get_Int (VALUE_NAME_BITS, 16);
  2225. hertz = registry.Get_Int (VALUE_NAME_HERTZ, 44100);
  2226. //
  2227. // Read the sound/music enabled settings
  2228. //
  2229. music_enabled = (registry.Get_Int (VALUE_NAME_MUSIC_ENABLED, 1) == 1);
  2230. sound_enabled = (registry.Get_Int (VALUE_NAME_SOUND_ENABLED, 1) == 1);
  2231. //
  2232. // Read the volume information
  2233. //
  2234. music_volume = registry.Get_Int (VALUE_NAME_MUSIC_VOL, 100) / 100.0F;
  2235. sound_volume = registry.Get_Int (VALUE_NAME_SOUND_VOL, 100) / 100.0F;
  2236. music_volume = WWMath::Clamp (music_volume, 0, 1.0F);
  2237. sound_volume = WWMath::Clamp (sound_volume, 0, 1.0F);
  2238. retval = true;
  2239. }
  2240. return retval;
  2241. }
  2242. ////////////////////////////////////////////////////////////////////////////////////////////
  2243. //
  2244. // Save_To_Registry
  2245. //
  2246. ////////////////////////////////////////////////////////////////////////////////////////////
  2247. bool
  2248. WWAudioClass::Save_To_Registry (const char *subkey_name)
  2249. {
  2250. StringClass device_name;
  2251. //
  2252. // Get the name of the current 3D driver
  2253. //
  2254. for (int index = 0; index < m_Driver3DList.Count (); index ++) {
  2255. DRIVER_INFO_STRUCT *info = m_Driver3DList[index];
  2256. //
  2257. // Is this the device we were looking for?
  2258. //
  2259. if (info != NULL && info->driver == m_Driver3D) {
  2260. device_name = info->name;
  2261. break;
  2262. }
  2263. }
  2264. //
  2265. // Save these settings to the registry
  2266. //
  2267. return Save_To_Registry (subkey_name, device_name, m_PlaybackStereo, m_PlaybackBits, m_PlaybackRate,
  2268. m_AreSoundEffectsEnabled, m_IsMusicEnabled, m_SoundVolume, m_MusicVolume);
  2269. }
  2270. ////////////////////////////////////////////////////////////////////////////////////////////
  2271. //
  2272. // Save_To_Registry
  2273. //
  2274. ////////////////////////////////////////////////////////////////////////////////////////////
  2275. bool
  2276. WWAudioClass::Save_To_Registry
  2277. (
  2278. const char * subkey_name,
  2279. const StringClass & device_name,
  2280. bool is_stereo,
  2281. int bits,
  2282. int hertz,
  2283. bool sound_enabled,
  2284. bool music_enabled,
  2285. float sound_volume,
  2286. float music_volume
  2287. )
  2288. {
  2289. bool retval = false;
  2290. //
  2291. // Attempt to open the registry key
  2292. //
  2293. RegistryClass registry (subkey_name);
  2294. if (registry.Is_Valid ()) {
  2295. //
  2296. // Save the settings to the registry
  2297. //
  2298. registry.Set_String (VALUE_NAME_DEVICE_NAME, device_name);
  2299. registry.Set_Int (VALUE_NAME_IS_STEREO, is_stereo);
  2300. registry.Set_Int (VALUE_NAME_BITS, bits);
  2301. registry.Set_Int (VALUE_NAME_HERTZ, hertz);
  2302. registry.Set_Int (VALUE_NAME_MUSIC_ENABLED, music_enabled);
  2303. registry.Set_Int (VALUE_NAME_SOUND_ENABLED, sound_enabled);
  2304. registry.Set_Int (VALUE_NAME_MUSIC_VOL, music_volume * 100);
  2305. registry.Set_Int (VALUE_NAME_SOUND_VOL, sound_volume * 100);
  2306. retval = true;
  2307. }
  2308. return retval;
  2309. }
  2310. ////////////////////////////////////////////////////////////////////////////////////////////
  2311. //
  2312. // File_Open_Callback
  2313. //
  2314. ////////////////////////////////////////////////////////////////////////////////////////////
  2315. U32 AILCALLBACK
  2316. WWAudioClass::File_Open_Callback (char const *filename, U32 *file_handle)
  2317. {
  2318. U32 retval = false;
  2319. if (Get_Instance () != NULL) {
  2320. //
  2321. // Open the file
  2322. //
  2323. FileClass *file = Get_Instance ()->Get_File (filename);
  2324. if (file != NULL && file->Open ()) {
  2325. (*file_handle) = (U32)file;
  2326. retval = true;
  2327. }
  2328. }
  2329. return retval;
  2330. }
  2331. ////////////////////////////////////////////////////////////////////////////////////////////
  2332. //
  2333. // File_Close_Callback
  2334. //
  2335. ////////////////////////////////////////////////////////////////////////////////////////////
  2336. void AILCALLBACK
  2337. WWAudioClass::File_Close_Callback (U32 file_handle)
  2338. {
  2339. if (Get_Instance () != NULL) {
  2340. //
  2341. // Close the file (if necessary)
  2342. //
  2343. FileClass *file = reinterpret_cast<FileClass *> (file_handle);
  2344. if (file != NULL) {
  2345. Get_Instance ()->Return_File (file);
  2346. }
  2347. }
  2348. return ;
  2349. }
  2350. ////////////////////////////////////////////////////////////////////////////////////////////
  2351. //
  2352. // File_Seek_Callback
  2353. //
  2354. ////////////////////////////////////////////////////////////////////////////////////////////
  2355. S32 AILCALLBACK
  2356. WWAudioClass::File_Seek_Callback (U32 file_handle, S32 offset, U32 type)
  2357. {
  2358. S32 retval = 0;
  2359. //
  2360. // Convert the handle to a file handle type
  2361. //
  2362. FileClass *file = reinterpret_cast<FileClass *> (file_handle);
  2363. if (file != NULL) {
  2364. //
  2365. // Convert the Miles seek type to one of our own
  2366. //
  2367. int seek_type = SEEK_CUR;
  2368. switch (type)
  2369. {
  2370. case AIL_FILE_SEEK_BEGIN:
  2371. seek_type = SEEK_SET;
  2372. break;
  2373. case AIL_FILE_SEEK_CURRENT:
  2374. seek_type = SEEK_CUR;
  2375. break;
  2376. case AIL_FILE_SEEK_END:
  2377. seek_type = SEEK_END;
  2378. break;
  2379. }
  2380. //
  2381. // Perform the seek
  2382. //
  2383. retval = file->Seek (offset, seek_type);
  2384. }
  2385. return retval;
  2386. }
  2387. ////////////////////////////////////////////////////////////////////////////////////////////
  2388. //
  2389. // File_Read_Callback
  2390. //
  2391. ////////////////////////////////////////////////////////////////////////////////////////////
  2392. U32 AILCALLBACK
  2393. WWAudioClass::File_Read_Callback (U32 file_handle, void *buffer, U32 bytes)
  2394. {
  2395. U32 retval = 0;
  2396. //
  2397. // Convert the handle to a file handle type
  2398. //
  2399. FileClass *file = reinterpret_cast<FileClass *> (file_handle);
  2400. if (file != NULL) {
  2401. //
  2402. // Read the bytes from the file
  2403. //
  2404. retval = file->Read (buffer, bytes);
  2405. }
  2406. return retval;
  2407. }