forked from StrongPC123/Far-Cry-1-Source-Full
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundSystem.cpp
2003 lines (1751 loc) · 53.2 KB
/
SoundSystem.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
//////////////////////////////////////////////////////////////////////
//
// Crytek (C) 2001
//
// CrySound Source Code
//
// File: SoundSystem.cpp
// Description: ISoundSystem interface implementation.
//
// History:
// -June 06,2001:Implemented by Marco Corbetta
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifndef _XBOX
#include <ISystem.h>
#include <CrySizer.h>
#include <algorithm>
#include <IConsole.h>
#include <ITimer.h>
#include <Cry_Math.h>
#include <Cry_Camera.h>
#include <IRenderer.h>
#include "SoundSystem.h"
#include "MusicSystem.h"
#include "Sound.h"
#include <I3dEngine.h> //needed to check if the listener is in indoor or outdoor
#include <ICryPak.h> //needed to check if the listener is in indoor or outdoor
//#define CS_BUFFERSIZE 10 /* millisecond value for FMOD buffersize. */
#ifdef WIN64
//#define CS_SAFEBORDER 16
#endif
//////////////////////////////////////////////////////////////////////////
void* _DSPUnit_SFXFilter_Callback(void *pOriginalBuffer, void *pNewBuffer, int nLength, INT_PTR nParam)
{
if (!nParam)
return pNewBuffer;
CSoundSystem *pOwner=(CSoundSystem*)nParam;
return pOwner->DSPUnit_SFXFilter_Callback(pOriginalBuffer, pNewBuffer, nLength);
}
extern "C"
{
#if CS_SAFEBORDER
enum {g_cFillConst = 0xCE};
void CheckSafeBorder (char* p)
{
for (unsigned i = 0; i < CS_SAFEBORDER; ++i)
if (p[i] != (char)(g_cFillConst+i))
__debugbreak();
}
void FillSafeBofder (char* p)
{
for (unsigned i = 0; i < CS_SAFEBORDER; ++i)
p[i] = (char)(g_cFillConst+i);
}
#endif
static void * F_CALLBACKAPI CrySound_Alloc (unsigned int size)
{
#if CS_SAFEBORDER
unsigned* pN = (unsigned*)malloc (size + 2 * CS_SAFEBORDER + sizeof(unsigned));
*pN = size;
char* p = (char*)(pN+1);
FillSafeBofder(p);
FillSafeBofder(p + size + CS_SAFEBORDER);
return p + CS_SAFEBORDER;
#else
return malloc (size);
#endif
}
static void F_CALLBACKAPI CrySound_Free (void* ptr)
{
#if CS_SAFEBORDER
char* pOld = ((char*)ptr) - CS_SAFEBORDER;
unsigned* pNOld = ((unsigned*)pOld) - 1;
CheckSafeBorder(pOld);
CheckSafeBorder(pOld + *pNOld + CS_SAFEBORDER);
free (pNOld);
#else
free (ptr);
#endif
}
static void * F_CALLBACKAPI CrySound_Realloc (void *ptr, unsigned int size)
{
#if CS_SAFEBORDER
char* pOld = ((char*)ptr) - CS_SAFEBORDER;
unsigned* pNOld = ((unsigned*)pOld) - 1;
char* pRet = (char*)CrySound_Alloc(size);
memcpy (pRet, ptr, min(*pNOld,size));
CrySound_Free (ptr);
return pRet;
#else
return realloc (ptr, size);
#endif
}
}
//////////////////////////////////////////////////////////////////////////
// File callbacks for fmod.
//////////////////////////////////////////////////////////////////////////
#ifdef CS_VERSION_372
static void * F_CALLBACKAPI CrySound_fopen( const char *name )
{
// File is opened for streaming.
FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb",ICryPak::FOPEN_HINT_DIRECT_OPERATION );
//FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb" );
if (!file)
{
GetISystem()->Warning( VALIDATOR_MODULE_SOUNDSYSTEM,VALIDATOR_WARNING,VALIDATOR_FLAG_SOUND,"Sound %s failed to load", name );
}
return (void *)file;
}
static void F_CALLBACKAPI CrySound_fclose( void * nFile )
{
FILE *file = (FILE*)nFile;
GetISystem()->GetIPak()->FClose( file );
}
static int F_CALLBACKAPI CrySound_fread( void *buffer, int size, void * nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FRead( buffer,1,size,file );
}
static int F_CALLBACKAPI CrySound_fseek( void * nFile, int pos, signed char mode)
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FSeek( file,pos,mode );
}
static int F_CALLBACKAPI CrySound_ftell( void * nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FTell( file );
}
#else
#ifdef CS_VERSION_361
static unsigned int F_CALLBACKAPI CrySound_fopen( const char *name )
{
// File is opened for streaming.
FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb",ICryPak::FOPEN_HINT_DIRECT_OPERATION );
//FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb" );
if (!file)
{
GetISystem()->Warning( VALIDATOR_MODULE_SOUNDSYSTEM,VALIDATOR_WARNING,VALIDATOR_FLAG_SOUND,"Sound %s failed to load", name );
}
return (unsigned int)file;
}
static void F_CALLBACKAPI CrySound_fclose( unsigned int nFile )
{
FILE *file = (FILE*)nFile;
GetISystem()->GetIPak()->FClose( file );
}
static int F_CALLBACKAPI CrySound_fread( void *buffer, int size, unsigned int nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FRead( buffer,1,size,file );
}
static int F_CALLBACKAPI CrySound_fseek( unsigned int nFile, int pos, signed char mode)
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FSeek( file,pos,mode );
}
static int F_CALLBACKAPI CrySound_ftell( unsigned int nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FTell( file );
}
#else
static UINT_PTR F_CALLBACKAPI CrySound_fopen( const char *name )
{
// File is opened for streaming.
FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb",ICryPak::FOPEN_HINT_DIRECT_OPERATION );
//FILE *file = GetISystem()->GetIPak()->FOpen( name,"rb" );
if (!file)
{
GetISystem()->Warning( VALIDATOR_MODULE_SOUNDSYSTEM,VALIDATOR_WARNING,VALIDATOR_FLAG_SOUND,"Sound %s failed to load", name );
}
return (UINT_PTR)file;
}
static void F_CALLBACKAPI CrySound_fclose( UINT_PTR nFile )
{
FILE *file = (FILE*)nFile;
GetISystem()->GetIPak()->FClose( file );
}
static int F_CALLBACKAPI CrySound_fread( void *buffer, int size, UINT_PTR nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FRead( buffer,1,size,file );
}
static int F_CALLBACKAPI CrySound_fseek( UINT_PTR nFile, int pos, signed char mode)
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FSeek( file,pos,mode );
}
static int F_CALLBACKAPI CrySound_ftell( UINT_PTR nFile )
{
FILE *file = (FILE*)nFile;
return GetISystem()->GetIPak()->FTell( file );
}
#endif
#endif
//////////////////////////////////////////////////////////////////////
CSoundSystem::CSoundSystem(ISystem* pSystem, HWND hWnd) : CSoundSystemCommon(pSystem)
{
GUARD_HEAP;
m_bOK=m_bInside=false;
if (!pSystem)
return;
m_fSFXVolume=1.0f;
m_fMusicVolume=1.0f;
m_pVisArea=NULL;
m_nSpeakerConfig=-1; //force to set
m_bPause=false;
m_pISystem = pSystem;
m_pILog=pSystem->GetILog();
m_pTimer=pSystem->GetITimer();
m_pStreamEngine=pSystem->GetStreamEngine();
m_itLastInactivePos=m_lstSoundSpotsInactive.end();
m_nInactiveSoundSpotsSize=0;
m_nBuffersLoaded=0;
m_nMinSoundPriority = 0;
m_pDSPUnitSFXFilter=NULL;
m_fDirAttCone=0.0f;
for (int i=0;i<MAX_SOUNDSCALE_GROUPS;i++) m_fSoundScale[i]=1.0f;
memset(m_VisAreas,0,MAX_VIS_AREAS*sizeof(int));
m_nMuteRefCnt=0;
m_nSampleRate=m_pCVarSampleRate->GetIVal();
//if (!m_pCVARSoundEnable->GetIVal())
// return;
if (m_pCVARDummySound->GetIVal())
return; // creates dummy sound system
if (CS_GetVersion() != CS_VERSION)
{
m_pILog->Log("Music:Init:Incorrect DLL version for CRYSOUND: Need %.2f\n",CS_VERSION);
return;
}
#if !defined(_DEBUG)// || defined(WIN64)
CS_SetMemorySystem(NULL,NULL,CrySound_Alloc,CrySound_Realloc,CrySound_Free);
#endif
m_pILog->Log("------------------------------------------CRYSOUND VERSION=%f\n",CS_GetVersion());
//configure CS's stability under windows
//CS_SetBufferSize(CS_BUFFERSIZE);
//init CS
//CS_SetOutput(CS_OUTPUT_DSOUND);
//CS_SetDriver(0);
//CS_SetMixer(CS_MIXER_QUALITY_AUTODETECT);
#if 0//defined(WIN64) || defined(CS_VERSION_3_63)
//CS_DSP_SetActive(CS_DSP_GetClearUnit(), FALSE);
//CS_DSP_SetActive(CS_DSP_GetSFXUnit(), FALSE);
//CS_DSP_SetActive(CS_DSP_GetMusicUnit(), FALSE);
//CS_DSP_SetActive(CS_DSP_GetClipAndCopyUnit(), FALSE);
CS_SetOutput(CS_OUTPUT_WINMM);
CS_SetMaxHardwareChannels(0);
#else
// Defines the minimum number of hardware channels.
// if less than that, the sound engine will switch to software mixing.
CS_SetMinHardwareChannels(m_pCVarMinHWChannels->GetIVal());
CS_SetMaxHardwareChannels(m_pCVarMaxHWChannels->GetIVal());
#endif
CS_SetHWND(hWnd);
// Assign file access callbacks to fmod to our pak file system.
CS_File_SetCallbacks( CrySound_fopen,CrySound_fclose,CrySound_fread,CrySound_fseek,CrySound_ftell );
for (int i=0; i < CS_GetNumDrivers(); i++)
{
m_pILog->Log("%d - %s\n", i+1, CS_GetDriverName(i)); // print driver names
if (m_pCVarCapsCheck->GetIVal()>0) //caps checking is slow
{
unsigned int caps =0;
CS_GetDriverCaps(i,&caps);
if (caps & CS_CAPS_HARDWARE)
m_pILog->Log(" Driver supports hardware 3D sound");
if (caps & CS_CAPS_EAX2)
m_pILog->Log(" Driver supports EAX 2.0 reverb");
if (caps & CS_CAPS_EAX3)
m_pILog->Log(" Driver supports EAX 3.0 reverb");
//if (caps & CS_CAPS_GEOMETRY_OCCLUSIONS)
// m_pILog->Log(" Driver supports hardware 3d geometry processing with occlusions");
//if (caps & CS_CAPS_GEOMETRY_REFLECTIONS)
// m_pILog->Log(" Driver supports hardware 3d geometry processing with reflections");
//if (caps & CS_CAPS_EAX2)
// m_pILog->Log(" Driver supports EAX 2.0 reverb!");
}
}
//CS_SetMixer(CS_MIXER_QUALITY_FPU);
if (m_pCVarCompatibleMode->GetIVal()!=0)
{
CS_SetBufferSize(200);
}
if (!CS_Init(m_nSampleRate, 256, 0))
{
m_pILog->Log("Cannot init CRYSOUND\n");
CS_Close();
return;
}
//CS_3D_SetRolloffFactor(0.0);
m_pILog->Log("CRYSOUND Driver: %s", CS_GetDriverName(CS_GetDriver()));
m_pILog->Log("Total number of channels available: %d\n",CS_GetMaxChannels());
// WARNING!!! Wat
//m_pILog->Log("Total number of hardware channels available: %d\n",CS_GetNumHardwareChannels());
SetSpeakerConfig();
// lets create a dsp unit for the sfx-filter
//m_pDSPUnitSFXFilter=CS_DSP_Create(_DSPUnit_SFXFilter_Callback, SFXFILTER_PRIORITY, (INT_PTR)this);
if (m_pDSPUnitSFXFilter)
{
m_fDSPUnitSFXFilterCutoff=500.0f;
m_fDSPUnitSFXFilterResonance=0.5f;
m_fDSPUnitSFXFilterLowLVal=0.0f;
m_fDSPUnitSFXFilterBandLVal=0.0f;
m_fDSPUnitSFXFilterLowRVal=0.0f;
m_fDSPUnitSFXFilterBandRVal=0.0f;
//CS_DSP_SetActive(m_pDSPUnitSFXFilter, TRUE);
}
Mute(false);
m_bOK = true;
m_bValidPos=false;
m_nLastEax=EAX_PRESET_OFF;
m_EAXIndoor.nPreset=EAX_PRESET_OFF;
m_EAXOutdoor.nPreset=EAX_PRESET_OFF;
strcpy(m_szEmptyName,"NONAME");
m_bResetVolume=false;
m_fSFXResetVolume=1.0f;
m_pLastEAXProps=NULL;
}
//////////////////////////////////////////////////////////////////////////
void* CSoundSystem::DSPUnit_SFXFilter_Callback(void *pOriginalBuffer, void *pNewBuffer, int nLength)
{
GUARD_HEAP;
// originalbuffer = crysounds original mixbuffer.
// newbuffer = the buffer passed from the previous DSP unit.
// length = length in samples at this mix time.
// param = user parameter passed through in CS_DSP_Create.
//
// modify the buffer in some fashion
int nMixer=CS_GetMixer();
if (nMixer!=CS_MIXER_QUALITY_FPU)
return pNewBuffer;
float f, q, h;
f=2.0f*(float)sin(3.1415962*m_fDSPUnitSFXFilterCutoff/(float)CS_GetOutputRate());
q=m_fDSPUnitSFXFilterResonance;
float fInput;
nLength<<=1;
for (int i=0;i<nLength;)
{
// left channel
fInput=((float*)pNewBuffer)[i];
m_fDSPUnitSFXFilterLowLVal+=f*m_fDSPUnitSFXFilterBandLVal;
h=fInput-m_fDSPUnitSFXFilterLowLVal-(m_fDSPUnitSFXFilterBandLVal*q);
m_fDSPUnitSFXFilterBandLVal+=f*h;
// writing output
((float*)pNewBuffer)[i]=m_fDSPUnitSFXFilterLowLVal;
// right channel
i++;
fInput=((float*)pNewBuffer)[i];
m_fDSPUnitSFXFilterLowRVal+=f*m_fDSPUnitSFXFilterBandRVal;
h=fInput-m_fDSPUnitSFXFilterLowRVal-(m_fDSPUnitSFXFilterBandRVal*q);
m_fDSPUnitSFXFilterBandRVal+=f*h;
// writing output
((float*)pNewBuffer)[i]=m_fDSPUnitSFXFilterLowRVal;
i++;
}
return pNewBuffer;
}
//////////////////////////////////////////////////////////////////////
void CSoundSystem::SetSpeakerConfig()
{
GUARD_HEAP;
int nCurrSpeakerConfig=m_pCVarSpeakerConfig->GetIVal();
if (nCurrSpeakerConfig!=m_nSpeakerConfig)
{
switch (nCurrSpeakerConfig)
{
case CSSPEAKERCONFIG_5POINT1:
CS_SetSpeakerMode(CS_SPEAKERMODE_DOLBYDIGITAL);
m_pILog->Log("Set speaker mode 5.1\n");
break;
case CSSPEAKERCONFIG_HEADPHONE:
CS_SetSpeakerMode(CS_SPEAKERMODE_HEADPHONES);
m_pILog->Log("Set speaker mode head phone\n");
break;
case CSSPEAKERCONFIG_MONO:
CS_SetSpeakerMode(CS_SPEAKERMODE_MONO);
m_pILog->Log("Set speaker mode mono\n");
break;
case CSSPEAKERCONFIG_QUAD:
CS_SetSpeakerMode(CS_SPEAKERMODE_QUAD);
m_pILog->Log("Set speaker mode quad\n");
break;
case CSSPEAKERCONFIG_STEREO:
CS_SetSpeakerMode(CS_SPEAKERMODE_STEREO);
m_pILog->Log("Set speaker mode stereo\n");
break;
case CSSPEAKERCONFIG_SURROUND:
m_pILog->Log("Set speaker mode surround\n");
CS_SetSpeakerMode(CS_SPEAKERMODE_SURROUND);
break;
}
m_nSpeakerConfig=nCurrSpeakerConfig;
//Set the pan scalar to full pan separation 'cos cs_setspeakermode will reset it.
CS_SetPanSeperation(1.0f);
}
}
//////////////////////////////////////////////////////////////////////
void CSoundSystem::Reset()
{
GUARD_HEAP;
Silence();
// for the sake of clarity...
m_vecSounds.clear();
m_lstSoundSpotsActive.clear();
m_lstSoundSpotsInactive.clear();m_nInactiveSoundSpotsSize=0;
//m_nLastInactiveListPos=0;
m_itLastInactivePos=m_lstSoundSpotsInactive.end();
m_lstFadeUnderwaterSounds.clear();
m_autoStopSounds.clear();
m_stoppedSoundToBeDeleted.clear();
}
//////////////////////////////////////////////////////////////////////
CSoundSystem::~CSoundSystem()
{
GUARD_HEAP;
Reset();
if (m_pDSPUnitSFXFilter)
CS_DSP_Free(m_pDSPUnitSFXFilter);
m_pDSPUnitSFXFilter=NULL;
if (m_bOK)
CS_Close();
m_bOK = false;
SetEaxListenerEnvironment(EAX_PRESET_OFF);
}
//////////////////////////////////////////////////////////////////////
void CSoundSystem::Release()
{
delete this;
}
//////////////////////////////////////////////////////////////////////////
void CSoundSystem::DeactivateSound( CSound *pSound )
{
if (pSound->m_bPlaying)
pSound->Stop();
if (pSound->m_State == eSoundState_Active)
{
m_lstSoundSpotsActive.erase(pSound);
pSound->m_State = eSoundState_Inactive;
m_lstSoundSpotsInactive.insert(pSound);
}
}
//////////////////////////////////////////////////////////////////////////
bool CSoundSystem::ProcessActiveSound( CSound *cs )
{
FUNCTION_PROFILER( GetSystem(),PROFILE_SOUND );
bool bFadeoutFinished = false;
bool bIsSoundPotentiallyHearable = IsSoundPH(cs);
if (!bIsSoundPotentiallyHearable)
{
// decrease it as the player is leaving the room or moving from outdoor to indoor or viceversa.
bFadeoutFinished = cs->FadeOut();
}
float fRatio = 1.0f;
if (cs->m_nFlags & FLAG_SOUND_RADIUS)
{
float fDist2=GetSquaredDistance(cs->m_RealPos,m_vPos);
if (fDist2 > cs->m_fMaxRadius2)
{
// Too far.
return false;
}
if (bFadeoutFinished)
{
return false;
}
if (fDist2 > cs->m_fMinRadius2)
{
//calc attenuation, cal sound ratio between min & max radius
if (!(cs->m_nFlags & FLAG_SOUND_NO_SW_ATTENUATION))
{
fRatio=1.0f-((fDist2-cs->m_fMinRadius2)/cs->m_fMaxRadius2);
}
else
fRatio=1.0f;
}
}
if (bIsSoundPotentiallyHearable)
{
// check if the sound became hearable because of sound occlusion
cs->FadeIn();
}
// if Steve doesnt want to change frequency and pan, this can be replaced just with set volume
cs->SetAttrib(cs->m_nMaxVolume,fRatio*cs->m_fCurrentFade*cs->m_fRatio,cs->m_nPan,1000,false);
return true;
}
//////////////////////////////////////////////////////////////////////
void CSoundSystem::Update()
{
GUARD_HEAP;
FUNCTION_PROFILER( GetSystem(),PROFILE_SOUND );
// check if volume has changed
if (Ffabs(m_pCVarMusicVolume->GetFVal()-m_fMusicVolume)>0.2f)
{
if (m_pCVarMusicVolume->GetFVal()<0)
m_pCVarMusicVolume->Set(0.0f);
m_fMusicVolume=m_pCVarMusicVolume->GetFVal();
}
if (m_pCVarSFXVolume->GetFVal()<0)
m_pCVarSFXVolume->Set(0.0f);
if (m_pCVarSFXVolume->GetFVal()>1.0f)
m_pCVarSFXVolume->Set(1.0f);
if ((Ffabs(m_pCVarSFXVolume->GetFVal()-m_fSFXVolume)>0.2f))
{
m_fSFXVolume=m_pCVarSFXVolume->GetFVal();
// call set volume for each sound so it can rescale itself
for (SoundListItor It=m_vecSounds.begin();It!=m_vecSounds.end();It++)
{
CSound *cs=(*It);
if (cs->m_nFlags & FLAG_SOUND_MUSIC)
continue;
cs->SetVolume(cs->GetVolume());
} // It
}
if (m_pISystem && m_pCVarSoundInfo->GetIVal())
{
IRenderer *pRenderer=m_pISystem->GetIRenderer();
if (pRenderer)
{
int nChannels = CSound::m_PlayingChannels;
float x = 40;
float fColor[4]={1.0f, 1.0f, 1.0f, 0.7f};
float fColorRed[4]={1.0f, 0.0f, 0.0f, 0.7f};
float fColorYellow[4]={1.0f, 1.0f, 0.0f, 0.7f};
float fColorGreen[4]={0.0f, 1.0f, 0.0f, 0.7f};
pRenderer->Draw2dLabel(x, 1, 1, fColor, false, "SoundSystem[Debug]");
pRenderer->Draw2dLabel(x, 10, 1, fColor, false, " Current Voices: %d (Channels: %d), CPU-Mixing-Overhead: %3.1f", GetUsedVoices(),nChannels, GetCPUUsage());
pRenderer->Draw2dLabel(x, 20, 1, fColor, false, " Loaded SoundBuffers: %d (%d)", m_nBuffersLoaded,m_soundBuffers.size());
pRenderer->Draw2dLabel(x, 30, 1, fColor, false, " SoundSpots-Active: %d, Inactive: %d", (int)m_lstSoundSpotsActive.size(), (int)m_lstSoundSpotsInactive.size());
if (!m_bValidPos)
{
pRenderer->Draw2dLabel(x, 40, 1, fColor, false, "Listener invalid!");
}
else
{
if (m_bInside)
pRenderer->Draw2dLabel(x, 40, 1, fColor, false, "Listener INSIDE: Vis area=: %s",m_pVisArea->GetName());
else
pRenderer->Draw2dLabel(x, 40, 1, fColor, false, "Listener OUTSIDE ");
}
float ypos=50;
if (m_pCVarSoundInfo->GetIVal()==1)
{
for (SoundListItor It=m_lstSoundSpotsActive.begin();It!=m_lstSoundSpotsActive.end();It++)
{
CSound *cs=(*It);
if (cs->IsPlayingOnChannel())
pRenderer->Draw2dLabel(1, ypos, 1, fColorRed, false, "<Playing>");
else if (cs->IsUsingChannel())
pRenderer->Draw2dLabel(1, ypos, 1, fColorYellow, false, "[%d]",cs->m_nChannel );
float *pCurColor = fColor;
if (!cs->m_bLoop)
pCurColor = fColorGreen;
if (cs->m_nFlags & FLAG_SOUND_3D)
pRenderer->Draw2dLabel(x, ypos, 1, pCurColor, false, "%s,3d,pos=%d,%d,%d,minR=%d,maxR=%d,vol=%d",cs->GetName(),(int)(cs->m_position.x),(int)(cs->m_position.y),(int)(cs->m_position.z),(int)(cs->m_fMinDist),(int)(cs->m_fMaxDist),cs->m_nPlayingVolume );
else
pRenderer->Draw2dLabel(x, ypos, 1, pCurColor, false, "%s,2d,volume=%d",cs->GetName(),cs->m_nPlayingVolume );
ypos+=10;
} //it
}
else
{
for (SoundBufferPropsMapItor It=m_soundBuffers.begin();It!=m_soundBuffers.end();It++)
{
CSoundBuffer *pBuf=It->second;
if (pBuf->GetSample())
{
pRenderer->Draw2dLabel(x, ypos, 1, fColor, false, "%s",pBuf->GetName());
ypos+=10;
}
} //it
}
// All playing sounds...
{
ypos = 10;
x = 400;
pRenderer->Draw2dLabel(x, ypos, 1, fColor, false, "----- Playing Sounds -----" );
ypos += 10;
for (SoundListItor It=m_vecSounds.begin();It!=m_vecSounds.end();It++)
{
CSound *cs=(*It);
if (!cs->IsUsingChannel())
continue;
float *pCurColor = fColorYellow;
if (cs->IsPlayingOnChannel())
pCurColor = fColorRed;
if (cs->m_nFlags & FLAG_SOUND_3D)
pRenderer->Draw2dLabel(x, ypos, 1, pCurColor, false, "%s,3d,pos=%d,%d,%d,minR=%d,maxR=%d,vol=%d,channel=%d",cs->GetName(),(int)(cs->m_position.x),(int)(cs->m_position.y),(int)(cs->m_position.z),(int)(cs->m_fMinDist),(int)(cs->m_fMaxDist),cs->m_nPlayingVolume,cs->m_nChannel );
else
pRenderer->Draw2dLabel(x, ypos, 1, pCurColor, false, "%s,2d,volume=%d,channel=%d",cs->GetName(),cs->m_nPlayingVolume,cs->m_nChannel );
ypos+=10;
} //it
}
}
}
Vec3_tpl<float> vPos=m_cCam.GetPos();
if (GetLengthSquared(vPos)<1)
{
m_bValidPos=false;
return; // hasnt been set yet, but the update3d function has been called
}
m_bValidPos=true;
if (m_pCVARSoundEnable->GetIVal()==0)
{
Silence();
return;
}
SetSpeakerConfig();
bool bWasInside=m_bInside;
m_bInside=false;
RecomputeSoundOcclusion(true,false);
m_bInside=(m_pVisArea!=NULL);
/*
if (bWasInside!=m_bInside)
{
if (m_bInside)
SetEaxListenerEnvironment(m_EAXIndoor.nPreset, (m_EAXIndoor.nPreset==-1) ? &m_EAXIndoor.EAX : NULL, FLAG_SOUND_INDOOR);
else
SetEaxListenerEnvironment(m_EAXOutdoor.nPreset, (m_EAXOutdoor.nPreset==-1) ? &m_EAXOutdoor.EAX : NULL, FLAG_SOUND_OUTDOOR);
}
*/
// check if sr has changed
if (m_pCVarSampleRate->GetFVal()!=m_nSampleRate)
{
m_nSampleRate=(int)m_pCVarSampleRate->GetFVal();
}
std::pair< SoundListItor, bool > pr;
int nActiveSize=m_lstSoundSpotsActive.size();
if ((nActiveSize<m_pCVarMaxSoundSpots->GetIVal()) && (!m_lstSoundSpotsInactive.empty()))
{
FRAME_PROFILER( "CSoundSystem::Update:Inactive",GetSystem(),PROFILE_SOUND );
// iterate through a couple of inactive sounds to see if they became active...
//int nInactiveSoundSpots=(int)m_vecSoundSpotsInactive.size();
//if (m_nLastInactiveListPos>=nInactiveSoundSpots)
//m_nLastInactiveListPos=0;
if (m_itLastInactivePos==m_lstSoundSpotsInactive.end())
m_itLastInactivePos=m_lstSoundSpotsInactive.begin();
// lets choose the iteration-count, so it iterates through all sounds every m_pCVarInactiveSoundIterationTimeout seconds
int nInactiveSoundItCount=(int)cry_ceilf((m_pTimer->GetFrameTime()/m_pCVarInactiveSoundIterationTimeout->GetFVal())*(float)m_nInactiveSoundSpotsSize);
if (!nInactiveSoundItCount)
nInactiveSoundItCount=1;
if (nInactiveSoundItCount>m_nInactiveSoundSpotsSize)
nInactiveSoundItCount=m_nInactiveSoundSpotsSize;
/*
if (m_pISystem && m_pCVarSoundInfo->GetIVal())
{
float fColor[4]={1.0f, 1.0f, 1.0f, 1.0f};
IRenderer *pRenderer=m_pISystem->GetIRenderer();
if (pRenderer)
pRenderer->Draw2dLabel(10, 110, 1, fColor, false, " SoundSpots-InActive size: %d, iter count=%d ", m_nInactiveSoundSpotsSize,nInactiveSoundItCount);
}
*/
SoundListItor It=m_itLastInactivePos; //m_vecSoundSpotsInactive.begin()+m_nLastInactiveListPos;
int nSoundSpotAdded=0;
for (int i=0;i<nInactiveSoundItCount;i++)
{
CSound *cs=(*It);
if (cs->m_bPlaying)
{
float fDist2=GetSquaredDistance(cs->m_RealPos,m_vPos);
if (fDist2<=cs->m_fPreloadRadius2)
{
//////////////////////////////////////////////////////////////////////////
// are we getting too many soundspots?
if ((nActiveSize+nSoundSpotAdded)>m_pCVarMaxSoundSpots->GetIVal())
{
break; // resume next frame
}
//////////////////////////////////////////////////////////////////////////
// move sound to active list...
//m_vecSoundSpotsActive.push_back(cs);
m_lstSoundSpotsActive.insert(cs);
nSoundSpotAdded++;
It=m_lstSoundSpotsInactive.erase(It);m_nInactiveSoundSpotsSize--;
m_itLastInactivePos=It;
cs->m_State=eSoundState_Active;
cs->Preload();
}
else
{
++It;
//m_nLastInactiveListPos++;
++m_itLastInactivePos;
}
}
else
{
++It;
//m_nLastInactiveListPos++;
++m_itLastInactivePos;
}
if (It==m_lstSoundSpotsInactive.end())
{
It=m_lstSoundSpotsInactive.begin();
//m_nLastInactiveListPos=0;
m_itLastInactivePos=It;
}
} //i
}
float fCurrTime = m_pTimer->GetCurrTime();
// process the sound spheres for fading and occlusion
//for (SoundVecItor It=m_vecSoundSpotsActive.begin();It!=m_vecSoundSpotsActive.end();)
SoundListItor It,nextIt;
for (It=m_lstSoundSpotsActive.begin();It!=m_lstSoundSpotsActive.end();)
{
FRAME_PROFILER( "CSoundSystem::Update:Active",GetSystem(),PROFILE_SOUND );
nextIt = It; nextIt++;
CSound *cs=(*It);
if (cs->m_pSound->LoadFailure())
{
cs->m_State=eSoundState_None;
It = m_lstSoundSpotsActive.erase(It);
continue;
}
// make sure that one-shot samples reset its playing flag when they exceed their play-time and are clipped...
if ((!cs->m_bPlaying) || (cs->IsUsingChannel() && !cs->m_bLoop && !cs->IsPlayingOnChannel()) || cs->IsPlayLengthExpired(fCurrTime))
{
FRAME_PROFILER( "CSoundSystem::Update:Active-Deactivate",GetSystem(),PROFILE_SOUND );
//++It;
// move sound to inactive list...
//m_vecSoundSpotsInactive.push_back(cs);
pr=m_lstSoundSpotsInactive.insert(cs);
m_itLastInactivePos=pr.first;
m_nInactiveSoundSpotsSize++;
cs->m_State=eSoundState_Inactive;
It=m_lstSoundSpotsActive.erase(It);
if (cs->m_bPlaying)
{
// Stop can release sound for autostop onces.
cs->Stop();
}
continue;
}
// check if the sound became occluded
bool bZeroFadeCheckDist=false;
bool bIsSoundPotentiallyHearable = IsSoundPH(cs);
if (!bIsSoundPotentiallyHearable)
{
FRAME_PROFILER( "CSoundSystem::Update:Active-FadeOut",GetSystem(),PROFILE_SOUND );
// decrease it as the player is leaving the room
// or moving from outdoor to indoor or viceversa
bZeroFadeCheckDist=cs->FadeOut();
/*{
++It;
continue;
}*/
}
//const char *szTest=cs->GetName(); //debug
float fRatio=1.0f;
if (cs->m_nFlags & FLAG_SOUND_RADIUS)
{
float fDist2=GetSquaredDistance(cs->m_RealPos,m_vPos);
if (fDist2>cs->m_fMaxRadius2)
{
cs->FreeChannel();
// do not change the fading here because
// there is a different reason why we stopped this sound
if (fDist2>cs->m_fPreloadRadius2)
{
// move sound to inactive list...
//m_vecSoundSpotsInactive.push_back(cs);
pr=m_lstSoundSpotsInactive.insert(cs);
m_itLastInactivePos=pr.first;
m_nInactiveSoundSpotsSize++;
It=m_lstSoundSpotsActive.erase(It);
cs->m_State=eSoundState_Inactive;
}
else
{
++It;
}
continue; //too far
}
if (bZeroFadeCheckDist)
{
++It;
continue; // the sound is faded out to 0, we just do this check here because it might also be too far away in which case it will be moved to the inactive list again...
}
if (fDist2>cs->m_fMinRadius2)
{
//calc attenuation
//calculate sound ratio between min & max radius
if (cs->m_nFlags & FLAG_SOUND_NO_SW_ATTENUATION)
{
//fRatio=0.5f-((fDist2-cs->m_fMinRadius2)/cs->m_fMaxRadius2);//cs->m_fMaxSoundDistance2);
//if (fRatio<0)
//fRatio=0;
fRatio=1.0f;
//m_pILog->LogToConsole("fratio=%f",fRatio);
}
else
fRatio=1.0f-((fDist2-cs->m_fMinRadius2)/cs->m_fMaxRadius2);//cs->m_fMaxSoundDistance2);
//else
}
}
if (bIsSoundPotentiallyHearable)
{
// check if the sound became hearable because of sound occlusion
cs->FadeIn();
}
if (!cs->IsPlayingOnChannel() && cs->IsPlayingVirtual()) //loop check
{
cs->Play(fRatio*cs->m_fCurrentFade, false, false);
}
{
FRAME_PROFILER( "CSoundSystem::Update:Active-SetAttrib",GetSystem(),PROFILE_SOUND );
// if Steve doesnt want to change frequency and pan, this
// can be replaced just with set volume
cs->SetAttrib(cs->m_nMaxVolume,fRatio*cs->m_fCurrentFade*cs->m_fRatio,cs->m_nPan,1000,false);
}
++It;
}
//////////////////////////////////////////////////////////////////////////
// Stop expired auto stop sounds.
//////////////////////////////////////////////////////////////////////////
if (!m_autoStopSounds.empty())
{
AutoStopSounds::iterator it,next;
for (it = m_autoStopSounds.begin(); it != m_autoStopSounds.end(); it = next)
{
next = it; next++;
CSound *pSound = *it;
if (pSound->IsPlayingOnChannel())
{
bool bContinueSound = ProcessActiveSound(pSound);
if (!bContinueSound || pSound->IsPlayLengthExpired(fCurrTime))
{
UnregisterAutoStopSound( pSound );
pSound->m_bAutoStop = false;
pSound->Stop();
continue;
}
}
else if (pSound->IsUsingChannel())
{
UnregisterAutoStopSound( pSound );
pSound->m_bAutoStop = false;
pSound->Stop();
}
}
}
/////////////////////////////////////////////////////////////////////////
if (m_nLastEax!=EAX_PRESET_UNDERWATER)
{
// if not underwater, check if we now went to outside
if (bWasInside!=m_bInside)
{
if (!m_bInside)
{
// if outside,set the global EAX outdoor environment
SetEaxListenerEnvironment(m_EAXOutdoor.nPreset, (m_EAXOutdoor.nPreset==-1) ? &m_EAXOutdoor.EAX : NULL);
}
}
}
/*
if (m_nLastEax==EAX_PRESET_UNDERWATER)