gamesound.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //---------------------------------------------------------------------------
  2. // GameSound.cpp - This file is the sound system code for the GAME
  3. //
  4. // MechCommander 2
  5. //
  6. //---------------------------------------------------------------------------//
  7. // Copyright (C) Microsoft Corporation. All rights reserved. //
  8. //===========================================================================//
  9. //---------------------------------------------------------------------------
  10. // Include files
  11. #ifndef GAMESOUND_H
  12. #include "gamesound.h"
  13. #endif
  14. #ifndef WARRIOR_H
  15. #include "warrior.h"
  16. #endif
  17. #ifndef VIEWER
  18. #ifndef CONTROLGUI_H
  19. #include "ControlGui.h"
  20. #endif
  21. #endif
  22. //---------------------------------------------------------------------------
  23. GameSoundSystem *soundSystem = NULL;
  24. extern bool wave_ParseWaveMemory(MemoryPtr lpChunkOfMemory, MC2_WAVEFORMATEX** lplpWaveHeader, MemoryPtr* lplpWaveSamples,DWORD *lpcbWaveSize);
  25. bool friendlyDestroyed = false;
  26. bool enemyDestroyed = false;
  27. extern bool GeneralAlarm;
  28. #define ALARM_TIME 5.0f
  29. //---------------------------------------------------------------------------
  30. void GameSoundSystem::purgeSoundSystem (void)
  31. {
  32. wholeMsgDone = true;
  33. currentMessage = NULL;
  34. messagesInQueue = 0;
  35. currentFragment = 0;
  36. playingNoise = false;
  37. radioHandle = NULL;
  38. //------------------------------------------------------------
  39. // dump the Radio Message Queue.
  40. messagesInQueue = 0;
  41. wholeMsgDone = true;
  42. for (long i=0;i<MAX_QUEUED_MESSAGES;i++)
  43. queue[i] = NULL;
  44. generalAlarmTimer = 0.0f;
  45. //---------------------------------------------------------
  46. // Toss everybody's radio. This MUST happen AFTER
  47. // all of the currently queued radio messages are dumped.
  48. // Radios all come from the Radio Heap now. This dumps 'em.
  49. for (i=0;i<MAX_RADIOS;i++)
  50. {
  51. Radio::radioList[i] = NULL;
  52. delete Radio::messagesFile[i];
  53. Radio::messagesFile[i] = NULL;
  54. }
  55. delete Radio::radioHeap;
  56. Radio::radioHeap = NULL;
  57. delete Radio::noiseFile;
  58. Radio::noiseFile = NULL;
  59. Radio::currentRadio = 0;
  60. Radio::radioListGo = FALSE;
  61. Radio::messageInfoLoaded = FALSE;
  62. isRaining = oldRaining = false;
  63. largestSensorContact = -1;
  64. //-----------------------------------------------
  65. // Check all samples to see if one should end.
  66. for (i=0;i<MAX_DIGITAL_SAMPLES;i++)
  67. {
  68. if (i != BETTY_CHANNEL)
  69. {
  70. gosAudio_SetChannelPlayMode(i, gosAudio_Stop);
  71. fadeDown[i] = FALSE;
  72. channelSampleId[i] = -1;
  73. channelInUse[i] = FALSE;
  74. }
  75. }
  76. }
  77. //---------------------------------------------------------------------------
  78. void GameSoundSystem::removeQueuedMessage (long msgNumber)
  79. {
  80. long i;
  81. if (msgNumber < 0 || msgNumber >= MAX_QUEUED_MESSAGES)
  82. return;
  83. if (queue[msgNumber])
  84. {
  85. if (queue[msgNumber]->pilot)
  86. queue[msgNumber]->pilot->clearMessagePlaying();
  87. UserHeapPtr msgHeap = queue[msgNumber]->msgHeap;
  88. for (i=0;i<MAX_FRAGMENTS;i++)
  89. {
  90. msgHeap->Free(queue[msgNumber]->data[i]);
  91. queue[msgNumber]->data[i] = NULL;
  92. msgHeap->Free(queue[msgNumber]->noise[i]);
  93. queue[msgNumber]->noise[i] = NULL;
  94. }
  95. msgHeap->Free(queue[msgNumber]);
  96. if (messagesInQueue > 0)
  97. messagesInQueue--;
  98. #ifdef _DEBUG
  99. for (long test=0;test<(long)messagesInQueue;test++)
  100. {
  101. if (queue[test])
  102. continue;
  103. else
  104. Fatal(-1," Bad Message in Queue -- RmoveQMsg");
  105. }
  106. #endif
  107. for (i=msgNumber;i<MAX_QUEUED_MESSAGES-1;i++)
  108. queue[i] = queue[i+1];
  109. queue[MAX_QUEUED_MESSAGES-1] = NULL;
  110. #ifdef _DEBUG
  111. for (test=0;test<(long)messagesInQueue;test++)
  112. {
  113. if (queue[test])
  114. continue;
  115. else
  116. Fatal(-1," Bad Message in Queue -- RmoveQMsg");
  117. }
  118. #endif
  119. }
  120. }
  121. //---------------------------------------------------------------------------
  122. bool GameSoundSystem::checkMessage (MechWarriorPtr pilot, byte priority, unsigned long messageType)
  123. {
  124. for (long i=0;i<MAX_QUEUED_MESSAGES;i++)
  125. {
  126. if (queue[i])
  127. if ((queue[i]->pilot == pilot && priority > queue[i]->priority) || // I'm already saying something more important, or
  128. (queue[i]->priority > 1 && queue[i]->msgType == messageType)) // someone else is saying this
  129. return FALSE;
  130. }
  131. return TRUE;
  132. }
  133. //---------------------------------------------------------------------------
  134. void GameSoundSystem::moveFromQueueToPlaying(void)
  135. {
  136. removeCurrentMessage();
  137. while (queue[0] &&
  138. queue[0]->pilot &&
  139. !(queue[0]->pilot->active()) &&
  140. (queue[0]->msgType != RADIO_DEATH) &&
  141. (queue[0]->msgType != RADIO_EJECTING))
  142. {
  143. removeQueuedMessage(0);
  144. }
  145. currentMessage = queue[0];
  146. for (long i=0;i<MAX_QUEUED_MESSAGES-1;i++)
  147. queue[i] = queue[i+1];
  148. queue[MAX_QUEUED_MESSAGES-1] = NULL;
  149. if (messagesInQueue > 0)
  150. messagesInQueue--;
  151. }
  152. //---------------------------------------------------------------------------
  153. void GameSoundSystem::removeCurrentMessage(void)
  154. {
  155. if (currentMessage)
  156. {
  157. if (currentMessage->pilot)
  158. currentMessage->pilot->clearMessagePlaying();
  159. UserHeapPtr msgHeap = currentMessage->msgHeap;
  160. for (long j=0;j<MAX_FRAGMENTS;j++)
  161. {
  162. msgHeap->Free(currentMessage->data[j]);
  163. currentMessage->data[j] = NULL;
  164. msgHeap->Free(currentMessage->noise[j]);
  165. currentMessage->noise[j] = NULL;
  166. }
  167. msgHeap->Free(currentMessage);
  168. currentMessage = NULL;
  169. }
  170. gosAudio_SetChannelPlayMode(RADIO_CHANNEL, gosAudio_Stop);
  171. wholeMsgDone = TRUE;
  172. }
  173. //---------------------------------------------------------------------------
  174. long GameSoundSystem::queueRadioMessage (RadioData *msgData)
  175. {
  176. long i;
  177. //-------------------------------------------------
  178. // First, search the Queue and see if this message
  179. // was already sent this turn.
  180. if (msgData->msgId >= MSG_TOTAL_MSGS) // is this a real message? (why are we asking this???)
  181. {
  182. for (i=MAX_QUEUED_MESSAGES-1;i>=0;i--)
  183. {
  184. if (queue[i])
  185. {
  186. if ((msgData->turnQueued == queue[i]->turnQueued) &&
  187. (msgData->msgId == queue[i]->msgId))
  188. {
  189. removeQueuedMessage(i);
  190. }
  191. }
  192. }
  193. }
  194. if (msgData->priority == 0) // top cancels playing message! top also means pilot's gone, so
  195. { // remove other messages from that pilot.
  196. removeCurrentMessage();
  197. for (i=MAX_QUEUED_MESSAGES-1;i>=0;i--)
  198. {
  199. if (queue[i] && queue[i]->pilot == msgData->pilot)
  200. {
  201. removeQueuedMessage(i);
  202. }
  203. }
  204. }
  205. for (i=0; i<MAX_QUEUED_MESSAGES;i++)
  206. {
  207. if (!queue[i])
  208. break;
  209. if (queue[i]->priority > msgData->priority) // if this messages priority higher (a lower number: 1 is top priority)
  210. { // push things down to make room.
  211. for (long j=MAX_QUEUED_MESSAGES-1;j>i;j--)
  212. {
  213. queue[j] = queue[j-1];
  214. }
  215. break;
  216. }
  217. }
  218. if (i == MAX_QUEUED_MESSAGES)
  219. return(-1);
  220. if (queue[MAX_QUEUED_MESSAGES-1])
  221. removeQueuedMessage(MAX_QUEUED_MESSAGES-1);
  222. queue[i] = msgData;
  223. #ifdef _DEBUG
  224. for (long test=0;test<(long)messagesInQueue;test++)
  225. {
  226. if (queue[test])
  227. continue;
  228. else
  229. Fatal(-1," Bad Message in Queue -- RmoveQMsg");
  230. }
  231. #endif
  232. messagesInQueue++;
  233. #ifdef _DEBUG
  234. for (test=0;test<(long)messagesInQueue;test++)
  235. {
  236. if (queue[test])
  237. continue;
  238. else
  239. Fatal(-1," Bad Message in Queue -- RmoveQMsg");
  240. }
  241. #endif
  242. return NO_ERR;
  243. }
  244. //---------------------------------------------------------------------------
  245. void GameSoundSystem::update (void)
  246. {
  247. //---------------------------------------------
  248. // Dynamic Music Code goes here!!!
  249. // New System(tm)
  250. //
  251. SoundSystem::update();
  252. if (GeneralAlarm && (generalAlarmTimer < ALARM_TIME))
  253. {
  254. playDigitalSample(BUZZER1);
  255. generalAlarmTimer += frameLength;
  256. }
  257. if (useSound && currentMessage && (gosAudio_GetChannelPlayMode(RADIO_CHANNEL) != gosAudio_PlayOnce))
  258. {
  259. if (radioHandle)
  260. {
  261. gosAudio_DestroyResource(&radioHandle); //Toss the current radio data which just completed!
  262. radioHandle = NULL;
  263. }
  264. if (!wholeMsgDone)
  265. {
  266. //---------------------------------------------------------------
  267. // We are finished with a fragment, move on.
  268. if (playingNoise)
  269. {
  270. playingNoise = FALSE;
  271. if (currentMessage->data[currentFragment])
  272. {
  273. //--------------------------------------------------------------------
  274. // Hand GOS sound the data it needs to create the resource Handle
  275. gosAudio_Format soundFormat;
  276. soundFormat.wFormatTag = 1; //PCM
  277. MC2_WAVEFORMATEX *waveFormat = NULL;
  278. MemoryPtr dataOffset = NULL;
  279. DWORD length = 0;
  280. DWORD bitsPerSec = 0;
  281. wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
  282. bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
  283. soundFormat.nChannels = waveFormat->nChannels;
  284. soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
  285. soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
  286. soundFormat.nBlockAlign = waveFormat->nBlockAlign;
  287. soundFormat.wBitsPerSample = bitsPerSec;
  288. soundFormat.cbSize = 0;
  289. gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
  290. if (isChannelPlaying(BETTY_CHANNEL))
  291. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
  292. else
  293. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
  294. gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
  295. gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
  296. if (currentMessage->pilot)
  297. currentMessage->pilot->setMessagePlaying();
  298. }
  299. else
  300. wholeMsgDone = TRUE;
  301. currentFragment++;
  302. }
  303. else
  304. {
  305. if (currentMessage->noise[currentFragment])
  306. {
  307. //--------------------------------------------------------------------
  308. // Hand GOS sound the data it needs to create the resource Handle
  309. gosAudio_Format soundFormat;
  310. soundFormat.wFormatTag = 1; //PCM
  311. MC2_WAVEFORMATEX *waveFormat = NULL;
  312. MemoryPtr dataOffset = NULL;
  313. DWORD length = 0;
  314. DWORD bitsPerSec = 0;
  315. wave_ParseWaveMemory(currentMessage->noise[currentFragment],&waveFormat,&dataOffset,&length);
  316. bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
  317. soundFormat.nChannels = waveFormat->nChannels;
  318. soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
  319. soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
  320. soundFormat.nBlockAlign = waveFormat->nBlockAlign;
  321. soundFormat.wBitsPerSample = bitsPerSec;
  322. soundFormat.cbSize = 0;
  323. gosAudio_CreateResource(&radioHandle, gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
  324. if (isChannelPlaying(BETTY_CHANNEL))
  325. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
  326. else
  327. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
  328. gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
  329. gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
  330. playingNoise = TRUE;
  331. if (currentMessage->pilot)
  332. currentMessage->pilot->setMessagePlaying();
  333. }
  334. else
  335. {
  336. playingNoise = FALSE;
  337. if (currentMessage->data[currentFragment])
  338. {
  339. //--------------------------------------------------------------------
  340. // Hand GOS sound the data it needs to create the resource Handle
  341. gosAudio_Format soundFormat;
  342. soundFormat.wFormatTag = 1; //PCM
  343. MC2_WAVEFORMATEX *waveFormat = NULL;
  344. MemoryPtr dataOffset = NULL;
  345. DWORD length = 0;
  346. DWORD bitsPerSec = 0;
  347. wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
  348. bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
  349. soundFormat.nChannels = waveFormat->nChannels;
  350. soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
  351. soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
  352. soundFormat.nBlockAlign = waveFormat->nBlockAlign;
  353. soundFormat.wBitsPerSample = bitsPerSec;
  354. soundFormat.cbSize = 0;
  355. gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
  356. if (isChannelPlaying(BETTY_CHANNEL))
  357. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
  358. else
  359. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
  360. gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
  361. gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
  362. if (currentMessage->pilot)
  363. currentMessage->pilot->setMessagePlaying();
  364. }
  365. else
  366. wholeMsgDone = TRUE;
  367. currentFragment++;
  368. }
  369. }
  370. }
  371. else // wholeMessageDone
  372. {
  373. if (currentMessage)
  374. {
  375. if (currentMessage->pilot)
  376. {
  377. currentMessage->pilot->clearMessagePlaying();
  378. #ifndef VIEWER
  379. ControlGui::instance->endPilotVideo();
  380. #endif
  381. }
  382. removeCurrentMessage();
  383. }
  384. }
  385. }
  386. if (useSound && messagesInQueue && wholeMsgDone)
  387. {
  388. if (radioHandle)
  389. {
  390. gosAudio_DestroyResource(&radioHandle); //Toss the current radio data which just completed!
  391. radioHandle = NULL;
  392. }
  393. //-----------------------------------------------------
  394. // We are done with the current sample, start the next
  395. currentFragment = 0;
  396. moveFromQueueToPlaying();
  397. if (!currentMessage && !messagesInQueue) //It is now possible for ALL remaining messages to go away because the pilot/mech died!
  398. return;
  399. #ifndef VIEWER
  400. if ( currentMessage->movieCode )
  401. {
  402. ControlGui::instance->playPilotVideo(
  403. currentMessage->pilot, currentMessage->movieCode );
  404. }
  405. #endif
  406. if (currentMessage->noise[currentFragment])
  407. {
  408. //--------------------------------------------------------------------
  409. // Hand GOS sound the data it needs to create the resource Handle
  410. gosAudio_Format soundFormat;
  411. soundFormat.wFormatTag = 1; //PCM
  412. MC2_WAVEFORMATEX *waveFormat = NULL;
  413. MemoryPtr dataOffset = NULL;
  414. DWORD length = 0;
  415. DWORD bitsPerSec = 0;
  416. wave_ParseWaveMemory(currentMessage->noise[currentFragment],&waveFormat,&dataOffset,&length);
  417. bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
  418. soundFormat.nChannels = waveFormat->nChannels;
  419. soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
  420. soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
  421. soundFormat.nBlockAlign = waveFormat->nBlockAlign;
  422. soundFormat.wBitsPerSample = bitsPerSec;
  423. soundFormat.cbSize = 0;
  424. gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
  425. if (isChannelPlaying(BETTY_CHANNEL))
  426. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
  427. else
  428. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
  429. gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
  430. gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
  431. playingNoise = TRUE;
  432. if (currentMessage->pilot)
  433. currentMessage->pilot->setMessagePlaying();
  434. }
  435. else
  436. {
  437. playingNoise = FALSE;
  438. if (currentMessage->data[currentFragment])
  439. {
  440. //--------------------------------------------------------------------
  441. // Hand GOS sound the data it needs to create the resource Handle
  442. gosAudio_Format soundFormat;
  443. soundFormat.wFormatTag = 1; //PCM
  444. MC2_WAVEFORMATEX *waveFormat = NULL;
  445. MemoryPtr dataOffset = NULL;
  446. DWORD length = 0;
  447. DWORD bitsPerSec = 0;
  448. wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
  449. bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
  450. soundFormat.nChannels = waveFormat->nChannels;
  451. soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
  452. soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
  453. soundFormat.nBlockAlign = waveFormat->nBlockAlign;
  454. soundFormat.wBitsPerSample = bitsPerSec;
  455. soundFormat.cbSize = 0;
  456. gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
  457. if (isChannelPlaying(BETTY_CHANNEL))
  458. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
  459. else
  460. gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
  461. gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
  462. gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
  463. if (currentMessage->pilot)
  464. currentMessage->pilot->setMessagePlaying();
  465. }
  466. }
  467. wholeMsgDone = FALSE;
  468. }
  469. }