SOUNDIO.CPP 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Id: soundio.cpp 1.41 1994/06/20 15:01:39 joe_bostic Exp $ */
  19. /***********************************************************************************************
  20. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Sound Library *
  24. * *
  25. * File Name : SOUND.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : July 22, 1991 *
  30. * *
  31. *---------------------------------------------------------------------------------------------*
  32. * Functions: *
  33. * Load_Long_Sample -- Loads a sample into XMS for double buffer system. *
  34. * Read_Long_Sample -- Loads/Processes/Formats/Builds offset. *
  35. * Save_Table_Entry -- Put an entry in the offset table. *
  36. * Play_Long_Sample -- Calls Init_Long_Sample and Start_Long_Sample. *
  37. * Start_Long_Sample -- Starts a sample playing that has be initialized. *
  38. * Get_Table_Entry -- Gets next entry in table. *
  39. * Long_Sample_Ticks -- Gets number of ticks in sample if in header. *
  40. * Long_Sample_Status -- Returns the status of the sample. *
  41. * Find_Table_Entry -- Finds next entry in table that matches mask. *
  42. * Get_Table_Start -- Returns a pointer to first entry in table. *
  43. * Long_Sample_Ticks_Played -- Number of ticks since sample started. *
  44. * Install_Sample_Driver_Callback -- Pokes callback function into JM driver *
  45. * Stop_Long_Sample -- Stops current long sample from playing. *
  46. * Long_Sample_Loaded_Size -- Max buffer size to load a long sample. *
  47. * Sound_Callback -- Audio driver callback function. *
  48. * DigiCallback -- Low level double buffering handler. *
  49. * Load_Sample_Into_Buffer -- Loads a digitized sample into a buffer. *
  50. * Stream_Sample -- Streams a sample directly from a file. *
  51. * Sample_Read -- Reads sample data from an openned file. *
  52. * Continue_Sample -- Tags another block of data onto the currently playing. *
  53. * Sample_Copy -- Copies sound data from source format to raw format. *
  54. * File_Stream_Preload -- Handles initial proload of a streaming samples bu*
  55. * Sample_Length -- returns length of a sample in ticks *
  56. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  57. extern void Colour_Debug (int call_number);
  58. #pragma pack(4)
  59. #define WIN32
  60. #ifndef _WIN32 // Denzil 6/2/98 Watcom 11.0 complains without this check
  61. #define _WIN32
  62. #endif // _WIN32
  63. #include <windows.h>
  64. #include <windowsx.h>
  65. #include "dsound.h"
  66. #include <mem.h>
  67. #include <wwmem.h>
  68. #include "soundint.h"
  69. #include <stdio.h>
  70. #include <string.h>
  71. #include <direct.h>
  72. #include <stdlib.h>
  73. #include <process.h>
  74. #include <keyboard.h>
  75. #include <file.h>
  76. #include <bios.h>
  77. #include <timer.h>
  78. #pragma pack(1)
  79. #include "audio.h"
  80. #pragma pack(4)
  81. LPDIRECTSOUNDBUFFER DumpBuffer;
  82. HANDLE SoundThreadHandle = NULL;
  83. BOOL SoundThreadActive = FALSE;
  84. /*
  85. ** If this is defined, then the streaming audio buffer will be filled
  86. ** to maximum whenever filling is to occur. If undefined, it will fill
  87. ** the streaming buffer in smaller chunks.
  88. */
  89. #define SIMPLE_FILLING
  90. /*
  91. ** This is the number of times per sec that the maintenance callback gets called.
  92. */
  93. #define MAINTENANCE_RATE 40 //30 times per sec plus a safety margin
  94. /*
  95. ** Size of the temporary buffer in XMS/EMS that direct file
  96. ** streaming of sounds will allocate.
  97. */
  98. //#define STREAM_BUFFER_SIZE (128L*1024L)
  99. #define STREAM_BUFFER_SIZE (128L*1024L)
  100. /*
  101. ** Define the number of "StreamBufferSize" blocks that are read in
  102. ** at a minimum when the streaming sample load callback routine
  103. ** is called. We will IGNORE loads that are less that this in order
  104. ** to avoid constant seeking on the CD.
  105. */
  106. #define STREAM_CUSHION_BLOCKS 4
  107. /*
  108. ** This is the maximum size that a sonarc block can be. All sonarc blocks
  109. ** must be either a multiple of this value or a binary root of this value.
  110. */
  111. #define LARGEST_SONARC_BLOCK 2048
  112. //////////////////////////////////////////////////////////////////////////////////////
  113. ////////////////////////////////////// structs ///////////////////////////////////////
  114. //void *DigiBuffer = NULL;
  115. static BOOL StartingFileStream = FALSE;
  116. short StreamLowImpact = FALSE;
  117. MemoryFlagType StreamBufferFlag = MEM_NORMAL;
  118. int Misc;
  119. SFX_Type SoundType;
  120. Sample_Type SampleType;
  121. int ReverseChannels = FALSE;
  122. LPDIRECTSOUND SoundObject; //Direct sound object
  123. LPDIRECTSOUNDBUFFER PrimaryBufferPtr; //Pointer to the buffer that the
  124. unsigned SoundTimerHandle=0; //Windows Handle for sound timer
  125. WAVEFORMATEX DsBuffFormat; //format of direct sound buffer
  126. DSBUFFERDESC BufferDesc; //Buffer description for creating buffers
  127. WAVEFORMATEX PrimaryBuffFormat; //Copy of format of direct sound primary buffer
  128. DSBUFFERDESC PrimaryBufferDesc; //Copy of buffer description for re-creating primary buffer
  129. CRITICAL_SECTION GlobalAudioCriticalSection;
  130. /*
  131. ** Function to call if we detect focus loss
  132. */
  133. extern void (*Audio_Focus_Loss_Function)(void) = NULL;
  134. /*=========================================================================*/
  135. /* The following PRIVATE functions are in this file: */
  136. /*=========================================================================*/
  137. static BOOL File_Callback(short id, short *odd, void **buffer, long *size);
  138. static int __cdecl Stream_Sample_Vol(void *buffer, long size, BOOL (*callback)(short id, short *odd, void **buffer, long *size), int volume, int handle);
  139. void CALLBACK Sound_Timer_Callback ( UINT, UINT, DWORD, DWORD, DWORD );
  140. //static int __cdecl Stream_Sample(void *buffer, long size, BOOL (*callback)(short id, short *odd, void **buffer, long *size));
  141. void Sound_Thread (void *);
  142. volatile BOOL AudioDone;
  143. /*= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
  144. // This callback is called whenever the queue buffer playback has begun
  145. // and another buffer is needed for queuing up. Returns TRUE if there
  146. // is more data to read from the file.
  147. static BOOL File_Callback(short id, short *odd, void **buffer, long *size)
  148. {
  149. SampleTrackerType *st; // Pointer to sample playback control struct.
  150. void *ptr; // Pointer to working portion of file buffer.
  151. if (id != -1) {
  152. st = &LockedData.SampleTracker[id];
  153. ptr = st->FileBuffer;
  154. if (ptr) {
  155. /*
  156. ** Move the next pending block into the primary
  157. ** position. Do this only if the queue pointer is
  158. ** null.
  159. */
  160. EnterCriticalSection(&GlobalAudioCriticalSection);
  161. st->DontTouch = TRUE;
  162. LeaveCriticalSection(&GlobalAudioCriticalSection);
  163. if (!*buffer && st->FilePending) {
  164. *buffer = Add_Long_To_Pointer(ptr, (long)(*odd % LockedData.StreamBufferCount)*(long)LockedData.StreamBufferSize);
  165. st->FilePending--;
  166. *odd = (short)(*odd + 1);
  167. if (!st->FilePending) {
  168. *size = st->FilePendingSize;
  169. } else {
  170. *size = LockedData.StreamBufferSize;
  171. }
  172. }
  173. EnterCriticalSection(&GlobalAudioCriticalSection);
  174. st->DontTouch = FALSE;
  175. LeaveCriticalSection(&GlobalAudioCriticalSection);
  176. Sound_Timer_Callback(0,0,0,0,0); //Shouldnt block as we are calling it from the same thread
  177. /*
  178. ** If the file handle is still valid, then read in the next
  179. ** block and add it to the next pending slot available.
  180. */
  181. if (st->FilePending <
  182. (StreamLowImpact ? (LockedData.StreamBufferCount>>1) : ((LockedData.StreamBufferCount-3))) && st->FileHandle != WW_ERROR) {
  183. int num_empty_buffers;
  184. #ifdef SIMPLE_FILLING
  185. num_empty_buffers = (LockedData.StreamBufferCount-2) - st->FilePending;
  186. #else
  187. //
  188. // num_empty_buffers will be from 1 to StreamBufferCount
  189. //
  190. if (StreamLowImpact) {
  191. num_empty_buffers = MIN((LockedData.StreamBufferCount >> 1)+STREAM_CUSHION_BLOCKS, (LockedData.StreamBufferCount - 2) - st->FilePending);
  192. }
  193. else {
  194. num_empty_buffers = (LockedData.StreamBufferCount - 2) - st->FilePending;
  195. }
  196. #endif
  197. while (num_empty_buffers && (st->FileHandle != WW_ERROR)) {
  198. int tofill;
  199. long psize;
  200. tofill = (*odd + st->FilePending) % LockedData.StreamBufferCount;
  201. ptr = Add_Long_To_Pointer(st->FileBuffer, (long)tofill * (long)LockedData.StreamBufferSize);
  202. psize = Read_File(st->FileHandle, ptr, LockedData.StreamBufferSize);
  203. /*
  204. ** If less than the requested amount of data was read, this
  205. ** indicates that the source file is exhausted. Flag the source
  206. ** file as closed so that no further reading is attempted.
  207. */
  208. if (psize != LockedData.StreamBufferSize) {
  209. Close_File(st->FileHandle);
  210. st->FileHandle = WW_ERROR;
  211. }
  212. /*
  213. ** If any real data went into the pending buffer, then flag
  214. ** that this buffer is valid.
  215. */
  216. if (psize) {
  217. EnterCriticalSection(&GlobalAudioCriticalSection);
  218. st->DontTouch = TRUE;
  219. st->FilePendingSize = psize;
  220. st->FilePending++;
  221. st->DontTouch = FALSE;
  222. LeaveCriticalSection(&GlobalAudioCriticalSection);
  223. Sound_Timer_Callback(0,0,0,0,0); //Shouldnt block as we are calling it from the same thread
  224. }
  225. num_empty_buffers--;
  226. }
  227. /*
  228. ** After filling all pending buffers, check to see if the queue buffer
  229. ** is empty. If so, then assign the first available pending buffer to the
  230. ** queue.
  231. */
  232. EnterCriticalSection(&GlobalAudioCriticalSection);
  233. st->DontTouch = TRUE;
  234. if (!st->QueueBuffer && st->FilePending) {
  235. st->QueueBuffer = Add_Long_To_Pointer(st->FileBuffer, (long)(st->Odd%LockedData.StreamBufferCount)*(long)LockedData.StreamBufferSize);
  236. st->FilePending--;
  237. st->Odd++;
  238. if (!st->FilePending) {
  239. st->QueueSize = st->FilePendingSize;
  240. } else {
  241. st->QueueSize = LockedData.StreamBufferSize;
  242. }
  243. }
  244. st->DontTouch = FALSE;
  245. LeaveCriticalSection(&GlobalAudioCriticalSection);
  246. Sound_Timer_Callback(0,0,0,0,0); //Shouldnt block as we are calling it from the same thread
  247. }
  248. /*
  249. ** If there are no more buffers that the callback routine
  250. ** can slot into the primary position, then signal that
  251. ** no furthur callbacks are needed.
  252. */
  253. if (st->FilePending) {
  254. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  255. return(TRUE);
  256. }
  257. }
  258. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  259. }
  260. return(FALSE);
  261. }
  262. // Generic streaming sample playback initialization.
  263. static int __cdecl Stream_Sample_Vol(void *buffer, long size, BOOL (*callback)(short id, short *odd, void **buffer, long *size), int volume, int handle)
  264. {
  265. int playid=-1; // Sample play ID.
  266. SampleTrackerType *st; // Working pointer to sample control structure.
  267. long oldsize; // Copy of original sound size.
  268. AUDHeaderType header;
  269. if (buffer && size && LockedData.DigiHandle != -1) {
  270. /*
  271. ** Start the first section of the sound playing.
  272. */
  273. Mem_Copy(buffer, &header, sizeof(header));
  274. oldsize = header.Size;
  275. header.Size = size-sizeof(header);
  276. Mem_Copy(&header, buffer, sizeof(header));
  277. playid = Play_Sample_Handle(buffer, 0xFF, volume, 0x0, handle);
  278. header.Size = oldsize;
  279. Mem_Copy(&header, buffer, sizeof(header));
  280. /*
  281. ** If the sample actually started playing, then flag this
  282. ** sample as a streaming type and signal for a callback
  283. ** to occur.
  284. */
  285. if (playid != -1) {
  286. st = &LockedData.SampleTracker[playid];
  287. st->Callback = callback;
  288. st->Odd = 0;
  289. // ServiceSomething = TRUE;
  290. }
  291. }
  292. return (playid);
  293. }
  294. #if (0)
  295. static int __cdecl Stream_Sample(void *buffer, long size, BOOL (*callback)(short id, short *odd, void **buffer, long *size), int handle)
  296. {
  297. return Stream_Sample_Vol(buffer, size, callback, 0xFF, handle);
  298. }
  299. #endif
  300. /***********************************************************************************************
  301. * File_Stream_Sample -- Streams a sample directly from a file. *
  302. * *
  303. * This will take the file specified and play it directly from disk. *
  304. * It performs this by allocating a temporary buffer in XMS/EMS and *
  305. * then keeping this buffer filled by the Sound_Callback() routine. *
  306. * *
  307. * INPUT: filename -- The name of the file to play. *
  308. * *
  309. * OUTPUT: Returns the handle to the sound -- just like Play_Sample(). *
  310. * *
  311. * WARNINGS: The temporary buffer is allocated when this routine is *
  312. * called and then freed when the sound is finished. Keep *
  313. * this in mind. *
  314. * *
  315. * HISTORY: *
  316. * 01/06/1994 JLB : Created. *
  317. *=============================================================================================*/
  318. int File_Stream_Sample(char const *filename, BOOL real_time_start)
  319. {
  320. return File_Stream_Sample_Vol(filename, 0xFF, real_time_start);
  321. }
  322. /***************************************************************************
  323. * FILE_STREAM_PRELOAD -- Handles initial proload of streaming samples *
  324. * *
  325. * This function is called before a sample which streams from disk is *
  326. * started. It can be called to either fill the buffer in small chunks *
  327. * from the call back routine or to fill the entire buffer at once. This *
  328. * is wholely dependant on whether the Loading bit is set within the *
  329. * sample tracker. *
  330. * *
  331. * INPUT:LockedData.SampleTracker * to the header which tracks this samples*
  332. * processing.*
  333. * *
  334. * OUTPUT: *
  335. * *
  336. * WARNINGS: *
  337. * *
  338. * HISTORY: *
  339. * 06/05/1995 PWG : Created. *
  340. *=========================================================================*/
  341. void File_Stream_Preload(int handle)
  342. {
  343. SampleTrackerType *st = &LockedData.SampleTracker[handle];
  344. int fh = st->FileHandle;
  345. int maxnum = (LockedData.StreamBufferCount >> 1) + STREAM_CUSHION_BLOCKS;
  346. void *buffer = st->FileBuffer;
  347. int num;
  348. /*
  349. ** Figure just how much we need to load. If we are doing the load in progress
  350. ** then we will only load two blocks.
  351. */
  352. if (st->Loading) {
  353. num = st->FilePending + 2;
  354. num = MIN(num, maxnum);
  355. } else {
  356. num = maxnum;
  357. }
  358. //EnterCriticalSection(&GlobalAudioCriticalSection);
  359. /*
  360. ** Loop through the blocks and load up the number we need.
  361. */
  362. for (int index = st->FilePending; index < num; index++) {
  363. long s = Read_File(fh, Add_Long_To_Pointer(buffer, (long)index * (long)LockedData.StreamBufferSize), LockedData.StreamBufferSize);
  364. if (s) {
  365. st->FilePendingSize = s;
  366. st->FilePending++;
  367. }
  368. if (s < LockedData.StreamBufferSize) break;
  369. }
  370. Sound_Timer_Callback(0,0,0,0,0); //Shouldnt block as we are calling it from the same thread
  371. /*
  372. ** If the last block was incomplete (ie. it didn't completely fill the buffer) or
  373. ** we have now filled up as much of the Streaming Buffer as we need to, then now is
  374. ** the time to kick off the sample.
  375. */
  376. if (st->FilePendingSize < LockedData.StreamBufferSize || index == maxnum) {
  377. /*
  378. ** Actually start the sample playing, and don't worry about the file callback
  379. ** it won't be called for a while.
  380. */
  381. int old = LockedData.SoundVolume;
  382. int size = (st->FilePending == 1) ? st->FilePendingSize : LockedData.StreamBufferSize;
  383. LockedData.SoundVolume = LockedData.ScoreVolume;
  384. StartingFileStream = TRUE;
  385. Stream_Sample_Vol(buffer, size, File_Callback, st->Volume, handle);
  386. StartingFileStream = FALSE;
  387. LockedData.SoundVolume = old;
  388. /*
  389. ** The Sample is finished loading (if it was loading in small pieces) so record that
  390. ** so that it will now use the active logic in the file call back.
  391. */
  392. st->Loading = FALSE;
  393. /*
  394. ** Decrement the file pending because the first block is already playing thanks
  395. ** to the play sample call above.
  396. */
  397. st->FilePending--;
  398. /*
  399. ** If File pending is now a zero, then we only preloaded one block and there
  400. ** is nothing more to play. So clear the sample tracing structure of the
  401. ** information it no longer needs.
  402. */
  403. if (!st->FilePending) {
  404. st->Odd = 0;
  405. st->QueueBuffer = 0;
  406. st->QueueSize = 0;
  407. st->FilePendingSize = 0;
  408. st->Callback = NULL;
  409. Close_File(fh);
  410. } else {
  411. /*
  412. ** The QueueBuffer counts as an already played block so remove it from the total.
  413. ** Note: We didn't remove it before because there might not have been one.
  414. */
  415. st->FilePending--;
  416. /*
  417. ** When we start loading we need to start past the first two blocks. Why this
  418. ** is called Odd, I haven't got the slightest.
  419. */
  420. st->Odd = 2;
  421. /*
  422. ** If the file pending size is less than the stream buffer, then the last block
  423. ** we loaded was the last block period. So close the file and reset the handle.
  424. */
  425. if (st->FilePendingSize != LockedData.StreamBufferSize) {
  426. Close_File(fh);
  427. st->FileHandle = WW_ERROR;
  428. }
  429. /*
  430. ** The Queue buffer needs to point at the next block to be processed. The size
  431. ** of the queue is dependant on how many more blocks there are.
  432. */
  433. st->QueueBuffer = Add_Long_To_Pointer(buffer, LockedData.StreamBufferSize);
  434. if (!st->FilePending) {
  435. st->QueueSize = st->FilePendingSize;
  436. } else {
  437. st->QueueSize = LockedData.StreamBufferSize;
  438. }
  439. }
  440. }
  441. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  442. }
  443. /***********************************************************************************************
  444. * File_Stream_Sample_Vol -- Streams a sample directly from a file. *
  445. * *
  446. * This will take the file specified and play it directly from disk. *
  447. * It performs this by allocating a temporary buffer in XMS/EMS and *
  448. * then keeping this buffer filled by the Sound_Callback() routine. *
  449. * *
  450. * INPUT: filename -- The name of the file to play. *
  451. * *
  452. * OUTPUT: Returns the handle to the sound -- just like Play_Sample(). *
  453. * *
  454. * WARNINGS: The temporary buffer is allocated when this routine is *
  455. * called and then freed when the sound is finished. Keep *
  456. * this in mind. *
  457. * *
  458. * HISTORY: *
  459. *=============================================================================================*/
  460. int File_Stream_Sample_Vol(char const *filename, int volume, BOOL real_time_start)
  461. {
  462. static void *buffer = NULL;
  463. SampleTrackerType *st;
  464. int fh;
  465. int handle = -1;
  466. int index;
  467. if (LockedData.DigiHandle != -1 && filename && Find_File(filename)) {
  468. /*
  469. ** Make sure all sample tracker structures point to the same
  470. ** upper memory buffer. This allocation only occurs if at
  471. ** least one sample gets "streamed".
  472. */
  473. if (!buffer) {
  474. buffer = Alloc(LockedData.StreamBufferSize * LockedData.StreamBufferCount, (MemoryFlagType)(StreamBufferFlag | MEM_TEMP | MEM_LOCK));
  475. for (index = 0; index < MAX_SFX; index++) {
  476. LockedData.SampleTracker[index].FileBuffer = buffer;
  477. }
  478. }
  479. /*
  480. ** If after trying to allocate the buffer we still fail then
  481. ** we can stream this sample.
  482. */
  483. if (!buffer) return(-1);
  484. /*
  485. ** Lets see if we can sucessfully open up the file. If we can't,
  486. ** then there is no point in going any farther.
  487. */
  488. if ((fh = Open_File(filename, READ)) == -1) {
  489. return (-1);
  490. }
  491. /*
  492. ** Reserve a handle so that we can fill in the sample tracker
  493. ** with the needed information. If we dont get valid handle then
  494. ** we might as well give up.
  495. */
  496. if ((unsigned)(handle = Get_Free_Sample_Handle(0xFF)) >= MAX_SFX) {
  497. return(-1);
  498. }
  499. /*
  500. ** Now lets get a pointer to the proper sample handler and start
  501. ** our manipulations.
  502. */
  503. st = &LockedData.SampleTracker[handle];
  504. st->IsScore = TRUE;
  505. st->FilePending = 0;
  506. st->FilePendingSize = 0;
  507. st->Loading = real_time_start;
  508. st->Volume = volume;
  509. st->FileHandle = fh;
  510. /*
  511. ** Now that we have setup our initial data properly, let load up
  512. ** the beginning of the sample we intend to stream.
  513. */
  514. File_Stream_Preload(handle);
  515. }
  516. return (handle);
  517. }
  518. /***********************************************************************************************
  519. * Sound_Callback -- Audio driver callback function. *
  520. * *
  521. * Maintains the audio buffers. This routine must be called at least *
  522. * 11 times per second or else audio glitches will occur. *
  523. * *
  524. * INPUT: none *
  525. * *
  526. * OUTPUT: none *
  527. * *
  528. * WARNINGS: If this routine is not called often enough then audio *
  529. * glitches will occur. *
  530. * *
  531. * HISTORY: *
  532. * 01/06/1994 JLB : Created. *
  533. *=============================================================================================*/
  534. void __cdecl Sound_Callback(void)
  535. {
  536. int index;
  537. SampleTrackerType *st;
  538. if (LockedData.DigiHandle != -1) {
  539. /*
  540. ** Call the timer callback now as we may block it in this function
  541. */
  542. Sound_Timer_Callback(0,0,0,0,0);
  543. st = &LockedData.SampleTracker[0];
  544. for (index = 0; index < MAX_SFX; index++) {
  545. if (st->Loading) {
  546. File_Stream_Preload(index);
  547. } else {
  548. /*
  549. ** General service routine to handle moving small blocks from the
  550. ** source into the low RAM staging buffers.
  551. */
  552. if (st->Active) {
  553. /*
  554. ** Special check to see if the sample is a fading one AND
  555. ** it has faded to silence, then stop it here.
  556. */
  557. if (st->Reducer && !st->Volume) {
  558. //EnterCriticalSection(&GlobalAudioCriticalSection);
  559. Stop_Sample(index);
  560. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  561. } else {
  562. /*
  563. ** Fill the queuebuffer if it is currently empty
  564. ** and there is a callback function defined to fill it.
  565. **
  566. ** PWG/CDY & CO: We should be down by at least two blocks
  567. ** before we bother with this
  568. */
  569. if ((!st->QueueBuffer ||
  570. (st->FileHandle != WW_ERROR && st->FilePending < LockedData.StreamBufferCount-3)) &&
  571. st->Callback) {
  572. if (!st->Callback((short)index, (short int *)&st->Odd, &st->QueueBuffer, &st->QueueSize)) {
  573. st->Callback = NULL;
  574. }
  575. }
  576. }
  577. } else {
  578. /*
  579. ** This catches the case where a streaming sample gets
  580. ** aborted prematurely because of failure to call the
  581. ** callback function frequently enough. In this case, the
  582. ** sample will be flagged as inactive, but the file handle
  583. ** will not have been closed.
  584. */
  585. if (st->FileHandle != WW_ERROR) {
  586. //EnterCriticalSection(&GlobalAudioCriticalSection);
  587. Close_File(st->FileHandle);
  588. st->FileHandle = WW_ERROR;
  589. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  590. }
  591. }
  592. }
  593. /*
  594. ** Advance to the next sample control structure.
  595. */
  596. st++;
  597. }
  598. }
  599. }
  600. /***********************************************************************************************
  601. * Load_Sample -- Loads a digitized sample into RAM. *
  602. * *
  603. * This routine loads a digitized sample into RAM. *
  604. * *
  605. * INPUT: filename -- Name of the sound file to load. *
  606. * *
  607. * OUTPUT: Returns with a pointer to the loaded sound file. This pointer *
  608. * is passed to Play_Sample when playback is desired. *
  609. * *
  610. * WARNINGS: If there is insufficient memory to load the sample, then *
  611. * NULL will be returned. *
  612. * *
  613. * HISTORY: *
  614. * 04/17/1992 JLB : Created. *
  615. * 01/06/1994 JLB : HMI version. *
  616. *=============================================================================================*/
  617. void *Load_Sample(char const *filename)
  618. {
  619. void *buffer = NULL;
  620. long size;
  621. int fh;
  622. if (LockedData.DigiHandle == -1 || !filename || !Find_File(filename)) {
  623. return (NULL);
  624. }
  625. fh = Open_File(filename, READ);
  626. if (fh != WW_ERROR) {
  627. size = File_Size(fh)+sizeof(AUDHeaderType);
  628. buffer = Alloc(size, MEM_NORMAL);
  629. if (buffer) {
  630. Sample_Read(fh, buffer, size);
  631. }
  632. Close_File(fh);
  633. Misc = size;
  634. }
  635. return(buffer);
  636. }
  637. /***********************************************************************************************
  638. * Load_Sample_Into_Buffer -- Loads a digitized sample into a buffer. *
  639. * *
  640. * This routine is used to load a digitized sample into a buffer *
  641. * provided by the programmer. This buffer can be in XMS or EMS. *
  642. * *
  643. * INPUT: filename -- The filename to load. *
  644. * *
  645. * buffer -- Pointer to the buffer to load into. *
  646. * *
  647. * size -- The size of the buffer to load into. *
  648. * *
  649. * OUTPUT: Returns the number of bytes actually used in the buffer. *
  650. * *
  651. * WARNINGS: This routine will not overflow the buffer provided. This *
  652. * means that the buffer must be big enough to hold the data *
  653. * or else the sound will be cut short. *
  654. * *
  655. * HISTORY: *
  656. * 01/06/1994 JLB : Created. *
  657. *=============================================================================================*/
  658. long Load_Sample_Into_Buffer(char const *filename, void *buffer, long size)
  659. {
  660. int fh;
  661. /*
  662. ** Verify legality of parameters.
  663. */
  664. if (!buffer || !size || LockedData.DigiHandle == -1 || !filename || !Find_File(filename)) {
  665. return (NULL);
  666. }
  667. fh = Open_File(filename, READ);
  668. if (fh != WW_ERROR) {
  669. size = Sample_Read(fh, buffer, size);
  670. Close_File(fh);
  671. } else {
  672. return(0);
  673. }
  674. return(size);
  675. }
  676. /***********************************************************************************************
  677. * Sample_Read -- Reads sample data from an openned file. *
  678. * *
  679. * This routine reads a sample file. It is presumed that the file is *
  680. * already positioned at the start of the sample. From this, it can *
  681. * determine if it is a VOC or raw data and proceed accordingly. *
  682. * *
  683. * INPUT: fh -- File handle of already openned sample file. *
  684. * *
  685. * buffer -- Pointer to the buffer to load data into. *
  686. * *
  687. * size -- The size of the buffer. *
  688. * *
  689. * OUTPUT: Returns the number of bytes actually used in the buffer. *
  690. * *
  691. * WARNINGS: none *
  692. * *
  693. * HISTORY: *
  694. * 01/06/1994 JLB : Created. *
  695. *=============================================================================================*/
  696. long Sample_Read(int fh, void *buffer, long size)
  697. {
  698. AUDHeaderType RawHeader;
  699. void *outbuffer; // Pointer to start of raw data.
  700. long actual_bytes_read; // Actual bytes read in, including header
  701. /*
  702. ** Conversion formula for TCrate and Hz rate.
  703. **
  704. ** TC = 256 - 1m/rate
  705. ** rate = 1m / (256-TC)
  706. */
  707. if (!buffer || fh == WW_ERROR || size <= sizeof(RawHeader)) return(NULL);
  708. size -= sizeof(RawHeader);
  709. outbuffer = Add_Long_To_Pointer(buffer, sizeof(RawHeader));
  710. actual_bytes_read = Read_File(fh, &RawHeader, sizeof(RawHeader));
  711. actual_bytes_read +=Read_File(fh, outbuffer, MIN(size, RawHeader.Size));
  712. Mem_Copy(&RawHeader, buffer, sizeof(RawHeader));
  713. return(actual_bytes_read);
  714. }
  715. /***********************************************************************************************
  716. * Free_Sample -- Frees a previously loaded digitized sample. *
  717. * *
  718. * Use this routine to free the memory allocated by a previous call to *
  719. * Load_Sample. *
  720. * *
  721. * INPUT: sample -- Pointer to the sample to be freed. *
  722. * *
  723. * OUTPUT: none *
  724. * *
  725. * WARNINGS: none *
  726. * *
  727. * HISTORY: *
  728. * 04/17/1992 JLB : Created. *
  729. *=============================================================================================*/
  730. void Free_Sample(void const *sample)
  731. {
  732. if (sample) Free((void *)sample);
  733. }
  734. /***********************************************************************************************
  735. * Sound_Timer_Callback -- windows timer callback for sound maintenance *
  736. * *
  737. * *
  738. * *
  739. * INPUT: Nothing *
  740. * *
  741. * OUTPUT: Nothing *
  742. * *
  743. * WARNINGS: None *
  744. * *
  745. * HISTORY: *
  746. * 11/2/95 4:01PM ST : Created *
  747. *=============================================================================================*/
  748. void CALLBACK Sound_Timer_Callback ( UINT, UINT, DWORD, DWORD, DWORD )
  749. {
  750. //if (!InTimerCallback){
  751. //InTimerCallback++;
  752. //Colour_Debug (5);
  753. EnterCriticalSection(&GlobalAudioCriticalSection);
  754. maintenance_callback();
  755. LeaveCriticalSection(&GlobalAudioCriticalSection);
  756. //Colour_Debug (0);
  757. //InTimerCallback--;
  758. //}
  759. }
  760. void Sound_Thread (void *)
  761. {
  762. DuplicateHandle (GetCurrentProcess(), GetCurrentThread() , GetCurrentProcess() ,&SoundThreadHandle , THREAD_ALL_ACCESS , TRUE , 0);
  763. SetThreadPriority (SoundThreadHandle, THREAD_PRIORITY_TIME_CRITICAL);
  764. SoundThreadActive = TRUE;
  765. while (!AudioDone){
  766. EnterCriticalSection(&GlobalAudioCriticalSection);
  767. maintenance_callback();
  768. LeaveCriticalSection(&GlobalAudioCriticalSection);
  769. Sleep(1000/40);
  770. }
  771. SoundThreadActive = FALSE;
  772. }
  773. /***********************************************************************************************
  774. * Set_Primary_Buffer_Format -- set the format of the primary sound buffer *
  775. * *
  776. * *
  777. * *
  778. * INPUT: Nothing *
  779. * *
  780. * OUTPUT: TRUE if successfully set *
  781. * *
  782. * WARNINGS: None *
  783. * *
  784. * HISTORY: *
  785. * 12/22/95 4:06PM ST : Created *
  786. *=============================================================================================*/
  787. BOOL Set_Primary_Buffer_Format(void)
  788. {
  789. if (SoundObject && PrimaryBufferPtr){
  790. return (PrimaryBufferPtr->SetFormat ( &PrimaryBuffFormat ) == DS_OK);
  791. }
  792. return (FALSE);
  793. }
  794. /***********************************************************************************************
  795. * Print_Sound_Error -- show error messages from failed sound initialisation *
  796. * *
  797. * *
  798. * *
  799. * INPUT: error text *
  800. * handle to window *
  801. * *
  802. * OUTPUT: Nothing *
  803. * *
  804. * WARNINGS: None *
  805. * *
  806. * HISTORY: *
  807. * 2/7/96 10:17AM ST : Created *
  808. *=============================================================================================*/
  809. void Print_Sound_Error(char *sound_error, HWND window)
  810. {
  811. MessageBox(window, sound_error, "Command & Conquer", MB_ICONEXCLAMATION|MB_OK);
  812. }
  813. /***********************************************************************************************
  814. * Audio_Init -- Initialise the sound system *
  815. * *
  816. * *
  817. * *
  818. * INPUT: window - window to send callback messages to *
  819. * maximum bits_per_sample - 8 or 16 *
  820. * stereo - will stereo samples be played *
  821. * rate - maximum sample rate required *
  822. * reverse_channels *
  823. * *
  824. * OUTPUT: TRUE if correctly initialised *
  825. * *
  826. * WARNINGS: None *
  827. * *
  828. * HISTORY: *
  829. * Unknown.... *
  830. * 08-24-95 10:01am ST : Modified for Windows 95 Direct Sound *
  831. *=============================================================================================*/
  832. BOOL Audio_Init( HWND window , int bits_per_sample, BOOL stereo , int rate , int reverse_channels)
  833. {
  834. int index;
  835. int sample=1;
  836. short old_bits_per_sample;
  837. short old_block_align;
  838. long old_bytes_per_sec;
  839. Init_Locked_Data();
  840. memset(&LockedData.SampleTracker[0], 0, sizeof(LockedData.SampleTracker));
  841. if ( !SoundObject ){
  842. /*
  843. ** Create the direct sound object
  844. */
  845. if ( DirectSoundCreate (NULL,&SoundObject,NULL) !=DS_OK ) {
  846. Print_Sound_Error("Warning - Unable to create Direct Sound Object",window);
  847. return (FALSE);
  848. }
  849. /*
  850. ** Give ourselves exclusive access to it
  851. */
  852. if ( SoundObject->SetCooperativeLevel( window, DSSCL_EXCLUSIVE ) != DS_OK){
  853. Print_Sound_Error("Warning - Unable to set Direct Sound cooperative level",window);
  854. SoundObject->Release();
  855. SoundObject = NULL;
  856. return (FALSE);
  857. }
  858. /*
  859. ** Set up the primary buffer structure
  860. */
  861. memset (&BufferDesc , 0 , sizeof(DSBUFFERDESC));
  862. BufferDesc.dwSize=sizeof(DSBUFFERDESC);
  863. BufferDesc.dwFlags=DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
  864. /*
  865. ** Set up the primary buffer format
  866. */
  867. memset (&DsBuffFormat , 0 , sizeof(WAVEFORMATEX));
  868. DsBuffFormat.wFormatTag = WAVE_FORMAT_PCM;
  869. DsBuffFormat.nChannels = (unsigned short) (1 + stereo);
  870. DsBuffFormat.nSamplesPerSec = rate;
  871. DsBuffFormat.wBitsPerSample = (short) bits_per_sample;
  872. DsBuffFormat.nBlockAlign = (unsigned short)( (DsBuffFormat.wBitsPerSample/8) * DsBuffFormat.nChannels);
  873. DsBuffFormat.nAvgBytesPerSec= DsBuffFormat.nSamplesPerSec * DsBuffFormat.nBlockAlign;
  874. DsBuffFormat.cbSize = 0;
  875. /*
  876. ** Make a copy of the primary buffer description so we can reset its format later
  877. */
  878. memcpy (&PrimaryBufferDesc , &BufferDesc , sizeof(DSBUFFERDESC));
  879. memcpy (&PrimaryBuffFormat , &DsBuffFormat , sizeof(WAVEFORMATEX));
  880. /*
  881. ** Create the primary buffer object
  882. */
  883. if ( SoundObject->CreateSoundBuffer (&PrimaryBufferDesc ,
  884. &PrimaryBufferPtr ,
  885. NULL ) !=DS_OK ){
  886. Print_Sound_Error("Warning - Unable to create Direct Sound primary buffer",window);
  887. SoundObject->Release();
  888. SoundObject = NULL;
  889. return (FALSE);
  890. }
  891. /*
  892. ** Set the format of the primary sound buffer
  893. **
  894. */
  895. if (!Set_Primary_Buffer_Format()){
  896. /*
  897. ** If we failed to create a 16 bit primary buffer - try for an 8bit one
  898. */
  899. if (DsBuffFormat.wBitsPerSample == 16){
  900. /*
  901. ** Save the old values
  902. */
  903. old_bits_per_sample = DsBuffFormat.wBitsPerSample;
  904. old_block_align = DsBuffFormat.nBlockAlign;
  905. old_bytes_per_sec = DsBuffFormat.nAvgBytesPerSec;
  906. /*
  907. ** Set up the 8-bit ones
  908. */
  909. DsBuffFormat.wBitsPerSample = 8;
  910. DsBuffFormat.nBlockAlign = (unsigned short)( (DsBuffFormat.wBitsPerSample/8) * DsBuffFormat.nChannels);
  911. DsBuffFormat.nAvgBytesPerSec= DsBuffFormat.nSamplesPerSec * DsBuffFormat.nBlockAlign;
  912. /*
  913. ** Make a copy of the primary buffer description so we can reset its format later
  914. */
  915. memcpy (&PrimaryBufferDesc , &BufferDesc , sizeof(DSBUFFERDESC));
  916. memcpy (&PrimaryBuffFormat , &DsBuffFormat , sizeof(WAVEFORMATEX));
  917. }
  918. if (!Set_Primary_Buffer_Format()){
  919. /*
  920. ** We failed to set any useful format so print up an error message and give up
  921. */
  922. PrimaryBufferPtr->Release();
  923. PrimaryBufferPtr = NULL;
  924. SoundObject->Release();
  925. SoundObject = NULL;
  926. Print_Sound_Error("Warning - Your sound card does not match C&C's audio requirements",window);
  927. return (FALSE);
  928. }else{
  929. /*
  930. ** OK, got an 8bit sound buffer. Not perfect but it will do
  931. ** We still want 16 bit secondary buffers so restore those values
  932. */
  933. DsBuffFormat.wBitsPerSample = old_bits_per_sample;
  934. DsBuffFormat.nBlockAlign = old_block_align;
  935. DsBuffFormat.nAvgBytesPerSec = old_bytes_per_sec;
  936. }
  937. }
  938. /*
  939. ** Start the primary sound buffer playing
  940. **
  941. */if ( PrimaryBufferPtr->Play(0,0,DSBPLAY_LOOPING) != DS_OK ){
  942. Print_Sound_Error("Unable to play Direct Sound primary buffer",window);
  943. PrimaryBufferPtr->Release();
  944. PrimaryBufferPtr = NULL;
  945. SoundObject->Release();
  946. SoundObject = NULL;
  947. return (FALSE);
  948. }
  949. LockedData.DigiHandle=1;
  950. /*
  951. ** Initialise the global critical section object for sound thread syncronisation
  952. */
  953. InitializeCriticalSection(&GlobalAudioCriticalSection);
  954. /*
  955. ** Initialise the Windows timer system to provide us with a callback
  956. **
  957. */
  958. SoundTimerHandle = timeSetEvent ( 1000/MAINTENANCE_RATE , 1 , Sound_Timer_Callback , 0 , TIME_PERIODIC);
  959. AudioDone = FALSE;
  960. //_beginthread(&Sound_Thread, NULL, 16*1024, NULL);
  961. /*
  962. ** Define the format for the secondary sound buffers
  963. */
  964. BufferDesc.dwFlags=DSBCAPS_CTRLVOLUME;
  965. BufferDesc.dwBufferBytes=SECONDARY_BUFFER_SIZE;
  966. BufferDesc.lpwfxFormat = (LPWAVEFORMATEX) &DsBuffFormat;
  967. /*
  968. ** Allocate a decompression buffer equal to the size of a SONARC frame
  969. ** block.
  970. */
  971. LockedData.UncompBuffer = Alloc(LARGEST_SONARC_BLOCK + 50, (MemoryFlagType)(MEM_NORMAL|MEM_CLEAR|MEM_LOCK));
  972. /*
  973. ** Allocate once secondary direct sound buffer for each simultaneous sound effect
  974. **
  975. */
  976. for (index = 0; index < MAX_SFX; index++) {
  977. SoundObject->CreateSoundBuffer (&BufferDesc , &LockedData.SampleTracker[index].PlayBuffer , NULL);
  978. LockedData.SampleTracker[index].PlaybackRate = rate;
  979. LockedData.SampleTracker[index].Stereo = (stereo) ? AUD_FLAG_STEREO : 0;
  980. LockedData.SampleTracker[index].BitSize = (bits_per_sample == 16) ? AUD_FLAG_16BIT : 0;
  981. LockedData.SampleTracker[index].FileHandle = WW_ERROR;
  982. LockedData.SampleTracker[index].QueueBuffer = NULL;
  983. InitializeCriticalSection (&LockedData.SampleTracker[index].AudioCriticalSection);
  984. }
  985. SoundType = (SFX_Type)sample;
  986. SampleType = (Sample_Type)sample;
  987. ReverseChannels = reverse_channels;
  988. }
  989. return(TRUE);
  990. }
  991. /***********************************************************************************************
  992. * Sound_End -- Uninitializes the sound driver. *
  993. * *
  994. * This routine will uninitialize the sound driver (if any was *
  995. * installed). This routine must be called at program termination *
  996. * time. *
  997. * *
  998. * INPUT: none *
  999. * *
  1000. * OUTPUT: none *
  1001. * *
  1002. * WARNINGS: none *
  1003. * *
  1004. * HISTORY: *
  1005. * 07/23/1991 JLB : Created. *
  1006. * 11/02/1995 ST : Modified for Direct Sound *
  1007. *=============================================================================================*/
  1008. void Sound_End(void)
  1009. {
  1010. int index;
  1011. if (SoundObject && PrimaryBufferPtr){
  1012. /*
  1013. ** Stop all sounds and release the Direct Sound secondary sound buffers
  1014. */
  1015. for (index=0 ; index < MAX_SFX; index++){
  1016. if ( LockedData.SampleTracker[index].PlayBuffer ){
  1017. Stop_Sample (index);
  1018. LockedData.SampleTracker[index].PlayBuffer->Stop();
  1019. LockedData.SampleTracker[index].PlayBuffer->Release();
  1020. LockedData.SampleTracker[index].PlayBuffer = NULL;
  1021. DeleteCriticalSection(&LockedData.SampleTracker[index].AudioCriticalSection);
  1022. }
  1023. }
  1024. }
  1025. /*
  1026. ** Stop and release the direct sound primary buffer
  1027. */
  1028. if (PrimaryBufferPtr){
  1029. PrimaryBufferPtr->Stop();
  1030. PrimaryBufferPtr->Release();
  1031. PrimaryBufferPtr = NULL;
  1032. }
  1033. /*
  1034. ** Release the Direct Sound Object
  1035. */
  1036. if (SoundObject){
  1037. SoundObject->Release();
  1038. SoundObject = NULL;
  1039. }
  1040. if (LockedData.UncompBuffer) {
  1041. Free(LockedData.UncompBuffer);
  1042. LockedData.UncompBuffer = 0;
  1043. }
  1044. /*
  1045. ** Remove the Windows timer event we installed for the sound callback
  1046. */
  1047. if (SoundTimerHandle){
  1048. timeKillEvent(SoundTimerHandle);
  1049. SoundTimerHandle = 0;
  1050. }
  1051. AudioDone = TRUE;
  1052. /*
  1053. ** Since the timer has stopped, we are finished with our global critical section.
  1054. */
  1055. DeleteCriticalSection(&GlobalAudioCriticalSection);
  1056. }
  1057. /***********************************************************************************************
  1058. * Stop_Sample -- Stops any currently playing sampled sound. *
  1059. * *
  1060. * *
  1061. * *
  1062. * INPUT: *
  1063. * *
  1064. * OUTPUT: *
  1065. * *
  1066. * WARNINGS: *
  1067. * *
  1068. * HISTORY: *
  1069. * 06/02/1992 JLB : Created. *
  1070. * 11/2/95 4:09PM ST : Modified for Direct Sound *
  1071. *=============================================================================================*/
  1072. void Stop_Sample(int handle)
  1073. {
  1074. if (LockedData.DigiHandle != -1 && (unsigned)handle < MAX_SFX) {
  1075. EnterCriticalSection (&GlobalAudioCriticalSection);
  1076. if (LockedData.SampleTracker[handle].Active || LockedData.SampleTracker[handle].Loading) {
  1077. LockedData.SampleTracker[handle].Active = FALSE;
  1078. if (!LockedData.SampleTracker[handle].IsScore) {
  1079. LockedData.SampleTracker[handle].Original = NULL;
  1080. }
  1081. LockedData.SampleTracker[handle].Priority = 0;
  1082. /*
  1083. ** Stop the sample if it is playing.
  1084. */
  1085. if (!LockedData.SampleTracker[handle].Loading) {
  1086. LockedData.SampleTracker[handle].PlayBuffer->Stop();
  1087. }
  1088. LockedData.SampleTracker[handle].Loading = FALSE;
  1089. /*
  1090. ** If this is a streaming sample, then close the source file.
  1091. */
  1092. if (LockedData.SampleTracker[handle].FileHandle != WW_ERROR) {
  1093. Close_File(LockedData.SampleTracker[handle].FileHandle);
  1094. LockedData.SampleTracker[handle].FileHandle = WW_ERROR;
  1095. }
  1096. LockedData.SampleTracker[handle].QueueBuffer = NULL;
  1097. }
  1098. LeaveCriticalSection (&GlobalAudioCriticalSection);
  1099. }
  1100. }
  1101. /***********************************************************************************************
  1102. * Sample_Status -- Queries the current playing sample status (if any). *
  1103. * *
  1104. * *
  1105. * *
  1106. * INPUT: *
  1107. * *
  1108. * OUTPUT: *
  1109. * *
  1110. * WARNINGS: *
  1111. * *
  1112. * HISTORY: *
  1113. * 06/02/1992 JLB : Created. *
  1114. *=============================================================================================*/
  1115. BOOL Sample_Status(int handle)
  1116. {
  1117. DWORD status;
  1118. /*
  1119. ** If its an invalid handle or we do not have a sound driver then
  1120. ** the sample in question is not playing.
  1121. */
  1122. if (LockedData.DigiHandle == -1 || (unsigned)handle >= MAX_SFX) return(FALSE);
  1123. /*
  1124. ** If the sample is loading, then for all intents and purposes the
  1125. ** sample is playing.
  1126. */
  1127. if (LockedData.SampleTracker[handle].Loading) return(TRUE);
  1128. /*
  1129. ** If the sample is not active, then it is not playing
  1130. */
  1131. if (!LockedData.SampleTracker[handle].Active) return(FALSE);
  1132. /*
  1133. ** If we made it this far, then the Sample is still playing if sos says
  1134. ** that it is.
  1135. */
  1136. DumpBuffer = LockedData.SampleTracker[handle].PlayBuffer;
  1137. if (LockedData.SampleTracker[handle].PlayBuffer->GetStatus( &status ) == DS_OK){
  1138. return ( (DSBSTATUS_PLAYING & status) || (DSBSTATUS_LOOPING & status) );
  1139. }else{
  1140. return (TRUE);
  1141. }
  1142. }
  1143. /***********************************************************************************************
  1144. * Is_Sample_Playing -- returns the play state of a sample *
  1145. * *
  1146. * *
  1147. * *
  1148. * INPUT: ptr to sample data *
  1149. * *
  1150. * OUTPUT: TRUE if sample is currently playing *
  1151. * *
  1152. * WARNINGS: None *
  1153. * *
  1154. * HISTORY: *
  1155. * 11/2/95 4:11PM ST : Commented *
  1156. *=============================================================================================*/
  1157. BOOL Is_Sample_Playing(void const * sample)
  1158. {
  1159. int index;
  1160. //EnterCriticalSection(&GlobalAudioCriticalSection);
  1161. if (!sample) {
  1162. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  1163. return FALSE;
  1164. }
  1165. for (index = 0; index < MAX_SFX; index++) {
  1166. if (LockedData.SampleTracker[index].Original == sample && Sample_Status(index)) {
  1167. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  1168. return (TRUE);
  1169. }
  1170. }
  1171. //LeaveCriticalSection(&GlobalAudioCriticalSection);
  1172. return (FALSE);
  1173. }
  1174. /***********************************************************************************************
  1175. * Stop_Sample_Playing -- stops a playing sample *
  1176. * *
  1177. * *
  1178. * *
  1179. * INPUT: ptr to sample data *
  1180. * *
  1181. * OUTPUT: Nothing *
  1182. * *
  1183. * WARNINGS: None *
  1184. * *
  1185. * HISTORY: *
  1186. * 11/2/95 4:13PM ST : Commented *
  1187. *=============================================================================================*/
  1188. void Stop_Sample_Playing(void const * sample)
  1189. {
  1190. int index;
  1191. if (sample) {
  1192. for (index = 0; index < MAX_SFX; index++) {
  1193. if (LockedData.SampleTracker[index].Original == sample) {
  1194. Stop_Sample(index);
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. /***********************************************************************************************
  1201. * Get_Free_Sample_Handle -- finds a free slot in which to play a new sample *
  1202. * *
  1203. * *
  1204. * *
  1205. * INPUT: priority of sample we want to play *
  1206. * *
  1207. * OUTPUT: Handle or -1 if none free *
  1208. * *
  1209. * WARNINGS: None *
  1210. * *
  1211. * HISTORY: *
  1212. * 11/2/95 4:14PM ST : Added function header *
  1213. *=============================================================================================*/
  1214. int Get_Free_Sample_Handle(int priority)
  1215. {
  1216. int id;
  1217. /*
  1218. ** Find a free SFX holding buffer slot.
  1219. */
  1220. for (id = MAX_SFX - 1; id >= 0; id--) {
  1221. if (!LockedData.SampleTracker[id].Active && !LockedData.SampleTracker[id].Loading) {
  1222. if (!StartingFileStream && LockedData.SampleTracker[id].IsScore) {
  1223. StartingFileStream = TRUE; // Ensures only one channel is kept free for scores.
  1224. continue;
  1225. }
  1226. break;
  1227. }
  1228. }
  1229. if (id < 0) {
  1230. for (id = 0; id < MAX_SFX; id++) {
  1231. if (LockedData.SampleTracker[id].Priority <= priority) break;
  1232. }
  1233. if (id == MAX_SFX) {
  1234. return(-1); // Cannot play!
  1235. }
  1236. Stop_Sample(id); // This sample gets clobbered.
  1237. }
  1238. if (id == -1) {
  1239. return -1;
  1240. }
  1241. if (LockedData.SampleTracker[id].FileHandle != WW_ERROR) {
  1242. Close_File(LockedData.SampleTracker[id].FileHandle);
  1243. LockedData.SampleTracker[id].FileHandle = WW_ERROR;
  1244. }
  1245. if (LockedData.SampleTracker[id].Original && !LockedData.SampleTracker[id].IsScore) {
  1246. LockedData.SampleTracker[id].Original = NULL;
  1247. }
  1248. LockedData.SampleTracker[id].IsScore = FALSE;
  1249. return(id);
  1250. }
  1251. int Play_Sample(void const *sample, int priority, int volume, signed short panloc)
  1252. {
  1253. return(Play_Sample_Handle(sample, priority, volume, panloc, Get_Free_Sample_Handle(priority)));
  1254. }
  1255. /***********************************************************************************************
  1256. * Attempt_Audio_Restore -- tries to restore the direct sound buffers *
  1257. * *
  1258. * *
  1259. * *
  1260. * INPUT: ptr to direct sound buffer *
  1261. * *
  1262. * OUTPUT: TRUE if buffer was successfully restored *
  1263. * *
  1264. * WARNINGS: None *
  1265. * *
  1266. * HISTORY: *
  1267. * 3/20/96 9:47AM ST : Created *
  1268. *=============================================================================================*/
  1269. BOOL Attempt_Audio_Restore (LPDIRECTSOUNDBUFFER sound_buffer)
  1270. {
  1271. int return_code;
  1272. DWORD play_status;
  1273. int restore_attempts=0;
  1274. /*
  1275. ** Call the audio focus loss function if it has been set up
  1276. */
  1277. if (Audio_Focus_Loss_Function){
  1278. Audio_Focus_Loss_Function();
  1279. }
  1280. /*
  1281. ** Try to restore the sound buffer
  1282. */
  1283. do{
  1284. Restore_Sound_Buffers();
  1285. return_code = sound_buffer->GetStatus ( &play_status );
  1286. } while (restore_attempts++<2 && return_code == DSERR_BUFFERLOST);
  1287. return ((BOOL) ~(return_code == DSERR_BUFFERLOST));
  1288. }
  1289. /***********************************************************************************************
  1290. * Play_Sample_Vol -- Plays a digitized sample. *
  1291. * *
  1292. * Use this routine to play a previously loaded digitized sample. *
  1293. * *
  1294. * INPUT: sample -- Sample pointer as returned from Load_Sample. *
  1295. * *
  1296. * volume -- The volume to play (0..255 with 255=loudest). *
  1297. * *
  1298. * OUTPUT: none *
  1299. * *
  1300. * WARNINGS: none *
  1301. * *
  1302. * HISTORY: *
  1303. * 04/17/1992 JLB : Created. *
  1304. * 05/24/1992 JLB : Volume support -- Soundblaster Pro *
  1305. * 04/22/1994 JLB : Multiple sample playback rates. *
  1306. * 11/02/1995 ST : Windows Direct Sound support *
  1307. *=============================================================================================*/
  1308. extern BOOL Any_Locked(void);
  1309. int Play_Sample_Handle(void const *sample, int priority, int volume, signed short , int id)
  1310. {
  1311. AUDHeaderType RawHeader;
  1312. SampleTrackerType *st=NULL; // Working pointer to sample tracker structure.
  1313. LPVOID play_buffer_ptr; //pointer to locked direct sound buffer
  1314. LPVOID dummy_buffer_ptr; //dummy pointer to second area of locked direct sound buffer
  1315. DWORD lock_length1;
  1316. DWORD lock_length2;
  1317. DWORD play_status;
  1318. HRESULT return_code;
  1319. int retries=0;
  1320. if (Any_Locked()) return(0);
  1321. st = &LockedData.SampleTracker[id];
  1322. //EnterCriticalSection (&GlobalAudioCriticalSection);
  1323. if (!sample || LockedData.DigiHandle == -1) {
  1324. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1325. return(-1);
  1326. }
  1327. if (id == -1) {
  1328. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1329. return -1;
  1330. }
  1331. /*
  1332. ** Fetch the control bytes from the start of the sample data.
  1333. */
  1334. Mem_Copy((void *)sample, (void *)&RawHeader, sizeof(RawHeader));
  1335. /*
  1336. ** Fudge the sample rate to 22k
  1337. */
  1338. if (RawHeader.Rate <24000 && RawHeader.Rate >20000) RawHeader.Rate = 22050;
  1339. /*
  1340. ** Prepare the sample tracker structure for processing of this
  1341. ** sample. Fill the structure with data that can be determined
  1342. ** before the sample is started.
  1343. */
  1344. EnterCriticalSection(&GlobalAudioCriticalSection);
  1345. st->Compression = (SCompressType) ((unsigned char)RawHeader.Compression);
  1346. st->Original = sample;
  1347. st->OriginalSize = RawHeader.Size + sizeof(RawHeader);
  1348. st->Priority = (short)priority;
  1349. st->DontTouch = TRUE;
  1350. st->Odd = 0;
  1351. st->Reducer = 0;
  1352. st->Restart = FALSE;
  1353. st->QueueBuffer = NULL;
  1354. st->QueueSize = NULL;
  1355. st->TrailerLen = 0;
  1356. st->Remainder = RawHeader.Size;
  1357. st->Source = Add_Long_To_Pointer((void *)sample, sizeof(RawHeader));
  1358. st->Service = FALSE;
  1359. LeaveCriticalSection(&GlobalAudioCriticalSection);
  1360. /*
  1361. ** If the code in question using HMI based compression then we need
  1362. ** to set up for uncompressing it.
  1363. */
  1364. if (st->Compression == SCOMP_SOS) {
  1365. st->sosinfo.wChannels = (RawHeader.Flags & AUD_FLAG_STEREO) ? 2 : 1;
  1366. st->sosinfo.wBitSize = (RawHeader.Flags & AUD_FLAG_16BIT) ? 16 : 8;
  1367. st->sosinfo.dwCompSize = RawHeader.Size;
  1368. st->sosinfo.dwUnCompSize = RawHeader.Size * ( st->sosinfo.wBitSize / 4 );
  1369. sosCODECInitStream(&st->sosinfo);
  1370. }
  1371. /*
  1372. ** If the sample rate , bits per sample or stereo capabilities of the buffer do not
  1373. ** match the sample then reallocate the direct sound buffer with the required capabilities
  1374. */
  1375. if ( ( RawHeader.Rate != st->PlaybackRate ) ||
  1376. ( ( RawHeader.Flags & AUD_FLAG_16BIT ) != ( st->BitSize & AUD_FLAG_16BIT ) ) ||
  1377. ( ( RawHeader.Flags & AUD_FLAG_STEREO) != ( st->Stereo & AUD_FLAG_STEREO ) ) ) {
  1378. st->Active=0;
  1379. st->Service=0;
  1380. st->MoreSource=0;
  1381. /*
  1382. ** Stop the sound buffer playing
  1383. */
  1384. DumpBuffer = st->PlayBuffer;
  1385. do {
  1386. return_code = st->PlayBuffer->GetStatus ( &play_status );
  1387. if (return_code==DSERR_BUFFERLOST){
  1388. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1389. }
  1390. }while (return_code == DSERR_BUFFERLOST);
  1391. if (play_status & (DSBSTATUS_PLAYING | DSBSTATUS_LOOPING) ){
  1392. st->PlayBuffer->Stop();
  1393. if (return_code==DSERR_BUFFERLOST){
  1394. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1395. }
  1396. }
  1397. st->PlayBuffer->Release();
  1398. st->PlayBuffer=NULL;
  1399. DsBuffFormat.nSamplesPerSec = (unsigned short int) RawHeader.Rate;
  1400. DsBuffFormat.nChannels = (RawHeader.Flags & AUD_FLAG_STEREO) ? 2 : 1 ;
  1401. DsBuffFormat.wBitsPerSample = (RawHeader.Flags & AUD_FLAG_16BIT) ? 16 : 8 ;
  1402. DsBuffFormat.nBlockAlign = (short) ((DsBuffFormat.wBitsPerSample/8) * DsBuffFormat.nChannels);
  1403. DsBuffFormat.nAvgBytesPerSec= DsBuffFormat.nSamplesPerSec * DsBuffFormat.nBlockAlign;
  1404. /*
  1405. ** Create the new sound buffer
  1406. */
  1407. return_code= SoundObject->CreateSoundBuffer (&BufferDesc , &st->PlayBuffer , NULL);
  1408. if (return_code==DSERR_BUFFERLOST){
  1409. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1410. }
  1411. /*
  1412. ** Just return if the create failed unexpectedly
  1413. **
  1414. ** If we failed then flag the buffer as having an impossible format so it wont match
  1415. ** any sample. This will ensure that we try and create the buffer again next time its used.
  1416. */
  1417. if (return_code!=DS_OK && return_code!=DSERR_BUFFERLOST){
  1418. st->PlaybackRate = 0;
  1419. st->Stereo = 0;
  1420. st->BitSize = 0;
  1421. return(-1);
  1422. }
  1423. /*
  1424. ** Remember the format of the new buffer
  1425. */
  1426. st->PlaybackRate = RawHeader.Rate;
  1427. st->Stereo = RawHeader.Flags & AUD_FLAG_STEREO;
  1428. st->BitSize = RawHeader.Flags & AUD_FLAG_16BIT;
  1429. }
  1430. /*
  1431. ** Fill in 3/4 of the play buffer.
  1432. */
  1433. //
  1434. // Stop the sound buffer playing before we lock it
  1435. //
  1436. do {
  1437. DumpBuffer = st->PlayBuffer;
  1438. return_code = st->PlayBuffer->GetStatus ( &play_status );
  1439. if (return_code==DSERR_BUFFERLOST){
  1440. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1441. }
  1442. } while (return_code==DSERR_BUFFERLOST);
  1443. if (play_status & (DSBSTATUS_PLAYING | DSBSTATUS_LOOPING) ){
  1444. st->Active=0;
  1445. st->Service=0;
  1446. st->MoreSource=0;
  1447. st->PlayBuffer->Stop();
  1448. }
  1449. //
  1450. // Lock the direct sound buffer so we can write to it
  1451. //
  1452. do {
  1453. return_code = st->PlayBuffer->Lock ( 0 ,
  1454. SECONDARY_BUFFER_SIZE,
  1455. &play_buffer_ptr,
  1456. &lock_length1,
  1457. &dummy_buffer_ptr,
  1458. &lock_length2,
  1459. 0 );
  1460. if (return_code==DSERR_BUFFERLOST){
  1461. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1462. }
  1463. } while (return_code==DSERR_BUFFERLOST);
  1464. if (return_code != DS_OK) {
  1465. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1466. return (-1);
  1467. }
  1468. //
  1469. // Decompress the sample into the direct sound buffer
  1470. //
  1471. st->DestPtr=(void*)Sample_Copy ( st,
  1472. &st->Source,
  1473. &st->Remainder,
  1474. &st->QueueBuffer,
  1475. &st->QueueSize,
  1476. play_buffer_ptr,
  1477. SECONDARY_BUFFER_SIZE*1/4,
  1478. st->Compression,
  1479. &st->Trailer[0],
  1480. &st->TrailerLen);
  1481. if ( st->DestPtr==(void*) (SECONDARY_BUFFER_SIZE*1/4) ){
  1482. // Must be more data to copy so we dont need to zero the buffer
  1483. st->MoreSource=TRUE;
  1484. st->Service=TRUE;
  1485. st->OneShot=FALSE;
  1486. } else {
  1487. // Whole sample is in the buffer so flag that we dont need to
  1488. // copy more. Clear out the end of the buffer so that it
  1489. // goes quiet if we play past the end
  1490. st->MoreSource=FALSE;
  1491. st->OneShot=TRUE;
  1492. st->Service=TRUE; //We still need to service it so that we can stop it when
  1493. // it plays past the end of the sample data
  1494. //memset ( (char*)( (unsigned)play_buffer_ptr + (unsigned)st->DestPtr ), 0 , SECONDARY_BUFFER_SIZE - (unsigned)st->DestPtr );
  1495. memset ( (char*)( (unsigned)play_buffer_ptr + (unsigned)st->DestPtr ), 0 , SECONDARY_BUFFER_SIZE/4);
  1496. }
  1497. st->PlayBuffer->Unlock( play_buffer_ptr,
  1498. lock_length1,
  1499. dummy_buffer_ptr,
  1500. lock_length2);
  1501. /*
  1502. **
  1503. ** Set the volume of the sample.
  1504. **
  1505. */
  1506. st->Volume = (volume << 7);
  1507. #ifdef NO_VOLUME_CONTROL
  1508. do {
  1509. return_code = st->PlayBuffer->SetVolume (- ( ( (32768- ( (st->Volume >> 8) *LockedData.SoundVolume) )
  1510. *1000) >>15 ) );
  1511. if (return_code==DSERR_BUFFERLOST){
  1512. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1513. }
  1514. } while (return_code==DSERR_BUFFERLOST);
  1515. #endif //NO_VOLUME_CONTROL
  1516. /*
  1517. ** Make sure the primary sound buffer is playing
  1518. */
  1519. if (!Start_Primary_Sound_Buffer(FALSE)){
  1520. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1521. return(-1);
  1522. }
  1523. /*
  1524. ** Set the buffers play pointer to the beginning of the buffer
  1525. */
  1526. do {
  1527. return_code = st->PlayBuffer->SetCurrentPosition (0);
  1528. if (return_code==DSERR_BUFFERLOST){
  1529. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1530. }
  1531. } while (return_code==DSERR_BUFFERLOST);
  1532. /*
  1533. ** Start the sample playing now.
  1534. */
  1535. do
  1536. {
  1537. return_code = st->PlayBuffer->Play (0,0,DSBPLAY_LOOPING);
  1538. switch (return_code){
  1539. case DS_OK :
  1540. EnterCriticalSection (&GlobalAudioCriticalSection);
  1541. st->Active=TRUE;
  1542. st->Handle=(short)id;
  1543. st->DontTouch = FALSE;
  1544. LeaveCriticalSection (&GlobalAudioCriticalSection);
  1545. return (st->Handle);
  1546. case DSERR_BUFFERLOST :
  1547. if (!Attempt_Audio_Restore(st->PlayBuffer)) return(-1);
  1548. break;
  1549. default:
  1550. st->Active=FALSE;
  1551. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1552. return (st->Handle);
  1553. }
  1554. } while (return_code==DSERR_BUFFERLOST);
  1555. //LeaveCriticalSection (&GlobalAudioCriticalSection);
  1556. return (st->Handle);
  1557. }
  1558. /***********************************************************************************************
  1559. * Restore_Sound_Buffers -- restore the sound buffers *
  1560. * *
  1561. * *
  1562. * *
  1563. * INPUT: Nothing *
  1564. * *
  1565. * OUTPUT: Nothing *
  1566. * *
  1567. * WARNINGS: None *
  1568. * *
  1569. * HISTORY: *
  1570. * 11/3/95 3:53PM ST : Created *
  1571. *=============================================================================================*/
  1572. void Restore_Sound_Buffers ( void )
  1573. {
  1574. if (PrimaryBufferPtr){
  1575. PrimaryBufferPtr->Restore();
  1576. }
  1577. for ( int index = 0; index < MAX_SFX; index++) {
  1578. if (LockedData.SampleTracker[index].PlayBuffer){
  1579. LockedData.SampleTracker[index].PlayBuffer->Restore();
  1580. }
  1581. }
  1582. }
  1583. /***********************************************************************************************
  1584. * Set_Sound_Vol -- sets the overall volume for sampled sounds *
  1585. * *
  1586. * *
  1587. * *
  1588. * INPUT: volume *
  1589. * *
  1590. * OUTPUT: the previous volume setting *
  1591. * *
  1592. * WARNINGS: None *
  1593. * *
  1594. * HISTORY: *
  1595. * 11/2/95 4:19PM ST : Added function header *
  1596. *=============================================================================================*/
  1597. int Set_Sound_Vol(int volume)
  1598. {
  1599. int old;
  1600. old = LockedData.SoundVolume;
  1601. LockedData.SoundVolume = volume & 0xFF;
  1602. return(old);
  1603. }
  1604. /***********************************************************************************************
  1605. * Set_Score_Vol -- sets the overall volume for music scores *
  1606. * *
  1607. * *
  1608. * *
  1609. * INPUT: volume *
  1610. * *
  1611. * OUTPUT: the previous volume setting *
  1612. * *
  1613. * WARNINGS: None *
  1614. * *
  1615. * HISTORY: *
  1616. * 11/2/95 4:19PM ST : Added function header *
  1617. *=============================================================================================*/
  1618. int Set_Score_Vol(int volume)
  1619. {
  1620. int old;
  1621. SampleTrackerType *st; //ptr to SampleTracker structure
  1622. old = LockedData.ScoreVolume;
  1623. LockedData.ScoreVolume = volume & 0xFF;
  1624. for (int index=0 ; index<MAX_SFX ; index++){
  1625. st = &LockedData.SampleTracker[index];
  1626. if (st->IsScore && st->Active){
  1627. #ifdef NO_VOLUME_CONTROL
  1628. st->PlayBuffer->SetVolume (- ( ( (32768- ( (st->Volume >> 8) *LockedData.ScoreVolume) )
  1629. *1000) >>15 ) );
  1630. #endif //ifdef NO_VOLUME_CONTROL
  1631. }
  1632. }
  1633. return(old);
  1634. }
  1635. /***********************************************************************************************
  1636. * Fade_Sample -- Start a sample fading *
  1637. * *
  1638. * *
  1639. * *
  1640. * INPUT: Sample handle *
  1641. * fade rate *
  1642. * *
  1643. * OUTPUT: Nothing *
  1644. * *
  1645. * WARNINGS: None *
  1646. * *
  1647. * HISTORY: *
  1648. * 11/2/95 4:21PM ST : Added function header *
  1649. *=============================================================================================*/
  1650. void Fade_Sample(int handle, int ticks)
  1651. {
  1652. if (Sample_Status(handle)) {
  1653. if (!ticks || LockedData.SampleTracker[handle].Loading) {
  1654. Stop_Sample(handle);
  1655. } else {
  1656. SampleTrackerType * st;
  1657. st = &LockedData.SampleTracker[handle];
  1658. st->Reducer = (short) ((st->Volume / ticks)+1);
  1659. }
  1660. }
  1661. }
  1662. int Get_Digi_Handle(void)
  1663. {
  1664. return(LockedData.DigiHandle);
  1665. }
  1666. /***************************************************************************
  1667. * SAMPLE_LENGTH -- returns length of a sample in ticks *
  1668. * *
  1669. * INPUT: void const *sample - pointer to the sample to get length of. *
  1670. * *
  1671. * OUTPUT: long - length of the sample in ticks (60/sec) *
  1672. * *
  1673. * HISTORY: *
  1674. * 07/05/1995 PWG : Created. *
  1675. *=========================================================================*/
  1676. long Sample_Length(void const *sample)
  1677. {
  1678. AUDHeaderType RawHeader;
  1679. if (!sample) return(0);
  1680. Mem_Copy((void *)sample, (void *)&RawHeader, sizeof(RawHeader));
  1681. long time = RawHeader.UncompSize;
  1682. /*
  1683. ** If the sample is a 16 bit sample, then it will take only half
  1684. ** as long to play.
  1685. */
  1686. if (RawHeader.Flags & AUD_FLAG_16BIT) {
  1687. time >>= 1;
  1688. }
  1689. /*
  1690. ** If the sample is a stereo sample, then it will take only half
  1691. ** as long to play.
  1692. */
  1693. if (RawHeader.Flags & AUD_FLAG_STEREO) {
  1694. time >>= 1;
  1695. }
  1696. if (RawHeader.Rate/60) {
  1697. time /= (RawHeader.Rate/60);
  1698. }
  1699. return(time);
  1700. }
  1701. /***********************************************************************************************
  1702. * Start_Primary_Sound_Buffer -- start the primary sound buffer playing *
  1703. * *
  1704. * *
  1705. * *
  1706. * INPUT: Nothing *
  1707. * *
  1708. * OUTPUT: Nothing *
  1709. * *
  1710. * WARNINGS: None *
  1711. * *
  1712. * HISTORY: *
  1713. * 2/1/96 12:28PM ST : Created *
  1714. *=============================================================================================*/
  1715. extern BOOL GameInFocus;
  1716. BOOL Start_Primary_Sound_Buffer (BOOL forced)
  1717. {
  1718. DWORD status;
  1719. if (PrimaryBufferPtr && GameInFocus){
  1720. if (forced){
  1721. PrimaryBufferPtr->Play(0,0,DSBPLAY_LOOPING);
  1722. return (TRUE);
  1723. } else {
  1724. if (PrimaryBufferPtr->GetStatus (&status) == DS_OK){
  1725. if (! ((status & DSBSTATUS_PLAYING) || (status & DSBSTATUS_LOOPING))){
  1726. PrimaryBufferPtr->Play(0,0,DSBPLAY_LOOPING);
  1727. return (TRUE);
  1728. }else{
  1729. return (TRUE);
  1730. }
  1731. }
  1732. }
  1733. }
  1734. return (FALSE);
  1735. }
  1736. /***********************************************************************************************
  1737. * Stop_Primary_Sound_Buffer -- stops the primary sound buffer from playing. *
  1738. * *
  1739. * *
  1740. * *
  1741. * INPUT: Nothing *
  1742. * *
  1743. * OUTPUT: Nothing *
  1744. * *
  1745. * WARNINGS: This stops all sound playback *
  1746. * *
  1747. * HISTORY: *
  1748. * 2/1/96 12:28PM ST : Created *
  1749. *=============================================================================================*/
  1750. void Stop_Primary_Sound_Buffer (void)
  1751. {
  1752. if (PrimaryBufferPtr){
  1753. PrimaryBufferPtr->Stop();
  1754. PrimaryBufferPtr->Stop(); // Oh I
  1755. PrimaryBufferPtr->Stop(); // Hate Direct Sound
  1756. PrimaryBufferPtr->Stop(); // So much.....
  1757. }
  1758. for ( int index = 0; index < MAX_SFX; index++) {
  1759. Stop_Sample(index);
  1760. }
  1761. }
  1762. void Suspend_Audio_Thread(void)
  1763. {
  1764. if (SoundThreadActive){
  1765. SuspendThread(SoundThreadHandle);
  1766. SoundThreadActive = FALSE;
  1767. }
  1768. }
  1769. void Resume_Audio_Thread(void)
  1770. {
  1771. if (!SoundThreadActive){
  1772. ResumeThread(SoundThreadHandle);
  1773. SoundThreadActive = TRUE;
  1774. }
  1775. }