forked from RigsOfRods/rigs-of-rods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundManager.cpp
1645 lines (1444 loc) · 76.8 KB
/
SoundManager.cpp
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
/*
This source file is part of Rigs of Rods
Copyright 2005-2012 Pierre-Michel Ricordel
Copyright 2007-2012 Thomas Fischer
Copyright 2013-2020 Petr Ohlidal
For more information, see http://www.rigsofrods.org/
Rigs of Rods is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, as
published by the Free Software Foundation.
Rigs of Rods 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 Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_OPENAL
#include "SoundManager.h"
#include "AeroEngine.h"
#include "Application.h"
#include "IWater.h"
#include "ScrewProp.h"
#include "Sound.h"
#include <OgreResourceGroupManager.h>
#define LOGSTREAM Ogre::LogManager::getSingleton().stream() << "[RoR|Audio] "
bool _checkALErrors(const char* filename, int linenum)
{
int err = alGetError();
if (err != AL_NO_ERROR)
{
char buf[1000] = {};
snprintf(buf, 1000, "OpenAL Error: %s (0x%x), @ %s:%d", alGetString(err), err, filename, linenum);
LOGSTREAM << buf;
return true;
}
return false;
}
#define hasALErrors() _checkALErrors(__FILE__, __LINE__)
using namespace RoR;
using namespace Ogre;
const float SoundManager::MAX_DISTANCE = 500.0f;
const float SoundManager::ROLLOFF_FACTOR = 1.0f;
const float SoundManager::REFERENCE_DISTANCE = 7.5f;
SoundManager::SoundManager()
{
if (App::audio_device_name->getStr() == "")
{
LOGSTREAM << "No audio device configured, opening default.";
audio_device = alcOpenDevice(nullptr);
}
else
{
audio_device = alcOpenDevice(App::audio_device_name->getStr().c_str());
if (!audio_device)
{
LOGSTREAM << "Failed to open configured audio device \"" << App::audio_device_name->getStr() << "\", opening default.";
App::audio_device_name->setStr("");
audio_device = alcOpenDevice(nullptr);
}
}
if (!audio_device)
{
LOGSTREAM << "Failed to open default audio device. Sound disabled.";
hasALErrors();
return;
}
sound_context = alcCreateContext(audio_device, NULL);
if (!sound_context)
{
alcCloseDevice(audio_device);
audio_device = NULL;
hasALErrors();
return;
}
alcMakeContextCurrent(sound_context);
if (alGetString(AL_VENDOR)) LOG("SoundManager: OpenAL vendor is: " + String(alGetString(AL_VENDOR)));
if (alGetString(AL_VERSION)) LOG("SoundManager: OpenAL version is: " + String(alGetString(AL_VERSION)));
if (alGetString(AL_RENDERER)) LOG("SoundManager: OpenAL renderer is: " + String(alGetString(AL_RENDERER)));
if (alGetString(AL_EXTENSIONS)) LOG("SoundManager: OpenAL extensions are: " + String(alGetString(AL_EXTENSIONS)));
if (alcGetString(audio_device, ALC_DEVICE_SPECIFIER)) LOG("SoundManager: OpenAL device is: " + String(alcGetString(audio_device, ALC_DEVICE_SPECIFIER)));
if (alcGetString(audio_device, ALC_EXTENSIONS)) LOG("SoundManager: OpenAL ALC extensions are: " + String(alcGetString(audio_device, ALC_EXTENSIONS)));
// initialize use of OpenAL EFX extensions
m_efx_is_available = alcIsExtensionPresent(audio_device, "ALC_EXT_EFX");
if (m_efx_is_available)
{
LOG("SoundManager: Found OpenAL EFX extension");
// Get OpenAL function pointers
alGenEffects = (LPALGENEFFECTS)alGetProcAddress("alGenEffects");
alDeleteEffects = (LPALDELETEEFFECTS)alGetProcAddress("alDeleteEffects");
alIsEffect = (LPALISEFFECT)alGetProcAddress("alIsEffect");
alEffecti = (LPALEFFECTI)alGetProcAddress("alEffecti");
alEffectf = (LPALEFFECTF)alGetProcAddress("alEffectf");
alEffectfv = (LPALEFFECTFV)alGetProcAddress("alEffectfv");
alGenFilters = (LPALGENFILTERS)alGetProcAddress("alGenFilters");
alDeleteFilters = (LPALDELETEFILTERS)alGetProcAddress("alDeleteFilters");
alIsFilter = (LPALISFILTER)alGetProcAddress("alIsFilter");
alFilteri = (LPALFILTERI)alGetProcAddress("alFilteri");
alFilterf = (LPALFILTERF)alGetProcAddress("alFilterf");
alGenAuxiliaryEffectSlots = (LPALGENAUXILIARYEFFECTSLOTS)alGetProcAddress("alGenAuxiliaryEffectSlots");
alDeleteAuxiliaryEffectSlots = (LPALDELETEAUXILIARYEFFECTSLOTS)alGetProcAddress("alDeleteAuxiliaryEffectSlots");
alIsAuxiliaryEffectSlot = (LPALISAUXILIARYEFFECTSLOT)alGetProcAddress("alIsAuxiliaryEffectSlot");
alAuxiliaryEffectSloti = (LPALAUXILIARYEFFECTSLOTI)alGetProcAddress("alAuxiliaryEffectSloti");
alAuxiliaryEffectSlotf = (LPALAUXILIARYEFFECTSLOTF)alGetProcAddress("alAuxiliaryEffectSlotf");
alAuxiliaryEffectSlotfv = (LPALAUXILIARYEFFECTSLOTFV)alGetProcAddress("alAuxiliaryEffectSlotfv");
if (App::audio_enable_efx->getBool())
{
// allow user to change reverb engines at will
switch (App::audio_efx_reverb_engine->getEnum<EfxReverbEngine>())
{
case EfxReverbEngine::EAXREVERB: m_efx_reverb_engine = EfxReverbEngine::EAXREVERB; break;
case EfxReverbEngine::REVERB: m_efx_reverb_engine = EfxReverbEngine::REVERB; break;
default:
m_efx_reverb_engine = EfxReverbEngine::NONE;
LOG("SoundManager: Reverb engine disabled");
}
if (m_efx_reverb_engine == EfxReverbEngine::EAXREVERB)
{
if (alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
{
LOG("SoundManager: OpenAL driver supports AL_EFFECT_EAXREVERB, using it");
}
else
{
LOG("SoundManager: AL_EFFECT_EAXREVERB requested but OpenAL driver does not support it, falling back to standard reverb. Advanced features, such as reflection panning, will not be available");
m_efx_reverb_engine = EfxReverbEngine::REVERB;
}
}
else if (m_efx_reverb_engine == EfxReverbEngine::REVERB)
{
LOG("SoundManager: Using OpenAL standard reverb");
}
// create effect slot for the listener
if (!this->alIsAuxiliaryEffectSlot(m_listener_slot))
{
alGetError();
this->alGenAuxiliaryEffectSlots(1, &m_listener_slot);
ALuint error = alGetError();
if (error != AL_NO_ERROR)
{
LOG("SoundManager: alGenAuxiliaryEffectSlots for listener_slot failed: " + TOSTRING(alGetString(error)));
m_listener_slot = AL_EFFECTSLOT_NULL;
}
}
this->PrepopulateEfxPropertiesMap();
/*
Create filter for obstruction
Currently we don't check for how much high-frequency content the obstacle
lets through. We assume it's a hard surface with significant absorption
of high frequencies (which should be true for trucks, buildings and terrain).
*/
alGetError();
this->alGenFilters(1, &m_efx_outdoor_obstruction_lowpass_filter_id);
ALuint e = alGetError();
if (e != AL_NO_ERROR)
{
m_efx_outdoor_obstruction_lowpass_filter_id = AL_FILTER_NULL;
}
else
{
this->alFilteri(m_efx_outdoor_obstruction_lowpass_filter_id, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
this->alFilterf(m_efx_outdoor_obstruction_lowpass_filter_id, AL_LOWPASS_GAIN, 0.33f);
this->alFilterf(m_efx_outdoor_obstruction_lowpass_filter_id, AL_LOWPASS_GAINHF, 0.25f);
}
/*
Create wet path filter for occlusion
Currently we don't check for how much high-frequency content the encompassing walls
let through. We assume it's a hard surface with significant absorption
of high frequencies (which should be true for most types of buildings).
*/
alGetError();
this->alGenFilters(1, &m_efx_occlusion_wet_path_lowpass_filter_id);
e = alGetError();
if (e != AL_NO_ERROR)
{
m_efx_occlusion_wet_path_lowpass_filter_id = AL_FILTER_NULL;
}
else
{
this->alFilteri(m_efx_occlusion_wet_path_lowpass_filter_id, AL_FILTER_TYPE, AL_FILTER_LOWPASS);
this->alFilterf(m_efx_occlusion_wet_path_lowpass_filter_id, AL_LOWPASS_GAIN, 0.33f);
this->alFilterf(m_efx_occlusion_wet_path_lowpass_filter_id, AL_LOWPASS_GAINHF, 0.25f);
}
}
}
else
{
LOG("SoundManager: OpenAL EFX extension not found, disabling EFX");
App::audio_enable_efx->setVal(false);
}
// generate the AL sources
for (hardware_sources_num = 0; hardware_sources_num < MAX_HARDWARE_SOURCES; hardware_sources_num++)
{
alGetError();
alGenSources(1, &hardware_sources[hardware_sources_num]);
if (alGetError() != AL_NO_ERROR)
break;
alSourcef(hardware_sources[hardware_sources_num], AL_REFERENCE_DISTANCE, REFERENCE_DISTANCE);
alSourcef(hardware_sources[hardware_sources_num], AL_ROLLOFF_FACTOR, ROLLOFF_FACTOR);
alSourcef(hardware_sources[hardware_sources_num], AL_MAX_DISTANCE, MAX_DISTANCE);
// connect source to listener slot effect
if (App::audio_enable_efx->getBool())
{
alSource3i(hardware_sources[hardware_sources_num], AL_AUXILIARY_SEND_FILTER, m_listener_slot, 0, AL_FILTER_NULL);
}
}
alDopplerFactor(App::audio_doppler_factor->getFloat());
alSpeedOfSound(343.3f);
for (int i = 0; i < MAX_HARDWARE_SOURCES; i++)
{
hardware_sources_map[i] = -1;
}
}
SoundManager::~SoundManager()
{
// delete the sources and buffers
alDeleteSources(MAX_HARDWARE_SOURCES, hardware_sources);
alDeleteBuffers(MAX_AUDIO_BUFFERS, audio_buffers);
if (m_efx_is_available)
{
if (this->alIsFilter(m_efx_outdoor_obstruction_lowpass_filter_id))
{
this->alDeleteFilters(1, &m_efx_outdoor_obstruction_lowpass_filter_id);
}
if (this->alIsFilter(m_efx_occlusion_wet_path_lowpass_filter_id))
{
this->alDeleteFilters(1, &m_efx_occlusion_wet_path_lowpass_filter_id);
}
if (this->alIsAuxiliaryEffectSlot(m_listener_slot))
{
this->alAuxiliaryEffectSloti(m_listener_slot, AL_EFFECTSLOT_EFFECT, AL_EFFECTSLOT_NULL);
this->alDeleteAuxiliaryEffectSlots(1, &m_listener_slot);
m_listener_slot = 0;
}
}
CleanUp();
// destroy the sound context and device
sound_context = alcGetCurrentContext();
audio_device = alcGetContextsDevice(sound_context);
alcMakeContextCurrent(NULL);
alcDestroyContext(sound_context);
if (audio_device)
{
alcCloseDevice(audio_device);
}
LOG("SoundManager destroyed.");
}
void SoundManager::CleanUp()
{
if (m_efx_is_available)
{
m_listener_efx_reverb_properties = nullptr;
if (this->alIsAuxiliaryEffectSlot(m_listener_slot))
{
this->alAuxiliaryEffectSloti(m_listener_slot, AL_EFFECTSLOT_EFFECT, AL_EFFECTSLOT_NULL);
}
for (auto it = m_efx_effect_id_map.begin(); it != m_efx_effect_id_map.end();)
{
this->DeleteAlEffect(it->second);
it = m_efx_effect_id_map.erase(it);
}
}
// TODO: Delete Sounds and buffers
}
const EFXEAXREVERBPROPERTIES* SoundManager::GetEfxProperties(const std::string& efx_preset_name) const
{
const auto it = m_efx_properties_map.find(efx_preset_name);
if (it != m_efx_properties_map.end())
{
return &it->second;
}
else
{
return nullptr;
}
}
void SoundManager::PrepopulateEfxPropertiesMap()
{
m_efx_properties_map["EFX_REVERB_PRESET_GENERIC"] = EFX_REVERB_PRESET_GENERIC;
m_efx_properties_map["EFX_REVERB_PRESET_ROOM"] = EFX_REVERB_PRESET_ROOM;
m_efx_properties_map["EFX_REVERB_PRESET_CAVE"] = EFX_REVERB_PRESET_CAVE;
m_efx_properties_map["EFX_REVERB_PRESET_ARENA"] = EFX_REVERB_PRESET_ARENA;
m_efx_properties_map["EFX_REVERB_PRESET_HANGAR"] = EFX_REVERB_PRESET_HANGAR;
m_efx_properties_map["EFX_REVERB_PRESET_ALLEY"] = EFX_REVERB_PRESET_ALLEY;
m_efx_properties_map["EFX_REVERB_PRESET_HALLWAY"] = EFX_REVERB_PRESET_HALLWAY;
m_efx_properties_map["EFX_REVERB_PRESET_FOREST"] = EFX_REVERB_PRESET_FOREST;
m_efx_properties_map["EFX_REVERB_PRESET_CITY"] = EFX_REVERB_PRESET_CITY;
m_efx_properties_map["EFX_REVERB_PRESET_MOUNTAINS"] = EFX_REVERB_PRESET_MOUNTAINS;
m_efx_properties_map["EFX_REVERB_PRESET_QUARRY"] = EFX_REVERB_PRESET_QUARRY;
m_efx_properties_map["EFX_REVERB_PRESET_PLAIN"] = EFX_REVERB_PRESET_PLAIN;
m_efx_properties_map["EFX_REVERB_PRESET_PARKINGLOT"] = EFX_REVERB_PRESET_PARKINGLOT;
m_efx_properties_map["EFX_REVERB_PRESET_UNDERWATER"] = EFX_REVERB_PRESET_UNDERWATER;
m_efx_properties_map["EFX_REVERB_PRESET_DRUGGED"] = EFX_REVERB_PRESET_DRUGGED;
m_efx_properties_map["EFX_REVERB_PRESET_DIZZY"] = EFX_REVERB_PRESET_DIZZY;
m_efx_properties_map["EFX_REVERB_PRESET_CASTLE_SHORTPASSAGE"] = EFX_REVERB_PRESET_CASTLE_SHORTPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_CASTLE_LARGEROOM"] = EFX_REVERB_PRESET_CASTLE_LARGEROOM;
m_efx_properties_map["EFX_REVERB_PRESET_CASTLE_LONGPASSAGE"] = EFX_REVERB_PRESET_CASTLE_LONGPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_CASTLE_HALL"] = EFX_REVERB_PRESET_CASTLE_HALL;
m_efx_properties_map["EFX_REVERB_PRESET_CASTLE_COURTYARD"] = EFX_REVERB_PRESET_CASTLE_COURTYARD;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_SMALLROOM"] = EFX_REVERB_PRESET_FACTORY_SMALLROOM;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_SHORTPASSAGE"] = EFX_REVERB_PRESET_FACTORY_SHORTPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_MEDIUMROOM"] = EFX_REVERB_PRESET_FACTORY_MEDIUMROOM;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_LARGEROOM"] = EFX_REVERB_PRESET_FACTORY_LARGEROOM;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_LONGPASSAGE"] = EFX_REVERB_PRESET_FACTORY_LONGPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_HALL"] = EFX_REVERB_PRESET_FACTORY_HALL;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_COURTYARD"] = EFX_REVERB_PRESET_FACTORY_COURTYARD;
m_efx_properties_map["EFX_REVERB_PRESET_FACTORY_ALCOVE"] = EFX_REVERB_PRESET_FACTORY_ALCOVE;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_SMALLROOM"] = EFX_REVERB_PRESET_SPACESTATION_SMALLROOM;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_SHORTPASSAGE"] = EFX_REVERB_PRESET_SPACESTATION_SHORTPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_MEDIUMROOM"] = EFX_REVERB_PRESET_SPACESTATION_MEDIUMROOM;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_LARGEROOM"] = EFX_REVERB_PRESET_SPACESTATION_LARGEROOM;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_LONGPASSAGE"] = EFX_REVERB_PRESET_SPACESTATION_LONGPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_SPACESTATION_HALL"] = EFX_REVERB_PRESET_SPACESTATION_HALL;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_SMALLROOM"] = EFX_REVERB_PRESET_WOODEN_SMALLROOM;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_SHORTPASSAGE"] = EFX_REVERB_PRESET_WOODEN_SHORTPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_MEDIUMROOM"] = EFX_REVERB_PRESET_WOODEN_MEDIUMROOM;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_LARGEROOM"] = EFX_REVERB_PRESET_WOODEN_LARGEROOM;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_LONGPASSAGE"] = EFX_REVERB_PRESET_WOODEN_LONGPASSAGE;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_HALL"] = EFX_REVERB_PRESET_WOODEN_HALL;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_COURTYARD"] = EFX_REVERB_PRESET_WOODEN_COURTYARD;
m_efx_properties_map["EFX_REVERB_PRESET_WOODEN_ALCOVE"] = EFX_REVERB_PRESET_WOODEN_ALCOVE;
m_efx_properties_map["EFX_REVERB_PRESET_SPORT_EMPTYSTADIUM"] = EFX_REVERB_PRESET_SPORT_EMPTYSTADIUM;
m_efx_properties_map["EFX_REVERB_PRESET_SPORT_FULLSTADIUM"] = EFX_REVERB_PRESET_SPORT_FULLSTADIUM;
m_efx_properties_map["EFX_REVERB_PRESET_SPORT_STADIUMTANNOY"] = EFX_REVERB_PRESET_SPORT_STADIUMTANNOY;
m_efx_properties_map["EFX_REVERB_PRESET_PREFAB_WORKSHOP"] = EFX_REVERB_PRESET_PREFAB_WORKSHOP;
m_efx_properties_map["EFX_REVERB_PRESET_PREFAB_OUTHOUSE"] = EFX_REVERB_PRESET_PREFAB_OUTHOUSE;
m_efx_properties_map["EFX_REVERB_PRESET_PREFAB_CARAVAN"] = EFX_REVERB_PRESET_PREFAB_CARAVAN;
m_efx_properties_map["EFX_REVERB_PRESET_PIPE_LARGE"] = EFX_REVERB_PRESET_PIPE_LARGE;
m_efx_properties_map["EFX_REVERB_PRESET_PIPE_LONGTHIN"] = EFX_REVERB_PRESET_PIPE_LONGTHIN;
m_efx_properties_map["EFX_REVERB_PRESET_PIPE_RESONANT"] = EFX_REVERB_PRESET_PIPE_RESONANT;
m_efx_properties_map["EFX_REVERB_PRESET_OUTDOORS_BACKYARD"] = EFX_REVERB_PRESET_OUTDOORS_BACKYARD;
m_efx_properties_map["EFX_REVERB_PRESET_OUTDOORS_ROLLINGPLAINS"] = EFX_REVERB_PRESET_OUTDOORS_ROLLINGPLAINS;
m_efx_properties_map["EFX_REVERB_PRESET_OUTDOORS_DEEPCANYON"] = EFX_REVERB_PRESET_OUTDOORS_DEEPCANYON;
m_efx_properties_map["EFX_REVERB_PRESET_OUTDOORS_CREEK"] = EFX_REVERB_PRESET_OUTDOORS_CREEK;
m_efx_properties_map["EFX_REVERB_PRESET_OUTDOORS_VALLEY"] = EFX_REVERB_PRESET_OUTDOORS_VALLEY;
m_efx_properties_map["EFX_REVERB_PRESET_MOOD_HEAVEN"] = EFX_REVERB_PRESET_MOOD_HEAVEN;
m_efx_properties_map["EFX_REVERB_PRESET_MOOD_HELL"] = EFX_REVERB_PRESET_MOOD_HELL;
m_efx_properties_map["EFX_REVERB_PRESET_MOOD_MEMORY"] = EFX_REVERB_PRESET_MOOD_MEMORY;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_COMMENTATOR"] = EFX_REVERB_PRESET_DRIVING_COMMENTATOR;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_PITGARAGE"] = EFX_REVERB_PRESET_DRIVING_PITGARAGE;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_INCAR_RACER"] = EFX_REVERB_PRESET_DRIVING_INCAR_RACER;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_INCAR_SPORTS"] = EFX_REVERB_PRESET_DRIVING_INCAR_SPORTS;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_INCAR_LUXURY"] = EFX_REVERB_PRESET_DRIVING_INCAR_LUXURY;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_FULLGRANDSTAND"] = EFX_REVERB_PRESET_DRIVING_FULLGRANDSTAND;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_EMPTYGRANDSTAND"] = EFX_REVERB_PRESET_DRIVING_EMPTYGRANDSTAND;
m_efx_properties_map["EFX_REVERB_PRESET_DRIVING_TUNNEL"] = EFX_REVERB_PRESET_DRIVING_TUNNEL;
m_efx_properties_map["EFX_REVERB_PRESET_CITY_STREETS"] = EFX_REVERB_PRESET_CITY_STREETS;
m_efx_properties_map["EFX_REVERB_PRESET_CITY_SUBWAY"] = EFX_REVERB_PRESET_CITY_SUBWAY;
m_efx_properties_map["EFX_REVERB_PRESET_CITY_UNDERPASS"] = EFX_REVERB_PRESET_CITY_UNDERPASS;
m_efx_properties_map["EFX_REVERB_PRESET_CITY_ABANDONED"] = EFX_REVERB_PRESET_CITY_ABANDONED;
}
void SoundManager::Update(const float dt_sec)
{
if (!audio_device)
return;
this->SetDopplerFactor(App::audio_doppler_factor->getFloat());
const auto water = App::GetGameContext()->GetTerrain()->getWater();
m_listener_is_underwater = (water != nullptr ? water->IsUnderWater(m_listener_position) : false);
this->recomputeAllSources();
this->UpdateAlListener();
this->UpdateListenerEnvironment();
if (App::audio_enable_efx->getBool())
{
// apply filters to sources when appropriate
for (int hardware_index = 0; hardware_index < hardware_sources_num; hardware_index++)
{
// update air absorption factor
alSourcef(hardware_sources[hardware_index], AL_AIR_ABSORPTION_FACTOR, m_air_absorption_factor);
this->UpdateSourceFilters(hardware_index);
}
this->UpdateListenerEffectSlot(dt_sec);
}
if (App::audio_enable_directed_sounds->getBool())
{
this->UpdateDirectedSounds();
}
}
void SoundManager::SetListener(Ogre::Vector3 position, Ogre::Vector3 direction, Ogre::Vector3 up, Ogre::Vector3 velocity)
{
m_listener_position = position;
m_listener_direction = direction;
m_listener_up = up;
m_listener_velocity = velocity;
}
void SoundManager::UpdateListenerEnvironment()
{
const EFXEAXREVERBPROPERTIES* listener_reverb_properties = nullptr;
if (App::audio_engine_controls_environmental_audio->getBool())
{
if (this->ListenerIsUnderwater())
{
this->SetSpeedOfSound(1522.0f); // assume listener is in sea water (i.e. salt water)
/*
* According to the Francois-Garrison formula for frequency-dependant absorption at 5kHz in seawater,
* the absorption should be 0.334 db/km. OpenAL multiplies the Air Absorption Factor with an internal
* value of 0.05dB/m, so we need a factor of 0.00668f.
*/
this->SetAirAbsorptionFactor(0.00668f);
}
else
{
this->SetSpeedOfSound(343.3f); // assume listener is in air at 20° celsius
this->SetAirAbsorptionFactor(1.0f);
}
if (App::audio_enable_efx->getBool())
{
m_listener_efx_reverb_properties = this->GetReverbPresetAt(m_listener_position);
}
}
}
void SoundManager::UpdateAlListener()
{
float orientation[6];
// direction
orientation[0] = m_listener_direction.x;
orientation[1] = m_listener_direction.y;
orientation[2] = m_listener_direction.z;
// up
orientation[3] = m_listener_up.x;
orientation[4] = m_listener_up.y;
orientation[5] = m_listener_up.z;
alListener3f(AL_POSITION, m_listener_position.x, m_listener_position.y, m_listener_position.z);
alListener3f(AL_VELOCITY, m_listener_velocity.x, m_listener_velocity.y, m_listener_velocity.z);
alListenerfv(AL_ORIENTATION, orientation);
}
const EFXEAXREVERBPROPERTIES* SoundManager::GetReverbPresetAt(const Ogre::Vector3 position) const
{
// for the listener we do additional checks
if (position == m_listener_position)
{
if (!App::audio_force_listener_efx_preset->getStr().empty())
{
return this->GetEfxProperties(App::audio_force_listener_efx_preset->getStr());
}
}
const auto water = App::GetGameContext()->GetTerrain()->getWater();
bool position_is_underwater = (water != nullptr ? water->IsUnderWater(position) : false);
if (position_is_underwater)
{
return this->GetEfxProperties("EFX_REVERB_PRESET_UNDERWATER");
}
// check if position is inside a collision box with a reverb_preset assigned to it
for (const collision_box_t& collision_box : App::GetGameContext()->GetTerrain()->GetCollisions()->getCollisionBoxes())
{
if (!collision_box.reverb_preset_name.empty())
{
const Ogre::AxisAlignedBox collision_box_aab = Ogre::AxisAlignedBox(collision_box.lo, collision_box.hi);
if (collision_box_aab.contains(position))
{
return this->GetEfxProperties(collision_box.reverb_preset_name);
}
}
}
if (!App::audio_default_efx_preset->getStr().empty())
{
return this->GetEfxProperties(App::audio_default_efx_preset->getStr());
}
else
{
return nullptr;
}
}
void SoundManager::UpdateListenerEffectSlot(const float dt_sec)
{
if (m_listener_efx_reverb_properties == nullptr)
{
this->SmoothlyUpdateAlAuxiliaryEffectSlot(dt_sec, m_listener_slot, nullptr);
return;
}
EFXEAXREVERBPROPERTIES current_environmental_properties = *m_listener_efx_reverb_properties;
// early reflections panning, delay and strength
if (App::audio_enable_reflection_panning->getBool() && m_efx_reverb_engine == EfxReverbEngine::EAXREVERB)
{
std::tuple<Ogre::Vector3, float, float> target_early_reflections_properties = this->ComputeEarlyReflectionsProperties();
// convert panning vector from RHS to EAXREVERB's LHS
current_environmental_properties.flReflectionsPan[0] = std::get<0>(target_early_reflections_properties).x;
current_environmental_properties.flReflectionsPan[1] = 0;
current_environmental_properties.flReflectionsPan[2] = -std::get<0>(target_early_reflections_properties).z;
current_environmental_properties.flReflectionsGain = std::get<1>(target_early_reflections_properties);
current_environmental_properties.flReflectionsDelay = std::get<2>(target_early_reflections_properties);
}
this->SmoothlyUpdateAlAuxiliaryEffectSlot(dt_sec, m_listener_slot, ¤t_environmental_properties);
}
void SoundManager::SmoothlyUpdateAlAuxiliaryEffectSlot(const float dt_sec, const ALuint slot_id, const EFXEAXREVERBPROPERTIES* target_efx_properties)
{
const float time_to_target = 0.333f; // seconds to reach the target properties from the current properties
// ensure to not exceed limits of reverb parameters if timestep is too large
const float step = std::min(dt_sec / time_to_target, 0.5f);
static std::map<ALuint, EFXEAXREVERBPROPERTIES> current_efx_properties_of_slot;
if (target_efx_properties == nullptr)
{
this->alAuxiliaryEffectSloti(slot_id, AL_EFFECTSLOT_EFFECT, AL_EFFECTSLOT_NULL);
return;
}
const auto it = current_efx_properties_of_slot.find(slot_id);
if (it == current_efx_properties_of_slot.end())
{
// previously unseen effect slot, set a starting point
current_efx_properties_of_slot[slot_id] = *target_efx_properties;
}
ALuint efx_effect_id;
// create new AL effect if not existing
if (m_efx_effect_id_map.find(slot_id) == m_efx_effect_id_map.end())
{
efx_effect_id = this->CreateAlEffect(target_efx_properties);
m_efx_effect_id_map[slot_id] = efx_effect_id;
}
else
{
efx_effect_id = m_efx_effect_id_map.find(slot_id)->second;
}
// compute intermediate step between current and target properties using linear interpolation based on time step
current_efx_properties_of_slot[slot_id] =
{
current_efx_properties_of_slot[slot_id].flDensity + step * (target_efx_properties->flDensity - current_efx_properties_of_slot[slot_id].flDensity),
current_efx_properties_of_slot[slot_id].flDiffusion + step * (target_efx_properties->flDiffusion - current_efx_properties_of_slot[slot_id].flDiffusion),
current_efx_properties_of_slot[slot_id].flGain + step * (target_efx_properties->flGain - current_efx_properties_of_slot[slot_id].flGain),
current_efx_properties_of_slot[slot_id].flGainHF + step * (target_efx_properties->flGainHF - current_efx_properties_of_slot[slot_id].flGainHF),
current_efx_properties_of_slot[slot_id].flGainLF + step * (target_efx_properties->flGainLF - current_efx_properties_of_slot[slot_id].flGainLF),
current_efx_properties_of_slot[slot_id].flDecayTime + step * (target_efx_properties->flDecayTime - current_efx_properties_of_slot[slot_id].flDecayTime),
current_efx_properties_of_slot[slot_id].flDecayHFRatio + step * (target_efx_properties->flDecayHFRatio - current_efx_properties_of_slot[slot_id].flDecayHFRatio),
current_efx_properties_of_slot[slot_id].flDecayLFRatio + step * (target_efx_properties->flDecayLFRatio - current_efx_properties_of_slot[slot_id].flDecayLFRatio),
current_efx_properties_of_slot[slot_id].flReflectionsGain + step * (target_efx_properties->flReflectionsGain - current_efx_properties_of_slot[slot_id].flReflectionsGain),
current_efx_properties_of_slot[slot_id].flReflectionsDelay + step * (target_efx_properties->flReflectionsDelay - current_efx_properties_of_slot[slot_id].flReflectionsDelay),
current_efx_properties_of_slot[slot_id].flReflectionsPan[0] + step * (target_efx_properties->flReflectionsPan[0] - current_efx_properties_of_slot[slot_id].flReflectionsPan[0]),
current_efx_properties_of_slot[slot_id].flReflectionsPan[1] + step * (target_efx_properties->flReflectionsPan[1] - current_efx_properties_of_slot[slot_id].flReflectionsPan[1]),
current_efx_properties_of_slot[slot_id].flReflectionsPan[2] + step * (target_efx_properties->flReflectionsPan[2] - current_efx_properties_of_slot[slot_id].flReflectionsPan[2]),
current_efx_properties_of_slot[slot_id].flLateReverbGain + step * (target_efx_properties->flLateReverbGain - current_efx_properties_of_slot[slot_id].flLateReverbGain),
current_efx_properties_of_slot[slot_id].flLateReverbDelay + step * (target_efx_properties->flLateReverbDelay - current_efx_properties_of_slot[slot_id].flLateReverbDelay),
current_efx_properties_of_slot[slot_id].flLateReverbPan[0] + step * (target_efx_properties->flLateReverbPan[0] - current_efx_properties_of_slot[slot_id].flLateReverbPan[0]),
current_efx_properties_of_slot[slot_id].flLateReverbPan[1] + step * (target_efx_properties->flLateReverbPan[1] - current_efx_properties_of_slot[slot_id].flLateReverbPan[1]),
current_efx_properties_of_slot[slot_id].flLateReverbPan[2] + step * (target_efx_properties->flLateReverbPan[2] - current_efx_properties_of_slot[slot_id].flLateReverbPan[2]),
current_efx_properties_of_slot[slot_id].flEchoTime + step * (target_efx_properties->flEchoTime - current_efx_properties_of_slot[slot_id].flEchoTime),
current_efx_properties_of_slot[slot_id].flEchoDepth + step * (target_efx_properties->flEchoDepth - current_efx_properties_of_slot[slot_id].flEchoDepth),
current_efx_properties_of_slot[slot_id].flModulationTime + step * (target_efx_properties->flModulationTime - current_efx_properties_of_slot[slot_id].flModulationTime),
current_efx_properties_of_slot[slot_id].flModulationDepth + step * (target_efx_properties->flModulationDepth - current_efx_properties_of_slot[slot_id].flModulationDepth),
current_efx_properties_of_slot[slot_id].flAirAbsorptionGainHF + step * (target_efx_properties->flAirAbsorptionGainHF - current_efx_properties_of_slot[slot_id].flAirAbsorptionGainHF),
current_efx_properties_of_slot[slot_id].flHFReference + step * (target_efx_properties->flHFReference - current_efx_properties_of_slot[slot_id].flHFReference),
current_efx_properties_of_slot[slot_id].flLFReference + step * (target_efx_properties->flLFReference - current_efx_properties_of_slot[slot_id].flLFReference),
current_efx_properties_of_slot[slot_id].flRoomRolloffFactor + step * (target_efx_properties->flRoomRolloffFactor - current_efx_properties_of_slot[slot_id].flRoomRolloffFactor),
static_cast<int>(std::round(current_efx_properties_of_slot[slot_id].iDecayHFLimit + step * (target_efx_properties->iDecayHFLimit - current_efx_properties_of_slot[slot_id].iDecayHFLimit))),
};
// update AL effect to intermediate values
switch (m_efx_reverb_engine)
{
case EfxReverbEngine::EAXREVERB:
this->alEffectf( efx_effect_id, AL_EAXREVERB_DENSITY, current_efx_properties_of_slot[slot_id].flDensity);
this->alEffectf( efx_effect_id, AL_EAXREVERB_DIFFUSION, current_efx_properties_of_slot[slot_id].flDiffusion);
this->alEffectf( efx_effect_id, AL_EAXREVERB_GAIN, current_efx_properties_of_slot[slot_id].flGain);
this->alEffectf( efx_effect_id, AL_EAXREVERB_GAINHF, current_efx_properties_of_slot[slot_id].flGainHF);
this->alEffectf( efx_effect_id, AL_EAXREVERB_GAINLF, current_efx_properties_of_slot[slot_id].flGainLF);
this->alEffectf( efx_effect_id, AL_EAXREVERB_DECAY_TIME, current_efx_properties_of_slot[slot_id].flDecayTime);
this->alEffectf( efx_effect_id, AL_EAXREVERB_DECAY_HFRATIO, current_efx_properties_of_slot[slot_id].flDecayHFRatio);
this->alEffectf( efx_effect_id, AL_EAXREVERB_DECAY_LFRATIO, current_efx_properties_of_slot[slot_id].flDecayLFRatio);
this->alEffectf( efx_effect_id, AL_EAXREVERB_REFLECTIONS_GAIN, current_efx_properties_of_slot[slot_id].flReflectionsGain);
this->alEffectf( efx_effect_id, AL_EAXREVERB_REFLECTIONS_DELAY, current_efx_properties_of_slot[slot_id].flReflectionsDelay);
this->alEffectfv(efx_effect_id, AL_EAXREVERB_REFLECTIONS_PAN, current_efx_properties_of_slot[slot_id].flReflectionsPan);
this->alEffectf( efx_effect_id, AL_EAXREVERB_LATE_REVERB_GAIN, current_efx_properties_of_slot[slot_id].flLateReverbGain);
this->alEffectf( efx_effect_id, AL_EAXREVERB_LATE_REVERB_DELAY, current_efx_properties_of_slot[slot_id].flLateReverbDelay);
this->alEffectfv(efx_effect_id, AL_EAXREVERB_LATE_REVERB_PAN, current_efx_properties_of_slot[slot_id].flLateReverbPan);
this->alEffectf( efx_effect_id, AL_EAXREVERB_ECHO_TIME, current_efx_properties_of_slot[slot_id].flEchoTime);
this->alEffectf( efx_effect_id, AL_EAXREVERB_ECHO_DEPTH, current_efx_properties_of_slot[slot_id].flEchoDepth);
this->alEffectf( efx_effect_id, AL_EAXREVERB_MODULATION_TIME, current_efx_properties_of_slot[slot_id].flModulationTime);
this->alEffectf( efx_effect_id, AL_EAXREVERB_MODULATION_DEPTH, current_efx_properties_of_slot[slot_id].flModulationDepth);
this->alEffectf( efx_effect_id, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, current_efx_properties_of_slot[slot_id].flAirAbsorptionGainHF);
this->alEffectf( efx_effect_id, AL_EAXREVERB_HFREFERENCE, current_efx_properties_of_slot[slot_id].flHFReference);
this->alEffectf( efx_effect_id, AL_EAXREVERB_LFREFERENCE, current_efx_properties_of_slot[slot_id].flLFReference);
this->alEffectf( efx_effect_id, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, current_efx_properties_of_slot[slot_id].flRoomRolloffFactor);
this->alEffecti( efx_effect_id, AL_EAXREVERB_DECAY_HFLIMIT, current_efx_properties_of_slot[slot_id].iDecayHFLimit);
break;
case EfxReverbEngine::REVERB:
this->alEffectf( efx_effect_id, AL_REVERB_DENSITY, current_efx_properties_of_slot[slot_id].flDensity);
this->alEffectf( efx_effect_id, AL_REVERB_DIFFUSION, current_efx_properties_of_slot[slot_id].flDiffusion);
this->alEffectf( efx_effect_id, AL_REVERB_GAIN, current_efx_properties_of_slot[slot_id].flGain);
this->alEffectf( efx_effect_id, AL_REVERB_GAINHF, current_efx_properties_of_slot[slot_id].flGainHF);
this->alEffectf( efx_effect_id, AL_REVERB_DECAY_TIME, current_efx_properties_of_slot[slot_id].flDecayTime);
this->alEffectf( efx_effect_id, AL_REVERB_DECAY_HFRATIO, current_efx_properties_of_slot[slot_id].flDecayHFRatio);
this->alEffectf( efx_effect_id, AL_REVERB_REFLECTIONS_GAIN, current_efx_properties_of_slot[slot_id].flReflectionsGain);
this->alEffectf( efx_effect_id, AL_REVERB_REFLECTIONS_DELAY, current_efx_properties_of_slot[slot_id].flReflectionsDelay);
this->alEffectf( efx_effect_id, AL_REVERB_LATE_REVERB_GAIN, current_efx_properties_of_slot[slot_id].flLateReverbGain);
this->alEffectf( efx_effect_id, AL_REVERB_LATE_REVERB_DELAY, current_efx_properties_of_slot[slot_id].flLateReverbDelay);
this->alEffectf( efx_effect_id, AL_REVERB_AIR_ABSORPTION_GAINHF, current_efx_properties_of_slot[slot_id].flAirAbsorptionGainHF);
this->alEffectf( efx_effect_id, AL_REVERB_ROOM_ROLLOFF_FACTOR, current_efx_properties_of_slot[slot_id].flRoomRolloffFactor);
this->alEffectf( efx_effect_id, AL_REVERB_DECAY_HFLIMIT, current_efx_properties_of_slot[slot_id].iDecayHFLimit);
break;
case EfxReverbEngine::NONE:
this->alAuxiliaryEffectSloti(slot_id, AL_EFFECTSLOT_EFFECT, AL_EFFECTSLOT_NULL);
return;
}
// make the slot use the updated AL effect
this->alAuxiliaryEffectSloti(slot_id, AL_EFFECTSLOT_EFFECT, efx_effect_id);
}
std::tuple<Ogre::Vector3, float, float> SoundManager::ComputeEarlyReflectionsProperties() const
{
const float max_distance = 2.0f;
const float reflections_gain_boost_max = 2.0f; // 6.32 db
float early_reflections_gain;
float early_reflections_delay;
float magnitude = 0;
Ogre::Vector3 early_reflections_pan = { 0.0f, 0.0f, 0.0f};
/*
* To detect surfaces around the listener within the vicinity of
* max_distance, we cast rays counter-clockwise in a 360° circle
* around the listener on a horizontal plane realative to the listener.
*/
bool nearby_surface_detected = false;
const float angle_step_size = 90;
float closest_surface_distance = std::numeric_limits<float>::max();
for (float angle = 0; angle < 360; angle += angle_step_size)
{
float closest_surface_distance_in_this_direction = std::numeric_limits<float>::max();
Ogre::Vector3 raycast_direction = Quaternion(Ogre::Degree(angle), m_listener_up) * m_listener_direction;
raycast_direction.normalise();
// accompany direction vector for how the intersectsTris function works
// check for nearby collision meshes
Ray ray = Ray(m_listener_position, raycast_direction * max_distance * App::GetGameContext()->GetTerrain()->GetCollisions()->GetCellSize());
std::pair<bool, Ogre::Real> intersection = App::GetGameContext()->GetTerrain()->GetCollisions()->intersectsTris(ray);
if (intersection.first)
{
closest_surface_distance_in_this_direction = intersection.second * max_distance;
}
ray.setDirection(ray.getDirection().normalisedCopy());
// check for nearby collision boxes
for (const collision_box_t& collision_box : App::GetGameContext()->GetTerrain()->GetCollisions()->getCollisionBoxes())
{
if (!collision_box.enabled || collision_box.virt) { continue; }
intersection = ray.intersects(Ogre::AxisAlignedBox(collision_box.lo, collision_box.hi));
if (intersection.first && intersection.second <= max_distance)
{
closest_surface_distance_in_this_direction = std::min(closest_surface_distance_in_this_direction, intersection.second);
}
}
// check for nearby actors
const ActorPtrVec& actors = App::GetGameContext()->GetActorManager()->GetActors();
for (const ActorPtr& actor : actors)
{
// ignore own truck if player is driving one
if (actor == App::GetGameContext()->GetPlayerCharacter()->GetActorCoupling()) { continue; }
intersection = ray.intersects(actor->ar_bounding_box);
if (intersection.first && intersection.second <= max_distance)
{
closest_surface_distance_in_this_direction = std::min(closest_surface_distance_in_this_direction, intersection.second);
}
}
closest_surface_distance = std::min(closest_surface_distance, closest_surface_distance_in_this_direction);
if (closest_surface_distance_in_this_direction <= max_distance)
{
early_reflections_pan += raycast_direction * (max_distance - closest_surface_distance_in_this_direction);
}
}
nearby_surface_detected = closest_surface_distance <= max_distance;
// TODO vertical raycasts
if (!nearby_surface_detected)
{
// reset values to the original values of the preset
early_reflections_delay = m_listener_efx_reverb_properties->flReflectionsDelay;
early_reflections_gain = m_listener_efx_reverb_properties->flReflectionsGain;
}
else // at least one nearby surface was detected
{
// we assume that surfaces further away cause less focussed reflections
magnitude = 1.0f - early_reflections_pan.length() / Ogre::Math::Sqrt(2.0f * Ogre::Math::Pow(max_distance, 2));
// set delay based on distance to the closest surface
early_reflections_delay = closest_surface_distance / this->GetSpeedOfSound();
early_reflections_gain = std::min(
(m_listener_efx_reverb_properties->flReflectionsGain
+ reflections_gain_boost_max
- (reflections_gain_boost_max * (magnitude))),
AL_EAXREVERB_MAX_REFLECTIONS_GAIN);
}
// transform the pan vector from being listener-relative to being user-relative
// determine the rotation of the listener direction from straight-ahead vector
// work around Quaternion quirks at around 180° rotation
Ogre::Quaternion horizontal_rotation;
if (m_listener_direction.z > 0.0f)
{
horizontal_rotation = Quaternion(Ogre::Degree(180), m_listener_up) * m_listener_direction.getRotationTo(Ogre::Vector3::UNIT_Z);
}
else
{
horizontal_rotation = m_listener_direction.getRotationTo(Ogre::Vector3::NEGATIVE_UNIT_Z);
}
early_reflections_pan = horizontal_rotation * early_reflections_pan;
early_reflections_pan.normalise();
early_reflections_pan = magnitude * early_reflections_pan;
return std::make_tuple(early_reflections_pan, early_reflections_gain, early_reflections_delay);
}
ALuint SoundManager::CreateAlEffect(const EFXEAXREVERBPROPERTIES* efx_properties) const
{
ALuint effect = 0;
ALenum error;
this->alGenEffects(1, &effect);
switch (m_efx_reverb_engine)
{
case EfxReverbEngine::EAXREVERB:
this->alEffecti( effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
this->alEffectf( effect, AL_EAXREVERB_DENSITY, efx_properties->flDensity);
this->alEffectf( effect, AL_EAXREVERB_DIFFUSION, efx_properties->flDiffusion);
this->alEffectf( effect, AL_EAXREVERB_GAIN, efx_properties->flGain);
this->alEffectf( effect, AL_EAXREVERB_GAINHF, efx_properties->flGainHF);
this->alEffectf( effect, AL_EAXREVERB_GAINLF, efx_properties->flGainLF);
this->alEffectf( effect, AL_EAXREVERB_DECAY_TIME, efx_properties->flDecayTime);
this->alEffectf( effect, AL_EAXREVERB_DECAY_HFRATIO, efx_properties->flDecayHFRatio);
this->alEffectf( effect, AL_EAXREVERB_DECAY_LFRATIO, efx_properties->flDecayLFRatio);
this->alEffectf( effect, AL_EAXREVERB_REFLECTIONS_GAIN, efx_properties->flReflectionsGain);
this->alEffectf( effect, AL_EAXREVERB_REFLECTIONS_DELAY, efx_properties->flReflectionsDelay);
this->alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, efx_properties->flReflectionsPan);
this->alEffectf( effect, AL_EAXREVERB_LATE_REVERB_GAIN, efx_properties->flLateReverbGain);
this->alEffectf( effect, AL_EAXREVERB_LATE_REVERB_DELAY, efx_properties->flLateReverbDelay);
this->alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, efx_properties->flLateReverbPan);
this->alEffectf( effect, AL_EAXREVERB_ECHO_TIME, efx_properties->flEchoTime);
this->alEffectf( effect, AL_EAXREVERB_ECHO_DEPTH, efx_properties->flEchoDepth);
this->alEffectf( effect, AL_EAXREVERB_MODULATION_TIME, efx_properties->flModulationTime);
this->alEffectf( effect, AL_EAXREVERB_MODULATION_DEPTH, efx_properties->flModulationDepth);
this->alEffectf( effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, efx_properties->flAirAbsorptionGainHF);
this->alEffectf( effect, AL_EAXREVERB_HFREFERENCE, efx_properties->flHFReference);
this->alEffectf( effect, AL_EAXREVERB_LFREFERENCE, efx_properties->flLFReference);
this->alEffectf( effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, efx_properties->flRoomRolloffFactor);
this->alEffecti( effect, AL_EAXREVERB_DECAY_HFLIMIT, efx_properties->iDecayHFLimit);
break;
case EfxReverbEngine::REVERB:
this->alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
this->alEffectf(effect, AL_REVERB_DENSITY, efx_properties->flDensity);
this->alEffectf(effect, AL_REVERB_DIFFUSION, efx_properties->flDiffusion);
this->alEffectf(effect, AL_REVERB_GAIN, efx_properties->flGain);
this->alEffectf(effect, AL_REVERB_GAINHF, efx_properties->flGainHF);
this->alEffectf(effect, AL_REVERB_DECAY_TIME, efx_properties->flDecayTime);
this->alEffectf(effect, AL_REVERB_DECAY_HFRATIO, efx_properties->flDecayHFRatio);
this->alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, efx_properties->flReflectionsGain);
this->alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, efx_properties->flReflectionsDelay);
this->alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, efx_properties->flLateReverbGain);
this->alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, efx_properties->flLateReverbDelay);
this->alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, efx_properties->flAirAbsorptionGainHF);
this->alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, efx_properties->flRoomRolloffFactor);
this->alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, efx_properties->iDecayHFLimit);
break;
case EfxReverbEngine::NONE:
default:
LOG("SoundManager: No usable reverb engine set, not creating reverb effect");
}
error = alGetError();
if (error != AL_NO_ERROR)
{
LOG("SoundManager: Could not create EFX effect:" + TOSTRING(alGetString(error)));
if (this->alIsEffect(effect))
this->alDeleteEffects(1, &effect);
return 0;
}
return effect;
}
void SoundManager::DeleteAlEffect(const ALuint efx_effect_id) const
{
ALenum error;
alGetError();
this->alDeleteEffects(1, &efx_effect_id);
error = alGetError();
if (error != AL_NO_ERROR)
{
LOG("SoundManager: Could not delete EFX effect: " + TOSTRING(alGetString(error)));
}
}
bool compareByAudibility(std::pair<int, float> a, std::pair<int, float> b)
{
return a.second > b.second;
}
// called when the camera moves
void SoundManager::recomputeAllSources()
{
// Creates this issue: https://github.com/RigsOfRods/rigs-of-rods/issues/1054
#if 0
if (!audio_device) return;
for (int i=0; i < m_audio_sources_in_use_count; i++)
{
audio_sources[i]->computeAudibility(m_listener_position);
audio_sources_most_audible[i].first = i;
audio_sources_most_audible[i].second = audio_sources[i]->audibility;
}
// sort first 'num_hardware_sources' sources by audibility
// see: https://en.wikipedia.org/wiki/Selection_algorithm
if ((m_audio_sources_in_use_count - 1) > hardware_sources_num)
{
std::nth_element(audio_sources_most_audible, audio_sources_most_audible+hardware_sources_num, audio_sources_most_audible + m_audio_sources_in_use_count - 1, compareByAudibility);
}
// retire out of range sources first
for (int i=0; i < m_audio_sources_in_use_count; i++)
{
if (audio_sources[audio_sources_most_audible[i].first]->hardware_index != -1 && (i >= hardware_sources_num || audio_sources_most_audible[i].second == 0))
retire(audio_sources_most_audible[i].first);
}
// assign new sources
for (int i=0; i < std::min(m_audio_sources_in_use_count, hardware_sources_num); i++)
{
if (audio_sources[audio_sources_most_audible[i].first]->hardware_index == -1 && audio_sources_most_audible[i].second > 0)
{
for (int j=0; j < hardware_sources_num; j++)
{
if (hardware_sources_map[j] == -1)
{
assign(audio_sources_most_audible[i].first, j);
break;
}
}
}
}
#endif
}
void SoundManager::UpdateSourceFilters(const int hardware_index) const
{
bool source_is_obstructed = this->IsHardwareSourceObstructed(hardware_index);
this->UpdateObstructionFilter(hardware_index, source_is_obstructed);
/*
* If an obstruction was detected, also check for occlusions.
* The current implementation ignores the case of exclusion since
* it compares the reverb environments of the source and listener.
* This would not be enough to reliably detect exclusion in outdoor environments.
*/
if (source_is_obstructed)
{
this->UpdateOcclusionFilter(hardware_index, m_listener_slot, m_listener_efx_reverb_properties);
}
else
{
// disable occlusion filter just in case it was previously active
alSource3i(hardware_sources[hardware_index], AL_AUXILIARY_SEND_FILTER, m_listener_slot, m_efx_occlusion_wet_path_send_id, AL_FILTER_NULL);
}
}
bool SoundManager::IsHardwareSourceObstructed(const int hardware_index) const
{
if (hardware_sources_map[hardware_index] == -1) { return false; } // no sound assigned to hardware source
/*
* There is no simple way to know whether a truck has a closed cabin or not; hence
* provide an option to always force obstruction if the player is inside a vehicle.
*/
if
(
App::audio_force_obstruction_inside_vehicles->getBool()
&& App::GetGameContext()->GetPlayerCharacter()->GetActorCoupling() != nullptr
&& App::GetGameContext()->GetPlayerCharacter()->GetActorCoupling()->ar_bounding_box.contains(m_listener_position)
)
{
return true;
}
/*
* Perform various line of sight checks until either a collision was detected
* and the filter has to be applied or no obstruction was detected.
*/
std::pair<bool, Ogre::Real> intersection;
const SoundPtr& corresponding_sound = audio_sources[hardware_sources_map[hardware_index]];
const Ogre::Vector3 direction_to_sound = corresponding_sound->getPosition() - m_listener_position;
const Ogre::Ray direct_path_to_sound = Ray(m_listener_position, direction_to_sound);
bool obstruction_detected = false;
// perform line of sight check against collision meshes
// for this to work correctly, the direction vector of the ray must have
// the length of the distance from the listener to the sound
intersection = App::GetGameContext()->GetTerrain()->GetCollisions()->intersectsTris(direct_path_to_sound);
obstruction_detected = intersection.first;
if (!obstruction_detected)
{
// perform line of sight check agains collision boxes
for (const collision_box_t& collision_box : App::GetGameContext()->GetTerrain()->GetCollisions()->getCollisionBoxes())
{
if (!collision_box.enabled || collision_box.virt) { continue; }
Ogre::AxisAlignedBox collision_box_aab = Ogre::AxisAlignedBox(collision_box.lo, collision_box.hi);
// Skip cases where the obstruction filter detection becomes unstable
if ( collision_box_aab.contains(corresponding_sound->getPosition())
|| collision_box_aab.distance(corresponding_sound->getPosition()) < 0.1f)
{
continue;