forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameAudio.cpp
More file actions
1162 lines (965 loc) · 39.3 KB
/
GameAudio.cpp
File metadata and controls
1162 lines (965 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------
//
// Westwood Studios Pacific.
//
// Confidential Information
// Copyright (C) 2001 - All Rights Reserved
//
//----------------------------------------------------------------------------
//
// Project: RTS3
//
// File name: GameAudio.cpp
//
// Created: 5/01/01
//
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "Common/GameAudio.h"
#include "Common/AudioAffect.h"
#include "Common/AudioEventInfo.h"
#include "Common/AudioEventRTS.h"
#include "Common/AudioHandleSpecialValues.h"
#include "Common/AudioRequest.h"
#include "Common/AudioSettings.h"
#include "Common/FileSystem.h"
#include "Common/GameEngine.h"
#include "Common/GameMusic.h"
#include "Common/GameSounds.h"
#include "Common/MiscAudio.h"
#include "Common/OSDisplay.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "Common/UserPreferences.h"
#include "GameClient/ControlBar.h"
#include "GameClient/Drawable.h"
#include "GameClient/View.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/TerrainLogic.h"
#include "WWMath/matrix3d.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
static const char* TheSpeakerTypes[] =
{
"2 Speakers",
"Headphones",
"Surround Sound",
"4 Speaker",
"5.1 Surround",
"7.1 Surround",
nullptr
};
static const Int TheSpeakerTypesCount = sizeof(TheSpeakerTypes) / sizeof(TheSpeakerTypes[0]);
static void parseSpeakerType( INI *ini, void *instance, void *store, const void *userData );
// Field Parse table for Audio Settings ///////////////////////////////////////////////////////////
static const FieldParse audioSettingsFieldParseTable[] =
{
{ "AudioRoot", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_audioRoot) },
{ "SoundsFolder", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_soundsFolder) },
{ "MusicFolder", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_musicFolder) },
{ "StreamingFolder", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_streamingFolder) },
{ "SoundsExtension", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_soundsExtension) },
{ "UseDigital", INI::parseBool, nullptr, offsetof( AudioSettings, m_useDigital) },
{ "UseMidi", INI::parseBool, nullptr, offsetof( AudioSettings, m_useMidi) },
{ "OutputRate", INI::parseInt, nullptr, offsetof( AudioSettings, m_outputRate) },
{ "OutputBits", INI::parseInt, nullptr, offsetof( AudioSettings, m_outputBits) },
{ "OutputChannels", INI::parseInt, nullptr, offsetof( AudioSettings, m_outputChannels) },
{ "SampleCount2D", INI::parseInt, nullptr, offsetof( AudioSettings, m_sampleCount2D) },
{ "SampleCount3D", INI::parseInt, nullptr, offsetof( AudioSettings, m_sampleCount3D) },
{ "StreamCount", INI::parseInt, nullptr, offsetof( AudioSettings, m_streamCount) },
{ "Preferred3DHW1", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_preferred3DProvider[0]) },
{ "Preferred3DHW2", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_preferred3DProvider[1]) },
{ "Preferred3DHW3", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_preferred3DProvider[2]) },
{ "Preferred3DHW4", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_preferred3DProvider[3]) },
{ "Preferred3DSW", INI::parseAsciiString, nullptr, offsetof( AudioSettings, m_preferred3DProvider[4]) },
{ "Default2DSpeakerType", parseSpeakerType, nullptr, offsetof( AudioSettings, m_defaultSpeakerType2D) },
{ "Default3DSpeakerType", parseSpeakerType, nullptr, offsetof( AudioSettings, m_defaultSpeakerType3D) },
{ "MinSampleVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_minVolume) },
{ "GlobalMinRange", INI::parseInt, nullptr, offsetof( AudioSettings, m_globalMinRange) },
{ "GlobalMaxRange", INI::parseInt, nullptr, offsetof( AudioSettings, m_globalMaxRange) },
{ "TimeBetweenDrawableSounds", INI::parseDurationUnsignedInt, nullptr, offsetof( AudioSettings, m_drawableAmbientFrames) },
{ "TimeToFadeAudio", INI::parseDurationUnsignedInt, nullptr, offsetof( AudioSettings, m_fadeAudioFrames) },
{ "AudioFootprintInBytes",INI::parseUnsignedInt, nullptr, offsetof( AudioSettings, m_maxCacheSize) },
{ "Relative2DVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_relative2DVolume ) },
{ "DefaultSoundVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_defaultSoundVolume) },
{ "Default3DSoundVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_default3DSoundVolume) },
{ "DefaultSpeechVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_defaultSpeechVolume) },
{ "DefaultMusicVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_defaultMusicVolume) },
{ "DefaultMoneyTransactionVolume", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_defaultMoneyTransactionVolume) },
{ "MicrophoneDesiredHeightAboveTerrain", INI::parseReal, nullptr, offsetof( AudioSettings, m_microphoneDesiredHeightAboveTerrain ) },
{ "MicrophoneMaxPercentageBetweenGroundAndCamera", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_microphoneMaxPercentageBetweenGroundAndCamera ) },
{ "ZoomMinDistance", INI::parseReal, nullptr, offsetof( AudioSettings, m_zoomMinDistance ) },
{ "ZoomMaxDistance", INI::parseReal, nullptr, offsetof( AudioSettings, m_zoomMaxDistance ) },
{ "ZoomSoundVolumePercentageAmount", INI::parsePercentToReal, nullptr, offsetof( AudioSettings, m_zoomSoundVolumePercentageAmount ) },
{ nullptr, nullptr, nullptr, 0 }
};
// Singleton TheAudio /////////////////////////////////////////////////////////////////////////////
AudioManager *TheAudio = nullptr;
const char *const AudioManager::MuteAudioReasonNames[] =
{
"MuteAudioReason_WindowFocus",
};
// AudioManager Device Independent functions //////////////////////////////////////////////////////
AudioManager::AudioManager() :
m_soundOn(TRUE),
m_sound3DOn(TRUE),
m_musicOn(TRUE),
m_speechOn(TRUE),
m_music(nullptr),
m_sound(nullptr),
m_surroundSpeakers(FALSE),
m_hardwareAccel(FALSE)
{
static_assert(ARRAY_SIZE(AudioManager::MuteAudioReasonNames) == MuteAudioReason_Count, "Incorrect array size");
m_adjustedVolumes.clear();
m_audioRequests.clear();
m_listenerPosition.zero();
m_musicTracks.clear();
m_musicVolume = 0.0f;
m_sound3DVolume = 0.0f;
m_soundVolume = 0.0f;
m_speechVolume = 0.0f;
m_systemMusicVolume = 0.0f;
m_systemSound3DVolume = 0.0f;
m_systemSoundVolume = 0.0f;
m_systemSpeechVolume = 0.0f;
m_volumeHasChanged = FALSE;
m_listenerOrientation.set(0.0, 1.0, 0.0);
theAudioHandlePool = AHSV_FirstHandle;
m_audioSettings = NEW AudioSettings;
m_miscAudio = NEW MiscAudio;
m_silentAudioEvent = NEW AudioEventRTS;
m_savedValues = nullptr;
m_muteReasonBits = 0;
m_disallowSpeech = FALSE;
}
//-------------------------------------------------------------------------------------------------
AudioManager::~AudioManager()
{
// cleanup all of the loaded AudioEventInfos
AudioEventInfoHashIt it;
for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
AudioEventInfo *eventInfo = (*it).second;
deleteInstance(eventInfo);
}
m_allAudioEventInfo.clear();
delete m_silentAudioEvent;
m_silentAudioEvent = nullptr;
delete m_music;
m_music = nullptr;
delete m_sound;
m_sound = nullptr;
delete m_miscAudio;
m_miscAudio = nullptr;
delete m_audioSettings;
m_audioSettings = nullptr;
delete [] m_savedValues;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::init()
{
INI ini;
ini.loadFileDirectory( "Data\\INI\\AudioSettings", INI_LOAD_OVERWRITE, nullptr);
ini.loadFileDirectory( "Data\\INI\\Default\\Music", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Music", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Default\\SoundEffects", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\SoundEffects", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Default\\Speech", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Speech", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Default\\Voice", INI_LOAD_OVERWRITE, nullptr );
ini.loadFileDirectory( "Data\\INI\\Voice", INI_LOAD_OVERWRITE, nullptr );
// do the miscellaneous sound files last so that we find the AudioEventRTS associated with the events.
ini.loadFileDirectory( "Data\\INI\\MiscAudio", INI_LOAD_OVERWRITE, nullptr);
m_music = NEW MusicManager;
m_sound = NEW SoundManager;
// Set our system volumes from the user's preferred settings, not the defaults.
m_systemMusicVolume = m_audioSettings->m_preferredMusicVolume;
m_systemSoundVolume = m_audioSettings->m_preferredSoundVolume;
m_systemSound3DVolume = m_audioSettings->m_preferred3DSoundVolume;
m_systemSpeechVolume = m_audioSettings->m_preferredSpeechVolume;
m_scriptMusicVolume = 1.0f;
m_scriptSoundVolume = 1.0f;
m_scriptSound3DVolume = 1.0f;
m_scriptSpeechVolume = 1.0f;
m_musicVolume = m_systemMusicVolume;
m_soundVolume = m_systemSoundVolume;
m_sound3DVolume = m_systemSound3DVolume;
m_speechVolume = m_systemSpeechVolume;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::postProcessLoad()
{
}
//-------------------------------------------------------------------------------------------------
void AudioManager::reset()
{
// clear out any adjusted volumes we might have set.
m_adjustedVolumes.clear();
// adjust the scripted volumes, and reset the
m_scriptMusicVolume = 1.0f;
m_scriptSoundVolume = 1.0f;
m_scriptSound3DVolume = 1.0f;
m_scriptSpeechVolume = 1.0f;
// restore the final values to the
m_musicVolume = m_systemMusicVolume;
m_soundVolume = m_systemSoundVolume;
m_sound3DVolume = m_systemSound3DVolume;
m_speechVolume = m_systemSpeechVolume;
m_disallowSpeech = FALSE;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::update()
{
Coord3D groundPos, microphonePos;
TheTacticalView->getPosition( &groundPos );
Real angle = TheTacticalView->getAngle();
Matrix3D rot = Matrix3D::Identity;
rot.Rotate_Z( angle );
Vector3 forward( 0, 1, 0 );
rot.mulVector3( forward );
Real desiredHeight = m_audioSettings->m_microphoneDesiredHeightAboveTerrain;
Real maxPercentage = m_audioSettings->m_microphoneMaxPercentageBetweenGroundAndCamera;
Coord3D lookTo;
lookTo.set(forward.X, forward.Y, forward.Z);
//Kris: At this point, the microphone is calculated to be at the ground position where the camera is looking at.
//Instead we want to move the microphone towards the camera. Hopefully, it'll be a desired altitude, but if it
//gets too close to the camera (or even past it), that would be undesirable. Therefore, we have a backup method
//of making sure we only go a certain percentage towards the camera or the desired height, whichever occurs first.
Coord3D cameraPos = TheTacticalView->get3DCameraPosition();
Coord3D groundToCameraVector;
groundToCameraVector.set( &cameraPos );
groundToCameraVector.sub( &groundPos );
Real bestScaleFactor;
if( cameraPos.z <= desiredHeight || groundToCameraVector.z <= 0.0f )
{
//Use the percentage calculation!
bestScaleFactor = maxPercentage;
}
else
{
//Calculate the stopping position of the groundToCameraVector when we force z to be m_microphoneDesiredHeightAboveTerrain
Real zScale = desiredHeight / groundToCameraVector.z;
//Use the smallest of the two scale calculations
bestScaleFactor = MIN( maxPercentage, zScale );
}
//Now apply the best scalar to the ground-to-camera vector.
groundToCameraVector.scale( bestScaleFactor );
//Set the microphone to be the ground position adjusted for terrain plus the vector we just calculated.
groundPos.z = TheTerrainLogic->getGroundHeight( groundPos.x, groundPos.y );
microphonePos.set( &groundPos );
microphonePos.add( &groundToCameraVector );
//Viola! A properly placed microphone.
setListenerPosition( µphonePos, &lookTo );
//Now determine if we would like to boost the volume based on the camera being close to the microphone!
Real maxBoostScalar = m_audioSettings->m_zoomSoundVolumePercentageAmount;
Real minDist = m_audioSettings->m_zoomMinDistance;
Real maxDist = m_audioSettings->m_zoomMaxDistance;
//We can't boost a sound above 100%, instead reduce the normal sound level.
m_zoomVolume = 1.0f - maxBoostScalar;
//Are we even using a boost?
if( maxBoostScalar > 0.0f )
{
//How far away is the camera from the microphone?
Coord3D vector = cameraPos;
vector.sub( µphonePos );
Real dist = vector.length();
if( dist < minDist )
{
//Max volume!
m_zoomVolume = 1.0f;
}
else if( dist < maxDist )
{
//Determine what the boost amount will be.
Real scalar = (dist - minDist) / (maxDist - minDist);
m_zoomVolume = 1.0f - scalar * maxBoostScalar;
}
}
set3DVolumeAdjustment( m_zoomVolume );
}
//-------------------------------------------------------------------------------------------------
void AudioManager::getInfoForAudioEvent( const AudioEventRTS *eventToFindAndFill ) const
{
if (!eventToFindAndFill) {
return;
}
if (eventToFindAndFill->getAudioEventInfo()) {
// already done
return;
}
eventToFindAndFill->setAudioEventInfo(findAudioEventInfo(eventToFindAndFill->getEventName()));
}
//-------------------------------------------------------------------------------------------------
AudioHandle AudioManager::addAudioEvent(const AudioEventRTS *eventToAdd)
{
if (eventToAdd->getEventName().isEmpty() || eventToAdd->getEventName() == "NoSound") {
return AHSV_NoSound;
}
#ifdef INTENSIVE_AUDIO_DEBUG
DEBUG_LOG(("AUDIO (%d): Received addAudioEvent('%s')", TheGameLogic->getFrame(), eventToAdd->getEventName().str()));
#endif
if (!eventToAdd->getAudioEventInfo()) {
getInfoForAudioEvent(eventToAdd);
if (!eventToAdd->getAudioEventInfo()) {
DEBUG_CRASH(("No info for requested audio event '%s'", eventToAdd->getEventName().str()));
return AHSV_Error;
}
}
const AudioType soundType = eventToAdd->getAudioEventInfo()->m_soundType;
// Check if audio type is on
// TheSuperHackers @info Zero audio volume is not a fail condition, because music, speech and sounds
// still need to be in flight in case the user raises the volume on runtime after the audio was already triggered.
switch (soundType)
{
case AT_Music:
if (!isOn(AudioAffect_Music))
return AHSV_NoSound;
break;
case AT_SoundEffect:
if (!isOn(AudioAffect_Sound) || !isOn(AudioAffect_Sound3D))
return AHSV_NoSound;
break;
case AT_Streaming:
// if we're currently playing uninterruptable speech, then disallow the addition of this sample
if (getDisallowSpeech())
return AHSV_NoSound;
if (!isOn(AudioAffect_Speech))
return AHSV_NoSound;
break;
}
// TheSuperHackers @info Scripted audio events are logical, i.e. synchronized across clients.
// In retail mode this early return cannot be taken for such audio events as it skips code that changes the logical game seed values.
// In non-retail mode logical audio events are decoupled from the CRC computation, so this early return is allowed.
#if RETAIL_COMPATIBLE_CRC
const Bool logicalAudio = eventToAdd->getIsLogicalAudio();
#else
const Bool logicalAudio = FALSE;
#endif
const Bool notForLocal = !eventToAdd->getUninterruptible() && !shouldPlayLocally(eventToAdd);
if (!logicalAudio && notForLocal)
{
return AHSV_NotForLocal;
}
AudioEventRTS *audioEvent = MSGNEW("AudioEventRTS") AudioEventRTS(*eventToAdd); // poolify
audioEvent->setPlayingHandle( allocateNewHandle() );
audioEvent->generateFilename(); // which file are we actually going to play?
eventToAdd->setPlayingAudioIndex( audioEvent->getPlayingAudioIndex() );
audioEvent->generatePlayInfo(); // generate pitch shift and volume shift now as well
std::list<std::pair<AsciiString, Real> >::iterator it;
for (it = m_adjustedVolumes.begin(); it != m_adjustedVolumes.end(); ++it) {
if (it->first == audioEvent->getEventName()) {
audioEvent->setVolume(it->second);
break;
}
}
#if RETAIL_COMPATIBLE_CRC
if (notForLocal)
{
releaseAudioEventRTS(audioEvent);
return AHSV_NotForLocal;
}
#endif
// cull muted audio
if (audioEvent->getVolume() < m_audioSettings->m_minVolume) {
#ifdef INTENSIVE_AUDIO_DEBUG
DEBUG_LOG((" - culled due to muting (%d).", audioEvent->getVolume()));
#endif
releaseAudioEventRTS(audioEvent);
return AHSV_Muted;
}
if (soundType == AT_Music)
{
m_music->addAudioEvent(audioEvent);
}
else
{
//Possible to nuke audioEvent inside.
m_sound->addAudioEvent(audioEvent);
}
if( audioEvent )
{
return audioEvent->getPlayingHandle();
}
return AHSV_NoSound;
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isValidAudioEvent(const AudioEventRTS *eventToCheck) const
{
if (eventToCheck->getEventName().isEmpty()) {
return false;
}
getInfoForAudioEvent(eventToCheck);
return (eventToCheck->getAudioEventInfo() != nullptr);
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isValidAudioEvent( AudioEventRTS *eventToCheck ) const
{
if( eventToCheck->getEventName().isEmpty() )
{
return false;
}
getInfoForAudioEvent( eventToCheck );
return( eventToCheck->getAudioEventInfo() );
}
//-------------------------------------------------------------------------------------------------
void AudioManager::addTrackName( const AsciiString& trackName )
{
m_musicTracks.push_back(trackName);
}
//-------------------------------------------------------------------------------------------------
AsciiString AudioManager::nextTrackName(const AsciiString& currentTrack )
{
std::vector<AsciiString>::iterator it;
for (it = m_musicTracks.begin(); it != m_musicTracks.end(); ++it) {
if (*it == currentTrack) {
break;
}
}
if (it != m_musicTracks.end()) {
++it;
}
if (it == m_musicTracks.end()) {
it = m_musicTracks.begin();
if (it == m_musicTracks.end()) {
return AsciiString::TheEmptyString;
}
}
return *it;
}
//-------------------------------------------------------------------------------------------------
AsciiString AudioManager::prevTrackName(const AsciiString& currentTrack )
{
std::vector<AsciiString>::reverse_iterator rit;
for (rit = m_musicTracks.rbegin(); rit != m_musicTracks.rend(); ++rit) {
if (*rit == currentTrack) {
break;
}
}
if (rit != m_musicTracks.rend()) {
++rit;
}
if (rit == m_musicTracks.rend()) {
rit = m_musicTracks.rbegin();
if (rit == m_musicTracks.rend()) {
return AsciiString::TheEmptyString;
}
}
return *rit;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::removeAudioEvent(AudioHandle audioEvent)
{
if (audioEvent == AHSV_StopTheMusic || audioEvent == AHSV_StopTheMusicFade) {
m_music->removeAudioEvent(audioEvent);
return;
}
if (audioEvent < AHSV_FirstHandle) {
return;
}
AudioRequest *req = allocateAudioRequest( false );
req->m_handleToInteractOn = audioEvent;
req->m_request = AR_Stop;
appendAudioRequest( req );
}
//-------------------------------------------------------------------------------------------------
void AudioManager::setAudioEventEnabled( AsciiString eventToAffect, Bool enable )
{
setAudioEventVolumeOverride(eventToAffect, (enable ? -1.0f : 0.0f) );
}
//-------------------------------------------------------------------------------------------------
void AudioManager::setAudioEventVolumeOverride( AsciiString eventToAffect, Real newVolume )
{
if (eventToAffect == AsciiString::TheEmptyString) {
m_adjustedVolumes.clear();
return;
}
// Find any playing audio events and adjust their volume accordingly.
if (newVolume != -1.0f) {
adjustVolumeOfPlayingAudio(eventToAffect, newVolume);
}
std::list<std::pair<AsciiString, Real> >::iterator it;
for (it = m_adjustedVolumes.begin(); it != m_adjustedVolumes.end(); ++it) {
if (it->first == eventToAffect) {
if (newVolume == -1.0f) {
m_adjustedVolumes.erase(it);
return;
} else {
it->second = newVolume;
return;
}
}
}
if (newVolume != -1.0f) {
std::pair<AsciiString, Real> newPair;
newPair.first = eventToAffect;
newPair.second = newVolume;
m_adjustedVolumes.push_front(newPair);
}
}
//-------------------------------------------------------------------------------------------------
void AudioManager::removeAudioEvent( AsciiString eventToRemove )
{
removePlayingAudio( eventToRemove );
}
//-------------------------------------------------------------------------------------------------
void AudioManager::removeDisabledEvents()
{
removeAllDisabledAudio();
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isCurrentlyPlaying( AudioHandle audioEvent )
{
return true;
}
//-------------------------------------------------------------------------------------------------
UnsignedInt AudioManager::translateSpeakerTypeToUnsignedInt( const AsciiString& speakerType )
{
for (UnsignedInt i = 0; TheSpeakerTypes[i]; ++i) {
if (TheSpeakerTypes[i] == speakerType) {
return i;
}
}
return 0;
}
//-------------------------------------------------------------------------------------------------
AsciiString AudioManager::translateUnsignedIntToSpeakerType( UnsignedInt speakerType )
{
if (speakerType >= TheSpeakerTypesCount) {
return TheSpeakerTypes[0];
}
return TheSpeakerTypes[speakerType];
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isOn( AudioAffect whichToGet ) const
{
if (whichToGet & AudioAffect_Music) {
return m_musicOn;
} else if (whichToGet & AudioAffect_Sound) {
return m_soundOn;
} else if (whichToGet & AudioAffect_Sound3D) {
return m_sound3DOn;
}
// Speech
return m_speechOn;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::setOn( Bool turnOn, AudioAffect whichToAffect )
{
if (whichToAffect & AudioAffect_Music) {
m_musicOn = turnOn;
}
if (whichToAffect & AudioAffect_Sound) {
m_soundOn = turnOn;
}
if (whichToAffect & AudioAffect_Sound3D) {
m_sound3DOn = turnOn;
}
if (whichToAffect & AudioAffect_Speech) {
m_speechOn = turnOn;
}
}
//-------------------------------------------------------------------------------------------------
void AudioManager::setVolume( Real volume, AudioAffect whichToAffect )
{
if (whichToAffect & AudioAffect_Music) {
if (whichToAffect & AudioAffect_SystemSetting) {
m_systemMusicVolume = volume;
} else {
m_scriptMusicVolume = volume;
}
m_musicVolume = m_scriptMusicVolume * m_systemMusicVolume;
}
if (whichToAffect & AudioAffect_Sound) {
if (whichToAffect & AudioAffect_SystemSetting) {
m_systemSoundVolume = volume;
} else {
m_scriptSoundVolume = volume;
}
m_soundVolume = m_scriptSoundVolume * m_systemSoundVolume;
}
if (whichToAffect & AudioAffect_Sound3D) {
if (whichToAffect & AudioAffect_SystemSetting) {
m_systemSound3DVolume = volume;
} else {
m_scriptSound3DVolume = volume;
}
m_sound3DVolume = m_scriptSound3DVolume * m_systemSound3DVolume;
}
if (whichToAffect & AudioAffect_Speech) {
if (whichToAffect & AudioAffect_SystemSetting) {
m_systemSpeechVolume = volume;
} else {
m_scriptSpeechVolume = volume;
}
m_speechVolume = m_scriptSpeechVolume * m_systemSpeechVolume;
}
m_volumeHasChanged = true;
}
//-------------------------------------------------------------------------------------------------
Real AudioManager::getVolume( AudioAffect whichToGet )
{
if (whichToGet & AudioAffect_Music) {
return m_musicVolume;
} else if (whichToGet & AudioAffect_Sound) {
return m_soundVolume;
} else if (whichToGet & AudioAffect_Sound3D) {
return m_sound3DVolume;
}
// Speech
return m_speechVolume;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::set3DVolumeAdjustment( Real volumeAdjustment )
{
m_sound3DVolume = volumeAdjustment * m_scriptSound3DVolume * m_systemSound3DVolume;
// clamp
if (m_sound3DVolume < 0.0f)
m_sound3DVolume = 0.0f;
if (m_sound3DVolume > 1.0f)
m_sound3DVolume = 1.0f;
if ( ! has3DSensitiveStreamsPlaying() )
m_volumeHasChanged = TRUE;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::setListenerPosition( const Coord3D *newListenerPos, const Coord3D *newListenerOrientation )
{
m_listenerPosition = *newListenerPos;
m_listenerOrientation = *newListenerOrientation;
}
//-------------------------------------------------------------------------------------------------
const Coord3D *AudioManager::getListenerPosition( void ) const
{
return &m_listenerPosition;
}
//-------------------------------------------------------------------------------------------------
AudioRequest *AudioManager::allocateAudioRequest( Bool useAudioEvent )
{
AudioRequest *audioReq = newInstance(AudioRequest);
audioReq->m_usePendingEvent = useAudioEvent;
audioReq->m_requiresCheckForSample = false;
return audioReq;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::releaseAudioRequest( AudioRequest *requestToRelease )
{
deleteInstance(requestToRelease);
}
//-------------------------------------------------------------------------------------------------
void AudioManager::appendAudioRequest( AudioRequest *m_request )
{
m_audioRequests.push_back(m_request);
}
//-------------------------------------------------------------------------------------------------
// Remove all pending audio requests
void AudioManager::removeAllAudioRequests( void )
{
std::list<AudioRequest*>::iterator it;
for ( it = m_audioRequests.begin(); it != m_audioRequests.end(); it++ ) {
releaseAudioRequest( *it );
}
m_audioRequests.clear();
}
//-------------------------------------------------------------------------------------------------
void AudioManager::processRequestList( void )
{
}
//-------------------------------------------------------------------------------------------------
AudioEventInfo *AudioManager::newAudioEventInfo( AsciiString audioName )
{
AudioEventInfo *eventInfo = findAudioEventInfo(audioName);
if (eventInfo) {
DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd", audioName.str()));
return eventInfo;
}
m_allAudioEventInfo[audioName] = newInstance(AudioEventInfo);
return m_allAudioEventInfo[audioName];
}
//-------------------------------------------------------------------------------------------------
// Add an AudioEventInfo structure allocated elsewhere to the audio event list
void AudioManager::addAudioEventInfo( AudioEventInfo * newEvent )
{
// Warning: Don't try to copy the structure. It may be a derived class
AudioEventInfo *eventInfo = findAudioEventInfo( newEvent->m_audioName );
if (eventInfo)
{
DEBUG_CRASH(("Requested add of '%s' multiple times. Is this intentional? - jkmcd", newEvent->m_audioName.str()));
*eventInfo = *newEvent;
}
else
{
m_allAudioEventInfo[newEvent->m_audioName] = newEvent;
}
}
//-------------------------------------------------------------------------------------------------
AudioEventInfo *AudioManager::findAudioEventInfo( AsciiString eventName ) const
{
AudioEventInfoHash::const_iterator it;
it = m_allAudioEventInfo.find(eventName);
if (it == m_allAudioEventInfo.end()) {
return nullptr;
}
return (*it).second;
}
//-------------------------------------------------------------------------------------------------
// Remove all AudioEventInfo's with the m_isLevelSpecific flag
void AudioManager::removeLevelSpecificAudioEventInfos(void)
{
AudioEventInfoHash::iterator it = m_allAudioEventInfo.begin();
while ( it != m_allAudioEventInfo.end() )
{
AudioEventInfoHash::iterator next = it; // Make sure erase doesn't cause problems
next++;
if ( it->second->isLevelSpecific() )
{
deleteInstance(it->second);
m_allAudioEventInfo.erase( it );
}
it = next;
}
}
//-------------------------------------------------------------------------------------------------
const AudioSettings *AudioManager::getAudioSettings( void ) const
{
return m_audioSettings;
}
//-------------------------------------------------------------------------------------------------
AudioSettings *AudioManager::friend_getAudioSettings( void )
{
return m_audioSettings;
}
//-------------------------------------------------------------------------------------------------
const MiscAudio *AudioManager::getMiscAudio( void ) const
{
return m_miscAudio;
}
//-------------------------------------------------------------------------------------------------
MiscAudio *AudioManager::friend_getMiscAudio( void )
{
return m_miscAudio;
}
//-------------------------------------------------------------------------------------------------
const FieldParse *AudioManager::getFieldParseTable( void ) const
{
return audioSettingsFieldParseTable;
}
//-------------------------------------------------------------------------------------------------
void AudioManager::refreshCachedVariables()
{
m_hardwareAccel = isCurrentProviderHardwareAccelerated();
m_surroundSpeakers = isCurrentSpeakerTypeSurroundSound();
}
//-------------------------------------------------------------------------------------------------
Real AudioManager::getAudioLengthMS( const AudioEventRTS *event )
{
if (!event->getAudioEventInfo()) {
getInfoForAudioEvent(event);
if (!event->getAudioEventInfo()) {
return 0.0f;
}
}
AudioEventRTS tmpEvent = *event;
tmpEvent.generateFilename();
tmpEvent.generatePlayInfo();
return getFileLengthMS(tmpEvent.getAttackFilename()) +
getFileLengthMS(tmpEvent.getFilename()) +
getFileLengthMS(tmpEvent.getDecayFilename());
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isMusicAlreadyLoaded(void) const
{
const AudioEventInfo *musicToLoad = nullptr;
AudioEventInfoHash::const_iterator it;
for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
if (it->second) {
const AudioEventInfo *aet = it->second;
if (aet->m_soundType == AT_Music) {
musicToLoad = aet;
}
}
}
if (!musicToLoad) {
return FALSE;
}
AudioEventRTS aud;
aud.setAudioEventInfo(musicToLoad);
aud.generateFilename();
AsciiString astr = aud.getFilename();
return (TheFileSystem->doesFileExist(astr.str()));
}
//-------------------------------------------------------------------------------------------------
void AudioManager::findAllAudioEventsOfType( AudioType audioType, std::vector<AudioEventInfo*>& allEvents )
{
AudioEventInfoHashIt it;
for (it = m_allAudioEventInfo.begin(); it != m_allAudioEventInfo.end(); ++it) {
AudioEventInfo *aud = (*it).second;
if (aud->m_soundType == audioType) {
allEvents.push_back(aud);
}
}
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isCurrentProviderHardwareAccelerated()
{
for (Int i = 0; i < MAX_HW_PROVIDERS; ++i) {
if (getProviderName(getSelectedProvider()) == m_audioSettings->m_preferred3DProvider[i]) {
return TRUE;
}
}
return FALSE;
}
//-------------------------------------------------------------------------------------------------
Bool AudioManager::isCurrentSpeakerTypeSurroundSound()
{
return (getSpeakerType() == m_audioSettings->m_defaultSpeakerType3D);
}