AUD_Device.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*****************************************************************************
  19. ** **
  20. ** Westwood Studios Pacific. **
  21. ** **
  22. ** Confidential Information **
  23. ** Copyright (C) 2000 - All Rights Reserved **
  24. ** **
  25. ******************************************************************************
  26. ** **
  27. ** Project: Dune Emperor **
  28. ** **
  29. ** Module: <module> (<prefix>_) **
  30. ** **
  31. ** Version: $ID$ **
  32. ** **
  33. ** File name: auddev.cpp **
  34. ** **
  35. ** Created by: 11/16/95 - TR **
  36. ** **
  37. ** Description: <description> **
  38. ** **
  39. *****************************************************************************/
  40. /*****************************************************************************
  41. ** Includes **
  42. *****************************************************************************/
  43. #include <wpaudio/altypes.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <wpaudio/memory.h>
  47. #include <wpaudio/list.h>
  48. #include <wpaudio/lock.h>
  49. #include <wpaudio/time.h>
  50. #include <wpaudio/attributes.h>
  51. #include <wpaudio/device.h>
  52. #include <wpaudio/channel.h>
  53. // 'assignment within condition expression'.
  54. #pragma warning(disable : 4706)
  55. DBG_DECLARE_TYPE ( AudioDevice )
  56. DBG_DECLARE_TYPE ( AudioSystem )
  57. DBG_DECLARE_TYPE ( AudioSearchStruct )
  58. DBG_DECLARE_TYPE ( AudioServiceInfoStruct )
  59. /*****************************************************************************
  60. ** Externals **
  61. *****************************************************************************/
  62. /*****************************************************************************
  63. ** Defines **
  64. *****************************************************************************/
  65. #define AUDIO_DEFAULT_SERVICE_INTERVAL (SECONDS(1)/30)
  66. #define VOLUME_SLACK (AUDIO_LEVEL_MAX/10)
  67. /*****************************************************************************
  68. ** Private Types **
  69. *****************************************************************************/
  70. // AudioDataStruct:
  71. // data that interrupt driven code needs to access is place in the struct
  72. typedef struct
  73. {
  74. ListHead systemList;
  75. ListHead devList;
  76. Lock devListAccess;
  77. AudioAttribs stdChannelAttribs;
  78. } AudioData;
  79. typedef struct
  80. {
  81. ListNode nd;
  82. AudioAttribs *attribs;
  83. DBG_TYPE()
  84. } AudioAttribsNode;
  85. DBG_DECLARE_TYPE ( AudioAttribsNode )
  86. /*****************************************************************************
  87. ** Private Data **
  88. *****************************************************************************/
  89. static AudioData audioDataStruct;
  90. static AudioData *audioData = &audioDataStruct;
  91. static int audioExclusiveSystemLoaded = FALSE;
  92. int audioInitialized = FALSE;
  93. /*****************************************************************************
  94. ** Public Data **
  95. *****************************************************************************/
  96. AudioAttribs *audioStdChannelAttribs = NULL;
  97. /*****************************************************************************
  98. ** Private Prototypes **
  99. *****************************************************************************/
  100. static AudioDevice* audioCreateDevice ( AudioSystem *system, int unit, AudioFormat *format );
  101. static void audioDestroyDevice ( AudioDevice *idev );
  102. static void audioDeviceAttribsListUpdate ( AudioDevice *idev );
  103. static void audioDeviceAttribsListUsed ( AudioDevice *idev );
  104. static AudioAttribsNode* audioDeviceAttribsListFind ( AudioDevice *idev, AudioAttribs * attribs);
  105. static void audioAddDevice ( AudioDevice *dev );
  106. static void audioRemoveDevice ( AudioDevice *dev );
  107. static AudioSystem* audioCreateSystem ( AudioSystemMaster *master );
  108. static void audioDestroySystem ( AudioSystem *system );
  109. static void audioAddSystem ( AudioSystem *system );
  110. static void audioRemoveSystem ( AudioSystem *system );
  111. static void audioMapDeviceUnit ( int unit, AudioSystem **system, int *systemUnit );
  112. static AudioDeviceCB audioDeviceHandler;
  113. /*****************************************************************************
  114. ** Private Functions **
  115. *****************************************************************************/
  116. /******************************************************************/
  117. /* */
  118. /* */
  119. /******************************************************************/
  120. static void audioDeviceAttribsListUpdate ( AudioDevice *idev )
  121. {
  122. AudioAttribsNode *node = ( AudioAttribsNode *) ListFirstItem ( &idev->attribsList );
  123. while ( node )
  124. {
  125. DBG_ASSERT_TYPE ( node, AudioAttribsNode );
  126. AudioAttribsUpdate ( node->attribs );
  127. node = (AudioAttribsNode *) ListNodeNext ( &node->nd );
  128. }
  129. }
  130. /******************************************************************/
  131. /* */
  132. /* */
  133. /******************************************************************/
  134. static void audioDeviceAttribsListUsed ( AudioDevice *idev )
  135. {
  136. AudioAttribsNode *node = ( AudioAttribsNode *) ListFirstItem ( &idev->attribsList );
  137. while ( node )
  138. {
  139. DBG_ASSERT_TYPE ( node, AudioAttribsNode );
  140. AudioAttribsUsed ( node->attribs );
  141. node = (AudioAttribsNode *) ListNodeNext ( &node->nd );
  142. }
  143. }
  144. /******************************************************************/
  145. /* */
  146. /* */
  147. /******************************************************************/
  148. static AudioAttribsNode* audioDeviceAttribsListFind ( AudioDevice *idev, AudioAttribs * attribs)
  149. {
  150. AudioAttribsNode *node = ( AudioAttribsNode *) ListFirstItem ( &idev->attribsList );
  151. while ( node )
  152. {
  153. DBG_ASSERT_TYPE ( node, AudioAttribsNode );
  154. if ( node->attribs == attribs )
  155. {
  156. return node;
  157. }
  158. node = (AudioAttribsNode *) ListNodeNext ( &node->nd );
  159. }
  160. return NULL;
  161. }
  162. /******************************************************************/
  163. /* */
  164. /* */
  165. /******************************************************************/
  166. static AudioDevice* audioCreateDevice ( AudioSystem *system, int unit, AudioFormat *format )
  167. {
  168. AudioDevice *dev = NULL;
  169. AudioSystemMaster *master = system->master;
  170. // create and initialise dev struct
  171. ALLOC_STRUCT ( dev, AudioDevice );
  172. // initialise driver structure
  173. DBG_SET_TYPE ( dev, AudioDevice );
  174. ListNodeInit ( &dev->nd );
  175. dev->systemUnit = unit;
  176. dev->system = system;
  177. LockInit ( &dev->lock );
  178. LockAcquire ( &system->lock ); // mark system as being used
  179. system->unit[unit] = dev;
  180. dev->driver = master->driver;
  181. ListInit ( &dev->channelList);
  182. ListInit ( &dev->attribsList);
  183. AudioAttribsInit ( &dev->Attribs );
  184. LockInit ( &dev->channelAccess );
  185. dev->deviceHandler = audioDeviceHandler ;
  186. AudioServiceInfoInit ( & dev->attribsUpdate );
  187. AudioServiceSetInterval ( &dev->attribsUpdate, AUDIO_DEFAULT_SERVICE_INTERVAL );
  188. AudioServiceSetMustServiceInterval ( &dev->attribsUpdate, AUDIO_DEFAULT_SERVICE_INTERVAL*5 );
  189. AudioServiceSetResetInterval ( &dev->attribsUpdate, AUDIO_DEFAULT_SERVICE_INTERVAL*5 );
  190. AudioServiceInfoInit ( & dev->mixerUpdate );
  191. AudioServiceSetInterval ( &dev->mixerUpdate, AUDIO_DEFAULT_SERVICE_INTERVAL );
  192. AudioFormatInit ( &dev->DefaultFormat ); // default sample sormat
  193. AudioFormatInit ( &dev->Format );
  194. if ( format )
  195. {
  196. AudioDeviceSetDefaultFormat ( dev, format );
  197. }
  198. // initialise device driver
  199. if ( master->open ( dev ) != vNO_ERROR )
  200. {
  201. goto error;
  202. }
  203. return dev;
  204. error:
  205. if (dev)
  206. {
  207. audioDestroyDevice ( dev );
  208. }
  209. return NULL;
  210. }
  211. /******************************************************************/
  212. /* */
  213. /* */
  214. /******************************************************************/
  215. static void audioDestroyDevice ( AudioDevice *dev )
  216. {
  217. DBG_ASSERT_TYPE ( dev, AudioDevice );
  218. AudioDeviceAttribsRemoveAll ( dev );
  219. AudioDeviceDestroyAllChannels ( dev, AUDIO_CHANNEL_TYPE_ALL );
  220. dev->system->master->close ( dev );
  221. dev->system->unit[dev->systemUnit] = NULL;
  222. LockRelease ( &dev->system->lock );
  223. AudioMemFree ( dev );
  224. }
  225. /******************************************************************/
  226. /* */
  227. /* */
  228. /******************************************************************/
  229. static void audioAddDevice ( AudioDevice *dev )
  230. {
  231. LockAcquire ( &audioData->devListAccess );
  232. ListAddToTail ( &audioData->devList, &dev->nd );
  233. LockRelease ( &audioData->devListAccess );
  234. }
  235. /******************************************************************/
  236. /* */
  237. /* */
  238. /******************************************************************/
  239. static void audioRemoveDevice ( AudioDevice *dev )
  240. {
  241. LockAcquire ( &audioData->devListAccess );
  242. ListNodeRemove ( &dev->nd );
  243. LockRelease ( &audioData->devListAccess );
  244. }
  245. /******************************************************************/
  246. /* */
  247. /* */
  248. /******************************************************************/
  249. static AudioSystem* audioCreateSystem ( AudioSystemMaster *master )
  250. {
  251. AudioSystem *system = NULL;
  252. ALLOC_STRUCT ( system, AudioSystem );
  253. DBG_SET_TYPE ( system, AudioSystem );
  254. ListNodeInit ( &system->nd );
  255. LockInit ( &system->lock );
  256. system->master = master;
  257. if ( master->load ( system ) != vNO_ERROR )
  258. {
  259. goto error;
  260. }
  261. return system;
  262. error:
  263. if ( system )
  264. {
  265. audioDestroySystem ( system );
  266. }
  267. return NULL;
  268. }
  269. /******************************************************************/
  270. /* */
  271. /* */
  272. /******************************************************************/
  273. static void audioDestroySystem ( AudioSystem *system )
  274. {
  275. system->master->unload ( system );
  276. FLAGS_CLEAR (system->master->flags, mAUDIO_SYSTEM_MASTER_LOADED);
  277. AudioMemFree ( system );
  278. }
  279. /******************************************************************/
  280. /* */
  281. /* */
  282. /******************************************************************/
  283. static void audioAddSystem ( AudioSystem *system )
  284. {
  285. ListAddToTail ( &audioData->systemList, &system->nd );
  286. }
  287. /******************************************************************/
  288. /* */
  289. /* */
  290. /******************************************************************/
  291. static void audioRemoveSystem ( AudioSystem *system )
  292. {
  293. ListNodeRemove ( &system->nd );
  294. }
  295. /******************************************************************/
  296. /* */
  297. /* */
  298. /******************************************************************/
  299. void audioAddChannel ( AudioDevice *dev, AudioChannel *chan )
  300. {
  301. chan->Device = dev;
  302. dev->channels++;
  303. LockAcquire ( &dev->channelAccess );
  304. ListAddToTail ( &dev->channelList, &chan->nd );
  305. LockRelease ( &dev->channelAccess );
  306. }
  307. /******************************************************************/
  308. /* */
  309. /* */
  310. /******************************************************************/
  311. void audioRemoveChannel ( AudioChannel *chan )
  312. {
  313. AudioDevice *dev = (AudioDevice *) chan->Device;
  314. ListNodeRemove ( &chan->nd );
  315. dev->channels--;
  316. LockAcquire ( &dev->channelAccess );
  317. ListNodeRemove ( &chan->nd );
  318. LockRelease ( &dev->channelAccess );
  319. }
  320. /******************************************************************/
  321. /* */
  322. /* */
  323. /******************************************************************/
  324. static void audioMapDeviceUnit ( int unit, AudioSystem **pSystem, int *pUnit )
  325. {
  326. AudioSystem *system;
  327. AudioSearch sh;
  328. system = AudioFirstSystem ( &sh );
  329. while ( system )
  330. {
  331. if ( unit < system->numUnits )
  332. {
  333. break;
  334. }
  335. unit -= system->numUnits;
  336. system = AudioNextSystem ( &sh );
  337. }
  338. if ( (*pSystem = system) )
  339. {
  340. *pUnit = unit; // we found it
  341. }
  342. else
  343. {
  344. *pUnit = -1; // no such unit
  345. }
  346. }
  347. /******************************************************************/
  348. /* */
  349. /* */
  350. /******************************************************************/
  351. static int audioDeviceHandler ( AudioDevice *dev )
  352. {
  353. AudioChannel *chan,
  354. *next,
  355. *head;
  356. int change = FALSE;
  357. TimeStamp now;
  358. if (!NotLocked(&dev->channelAccess ))
  359. {
  360. // channel lists are being accessed so we can do nothing at this time
  361. return ERROR_CODE_IN_USE;
  362. }
  363. now = AudioGetTime ();
  364. if ( !AudioServiceNeeded ( &dev->attribsUpdate, now ) )
  365. {
  366. return vNO_ERROR;
  367. }
  368. AudioServicePerform ( &dev->attribsUpdate, now );
  369. audioDeviceAttribsListUpdate ( dev );
  370. AudioAttribsUpdate ( &dev->Attribs );
  371. if ( !dev->GroupAttribs || !(change = AudioAttribsChanged( dev->GroupAttribs )))
  372. {
  373. change = AudioAttribsChanged( &dev->Attribs ) ;
  374. }
  375. chan = head = (AudioChannel *) &dev->channelList;
  376. next = (AudioChannel *) chan->nd.next;
  377. while ( (chan = next) != head )
  378. {
  379. next = (AudioChannel *) chan->nd.next;
  380. if ( ( chan->Control.Status & mAUDIO_CTRL_PLAYING ))
  381. {
  382. // update channel attributes
  383. if ( chan->sample )
  384. {
  385. AudioAttribsUpdate ( &chan->ChannelAttribs );
  386. if ( change || AudioAttribsChanged (&chan->ChannelAttribs) ||
  387. (chan->GroupAttribs && AudioAttribsChanged ( chan->GroupAttribs )) ||
  388. (chan->CompAttribs && AudioAttribsChanged ( chan->CompAttribs )) ||
  389. (chan->FadeAttribs && AudioAttribsChanged ( chan->FadeAttribs )) ||
  390. (chan->SfxAttribs && AudioAttribsChanged ( chan->SfxAttribs )) )
  391. {
  392. audioChannelRecalcAttribs ( chan );
  393. chan->driver->update ( chan );
  394. }
  395. }
  396. }
  397. }
  398. AudioAttribsUsed ( &dev->Attribs );
  399. audioDeviceAttribsListUsed ( dev );
  400. return vNO_ERROR;
  401. }
  402. /*****************************************************************************
  403. ** Public Functions **
  404. *****************************************************************************/
  405. /******************************************************************/
  406. /* */
  407. /* */
  408. /******************************************************************/
  409. int AudioSetUp ( void )
  410. {
  411. DBG_ASSERT ( !audioInitialized ); // audio system was already initialized
  412. DBGPRINTF (("Initializing audio module\n"));
  413. DBG_ASSERT ( audioData != NULL );
  414. InitAudioTimer();
  415. // Initialize lists
  416. ListInit ( &audioData->systemList );
  417. ListInit ( &audioData->devList );
  418. LockInit ( &audioData->devListAccess );
  419. audioStdChannelAttribs = &audioData->stdChannelAttribs;
  420. AudioAttribsInit ( audioStdChannelAttribs );
  421. // Process tags
  422. audioInitialized = TRUE;
  423. return vNO_ERROR;
  424. }
  425. /******************************************************************/
  426. /* */
  427. /* */
  428. /******************************************************************/
  429. void AudioCloseDown ( void)
  430. {
  431. if (!audioInitialized)
  432. {
  433. return;
  434. }
  435. AudioDestroyAllDevices ();
  436. AudioUnloadAllSystems ();
  437. DBGPRINTF (("WPAudio system has been shut down\n"));
  438. audioInitialized = FALSE;
  439. }
  440. /******************************************************************/
  441. /* */
  442. /* */
  443. /******************************************************************/
  444. void AudioServiceAllDevices ( void )
  445. {
  446. AudioDevice *dev,*head;
  447. AudioData *data = audioData;
  448. if ( !audioInitialized || !NotLocked ( &data->devListAccess ))
  449. {
  450. return;
  451. }
  452. dev = head = (AudioDevice *) &data->devList;
  453. while ( (dev = (AudioDevice *) dev->nd.next) != head )
  454. {
  455. DBG_ASSERT ( dev->deviceHandler != NULL );
  456. if ( !(dev->flags & mAUDIO_DEV_DONT_SERVICE ))
  457. {
  458. dev->deviceHandler ( dev );
  459. }
  460. }
  461. }
  462. /******************************************************************/
  463. /* */
  464. /* */
  465. /******************************************************************/
  466. AudioSystem* AudioLoadSystem ( AudioSystemMaster *master )
  467. {
  468. AudioSystem *system;
  469. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  470. DBG_ASSERT ( master != NULL );
  471. DBG_ASSERT ( master->stamp == AUDIO_STAMP_SYSTEM_MASTER );
  472. DBG_ASSERT ( ! (master->flags & mAUDIO_SYSTEM_MASTER_LOADED )); // master was already loaded
  473. DBG_ASSERT ( !audioExclusiveSystemLoaded);
  474. DBGPRINTF (("Loading driver system for %s\n", master->Name));
  475. if ( ! (system = audioCreateSystem ( master )))
  476. {
  477. goto error;
  478. }
  479. master->flags |= mAUDIO_SYSTEM_MASTER_LOADED;
  480. audioAddSystem ( system );
  481. if ( master->Properties & mAUDIO_SYS_PROP_EXCLUSIVE )
  482. {
  483. audioExclusiveSystemLoaded = TRUE;
  484. }
  485. return system;
  486. error:
  487. if ( system)
  488. {
  489. audioDestroySystem ( system );
  490. }
  491. return NULL;
  492. }
  493. /******************************************************************/
  494. /* */
  495. /* */
  496. /******************************************************************/
  497. void AudioUnloadSystem ( AudioSystem *system )
  498. {
  499. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  500. DBG_ASSERT_TYPE ( system, AudioSystem );
  501. DBG_ASSERT ( !Locked ( &system->lock )); // trying to unload a system while it is still being used
  502. audioRemoveSystem ( system );
  503. audioDestroySystem ( system );
  504. audioExclusiveSystemLoaded = FALSE;
  505. }
  506. /******************************************************************/
  507. /* */
  508. /* */
  509. /******************************************************************/
  510. void AudioUnloadAllSystems ( void )
  511. {
  512. AudioSystem *sys,*next;
  513. AudioSearch sh;
  514. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  515. sys = AudioFirstSystem ( &sh );
  516. while (sys)
  517. {
  518. next = AudioNextSystem( &sh );
  519. AudioUnloadSystem ( sys );
  520. sys = next;
  521. }
  522. }
  523. /******************************************************************/
  524. /* */
  525. /* */
  526. /******************************************************************/
  527. AudioSystem* AudioFirstSystem ( AudioSearch *sh )
  528. {
  529. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  530. DBG_SET_TYPE ( sh, AudioSearchStruct );
  531. sh->item = ListFirstItem ( &audioData->systemList );
  532. DBG_CODE
  533. (
  534. if ( sh->item )
  535. {
  536. DBG_ASSERT_TYPE ( (AudioSystem *) sh->item, AudioSystem );
  537. }
  538. )
  539. return (AudioSystem *) sh->item;
  540. }
  541. /******************************************************************/
  542. /* */
  543. /* */
  544. /******************************************************************/
  545. AudioSystem* AudioNextSystem ( AudioSearch *sh )
  546. {
  547. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  548. DBG_ASSERT_TYPE ( sh, AudioSearchStruct );
  549. if ( sh->item )
  550. {
  551. sh->item = ListNextItem ( (ListNode*) sh->item );
  552. }
  553. DBG_CODE
  554. (
  555. if ( sh->item )
  556. {
  557. DBG_ASSERT_TYPE ( (AudioSystem *) sh->item, AudioSystem );
  558. }
  559. )
  560. return (AudioSystem *) sh->item;
  561. }
  562. /******************************************************************/
  563. /* */
  564. /* */
  565. /******************************************************************/
  566. int AudioNumberOfDevices ( void )
  567. {
  568. int devices = 0;
  569. AudioSystem *system;
  570. AudioSearch sh;
  571. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  572. system = AudioFirstSystem ( &sh );
  573. while ( system )
  574. {
  575. devices += system->numUnits;
  576. system = AudioNextSystem ( &sh );
  577. }
  578. return devices;
  579. }
  580. /******************************************************************/
  581. /* */
  582. /* */
  583. /******************************************************************/
  584. AudioDevice* AudioOpenDevice ( int unit, AudioFormat *format )
  585. {
  586. AudioDevice *dev = NULL;
  587. AudioSystem *system;
  588. int systemUnit;
  589. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  590. if ( unit == AUDIO_DEVICE_DEFAULT )
  591. {
  592. unit = 0;
  593. }
  594. // map unit number to system and system unit number
  595. audioMapDeviceUnit ( unit, &system, &systemUnit );
  596. if ( !system ) // there is no such unit
  597. {
  598. DBGPRINTF (("No such unit\n"));
  599. goto error;
  600. }
  601. if ( !( dev = system->unit[systemUnit]))
  602. {
  603. // first time opening this device
  604. if ( ! (dev = audioCreateDevice ( system, systemUnit, format )))
  605. {
  606. DBGPRINTF (("Unable to create device\n"));
  607. goto error; // failed to open the device driver
  608. }
  609. dev->Unit = unit;
  610. audioAddDevice ( dev ); // add it to the device list
  611. }
  612. LockAcquire (&dev->lock); // mark as being used
  613. return dev;
  614. error:
  615. if ( dev )
  616. {
  617. AudioDeviceClose ( dev );
  618. }
  619. return NULL;
  620. }
  621. /******************************************************************/
  622. /* */
  623. /* */
  624. /******************************************************************/
  625. void AudioDeviceSetDefaultFormat ( AudioDevice *dev, AudioFormat *format )
  626. {
  627. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  628. DBG_ASSERT_TYPE ( dev, AudioDevice );
  629. DBG_ASSERT ( format != NULL );
  630. memcpy ( &dev->DefaultFormat, format, sizeof ( AudioFormat ) );
  631. }
  632. /******************************************************************/
  633. /* */
  634. /* */
  635. /******************************************************************/
  636. AudioDevice* AudioFirstDevice ( AudioSearch *sh )
  637. {
  638. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  639. DBG_SET_TYPE ( sh, AudioSearchStruct );
  640. sh->item = ListFirstItem ( &audioData->devList );
  641. DBG_CODE
  642. (
  643. if ( sh->item )
  644. {
  645. DBG_ASSERT_TYPE ( (AudioDevice *) sh->item, AudioDevice );
  646. }
  647. )
  648. return (AudioDevice *) sh->item;
  649. }
  650. /******************************************************************/
  651. /* */
  652. /* */
  653. /******************************************************************/
  654. AudioDevice* AudioNextDevice ( AudioSearch *sh )
  655. {
  656. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  657. DBG_ASSERT_TYPE ( sh, AudioSearchStruct );
  658. if ( sh->item )
  659. {
  660. sh->item = ListNextItem ( (ListNode*) sh->item );
  661. }
  662. DBG_CODE
  663. (
  664. if ( sh->item )
  665. {
  666. DBG_ASSERT_TYPE ( (AudioDevice *) sh->item, AudioDevice );
  667. }
  668. )
  669. return (AudioDevice *) sh->item;
  670. }
  671. /******************************************************************/
  672. /* */
  673. /* */
  674. /******************************************************************/
  675. void AudioDestroyAllDevices ( void )
  676. {
  677. AudioDevice *dev,*next;
  678. AudioSearch sh;
  679. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  680. LockAcquire ( &audioData->devListAccess);
  681. dev = AudioFirstDevice ( &sh );
  682. while (dev)
  683. {
  684. next = AudioNextDevice( &sh );
  685. AudioDeviceDestroy ( dev );
  686. dev = next;
  687. }
  688. LockRelease ( &audioData->devListAccess);
  689. }
  690. /******************************************************************/
  691. /* */
  692. /* */
  693. /******************************************************************/
  694. void AudioDeviceClose ( AudioDevice *dev )
  695. {
  696. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  697. DBG_ASSERT_TYPE ( dev, AudioDevice );
  698. LockRelease ( &dev->lock );
  699. if ( !Locked ( &dev->lock ))
  700. {
  701. AudioDeviceDestroy ( dev );
  702. }
  703. }
  704. /******************************************************************/
  705. /* */
  706. /* */
  707. /******************************************************************/
  708. void AudioDeviceDestroy ( AudioDevice *dev )
  709. {
  710. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  711. DBG_ASSERT_TYPE ( dev, AudioDevice );
  712. audioRemoveDevice ( dev );
  713. audioDestroyDevice ( dev );
  714. }
  715. /******************************************************************/
  716. /* */
  717. /* */
  718. /******************************************************************/
  719. int AudioDeviceSetFormat ( AudioDevice *dev, AudioFormat *format)
  720. {
  721. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  722. DBG_ASSERT_TYPE ( dev, AudioDevice );
  723. DBG_ASSERT ( format );
  724. return ERROR_CODE_FAIL;
  725. }
  726. /******************************************************************/
  727. /* */
  728. /* */
  729. /******************************************************************/
  730. void AudioDeviceSetVolume ( AudioDevice *dev, int newVolume )
  731. {
  732. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  733. DBG_ASSERT_TYPE ( dev, AudioDevice );
  734. AudioAttribsSetVolume ( &dev->Attribs, newVolume );
  735. }
  736. /******************************************************************/
  737. /* */
  738. /* */
  739. /******************************************************************/
  740. void AudioDeviceAdjustVolume ( AudioDevice *dev, int newVolume )
  741. {
  742. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  743. DBG_ASSERT_TYPE ( dev, AudioDevice );
  744. AudioAttribsAdjustVolume ( &dev->Attribs, newVolume );
  745. }
  746. /******************************************************************/
  747. /* */
  748. /* */
  749. /******************************************************************/
  750. int AudioDeviceMaxChannels ( AudioDevice *dev )
  751. {
  752. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  753. DBG_ASSERT_TYPE ( dev, AudioDevice );
  754. return dev->maxChannels;
  755. }
  756. /******************************************************************/
  757. /* */
  758. /* */
  759. /******************************************************************/
  760. int AudioDeviceChannels ( AudioDevice *dev )
  761. {
  762. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  763. DBG_ASSERT_TYPE ( dev, AudioDevice );
  764. return dev->channels;
  765. }
  766. /******************************************************************/
  767. /* */
  768. /* */
  769. /******************************************************************/
  770. AudioChannel* AudioDeviceCreateChannel ( AudioDevice *dev )
  771. {
  772. AudioChannel *channel = NULL;
  773. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  774. DBG_ASSERT_TYPE ( dev, AudioDevice );
  775. if ( dev->channels >= dev->maxChannels )
  776. {
  777. DBGPRINTF (("Maximum of %d channels already open\n", dev->maxChannels));
  778. goto error;
  779. }
  780. if ( !( channel = audioChannelCreate ( dev )) )
  781. {
  782. goto error;
  783. }
  784. audioAddChannel ( dev, channel );
  785. return channel;
  786. error:
  787. if ( channel )
  788. {
  789. audioChannelDestroy ( channel );
  790. }
  791. return NULL;
  792. }
  793. /******************************************************************/
  794. /* */
  795. /* */
  796. /******************************************************************/
  797. int AudioDeviceCreateChannels ( AudioDevice *dev, int num )
  798. {
  799. int count = 0;
  800. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  801. DBG_ASSERT_TYPE ( dev, AudioDevice );
  802. DBG_ASSERT ( num != 0);
  803. do
  804. {
  805. if ( !AudioDeviceCreateChannel ( dev ))
  806. {
  807. break;
  808. }
  809. count ++;
  810. } while (--num);
  811. return count;
  812. }
  813. /******************************************************************/
  814. /* */
  815. /* */
  816. /******************************************************************/
  817. AudioChannel* AudioDeviceFirstChannel ( AudioDevice *dev, AudioSearch *sh )
  818. {
  819. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  820. DBG_ASSERT_TYPE ( dev, AudioDevice );
  821. DBG_ASSERT ( sh != NULL );
  822. DBG_SET_TYPE ( sh, AudioSearchStruct );
  823. sh->item = ListFirstItem ( &dev->channelList );
  824. if ( sh->item )
  825. {
  826. DBG_ASSERT_TYPE ( (AudioChannel *) sh->item, AudioChannel );
  827. }
  828. return (AudioChannel *) sh->item;
  829. }
  830. /******************************************************************/
  831. /* */
  832. /* */
  833. /******************************************************************/
  834. AudioChannel* AudioDeviceNextChannel ( AudioSearch *sh )
  835. {
  836. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  837. DBG_ASSERT_TYPE ( sh, AudioSearchStruct );
  838. if ( sh->item )
  839. {
  840. sh->item = ListNextItem ( (ListNode *) sh->item );
  841. }
  842. if ( sh->item )
  843. {
  844. DBG_ASSERT_TYPE ( (AudioChannel *) sh->item, AudioChannel );
  845. }
  846. return (AudioChannel *) sh->item;
  847. }
  848. /******************************************************************/
  849. /* */
  850. /* */
  851. /******************************************************************/
  852. AudioChannel* AudioDeviceGetChannel ( AudioDevice *dev, AudioChannelType type )
  853. {
  854. AudioChannel *channel;
  855. AudioSearch sh;
  856. int lowestPriorityPlaying,
  857. lowestPriorityPaused;
  858. AudioChannel *lowestPlaying,
  859. *lowestPaused;
  860. uint lowestPlayingVolume;
  861. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  862. DBG_ASSERT_TYPE ( dev, AudioDevice );
  863. if ( type == AUDIO_CHANNEL_TYPE_ANY)
  864. {
  865. // you must know what type of channel you want so AUDIO_CHANNEL_ANY is taken
  866. // to mean a standard channel
  867. type = AUDIO_CHANNEL_TYPE_STD;
  868. }
  869. // search through the channel list.
  870. // First try to get a non active channel
  871. // Then settle on getting the lowest priority paused channel.
  872. // Then settle on getting the lowest priority playing channel.
  873. // ...and among the channels with lowest priority, get the one
  874. // with lowest volume.
  875. lowestPriorityPlaying = AUD_HIGHEST_PRIORITY;
  876. lowestPriorityPaused = AUD_HIGHEST_PRIORITY;
  877. lowestPlaying = NULL;
  878. lowestPaused = NULL;
  879. lowestPlayingVolume = 0xFFFFFFFF;
  880. channel = AudioDeviceFirstChannel ( dev, &sh );
  881. while ( channel )
  882. {
  883. if ( channel->Type == type )
  884. {
  885. if ( !(FLAGS_CHECK (channel->Control.Status, mAUDIO_CTRL_ACTIVE | mAUDIO_CTRL_INUSE )))
  886. {
  887. break; // we found a free channel
  888. }
  889. if ( (FLAGS_CHECK (channel->Control.Status, mAUDIO_CTRL_PLAYING )))
  890. {
  891. if ( channel->Control.Priority < lowestPriorityPlaying )
  892. {
  893. lowestPriorityPlaying = channel->Control.Priority;
  894. lowestPlaying = channel;
  895. lowestPlayingVolume = channel->attribs.VolumeLevel.level;
  896. }
  897. else if ( channel->Control.Priority == lowestPriorityPlaying )
  898. {
  899. // If priorities are equal, choose the channel whose volume
  900. // is lowest -- but only award lowest status if this one is
  901. // quieter than the previous best by a significant amount.
  902. if ( channel->attribs.VolumeLevel.level < lowestPlayingVolume
  903. && ( lowestPlayingVolume - channel->attribs.VolumeLevel.level >= VOLUME_SLACK ) )
  904. {
  905. lowestPriorityPlaying = channel->Control.Priority;
  906. lowestPlaying = channel;
  907. lowestPlayingVolume = channel->attribs.VolumeLevel.level;
  908. }
  909. }
  910. }
  911. else
  912. {
  913. if ( channel->Control.Priority < lowestPriorityPaused )
  914. {
  915. lowestPriorityPaused = channel->Control.Priority;
  916. lowestPaused = channel;
  917. }
  918. }
  919. }
  920. channel = AudioDeviceNextChannel ( &sh );
  921. }
  922. if ( !channel ) // we did not find a free channel
  923. {
  924. if ( !( channel = lowestPaused) || (lowestPriorityPaused > lowestPriorityPlaying) )
  925. {
  926. // we did not find a paused channel of lower priority
  927. channel = lowestPlaying;
  928. }
  929. }
  930. return channel;
  931. }
  932. /******************************************************************/
  933. /* */
  934. /* */
  935. /******************************************************************/
  936. AudioChannel* AudioDeviceReserveChannel ( AudioDevice *dev, AudioChannelType newType )
  937. {
  938. AudioChannel *chan;
  939. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  940. DBG_ASSERT_TYPE ( dev, AudioDevice );
  941. DBG_ASSERT ( newType != AUDIO_CHANNEL_TYPE_ALL ); // this does not make sense, think about it
  942. DBG_ASSERT ( newType != AUDIO_CHANNEL_TYPE_STD ); // illegal value
  943. chan = AudioDeviceGetChannel ( dev, AUDIO_CHANNEL_TYPE_STD );
  944. if ( chan )
  945. {
  946. AudioChannelReserve ( chan, newType );
  947. }
  948. return chan;
  949. }
  950. /******************************************************************/
  951. /* */
  952. /* */
  953. /******************************************************************/
  954. int AudioDeviceDestroyAllChannels ( AudioDevice *dev, AudioChannelType type )
  955. {
  956. AudioChannel *channel,*next;
  957. AudioSearch sh;
  958. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  959. DBG_ASSERT_TYPE ( dev, AudioDevice );
  960. channel = AudioDeviceFirstChannel ( dev, &sh );
  961. while (channel)
  962. {
  963. next = AudioDeviceNextChannel( &sh );
  964. if ( type == AUDIO_CHANNEL_TYPE_ALL || channel->Type == type )
  965. {
  966. AudioChannelDestroy ( channel );
  967. }
  968. channel = next;
  969. }
  970. return vNO_ERROR;
  971. }
  972. /******************************************************************/
  973. /* */
  974. /* */
  975. /******************************************************************/
  976. void AudioDeviceStopAllChannels ( AudioDevice *dev, AudioChannelType type)
  977. {
  978. AudioChannel *channel,*next;
  979. AudioSearch sh;
  980. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  981. DBG_ASSERT_TYPE ( dev, AudioDevice );
  982. channel = AudioDeviceFirstChannel ( dev, &sh );
  983. while (channel)
  984. {
  985. next = AudioDeviceNextChannel( &sh );
  986. if ( type == AUDIO_CHANNEL_TYPE_ALL || channel->Type == type )
  987. {
  988. AudioChannelStop ( channel );
  989. }
  990. channel = next;
  991. }
  992. }
  993. /******************************************************************/
  994. /* */
  995. /* */
  996. /******************************************************************/
  997. void AudioDevicePauseAllChannels ( AudioDevice *dev, AudioChannelType type )
  998. {
  999. AudioChannel *channel;
  1000. AudioSearch sh;
  1001. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1002. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1003. channel = AudioDeviceFirstChannel ( dev, &sh );
  1004. while (channel)
  1005. {
  1006. if ( FLAGS_CHECK ( channel->Control.Status, mAUDIO_CTRL_PLAYING ) )
  1007. {
  1008. if ( type == AUDIO_CHANNEL_TYPE_ALL || channel->Type == type )
  1009. {
  1010. AudioChannelPause ( channel );
  1011. }
  1012. }
  1013. channel = AudioDeviceNextChannel( &sh );
  1014. }
  1015. }
  1016. /******************************************************************/
  1017. /* */
  1018. /* */
  1019. /******************************************************************/
  1020. void AudioDeviceResumeAllChannels ( AudioDevice *dev, AudioChannelType type )
  1021. {
  1022. AudioChannel *channel;
  1023. AudioSearch sh;
  1024. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1025. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1026. channel = AudioDeviceFirstChannel ( dev, &sh );
  1027. while (channel)
  1028. {
  1029. if ( FLAGS_CHECK ( channel->Control.Status, mAUDIO_CTRL_PAUSED ) )
  1030. {
  1031. if ( type == AUDIO_CHANNEL_TYPE_ALL || channel->Type == type )
  1032. {
  1033. AudioChannelResume ( channel );
  1034. }
  1035. }
  1036. channel = AudioDeviceNextChannel( &sh );
  1037. }
  1038. }
  1039. /******************************************************************/
  1040. /* */
  1041. /* */
  1042. /******************************************************************/
  1043. int AudioDeviceService ( AudioDevice *dev)
  1044. {
  1045. int result;
  1046. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1047. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1048. result = dev->deviceHandler ( dev );
  1049. return result;
  1050. }
  1051. void AudioDeviceAttribsAdd ( AudioDevice *dev, AudioAttribs *attribs )
  1052. {
  1053. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1054. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1055. if ( !attribs )
  1056. {
  1057. return;
  1058. }
  1059. if ( audioDeviceAttribsListFind ( dev, attribs ))
  1060. {
  1061. return;
  1062. }
  1063. AudioAttribsNode *node;
  1064. ALLOC_STRUCT ( node, AudioAttribsNode );
  1065. DBG_SET_TYPE ( node, AudioAttribsNode );
  1066. ListNodeInit ( &node->nd );
  1067. node->attribs = attribs;
  1068. LockAcquire ( &dev->channelAccess );
  1069. ListAddToTail ( &dev->attribsList, &node->nd );
  1070. LockRelease ( &dev->channelAccess );
  1071. }
  1072. void AudioDeviceAttribsRemove ( AudioDevice *dev, AudioAttribs *attribs )
  1073. {
  1074. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1075. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1076. AudioAttribsNode *node = audioDeviceAttribsListFind ( dev, attribs );
  1077. if ( node )
  1078. {
  1079. DBG_ASSERT_TYPE ( node, AudioAttribsNode );
  1080. LockAcquire ( &dev->channelAccess );
  1081. ListNodeRemove ( &node->nd );
  1082. LockRelease ( &dev->channelAccess );
  1083. DBG_INVALIDATE_TYPE ( node );
  1084. AudioMemFree ( node );
  1085. }
  1086. }
  1087. void AudioDeviceAttribsRemoveAll ( AudioDevice *dev )
  1088. {
  1089. DBG_ASSERT ( audioInitialized ); // AudioSetUp() was not called
  1090. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1091. AudioAttribsNode *node;
  1092. while ( (node = (AudioAttribsNode *) ListFirstItem ( &dev->attribsList )) )
  1093. {
  1094. DBG_ASSERT_TYPE ( node, AudioAttribsNode );
  1095. ListNodeRemove ( &node->nd );
  1096. DBG_INVALIDATE_TYPE ( node );
  1097. AudioMemFree ( node );
  1098. }
  1099. }
  1100. /******************************************************************/
  1101. /* */
  1102. /* */
  1103. /******************************************************************/
  1104. void AudioServiceInfoInit ( AudioServiceInfo *asi )
  1105. {
  1106. DBG_ASSERT_PTR ( asi );
  1107. DBG_SET_TYPE ( asi, AudioServiceInfoStruct );
  1108. asi->serviceInterval = 0;
  1109. asi->mustServiceInterval = 0;
  1110. asi->lastInterval = 0;
  1111. asi->longestInterval = 0;
  1112. asi->lastServiceTime = 0;
  1113. asi->count = 0;
  1114. asi->lastcount = 0;
  1115. asi->periodInterval = SECONDS(5);
  1116. asi->longestIntervalforPeriod = 0;
  1117. asi->periodStart = 0;
  1118. asi->animPos = 0;
  1119. asi->missCount = 0;
  1120. }
  1121. /******************************************************************/
  1122. /* */
  1123. /* */
  1124. /******************************************************************/
  1125. void AudioServiceSetInterval ( AudioServiceInfo *asi, TimeStamp interval )
  1126. {
  1127. DBG_ASSERT_TYPE ( asi, AudioServiceInfoStruct );
  1128. asi->serviceInterval = interval;
  1129. asi->mustServiceInterval = interval;
  1130. asi->longestReset = asi->serviceInterval*10;
  1131. }
  1132. /******************************************************************/
  1133. /* */
  1134. /* */
  1135. /******************************************************************/
  1136. void AudioServiceSetMustServiceInterval ( AudioServiceInfo *asi, TimeStamp interval )
  1137. {
  1138. DBG_ASSERT_TYPE ( asi, AudioServiceInfoStruct );
  1139. asi->mustServiceInterval = interval;
  1140. }
  1141. /******************************************************************/
  1142. /* */
  1143. /* */
  1144. /******************************************************************/
  1145. void AudioServiceSetResetInterval ( AudioServiceInfo *asi, TimeStamp interval )
  1146. {
  1147. DBG_ASSERT_TYPE ( asi, AudioServiceInfoStruct );
  1148. asi->longestReset = interval;
  1149. }
  1150. /******************************************************************/
  1151. /* */
  1152. /* */
  1153. /******************************************************************/
  1154. int AudioServiceNeeded ( AudioServiceInfo *asi, TimeStamp now )
  1155. {
  1156. TimeStamp interval;
  1157. DBG_ASSERT_TYPE ( asi, AudioServiceInfoStruct );
  1158. interval = now - asi->lastServiceTime;
  1159. if ( interval < asi->serviceInterval )
  1160. {
  1161. return FALSE;
  1162. }
  1163. return TRUE;
  1164. }
  1165. /******************************************************************/
  1166. /* */
  1167. /* */
  1168. /******************************************************************/
  1169. void AudioServicePerform ( AudioServiceInfo *asi, TimeStamp now )
  1170. {
  1171. TimeStamp interval;
  1172. DBG_ASSERT_TYPE ( asi, AudioServiceInfoStruct );
  1173. interval = now - asi->lastServiceTime;
  1174. asi->lastInterval = interval;
  1175. asi->count++;
  1176. if ( asi->longestInterval > asi->longestReset )
  1177. {
  1178. asi->longestInterval = 0; // reset the longest count if it is extremely long
  1179. }
  1180. if ( interval > asi->longestInterval )
  1181. {
  1182. asi->longestInterval = interval;
  1183. }
  1184. if ( interval > asi->mustServiceInterval )
  1185. {
  1186. asi->missCount++;
  1187. }
  1188. if ( interval > asi->longestIntervalforPeriod )
  1189. {
  1190. asi->longestIntervalforPeriod = interval;
  1191. }
  1192. asi->lastServiceTime = now;
  1193. }
  1194. /******************************************************************/
  1195. /* */
  1196. /* */
  1197. /******************************************************************/
  1198. void AudioServiceInfoDump ( AudioServiceInfo *asi, char *string )
  1199. {
  1200. static char *anim = "|/-\\";
  1201. TimeStamp now = AudioGetTime();
  1202. if ( asi->count != asi->lastcount )
  1203. {
  1204. asi->animPos++;
  1205. if ( asi->animPos >= strlen (anim ))
  1206. {
  1207. asi->animPos = 0;
  1208. }
  1209. asi->lastcount = asi->count;
  1210. }
  1211. if ( asi->periodStart + asi->periodInterval < now )
  1212. {
  1213. asi->longestIntervalforPeriod = asi->serviceInterval;
  1214. asi->periodStart = now;
  1215. }
  1216. sprintf ( string, "%05dms (%04d,%04d,%04d,~%2d) %c \n",
  1217. (int) asi->serviceInterval,
  1218. (int) asi->lastInterval,
  1219. (int) asi->longestIntervalforPeriod,
  1220. (int) asi->longestInterval,
  1221. (int) asi->missCount,
  1222. anim[asi->animPos] );
  1223. }
  1224. /******************************************************************/
  1225. /* */
  1226. /* */
  1227. /******************************************************************/
  1228. void AudioDeviceDump ( AudioDevice *dev, void (*print) ( char *), int names )
  1229. {
  1230. char string[200];
  1231. char temp[100];
  1232. AudioChannel *chan;
  1233. AudioSearch sh;
  1234. char *state;
  1235. char *type;
  1236. int count;
  1237. DBG_ASSERT_TYPE ( dev, AudioDevice );
  1238. print ( "Audio Device Dump ---------------------\n");
  1239. sprintf ( string, "%s (%d)\n",
  1240. dev->system->master->Name,
  1241. dev->Unit);
  1242. print ( string );
  1243. AudioServiceInfoDump ( &dev->mixerUpdate, temp );
  1244. sprintf ( string, "mixer:%s", temp );
  1245. print ( string );
  1246. AudioServiceInfoDump ( &dev->attribsUpdate, temp );
  1247. sprintf ( string, "level:%s", temp );
  1248. print ( string );
  1249. sprintf (string, "Frames: %d Oversamp: %d Lag: %d\n", dev->frames, dev->over_sample, dev->frame_lag );
  1250. print ( string );
  1251. if ( names )
  1252. {
  1253. sprintf ( string, "Chan:TYPE: STATE : FORMAT :PRI :VOL: NAME\n");
  1254. }
  1255. else
  1256. {
  1257. sprintf ( string, "Chan:TYPE: STATE : FORMAT :PRI :VOL:\n");
  1258. }
  1259. print ( string );
  1260. chan = AudioDeviceFirstChannel ( dev, &sh );
  1261. count = 0;
  1262. while (chan)
  1263. {
  1264. if ( FLAGS_CHECK ( chan->Control.Status, mAUDIO_CTRL_PLAYING) )
  1265. {
  1266. state = "PLAYING";
  1267. }
  1268. else if ( FLAGS_CHECK ( chan->Control.Status, mAUDIO_CTRL_PAUSED))
  1269. {
  1270. state = "PAUSED ";
  1271. }
  1272. else
  1273. {
  1274. state = "FREE ";
  1275. }
  1276. if ( chan->Type >= AUDIO_CHANNEL_TYPE_USER )
  1277. {
  1278. type = "USER";
  1279. }
  1280. else if ( chan->Type == AUDIO_CHANNEL_TYPE_STD )
  1281. {
  1282. type = "STD ";
  1283. }
  1284. else
  1285. {
  1286. type = "??? ";
  1287. }
  1288. AudioFormat *format;
  1289. char fstr[200];
  1290. char name[200];
  1291. name[0] = 0;
  1292. if ( names )
  1293. {
  1294. #ifndef IG_FINAL_RELEASE
  1295. strcpy ( name, chan->sample_name );
  1296. #endif
  1297. }
  1298. format = &chan->current_format;
  1299. char temp[3];
  1300. if ( format->Compression == AUDIO_COMPRESS_MP3 )
  1301. {
  1302. strcpy ( temp, "mp" );
  1303. }
  1304. else
  1305. {
  1306. sprintf ( temp, "%2d", format->Compression == AUDIO_COMPRESS_NONE ? format->SampleWidth *8 : 4 );
  1307. }
  1308. sprintf ( fstr, "%02d-%s-%c%c%c",
  1309. format->Rate / 1000,
  1310. temp,
  1311. format->Channels == 1 ? 'm' : 's',
  1312. chan->format_changed ? '*' : ' ',
  1313. chan->drv_format_changed ? '+' : ' ' );
  1314. //sprintf ( string, "Chan:TYPE: STATE : FORMAT :PRI :VOL:\n");
  1315. sprintf ( string, "#%2d :%s:%s:%s:%4d:%3d:%.31s\n",
  1316. count,
  1317. type,
  1318. state,
  1319. fstr,
  1320. chan->Control.Priority,
  1321. AudioLevelApply ( &chan->attribs.VolumeLevel, 100 ),
  1322. name);
  1323. print ( string );
  1324. count++;
  1325. chan = AudioDeviceNextChannel( &sh );
  1326. }
  1327. print ( "--------------------------------------\n");
  1328. }