Skip to content

Commit d2e5d60

Browse files
authored
[GEN][ZH] Unify code of GameAudio and MilesAudioManager (#782)
1 parent d7f7b37 commit d2e5d60

File tree

6 files changed

+145
-9
lines changed

6 files changed

+145
-9
lines changed

Generals/Code/GameEngine/Include/Common/AudioEventInfo.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ enum AudioControl CPP_11(: Int)
8282
AC_INTERRUPT = 0x0010,
8383
};
8484

85+
class DynamicAudioEventInfo;
86+
8587
struct AudioEventInfo : public MemoryPoolObject
8688
{
8789
MEMORY_POOL_GLUE_WITH_USERLOOKUP_CREATE( AudioEventInfo, "AudioEventInfo" )
@@ -118,6 +120,16 @@ struct AudioEventInfo : public MemoryPoolObject
118120

119121
AudioType m_soundType; // This should be either Music, Streaming or SoundEffect
120122

123+
124+
// DynamicAudioEventInfo interfacing functions
125+
virtual Bool isLevelSpecific() const { return false; } ///< If true, this sound is only defined on the current level and can be deleted when that level ends
126+
virtual DynamicAudioEventInfo * getDynamicAudioEventInfo() { return NULL; } ///< If this object is REALLY a DynamicAudioEventInfo, return a pointer to the derived class
127+
virtual const DynamicAudioEventInfo * getDynamicAudioEventInfo() const { return NULL; } ///< If this object is REALLY a DynamicAudioEventInfo, return a pointer to the derived class
128+
129+
/// Is this a permenant sound? That is, if I start this sound up, will it ever end
130+
/// "on its own" or only if I explicitly kill it?
131+
Bool isPermanentSound() const { return BitIsSet( m_control, AC_LOOP ) && (m_loopCount == 0 ); }
132+
121133
static const FieldParse m_audioEventInfo[]; ///< the parse table for INI definition
122134
const FieldParse *getFieldParse( void ) const { return m_audioEventInfo; }
123135
};

Generals/Code/GameEngine/Include/Common/GameAudio.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class AudioManager : public SubsystemInterface
236236
// on zoom.
237237
virtual void set3DVolumeAdjustment( Real volumeAdjustment );
238238

239+
virtual Bool has3DSensitiveStreamsPlaying( void ) const = 0;
240+
239241
virtual void *getHandleForBink( void ) = 0;
240242
virtual void releaseHandleForBink( void ) = 0;
241243

@@ -253,6 +255,7 @@ class AudioManager : public SubsystemInterface
253255
virtual void processRequestList( void );
254256

255257
virtual AudioEventInfo *newAudioEventInfo( AsciiString newEventName );
258+
virtual void addAudioEventInfo( AudioEventInfo * newEventInfo );
256259
virtual AudioEventInfo *findAudioEventInfo( AsciiString eventName ) const;
257260

258261
const AudioSettings *getAudioSettings( void ) const;
@@ -294,6 +297,7 @@ class AudioManager : public SubsystemInterface
294297

295298
// For Worldbuilder, to build lists from which to select
296299
virtual void findAllAudioEventsOfType( AudioType audioType, std::vector<AudioEventInfo*>& allEvents );
300+
virtual const AudioEventInfoHash & getAllAudioEvents() const { return m_allAudioEventInfo; }
297301

298302
Real getZoomVolume() const { return m_zoomVolume; }
299303
protected:
@@ -313,6 +317,11 @@ class AudioManager : public SubsystemInterface
313317
// For tracking purposes
314318
virtual AudioHandle allocateNewHandle( void );
315319

320+
// Remove all AudioEventInfo's with the m_isLevelSpecific flag
321+
virtual void removeLevelSpecificAudioEventInfos( void );
322+
323+
void removeAllAudioRequests( void );
324+
316325
protected:
317326
AudioSettings *m_audioSettings;
318327
MiscAudio *m_miscAudio;

Generals/Code/GameEngine/Source/Common/Audio/GameAudio.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ void AudioManager::update()
390390
m_zoomVolume = 1.0f - scalar * maxBoostScalar;
391391
}
392392
}
393+
393394
set3DVolumeAdjustment( m_zoomVolume );
394395

395396
}
@@ -780,8 +781,8 @@ void AudioManager::set3DVolumeAdjustment( Real volumeAdjustment )
780781
if (m_sound3DVolume > 1.0f)
781782
m_sound3DVolume = 1.0f;
782783

783-
784-
m_volumeHasChanged = TRUE;
784+
if ( ! has3DSensitiveStreamsPlaying() )
785+
m_volumeHasChanged = TRUE;
785786
}
786787

787788
//-------------------------------------------------------------------------------------------------
@@ -820,6 +821,18 @@ void AudioManager::appendAudioRequest( AudioRequest *m_request )
820821
m_audioRequests.push_back(m_request);
821822
}
822823

824+
//-------------------------------------------------------------------------------------------------
825+
// Remove all pending audio requests
826+
void AudioManager::removeAllAudioRequests( void )
827+
{
828+
std::list<AudioRequest*>::iterator it;
829+
for ( it = m_audioRequests.begin(); it != m_audioRequests.end(); it++ ) {
830+
releaseAudioRequest( *it );
831+
}
832+
833+
m_audioRequests.clear();
834+
}
835+
823836
//-------------------------------------------------------------------------------------------------
824837
void AudioManager::processRequestList( void )
825838
{
@@ -839,6 +852,23 @@ AudioEventInfo *AudioManager::newAudioEventInfo( AsciiString audioName )
839852
return m_allAudioEventInfo[audioName];
840853
}
841854

855+
//-------------------------------------------------------------------------------------------------
856+
// Add an AudioEventInfo structure allocated elsewhere to the audio event list
857+
void AudioManager::addAudioEventInfo( AudioEventInfo * newEvent )
858+
{
859+
// Warning: Don't try to copy the structure. It may be a derived class
860+
AudioEventInfo *eventInfo = findAudioEventInfo( newEvent->m_audioName );
861+
if (eventInfo)
862+
{
863+
DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd\n", newEvent->m_audioName.str()));
864+
*eventInfo = *newEvent;
865+
}
866+
else
867+
{
868+
m_allAudioEventInfo[newEvent->m_audioName] = newEvent;
869+
}
870+
}
871+
842872
//-------------------------------------------------------------------------------------------------
843873
AudioEventInfo *AudioManager::findAudioEventInfo( AsciiString eventName ) const
844874
{
@@ -851,6 +881,28 @@ AudioEventInfo *AudioManager::findAudioEventInfo( AsciiString eventName ) const
851881
return (*it).second;
852882
}
853883

884+
//-------------------------------------------------------------------------------------------------
885+
// Remove all AudioEventInfo's with the m_isLevelSpecific flag
886+
void AudioManager::removeLevelSpecificAudioEventInfos(void)
887+
{
888+
AudioEventInfoHash::iterator it = m_allAudioEventInfo.begin();
889+
890+
while ( it != m_allAudioEventInfo.end() )
891+
{
892+
AudioEventInfoHash::iterator next = it; // Make sure erase doesn't cause problems
893+
next++;
894+
895+
if ( it->second->isLevelSpecific() )
896+
{
897+
it->second->deleteInstance();
898+
m_allAudioEventInfo.erase( it );
899+
}
900+
901+
it = next;
902+
}
903+
904+
}
905+
854906
//-------------------------------------------------------------------------------------------------
855907
const AudioSettings *AudioManager::getAudioSettings( void ) const
856908
{

Generals/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct PlayingAudio
7373
m_audioEventRTS(NULL),
7474
m_requestStop(false),
7575
m_cleanupAudioEventRTS(true),
76+
m_sample(NULL),
7677
m_framesFaded(0)
7778
{ }
7879
};
@@ -223,6 +224,10 @@ class MilesAudioManager : public AudioManager
223224

224225
virtual void closeAnySamplesUsingFile( const void *fileToClose );
225226

227+
228+
virtual Bool has3DSensitiveStreamsPlaying( void ) const;
229+
230+
226231
protected:
227232
// 3-D functions
228233
virtual void setDeviceListenerPosition( void );

Generals/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ void MilesAudioManager::reset()
474474

475475
AudioManager::reset();
476476
stopAllAudioImmediately();
477+
removeAllAudioRequests();
478+
// This must come after stopAllAudioImmediately() and removeAllAudioRequests(), to ensure that
479+
// sounds pointing to the temporary AudioEventInfo handles are deleted before their info is deleted
480+
removeLevelSpecificAudioEventInfos();
477481
}
478482

479483
//-------------------------------------------------------------------------------------------------
@@ -603,6 +607,7 @@ void MilesAudioManager::pauseAudio( AudioAffect which )
603607
}
604608
}
605609

610+
606611
//-------------------------------------------------------------------------------------------------
607612
void MilesAudioManager::resumeAudio( AudioAffect which )
608613
{
@@ -1161,6 +1166,16 @@ void MilesAudioManager::stopAllAudioImmediately( void )
11611166
it = m_playingStreams.erase(it);
11621167
}
11631168

1169+
for (it = m_fadingAudio.begin(); it != m_fadingAudio.end(); ) {
1170+
playing = (*it);
1171+
if (!playing) {
1172+
continue;
1173+
}
1174+
1175+
releasePlayingAudio(playing);
1176+
it = m_fadingAudio.erase(it);
1177+
}
1178+
11641179
std::list<HAUDIO>::iterator hit;
11651180
for (hit = m_audioForcePlayed.begin(); hit != m_audioForcePlayed.end(); ++hit) {
11661181
if (*hit) {
@@ -2377,6 +2392,44 @@ void MilesAudioManager::processPlayingList( void )
23772392
}
23782393
}
23792394

2395+
//Patch for a rare bug (only on about 5% of in-studio machines suffer, and not all the time) .
2396+
//The actual mechanics of this problem are still elusive as of the date of this comment. 8/21/03
2397+
//but the cause is clear. Some cinematics do a radical change in the microphone position, which
2398+
//calls for a radical 3DSoundVolume adjustment. If this happens while a stereo stream is *ENDING*,
2399+
//low-level code gets caught in a tight loop. (Hangs) on some machines.
2400+
//To prevent this condition, we just suppress the updating of 3DSoundVolume while one of these
2401+
//is on the list. Since the music tracks play continuously, they never *END* during these cinematics.
2402+
//so we filter them out as, *NOT SENSITIVE*... we do want to update 3DSoundVolume during music,
2403+
//which is almost all of the time.
2404+
2405+
Bool MilesAudioManager::has3DSensitiveStreamsPlaying( void ) const
2406+
{
2407+
if ( m_playingStreams.empty() )
2408+
return FALSE;
2409+
2410+
for ( std::list< PlayingAudio* >::const_iterator it = m_playingStreams.begin(); it != m_playingStreams.end(); ++it )
2411+
{
2412+
const PlayingAudio *playing = (*it);
2413+
2414+
if ( ! playing )
2415+
continue;
2416+
2417+
if ( playing->m_audioEventRTS->getAudioEventInfo()->m_soundType != AT_Music )
2418+
{
2419+
return TRUE;
2420+
}
2421+
2422+
if ( playing->m_audioEventRTS->getEventName().startsWith("Game_") == FALSE )
2423+
{
2424+
return TRUE;
2425+
}
2426+
}
2427+
2428+
return FALSE;
2429+
2430+
}
2431+
2432+
23802433
//-------------------------------------------------------------------------------------------------
23812434
void MilesAudioManager::processFadingList( void )
23822435
{
@@ -2476,7 +2529,14 @@ Bool MilesAudioManager::checkForSample( AudioRequest *req )
24762529
return true;
24772530
}
24782531

2479-
if (req->m_pendingEvent->getAudioEventInfo()->m_type != AT_SoundEffect) {
2532+
if ( req->m_pendingEvent->getAudioEventInfo() == NULL )
2533+
{
2534+
// Fill in event info
2535+
getInfoForAudioEvent( req->m_pendingEvent );
2536+
}
2537+
2538+
if (req->m_pendingEvent->getAudioEventInfo()->m_type != AT_SoundEffect)
2539+
{
24802540
return true;
24812541
}
24822542

GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ enum PlayingWhich CPP_11(: Int)
5353

5454
struct PlayingAudio
5555
{
56-
// union
57-
// {
56+
union
57+
{
5858
HSAMPLE m_sample;
5959
H3DSAMPLE m_3DSample;
6060
HSTREAM m_stream;
61-
// };
61+
};
6262

6363
PlayingAudioType m_type;
6464
volatile PlayingStatus m_status; // This member is adjusted by another running thread.
@@ -73,9 +73,7 @@ struct PlayingAudio
7373
m_audioEventRTS(NULL),
7474
m_requestStop(false),
7575
m_cleanupAudioEventRTS(true),
76-
m_sample(0),
77-
m_3DSample(0),
78-
m_stream(0),
76+
m_sample(NULL),
7977
m_framesFaded(0)
8078
{ }
8179
};

0 commit comments

Comments
 (0)