forked from id-Software/Quake-III-Arena
-
-
Notifications
You must be signed in to change notification settings - Fork 521
/
snd_openal.c
2730 lines (2290 loc) · 62.1 KB
/
snd_openal.c
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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2005 Stuart Dalton (badcdev@gmail.com)
This file is part of Quake III Arena source code.
Quake III Arena source code 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 2 of the License,
or (at your option) any later version.
Quake III Arena source code 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 Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "snd_local.h"
#include "snd_codec.h"
#include "client.h"
#ifdef USE_OPENAL
#include "qal.h"
// Console variables specific to OpenAL
cvar_t *s_alPrecache;
cvar_t *s_alGain;
cvar_t *s_alSources;
cvar_t *s_alDopplerFactor;
cvar_t *s_alDopplerSpeed;
cvar_t *s_alMinDistance;
cvar_t *s_alMaxDistance;
cvar_t *s_alRolloff;
cvar_t *s_alGraceDistance;
cvar_t *s_alDriver;
cvar_t *s_alDevice;
cvar_t *s_alInputDevice;
cvar_t *s_alAvailableDevices;
cvar_t *s_alAvailableInputDevices;
static qboolean enumeration_ext = qfalse;
static qboolean enumeration_all_ext = qfalse;
#ifdef USE_VOIP
static qboolean capture_ext = qfalse;
#endif
/*
=================
S_AL_Format
=================
*/
static
ALuint S_AL_Format(int width, int channels)
{
ALuint format = AL_FORMAT_MONO16;
// Work out format
if(width == 1)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
else if(width == 2)
{
if(channels == 1)
format = AL_FORMAT_MONO16;
else if(channels == 2)
format = AL_FORMAT_STEREO16;
}
return format;
}
/*
=================
S_AL_ErrorMsg
=================
*/
static const char *S_AL_ErrorMsg(ALenum error)
{
switch(error)
{
case AL_NO_ERROR:
return "No error";
case AL_INVALID_NAME:
return "Invalid name";
case AL_INVALID_ENUM:
return "Invalid enumerator";
case AL_INVALID_VALUE:
return "Invalid value";
case AL_INVALID_OPERATION:
return "Invalid operation";
case AL_OUT_OF_MEMORY:
return "Out of memory";
default:
return "Unknown error";
}
}
/*
=================
S_AL_ClearError
=================
*/
static void S_AL_ClearError( qboolean quiet )
{
int error = qalGetError();
if( quiet )
return;
if(error != AL_NO_ERROR)
{
Com_Printf(S_COLOR_YELLOW "WARNING: unhandled AL error: %s\n",
S_AL_ErrorMsg(error));
}
}
//===========================================================================
typedef struct alSfx_s
{
char filename[MAX_QPATH];
ALuint buffer; // OpenAL buffer
snd_info_t info; // information for this sound like rate, sample count..
qboolean isDefault; // Couldn't be loaded - use default FX
qboolean isDefaultChecked; // Sound has been check if it isDefault
qboolean inMemory; // Sound is stored in memory
qboolean isLocked; // Sound is locked (can not be unloaded)
int lastUsedTime; // Time last used
int loopCnt; // number of loops using this sfx
int loopActiveCnt; // number of playing loops using this sfx
int masterLoopSrc; // All other sources looping this buffer are synced to this master src
} alSfx_t;
static qboolean alBuffersInitialised = qfalse;
// Sound effect storage, data structures
#define MAX_SFX 4096
static alSfx_t knownSfx[MAX_SFX];
static sfxHandle_t numSfx = 0;
static sfxHandle_t default_sfx;
/*
=================
S_AL_BufferFindFree
Find a free handle
=================
*/
static sfxHandle_t S_AL_BufferFindFree( void )
{
int i;
for(i = 0; i < MAX_SFX; i++)
{
// Got one
if(knownSfx[i].filename[0] == '\0')
{
if(i >= numSfx)
numSfx = i + 1;
return i;
}
}
// Shit...
Com_Error(ERR_FATAL, "S_AL_BufferFindFree: No free sound handles");
return -1;
}
/*
=================
S_AL_BufferFind
Find a sound effect if loaded, set up a handle otherwise
=================
*/
static sfxHandle_t S_AL_BufferFind(const char *filename)
{
// Look it up in the table
sfxHandle_t sfx = -1;
int i;
if ( !filename ) {
Com_Error( ERR_FATAL, "Sound name is NULL" );
}
if ( !filename[0] ) {
Com_Printf( S_COLOR_YELLOW "WARNING: Sound name is empty\n" );
return 0;
}
if ( strlen( filename ) >= MAX_QPATH ) {
Com_Printf( S_COLOR_YELLOW "WARNING: Sound name is too long: %s\n", filename );
return 0;
}
if ( filename[0] == '*' ) {
Com_Printf( S_COLOR_YELLOW "WARNING: Tried to load player sound directly: %s\n", filename );
return 0;
}
for(i = 0; i < numSfx; i++)
{
if(!Q_stricmp(knownSfx[i].filename, filename))
{
sfx = i;
break;
}
}
// Not found in table?
if(sfx == -1)
{
alSfx_t *ptr;
sfx = S_AL_BufferFindFree();
// Clear and copy the filename over
ptr = &knownSfx[sfx];
memset(ptr, 0, sizeof(*ptr));
ptr->masterLoopSrc = -1;
strcpy(ptr->filename, filename);
}
// Return the handle
return sfx;
}
/*
=================
S_AL_BufferUseDefault
=================
*/
static void S_AL_BufferUseDefault(sfxHandle_t sfx)
{
if(sfx == default_sfx)
Com_Error(ERR_FATAL, "Can't load default sound effect %s", knownSfx[sfx].filename);
Com_Printf( S_COLOR_YELLOW "WARNING: Using default sound for %s\n", knownSfx[sfx].filename);
knownSfx[sfx].isDefault = qtrue;
knownSfx[sfx].buffer = knownSfx[default_sfx].buffer;
}
/*
=================
S_AL_BufferUnload
=================
*/
static void S_AL_BufferUnload(sfxHandle_t sfx)
{
if(knownSfx[sfx].filename[0] == '\0')
return;
if(!knownSfx[sfx].inMemory)
return;
// Delete it
S_AL_ClearError( qfalse );
qalDeleteBuffers(1, &knownSfx[sfx].buffer);
if(qalGetError() != AL_NO_ERROR)
Com_Printf( S_COLOR_RED "ERROR: Can't delete sound buffer for %s\n",
knownSfx[sfx].filename);
knownSfx[sfx].inMemory = qfalse;
}
/*
=================
S_AL_BufferEvict
=================
*/
static qboolean S_AL_BufferEvict( void )
{
int i, oldestBuffer = -1;
int oldestTime = Sys_Milliseconds( );
for( i = 0; i < numSfx; i++ )
{
if( !knownSfx[ i ].filename[ 0 ] )
continue;
if( !knownSfx[ i ].inMemory )
continue;
if( knownSfx[ i ].lastUsedTime < oldestTime )
{
oldestTime = knownSfx[ i ].lastUsedTime;
oldestBuffer = i;
}
}
if( oldestBuffer >= 0 )
{
S_AL_BufferUnload( oldestBuffer );
return qtrue;
}
else
return qfalse;
}
/*
=================
S_AL_GenBuffers
=================
*/
static qboolean S_AL_GenBuffers(ALsizei numBuffers, ALuint *buffers, const char *name)
{
ALenum error;
S_AL_ClearError( qfalse );
qalGenBuffers( numBuffers, buffers );
error = qalGetError();
// If we ran out of buffers, start evicting the least recently used sounds
while( error == AL_INVALID_VALUE )
{
if( !S_AL_BufferEvict( ) )
{
Com_Printf( S_COLOR_RED "ERROR: Out of audio buffers\n");
return qfalse;
}
// Try again
S_AL_ClearError( qfalse );
qalGenBuffers( numBuffers, buffers );
error = qalGetError();
}
if( error != AL_NO_ERROR )
{
Com_Printf( S_COLOR_RED "ERROR: Can't create a sound buffer for %s - %s\n",
name, S_AL_ErrorMsg(error));
return qfalse;
}
return qtrue;
}
/*
=================
S_AL_BufferLoad
=================
*/
static void S_AL_BufferLoad(sfxHandle_t sfx, qboolean cache)
{
ALenum error;
ALuint format;
void *data;
snd_info_t info;
alSfx_t *curSfx = &knownSfx[sfx];
// Nothing?
if(curSfx->filename[0] == '\0')
return;
// Already done?
if((curSfx->inMemory) || (curSfx->isDefault) || (!cache && curSfx->isDefaultChecked))
return;
// Try to load
data = S_CodecLoad(curSfx->filename, &info);
if(!data)
{
S_AL_BufferUseDefault(sfx);
return;
}
curSfx->isDefaultChecked = qtrue;
if (!cache)
{
// Don't create AL cache
Hunk_FreeTempMemory(data);
return;
}
format = S_AL_Format(info.width, info.channels);
// Create a buffer
if (!S_AL_GenBuffers(1, &curSfx->buffer, curSfx->filename))
{
S_AL_BufferUseDefault(sfx);
Hunk_FreeTempMemory(data);
return;
}
// Fill the buffer
if( info.size == 0 )
{
// We have no data to buffer, so buffer silence
byte dummyData[ 2 ] = { 0 };
qalBufferData(curSfx->buffer, AL_FORMAT_MONO16, (void *)dummyData, 2, 22050);
}
else
qalBufferData(curSfx->buffer, format, data, info.size, info.rate);
error = qalGetError();
// If we ran out of memory, start evicting the least recently used sounds
while(error == AL_OUT_OF_MEMORY)
{
if( !S_AL_BufferEvict( ) )
{
qalDeleteBuffers(1, &curSfx->buffer);
S_AL_BufferUseDefault(sfx);
Hunk_FreeTempMemory(data);
Com_Printf( S_COLOR_RED "ERROR: Out of memory loading %s\n", curSfx->filename);
return;
}
// Try load it again
qalBufferData(curSfx->buffer, format, data, info.size, info.rate);
error = qalGetError();
}
// Some other error condition
if(error != AL_NO_ERROR)
{
qalDeleteBuffers(1, &curSfx->buffer);
S_AL_BufferUseDefault(sfx);
Hunk_FreeTempMemory(data);
Com_Printf( S_COLOR_RED "ERROR: Can't fill sound buffer for %s - %s\n",
curSfx->filename, S_AL_ErrorMsg(error));
return;
}
curSfx->info = info;
// Free the memory
Hunk_FreeTempMemory(data);
// Woo!
curSfx->inMemory = qtrue;
}
/*
=================
S_AL_BufferUse
=================
*/
static
void S_AL_BufferUse(sfxHandle_t sfx)
{
if(knownSfx[sfx].filename[0] == '\0')
return;
if((!knownSfx[sfx].inMemory) && (!knownSfx[sfx].isDefault))
S_AL_BufferLoad(sfx, qtrue);
knownSfx[sfx].lastUsedTime = Sys_Milliseconds();
}
/*
=================
S_AL_BufferInit
=================
*/
static
qboolean S_AL_BufferInit( void )
{
if(alBuffersInitialised)
return qtrue;
// Clear the hash table, and SFX table
memset(knownSfx, 0, sizeof(knownSfx));
numSfx = 0;
// Load the default sound, and lock it
default_sfx = S_AL_BufferFind("sound/feedback/hit.wav");
S_AL_BufferUse(default_sfx);
knownSfx[default_sfx].isLocked = qtrue;
// All done
alBuffersInitialised = qtrue;
return qtrue;
}
/*
=================
S_AL_BufferShutdown
=================
*/
static
void S_AL_BufferShutdown( void )
{
int i;
if(!alBuffersInitialised)
return;
// Unlock the default sound effect
knownSfx[default_sfx].isLocked = qfalse;
// Free all used effects
for(i = 0; i < numSfx; i++)
S_AL_BufferUnload(i);
// Clear the tables
numSfx = 0;
// All undone
alBuffersInitialised = qfalse;
}
/*
=================
S_AL_RegisterSound
=================
*/
static
sfxHandle_t S_AL_RegisterSound( const char *sample, qboolean compressed )
{
sfxHandle_t sfx = S_AL_BufferFind(sample);
if((!knownSfx[sfx].inMemory) && (!knownSfx[sfx].isDefault))
S_AL_BufferLoad(sfx, s_alPrecache->integer);
knownSfx[sfx].lastUsedTime = Com_Milliseconds();
if (knownSfx[sfx].isDefault) {
return 0;
}
return sfx;
}
/*
=================
S_AL_BufferGet
Return's a sfx's buffer
=================
*/
static
ALuint S_AL_BufferGet(sfxHandle_t sfx)
{
return knownSfx[sfx].buffer;
}
//===========================================================================
typedef struct src_s
{
ALuint alSource; // OpenAL source object
sfxHandle_t sfx; // Sound effect in use
int lastUsedTime; // Last time used
alSrcPriority_t priority; // Priority
int entity; // Owning entity (-1 if none)
int channel; // Associated channel (-1 if none)
qboolean isActive; // Is this source currently in use?
qboolean isPlaying; // Is this source currently playing, or stopped?
qboolean isLocked; // This is locked (un-allocatable)
qboolean isLooping; // Is this a looping effect (attached to an entity)
qboolean isTracking; // Is this object tracking its owner
qboolean isStream; // Is this source a stream
float curGain; // gain employed if source is within maxdistance.
float scaleGain; // Last gain value for this source. 0 if muted.
float lastTimePos; // On stopped loops, the last position in the buffer
int lastSampleTime; // Time when this was stopped
vec3_t loopSpeakerPos; // Origin of the loop speaker
qboolean local; // Is this local (relative to the cam)
} src_t;
#ifdef MACOS_X
#define MAX_SRC 64
#else
#define MAX_SRC 128
#endif
static src_t srcList[MAX_SRC];
static int srcCount = 0;
static int srcActiveCnt = 0;
static qboolean alSourcesInitialised = qfalse;
static int lastListenerNumber = -1;
static vec3_t lastListenerOrigin = { 0.0f, 0.0f, 0.0f };
typedef struct sentity_s
{
vec3_t origin;
qboolean srcAllocated; // If a src_t has been allocated to this entity
int srcIndex;
qboolean loopAddedThisFrame;
alSrcPriority_t loopPriority;
sfxHandle_t loopSfx;
qboolean startLoopingSound;
} sentity_t;
static sentity_t entityList[MAX_GENTITIES];
/*
=================
S_AL_SanitiseVector
=================
*/
#define S_AL_SanitiseVector(v) _S_AL_SanitiseVector(v,__LINE__)
static void _S_AL_SanitiseVector( vec3_t v, int line )
{
if( Q_isnan( v[ 0 ] ) || Q_isnan( v[ 1 ] ) || Q_isnan( v[ 2 ] ) )
{
Com_DPrintf( S_COLOR_YELLOW "WARNING: vector with one or more NaN components "
"being passed to OpenAL at %s:%d -- zeroing\n", __FILE__, line );
VectorClear( v );
}
}
/*
=================
S_AL_Gain
Set gain to 0 if muted, otherwise set it to given value.
=================
*/
static void S_AL_Gain(ALuint source, float gainval)
{
if(s_muted->integer)
qalSourcef(source, AL_GAIN, 0.0f);
else
qalSourcef(source, AL_GAIN, gainval);
}
/*
=================
S_AL_ScaleGain
Adapt the gain if necessary to get a quicker fadeout when the source is too far away.
=================
*/
static void S_AL_ScaleGain(src_t *chksrc, vec3_t origin)
{
float distance;
if(!chksrc->local)
distance = Distance(origin, lastListenerOrigin);
// If we exceed a certain distance, scale the gain linearly until the sound
// vanishes into nothingness.
if(!chksrc->local && (distance -= s_alMaxDistance->value) > 0)
{
float scaleFactor;
if(distance >= s_alGraceDistance->value)
scaleFactor = 0.0f;
else
scaleFactor = 1.0f - distance / s_alGraceDistance->value;
scaleFactor *= chksrc->curGain;
if(chksrc->scaleGain != scaleFactor)
{
chksrc->scaleGain = scaleFactor;
S_AL_Gain(chksrc->alSource, chksrc->scaleGain);
}
}
else if(chksrc->scaleGain != chksrc->curGain)
{
chksrc->scaleGain = chksrc->curGain;
S_AL_Gain(chksrc->alSource, chksrc->scaleGain);
}
}
/*
=================
S_AL_HearingThroughEntity
Also see S_Base_HearingThroughEntity
=================
*/
static qboolean S_AL_HearingThroughEntity( int entityNum )
{
float distanceSq;
if( lastListenerNumber == entityNum )
{
// This is an outrageous hack to detect
// whether or not the player is rendering in third person or not. We can't
// ask the renderer because the renderer has no notion of entities and we
// can't ask cgame since that would involve changing the API and hence mod
// compatibility. I don't think there is any way around this, but I'll leave
// the FIXME just in case anyone has a bright idea.
distanceSq = DistanceSquared(
entityList[ entityNum ].origin,
lastListenerOrigin );
if( distanceSq > THIRD_PERSON_THRESHOLD_SQ )
return qfalse; //we're the player, but third person
else
return qtrue; //we're the player
}
else
return qfalse; //not the player
}
/*
=================
S_AL_SrcInit
=================
*/
static
qboolean S_AL_SrcInit( void )
{
int i;
int limit;
// Clear the sources data structure
memset(srcList, 0, sizeof(srcList));
srcCount = 0;
srcActiveCnt = 0;
// Cap s_alSources to MAX_SRC
limit = s_alSources->integer;
if(limit > MAX_SRC)
limit = MAX_SRC;
else if(limit < 16)
limit = 16;
S_AL_ClearError( qfalse );
// Allocate as many sources as possible
for(i = 0; i < limit; i++)
{
qalGenSources(1, &srcList[i].alSource);
if(qalGetError() != AL_NO_ERROR)
break;
srcCount++;
}
// All done. Print this for informational purposes
Com_Printf( "Allocated %d sources.\n", srcCount);
alSourcesInitialised = qtrue;
return qtrue;
}
/*
=================
S_AL_SrcShutdown
=================
*/
static
void S_AL_SrcShutdown( void )
{
int i;
src_t *curSource;
if(!alSourcesInitialised)
return;
// Destroy all the sources
for(i = 0; i < srcCount; i++)
{
curSource = &srcList[i];
if(curSource->isLocked)
Com_DPrintf( S_COLOR_YELLOW "WARNING: Source %d is locked\n", i);
if(curSource->entity > 0)
entityList[curSource->entity].srcAllocated = qfalse;
qalSourceStop(srcList[i].alSource);
qalDeleteSources(1, &srcList[i].alSource);
}
memset(srcList, 0, sizeof(srcList));
alSourcesInitialised = qfalse;
}
/*
=================
S_AL_SrcSetup
=================
*/
static void S_AL_SrcSetup(srcHandle_t src, sfxHandle_t sfx, alSrcPriority_t priority,
int entity, int channel, qboolean local)
{
src_t *curSource;
// Set up src struct
curSource = &srcList[src];
curSource->lastUsedTime = Sys_Milliseconds();
curSource->sfx = sfx;
curSource->priority = priority;
curSource->entity = entity;
curSource->channel = channel;
curSource->isPlaying = qfalse;
curSource->isLocked = qfalse;
curSource->isLooping = qfalse;
curSource->isTracking = qfalse;
curSource->isStream = qfalse;
curSource->curGain = s_alGain->value * s_volume->value;
curSource->scaleGain = curSource->curGain;
curSource->local = local;
// Set up OpenAL source
if(sfx >= 0)
{
// Mark the SFX as used, and grab the raw AL buffer
S_AL_BufferUse(sfx);
qalSourcei(curSource->alSource, AL_BUFFER, S_AL_BufferGet(sfx));
}
qalSourcef(curSource->alSource, AL_PITCH, 1.0f);
S_AL_Gain(curSource->alSource, curSource->curGain);
qalSourcefv(curSource->alSource, AL_POSITION, vec3_origin);
qalSourcefv(curSource->alSource, AL_VELOCITY, vec3_origin);
qalSourcei(curSource->alSource, AL_LOOPING, AL_FALSE);
qalSourcef(curSource->alSource, AL_REFERENCE_DISTANCE, s_alMinDistance->value);
if(local)
{
qalSourcei(curSource->alSource, AL_SOURCE_RELATIVE, AL_TRUE);
qalSourcef(curSource->alSource, AL_ROLLOFF_FACTOR, 0.0f);
}
else
{
qalSourcei(curSource->alSource, AL_SOURCE_RELATIVE, AL_FALSE);
qalSourcef(curSource->alSource, AL_ROLLOFF_FACTOR, s_alRolloff->value);
}
}
/*
=================
S_AL_NewLoopMaster
Remove given source as loop master if it is the master and hand off master status to another source in this case.
=================
*/
static void S_AL_SaveLoopPos(src_t *dest, ALuint alSource)
{
int error;
S_AL_ClearError(qfalse);
qalGetSourcef(alSource, AL_SEC_OFFSET, &dest->lastTimePos);
if((error = qalGetError()) != AL_NO_ERROR)
{
// Old OpenAL implementations don't support AL_SEC_OFFSET
if(error != AL_INVALID_ENUM)
{
Com_Printf(S_COLOR_YELLOW "WARNING: Could not get time offset for alSource %d: %s\n",
alSource, S_AL_ErrorMsg(error));
}
dest->lastTimePos = -1;
}
else
dest->lastSampleTime = Sys_Milliseconds();
}
/*
=================
S_AL_NewLoopMaster
Remove given source as loop master if it is the master and hand off master status to another source in this case.
=================
*/
static void S_AL_NewLoopMaster(src_t *rmSource, qboolean iskilled)
{
int index;
src_t *curSource = NULL;
alSfx_t *curSfx;
curSfx = &knownSfx[rmSource->sfx];
if(rmSource->isPlaying)
curSfx->loopActiveCnt--;
if(iskilled)
curSfx->loopCnt--;
if(curSfx->loopCnt)
{
if(rmSource->priority == SRCPRI_ENTITY)
{
if(!iskilled && rmSource->isPlaying)
{
// only sync ambient loops...
// It makes more sense to have sounds for weapons/projectiles unsynced
S_AL_SaveLoopPos(rmSource, rmSource->alSource);
}
}
else if(rmSource == &srcList[curSfx->masterLoopSrc])
{
int firstInactive = -1;
// Only if rmSource was the master and if there are still playing loops for
// this sound will we need to find a new master.
if(iskilled || curSfx->loopActiveCnt)
{
for(index = 0; index < srcCount; index++)
{
curSource = &srcList[index];
if(curSource->sfx == rmSource->sfx && curSource != rmSource &&
curSource->isActive && curSource->isLooping && curSource->priority == SRCPRI_AMBIENT)
{
if(curSource->isPlaying)
{
curSfx->masterLoopSrc = index;
break;
}
else if(firstInactive < 0)
firstInactive = index;
}
}
}
if(!curSfx->loopActiveCnt)
{
if(firstInactive < 0)
{
if(iskilled)
{
curSfx->masterLoopSrc = -1;
return;
}
else
curSource = rmSource;
}
else
curSource = &srcList[firstInactive];
if(rmSource->isPlaying)
{
// this was the last not stopped source, save last sample position + time
S_AL_SaveLoopPos(curSource, rmSource->alSource);
}
else
{
// second case: all loops using this sound have stopped due to listener being of of range,
// and now the inactive master gets deleted. Just move over the soundpos settings to the
// new master.
curSource->lastTimePos = rmSource->lastTimePos;
curSource->lastSampleTime = rmSource->lastSampleTime;
}
}
}
}
else
curSfx->masterLoopSrc = -1;
}
/*
=================
S_AL_SrcKill
=================
*/
static void S_AL_SrcKill(srcHandle_t src)
{
src_t *curSource = &srcList[src];
// I'm not touching it. Unlock it first.
if(curSource->isLocked)
return;
// Remove the entity association and loop master status
if(curSource->isLooping)
{
curSource->isLooping = qfalse;
if(curSource->entity != -1)
{
sentity_t *curEnt = &entityList[curSource->entity];
curEnt->srcAllocated = qfalse;
curEnt->srcIndex = -1;
curEnt->loopAddedThisFrame = qfalse;
curEnt->startLoopingSound = qfalse;
}
S_AL_NewLoopMaster(curSource, qtrue);
}
// Stop it if it's playing
if(curSource->isPlaying)
{
qalSourceStop(curSource->alSource);
curSource->isPlaying = qfalse;