File: snd_3dras.c

package info (click to toggle)
darkplaces 0~20180412~beta1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,200 kB
  • sloc: ansic: 176,886; makefile: 485; pascal: 455; perl: 372; objc: 245; sh: 102
file content (1042 lines) | stat: -rw-r--r-- 33,603 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
// BSD

#include "quakedef.h"
#include "snd_3dras_typedefs.h"
#include "snd_3dras.h"

cvar_t bgmvolume = {CVAR_SAVE, "bgmvolume", "1", "volume of background music (such as CD music or replacement files such as sound/cdtracks/track002.ogg)"};
cvar_t mastervolume = {CVAR_SAVE, "mastervolume", "1", "master volume"};
cvar_t volume = {CVAR_SAVE, "volume", "0.7", "volume of sound effects"};
cvar_t snd_staticvolume = {CVAR_SAVE, "snd_staticvolume", "1", "volume of ambient sound effects (such as swampy sounds at the start of e1m2)"};
cvar_t snd_initialized = { CVAR_READONLY, "snd_initialized", "0", "indicates the sound subsystem is active"};
cvar_t snd_mutewhenidle = {CVAR_SAVE, "snd_mutewhenidle", "1", "whether to disable sound output when game window is inactive"};
static cvar_t snd_precache = {0, "snd_precache", "1", "loads sounds before they are used"};

static dllhandle_t   ras_dll = NULL;
// This values is used as a multiple version support check AND to check if the lib has been loaded with success (its 0 if it failed)
int ras_version;

static mempool_t *snd_mempool;
static sfx_t sfx_list ={//This is a header, never contains only useful data, only the first next (makes the code less complex, later)
	NULL, //next
	"",  //name[MAX_QPATH];
	NULL, //sounddata
	0,    //locks
	0    //flags
	//0,    //loopstart,
	//0     //total_length
};
static unsigned int channel_id_count=0;
static channel_t channel_list={
	NULL, //next
	NULL, //soundevent
	0, //entnum
	0, //entchannel
	0 //id
};
static entnum_t  entnum_list={
	NULL,// *next;
	0,   //  entnum;
	{0.0,0.0,0.0}, //lastloc
	NULL,// *soundsource;// This is also used to indicate a unused slot (when it's pointing to 0)
};

int   updatecount=0;
int   soundblocked=0;
int   openframe;
void* soundworld;
void* listener;
float listener_location   [3];

//1 qu = 0.0381 meter aka 38.1 mm
//2^17 qu's is the max map size in DP
//3DRAS uses atleast 32 bit to store it's locations.
//So the smallest possible step is 0.0381*2^17/2^(32)
// =~ 1.16 nm so let's pick 2 to be safe
static float DP_Ras_UnitSize=(float)2/1000/1000; //2 nm
static float DP_Ras_VolumeScale=0.075;
//static float QU_Size = 0.0381; //meter
//static float DP_QU_Ras_Scale=QU_Size/DP_Ras_UnitSize;
static float DP_QU_Ras_Scale=19050;
static void* (*ras_delete                     )(void*);
static int   (*ras_getversion                 )();
static void* (*ras_soundworld_new             )(SampleRate, WaveLength);
static void  (*ras_soundworld_destroy         )(void*);
static void  (*ras_soundworld_endframe        )(void*);
static int   (*ras_soundworld_beginframe      )(void*);
static void  (*ras_soundworld_setmainlistener )(void*,void*);
static void  (*ras_soundworld_setscale        )(void*,const Scale);
static void* (*ras_fileinputwhole_new         )(unsigned char*, Index);
static void* (*ras_audiodecoderwav_new        )(void*, int);
static void* (*ras_audiodecoderogg_new        )(void*);
static void* (*ras_sounddataoneshot_new       )(void*,WaveLength,Amount);
static void* (*ras_sounddataloop_new          )(void*,WaveLength,Amount);
static void* (*ras_listener_new               )(void*,Location*,Scalar*);
static void* (*ras_listener_setlocation       )(void*,Location*);
static void* (*ras_listener_setrotation       )(void*,Scalar  *,Scalar*,Scalar*);
static void* (*ras_soundsource_new            )(void*,SoundVolume,Location*);
static int   (*ras_soundsource_ended          )(void*);
static void  (*ras_soundsource_setlocation    )(void*,Location*);
static void* (*ras_soundevent_new             )(void*,void*,void*,SoundPower,Ratio);
static void  (*ras_soundevent_setsoundpower   )(void*,SoundPower);
static int   (*ras_soundevent_ended           )(void*);
static int   (*ras_setcoordinatesystem        )(Location*,Location*,Location*);
static int   (*ras_testrotation               )(Scalar  *,Scalar  *,Scalar  *);

// #define RAS_PRINT //Comment out for to not print extra crap.

static dllfunction_t ras_funcs[] =
{
	{"Delete"                                      ,(void**) &ras_delete                     },
	{"SetCoordinateSystem"                         ,(void**) &ras_setcoordinatesystem        },
	{"TestRotation"                                ,(void**) &ras_testrotation               },
	{"GetVersion"                                  ,(void**) &ras_getversion                 },
	{"SoundWorld_New"                              ,(void**) &ras_soundworld_new             },
	{"SoundWorld_Destroy"                          ,(void**) &ras_soundworld_destroy         },
	{"SoundWorld_EndFrame"                         ,(void**) &ras_soundworld_endframe        },
	{"SoundWorld_BeginFrame"                       ,(void**) &ras_soundworld_beginframe      },
	{"FileInputWhile_New"                          ,(void**) &ras_fileinputwhole_new         },
	{"AudioDecoderFileWav_New"                     ,(void**) &ras_audiodecoderwav_new        },
	{"AudioDecoderFileOgg_New"                     ,(void**) &ras_audiodecoderogg_new        },
	{"SoundDataAudioDecoderOneShot_New"            ,(void**) &ras_sounddataoneshot_new       },
	//{"SoundDataAudioDecoderFileLoop_New"           ,(void**) &ras_sounddataloop_new          },
	{"SoundWorld_SetMainListener"                  ,(void**) &ras_soundworld_setmainlistener },
	{"SoundWorld_SetScale"                         ,(void**) &ras_soundworld_setscale        },
	{"ListenerPlayer_New"                          ,(void**) &ras_listener_new               },
	{"ListenerPlayer_SetLocation"                  ,(void**) &ras_listener_setlocation       },
	{"ListenerPlayer_SetRotation_InVectors"        ,(void**) &ras_listener_setrotation       },
	{"SoundSource_Ended"                           ,(void**) &ras_soundsource_ended          },
	{"SoundSourcePoint_New"                        ,(void**) &ras_soundsource_new            },
	{"SoundSourcePoint_SetLocation"                ,(void**) &ras_soundsource_setlocation    },
	{"SoundEvent_New"                              ,(void**) &ras_soundevent_new             },
	{"SoundEvent_Ended"                            ,(void**) &ras_soundevent_ended           },
	{"SoundEvent_SetSoundPower"                    ,(void**) &ras_soundevent_setsoundpower   },
	{ NULL                                         , NULL                                      }
};
static const char* ras_dllname [] =
{
#if defined(WIN32)
		"3dras32.dll",
#elif defined(MACOSX)
		"3dras.dylib",
#else
		"3dras.so",
#endif
		NULL
};

// --- entnum_t List functions ----
void entnum_new(entnum_t** prev, entnum_t** new){ //Adds a new item to the start of the list and sets the pointers.
	(*new)=Mem_Alloc(snd_mempool,sizeof(entnum_t));
	if(&new){
		(*new)->next=entnum_list.next;
		entnum_list.next=(*new);
		(*prev)=&entnum_list;
	}else{
		Con_Printf("Could not allocate memory for a new entnum_t");
	}
}
void entnum_begin(entnum_t** prev, entnum_t** now){ //Goes to the beginning of the list and sets the pointers.
	(*prev)=&entnum_list;
	(*now )=entnum_list.next;
}
void entnum_next(entnum_t** prev, entnum_t** now){ //Goes to the next element
	(*prev)=(*now);
	(*now )=(*now)->next;
}
void entnum_delete_and_next(entnum_t** prev, entnum_t** now){ //Deletes the element and goes to the next element
	entnum_t* next;
	next=(*now)->next;
	if((*now)->rasptr) ras_delete((*now)->rasptr);
	Mem_Free(*now);
	(*now)=next;
	(*prev)->next=(*now);
}
// --- End Of entnum_t List functions ----

// --- channel_t List functions ----
void channel_new(channel_t** prev, channel_t** new){ //Adds a new item to the start of the list and sets the pointers.
	(*new)=Mem_Alloc(snd_mempool,sizeof(channel_t));
	if(&new){
		(*new)->next=channel_list.next;
		channel_list.next=(*new);
		(*prev)=&channel_list;
	}else{
		Con_Printf("Could not allocate memory for a new channel_t");
	}
}
void channel_begin(channel_t** prev, channel_t** now){ //Goes to the beginning of the list and sets the pointers.
	(*prev)=&channel_list;
	(*now )=channel_list.next;
}
void channel_next(channel_t** prev, channel_t** now){ //Goes to the next element
	(*prev)=(*now );
	(*now )=(*now)->next;
}
void channel_delete_and_next(channel_t** prev, channel_t** now){ //Deletes the element and goes to the next element
	channel_t* next;
	next=(*now)->next;
	if((*now)->rasptr) ras_delete((*now)->rasptr);
	Mem_Free(*now);
	(*now)=next;
	(*prev)->next=(*now);
}
// --- End Of channel_t List functions ----

// --- sfx_t List functions ----
void sfx_new(sfx_t** prev, sfx_t** new){ //Adds a new item to the start of the list and sets the pointers.
	(*new)=Mem_Alloc(snd_mempool,sizeof(sfx_t));
	if(&new){
		(*new)->next=sfx_list.next;
		sfx_list.next=(*new);
		(*prev)=&sfx_list;
	}else{
		Con_Printf("Could not allocate memory for a new sfx_t");
	}
}
void sfx_begin(sfx_t** prev, sfx_t** now){ //Goes to the beginning of the list and sets the pointers.
	(*prev)=&sfx_list;
	(*now )=sfx_list.next;
}
void sfx_next(sfx_t** prev, sfx_t** now){ //Goes to the next element
	(*prev)=(*now );
	(*now )=(*now)->next;
}
void sfx_delete_and_next(sfx_t** prev, sfx_t** now){ //Deletes the element and goes to the next element
	sfx_t* next;
	next=(*now)->next;
	if((*now)->rasptr) ras_delete((*now)->rasptr);
	Mem_Free(*now);
	(*now)=next;
	(*prev)->next=(*now);
}
// --- End Of sfx_t List functions ----

void channel_new_smart(channel_t** prev, channel_t** now){
	channel_new(prev,now);
	++channel_id_count;
	(*now)->id=channel_id_count;
}
void Free_Unlocked_Sfx(void){
	sfx_t *prev, *now;
	sfx_begin(&prev,&now);
	while(now){
		if(
			!(now->flags & SFXFLAG_SERVERSOUND) &&
			now->locks<=0
		){
			sfx_delete_and_next(&prev,&now);
		}else{
			sfx_next(&prev,&now);
		}
	}
}
void DP_To_Ras_Location(const float in[3],Location out[3]){
	out[0]=(Location)(in[0]*DP_QU_Ras_Scale );
	out[1]=(Location)(in[1]*DP_QU_Ras_Scale );
	out[2]=(Location)(in[2]*DP_QU_Ras_Scale );
}
void S_Restart_f(void){
	S_Shutdown();
	S_Startup();
}
static void S_Play_Common (float fvol, float attenuation){
	int i;
	char name [MAX_QPATH];
	sfx_t *sfx;
	if(ras_version>0 && ras_dll){
		#ifdef RAS_PRINT
			Con_Printf("Called S_Play_Common\n");
			Con_Printf("Does this need to be location in depend channel ?\n");
		#endif

		i = 1;
		while (i < Cmd_Argc ())
		{
			// Get the name, and appends ".wav" as an extension if there's none
			strlcpy (name, Cmd_Argv (i), sizeof (name));
			if (!strrchr (name, '.'))
				strlcat (name, ".wav", sizeof (name));
			i++;

			// If we need to get the volume from the command line
			if (fvol == -1.0f)
			{
				fvol = atof (Cmd_Argv (i));
				i++;
			}

			sfx = S_PrecacheSound (name, true, true);
			if (sfx)
				S_StartSound (-1, 0, sfx, listener_location, fvol, attenuation);
		}
	}
}
static void S_Play_f(void){
	S_Play_Common (1.0f, 1.0f);
}
static void S_Play2_f(void){
	S_Play_Common (1.0f, 0.0f);
}
static void S_PlayVol_f(void){
	S_Play_Common (-1.0f, 0.0f);
}
static void S_SoundList_f(void){
	channel_t *prev_c, *now_c;
	    sfx_t *prev_s, *now_s;
	 entnum_t *prev_e, *now_e;
	 int count_c,count_s,count_e;
	
	if(ras_version>0 && ras_dll){

		Con_Printf("Sfx (SoundDatas) :\n"
				 "------------------\n"
				 "Locks\tflags\tpointer\tName\n");
		count_s=0;
		sfx_begin(&prev_s,&now_s);
		while(now_s){
			++count_s;
			Con_Printf("%i\t%i\t%i\t%s\n",
				now_s->locks, now_s->flags, now_s->rasptr!=NULL, now_s->name
			);
			sfx_next(&prev_s,&now_s);
		}

		Con_Printf("Entnum (SoundSources) :\n"
				 "-----------------------\n"
				 "Ent\tpointer\n");
		count_e=0;
		entnum_begin(&prev_e,&now_e);
		while(now_e){
			++count_e;
			Con_Printf("%i\t%i\n",
				now_e->entnum, now_e->rasptr!=NULL
			);
			entnum_next(&prev_e,&now_e);
		}

		Con_Printf("Channels (SoundEvents) :\n"
				 "------------------------\n"
				 "Ent\tChannel\tID\tpointer\n");
		count_c=0;
		channel_begin(&prev_c,&now_c);
		while(now_c){
			++count_c;
			Con_Printf("%i\t%i\t%i\t%i\n",
				now_c->entnum, now_c->entchannel, now_c->id, now_c->rasptr!=NULL
			);
			channel_next(&prev_c,&now_c);
		}

		Con_Printf(
			"Count:\n"
			"------\n"
			"Channels: %i\n"
			"Sfx's:    %i\n"
			"Entities: %i\n",
			count_c,count_s,count_e
		);
	}
}
void Free_All_sfx(){
	sfx_t *prev, *now;
	sfx_begin(&prev,&now);
	while(now) sfx_delete_and_next(&prev,&now);
}
void Free_All_channel(){
	channel_t *prev, *now;
	channel_begin(&prev,&now);
	while(now) channel_delete_and_next(&prev,&now);
}
void Free_All_entnum(){
	entnum_t *prev, *now;
	entnum_begin(&prev,&now);
	while(now) entnum_delete_and_next(&prev,&now);
}
void S_Init (void){
	Location up[3],right[3],front[3];
	ras_version=0;
	snd_mempool = Mem_AllocPool("sound", 0, NULL);
	if(ras_dll) Con_Printf( "3D RAS already loaded ... (this indicates a bug)\n");
	if (Sys_LoadLibrary (ras_dllname, &ras_dll, ras_funcs))
	{
		Con_Printf ("Loading 3D RAS succeeded\n");
		Con_Printf ("Checking the lib version\n");
		ras_version=ras_getversion();
		if (ras_version>0){
			
			Con_Printf ("Version %i found\n",ras_version);
			Cvar_RegisterVariable(&volume);
			Cvar_RegisterVariable(&bgmvolume);
			Cvar_RegisterVariable(&mastervolume);
			Cvar_RegisterVariable(&snd_staticvolume);
			Cvar_RegisterVariable(&snd_precache);

			Cmd_AddCommand("play", S_Play_f, "play a sound at your current location (not heard by anyone else)");
			Cmd_AddCommand("snd_play", S_Play_f, "play a sound at your current location (not heard by anyone else)");
			Cmd_AddCommand("play2", S_Play2_f, "play a sound globally throughout the level (not heard by anyone else)");
			Cmd_AddCommand("snd_play2", S_Play2_f, "play a sound globally throughout the level (not heard by anyone else)");
			Cmd_AddCommand("playvol", S_PlayVol_f, "play a sound at the specified volume level at your current location (not heard by anyone else)");
			Cmd_AddCommand("snd_playvol", S_PlayVol_f, "play a sound at the specified volume level at your current location (not heard by anyone else)");
			Cmd_AddCommand("stopsound", S_StopAllSounds, "silence");
			Cmd_AddCommand("soundlist", S_SoundList_f, "list loaded sounds");
			Cmd_AddCommand("snd_stopsound", S_StopAllSounds, "silence");
			Cmd_AddCommand("snd_soundlist", S_SoundList_f, "list loaded sounds");
			Cmd_AddCommand("snd_restart", S_Restart_f, "restart sound system");
			Cmd_AddCommand("snd_shutdown", S_Shutdown, "shutdown the sound system keeping the dll loaded");
			Cmd_AddCommand("snd_startup", S_Startup, "start the sound system");
			Cmd_AddCommand("snd_unloadallsounds", S_UnloadAllSounds_f, "unload all sound files");

			//Set the coordinate system inside the lib equal to the one inside dp:
			   up[0]= 0 ,   up[1]= 0 ,    up[2]=1;
			right[0]= 0 ,right[1]=-1 , right[2]=0;
			front[0]= 1 ,front[1]= 0 , front[2]=0;
			if(ras_setcoordinatesystem(right,up,front)==0){
				Con_Printf("Failed to set the Coordinate System\n");
				ras_version=0;
			}
		}else{
			Con_Printf ("Failed to get the lib version\n");
			Sys_UnloadLibrary (&ras_dll);
			ras_dll=0;
		}
	}else{
		ras_dll=0;
		Con_Printf ("Loading 3D RAS failed\n");
		Sys_UnloadLibrary (&ras_dll);
	}
}
void S_Terminate (void){
	if(ras_dll){
		S_Shutdown();
		Free_All_sfx(); // <= The only valid place to free the sfx.
		Sys_UnloadLibrary(&ras_dll);
		ras_dll=0;
		ras_version=0;
	}
}

void S_Startup (void){
	Location loc[3]={0, 0, 0};
	Scalar   rot[4]={1.0, 0, 0, 0};
	if(ras_version>0 && ras_dll){
		channel_id_count=1;
		soundworld= ras_soundworld_new(48000,0.1);
		if(soundworld==0){
			Con_Printf("Failed to start a SoundWorld\n");
		}else{
			Con_Printf("Succeeded in starting a new SoundWorld\n");
			listener=ras_listener_new(soundworld,loc,rot);
			ras_soundworld_setmainlistener(soundworld,listener);
			openframe = ras_soundworld_beginframe(soundworld);
			ras_soundworld_setscale(soundworld,DP_Ras_UnitSize);
		}
	}
}
void S_Shutdown (void){
	if(ras_version>0 && ras_dll && soundworld){
		if(openframe) ras_soundworld_endframe(soundworld);
		
		//Order doesn't really matter because the lib takes care of the references
		//Free_All_sfx(); <= DO NOT FREE SFX ... they just keep sending in the old sfx causing havoc.
		Free_All_channel();
		Free_All_entnum();
		
		ras_soundworld_destroy(soundworld);
		soundworld=ras_delete(soundworld);
		if(soundworld){
			Con_Printf("Failed to stop the SoundWorld\n");
		}else{
			Con_Printf("Succeeded in stopping the SoundWorld\n");
		}
	}
}
void S_UnloadAllSounds_f(void){
	if(ras_version>0 && ras_dll){
		Free_All_sfx();
	}
}

void S_Update(const matrix4x4_t *listener_matrix){
	float forward   [3];
	float left      [3];
	float up        [3];
	float float3    [3];
	Location location3 [3];
	entnum_t  *prev_e, *now_e;
	channel_t *prev_c, *now_c;
	if(ras_version>0 && ras_dll && soundworld){
		Matrix4x4_ToVectors(listener_matrix,forward,left,up,listener_location); //Add the new player location.
		if(openframe){
			VectorNegate(left,left);
			DP_To_Ras_Location(listener_location,location3);
			ras_listener_setlocation(listener,location3);
			ras_listener_setrotation(listener,left,up,forward);
			/*
			Con_Printf(
				"DP:  Left={%f|%f|%f} Up={%f|%f|%f} Front={%f|%f|%f}\n",
				   left[0],   left[1],   left[2],
				     up[0],     up[1],     up[2],
				forward[0],forward[1],forward[2]
			);
			ras_testrotation(left,up,forward);
			Con_Printf(
				"RAS: Left={%f|%f|%f} Up={%f|%f|%f} Front={%f|%f|%f}\n",
				   left[0],   left[1],   left[2],
				     up[0],     up[1],     up[2],
				forward[0],forward[1],forward[2]
			);
			*/
			if(updatecount>100){
				updatecount=0;
				#ifdef RAS_PRINT
				Con_Printf("S_Update: Add a callback to SCR_CaptureVideo_SoundFrame.\n");
				Con_Printf("S_Update: Add Slomo.\n");
				Con_Printf("S_Update: Add BlockedSoundCheck.\n");
				Con_Printf("S_Update: Add Slomo(as a cvar) and pauze.\n");
				#endif
			}else{
				++updatecount;
			}
			//(15:20:31) div0: (at the moment, you can extend it to multichannel)
			//(15:20:40) div0: see S_CaptureAVISound()
			if(cl.entities){ //if there is a list of ents
				//Update all entities there position into the sound sources.
				entnum_begin(&prev_e,&now_e);
				while(now_e){
					if(!now_e->rasptr){
						Con_Printf("S_Update: Found an entnum_t without a valid RAS-ptr... This indicates a bug.\n");
						entnum_delete_and_next(&prev_e,&now_e);
					}else{ //Look for unused ent and drop them.
						if(now_e->entnum!=-1){ //Talking about an ent ? Or a static sound source ?
							if(ras_soundsource_ended(now_e->rasptr)){
									VectorCopy(cl.entities[now_e->entnum].state_current.origin,float3);
									VectorCopy(now_e->lastloc,float3);
									DP_To_Ras_Location(float3,location3);
									ras_soundsource_setlocation(now_e->rasptr,location3);
									entnum_next(&prev_e,&now_e);
							}else{
								entnum_delete_and_next(&prev_e,&now_e);
							}
						}else{
							if(ras_soundsource_ended(now_e->rasptr)){
								entnum_delete_and_next(&prev_e,&now_e);
							}else{
								entnum_next(&prev_e,&now_e);
							}
						}
					}
				}
			}else{
				Free_All_entnum();
			}
			channel_begin(&prev_c,&now_c);
			while(now_c){
				if(!now_c->rasptr){
					Con_Printf("S_Update: Found an channel_t without a valid RAS-ptr... This indicates a bug.\n");
					channel_delete_and_next(&prev_c,&now_c);
				}else{ //Look for stopped sound channels and free them
					if(ras_soundevent_ended(now_c->rasptr)){
						channel_delete_and_next(&prev_c,&now_c);
					}else{
						channel_next(&prev_c,&now_c);
					}
				}
			}
			ras_soundworld_endframe  (soundworld);
		}
		openframe =ras_soundworld_beginframe(soundworld);
	}
}
void S_ExtraUpdate (void){
	// This lib is unable to use any extra updates.
	//if(ras_version>0 && ras_dll){
	//}
}
sfx_t* S_FindName (const char *name){
	sfx_t *prev,*now;
	if(ras_version>0 && ras_dll){
		#ifdef RAS_PRINT
		Con_Printf("Called S_FindName %s\n",name);
		#endif

		if (strlen (name) >= sizeof (now->name))
		{
			Con_Printf ("S_FindName: sound name too long (%s)\n", name);
			return NULL;
		}
		
		sfx_begin(&prev,&now);
		// Seek in list
		while (now){
			if(strcmp (now->name, name)==0) return now;
			sfx_next(&prev,&now);
		}
		
		// None found in the list,
		// Add a sfx_t struct for this sound
		sfx_new(&prev,&now);
		now->locks=0;
		now->flags=0;
		now->rasptr=0;
		//sfx->looptstart=0;
		//sfx->total_length=0;
		strlcpy (now->name, name, sizeof (now->name));
		return now;
	}
	return NULL;
}
int S_LoadSound(sfx_t *sfx, int complain){
	// TODO add SCR_PushLoadingScreen, SCR_PopLoadingScreen calls to this
	fs_offset_t filesize;
	char namebuffer[MAX_QPATH +16  ];
	char filename  [MAX_QPATH +16+4];
	char fileext   [4];
	size_t len;
	unsigned char *data=NULL;
	void* file_ptr=NULL;
	void* decoder_ptr=NULL;
	if(ras_version>0 && ras_dll){

		fileext[3]=0; //Terminator
		// See if already loaded
		if (sfx->rasptr) return true;

		// LordHavoc: if the sound filename does not begin with sound/, try adding it
		if (!data && strncasecmp(sfx->name, "sound/", 6))
		{
			len = dpsnprintf (namebuffer, sizeof(namebuffer), "sound/%s", sfx->name);
			if (len < 0){ // name too long
				Con_DPrintf("S_LoadSound: name \"%s\" is too long\n", sfx->name);
				return false;
			}
			if(!data){
				data = FS_LoadFile(namebuffer, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,namebuffer+len-3,3); //Copy the extention
			}
			if(!data){ //Stick .wav to the end and try again
				memcpy(filename,namebuffer,len);
				memcpy(filename+len-4,".wav",5);
				data = FS_LoadFile(filename, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,"wav",3);
			}
			if(!data){ //Stick .ogg to the end and try again
				memcpy(filename,namebuffer,len);
				memcpy(filename+len-4,".ogg",5);
				data = FS_LoadFile(filename, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,"ogg",3);
			}
		}
		if(!data){
			// LordHavoc: then try without the added sound/ as wav and ogg
			len = dpsnprintf (namebuffer, sizeof(namebuffer), "%s", sfx->name);
			if (len < 0){ // name too long
				Con_DPrintf("S_LoadSound: name \"%s\" is too long\n", sfx->name);
				return false;
			}
			if(!data){
				data = FS_LoadFile(namebuffer, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,namebuffer+len-3,3); //Copy the file extention
			}
			if(!data){ //Stick .wav to the end
				memcpy(filename,namebuffer,len);
				memcpy(filename+len-4,".wav",5);
				data = FS_LoadFile(filename, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,"wav",3);
			}
			if(!data){ //Stick .ogg to the end
				memcpy(filename,namebuffer,len);
				memcpy(filename+len-4,".ogg",5);
				data = FS_LoadFile(filename, snd_mempool, false, &filesize);
				if(data) memcpy(fileext,"ogg",3);
			}
		}
		if (!data){
			if(complain) Con_Printf("Failed attempt load file '%s'\n",namebuffer);
		}else{ //if the file loaded: pass to RAS 3D
			file_ptr=ras_fileinputwhole_new(data,filesize);
			// There we transfered to file to RAS 3D
			// So lets free up data shall we ?
			FS_Close(data);

			if(!file_ptr){
				Con_Printf("Failed to upload file to audio lib\n");
			}else{
				if(0==strncasecmp(fileext,"wav",3)){
					decoder_ptr=ras_audiodecoderwav_new(file_ptr,true); //(true)use seek mode: some quake files are broken.
				}
				if(0==strncasecmp(fileext,"ogg",3)){
					decoder_ptr=ras_audiodecoderogg_new(file_ptr);
				}
				if(!decoder_ptr){
					Con_Printf("File succeeded to load, but no decoder available for '%s'\n",fileext);
				}else{
					#ifdef RAS_PRINT
					Con_Printf("ToDo: Add a cvar to configure the cache size and number of cache blocks.\n");
					Con_Printf("ToDo: Add support for looping sounds.\n");
					#endif
					sfx->rasptr=ras_sounddataoneshot_new(decoder_ptr,0.05,8);
				}
				file_ptr=ras_delete(file_ptr);
			}
			return !(sfx->rasptr);
		}
		return false;
	}
	return false;
}
sfx_t *S_PrecacheSound (const char *name, qboolean complain, qboolean serversound){
	sfx_t *sfx;
	if(ras_version>0 && ras_dll){
		#ifdef RAS_PRINT
		Con_Printf("Called S_PrecacheSound %s, %i, %i\n",name,complain,serversound);
		#endif
		if (name == NULL || name[0] == 0)
			return NULL;
		sfx = S_FindName (name);
		if (sfx == NULL) return NULL;
		if (lock) ++(sfx->locks);
		if (snd_precache.integer) S_LoadSound(sfx, complain);
		return sfx;
	}
	return NULL;
}
void S_ClearUsed (void){
	sfx_t *prev_s, *now_s;
	unsigned int i;

	if(ras_version>0 && ras_dll){
		Con_Printf("Called S_ClearUsed\n");
		for(i=0;i<numsounds;++i){
			Con_Printf("Loading :'%s'\n",serversound[i]);
			// Load the ambient sounds

			Con_Printf("ToDo: Load abmient sounds (Need geometry).\n");

			// Remove the SFXFLAG_SERVERSOUND flag
			sfx_begin(&prev_s,&now_s);
			while(now_s){
				if (now_s->flags & SFXFLAG_SERVERSOUND) now_s->flags &= ~SFXFLAG_SERVERSOUND;
				sfx_next(&prev_s,&now_s);
			}
		}
	}
}
void S_PurgeUnused(void){
	Free_Unlocked_Sfx();
}
qboolean S_IsSoundPrecached (const sfx_t *sfx){
	if(ras_version>0 && ras_dll){
		return !sfx->rasptr;
	}
	return 0;
}

void S_KillChannel (channel_t *now){ //Silences a SoundEvent
	if(now->rasptr){
		ras_soundevent_setsoundpower(now->rasptr,0);
		ras_delete(now->rasptr);
		now->rasptr=0;
	}else{
		Con_Printf("S_KillChannel: Warning pointer was 0 ... this indicates a bug.\n");
	}
}

int S_StartSound_OnEnt (int entnum, int entchannel, sfx_t *sfx, float fvol, float attenuation){
	 entnum_t *prev_e, *now_e;
	channel_t *prev_c, *now_c;
	Location tmp_location[3];

	//If there is a game world
	if(!cl.entities){
		Con_Printf("S_StartSound_OnEnt: no entity list exists\n");
		return -1;
	}

	// Look for the correct ent_t
	entnum_begin(&prev_e,&now_e);
	while(now_e){
		if(now_e->entnum==entnum) break;
		entnum_next(&prev_e,&now_e);
	}
	//We found no ent ...  lets make one...
	if(!now_e){
		entnum_new(&prev_e,&now_e);
		if(!now_e){
			Con_Printf("S_StartSound_OnEnt: could not make new entnum_t\n");
			return -1;
		}
		VectorCopy(cl.entities[entnum].state_current.origin, now_e->lastloc);
		DP_To_Ras_Location(now_e->lastloc,tmp_location);
		now_e->rasptr=ras_soundsource_new(soundworld,1.0,tmp_location);
		if(!now_e->rasptr){
			Con_Printf("S_StartSound_OnEnt: could not create a new sound source\n");
			return -1;
		}
	}

	//Ok now lets look for the channel.
	channel_begin(&prev_c,&now_c);
	while(now_c){
		if(
			now_c->entnum==entnum &&
			now_c->entchannel==entchannel
		) break;
		channel_next(&prev_c,&now_c);
	}
	
	if(now_c){ //O dear the channel excists ....
		S_KillChannel(now_c);
	}else{ //We found no channel .... So we need to make a new one ...
		channel_new_smart(&prev_c,&now_c);
		if(!now_c){
			Con_Printf("S_StartSound_OnEnt: could not make new channel_t\n");
			channel_delete_and_next(&prev_c,&now_c);
			return -1;
		}
		now_c->entnum    =entnum;
		now_c->entchannel=entchannel;
	}

	//Lets start the sound on the acquired sound source and channel
	now_c->rasptr=ras_soundevent_new(
		soundworld,now_e->rasptr,sfx->rasptr,fvol*DP_Ras_VolumeScale,1.0
	);
	if(!now_c->rasptr){ //Whoops, failed, lets delete this channel then.
		channel_delete_and_next(&prev_c,&now_c);
		Con_Printf("S_StartSound_OnEnt: could not make a new soundevent.\n");
		return -1;
	}
	return now_c->id;
}
int S_StartSound_OnLocation (sfx_t *sfx, vec3_t origin, float fvol, float attenuation){
	 entnum_t *prev_e, *now_e;
	channel_t *prev_c, *now_c;
	Location tmp_location[3];
	DP_To_Ras_Location(origin,tmp_location);
	
	 entnum_new      (&prev_e,&now_e);
	VectorCopy(now_e->lastloc,origin);
	now_e->entnum=-1;
	now_e->rasptr=ras_soundsource_new(soundworld,1.0,tmp_location);
	if(!now_e->rasptr){
		Con_Printf("S_StartSound_OnLocation: Could not make a new soundsource.\n");
		entnum_delete_and_next(&prev_e,&now_e);
		return -1;
	}
	channel_new_smart(&prev_c,&now_c);
	now_c->entnum=-1;
	now_c->entchannel=-1;
	now_c->rasptr =ras_soundevent_new(soundworld,now_e->rasptr,sfx->rasptr,fvol*DP_Ras_VolumeScale,1.0);
	if(!now_c->rasptr){
		 entnum_delete_and_next(&prev_e,&now_e);
		channel_delete_and_next(&prev_c,&now_c);
		Con_Printf("S_StartSound_OnLocation: Could not make a new soundevent.\n");
		return -1;
	}
	return now_c->id;
}


// Qantourisc on the wicked-quake-sound-system:
// --------------------------------------------
// entnum can be Zero or lower => This means "use the origin" so it's not tracked.
// entnum -1 is a "world" containing more then 1 soundsource.
// If channel !=  0 try to overwrite the requested channel. Otherwise play it on some random channel.
// If channel == -1 overwrite the first track of the ent.
// If a channel replacement is requested, only allow overwriting if it's owned by the same channel.
// If no channel can be replaced, pick a new one.
// Also when you overwrite a channel, that one has to stop dead.
// This function returns the channel it was played on (so it can be stopped later)
// This starts CD-music: S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
// The channel you return then, can later be requested to be stopped.

int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation){
	sfx_t *prev_s,*now_s;
	int sfx_ok;
	if(ras_version>0 && ras_dll && soundworld){
		#ifdef RAS_PRINT
		Con_Printf("Called S_StartSound %i, %i, %f, %f\n",entnum,entchannel,fvol,attenuation);
		#endif
		if(sfx==NULL){ // They pass this to me ... but WHY ? it makes no sense !
			#ifdef RAS_PRINT
			Con_Printf("S_StartSound: forgot to mention a sfx!\n");
			#endif
			return -1;
		}

		sfx_ok=0;
		sfx_begin(&prev_s,&now_s);
		while(now_s){
			if(now_s==sfx){
				sfx_ok=1;
				break;
			}
			sfx_next(&prev_s,&now_s);
		}
		if(!sfx_ok){
			Con_Printf("S_StartSound: passed illegal sfx_t!\n");
			return -1;
		}
		if (!S_LoadSound(sfx,true)) return -1;


		if(entnum!=-1){ //If we are talking about an ent
			return S_StartSound_OnEnt(entnum,entchannel,sfx,fvol,attenuation);
		}else{
			return S_StartSound_OnLocation(      sfx,origin,fvol,attenuation);
		}
	}
	Con_Printf("S_StartSound: engine not stated\n");
	return -1;
}
qboolean S_LocalSound (const char *s){
	sfx_t	*sfx;
	int		ch_ind;
	if(ras_version>0 && ras_dll){
		#ifdef RAS_PRINT
		Con_Printf("Called S_LocalSound %s\n",s);
		#endif

		sfx = S_PrecacheSound (s, true, true);
		if (!sfx)
		{
			Con_Printf("S_LocalSound: can't precache %s\n", s);
			return false;
		}

		// Local sounds must not be freed
		sfx->flags |= SFXFLAG_PERMANENTLOCK;
		#ifdef RAS_PRINT
		Con_Printf("S_LocalSound: this is still a small hack\n");
		#endif
		ch_ind = S_StartSound (cl.viewentity, 0, sfx, listener_location, 1, 0);
		if (ch_ind < 0)
			return false;

		//channels[ch_ind].flags |= CHANNELFLAG_LOCALSOUND;
		return true;
	}
	return 0;
}
void S_StaticSound (sfx_t *sfx, vec3_t origin, float fvol, float attenuation){
	//Static sounds should not be looped
	if(ras_version>0 && ras_dll){
		#ifdef RAS_PRINT
		Con_Printf("Called S_StaticSound\n");
		Con_Printf("Waiting on Qantourisc to add Static sounds in his lib.\n");
		#endif
		//Static sounds are sounds that are not pauzed, and or played locally.
	}
}
void S_StopSound (int entnum, int entchannel){
	channel_t *prev, *now;
	if(ras_version>0 && ras_dll){
		//Con_Printf("Called S_StopSound %i, %i\n",entnum,entchannel);
		channel_begin(&prev,&now);
		while(now){
			if(now->entnum==entnum && now->entchannel==entchannel) break;
			channel_next(&prev,&now);
		}
		if(now){ //If we found our to delete sound.
			S_KillChannel(now);
			channel_delete_and_next(&prev,&now);
		}else{
			Con_Printf("S_StopSound: Could not find the requested entnum-entchannel sound\n");
		}
	}
}
void S_StopAllSounds (void){
	channel_t *prev, *now;
	if(ras_version>0 && ras_dll){
		//Con_Printf("Called S_StopAllSounds\n");
		channel_begin(&prev,&now);
		while(now){
			S_KillChannel(now);
			channel_next(&prev,&now);
		}
	}
}
void S_PauseGameSounds (qboolean toggle){
	if(ras_version>0 && ras_dll){
		Con_Printf("Called S_PauseGameSounds %i\n",toggle);
		//Localsounds should not be pauzed
	}
}
void S_StopChannel (unsigned int channel_ind){
	channel_t *prev,*now;
	if(ras_version>0 && ras_dll){
		channel_begin(&prev,&now);
		while(now){
			if(now->id==channel_ind){
				S_KillChannel(now);
				channel_delete_and_next(&prev,&now);
			}else{
				channel_next(&prev,&now);
			}
		}
	}
}
qboolean S_SetChannelFlag (unsigned int ch_ind, unsigned int flag, qboolean value){
	if(ras_version>0 && ras_dll){
		Con_Printf("Called S_SetChannelFlag %u, %u, %i\n",ch_ind, flag, value);
	}
	return 0;
}
void S_SetChannelVolume (unsigned int ch_ind, float fvol){
	channel_t *prev,*now;
	if(ras_version>0 && ras_dll){
		Con_Printf("Called S_SetChannelVolume %u, %f\n",ch_ind, fvol);
		channel_begin(&prev,&now);
		while(now){
			if(now->id==ch_ind){
				if(now->rasptr){
					ras_soundevent_setsoundpower(now->rasptr,fvol*DP_Ras_VolumeScale);
				}else{
					Con_Printf("S_StopChannel: Warning pointer was 0 ... this indicates a bug.\n");
				}
			}
			channel_next(&prev,&now);
		}
	}
}

float S_GetChannelPosition (unsigned int ch_ind)
{
	// FIXME unsupported
	return -1;
}

void S_BlockSound (void){
	soundblocked++;
}
void S_UnblockSound (void){
	soundblocked--;
	if(soundblocked<0){
		Con_Printf("S_UnblockSound: Requested more S_UnblockSound then S_BlockSound.\n");
	}
}

int S_GetSoundRate (void){
	Con_Printf("Inside 3DRAS, the soundrate of the end-user is NONE of the dev's concern.\n");
	Con_Printf("So let's assume 44100.\n");
	return 44100;
}

int S_GetSoundChannels (void){
	Con_Printf("Inside 3DRAS, the soundrate of the end-user is NONE of the dev's concern.\n");
	Con_Printf("So let's assume 2.\n");
	return 2;
}

/*
====================
SndSys_SendKeyEvents

Send keyboard events originating from the sound system (e.g. MIDI)
====================
*/
void SndSys_SendKeyEvents(void)
{
	// not supported
}