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
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
|
/**
* Reverb for the OpenAL cross platform audio library
* Copyright (C) 2008-2009 by Christopher Fitzgerald.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alMain.h"
#include "alu.h"
#include "alAuxEffectSlot.h"
#include "alEffect.h"
#include "alFilter.h"
#include "alError.h"
#include "mixer_defs.h"
/* This is the maximum number of samples processed for each inner loop
* iteration. */
#define MAX_UPDATE_SAMPLES 256
static MixerFunc MixSamples = Mix_C;
static alonce_flag mixfunc_inited = AL_ONCE_FLAG_INIT;
static void init_mixfunc(void)
{
MixSamples = SelectMixer();
}
typedef struct DelayLine
{
// The delay lines use sample lengths that are powers of 2 to allow the
// use of bit-masking instead of a modulus for wrapping.
ALuint Mask;
ALfloat *Line;
} DelayLine;
typedef struct ALreverbState {
DERIVE_FROM_TYPE(ALeffectState);
ALboolean IsEax;
// For HRTF and UHJ
ALfloat (*ExtraOut)[BUFFERSIZE];
ALuint ExtraChannels;
// All delay lines are allocated as a single buffer to reduce memory
// fragmentation and management code.
ALfloat *SampleBuffer;
ALuint TotalSamples;
// Master effect filters
ALfilterState LpFilter;
ALfilterState HpFilter; // EAX only
struct {
// Modulator delay line.
DelayLine Delay;
// The vibrato time is tracked with an index over a modulus-wrapped
// range (in samples).
ALuint Index;
ALuint Range;
// The depth of frequency change (also in samples) and its filter.
ALfloat Depth;
ALfloat Coeff;
ALfloat Filter;
} Mod; // EAX only
/* Core delay line (early reflections and late reverb tap from this). */
DelayLine Delay;
/* The tap points for the initial delay. First tap goes to early
* reflections, second to late reverb.
*/
ALuint DelayTap[2];
/* There are actually 4 decorrelator taps, but the first occurs at the late
* reverb tap.
*/
ALuint DecoTap[3];
struct {
// Early reflections are done with 4 delay lines.
ALfloat Coeff[4];
DelayLine Delay[4];
ALuint Offset[4];
// The gain for each output channel based on 3D panning.
ALfloat CurrentGain[4][MAX_OUTPUT_CHANNELS+2];
ALfloat PanGain[4][MAX_OUTPUT_CHANNELS+2];
} Early;
struct {
// Output gain for late reverb.
ALfloat Gain;
// Attenuation to compensate for the modal density and decay rate of
// the late lines.
ALfloat DensityGain;
// The feed-back and feed-forward all-pass coefficient.
ALfloat ApFeedCoeff;
// Mixing matrix coefficient.
ALfloat MixCoeff;
// Late reverb has 4 parallel all-pass filters.
ALfloat ApCoeff[4];
DelayLine ApDelay[4];
ALuint ApOffset[4];
// In addition to 4 cyclical delay lines.
ALfloat Coeff[4];
DelayLine Delay[4];
ALuint Offset[4];
// The cyclical delay lines are 1-pole low-pass filtered.
ALfloat LpCoeff[4];
ALfloat LpSample[4];
// The gain for each output channel based on 3D panning.
ALfloat CurrentGain[4][MAX_OUTPUT_CHANNELS+2];
ALfloat PanGain[4][MAX_OUTPUT_CHANNELS+2];
} Late;
struct {
// Attenuation to compensate for the modal density and decay rate of
// the echo line.
ALfloat DensityGain;
// Echo delay and all-pass lines.
DelayLine Delay;
DelayLine ApDelay;
ALfloat Coeff;
ALfloat ApFeedCoeff;
ALfloat ApCoeff;
ALuint Offset;
ALuint ApOffset;
// The echo line is 1-pole low-pass filtered.
ALfloat LpCoeff;
ALfloat LpSample;
// Echo mixing coefficient.
ALfloat MixCoeff;
} Echo; // EAX only
// The current read offset for all delay lines.
ALuint Offset;
/* Temporary storage used when processing. */
alignas(16) ALfloat ReverbSamples[4][MAX_UPDATE_SAMPLES];
alignas(16) ALfloat EarlySamples[4][MAX_UPDATE_SAMPLES];
} ALreverbState;
static ALvoid ALreverbState_Destruct(ALreverbState *State);
static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device);
static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props);
static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
DECLARE_DEFAULT_ALLOCATORS(ALreverbState)
DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState);
static void ALreverbState_Construct(ALreverbState *state)
{
ALuint index, l;
ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
SET_VTABLE2(ALreverbState, ALeffectState, state);
state->IsEax = AL_FALSE;
state->ExtraChannels = 0;
state->TotalSamples = 0;
state->SampleBuffer = NULL;
ALfilterState_clear(&state->LpFilter);
ALfilterState_clear(&state->HpFilter);
state->Mod.Delay.Mask = 0;
state->Mod.Delay.Line = NULL;
state->Mod.Index = 0;
state->Mod.Range = 1;
state->Mod.Depth = 0.0f;
state->Mod.Coeff = 0.0f;
state->Mod.Filter = 0.0f;
state->Delay.Mask = 0;
state->Delay.Line = NULL;
state->DelayTap[0] = 0;
state->DelayTap[1] = 0;
state->DecoTap[0] = 0;
state->DecoTap[1] = 0;
state->DecoTap[2] = 0;
for(index = 0;index < 4;index++)
{
state->Early.Coeff[index] = 0.0f;
state->Early.Delay[index].Mask = 0;
state->Early.Delay[index].Line = NULL;
state->Early.Offset[index] = 0;
}
state->Late.Gain = 0.0f;
state->Late.DensityGain = 0.0f;
state->Late.ApFeedCoeff = 0.0f;
state->Late.MixCoeff = 0.0f;
for(index = 0;index < 4;index++)
{
state->Late.ApCoeff[index] = 0.0f;
state->Late.ApDelay[index].Mask = 0;
state->Late.ApDelay[index].Line = NULL;
state->Late.ApOffset[index] = 0;
state->Late.Coeff[index] = 0.0f;
state->Late.Delay[index].Mask = 0;
state->Late.Delay[index].Line = NULL;
state->Late.Offset[index] = 0;
state->Late.LpCoeff[index] = 0.0f;
state->Late.LpSample[index] = 0.0f;
}
for(l = 0;l < 4;l++)
{
for(index = 0;index < MAX_OUTPUT_CHANNELS;index++)
{
state->Early.PanGain[l][index] = 0.0f;
state->Late.PanGain[l][index] = 0.0f;
}
}
state->Echo.DensityGain = 0.0f;
state->Echo.Delay.Mask = 0;
state->Echo.Delay.Line = NULL;
state->Echo.ApDelay.Mask = 0;
state->Echo.ApDelay.Line = NULL;
state->Echo.Coeff = 0.0f;
state->Echo.ApFeedCoeff = 0.0f;
state->Echo.ApCoeff = 0.0f;
state->Echo.Offset = 0;
state->Echo.ApOffset = 0;
state->Echo.LpCoeff = 0.0f;
state->Echo.LpSample = 0.0f;
state->Echo.MixCoeff = 0.0f;
state->Offset = 0;
}
static ALvoid ALreverbState_Destruct(ALreverbState *State)
{
al_free(State->SampleBuffer);
State->SampleBuffer = NULL;
ALeffectState_Destruct(STATIC_CAST(ALeffectState,State));
}
/* This is a user config option for modifying the overall output of the reverb
* effect.
*/
ALfloat ReverbBoost = 1.0f;
/* Specifies whether to use a standard reverb effect in place of EAX reverb (no
* high-pass, modulation, or echo).
*/
ALboolean EmulateEAXReverb = AL_FALSE;
/* This coefficient is used to define the maximum frequency range controlled
* by the modulation depth. The current value of 0.1 will allow it to swing
* from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
* sampler to stall on the downswing, and above 1 it will cause it to sample
* backwards.
*/
static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
/* A filter is used to avoid the terrible distortion caused by changing
* modulation time and/or depth. To be consistent across different sample
* rates, the coefficient must be raised to a constant divided by the sample
* rate: coeff^(constant / rate).
*/
static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
// When diffusion is above 0, an all-pass filter is used to take the edge off
// the echo effect. It uses the following line length (in seconds).
static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
// Input into the late reverb is decorrelated between four channels. Their
// timings are dependent on a fraction and multiplier. See the
// UpdateDecorrelator() routine for the calculations involved.
static const ALfloat DECO_FRACTION = 0.15f;
static const ALfloat DECO_MULTIPLIER = 2.0f;
// All delay line lengths are specified in seconds.
// The lengths of the early delay lines.
static const ALfloat EARLY_LINE_LENGTH[4] =
{
0.0015f, 0.0045f, 0.0135f, 0.0405f
};
// The lengths of the late all-pass delay lines.
static const ALfloat ALLPASS_LINE_LENGTH[4] =
{
0.0151f, 0.0167f, 0.0183f, 0.0200f,
};
// The lengths of the late cyclical delay lines.
static const ALfloat LATE_LINE_LENGTH[4] =
{
0.0211f, 0.0311f, 0.0461f, 0.0680f
};
// The late cyclical delay lines have a variable length dependent on the
// effect's density parameter (inverted for some reason) and this multiplier.
static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
#if defined(_WIN32) && !defined (_M_X64) && !defined(_M_ARM)
/* HACK: Workaround for a modff bug in 32-bit Windows, which attempts to write
* a 64-bit double to the 32-bit float parameter.
*/
static inline float hack_modff(float x, float *y)
{
double di;
double df = modf((double)x, &di);
*y = (float)di;
return (float)df;
}
#define modff hack_modff
#endif
/**************************************
* Device Update *
**************************************/
// Given the allocated sample buffer, this function updates each delay line
// offset.
static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLine *Delay)
{
Delay->Line = &sampleBuffer[(ptrdiff_t)Delay->Line];
}
// Calculate the length of a delay line and store its mask and offset.
static ALuint CalcLineLength(ALfloat length, ptrdiff_t offset, ALuint frequency, ALuint extra, DelayLine *Delay)
{
ALuint samples;
// All line lengths are powers of 2, calculated from their lengths, with
// an additional sample in case of rounding errors.
samples = fastf2u(length*frequency) + extra;
samples = NextPowerOf2(samples + 1);
// All lines share a single sample buffer.
Delay->Mask = samples - 1;
Delay->Line = (ALfloat*)offset;
// Return the sample count for accumulation.
return samples;
}
/* Calculates the delay line metrics and allocates the shared sample buffer
* for all lines given the sample rate (frequency). If an allocation failure
* occurs, it returns AL_FALSE.
*/
static ALboolean AllocLines(ALuint frequency, ALreverbState *State)
{
ALuint totalSamples, index;
ALfloat length;
// All delay line lengths are calculated to accomodate the full range of
// lengths given their respective paramters.
totalSamples = 0;
/* The modulator's line length is calculated from the maximum modulation
* time and depth coefficient, and halfed for the low-to-high frequency
* swing. An additional sample is added to keep it stable when there is no
* modulation.
*/
length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f);
totalSamples += CalcLineLength(length, totalSamples, frequency, 1,
&State->Mod.Delay);
/* The initial delay is the sum of the reflections and late reverb delays.
* The decorrelator length is calculated from the lowest reverb density (a
* parameter value of 1). This must include space for storing a loop
* update.
*/
length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
length += (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
totalSamples += CalcLineLength(length, totalSamples, frequency,
MAX_UPDATE_SAMPLES, &State->Delay);
// The early reflection lines.
for(index = 0;index < 4;index++)
totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
frequency, 0, &State->Early.Delay[index]);
// The late all-pass lines.
for(index = 0;index < 4;index++)
totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
frequency, 0, &State->Late.ApDelay[index]);
// The late delay lines are calculated from the lowest reverb density.
for(index = 0;index < 4;index++)
{
length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
&State->Late.Delay[index]);
}
// The echo all-pass and delay lines.
totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
frequency, 0, &State->Echo.ApDelay);
totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
frequency, 0, &State->Echo.Delay);
if(totalSamples != State->TotalSamples)
{
ALfloat *newBuffer;
TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
newBuffer = al_calloc(16, sizeof(ALfloat) * totalSamples);
if(!newBuffer) return AL_FALSE;
al_free(State->SampleBuffer);
State->SampleBuffer = newBuffer;
State->TotalSamples = totalSamples;
}
// Update all delays to reflect the new sample buffer.
RealizeLineOffset(State->SampleBuffer, &State->Delay);
for(index = 0;index < 4;index++)
{
RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
}
RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
// Clear the sample buffer.
for(index = 0;index < State->TotalSamples;index++)
State->SampleBuffer[index] = 0.0f;
return AL_TRUE;
}
static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device)
{
ALuint frequency = Device->Frequency, index;
// Allocate the delay lines.
if(!AllocLines(frequency, State))
return AL_FALSE;
/* HRTF and UHJ will mix to the real output for ambient output. */
if(Device->Hrtf.Handle || Device->Uhj_Encoder)
{
State->ExtraOut = Device->RealOut.Buffer;
State->ExtraChannels = Device->RealOut.NumChannels;
}
else
{
State->ExtraOut = NULL;
State->ExtraChannels = 0;
}
// Calculate the modulation filter coefficient. Notice that the exponent
// is calculated given the current sample rate. This ensures that the
// resulting filter response over time is consistent across all sample
// rates.
State->Mod.Coeff = powf(MODULATION_FILTER_COEFF,
MODULATION_FILTER_CONST / frequency);
// The early reflection and late all-pass filter line lengths are static,
// so their offsets only need to be calculated once.
for(index = 0;index < 4;index++)
{
State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] * frequency);
State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] * frequency);
}
// The echo all-pass filter line length is static, so its offset only
// needs to be calculated once.
State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
return AL_TRUE;
}
/**************************************
* Effect Update *
**************************************/
// Calculate a decay coefficient given the length of each cycle and the time
// until the decay reaches -60 dB.
static inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
{
return powf(0.001f/*-60 dB*/, length/decayTime);
}
// Calculate a decay length from a coefficient and the time until the decay
// reaches -60 dB.
static inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
{
return log10f(coeff) * decayTime / log10f(0.001f)/*-60 dB*/;
}
// Calculate an attenuation to be applied to the input of any echo models to
// compensate for modal density and decay time.
static inline ALfloat CalcDensityGain(ALfloat a)
{
/* The energy of a signal can be obtained by finding the area under the
* squared signal. This takes the form of Sum(x_n^2), where x is the
* amplitude for the sample n.
*
* Decaying feedback matches exponential decay of the form Sum(a^n),
* where a is the attenuation coefficient, and n is the sample. The area
* under this decay curve can be calculated as: 1 / (1 - a).
*
* Modifying the above equation to find the squared area under the curve
* (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
* calculated by inverting the square root of this approximation,
* yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
*/
return sqrtf(1.0f - (a * a));
}
// Calculate the mixing matrix coefficients given a diffusion factor.
static inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
{
ALfloat n, t;
// The matrix is of order 4, so n is sqrt (4 - 1).
n = sqrtf(3.0f);
t = diffusion * atanf(n);
// Calculate the first mixing matrix coefficient.
*x = cosf(t);
// Calculate the second mixing matrix coefficient.
*y = sinf(t) / n;
}
// Calculate the limited HF ratio for use with the late reverb low-pass
// filters.
static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
{
ALfloat limitRatio;
/* Find the attenuation due to air absorption in dB (converting delay
* time to meters using the speed of sound). Then reversing the decay
* equation, solve for HF ratio. The delay length is cancelled out of
* the equation, so it can be calculated once for all lines.
*/
limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
SPEEDOFSOUNDMETRESPERSEC);
/* Using the limit calculated above, apply the upper bound to the HF
* ratio. Also need to limit the result to a minimum of 0.1, just like the
* HF ratio parameter. */
return clampf(limitRatio, 0.1f, hfRatio);
}
// Calculate the coefficient for a HF (and eventually LF) decay damping
// filter.
static inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
{
ALfloat coeff, g;
// Eventually this should boost the high frequencies when the ratio
// exceeds 1.
coeff = 0.0f;
if (hfRatio < 1.0f)
{
// Calculate the low-pass coefficient by dividing the HF decay
// coefficient by the full decay coefficient.
g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
// Damping is done with a 1-pole filter, so g needs to be squared.
g *= g;
if(g < 0.9999f) /* 1-epsilon */
{
/* Be careful with gains < 0.001, as that causes the coefficient
* head towards 1, which will flatten the signal. */
g = maxf(g, 0.001f);
coeff = (1 - g*cw - sqrtf(2*g*(1-cw) - g*g*(1 - cw*cw))) /
(1 - g);
}
// Very low decay times will produce minimal output, so apply an
// upper bound to the coefficient.
coeff = minf(coeff, 0.98f);
}
return coeff;
}
// Update the EAX modulation index, range, and depth. Keep in mind that this
// kind of vibrato is additive and not multiplicative as one may expect. The
// downswing will sound stronger than the upswing.
static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALreverbState *State)
{
ALuint range;
/* Modulation is calculated in two parts.
*
* The modulation time effects the sinus applied to the change in
* frequency. An index out of the current time range (both in samples)
* is incremented each sample. The range is bound to a reasonable
* minimum (1 sample) and when the timing changes, the index is rescaled
* to the new range (to keep the sinus consistent).
*/
range = maxu(fastf2u(modTime*frequency), 1);
State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
State->Mod.Range);
State->Mod.Range = range;
/* The modulation depth effects the amount of frequency change over the
* range of the sinus. It needs to be scaled by the modulation time so
* that a given depth produces a consistent change in frequency over all
* ranges of time. Since the depth is applied to a sinus value, it needs
* to be halfed once for the sinus range and again for the sinus swing
* in time (half of it is spent decreasing the frequency, half is spent
* increasing it).
*/
State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
2.0f * frequency;
}
// Update the offsets for the initial effect delay line.
static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALreverbState *State)
{
// Calculate the initial delay taps.
State->DelayTap[0] = fastf2u(earlyDelay * frequency);
State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
}
// Update the early reflections mix and line coefficients.
static ALvoid UpdateEarlyLines(ALfloat lateDelay, ALreverbState *State)
{
ALuint index;
// Calculate the gain (coefficient) for each early delay line using the
// late delay time. This expands the early reflections to the start of
// the late reverb.
for(index = 0;index < 4;index++)
State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
lateDelay);
}
// Update the offsets for the decorrelator line.
static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALreverbState *State)
{
ALuint index;
ALfloat length;
/* The late reverb inputs are decorrelated to smooth the reverb tail and
* reduce harsh echos. The first tap occurs immediately, while the
* remaining taps are delayed by multiples of a fraction of the smallest
* cyclical delay time.
*
* offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
*/
for(index = 0;index < 3;index++)
{
length = (DECO_FRACTION * powf(DECO_MULTIPLIER, (ALfloat)index)) *
LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
State->DecoTap[index] = fastf2u(length * frequency) + State->DelayTap[1];
}
}
// Update the late reverb mix, line lengths, and line coefficients.
static ALvoid UpdateLateLines(ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
{
ALfloat length;
ALuint index;
/* Calculate the late reverb gain. Since the output is tapped prior to the
* application of the next delay line coefficients, this gain needs to be
* attenuated by the 'x' mixing matrix coefficient as well. Also attenuate
* the late reverb when echo depth is high and diffusion is low, so the
* echo is slightly stronger than the decorrelated echos in the reverb
* tail.
*/
State->Late.Gain = xMix * (1.0f - (echoDepth*0.5f*(1.0f - diffusion)));
/* To compensate for changes in modal density and decay time of the late
* reverb signal, the input is attenuated based on the maximal energy of
* the outgoing signal. This approximation is used to keep the apparent
* energy of the signal equal for all ranges of density and decay time.
*
* The average length of the cyclcical delay lines is used to calculate
* the attenuation coefficient.
*/
length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
State->Late.DensityGain = CalcDensityGain(
CalcDecayCoeff(length, decayTime)
);
// Calculate the all-pass feed-back and feed-forward coefficient.
State->Late.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
for(index = 0;index < 4;index++)
{
// Calculate the gain (coefficient) for each all-pass line.
State->Late.ApCoeff[index] = CalcDecayCoeff(
ALLPASS_LINE_LENGTH[index], decayTime
);
// Calculate the length (in seconds) of each cyclical delay line.
length = LATE_LINE_LENGTH[index] *
(1.0f + (density * LATE_LINE_MULTIPLIER));
// Calculate the delay offset for each cyclical delay line.
State->Late.Offset[index] = fastf2u(length * frequency);
// Calculate the gain (coefficient) for each cyclical line.
State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
// Calculate the damping coefficient for each low-pass filter.
State->Late.LpCoeff[index] = CalcDampingCoeff(
hfRatio, length, decayTime, State->Late.Coeff[index], cw
);
// Attenuate the cyclical line coefficients by the mixing coefficient
// (x).
State->Late.Coeff[index] *= xMix;
}
}
// Update the echo gain, line offset, line coefficients, and mixing
// coefficients.
static ALvoid UpdateEchoLine(ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
{
// Update the offset and coefficient for the echo delay line.
State->Echo.Offset = fastf2u(echoTime * frequency);
// Calculate the decay coefficient for the echo line.
State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
// Calculate the energy-based attenuation coefficient for the echo delay
// line.
State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
// Calculate the echo all-pass feed coefficient.
State->Echo.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
// Calculate the echo all-pass attenuation coefficient.
State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
// Calculate the damping coefficient for each low-pass filter.
State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
State->Echo.Coeff, cw);
/* Calculate the echo mixing coefficient. This is applied to the output mix
* only, not the feedback.
*/
State->Echo.MixCoeff = echoDepth;
}
// Update the early and late 3D panning gains.
static ALvoid UpdateMixedPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
{
ALfloat DirGains[MAX_OUTPUT_CHANNELS];
ALfloat coeffs[MAX_AMBI_COEFFS];
ALfloat length;
ALuint i;
/* With HRTF or UHJ, the normal output provides a panned reverb channel
* when a non-0-length vector is specified, while the real stereo output
* provides two other "direct" non-panned reverb channels.
*/
memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
if(!(length > FLT_EPSILON))
{
for(i = 0;i < Device->RealOut.NumChannels;i++)
State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain;
}
else
{
/* Note that EAX Reverb's panning vectors are using right-handed
* coordinates, rather than OpenAL's left-handed coordinates. Negate Z
* to fix this.
*/
ALfloat pan[3] = {
ReflectionsPan[0] / length,
ReflectionsPan[1] / length,
-ReflectionsPan[2] / length,
};
length = minf(length, 1.0f);
CalcDirectionCoeffs(pan, 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Early.PanGain[3][i] = DirGains[i] * EarlyGain * length;
for(i = 0;i < Device->RealOut.NumChannels;i++)
State->Early.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * EarlyGain * (1.0f-length);
}
memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
if(!(length > FLT_EPSILON))
{
for(i = 0;i < Device->RealOut.NumChannels;i++)
State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain;
}
else
{
ALfloat pan[3] = {
LateReverbPan[0] / length,
LateReverbPan[1] / length,
-LateReverbPan[2] / length,
};
length = minf(length, 1.0f);
CalcDirectionCoeffs(pan, 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Late.PanGain[3][i] = DirGains[i] * LateGain * length;
for(i = 0;i < Device->RealOut.NumChannels;i++)
State->Late.PanGain[i&3][Device->Dry.NumChannels+i] = Gain * LateGain * (1.0f-length);
}
}
static ALvoid UpdateDirectPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
{
ALfloat AmbientGains[MAX_OUTPUT_CHANNELS];
ALfloat DirGains[MAX_OUTPUT_CHANNELS];
ALfloat coeffs[MAX_AMBI_COEFFS];
ALfloat length;
ALuint i;
/* Apply a boost of about 3dB to better match the expected stereo output volume. */
ComputeAmbientGains(Device->Dry, Gain*1.414213562f, AmbientGains);
memset(State->Early.PanGain, 0, sizeof(State->Early.PanGain));
length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
if(!(length > FLT_EPSILON))
{
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Early.PanGain[i&3][i] = AmbientGains[i] * EarlyGain;
}
else
{
ALfloat pan[3] = {
ReflectionsPan[0] / length,
ReflectionsPan[1] / length,
-ReflectionsPan[2] / length,
};
length = minf(length, 1.0f);
CalcDirectionCoeffs(pan, 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Early.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * EarlyGain;
}
memset(State->Late.PanGain, 0, sizeof(State->Late.PanGain));
length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
if(!(length > FLT_EPSILON))
{
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Late.PanGain[i&3][i] = AmbientGains[i] * LateGain;
}
else
{
ALfloat pan[3] = {
LateReverbPan[0] / length,
LateReverbPan[1] / length,
-LateReverbPan[2] / length,
};
length = minf(length, 1.0f);
CalcDirectionCoeffs(pan, 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain, DirGains);
for(i = 0;i < Device->Dry.NumChannels;i++)
State->Late.PanGain[i&3][i] = lerp(AmbientGains[i], DirGains[i], length) * LateGain;
}
}
static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALfloat EarlyGain, ALfloat LateGain, ALreverbState *State)
{
static const ALfloat PanDirs[4][3] = {
{ -0.707106781f, 0.0f, -0.707106781f }, /* Front left */
{ 0.707106781f, 0.0f, -0.707106781f }, /* Front right */
{ 0.707106781f, 0.0f, 0.707106781f }, /* Back right */
{ -0.707106781f, 0.0f, 0.707106781f } /* Back left */
};
ALfloat coeffs[MAX_AMBI_COEFFS];
ALfloat gain[4];
ALfloat length;
ALuint i;
/* sqrt(0.5) would be the gain scaling when the panning vector is 0. This
* also equals sqrt(2/4), a nice gain scaling for the four virtual points
* producing an "ambient" response.
*/
gain[0] = gain[1] = gain[2] = gain[3] = 0.707106781f;
length = sqrtf(ReflectionsPan[0]*ReflectionsPan[0] + ReflectionsPan[1]*ReflectionsPan[1] + ReflectionsPan[2]*ReflectionsPan[2]);
if(length > 1.0f)
{
ALfloat pan[3] = {
ReflectionsPan[0] / length,
ReflectionsPan[1] / length,
-ReflectionsPan[2] / length,
};
for(i = 0;i < 4;i++)
{
ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
}
}
else if(length > FLT_EPSILON)
{
for(i = 0;i < 4;i++)
{
ALfloat dotp = ReflectionsPan[0]*PanDirs[i][0] + ReflectionsPan[1]*PanDirs[i][1] +
-ReflectionsPan[2]*PanDirs[i][2];
gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
}
}
for(i = 0;i < 4;i++)
{
CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain*EarlyGain*gain[i],
State->Early.PanGain[i]);
}
gain[0] = gain[1] = gain[2] = gain[3] = 0.707106781f;
length = sqrtf(LateReverbPan[0]*LateReverbPan[0] + LateReverbPan[1]*LateReverbPan[1] + LateReverbPan[2]*LateReverbPan[2]);
if(length > 1.0f)
{
ALfloat pan[3] = {
LateReverbPan[0] / length,
LateReverbPan[1] / length,
-LateReverbPan[2] / length,
};
for(i = 0;i < 4;i++)
{
ALfloat dotp = pan[0]*PanDirs[i][0] + pan[1]*PanDirs[i][1] + pan[2]*PanDirs[i][2];
gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
}
}
else if(length > FLT_EPSILON)
{
for(i = 0;i < 4;i++)
{
ALfloat dotp = LateReverbPan[0]*PanDirs[i][0] + LateReverbPan[1]*PanDirs[i][1] +
-LateReverbPan[2]*PanDirs[i][2];
gain[i] = sqrtf(clampf(dotp*0.5f + 0.5f, 0.0f, 1.0f));
}
}
for(i = 0;i < 4;i++)
{
CalcDirectionCoeffs(PanDirs[i], 0.0f, coeffs);
ComputePanningGains(Device->Dry, coeffs, Gain*LateGain*gain[i],
State->Late.PanGain[i]);
}
}
static ALvoid ALreverbState_update(ALreverbState *State, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props)
{
ALuint frequency = Device->Frequency;
ALfloat lfscale, hfscale, hfRatio;
ALfloat gain, gainlf, gainhf;
ALfloat cw, x, y;
if(Slot->Params.EffectType == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
State->IsEax = AL_TRUE;
else if(Slot->Params.EffectType == AL_EFFECT_REVERB || EmulateEAXReverb)
State->IsEax = AL_FALSE;
// Calculate the master filters
hfscale = props->Reverb.HFReference / frequency;
gainhf = maxf(props->Reverb.GainHF, 0.0001f);
ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
gainhf, hfscale, calc_rcpQ_from_slope(gainhf, 0.75f));
lfscale = props->Reverb.LFReference / frequency;
gainlf = maxf(props->Reverb.GainLF, 0.0001f);
ALfilterState_setParams(&State->HpFilter, ALfilterType_LowShelf,
gainlf, lfscale, calc_rcpQ_from_slope(gainlf, 0.75f));
// Update the modulator line.
UpdateModulator(props->Reverb.ModulationTime, props->Reverb.ModulationDepth,
frequency, State);
// Update the initial effect delay.
UpdateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
frequency, State);
// Update the decorrelator.
UpdateDecorrelator(props->Reverb.Density, frequency, State);
// Update the early lines.
UpdateEarlyLines(props->Reverb.LateReverbDelay, State);
// Get the mixing matrix coefficients (x and y).
CalcMatrixCoeffs(props->Reverb.Diffusion, &x, &y);
// Then divide x into y to simplify the matrix calculation.
State->Late.MixCoeff = y / x;
// If the HF limit parameter is flagged, calculate an appropriate limit
// based on the air absorption parameter.
hfRatio = props->Reverb.DecayHFRatio;
if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
props->Reverb.DecayTime);
cw = cosf(F_TAU * hfscale);
// Update the late lines.
UpdateLateLines(x, props->Reverb.Density, props->Reverb.DecayTime,
props->Reverb.Diffusion, props->Reverb.EchoDepth,
hfRatio, cw, frequency, State);
// Update the echo line.
UpdateEchoLine(props->Reverb.EchoTime, props->Reverb.DecayTime,
props->Reverb.Diffusion, props->Reverb.EchoDepth,
hfRatio, cw, frequency, State);
gain = props->Reverb.Gain * Slot->Params.Gain * ReverbBoost;
// Update early and late 3D panning.
if(Device->Hrtf.Handle || Device->Uhj_Encoder)
UpdateMixedPanning(Device, props->Reverb.ReflectionsPan,
props->Reverb.LateReverbPan, gain,
props->Reverb.ReflectionsGain,
props->Reverb.LateReverbGain, State);
else if(Device->AmbiDecoder || (Device->FmtChans >= DevFmtAmbi1 &&
Device->FmtChans <= DevFmtAmbi3))
Update3DPanning(Device, props->Reverb.ReflectionsPan,
props->Reverb.LateReverbPan, gain,
props->Reverb.ReflectionsGain,
props->Reverb.LateReverbGain, State);
else
UpdateDirectPanning(Device, props->Reverb.ReflectionsPan,
props->Reverb.LateReverbPan, gain,
props->Reverb.ReflectionsGain,
props->Reverb.LateReverbGain, State);
}
/**************************************
* Effect Processing *
**************************************/
// Basic delay line input/output routines.
static inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
{
return Delay->Line[offset&Delay->Mask];
}
static inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
{
Delay->Line[offset&Delay->Mask] = in;
}
// Given some input samples, this function produces modulation for the late
// reverb.
static void EAXModulation(ALreverbState *State, ALuint offset, ALfloat*restrict dst, const ALfloat*restrict src, ALuint todo)
{
ALfloat sinus, frac, fdelay;
ALfloat out0, out1;
ALuint delay, i;
for(i = 0;i < todo;i++)
{
/* Calculate the sinus rythm (dependent on modulation time and the
* sampling rate). The center of the sinus is moved to reduce the
* delay of the effect when the time or depth are low.
*/
sinus = 1.0f - cosf(F_TAU * State->Mod.Index / State->Mod.Range);
/* Step the modulation index forward, keeping it bound to its range. */
State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
/* The depth determines the range over which to read the input samples
* from, so it must be filtered to reduce the distortion caused by even
* small parameter changes.
*/
State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
State->Mod.Coeff);
/* Calculate the read offset and fraction between it and the next
* sample.
*/
frac = modff(State->Mod.Filter*sinus, &fdelay);
delay = fastf2u(fdelay);
/* Add the incoming sample to the delay line first, so a 0 delay gets
* the incoming sample.
*/
DelayLineIn(&State->Mod.Delay, offset, src[i]);
/* Get the two samples crossed by the offset delay */
out0 = DelayLineOut(&State->Mod.Delay, offset - delay);
out1 = DelayLineOut(&State->Mod.Delay, offset - delay - 1);
offset++;
/* The output is obtained by linearly interpolating the two samples
* that were acquired above.
*/
dst[i] = lerp(out0, out1, frac);
}
}
// Given some input sample, this function produces four-channel outputs for the
// early reflections.
static inline ALvoid EarlyReflection(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
{
ALfloat d[4], v, f[4];
ALuint i;
for(i = 0;i < todo;i++)
{
ALuint offset = State->Offset+i;
// Obtain the decayed results of each early delay line.
d[0] = DelayLineOut(&State->Early.Delay[0], offset-State->Early.Offset[0]) * State->Early.Coeff[0];
d[1] = DelayLineOut(&State->Early.Delay[1], offset-State->Early.Offset[1]) * State->Early.Coeff[1];
d[2] = DelayLineOut(&State->Early.Delay[2], offset-State->Early.Offset[2]) * State->Early.Coeff[2];
d[3] = DelayLineOut(&State->Early.Delay[3], offset-State->Early.Offset[3]) * State->Early.Coeff[3];
/* The following uses a lossless scattering junction from waveguide
* theory. It actually amounts to a householder mixing matrix, which
* will produce a maximally diffuse response, and means this can
* probably be considered a simple feed-back delay network (FDN).
* N
* ---
* \
* v = 2/N / d_i
* ---
* i=1
*/
v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
// The junction is loaded with the input here.
v += DelayLineOut(&State->Delay, offset-State->DelayTap[0]);
// Calculate the feed values for the delay lines.
f[0] = v - d[0];
f[1] = v - d[1];
f[2] = v - d[2];
f[3] = v - d[3];
// Re-feed the delay lines.
DelayLineIn(&State->Early.Delay[0], offset, f[0]);
DelayLineIn(&State->Early.Delay[1], offset, f[1]);
DelayLineIn(&State->Early.Delay[2], offset, f[2]);
DelayLineIn(&State->Early.Delay[3], offset, f[3]);
/* Output the results of the junction for all four channels with a
* constant attenuation of 0.5.
*/
out[0][i] = f[0] * 0.5f;
out[1][i] = f[1] * 0.5f;
out[2][i] = f[2] * 0.5f;
out[3][i] = f[3] * 0.5f;
}
}
// Basic attenuated all-pass input/output routine.
static inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
{
ALfloat out, feed;
out = DelayLineOut(Delay, outOffset);
feed = feedCoeff * in;
DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
// The time-based attenuation is only applied to the delay output to
// keep it from affecting the feed-back path (which is already controlled
// by the all-pass feed coefficient).
return (coeff * out) - feed;
}
// All-pass input/output routine for late reverb.
static inline ALfloat LateAllPassInOut(ALreverbState *State, ALuint offset, ALuint index, ALfloat in)
{
return AllpassInOut(&State->Late.ApDelay[index],
offset - State->Late.ApOffset[index],
offset, in, State->Late.ApFeedCoeff,
State->Late.ApCoeff[index]);
}
// Low-pass filter input/output routine for late reverb.
static inline ALfloat LateLowPassInOut(ALreverbState *State, ALuint index, ALfloat in)
{
in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
State->Late.LpSample[index] = in;
return in;
}
// Given four decorrelated input samples, this function produces four-channel
// output for the late reverb.
static inline ALvoid LateReverb(ALreverbState *State, ALuint todo, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
{
ALfloat d[4], f[4];
ALuint offset;
ALuint base, i;
offset = State->Offset;
for(base = 0;base < todo;)
{
ALfloat tmp[MAX_UPDATE_SAMPLES/4][4];
ALuint tmp_todo = minu(todo, MAX_UPDATE_SAMPLES/4);
for(i = 0;i < tmp_todo;i++)
{
/* Obtain four decorrelated input samples. */
f[0] = DelayLineOut(&State->Delay, offset-State->DelayTap[1]) * State->Late.DensityGain;
f[1] = DelayLineOut(&State->Delay, offset-State->DecoTap[0]) * State->Late.DensityGain;
f[2] = DelayLineOut(&State->Delay, offset-State->DecoTap[1]) * State->Late.DensityGain;
f[3] = DelayLineOut(&State->Delay, offset-State->DecoTap[2]) * State->Late.DensityGain;
/* Add the decayed results of the cyclical delay lines, then pass
* the results through the low-pass filters.
*/
f[0] += DelayLineOut(&State->Late.Delay[0], offset-State->Late.Offset[0]) * State->Late.Coeff[0];
f[1] += DelayLineOut(&State->Late.Delay[1], offset-State->Late.Offset[1]) * State->Late.Coeff[1];
f[2] += DelayLineOut(&State->Late.Delay[2], offset-State->Late.Offset[2]) * State->Late.Coeff[2];
f[3] += DelayLineOut(&State->Late.Delay[3], offset-State->Late.Offset[3]) * State->Late.Coeff[3];
/* This is where the feed-back cycles from line 0 to 1 to 3 to 2
* and back to 0.
*/
d[0] = LateLowPassInOut(State, 2, f[2]);
d[1] = LateLowPassInOut(State, 0, f[0]);
d[2] = LateLowPassInOut(State, 3, f[3]);
d[3] = LateLowPassInOut(State, 1, f[1]);
/* To help increase diffusion, run each line through an all-pass
* filter. When there is no diffusion, the shortest all-pass filter
* will feed the shortest delay line.
*/
d[0] = LateAllPassInOut(State, offset, 0, d[0]);
d[1] = LateAllPassInOut(State, offset, 1, d[1]);
d[2] = LateAllPassInOut(State, offset, 2, d[2]);
d[3] = LateAllPassInOut(State, offset, 3, d[3]);
/* Late reverb is done with a modified feed-back delay network (FDN)
* topology. Four input lines are each fed through their own all-pass
* filter and then into the mixing matrix. The four outputs of the
* mixing matrix are then cycled back to the inputs. Each output feeds
* a different input to form a circlular feed cycle.
*
* The mixing matrix used is a 4D skew-symmetric rotation matrix
* derived using a single unitary rotational parameter:
*
* [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
* [ -a, d, c, -b ]
* [ -b, -c, d, a ]
* [ -c, b, -a, d ]
*
* The rotation is constructed from the effect's diffusion parameter,
* yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
* with differing signs, and d is the coefficient x. The matrix is
* thus:
*
* [ x, y, -y, y ] n = sqrt(matrix_order - 1)
* [ -y, x, y, y ] t = diffusion_parameter * atan(n)
* [ y, -y, x, y ] x = cos(t)
* [ -y, -y, -y, x ] y = sin(t) / n
*
* To reduce the number of multiplies, the x coefficient is applied
* with the cyclical delay line coefficients. Thus only the y
* coefficient is applied when mixing, and is modified to be: y / x.
*/
f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
/* Re-feed the cyclical delay lines. */
DelayLineIn(&State->Late.Delay[0], offset, f[0]);
DelayLineIn(&State->Late.Delay[1], offset, f[1]);
DelayLineIn(&State->Late.Delay[2], offset, f[2]);
DelayLineIn(&State->Late.Delay[3], offset, f[3]);
offset++;
/* Output the results of the matrix for all four channels,
* attenuated by the late reverb gain (which is attenuated by the
* 'x' mix coefficient).
*/
tmp[i][0] = State->Late.Gain * f[0];
tmp[i][1] = State->Late.Gain * f[1];
tmp[i][2] = State->Late.Gain * f[2];
tmp[i][3] = State->Late.Gain * f[3];
}
/* Deinterlace to output */
for(i = 0;i < tmp_todo;i++) out[0][base+i] = tmp[i][0];
for(i = 0;i < tmp_todo;i++) out[1][base+i] = tmp[i][1];
for(i = 0;i < tmp_todo;i++) out[2][base+i] = tmp[i][2];
for(i = 0;i < tmp_todo;i++) out[3][base+i] = tmp[i][3];
base += tmp_todo;
}
}
// Given an input sample, this function mixes echo into the four-channel late
// reverb.
static inline ALvoid EAXEcho(ALreverbState *State, ALuint todo, ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
{
ALfloat out[MAX_UPDATE_SAMPLES];
ALfloat feed;
ALuint offset;
ALuint i;
offset = State->Offset;
for(i = 0;i < todo;i++)
{
// Get the latest attenuated echo sample for output.
feed = DelayLineOut(&State->Echo.Delay, offset-State->Echo.Offset) *
State->Echo.Coeff;
// Write the output.
out[i] = State->Echo.MixCoeff * feed;
// Mix the energy-attenuated input with the output and pass it through
// the echo low-pass filter.
feed += DelayLineOut(&State->Delay, offset-State->DelayTap[1]) *
State->Echo.DensityGain;
feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
State->Echo.LpSample = feed;
// Then the echo all-pass filter.
feed = AllpassInOut(&State->Echo.ApDelay, offset-State->Echo.ApOffset,
offset, feed, State->Echo.ApFeedCoeff,
State->Echo.ApCoeff);
// Feed the delay with the mixed and filtered sample.
DelayLineIn(&State->Echo.Delay, offset, feed);
offset++;
}
// Mix the output into the late reverb channels.
for(i = 0;i < todo;i++) late[0][i] += out[i];
for(i = 0;i < todo;i++) late[1][i] += out[i];
for(i = 0;i < todo;i++) late[2][i] += out[i];
for(i = 0;i < todo;i++) late[3][i] += out[i];
}
// Perform the non-EAX reverb pass on a given input sample, resulting in
// four-channel output.
static inline ALvoid VerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[MAX_UPDATE_SAMPLES], ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
{
ALuint i;
// Low-pass filter the incoming samples (use the early buffer as temp storage).
ALfilterState_process(&State->LpFilter, &early[0][0], input, todo);
for(i = 0;i < todo;i++)
DelayLineIn(&State->Delay, State->Offset+i, early[0][i]);
// Calculate the early reflection from the first delay tap.
EarlyReflection(State, todo, early);
// Calculate the late reverb from the decorrelator taps.
LateReverb(State, todo, late);
// Step all delays forward one sample.
State->Offset += todo;
}
// Perform the EAX reverb pass on a given input sample, resulting in four-
// channel output.
static inline ALvoid EAXVerbPass(ALreverbState *State, ALuint todo, const ALfloat *input, ALfloat (*restrict early)[MAX_UPDATE_SAMPLES], ALfloat (*restrict late)[MAX_UPDATE_SAMPLES])
{
ALuint i;
/* Perform any modulation on the input (use the early buffer as temp storage). */
EAXModulation(State, State->Offset, &early[0][0], input, todo);
/* Band-pass the incoming samples */
ALfilterState_process(&State->LpFilter, &early[1][0], &early[0][0], todo);
ALfilterState_process(&State->HpFilter, &early[2][0], &early[1][0], todo);
// Feed the initial delay line.
for(i = 0;i < todo;i++)
DelayLineIn(&State->Delay, State->Offset+i, early[2][i]);
// Calculate the early reflection from the first delay tap.
EarlyReflection(State, todo, early);
// Calculate the late reverb from the decorrelator taps.
LateReverb(State, todo, late);
// Calculate and mix in any echo.
EAXEcho(State, todo, late);
// Step all delays forward.
State->Offset += todo;
}
static void DoMix(const ALfloat *restrict src, ALfloat (*dst)[BUFFERSIZE], ALuint num_chans,
const ALfloat *restrict target_gains, ALfloat *restrict current_gains,
ALfloat delta, ALuint offset, ALuint total_rem, ALuint todo)
{
MixGains gains[MAX_OUTPUT_CHANNELS];
ALuint c;
for(c = 0;c < num_chans;c++)
{
ALfloat diff;
gains[c].Target = target_gains[c];
gains[c].Current = current_gains[c];
diff = gains[c].Target - gains[c].Current;
if(fabsf(diff) >= GAIN_SILENCE_THRESHOLD)
gains[c].Step = diff * delta;
else
{
gains[c].Current = gains[c].Target;
gains[c].Step = 0.0f;
}
}
MixSamples(src, num_chans, dst, gains, total_rem, offset, todo);
for(c = 0;c < num_chans;c++)
current_gains[c] = gains[c].Current;
}
static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
{
ALfloat (*restrict early)[MAX_UPDATE_SAMPLES] = State->EarlySamples;
ALfloat (*restrict late)[MAX_UPDATE_SAMPLES] = State->ReverbSamples;
ALuint base, c;
/* Process reverb for these samples. */
for(base = 0;base < SamplesToDo;)
{
const ALfloat delta = 1.0f / (ALfloat)(SamplesToDo-base);
ALuint todo = minu(SamplesToDo-base, MAX_UPDATE_SAMPLES);
VerbPass(State, todo, &SamplesIn[base], early, late);
for(c = 0;c < 4;c++)
{
DoMix(early[c], SamplesOut, NumChannels, State->Early.PanGain[c],
State->Early.CurrentGain[c], delta, base, SamplesToDo-base, todo
);
if(State->ExtraChannels > 0)
DoMix(early[c], State->ExtraOut, State->ExtraChannels,
State->Early.PanGain[c]+NumChannels,
State->Early.CurrentGain[c]+NumChannels, delta, base,
SamplesToDo-base, todo
);
}
for(c = 0;c < 4;c++)
{
DoMix(late[c], SamplesOut, NumChannels, State->Late.PanGain[c],
State->Late.CurrentGain[c], delta, base, SamplesToDo, todo
);
if(State->ExtraChannels > 0)
DoMix(late[c], State->ExtraOut, State->ExtraChannels,
State->Late.PanGain[c]+NumChannels,
State->Late.CurrentGain[c]+NumChannels, delta, base,
SamplesToDo-base, todo
);
}
base += todo;
}
}
static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
{
ALfloat (*restrict early)[MAX_UPDATE_SAMPLES] = State->EarlySamples;
ALfloat (*restrict late)[MAX_UPDATE_SAMPLES] = State->ReverbSamples;
ALuint base, c;
/* Process reverb for these samples. */
for(base = 0;base < SamplesToDo;)
{
const ALfloat delta = 1.0f / (ALfloat)(SamplesToDo-base);
ALuint todo = minu(SamplesToDo-base, MAX_UPDATE_SAMPLES);
EAXVerbPass(State, todo, &SamplesIn[base], early, late);
for(c = 0;c < 4;c++)
{
DoMix(early[c], SamplesOut, NumChannels, State->Early.PanGain[c],
State->Early.CurrentGain[c], delta, base, SamplesToDo-base, todo
);
if(State->ExtraChannels > 0)
DoMix(early[c], SamplesOut, State->ExtraChannels,
State->Early.PanGain[c]+NumChannels,
State->Early.CurrentGain[c]+NumChannels, delta, base,
SamplesToDo-base, todo
);
}
for(c = 0;c < 4;c++)
{
DoMix(late[c], SamplesOut, NumChannels, State->Late.PanGain[c],
State->Late.CurrentGain[c], delta, base, SamplesToDo, todo
);
if(State->ExtraChannels > 0)
DoMix(late[c], SamplesOut, State->ExtraChannels,
State->Late.PanGain[c]+NumChannels,
State->Late.CurrentGain[c]+NumChannels, delta, base,
SamplesToDo-base, todo
);
}
base += todo;
}
}
static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
{
if(State->IsEax)
ALreverbState_processEax(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
else
ALreverbState_processStandard(State, SamplesToDo, SamplesIn[0], SamplesOut, NumChannels);
}
typedef struct ALreverbStateFactory {
DERIVE_FROM_TYPE(ALeffectStateFactory);
} ALreverbStateFactory;
static ALeffectState *ALreverbStateFactory_create(ALreverbStateFactory* UNUSED(factory))
{
ALreverbState *state;
alcall_once(&mixfunc_inited, init_mixfunc);
NEW_OBJ0(state, ALreverbState)();
if(!state) return NULL;
return STATIC_CAST(ALeffectState, state);
}
DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory);
ALeffectStateFactory *ALreverbStateFactory_getFactory(void)
{
static ALreverbStateFactory ReverbFactory = { { GET_VTABLE2(ALreverbStateFactory, ALeffectStateFactory) } };
return STATIC_CAST(ALeffectStateFactory, &ReverbFactory);
}
void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_DECAY_HFLIMIT:
if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayHFLimit = val;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
{
ALeaxreverb_setParami(effect, context, param, vals[0]);
}
void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_DENSITY:
if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Density = val;
break;
case AL_EAXREVERB_DIFFUSION:
if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Diffusion = val;
break;
case AL_EAXREVERB_GAIN:
if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Gain = val;
break;
case AL_EAXREVERB_GAINHF:
if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.GainHF = val;
break;
case AL_EAXREVERB_GAINLF:
if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.GainLF = val;
break;
case AL_EAXREVERB_DECAY_TIME:
if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayTime = val;
break;
case AL_EAXREVERB_DECAY_HFRATIO:
if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayHFRatio = val;
break;
case AL_EAXREVERB_DECAY_LFRATIO:
if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayLFRatio = val;
break;
case AL_EAXREVERB_REFLECTIONS_GAIN:
if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ReflectionsGain = val;
break;
case AL_EAXREVERB_REFLECTIONS_DELAY:
if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ReflectionsDelay = val;
break;
case AL_EAXREVERB_LATE_REVERB_GAIN:
if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LateReverbGain = val;
break;
case AL_EAXREVERB_LATE_REVERB_DELAY:
if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LateReverbDelay = val;
break;
case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.AirAbsorptionGainHF = val;
break;
case AL_EAXREVERB_ECHO_TIME:
if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.EchoTime = val;
break;
case AL_EAXREVERB_ECHO_DEPTH:
if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.EchoDepth = val;
break;
case AL_EAXREVERB_MODULATION_TIME:
if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ModulationTime = val;
break;
case AL_EAXREVERB_MODULATION_DEPTH:
if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ModulationDepth = val;
break;
case AL_EAXREVERB_HFREFERENCE:
if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.HFReference = val;
break;
case AL_EAXREVERB_LFREFERENCE:
if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LFReference = val;
break;
case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.RoomRolloffFactor = val;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_REFLECTIONS_PAN:
if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ReflectionsPan[0] = vals[0];
props->Reverb.ReflectionsPan[1] = vals[1];
props->Reverb.ReflectionsPan[2] = vals[2];
break;
case AL_EAXREVERB_LATE_REVERB_PAN:
if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LateReverbPan[0] = vals[0];
props->Reverb.LateReverbPan[1] = vals[1];
props->Reverb.LateReverbPan[2] = vals[2];
break;
default:
ALeaxreverb_setParamf(effect, context, param, vals[0]);
break;
}
}
void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_DECAY_HFLIMIT:
*val = props->Reverb.DecayHFLimit;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
{
ALeaxreverb_getParami(effect, context, param, vals);
}
void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_DENSITY:
*val = props->Reverb.Density;
break;
case AL_EAXREVERB_DIFFUSION:
*val = props->Reverb.Diffusion;
break;
case AL_EAXREVERB_GAIN:
*val = props->Reverb.Gain;
break;
case AL_EAXREVERB_GAINHF:
*val = props->Reverb.GainHF;
break;
case AL_EAXREVERB_GAINLF:
*val = props->Reverb.GainLF;
break;
case AL_EAXREVERB_DECAY_TIME:
*val = props->Reverb.DecayTime;
break;
case AL_EAXREVERB_DECAY_HFRATIO:
*val = props->Reverb.DecayHFRatio;
break;
case AL_EAXREVERB_DECAY_LFRATIO:
*val = props->Reverb.DecayLFRatio;
break;
case AL_EAXREVERB_REFLECTIONS_GAIN:
*val = props->Reverb.ReflectionsGain;
break;
case AL_EAXREVERB_REFLECTIONS_DELAY:
*val = props->Reverb.ReflectionsDelay;
break;
case AL_EAXREVERB_LATE_REVERB_GAIN:
*val = props->Reverb.LateReverbGain;
break;
case AL_EAXREVERB_LATE_REVERB_DELAY:
*val = props->Reverb.LateReverbDelay;
break;
case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
*val = props->Reverb.AirAbsorptionGainHF;
break;
case AL_EAXREVERB_ECHO_TIME:
*val = props->Reverb.EchoTime;
break;
case AL_EAXREVERB_ECHO_DEPTH:
*val = props->Reverb.EchoDepth;
break;
case AL_EAXREVERB_MODULATION_TIME:
*val = props->Reverb.ModulationTime;
break;
case AL_EAXREVERB_MODULATION_DEPTH:
*val = props->Reverb.ModulationDepth;
break;
case AL_EAXREVERB_HFREFERENCE:
*val = props->Reverb.HFReference;
break;
case AL_EAXREVERB_LFREFERENCE:
*val = props->Reverb.LFReference;
break;
case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
*val = props->Reverb.RoomRolloffFactor;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_EAXREVERB_REFLECTIONS_PAN:
vals[0] = props->Reverb.ReflectionsPan[0];
vals[1] = props->Reverb.ReflectionsPan[1];
vals[2] = props->Reverb.ReflectionsPan[2];
break;
case AL_EAXREVERB_LATE_REVERB_PAN:
vals[0] = props->Reverb.LateReverbPan[0];
vals[1] = props->Reverb.LateReverbPan[1];
vals[2] = props->Reverb.LateReverbPan[2];
break;
default:
ALeaxreverb_getParamf(effect, context, param, vals);
break;
}
}
DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_REVERB_DECAY_HFLIMIT:
if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayHFLimit = val;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
{
ALreverb_setParami(effect, context, param, vals[0]);
}
void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
{
ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_REVERB_DENSITY:
if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Density = val;
break;
case AL_REVERB_DIFFUSION:
if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Diffusion = val;
break;
case AL_REVERB_GAIN:
if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.Gain = val;
break;
case AL_REVERB_GAINHF:
if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.GainHF = val;
break;
case AL_REVERB_DECAY_TIME:
if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayTime = val;
break;
case AL_REVERB_DECAY_HFRATIO:
if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.DecayHFRatio = val;
break;
case AL_REVERB_REFLECTIONS_GAIN:
if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ReflectionsGain = val;
break;
case AL_REVERB_REFLECTIONS_DELAY:
if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.ReflectionsDelay = val;
break;
case AL_REVERB_LATE_REVERB_GAIN:
if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LateReverbGain = val;
break;
case AL_REVERB_LATE_REVERB_DELAY:
if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.LateReverbDelay = val;
break;
case AL_REVERB_AIR_ABSORPTION_GAINHF:
if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.AirAbsorptionGainHF = val;
break;
case AL_REVERB_ROOM_ROLLOFF_FACTOR:
if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
props->Reverb.RoomRolloffFactor = val;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
{
ALreverb_setParamf(effect, context, param, vals[0]);
}
void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_REVERB_DECAY_HFLIMIT:
*val = props->Reverb.DecayHFLimit;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
{
ALreverb_getParami(effect, context, param, vals);
}
void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
{
const ALeffectProps *props = &effect->Props;
switch(param)
{
case AL_REVERB_DENSITY:
*val = props->Reverb.Density;
break;
case AL_REVERB_DIFFUSION:
*val = props->Reverb.Diffusion;
break;
case AL_REVERB_GAIN:
*val = props->Reverb.Gain;
break;
case AL_REVERB_GAINHF:
*val = props->Reverb.GainHF;
break;
case AL_REVERB_DECAY_TIME:
*val = props->Reverb.DecayTime;
break;
case AL_REVERB_DECAY_HFRATIO:
*val = props->Reverb.DecayHFRatio;
break;
case AL_REVERB_REFLECTIONS_GAIN:
*val = props->Reverb.ReflectionsGain;
break;
case AL_REVERB_REFLECTIONS_DELAY:
*val = props->Reverb.ReflectionsDelay;
break;
case AL_REVERB_LATE_REVERB_GAIN:
*val = props->Reverb.LateReverbGain;
break;
case AL_REVERB_LATE_REVERB_DELAY:
*val = props->Reverb.LateReverbDelay;
break;
case AL_REVERB_AIR_ABSORPTION_GAINHF:
*val = props->Reverb.AirAbsorptionGainHF;
break;
case AL_REVERB_ROOM_ROLLOFF_FACTOR:
*val = props->Reverb.RoomRolloffFactor;
break;
default:
SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
}
}
void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
{
ALreverb_getParamf(effect, context, param, vals);
}
DEFINE_ALEFFECT_VTABLE(ALreverb);
|