Skip to content

Commit 81fffb3

Browse files
authored
refactor(audio): Simplify available audio samples management (TheSuperHackers#2773)
1 parent 7bbd643 commit 81fffb3

5 files changed

Lines changed: 61 additions & 117 deletions

File tree

Core/GameEngine/Include/Common/GameAudio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class AudioManager : public SubsystemInterface
219219
virtual UnsignedInt getNum2DSamples() const = 0;
220220
virtual UnsignedInt getNum3DSamples() const = 0;
221221
virtual UnsignedInt getNumStreams() const = 0;
222+
virtual UnsignedInt getNumAvailable2DSamples() const = 0;
223+
virtual UnsignedInt getNumAvailable3DSamples() const = 0;
222224

223225
// Device Dependent calls to determine sound prioritization info
224226
virtual Bool doesViolateLimit( AudioEventRTS *event ) const = 0;

Core/GameEngine/Include/Common/GameSounds.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ class SoundManager : public SubsystemInterface
6969

7070
virtual void addAudioEvent(AudioEventRTS *&eventToAdd); // pre-copied
7171

72-
virtual void notifyOf2DSampleStart();
73-
virtual void notifyOf3DSampleStart();
74-
75-
virtual void notifyOf2DSampleCompletion();
76-
virtual void notifyOf3DSampleCompletion();
77-
78-
virtual Int getAvailableSamples();
79-
virtual Int getAvailable3DSamples();
80-
8172
// empty string means that this sound wasn't found or some error occurred. CHECK FOR EMPTY STRING.
8273
virtual AsciiString getFilenameForPlayFromAudioEvent( const AudioEventRTS *eventToGetFrom );
8374

@@ -87,12 +78,4 @@ class SoundManager : public SubsystemInterface
8778
protected:
8879
virtual Bool violatesVoice( AudioEventRTS *event );
8980
virtual Bool isInterrupting( AudioEventRTS *event );
90-
91-
92-
protected:
93-
UnsignedInt m_num2DSamples;
94-
UnsignedInt m_num3DSamples;
95-
96-
UnsignedInt m_numPlaying2DSamples;
97-
UnsignedInt m_numPlaying3DSamples;
9881
};

Core/GameEngine/Source/Common/Audio/GameSounds.cpp

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ void SoundManager::update()
9292
//-------------------------------------------------------------------------------------------------
9393
void SoundManager::reset()
9494
{
95-
m_numPlaying2DSamples = 0;
96-
m_numPlaying3DSamples = 0;
95+
9796
}
9897

9998
//-------------------------------------------------------------------------------------------------
@@ -135,11 +134,6 @@ Real SoundManager::getCameraAudibleDistance()
135134
//-------------------------------------------------------------------------------------------------
136135
void SoundManager::addAudioEvent(AudioEventRTS *&eventToAdd)
137136
{
138-
if (m_num2DSamples == 0 && m_num3DSamples == 0) {
139-
m_num2DSamples = TheAudio->getNum2DSamples();
140-
m_num3DSamples = TheAudio->getNum3DSamples();
141-
}
142-
143137
if (canPlayNow(eventToAdd)) {
144138
#ifdef INTENSIVE_AUDIO_DEBUG
145139
DEBUG_LOG((" - appended to request list with handle '%d'.", (UnsignedInt) eventToAdd->getPlayingHandle()));
@@ -153,46 +147,6 @@ void SoundManager::addAudioEvent(AudioEventRTS *&eventToAdd)
153147
}
154148
}
155149

156-
//-------------------------------------------------------------------------------------------------
157-
void SoundManager::notifyOf2DSampleStart()
158-
{
159-
++m_numPlaying2DSamples;
160-
}
161-
162-
//-------------------------------------------------------------------------------------------------
163-
void SoundManager::notifyOf3DSampleStart()
164-
{
165-
++m_numPlaying3DSamples;
166-
}
167-
168-
//-------------------------------------------------------------------------------------------------
169-
void SoundManager::notifyOf2DSampleCompletion()
170-
{
171-
if (m_numPlaying2DSamples > 0) {
172-
--m_numPlaying2DSamples;
173-
}
174-
}
175-
176-
//-------------------------------------------------------------------------------------------------
177-
void SoundManager::notifyOf3DSampleCompletion()
178-
{
179-
if (m_numPlaying3DSamples > 0) {
180-
--m_numPlaying3DSamples;
181-
}
182-
}
183-
184-
//-------------------------------------------------------------------------------------------------
185-
Int SoundManager::getAvailableSamples()
186-
{
187-
return (m_num2DSamples - m_numPlaying2DSamples);
188-
}
189-
190-
//-------------------------------------------------------------------------------------------------
191-
Int SoundManager::getAvailable3DSamples()
192-
{
193-
return (m_num3DSamples - m_numPlaying3DSamples);
194-
}
195-
196150
//-------------------------------------------------------------------------------------------------
197151
AsciiString SoundManager::getFilenameForPlayFromAudioEvent( const AudioEventRTS *eventToGetFrom )
198152
{
@@ -272,18 +226,19 @@ Bool SoundManager::canPlayNow( AudioEventRTS *event )
272226

273227
if (event->isPositionalAudio())
274228
{
275-
if (m_numPlaying3DSamples < m_num3DSamples)
229+
if (TheAudio->getNumAvailable3DSamples() > 0)
276230
{
277231
return true;
278232
}
279233
#ifdef INTENSIVE_AUDIO_DEBUG
280-
DEBUG_LOG(("- %d samples playing, %d samples available", m_numPlaying3DSamples, m_num3DSamples));
234+
DEBUG_LOG(("- %d samples playing, %d samples available",
235+
TheAudio->getNum3DSamples() - TheAudio->getNumAvailable3DSamples(), TheAudio->getNum3DSamples()));
281236
#endif
282237
}
283238
else
284239
{
285240
// its a UI sound (and thus, 2-D)
286-
if (m_numPlaying2DSamples < m_num2DSamples)
241+
if (TheAudio->getNumAvailable2DSamples() > 0)
287242
{
288243
return true;
289244
}

Core/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ class MilesAudioManager : public AudioManager
195195
virtual UnsignedInt getNum2DSamples() const override;
196196
virtual UnsignedInt getNum3DSamples() const override;
197197
virtual UnsignedInt getNumStreams() const override;
198+
virtual UnsignedInt getNumAvailable2DSamples() const override;
199+
virtual UnsignedInt getNumAvailable3DSamples() const override;
198200

199201
virtual Bool doesViolateLimit( AudioEventRTS *event ) const override;
200202
virtual Bool isPlayingLowerPriority( AudioEventRTS *event ) const override;
@@ -272,8 +274,8 @@ class MilesAudioManager : public AudioManager
272274
void stopAllAudioImmediately();
273275
void freeAllMilesHandles();
274276

275-
HSAMPLE getFirst2DSample( AudioEventRTS *event );
276-
H3DSAMPLE getFirst3DSample( AudioEventRTS *event );
277+
HSAMPLE getAvailable2DSample( AudioEventRTS *event );
278+
H3DSAMPLE getAvailable3DSample( AudioEventRTS *event );
277279

278280
void adjustPlayingVolume( PlayingAudio *audio );
279281

@@ -301,8 +303,8 @@ class MilesAudioManager : public AudioManager
301303

302304
// Available handles for play. Note that there aren't handles open in advance for
303305
// streaming things, only 2-D and 3-D sounds.
304-
std::list<HSAMPLE> m_availableSamples;
305-
std::list<H3DSAMPLE> m_available3DSamples;
306+
std::vector<HSAMPLE> m_availableSamples;
307+
std::vector<H3DSAMPLE> m_available3DSamples;
306308

307309
// Currently Playing audio. Useful if we have to preempt it.
308310
// This should rarely if ever happen, as we mirror this in Sounds, and attempt to
@@ -368,6 +370,8 @@ class MilesAudioManagerDummy : public MilesAudioManager
368370
virtual UnsignedInt getNum2DSamples() const override { return 0; }
369371
virtual UnsignedInt getNum3DSamples() const override { return 0; }
370372
virtual UnsignedInt getNumStreams() const override { return 0; }
373+
virtual UnsignedInt getNumAvailable2DSamples() const override { return 0; }
374+
virtual UnsignedInt getNumAvailable3DSamples() const override { return 0; }
371375
virtual Bool doesViolateLimit(AudioEventRTS* event) const override { return false; }
372376
virtual Bool isPlayingLowerPriority(AudioEventRTS* event) const override { return false; }
373377
virtual Bool isPlayingAlready(AudioEventRTS* event) const override { return false; }

Core/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,8 @@ void MilesAudioManager::audioDebugDisplay(DebugDisplayInterface *dd, void *, FIL
165165
dd->printf("Speech: %s ", (isOn(AudioAffect_Speech) ? "Yes" : "No"));
166166
dd->printf("Music: %s\n", (isOn(AudioAffect_Music) ? "Yes" : "No"));
167167
dd->printf("Channels Available: ");
168-
dd->printf("%d Sounds ", m_sound->getAvailableSamples());
169-
170-
dd->printf("%d(%d) 3D Sounds\n", m_sound->getAvailable3DSamples(), m_available3DSamples.size() );
168+
dd->printf("%u Sounds ", getNumAvailable2DSamples());
169+
dd->printf("%u 3D Sounds\n", getNumAvailable3DSamples());
171170
dd->printf("Volume: ");
172171
dd->printf("Sound: %d ", REAL_TO_INT(m_soundVolume * 100.0f));
173172
dd->printf("3DSound: %d ", REAL_TO_INT(m_sound3DVolume * 100.0f));
@@ -196,8 +195,8 @@ void MilesAudioManager::audioDebugDisplay(DebugDisplayInterface *dd, void *, FIL
196195
fprintf( fp, "Speech: %s ", (isOn(AudioAffect_Speech) ? "Yes" : "No") );
197196
fprintf( fp, "Music: %s\n", (isOn(AudioAffect_Music) ? "Yes" : "No") );
198197
fprintf( fp, "Channels Available: " );
199-
fprintf( fp, "%d Sounds ", m_sound->getAvailableSamples() );
200-
fprintf( fp, "%d 3D Sounds\n", m_sound->getAvailable3DSamples() );
198+
fprintf( fp, "%u Sounds ", getNumAvailable2DSamples() );
199+
fprintf( fp, "%u 3D Sounds\n", getNumAvailable3DSamples() );
201200
fprintf( fp, "Volume: ");
202201
fprintf( fp, "Sound: %d ", REAL_TO_INT(m_soundVolume * 100.0f) );
203202
fprintf( fp, "3DSound: %d ", REAL_TO_INT(m_sound3DVolume * 100.0f) );
@@ -715,7 +714,7 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
715714
H3DSAMPLE sample3D;
716715
if( !handleToKill || foundSoundToReplace )
717716
{
718-
sample3D = getFirst3DSample( event );
717+
sample3D = getAvailable3DSample( event );
719718
if( !sample3D )
720719
{
721720
//If we don't have an available sample, kill the lowest priority assuming we have one that is lower
@@ -724,7 +723,7 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
724723
//in which case we need to attempt to find another sound to kill.
725724
if( killLowestPrioritySoundImmediately( event ) )
726725
{
727-
sample3D = getFirst3DSample( event );
726+
sample3D = getAvailable3DSample( event );
728727
}
729728
}
730729
}
@@ -740,7 +739,6 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
740739

741740
if (sample3D) {
742741
audio->m_file = playSample3D(event, sample3D);
743-
m_sound->notifyOf3DSampleStart();
744742
}
745743

746744
if( !audio->m_file )
@@ -781,7 +779,7 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
781779
HSAMPLE sample;
782780
if( !handleToKill || foundSoundToReplace )
783781
{
784-
sample = getFirst2DSample(event);
782+
sample = getAvailable2DSample(event);
785783
if( !sample )
786784
{
787785
//If we don't have an available sample, kill the lowest priority assuming we have one that is lower
@@ -790,7 +788,7 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
790788
//in which case we need to attempt to find another sound to kill.
791789
if( killLowestPrioritySoundImmediately( event ) )
792790
{
793-
sample = getFirst2DSample( event );
791+
sample = getAvailable2DSample( event );
794792
}
795793
}
796794
}
@@ -807,7 +805,6 @@ void MilesAudioManager::playAudioEvent( AudioRequest* req )
807805

808806
if (sample) {
809807
audio->m_file = playSample(event, sample);
810-
m_sound->notifyOf2DSampleStart();
811808
}
812809

813810
if (!audio->m_file) {
@@ -1042,17 +1039,6 @@ void MilesAudioManager::stopPlayingAudio( PlayingAudio *release )
10421039
if (prevStatus != PS_Stopping) {
10431040
return;
10441041
}
1045-
if (release->m_audioEventRTS->getAudioEventInfo()->m_soundType == AT_SoundEffect) {
1046-
if (release->m_type == PAT_Sample) {
1047-
if (release->m_sample) {
1048-
m_sound->notifyOf2DSampleCompletion();
1049-
}
1050-
} else {
1051-
if (release->m_3DSample) {
1052-
m_sound->notifyOf3DSampleCompletion();
1053-
}
1054-
}
1055-
}
10561042
releaseMilesHandles(release);
10571043
}
10581044

@@ -1175,48 +1161,47 @@ void MilesAudioManager::freeAllMilesHandles()
11751161
stopAllAudioImmediately();
11761162

11771163
// Walks through the available 2-D and 3-D handles and releases them
1178-
std::list<HSAMPLE>::iterator it;
1179-
for ( it = m_availableSamples.begin(); it != m_availableSamples.end(); ) {
1180-
HSAMPLE sample = *it;
1181-
AIL_release_sample_handle(sample);
1182-
it = m_availableSamples.erase(it);
1164+
std::vector<HSAMPLE>::iterator it;
1165+
for ( it = m_availableSamples.begin(); it != m_availableSamples.end(); ++it ) {
1166+
AIL_release_sample_handle(*it);
11831167
}
1168+
1169+
m_availableSamples.clear();
11841170
m_num2DSamples = 0;
11851171

1186-
std::list<H3DSAMPLE>::iterator it3D;
1187-
for ( it3D = m_available3DSamples.begin(); it3D != m_available3DSamples.end(); ) {
1188-
H3DSAMPLE sample3D = *it3D;
1189-
AIL_release_3D_sample_handle(sample3D);
1190-
it3D = m_available3DSamples.erase(it3D);
1172+
std::vector<H3DSAMPLE>::iterator it3D;
1173+
for ( it3D = m_available3DSamples.begin(); it3D != m_available3DSamples.end(); ++it3D ) {
1174+
AIL_release_3D_sample_handle(*it3D);
11911175
}
1176+
1177+
m_available3DSamples.clear();
11921178
m_num3DSamples = 0;
11931179
m_numStreams = 0;
11941180
}
11951181

11961182
//-------------------------------------------------------------------------------------------------
1197-
HSAMPLE MilesAudioManager::getFirst2DSample( AudioEventRTS *event )
1183+
HSAMPLE MilesAudioManager::getAvailable2DSample( AudioEventRTS *event )
11981184
{
1199-
if (m_availableSamples.begin() != m_availableSamples.end()) {
1200-
HSAMPLE retSample = *m_availableSamples.begin();
1201-
m_availableSamples.erase(m_availableSamples.begin());
1202-
return (retSample);
1185+
if (!m_availableSamples.empty()) {
1186+
HSAMPLE retSample = m_availableSamples.back();
1187+
m_availableSamples.pop_back();
1188+
return retSample;
12031189
}
12041190

1205-
// Find the first sample of lower priority than my augmented priority that is interruptable and take its handle
1206-
1191+
// Find the first sample of lower priority than my augmented priority that is interruptible and take its handle
12071192
return nullptr;
12081193
}
12091194

12101195
//-------------------------------------------------------------------------------------------------
1211-
H3DSAMPLE MilesAudioManager::getFirst3DSample( AudioEventRTS *event )
1196+
H3DSAMPLE MilesAudioManager::getAvailable3DSample( AudioEventRTS *event )
12121197
{
1213-
if (m_available3DSamples.begin() != m_available3DSamples.end()) {
1214-
H3DSAMPLE retSample = *m_available3DSamples.begin();
1215-
m_available3DSamples.erase(m_available3DSamples.begin());
1216-
return (retSample);
1198+
if (!m_available3DSamples.empty()) {
1199+
H3DSAMPLE retSample = m_available3DSamples.back();
1200+
m_available3DSamples.pop_back();
1201+
return retSample;
12171202
}
12181203

1219-
// Find the first sample of lower priority than my augmented priority that is interruptable and take its handle
1204+
// Find the first sample of lower priority than my augmented priority that is interruptible and take its handle
12201205
return nullptr;
12211206
}
12221207

@@ -1755,6 +1740,18 @@ UnsignedInt MilesAudioManager::getNumStreams() const
17551740
return m_numStreams;
17561741
}
17571742

1743+
//-------------------------------------------------------------------------------------------------
1744+
UnsignedInt MilesAudioManager::getNumAvailable2DSamples() const
1745+
{
1746+
return (UnsignedInt)m_availableSamples.size();
1747+
}
1748+
1749+
//-------------------------------------------------------------------------------------------------
1750+
UnsignedInt MilesAudioManager::getNumAvailable3DSamples() const
1751+
{
1752+
return (UnsignedInt)m_available3DSamples.size();
1753+
}
1754+
17581755
//-------------------------------------------------------------------------------------------------
17591756
Bool MilesAudioManager::doesViolateLimit( AudioEventRTS *event ) const
17601757
{
@@ -2769,6 +2766,9 @@ void MilesAudioManager::initSamplePools()
27692766
return;
27702767
}
27712768

2769+
m_availableSamples.reserve(getAudioSettings()->m_sampleCount2D);
2770+
m_available3DSamples.reserve(getAudioSettings()->m_sampleCount3D);
2771+
27722772
int i = 0;
27732773
for (i = 0; i < getAudioSettings()->m_sampleCount2D; ++i) {
27742774
HSAMPLE sample = AIL_allocate_sample_handle(m_digitalHandle);
@@ -2825,7 +2825,7 @@ void *MilesAudioManager::getHandleForBink()
28252825
PlayingAudio *aud = allocatePlayingAudio();
28262826
aud->m_audioEventRTS = NEW AudioEventRTS("BinkHandle"); // poolify
28272827
getInfoForAudioEvent(aud->m_audioEventRTS);
2828-
aud->m_sample = getFirst2DSample(aud->m_audioEventRTS);
2828+
aud->m_sample = getAvailable2DSample(aud->m_audioEventRTS);
28292829
aud->m_type = PAT_Sample;
28302830

28312831
if (!aud->m_sample) {

0 commit comments

Comments
 (0)