123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- //---------------------------------------------------------------------------
- // GameSound.cpp - This file is the sound system code for the GAME
- //
- // MechCommander 2
- //
- //---------------------------------------------------------------------------//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- //---------------------------------------------------------------------------
- // Include files
- #ifndef GAMESOUND_H
- #include "gamesound.h"
- #endif
- #ifndef WARRIOR_H
- #include "warrior.h"
- #endif
- #ifndef VIEWER
- #ifndef CONTROLGUI_H
- #include "ControlGui.h"
- #endif
- #endif
- //---------------------------------------------------------------------------
- GameSoundSystem *soundSystem = NULL;
- extern bool wave_ParseWaveMemory(MemoryPtr lpChunkOfMemory, MC2_WAVEFORMATEX** lplpWaveHeader, MemoryPtr* lplpWaveSamples,DWORD *lpcbWaveSize);
- bool friendlyDestroyed = false;
- bool enemyDestroyed = false;
- extern bool GeneralAlarm;
- #define ALARM_TIME 5.0f
- //---------------------------------------------------------------------------
- void GameSoundSystem::purgeSoundSystem (void)
- {
- wholeMsgDone = true;
- currentMessage = NULL;
- messagesInQueue = 0;
- currentFragment = 0;
- playingNoise = false;
- radioHandle = NULL;
- //------------------------------------------------------------
- // dump the Radio Message Queue.
- messagesInQueue = 0;
- wholeMsgDone = true;
- for (long i=0;i<MAX_QUEUED_MESSAGES;i++)
- queue[i] = NULL;
- generalAlarmTimer = 0.0f;
-
- //---------------------------------------------------------
- // Toss everybody's radio. This MUST happen AFTER
- // all of the currently queued radio messages are dumped.
- // Radios all come from the Radio Heap now. This dumps 'em.
- for (i=0;i<MAX_RADIOS;i++)
- {
- Radio::radioList[i] = NULL;
- delete Radio::messagesFile[i];
- Radio::messagesFile[i] = NULL;
- }
- delete Radio::radioHeap;
- Radio::radioHeap = NULL;
-
- delete Radio::noiseFile;
- Radio::noiseFile = NULL;
- Radio::currentRadio = 0;
- Radio::radioListGo = FALSE;
- Radio::messageInfoLoaded = FALSE;
- isRaining = oldRaining = false;
- largestSensorContact = -1;
- //-----------------------------------------------
- // Check all samples to see if one should end.
- for (i=0;i<MAX_DIGITAL_SAMPLES;i++)
- {
- if (i != BETTY_CHANNEL)
- {
- gosAudio_SetChannelPlayMode(i, gosAudio_Stop);
- fadeDown[i] = FALSE;
- channelSampleId[i] = -1;
- channelInUse[i] = FALSE;
- }
- }
- }
- //---------------------------------------------------------------------------
- void GameSoundSystem::removeQueuedMessage (long msgNumber)
- {
- long i;
- if (msgNumber < 0 || msgNumber >= MAX_QUEUED_MESSAGES)
- return;
- if (queue[msgNumber])
- {
- if (queue[msgNumber]->pilot)
- queue[msgNumber]->pilot->clearMessagePlaying();
- UserHeapPtr msgHeap = queue[msgNumber]->msgHeap;
- for (i=0;i<MAX_FRAGMENTS;i++)
- {
- msgHeap->Free(queue[msgNumber]->data[i]);
- queue[msgNumber]->data[i] = NULL;
- msgHeap->Free(queue[msgNumber]->noise[i]);
- queue[msgNumber]->noise[i] = NULL;
- }
-
- msgHeap->Free(queue[msgNumber]);
- if (messagesInQueue > 0)
- messagesInQueue--;
- #ifdef _DEBUG
- for (long test=0;test<(long)messagesInQueue;test++)
- {
- if (queue[test])
- continue;
- else
- Fatal(-1," Bad Message in Queue -- RmoveQMsg");
- }
- #endif
- for (i=msgNumber;i<MAX_QUEUED_MESSAGES-1;i++)
- queue[i] = queue[i+1];
- queue[MAX_QUEUED_MESSAGES-1] = NULL;
- #ifdef _DEBUG
- for (test=0;test<(long)messagesInQueue;test++)
- {
- if (queue[test])
- continue;
- else
- Fatal(-1," Bad Message in Queue -- RmoveQMsg");
- }
- #endif
- }
- }
- //---------------------------------------------------------------------------
- bool GameSoundSystem::checkMessage (MechWarriorPtr pilot, byte priority, unsigned long messageType)
- {
- for (long i=0;i<MAX_QUEUED_MESSAGES;i++)
- {
- if (queue[i])
- if ((queue[i]->pilot == pilot && priority > queue[i]->priority) || // I'm already saying something more important, or
- (queue[i]->priority > 1 && queue[i]->msgType == messageType)) // someone else is saying this
- return FALSE;
- }
- return TRUE;
- }
- //---------------------------------------------------------------------------
- void GameSoundSystem::moveFromQueueToPlaying(void)
- {
- removeCurrentMessage();
- while (queue[0] &&
- queue[0]->pilot &&
- !(queue[0]->pilot->active()) &&
- (queue[0]->msgType != RADIO_DEATH) &&
- (queue[0]->msgType != RADIO_EJECTING))
- {
- removeQueuedMessage(0);
- }
- currentMessage = queue[0];
- for (long i=0;i<MAX_QUEUED_MESSAGES-1;i++)
- queue[i] = queue[i+1];
-
- queue[MAX_QUEUED_MESSAGES-1] = NULL;
- if (messagesInQueue > 0)
- messagesInQueue--;
- }
- //---------------------------------------------------------------------------
- void GameSoundSystem::removeCurrentMessage(void)
- {
- if (currentMessage)
- {
- if (currentMessage->pilot)
- currentMessage->pilot->clearMessagePlaying();
- UserHeapPtr msgHeap = currentMessage->msgHeap;
- for (long j=0;j<MAX_FRAGMENTS;j++)
- {
- msgHeap->Free(currentMessage->data[j]);
- currentMessage->data[j] = NULL;
- msgHeap->Free(currentMessage->noise[j]);
- currentMessage->noise[j] = NULL;
- }
-
- msgHeap->Free(currentMessage);
- currentMessage = NULL;
- }
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL, gosAudio_Stop);
- wholeMsgDone = TRUE;
- }
- //---------------------------------------------------------------------------
- long GameSoundSystem::queueRadioMessage (RadioData *msgData)
- {
- long i;
- //-------------------------------------------------
- // First, search the Queue and see if this message
- // was already sent this turn.
- if (msgData->msgId >= MSG_TOTAL_MSGS) // is this a real message? (why are we asking this???)
- {
- for (i=MAX_QUEUED_MESSAGES-1;i>=0;i--)
- {
- if (queue[i])
- {
- if ((msgData->turnQueued == queue[i]->turnQueued) &&
- (msgData->msgId == queue[i]->msgId))
- {
- removeQueuedMessage(i);
- }
- }
- }
- }
- if (msgData->priority == 0) // top cancels playing message! top also means pilot's gone, so
- { // remove other messages from that pilot.
- removeCurrentMessage();
- for (i=MAX_QUEUED_MESSAGES-1;i>=0;i--)
- {
- if (queue[i] && queue[i]->pilot == msgData->pilot)
- {
- removeQueuedMessage(i);
- }
- }
- }
- for (i=0; i<MAX_QUEUED_MESSAGES;i++)
- {
- if (!queue[i])
- break;
- if (queue[i]->priority > msgData->priority) // if this messages priority higher (a lower number: 1 is top priority)
- { // push things down to make room.
- for (long j=MAX_QUEUED_MESSAGES-1;j>i;j--)
- {
- queue[j] = queue[j-1];
- }
- break;
- }
- }
-
- if (i == MAX_QUEUED_MESSAGES)
- return(-1);
- if (queue[MAX_QUEUED_MESSAGES-1])
- removeQueuedMessage(MAX_QUEUED_MESSAGES-1);
- queue[i] = msgData;
- #ifdef _DEBUG
- for (long test=0;test<(long)messagesInQueue;test++)
- {
- if (queue[test])
- continue;
- else
- Fatal(-1," Bad Message in Queue -- RmoveQMsg");
- }
- #endif
- messagesInQueue++;
- #ifdef _DEBUG
- for (test=0;test<(long)messagesInQueue;test++)
- {
- if (queue[test])
- continue;
- else
- Fatal(-1," Bad Message in Queue -- RmoveQMsg");
- }
- #endif
- return NO_ERR;
- }
- //---------------------------------------------------------------------------
- void GameSoundSystem::update (void)
- {
- //---------------------------------------------
- // Dynamic Music Code goes here!!!
- // New System(tm)
- //
-
- SoundSystem::update();
- if (GeneralAlarm && (generalAlarmTimer < ALARM_TIME))
- {
- playDigitalSample(BUZZER1);
- generalAlarmTimer += frameLength;
- }
- if (useSound && currentMessage && (gosAudio_GetChannelPlayMode(RADIO_CHANNEL) != gosAudio_PlayOnce))
- {
- if (radioHandle)
- {
- gosAudio_DestroyResource(&radioHandle); //Toss the current radio data which just completed!
- radioHandle = NULL;
- }
- if (!wholeMsgDone)
- {
- //---------------------------------------------------------------
- // We are finished with a fragment, move on.
- if (playingNoise)
- {
- playingNoise = FALSE;
-
- if (currentMessage->data[currentFragment])
- {
- //--------------------------------------------------------------------
- // Hand GOS sound the data it needs to create the resource Handle
- gosAudio_Format soundFormat;
- soundFormat.wFormatTag = 1; //PCM
-
- MC2_WAVEFORMATEX *waveFormat = NULL;
- MemoryPtr dataOffset = NULL;
- DWORD length = 0;
- DWORD bitsPerSec = 0;
- wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
-
- bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
-
- soundFormat.nChannels = waveFormat->nChannels;
- soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
- soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
- soundFormat.nBlockAlign = waveFormat->nBlockAlign;
- soundFormat.wBitsPerSample = bitsPerSec;
- soundFormat.cbSize = 0;
- gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
-
- if (isChannelPlaying(BETTY_CHANNEL))
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
- else
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
- gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
-
- if (currentMessage->pilot)
- currentMessage->pilot->setMessagePlaying();
- }
- else
- wholeMsgDone = TRUE;
-
- currentFragment++;
- }
- else
- {
- if (currentMessage->noise[currentFragment])
- {
- //--------------------------------------------------------------------
- // Hand GOS sound the data it needs to create the resource Handle
- gosAudio_Format soundFormat;
- soundFormat.wFormatTag = 1; //PCM
-
- MC2_WAVEFORMATEX *waveFormat = NULL;
- MemoryPtr dataOffset = NULL;
- DWORD length = 0;
- DWORD bitsPerSec = 0;
- wave_ParseWaveMemory(currentMessage->noise[currentFragment],&waveFormat,&dataOffset,&length);
-
- bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
-
- soundFormat.nChannels = waveFormat->nChannels;
- soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
- soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
- soundFormat.nBlockAlign = waveFormat->nBlockAlign;
- soundFormat.wBitsPerSample = bitsPerSec;
- soundFormat.cbSize = 0;
- gosAudio_CreateResource(&radioHandle, gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
-
- if (isChannelPlaying(BETTY_CHANNEL))
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
- else
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
- gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
- playingNoise = TRUE;
-
- if (currentMessage->pilot)
- currentMessage->pilot->setMessagePlaying();
- }
- else
- {
- playingNoise = FALSE;
- if (currentMessage->data[currentFragment])
- {
- //--------------------------------------------------------------------
- // Hand GOS sound the data it needs to create the resource Handle
- gosAudio_Format soundFormat;
- soundFormat.wFormatTag = 1; //PCM
-
- MC2_WAVEFORMATEX *waveFormat = NULL;
- MemoryPtr dataOffset = NULL;
- DWORD length = 0;
- DWORD bitsPerSec = 0;
- wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
-
- bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
-
- soundFormat.nChannels = waveFormat->nChannels;
- soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
- soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
- soundFormat.nBlockAlign = waveFormat->nBlockAlign;
- soundFormat.wBitsPerSample = bitsPerSec;
- soundFormat.cbSize = 0;
- gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
-
- if (isChannelPlaying(BETTY_CHANNEL))
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
- else
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
- gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
-
- if (currentMessage->pilot)
- currentMessage->pilot->setMessagePlaying();
- }
- else
- wholeMsgDone = TRUE;
-
- currentFragment++;
- }
- }
- }
- else // wholeMessageDone
- {
- if (currentMessage)
- {
- if (currentMessage->pilot)
- {
- currentMessage->pilot->clearMessagePlaying();
- #ifndef VIEWER
- ControlGui::instance->endPilotVideo();
- #endif
- }
- removeCurrentMessage();
- }
- }
- }
-
- if (useSound && messagesInQueue && wholeMsgDone)
- {
- if (radioHandle)
- {
- gosAudio_DestroyResource(&radioHandle); //Toss the current radio data which just completed!
- radioHandle = NULL;
- }
-
- //-----------------------------------------------------
- // We are done with the current sample, start the next
- currentFragment = 0;
- moveFromQueueToPlaying();
- if (!currentMessage && !messagesInQueue) //It is now possible for ALL remaining messages to go away because the pilot/mech died!
- return;
- #ifndef VIEWER
- if ( currentMessage->movieCode )
- {
- ControlGui::instance->playPilotVideo(
- currentMessage->pilot, currentMessage->movieCode );
- }
- #endif
- if (currentMessage->noise[currentFragment])
- {
- //--------------------------------------------------------------------
- // Hand GOS sound the data it needs to create the resource Handle
- gosAudio_Format soundFormat;
- soundFormat.wFormatTag = 1; //PCM
- MC2_WAVEFORMATEX *waveFormat = NULL;
- MemoryPtr dataOffset = NULL;
- DWORD length = 0;
- DWORD bitsPerSec = 0;
- wave_ParseWaveMemory(currentMessage->noise[currentFragment],&waveFormat,&dataOffset,&length);
-
- bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
-
- soundFormat.nChannels = waveFormat->nChannels;
- soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
- soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
- soundFormat.nBlockAlign = waveFormat->nBlockAlign;
- soundFormat.wBitsPerSample = bitsPerSec;
- soundFormat.cbSize = 0;
- gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
-
- if (isChannelPlaying(BETTY_CHANNEL))
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
- else
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
- gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
- playingNoise = TRUE;
-
- if (currentMessage->pilot)
- currentMessage->pilot->setMessagePlaying();
- }
- else
- {
- playingNoise = FALSE;
- if (currentMessage->data[currentFragment])
- {
- //--------------------------------------------------------------------
- // Hand GOS sound the data it needs to create the resource Handle
- gosAudio_Format soundFormat;
- soundFormat.wFormatTag = 1; //PCM
-
- MC2_WAVEFORMATEX *waveFormat = NULL;
- MemoryPtr dataOffset = NULL;
- DWORD length = 0;
- DWORD bitsPerSec = 0;
- wave_ParseWaveMemory(currentMessage->data[currentFragment],&waveFormat,&dataOffset,&length);
-
- bitsPerSec = waveFormat->nBlockAlign / waveFormat->nChannels * 8;
-
- soundFormat.nChannels = waveFormat->nChannels;
- soundFormat.nSamplesPerSec = waveFormat->nSamplesPerSec;
- soundFormat.nAvgBytesPerSec = waveFormat->nAvgBytesPerSec;
- soundFormat.nBlockAlign = waveFormat->nBlockAlign;
- soundFormat.wBitsPerSample = bitsPerSec;
- soundFormat.cbSize = 0;
- gosAudio_CreateResource(&radioHandle,gosAudio_UserMemory, NULL, &soundFormat,dataOffset,length);
-
- if (isChannelPlaying(BETTY_CHANNEL))
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume * 0.5f);
- else
- gosAudio_SetChannelSlider(RADIO_CHANNEL, gosAudio_Volume, SoundSystem::digitalMasterVolume * radioVolume);
- gosAudio_AssignResourceToChannel(RADIO_CHANNEL, radioHandle);
- gosAudio_SetChannelPlayMode(RADIO_CHANNEL,gosAudio_PlayOnce);
-
- if (currentMessage->pilot)
- currentMessage->pilot->setMessagePlaying();
- }
- }
-
- wholeMsgDone = FALSE;
- }
- }
|