-
Notifications
You must be signed in to change notification settings - Fork 1
/
s_dsp_XT.c
1325 lines (1072 loc) · 34.1 KB
/
s_dsp_XT.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 XashXT Group 2009 ©
// s_dsp.c - digital signal processing algorithms for audio FX
//=======================================================================
#include "quakedef.h"
extern portable_samplepair_t paintbuffer[];
#define SXDLY_MAX 0.400 // max delay in seconds
#define SXRVB_MAX 0.100 // max reverb reflection time
#define SXSTE_MAX 0.100 // max stereo delay line time
#define SOUNDCLIP(x) ((x) > (32767*256) ? (32767*256) : ((x) < (-32767*256) ? (-32767*256) : (x)))
#define CSXROOM 29
#define DSP_CONSTANT_GAIN 128
typedef int sample_t; // delay lines must be 32 bit, now that we have 16 bit samples
typedef struct dlyline_s
{
int cdelaysamplesmax; // size of delay line in samples
int lp; // lowpass flag 0 = off, 1 = on
int idelayinput; // i/o indices into circular delay line
int idelayoutput;
int idelayoutputxf; // crossfade output pointer
int xfade; // crossfade value
int delaysamples; // current delay setting
int delayfeed; // current feedback setting
int lp0, lp1, lp2, lp3, lp4, lp5; // lowpass filter buffer
int mod; // sample modulation count
int modcur;
sample_t *lpdelayline; // buffer
} dlyline_t;
#define ISXMONODLY 0 // mono delay line
#define ISXRVB 1 // first of the reverb delay lines
#define CSXRVBMAX 2
#define ISXSTEREODLY 3 // 50ms left side delay
#define CSXDLYMAX 4
dlyline_t rgsxdly[CSXDLYMAX]; // array of delay lines
#define gdly0 (rgsxdly[ISXMONODLY])
#define gdly1 (rgsxdly[ISXRVB])
#define gdly2 (rgsxdly[ISXRVB + 1])
#define gdly3 (rgsxdly[ISXSTEREODLY])
#define CSXLPMAX 10 // lowpass filter memory
int rgsxlp[CSXLPMAX];
int sxamodl, sxamodr; // amplitude modulation values
int sxamodlt, sxamodrt; // modulation targets
int sxmod1, sxmod2;
int sxmod1cur, sxmod2cur;
// Mono Delay parameters
cvar_t sxdly_delay = { "room_delay", "0" }; // current delay in seconds
cvar_t sxdly_feedback = { "room_feedback", "0.2" }; // cyles
cvar_t sxdly_lp = { "room_dlylp", "1" }; // lowpass filter
float sxdly_delayprev;
// Mono Reverb parameters
cvar_t sxrvb_size = { "room_size", "0" }; // room size 0 (off) 0.1 small - 0.35 huge
cvar_t sxrvb_feedback = { "room_refl", "0.7" }; // reverb decay 0.1 short - 0.9 long
cvar_t sxrvb_lp = { "room_rvblp", "1" }; // lowpass filter
float sxrvb_sizeprev;
// stereo delay (no feedback)
cvar_t sxste_delay = { "room_left", "0" }; // straight left delay
float sxste_delayprev;
// Underwater/special fx modulations
cvar_t sxmod_lowpass = { "room_lp", "0" };
cvar_t sxmod_mod = { "room_mod", "0" };
int sxroom_typeprev;
// Main interface
cvar_t sxroom_type = { "room_type", "0" }; // legacy support
cvar_t sxroomwater_type = { "waterroom_type","15" }; // legacy support
cvar_t sxroom_off = { "room_off", "0" }; // legacy support
qboolean SXDLY_Init( int idelay, float delay );
void SXDLY_Free( int idelay );
void SXDLY_DoDelay( int count );
void SXRVB_DoReverb( int count );
void SXDLY_DoStereoDelay( int count );
void SXRVB_DoAMod( int count );
//=====================================================================
// Init/release all structures for sound effects
//=====================================================================
void SX_Init( void )
{
Q_memset(rgsxdly, 0, sizeof (dlyline_t) * CSXDLYMAX);
Q_memset(rgsxlp, 0, sizeof(int) * CSXLPMAX);
sxdly_delayprev = -1.0;
sxrvb_sizeprev = -1.0;
sxste_delayprev = -1.0;
sxroom_typeprev = -1.0;
// initialize SX cvars
Cvar_RegisterVariable (&sxdly_delay);
Cvar_RegisterVariable (&sxdly_feedback);
Cvar_RegisterVariable (&sxdly_lp);
Cvar_RegisterVariable (&sxrvb_size);
Cvar_RegisterVariable (&sxrvb_feedback);
Cvar_RegisterVariable (&sxrvb_lp);
Cvar_RegisterVariable (&sxste_delay);
Cvar_RegisterVariable (&sxmod_lowpass);
Cvar_RegisterVariable (&sxmod_mod);
Cvar_RegisterVariable (&sxroom_type);
Cvar_RegisterVariable (&sxroomwater_type);
Cvar_RegisterVariable (&sxroom_off);
// init amplitude modulation params
sxamodl = sxamodr = 255;
sxamodlt = sxamodrt = 255;
sxmod1 = 350 * (shm->speed / 11025); // 11k was the original sample rate all dsp was tuned at
sxmod2 = 450 * (shm->speed / 11025);
sxmod1cur = sxmod1;
sxmod2cur = sxmod2;
Con_Printf("FX Processor Initialized\n" );
}
void SX_Free( void )
{
int i;
// release mono delay line
SXDLY_Free( ISXMONODLY );
// release reverb lines
for( i = 0; i < CSXRVBMAX; i++ )
SXDLY_Free( i + ISXRVB );
SXDLY_Free( ISXSTEREODLY );
}
// Set up a delay line buffer allowing a max delay of 'delay' seconds
// Frees current buffer if it already exists. idelay indicates which of
// the available delay lines to init.
qboolean SXDLY_Init( int idelay, float delay )
{
size_t cbsamples;
byte *lpData;
dlyline_t *pdly;
pdly = &(rgsxdly[idelay]);
if( delay > SXDLY_MAX )
delay = SXDLY_MAX;
if( pdly->lpdelayline )
{
free( pdly->lpdelayline );
pdly->lpdelayline = NULL;
}
if( delay == 0.0 )
return true;
pdly->cdelaysamplesmax = shm->speed * delay;
pdly->cdelaysamplesmax += 1;
cbsamples = pdly->cdelaysamplesmax * sizeof( sample_t );
lpData = malloc(cbsamples);
if (!lpData)
{
Con_Printf("Sound FX: Out of memory.\n");
return false;
}
Q_memset (lpData, 0, cbsamples);
pdly->lpdelayline = (sample_t *)lpData;
// init delay loop input and output counters.
// NOTE: init of idelayoutput only valid if pdly->delaysamples is set
// NOTE: before this call!
pdly->idelayinput = 0;
pdly->idelayoutput = pdly->cdelaysamplesmax - pdly->delaysamples;
pdly->xfade = 0;
pdly->lp = 1;
pdly->mod = 0;
pdly->modcur = 0;
// init lowpass filter memory
pdly->lp0 = pdly->lp1 = pdly->lp2 = pdly->lp3 = pdly->lp4 = pdly->lp5 = 0;
return true;
}
// release delay buffer and deactivate delay
void SXDLY_Free( int idelay )
{
dlyline_t *pdly = &(rgsxdly[idelay]);
if( pdly->lpdelayline )
{
free( pdly->lpdelayline );
pdly->lpdelayline = NULL; // this deactivates the delay
}
}
// check for new stereo delay param
void SXDLY_CheckNewStereoDelayVal( void )
{
dlyline_t *pdly = &(rgsxdly[ISXSTEREODLY]);
int delaysamples;
if( shm->channels < 2 )
return;
// set up stereo delay
if (sxste_delay.value != sxste_delayprev)
{
if( sxste_delay.value == 0.0 )
{
// deactivate delay line
SXDLY_Free( ISXSTEREODLY );
sxste_delayprev = 0.0;
}
else
{
delaysamples = fmin( sxste_delay.value, SXSTE_MAX ) * shm->speed;
// init delay line if not active
if( pdly->lpdelayline == NULL )
{
pdly->delaysamples = delaysamples;
SXDLY_Init( ISXSTEREODLY, SXSTE_MAX );
}
// do crossfade to new delay if delay has changed
if( delaysamples != pdly->delaysamples )
{
// set up crossfade from old pdly->delaysamples to new delaysamples
pdly->idelayoutputxf = pdly->idelayinput - delaysamples;
if( pdly->idelayoutputxf < 0 )
pdly->idelayoutputxf += pdly->cdelaysamplesmax;
pdly->xfade = 128;
}
sxste_delayprev = sxste_delay.value;
pdly->mod = 0;
pdly->modcur = pdly->mod;
// deactivate line if rounded down to 0 delay
if( pdly->delaysamples == 0 )
SXDLY_Free( ISXSTEREODLY );
}
}
}
// stereo delay, left channel only, no feedback
void SXDLY_DoStereoDelay( int count )
{
int left;
sample_t sampledly;
sample_t samplexf;
portable_samplepair_t *pbuf;
int countr;
if( shm->channels < 2 )
return;
// process delay line if active
if( rgsxdly[ISXSTEREODLY].lpdelayline )
{
pbuf = paintbuffer;
countr = count;
// process each sample in the paintbuffer...
while( countr-- )
{
if( gdly3.mod && ( --gdly3.modcur < 0 ))
gdly3.modcur = gdly3.mod;
// get delay line sample from left line
sampledly = *(gdly3.lpdelayline + gdly3.idelayoutput);
left = pbuf->left;
// only process if left value or delayline value are non-zero or xfading
if( gdly3.xfade || sampledly || left )
{
// if we're not crossfading, and we're not modulating, but we'd like to be modulating,
// then setup a new crossfade.
if( !gdly3.xfade && !gdly3.modcur && gdly3.mod )
{
// set up crossfade to new delay value, if we're not already doing an xfade
gdly3.idelayoutputxf = gdly3.idelayoutput + ((RandomLong(0, 0xFF) * gdly3.delaysamples) >> 9); // 100 = ~ 9ms
if( gdly3.idelayoutputxf >= gdly3.cdelaysamplesmax )
gdly3.idelayoutputxf -= gdly3.cdelaysamplesmax;
gdly3.xfade = 128;
}
// modify sampledly if crossfading to new delay value
if( gdly3.xfade )
{
samplexf = (*(gdly3.lpdelayline + gdly3.idelayoutputxf) * (128 - gdly3.xfade)) >> 7;
sampledly = ((sampledly * gdly3.xfade) >> 7) + samplexf;
if( ++gdly3.idelayoutputxf >= gdly3.cdelaysamplesmax )
gdly3.idelayoutputxf = 0;
if( --gdly3.xfade == 0 )
gdly3.idelayoutput = gdly3.idelayoutputxf;
}
// save output value into delay line
// left = CLIP(left);
*(gdly3.lpdelayline + gdly3.idelayinput) = SOUNDCLIP( left );
// save delay line sample into output buffer
pbuf->left = SOUNDCLIP( sampledly );
}
else
{
// keep clearing out delay line, even if no signal in or out
*(gdly3.lpdelayline + gdly3.idelayinput) = 0;
}
// update delay buffer pointers
if( ++gdly3.idelayinput >= gdly3.cdelaysamplesmax )
gdly3.idelayinput = 0;
if( ++gdly3.idelayoutput >= gdly3.cdelaysamplesmax )
gdly3.idelayoutput = 0;
pbuf++;
}
}
}
// If sxdly_delay or sxdly_feedback have changed, update delaysamples
// and delayfeed values. This applies only to delay 0, the main echo line.
void SXDLY_CheckNewDelayVal( void )
{
dlyline_t *pdly = &(rgsxdly[ISXMONODLY]);
if (sxdly_delay.value != sxdly_delayprev)
{
if( sxdly_delay.value == 0.0f )
{
// deactivate delay line
SXDLY_Free( ISXMONODLY );
sxdly_delayprev = sxdly_delay.value;
}
else
{
// init delay line if not active
pdly->delaysamples = fmin( sxdly_delay.value, SXDLY_MAX ) * shm->speed;
if( pdly->lpdelayline == NULL )
SXDLY_Init( ISXMONODLY, SXDLY_MAX );
// flush delay line and filters
if( pdly->lpdelayline )
{
memset( pdly->lpdelayline, 0, pdly->cdelaysamplesmax * sizeof( sample_t ));
pdly->lp0 = 0;
pdly->lp1 = 0;
pdly->lp2 = 0;
pdly->lp3 = 0;
pdly->lp4 = 0;
pdly->lp5 = 0;
}
// init delay loop input and output counters
pdly->idelayinput = 0;
pdly->idelayoutput = pdly->cdelaysamplesmax - pdly->delaysamples;
sxdly_delayprev = sxdly_delay.value;
// deactivate line if rounded down to 0 delay
if( pdly->delaysamples == 0 )
SXDLY_Free( ISXMONODLY );
}
}
pdly->lp = (int)sxdly_lp.value;
pdly->delayfeed = sxdly_feedback.value * 255;
}
// This routine updates both left and right output with
// the mono delayed signal. Delay is set through console vars room_delay
// and room_feedback.
void SXDLY_DoDelay( int count )
{
int val[2];
int valt, valt2;
int samp[2];
int sampsum, dfdly;
sample_t sampledly;
portable_samplepair_t *pbuf;
int countr;
int gain;
Q_memset(samp, 0, sizeof(samp));
Q_memset(val, 0, sizeof(val));
// process mono delay line if active
if( rgsxdly[ISXMONODLY].lpdelayline )
{
gain = DSP_CONSTANT_GAIN;
pbuf = paintbuffer;
countr = count;
// process each sample in the paintbuffer...
while( countr-- )
{
// get delay line sample
sampledly = *(gdly0.lpdelayline + gdly0.idelayoutput);
if( shm->channels == 2 )
{
samp[0] = pbuf->left;
samp[1] = pbuf->right;
sampsum = samp[0] + samp[1];
}
else
{
samp[0] = pbuf->left;
sampsum = samp[0];
}
// only process if delay line and paintbuffer samples are non zero
if( sampledly || sampsum )
{
// get current sample from delay buffer
dfdly = ((gdly0.delayfeed * sampledly) >> 8);
if( shm->channels == 2 )
{
val[0] = SOUNDCLIP( samp[0] + dfdly );
val[1] = SOUNDCLIP( samp[1] + dfdly );
valt = SOUNDCLIP(( sampsum + (dfdly * 2)) >> 1);
}
else
{
val[0] = SOUNDCLIP(samp[0] + dfdly);
valt = val[0];
}
// lowpass
if( gdly0.lp )
{
if( shm->channels == 2 )
{
val[0] = (gdly0.lp0 + gdly0.lp1 + gdly0.lp2 + gdly0.lp3 + val[0]) / 5;
val[1] = (gdly0.lp0 + gdly0.lp1 + gdly0.lp2 + gdly0.lp3 + val[1]) / 5;
valt2 = (val[0] + val[1]) >> 1;
}
else
{
val[0] = (gdly0.lp0 + gdly0.lp1 + gdly0.lp2 + gdly0.lp3 + val[0]) / 5;
valt2 = val[0];
}
gdly0.lp0 = gdly0.lp1;
gdly0.lp1 = gdly0.lp2;
gdly0.lp2 = gdly0.lp3;
gdly0.lp3 = valt;
}
else
{
valt2 = valt;
}
// store delay output value into output buffer
*(gdly0.lpdelayline + gdly0.idelayinput) = SOUNDCLIP( valt2 );
// decrease output value by max gain of delay with feedback
// to provide for unity gain reverb
// note: this gain varies with the feedback value.
if( shm->channels == 2 )
{
pbuf->left = ((val[0] * gain) >> 8);
pbuf->right = ((val[1] * gain) >> 8);
}
else
{
pbuf->left = ((val[0] * gain) >> 8);
}
}
else
{
// not playing samples, but must still flush lowpass buffer and delay line
gdly0.lp0 = gdly0.lp1 = gdly0.lp2 = gdly0.lp3 = 0;
*(gdly0.lpdelayline + gdly0.idelayinput) = valt2;
}
// update delay buffer pointers
if( ++gdly0.idelayinput >= gdly0.cdelaysamplesmax )
gdly0.idelayinput = 0;
if( ++gdly0.idelayoutput >= gdly0.cdelaysamplesmax )
gdly0.idelayoutput = 0;
pbuf++;
}
}
}
// check for a parameter change on the reverb processor
#define RVB_XFADE (32 * shm->speed / 11025) // xfade time between new delays
#define RVB_MODRATE1 (500 * (shm->speed / 11025)) // how often, in samples, to change delay (1st rvb)
#define RVB_MODRATE2 (700 * (shm->speed / 11025)) // how often, in samples, to change delay (2nd rvb)
void SXRVB_CheckNewReverbVal( void )
{
dlyline_t *pdly;
int delaysamples;
int i, mod;
if (sxrvb_size.value != sxrvb_sizeprev)
{
sxrvb_sizeprev = sxrvb_size.value;
if (sxrvb_size.value == 0.0)
{
// deactivate all delay lines
SXDLY_Free( ISXRVB );
SXDLY_Free( ISXRVB + 1 );
}
else
{
for( i = ISXRVB; i < ISXRVB + CSXRVBMAX; i++ )
{
// init delay line if not active
pdly = &(rgsxdly[i]);
switch( i )
{
case ISXRVB:
delaysamples = fmin(sxrvb_size.value, SXRVB_MAX) * shm->speed;
pdly->mod = RVB_MODRATE1;
break;
case ISXRVB+1:
delaysamples = fmin(sxrvb_size.value * 0.71, SXRVB_MAX) * shm->speed;
pdly->mod = RVB_MODRATE2;
break;
default:
delaysamples = 0;
break;
}
mod = pdly->mod;
if( pdly->lpdelayline == NULL )
{
pdly->delaysamples = delaysamples;
SXDLY_Init( i, SXRVB_MAX );
}
pdly->modcur = pdly->mod = mod;
// do crossfade to new delay if delay has changed
if( delaysamples != pdly->delaysamples )
{
// set up crossfade from old pdly->delaysamples to new delaysamples
pdly->idelayoutputxf = pdly->idelayinput - delaysamples;
if( pdly->idelayoutputxf < 0 )
pdly->idelayoutputxf += pdly->cdelaysamplesmax;
pdly->xfade = RVB_XFADE;
}
// deactivate line if rounded down to 0 delay
if( pdly->delaysamples == 0 )
SXDLY_Free( i );
}
}
}
rgsxdly[ISXRVB].delayfeed = (sxrvb_feedback.value) * 255;
rgsxdly[ISXRVB].lp = sxrvb_lp.value;
rgsxdly[ISXRVB + 1].delayfeed = (sxrvb_feedback.value) * 255;
rgsxdly[ISXRVB + 1].lp = sxrvb_lp.value;
}
// main routine for updating the paintbuffer with new reverb values.
// This routine updates both left and right lines with
// the mono reverb signal. Delay is set through console vars room_reverb
// and room_feedback. 2 reverbs operating in parallel.
void SXRVB_DoReverbMono( int count )
{
portable_samplepair_t *pbuf;
int val;
int valt;
int mono;
sample_t sampledly;
sample_t samplexf;
int countr;
int voutm;
int gain;
// process reverb lines if active
if( rgsxdly[ISXRVB].lpdelayline )
{
gain = DSP_CONSTANT_GAIN;
pbuf = paintbuffer;
countr = count;
// process each sample in the paintbuffer...
while( countr-- )
{
mono = pbuf->left;
voutm = 0;
if( --gdly1.modcur < 0 )
gdly1.modcur = gdly1.mod;
// ========================== ISXRVB============================
// get sample from delay line
sampledly = *(gdly1.lpdelayline + gdly1.idelayoutput);
// only process if something is non-zero
if( gdly1.xfade || sampledly || mono )
{
// modulate delay rate
// UNDONE: modulation disabled
if( 0 && !gdly1.xfade && !gdly1.modcur && gdly1.mod )
{
// set up crossfade to new delay value, if we're not already doing an xfade
gdly1.idelayoutputxf = gdly1.idelayoutput + ((RandomLong(0, 0xFF) * gdly1.delaysamples) >> 9); // 100 = ~ 9ms
if( gdly1.idelayoutputxf >= gdly1.cdelaysamplesmax )
gdly1.idelayoutputxf -= gdly1.cdelaysamplesmax;
gdly1.xfade = RVB_XFADE;
}
// modify sampledly if crossfading to new delay value
if( gdly1.xfade )
{
samplexf = (*(gdly1.lpdelayline + gdly1.idelayoutputxf) * (RVB_XFADE - gdly1.xfade)) / RVB_XFADE;
sampledly = ((sampledly * gdly1.xfade) / RVB_XFADE) + samplexf;
if( ++gdly1.idelayoutputxf >= gdly1.cdelaysamplesmax )
gdly1.idelayoutputxf = 0;
if( --gdly1.xfade == 0 )
gdly1.idelayoutput = gdly1.idelayoutputxf;
}
if( sampledly )
{
// get current sample from delay buffer
// calculate delayed value
val = SOUNDCLIP(mono + ((gdly1.delayfeed * sampledly) >> 8));
}
else
{
val = mono;
}
// lowpass
if( gdly1.lp )
{
valt = (gdly1.lp0 + gdly1.lp1 + (val<<1)) >> 2;
gdly1.lp1 = gdly1.lp0;
gdly1.lp0 = val;
}
else
{
valt = val;
}
// store delay output value into output buffer
*(gdly1.lpdelayline + gdly1.idelayinput) = valt;
voutm = valt;
}
else
{
// not playing samples, but still must flush lowpass buffer & delay line
gdly1.lp0 = gdly1.lp1 = 0;
*(gdly1.lpdelayline + gdly1.idelayinput) = 0;
voutm = 0;
}
// update delay buffer pointers
if( ++gdly1.idelayinput >= gdly1.cdelaysamplesmax )
gdly1.idelayinput = 0;
if( ++gdly1.idelayoutput >= gdly1.cdelaysamplesmax )
gdly1.idelayoutput = 0;
// ========================== ISXRVB + 1========================
if( --gdly2.modcur < 0 )
gdly2.modcur = gdly2.mod;
if( gdly2.lpdelayline )
{
// get sample from delay line
sampledly = *(gdly2.lpdelayline + gdly2.idelayoutput);
// only process if something is non-zero
if( gdly2.xfade || sampledly || mono )
{
// UNDONE: modulation disabled
if( 0 && !gdly2.xfade && gdly2.modcur && gdly2.mod )
{
// set up crossfade to new delay value, if we're not already doing an xfade
gdly2.idelayoutputxf = gdly2.idelayoutput + ((RandomLong(0,0xFF) * gdly2.delaysamples) >> 9); // 100 = ~ 9ms
if( gdly2.idelayoutputxf >= gdly2.cdelaysamplesmax )
gdly2.idelayoutputxf -= gdly2.cdelaysamplesmax;
gdly2.xfade = RVB_XFADE;
}
// modify sampledly if crossfading to new delay value
if( gdly2.xfade )
{
samplexf = (*(gdly2.lpdelayline + gdly2.idelayoutputxf) * (RVB_XFADE - gdly2.xfade)) / RVB_XFADE;
sampledly = ((sampledly * gdly2.xfade) / RVB_XFADE) + samplexf;
if( ++gdly2.idelayoutputxf >= gdly2.cdelaysamplesmax )
gdly2.idelayoutputxf = 0;
if( --gdly2.xfade == 0 )
gdly2.idelayoutput = gdly2.idelayoutputxf;
}
if( sampledly )
{
// get current sample from delay buffer
val = SOUNDCLIP(mono + ((gdly2.delayfeed * sampledly) >> 8));
}
else
{
val = mono;
}
// lowpass
if( gdly2.lp )
{
valt = (gdly2.lp0 + gdly2.lp1 + (val<<1)) >> 2;
gdly2.lp0 = val;
}
else
{
valt = val;
}
// store delay output value into output buffer
*(gdly2.lpdelayline + gdly2.idelayinput) = valt;
voutm += valt;
}
else
{
// not playing samples, but still must flush lowpass buffer
gdly2.lp0 = gdly2.lp1 = 0;
*(gdly2.lpdelayline + gdly2.idelayinput) = 0;
}
// update delay buffer pointers
if( ++gdly2.idelayinput >= gdly2.cdelaysamplesmax )
gdly2.idelayinput = 0;
if( ++gdly2.idelayoutput >= gdly2.cdelaysamplesmax )
gdly2.idelayoutput = 0;
}
// ============================ Mix ================================
// add mono delay to left and right channels
// drop output by inverse of cascaded gain for both reverbs
voutm = (gain * voutm) >> 8;
pbuf->left = SOUNDCLIP( voutm );
pbuf++;
}
}
}
void SXRVB_DoReverb( int count )
{
int val[2];
int valt[2];
int left;
int right;
sample_t sampledly;
sample_t samplexf;
portable_samplepair_t *pbuf;
int countr;
int voutm[2];
int gain;
if( shm->channels < 2 )
{
SXRVB_DoReverbMono( count );
return;
}
// process reverb lines if active
if( rgsxdly[ISXRVB].lpdelayline )
{
gain = DSP_CONSTANT_GAIN;
pbuf = paintbuffer;
countr = count;
// process each sample in the paintbuffer...
while( countr-- )
{
left = pbuf->left;
right = pbuf->right;
voutm[0] = 0;
voutm[1] = 0;
if( --gdly1.modcur < 0 )
gdly1.modcur = gdly1.mod;
// ========================== ISXRVB============================
// get sample from delay line
sampledly = *(gdly1.lpdelayline + gdly1.idelayoutput);
// only process if something is non-zero
if( gdly1.xfade || sampledly || left || right )
{
// modulate delay rate
// UNDONE: modulation disabled
if( 0 && !gdly1.xfade && !gdly1.modcur && gdly1.mod )
{
// set up crossfade to new delay value, if we're not already doing an xfade
gdly1.idelayoutputxf = gdly1.idelayoutput + ((RandomLong(0, 0xFF) * gdly1.delaysamples) >> 9); // 100 = ~ 9ms
if( gdly1.idelayoutputxf >= gdly1.cdelaysamplesmax )
gdly1.idelayoutputxf -= gdly1.cdelaysamplesmax;
gdly1.xfade = RVB_XFADE;
}
// modify sampledly if crossfading to new delay value
if( gdly1.xfade )
{
samplexf = (*(gdly1.lpdelayline + gdly1.idelayoutputxf) * (RVB_XFADE - gdly1.xfade)) / RVB_XFADE;
sampledly = ((sampledly * gdly1.xfade) / RVB_XFADE) + samplexf;
if( ++gdly1.idelayoutputxf >= gdly1.cdelaysamplesmax )
gdly1.idelayoutputxf = 0;
if( --gdly1.xfade == 0 )
gdly1.idelayoutput = gdly1.idelayoutputxf;
}
if( sampledly )
{
// get current sample from delay buffer
// calculate delayed value
val[0] = SOUNDCLIP(left + ((gdly1.delayfeed * sampledly) >> 8));
val[1] = SOUNDCLIP(right + ((gdly1.delayfeed * sampledly) >> 8));
}
else
{
val[0] = left;
val[1] = right;
}
// lowpass
if( gdly1.lp )
{
valt[0] = (gdly1.lp0 + gdly1.lp1 + (val[0]<<1)) >> 2;
valt[1] = (gdly1.lp0 + gdly1.lp1 + (val[1]<<1)) >> 2;
gdly1.lp1 = gdly1.lp0;
gdly1.lp0 = (val[0] + val[1]) >> 1;
}
else
{
valt[0] = val[0];
valt[1] = val[1];
}
// store delay output value into output buffer
*(gdly1.lpdelayline + gdly1.idelayinput) = (valt[0] + valt[1]) >> 1;
voutm[0] = valt[0];
voutm[1] = valt[1];
}
else
{
// not playing samples, but still must flush lowpass buffer & delay line
gdly1.lp0 = gdly1.lp1 = 0;
*(gdly1.lpdelayline + gdly1.idelayinput) = 0;
voutm[0] = 0;
voutm[1] = 0;
}
// update delay buffer pointers
if( ++gdly1.idelayinput >= gdly1.cdelaysamplesmax )
gdly1.idelayinput = 0;
if( ++gdly1.idelayoutput >= gdly1.cdelaysamplesmax )
gdly1.idelayoutput = 0;
// ========================== ISXRVB + 1========================
if( --gdly2.modcur < 0 )
gdly2.modcur = gdly2.mod;
if( gdly2.lpdelayline )
{
// get sample from delay line
sampledly = *(gdly2.lpdelayline + gdly2.idelayoutput);
// only process if something is non-zero
if( gdly2.xfade || sampledly || left || right )
{
// UNDONE: modulation disabled
if( 0 && !gdly2.xfade && gdly2.modcur && gdly2.mod )
{
// set up crossfade to new delay value, if we're not already doing an xfade
gdly2.idelayoutputxf = gdly2.idelayoutput + ((RandomLong(0,0xFF) * gdly2.delaysamples) >> 9); // 100 = ~ 9ms
if( gdly2.idelayoutputxf >= gdly2.cdelaysamplesmax )
gdly2.idelayoutputxf -= gdly2.cdelaysamplesmax;
gdly2.xfade = RVB_XFADE;
}
// modify sampledly if crossfading to new delay value
if( gdly2.xfade )
{
samplexf = (*(gdly2.lpdelayline + gdly2.idelayoutputxf) * (RVB_XFADE - gdly2.xfade)) / RVB_XFADE;
sampledly = ((sampledly * gdly2.xfade) / RVB_XFADE) + samplexf;
if( ++gdly2.idelayoutputxf >= gdly2.cdelaysamplesmax )
gdly2.idelayoutputxf = 0;
if( --gdly2.xfade == 0 )
gdly2.idelayoutput = gdly2.idelayoutputxf;
}
if( sampledly )
{
// get current sample from delay buffer
val[0] = SOUNDCLIP(left + ((gdly2.delayfeed * sampledly) >> 8));
val[1] = SOUNDCLIP(right + ((gdly2.delayfeed * sampledly) >> 8));
}
else
{
val[0] = left;
val[1] = right;
}
// lowpass
if( gdly2.lp )
{
valt[0] = (gdly2.lp0 + gdly2.lp1 + (val[0]<<1)) >> 2;
valt[1] = (gdly2.lp0 + gdly2.lp1 + (val[1]<<1)) >> 2;
gdly2.lp0 = (val[0] + val[1]) >> 1;
}
else