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
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Portable code to mix sounds for snd_dma.cpp.
//
//=============================================================================//
#include "audio_pch.h"
#include "mouthinfo.h"
#include "../../cl_main.h"
#include "icliententitylist.h"
#include "icliententity.h"
#include "../../sys_dll.h"
#include "video/ivideoservices.h"
#include "engine/IEngineSound.h"
#if defined( REPLAY_ENABLED )
#include "demo.h"
#include "replay_internal.h"
#endif
#ifdef GNUC
// we don't suport the ASM in this file right now under GCC, fallback to C libs
#undef id386
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#if defined( REPLAY_ENABLED )
extern IReplayMovieManager *g_pReplayMovieManager;
#endif
#if defined(_WIN32) && id386
// warning C4731: frame pointer register 'ebp' modified by inline assembly code
#pragma warning(disable : 4731)
#endif
// NOTE: !!!!!! YOU MUST UPDATE SND_MIXA.S IF THIS VALUE IS CHANGED !!!!!
#define SND_SCALE_BITS 7
#define SND_SCALE_SHIFT (8-SND_SCALE_BITS)
#define SND_SCALE_LEVELS (1<<SND_SCALE_BITS)
#define SND_SCALE_BITS16 8
#define SND_SCALE_SHIFT16 (8-SND_SCALE_BITS16)
#define SND_SCALE_LEVELS16 (1<<SND_SCALE_BITS16)
void Snd_WriteLinearBlastStereo16(void);
void SND_PaintChannelFrom8( portable_samplepair_t *pOutput, int *volume, byte *pData8, int count );
bool Con_IsVisible( void );
void SND_RecordBuffer( void );
bool DSP_RoomDSPIsOff( void );
bool BChannelLowVolume( channel_t *pch, int vol_min );
void ChannelCopyVolumes( channel_t *pch, int *pvolume_dest, int ivol_start, int cvol );
float ChannelLoudestCurVolume( const channel_t * RESTRICT pch );
extern int g_soundtime;
extern float host_frametime;
extern float host_frametime_unbounded;
#if !defined( NO_VOICE )
extern int g_SND_VoiceOverdriveInt;
#endif
extern ConVar dsp_room;
extern ConVar dsp_water;
extern ConVar dsp_player;
extern ConVar dsp_facingaway;
extern ConVar snd_showstart;
extern ConVar dsp_automatic;
extern ConVar snd_pitchquality;
extern float DSP_ROOM_MIX;
extern float DSP_NOROOM_MIX;
portable_samplepair_t *g_paintbuffer;
// temp paintbuffer - not included in main list of paintbuffers
// NOTE: this paintbuffer is also used as a copy buffer by interpolating pitch
// shift routines. Decreasing TEMP_COPY_BUFFER_SIZE (or PAINTBUFFER_MEM_SIZE)
// will decrease the maximum pitch level (current 4.0)!
portable_samplepair_t *g_temppaintbuffer = NULL;
CUtlVector< paintbuffer_t > g_paintBuffers;
// pointer to current paintbuffer (front and reare), used by all mixing, upsampling and dsp routines
portable_samplepair_t *g_curpaintbuffer = NULL;
portable_samplepair_t *g_currearpaintbuffer = NULL;
portable_samplepair_t *g_curcenterpaintbuffer = NULL;
bool g_bdirectionalfx;
bool g_bDspOff;
float g_dsp_volume;
// dsp performance timing
unsigned g_snd_call_time_debug = 0;
unsigned g_snd_time_debug = 0;
unsigned g_snd_count_debug = 0;
unsigned g_snd_samplecount = 0;
unsigned g_snd_frametime = 0;
unsigned g_snd_frametime_total = 0;
int g_snd_profile_type = 0; // type 1 dsp, type 2 mixer, type 3 load sound, type 4 all sound
#define FILTERTYPE_NONE 0
#define FILTERTYPE_LINEAR 1
#define FILTERTYPE_CUBIC 2
// filter memory for upsampling
portable_samplepair_t cubicfilter1[3] = {{0,0},{0,0},{0,0}};
portable_samplepair_t cubicfilter2[3] = {{0,0},{0,0},{0,0}};
portable_samplepair_t linearfilter1[1] = {{0,0}};
portable_samplepair_t linearfilter2[1] = {{0,0}};
portable_samplepair_t linearfilter3[1] = {{0,0}};
portable_samplepair_t linearfilter4[1] = {{0,0}};
portable_samplepair_t linearfilter5[1] = {{0,0}};
portable_samplepair_t linearfilter6[1] = {{0,0}};
portable_samplepair_t linearfilter7[1] = {{0,0}};
portable_samplepair_t linearfilter8[1] = {{0,0}};
int snd_scaletable[SND_SCALE_LEVELS][256]; // 32k*4 = 128K
int *snd_p, snd_linear_count, snd_vol;
short *snd_out;
extern int DSP_Alloc( int ipset, float xfade, int cchan );
bool DSP_CheckDspAutoEnabled( void );
int Get_idsp_room ( void );
int dsp_room_GetInt ( void );
void DSP_SetDspAuto( int dsp_preset );
bool DSP_CheckDspAutoEnabled( void );
void MIX_ScalePaintBuffer( int bufferIndex, int count, float fgain );
bool IsReplayRendering()
{
#if defined( REPLAY_ENABLED )
return g_pReplayMovieManager && g_pReplayMovieManager->IsRendering();
#else
return false;
#endif
}
//-----------------------------------------------------------------------------
// Free allocated memory buffers
//-----------------------------------------------------------------------------
void MIX_FreeAllPaintbuffers(void)
{
if ( g_paintBuffers.Count() )
{
if ( g_temppaintbuffer )
{
_aligned_free( g_temppaintbuffer );
g_temppaintbuffer = NULL;
}
for ( int i = 0; i < g_paintBuffers.Count(); i++ )
{
if ( g_paintBuffers[i].pbuf )
{
_aligned_free( g_paintBuffers[i].pbuf );
}
if ( g_paintBuffers[i].pbufrear )
{
_aligned_free( g_paintBuffers[i].pbufrear );
}
if ( g_paintBuffers[i].pbufcenter )
{
_aligned_free( g_paintBuffers[i].pbufcenter );
}
}
g_paintBuffers.RemoveAll();
}
}
void MIX_InitializePaintbuffer( paintbuffer_t *pPaintBuffer, bool bSurround, bool bSurroundCenter )
{
V_memset( pPaintBuffer, 0, sizeof( *pPaintBuffer ) );
pPaintBuffer->pbuf = (portable_samplepair_t *)_aligned_malloc( PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t), 16 );
V_memset( pPaintBuffer->pbuf, 0, PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t) );
if ( bSurround )
{
pPaintBuffer->pbufrear = (portable_samplepair_t *)_aligned_malloc( PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t), 16 );
V_memset( pPaintBuffer->pbufrear, 0, PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t) );
}
if ( bSurroundCenter )
{
pPaintBuffer->pbufcenter = (portable_samplepair_t *)_aligned_malloc( PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t), 16 );
V_memset( pPaintBuffer->pbufcenter, 0, PAINTBUFFER_MEM_SIZE*sizeof(portable_samplepair_t) );
}
}
//-----------------------------------------------------------------------------
// Allocate memory buffers
// Initialize paintbuffers array, set current paint buffer to main output buffer SOUND_BUFFER_PAINT
//-----------------------------------------------------------------------------
bool MIX_InitAllPaintbuffers(void)
{
bool bSurround;
bool bSurroundCenter;
bSurroundCenter = g_AudioDevice->IsSurroundCenter();
bSurround = g_AudioDevice->IsSurround() || bSurroundCenter;
g_temppaintbuffer = (portable_samplepair_t*)_aligned_malloc( TEMP_COPY_BUFFER_SIZE*sizeof(portable_samplepair_t), 16 );
V_memset( g_temppaintbuffer, 0, TEMP_COPY_BUFFER_SIZE*sizeof(portable_samplepair_t) );
while ( g_paintBuffers.Count() < SOUND_BUFFER_BASETOTAL )
{
int nIndex = g_paintBuffers.AddToTail();
MIX_InitializePaintbuffer( &(g_paintBuffers[ nIndex ]), bSurround, bSurroundCenter );
}
g_paintbuffer = g_paintBuffers[SOUND_BUFFER_PAINT].pbuf;
// buffer flags
g_paintBuffers[SOUND_BUFFER_ROOM].flags = SOUND_BUSS_ROOM;
g_paintBuffers[SOUND_BUFFER_FACING].flags = SOUND_BUSS_FACING;
g_paintBuffers[SOUND_BUFFER_FACINGAWAY].flags = SOUND_BUSS_FACINGAWAY;
g_paintBuffers[SOUND_BUFFER_SPEAKER].flags = SOUND_BUSS_SPEAKER;
g_paintBuffers[SOUND_BUFFER_DRY].flags = SOUND_BUSS_DRY;
// buffer surround sound flag
g_paintBuffers[SOUND_BUFFER_PAINT].fsurround = bSurround;
g_paintBuffers[SOUND_BUFFER_FACING].fsurround = bSurround;
g_paintBuffers[SOUND_BUFFER_FACINGAWAY].fsurround = bSurround;
g_paintBuffers[SOUND_BUFFER_DRY].fsurround = bSurround;
// buffer 5 channel surround sound flag
g_paintBuffers[SOUND_BUFFER_PAINT].fsurround_center = bSurroundCenter;
g_paintBuffers[SOUND_BUFFER_FACING].fsurround_center = bSurroundCenter;
g_paintBuffers[SOUND_BUFFER_FACINGAWAY].fsurround_center = bSurroundCenter;
g_paintBuffers[SOUND_BUFFER_DRY].fsurround_center = bSurroundCenter;
// room buffer mixes down to mono or stereo, never to 4 or 5 ch
g_paintBuffers[SOUND_BUFFER_ROOM].fsurround = false;
g_paintBuffers[SOUND_BUFFER_ROOM].fsurround_center = false;
// speaker buffer mixes to mono
g_paintBuffers[SOUND_BUFFER_SPEAKER].fsurround = false;
g_paintBuffers[SOUND_BUFFER_SPEAKER].fsurround_center = false;
MIX_SetCurrentPaintbuffer( SOUND_BUFFER_PAINT );
return true;
}
// called before loading samples to mix - cap the mix rate (ie: pitch) so that
// we never overflow the mix copy buffer.
double MIX_GetMaxRate( double rate, int sampleCount )
{
if (rate <= 2.0)
return rate;
// copybuf_bytes = rate_max * samples_max * samplesize_max
// so:
// rate_max = copybuf_bytes / (samples_max * samplesize_max )
double samplesize_max = 4.0; // stereo 16bit samples
double copybuf_bytes = (double)(TEMP_COPY_BUFFER_SIZE * sizeof(portable_samplepair_t));
double samples_max = (double)(PAINTBUFFER_SIZE);
double rate_max = copybuf_bytes / (samples_max * samplesize_max);
// make sure sampleCount is never greater than paintbuffer samples
// (this should have been set up in MIX_PaintChannels)
Assert (sampleCount <= PAINTBUFFER_SIZE);
return fpmin( rate, rate_max );
}
// Transfer (endtime - lpaintedtime) stereo samples in pfront out to hardware
// pfront - pointer to stereo paintbuffer - 32 bit samples, interleaved stereo
// lpaintedtime - total number of 32 bit stereo samples previously output to hardware
// endtime - total number of 32 bit stereo samples currently mixed in paintbuffer
void S_TransferStereo16( void *pOutput, const portable_samplepair_t *pfront, int lpaintedtime, int endtime )
{
int lpos;
if ( IsX360() )
{
// not the right path for 360
Assert( 0 );
return;
}
Assert( pOutput );
snd_vol = S_GetMasterVolume()*256;
snd_p = (int *)pfront;
// get size of output buffer in full samples (LR pairs)
int samplePairCount = g_AudioDevice->DeviceSampleCount() >> 1;
int sampleMask = samplePairCount - 1;
bool bShouldPlaySound = !cl_movieinfo.IsRecording() && !IsReplayRendering();
while ( lpaintedtime < endtime )
{
// pbuf can hold 16384, 16 bit L/R samplepairs.
// lpaintedtime - where to start painting into dma buffer.
// (modulo size of dma buffer for current position).
// handle recirculating buffer issues
// lpos - samplepair index into dma buffer. First samplepair from paintbuffer to be xfered here.
lpos = lpaintedtime & sampleMask;
// snd_out is L/R sample index into dma buffer. First L sample from paintbuffer goes here.
snd_out = (short *)pOutput + (lpos<<1);
// snd_linear_count is number of samplepairs between end of dma buffer and xfer start index.
snd_linear_count = samplePairCount - lpos;
// clamp snd_linear_count to be only as many samplepairs premixed
if ( snd_linear_count > endtime - lpaintedtime )
{
// endtime - lpaintedtime = number of premixed sample pairs ready for xfer.
snd_linear_count = endtime - lpaintedtime;
}
// snd_linear_count is now number of mono 16 bit samples (L and R) to xfer.
snd_linear_count <<= 1;
// write a linear blast of samples
SND_RecordBuffer();
if ( bShouldPlaySound )
{
// transfer 16bit samples from snd_p into snd_out, multiplying each sample by volume.
Snd_WriteLinearBlastStereo16();
}
// advance paintbuffer pointer
snd_p += snd_linear_count;
// advance lpaintedtime by number of samplepairs just xfered.
lpaintedtime += (snd_linear_count>>1);
}
}
// Transfer contents of main paintbuffer pfront out to
// device. Perform volume multiply on each sample.
void S_TransferPaintBuffer(void *pOutput, const portable_samplepair_t *pfront, int lpaintedtime, int endtime)
{
int out_idx; // mono sample index
int count; // number of mono samples to output
int out_mask;
int step;
int val;
int nSoundVol;
const int *p;
if ( IsX360() )
{
// not the right path for 360
Assert( 0 );
return;
}
Assert( pOutput );
p = (const int *) pfront;
count = ((endtime - lpaintedtime) * g_AudioDevice->DeviceChannels());
out_mask = g_AudioDevice->DeviceSampleCount() - 1;
// 44k: remove old 22k sound support << HISPEED_DMA
// out_idx = ((paintedtime << HISPEED_DMA) * g_AudioDevice->DeviceChannels()) & out_mask;
out_idx = (lpaintedtime * g_AudioDevice->DeviceChannels()) & out_mask;
step = 3 - g_AudioDevice->DeviceChannels(); // mono output buffer - step 2, stereo - step 1
nSoundVol = S_GetMasterVolume()*256;
if (g_AudioDevice->DeviceSampleBits() == 16)
{
short *out = (short *) pOutput;
while (count--)
{
val = (*p * nSoundVol) >> 8;
p+= step;
val = CLIP(val);
out[out_idx] = val;
out_idx = (out_idx + 1) & out_mask;
}
}
else if (g_AudioDevice->DeviceSampleBits() == 8)
{
unsigned char *out = (unsigned char *) pOutput;
while (count--)
{
val = (*p * nSoundVol) >> 8;
p+= step;
val = CLIP(val);
out[out_idx] = (val>>8) + 128;
out_idx = (out_idx + 1) & out_mask;
}
}
}
/*
===============================================================================
CHANNEL MIXING
===============================================================================
*/
// free channel so that it may be allocated by the
// next request to play a sound. If sound is a
// word in a sentence, release the sentence.
// Works for static, dynamic, sentence and stream sounds
void S_FreeChannel(channel_t *ch)
{
// Don't reenter in here (can happen inside voice code).
if ( ch->flags.m_bIsFreeingChannel )
return;
ch->flags.m_bIsFreeingChannel = true;
SND_CloseMouth(ch);
g_pSoundServices->OnSoundStopped( ch->guid, ch->soundsource, ch->entchannel, ch->sfx->getname() );
ch->flags.isSentence = false;
// Msg("End sound %s\n", ch->sfx->getname() );
delete ch->pMixer;
ch->pMixer = NULL;
ch->sfx = NULL;
// zero all data in channel
g_ActiveChannels.Remove( ch );
Q_memset(ch, 0, sizeof(channel_t));
}
// Mix all channels into active paintbuffers until paintbuffer is full or 'endtime' is reached.
// endtime: time in 44khz samples to mix
// rate: ignore samples which are not natively at this rate (for multipass mixing/filtering)
// if rate == SOUND_ALL_RATES then mix all samples this pass
// flags: if SOUND_MIX_DRY, then mix only samples with channel flagged as 'dry'
// outputRate: target mix rate for all samples. Note, if outputRate = SOUND_DMA_SPEED, then
// this routine will fill the paintbuffer to endtime. Otherwise, fewer samples are mixed.
// if (endtime - paintedtime) is not aligned on boundaries of 4,
// we'll miss data if outputRate < SOUND_DMA_SPEED!
void MIX_MixChannelsToPaintbuffer( CChannelList &list, int endtime, int flags, int rate, int outputRate )
{
VPROF( "MixChannelsToPaintbuffer" );
int i;
int sampleCount;
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s c:%d %d/%d", __FUNCTION__, list.Count(), rate, outputRate );
// mix each channel into paintbuffer
// validate parameters
Assert( outputRate <= SOUND_DMA_SPEED );
Assert( !((endtime - g_paintedtime) & 0x3) || (outputRate == SOUND_DMA_SPEED) ); // make sure we're not discarding data
// 44k: try to mix this many samples at outputRate
sampleCount = ( endtime - g_paintedtime ) / ( SOUND_DMA_SPEED / outputRate );
if ( sampleCount <= 0 )
return;
// Apply a global pitch shift if we're playing back a time-scaled replay
float flGlobalPitchScale = 1.0f;
#if defined( REPLAY_ENABLED )
extern IDemoPlayer *g_pReplayDemoPlayer;
if ( demoplayer->IsPlayingBack() && demoplayer == g_pReplayDemoPlayer )
{
// adjust time scale if playing back demo
flGlobalPitchScale = demoplayer->GetPlaybackTimeScale();
}
#endif
for ( i = list.Count(); --i >= 0; )
{
channel_t *ch = list.GetChannel( i );
Assert( ch->sfx );
// must never have a 'dry' and 'speaker' set - causes double mixing & double data reading
Assert ( !( ( ch->flags.bdry && ch->flags.bSpeaker ) || ( ch->flags.bdry && ch->special_dsp != 0 ) ) );
// if mixing with SOUND_MIX_DRY flag, ignore (don't even load) all channels not flagged as 'dry'
if ( flags == SOUND_MIX_DRY )
{
if ( !ch->flags.bdry )
continue;
}
// if mixing with SOUND_MIX_WET flag, ignore (don't even load) all channels flagged as 'dry' or 'speaker'
if ( flags == SOUND_MIX_WET )
{
if ( ch->flags.bdry || ch->flags.bSpeaker || ch->special_dsp != 0 )
continue;
}
// if mixing with SOUND_MIX_SPEAKER flag, ignore (don't even load) all channels not flagged as 'speaker'
if ( flags == SOUND_MIX_SPEAKER )
{
if ( !ch->flags.bSpeaker )
continue;
}
// if mixing with SOUND_MIX_SPEAKER flag, ignore (don't even load) all channels not flagged as 'speaker'
if ( flags == SOUND_MIX_SPECIAL_DSP )
{
if ( ch->special_dsp == 0 )
continue;
}
// multipass mixing - only mix samples of specified sample rate
switch ( rate )
{
case SOUND_11k:
case SOUND_22k:
case SOUND_44k:
if ( rate != ch->sfx->pSource->SampleRate() )
continue;
break;
default:
case SOUND_ALL_RATES:
break;
}
// Tracker 20771, if breen is speaking through the monitor, the client doesn't have an entity
// for the "soundsource" but we still need the lipsync to pause if the game is paused. Therefore
// I changed SND_IsMouth to look for any .wav on any channels which has sentence data
bool bIsMouth = SND_IsMouth(ch);
bool bShouldPause = IsX360() ? !ch->sfx->m_bIsUISound : bIsMouth;
// Tracker 14637: Pausing the game pauses voice sounds, but not other sounds...
if ( bShouldPause && g_pSoundServices->IsGamePaused() )
{
continue;
}
if ( bIsMouth )
{
if ( ( ch->soundsource == SOUND_FROM_UI_PANEL ) || entitylist->GetClientEntity(ch->soundsource) ||
( ch->flags.bSpeaker && entitylist->GetClientEntity( ch->speakerentity ) ) )
{
// UNDONE: recode this as a member function of CAudioMixer
SND_MoveMouth8(ch, ch->sfx->pSource, sampleCount);
}
}
// mix channel to all active paintbuffers:
// mix 'dry' sounds only to dry paintbuffer.
// mix 'speaker' sounds only to speaker paintbuffer.
// mix all other sounds between room, facing & facingaway paintbuffers
// NOTE: must be called once per channel only - consecutive calls retrieve additional data.
float flPitch = ch->pitch;
ch->pitch *= flGlobalPitchScale;
if (list.IsQuashed(i))
{
// If the sound has been silenced as a performance heuristic, quash it.
ch->pMixer->SkipSamples( ch, sampleCount, outputRate, 0 );
// DevMsg("Quashed channel %d (%s)\n", i, ch->sfx->GetFileName());
}
else
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "MixDataToDevice" );
ch->pMixer->MixDataToDevice( g_AudioDevice, ch, sampleCount, outputRate, 0 );
}
// restore to original pitch settings
ch->pitch = flPitch;
if ( !ch->pMixer->ShouldContinueMixing() )
{
S_FreeChannel( ch );
list.RemoveChannelFromList(i);
}
if ( (ch->nFreeChannelAtSampleTime > 0 && (int)ch->nFreeChannelAtSampleTime <= endtime) )
{
S_FreeChannel( ch );
list.RemoveChannelFromList(i);
}
}
}
// pass in index -1...count+2, return pointer to source sample in either paintbuffer or delay buffer
inline portable_samplepair_t * S_GetNextpFilter(int i, portable_samplepair_t *pbuffer, portable_samplepair_t *pfiltermem)
{
// The delay buffer is assumed to precede the paintbuffer by 6 duplicated samples
if (i == -1)
return (&(pfiltermem[0]));
if (i == 0)
return (&(pfiltermem[1]));
if (i == 1)
return (&(pfiltermem[2]));
// return from paintbuffer, where samples are doubled.
// even samples are to be replaced with interpolated value.
return (&(pbuffer[(i-2)*2 + 1]));
}
// pass forward over passed in buffer and cubic interpolate all odd samples
// pbuffer: buffer to filter (in place)
// prevfilter: filter memory. NOTE: this must match the filtertype ie: filtercubic[] for FILTERTYPE_CUBIC
// if NULL then perform no filtering. UNDONE: should have a filter memory array type
// count: how many samples to upsample. will become count*2 samples in buffer, in place.
void S_Interpolate2xCubic( portable_samplepair_t *pbuffer, portable_samplepair_t *pfiltermem, int cfltmem, int count )
{
// implement cubic interpolation on 2x upsampled buffer. Effectively delays buffer contents by 2 samples.
// pbuffer: contains samples at 0, 2, 4, 6...
// temppaintbuffer is temp buffer, of same or larger size than a paintbuffer, used to store processed values
// count: number of samples to process in buffer ie: how many samples at 0, 2, 4, 6...
// finpos is the fractional, inpos the integer part.
// finpos = 0.5 for upsampling by 2x
// inpos is the position of the sample
// xm1 = x [inpos - 1];
// x0 = x [inpos + 0];
// x1 = x [inpos + 1];
// x2 = x [inpos + 2];
// a = (3 * (x0-x1) - xm1 + x2) / 2;
// b = 2*x1 + xm1 - (5*x0 + x2) / 2;
// c = (x1 - xm1) / 2;
// y [outpos] = (((a * finpos) + b) * finpos + c) * finpos + x0;
int i, upCount = count << 1;
int a, b, c;
int xm1, x0, x1, x2;
portable_samplepair_t *psamp0;
portable_samplepair_t *psamp1;
portable_samplepair_t *psamp2;
portable_samplepair_t *psamp3;
int outpos = 0;
Assert (upCount <= PAINTBUFFER_SIZE);
// pfiltermem holds 6 samples from previous buffer pass
// process 'count' samples
for ( i = 0; i < count; i++)
{
// get source sample pointer
psamp0 = S_GetNextpFilter(i-1, pbuffer, pfiltermem);
psamp1 = S_GetNextpFilter(i, pbuffer, pfiltermem);
psamp2 = S_GetNextpFilter(i+1, pbuffer, pfiltermem);
psamp3 = S_GetNextpFilter(i+2, pbuffer, pfiltermem);
// write out original sample to interpolation buffer
g_temppaintbuffer[outpos++] = *psamp1;
// get all left samples for interpolation window
xm1 = psamp0->left;
x0 = psamp1->left;
x1 = psamp2->left;
x2 = psamp3->left;
// interpolate
a = (3 * (x0-x1) - xm1 + x2) / 2;
b = 2*x1 + xm1 - (5*x0 + x2) / 2;
c = (x1 - xm1) / 2;
// write out interpolated sample
g_temppaintbuffer[outpos].left = a/8 + b/4 + c/2 + x0;
// get all right samples for window
xm1 = psamp0->right;
x0 = psamp1->right;
x1 = psamp2->right;
x2 = psamp3->right;
// interpolate
a = (3 * (x0-x1) - xm1 + x2) / 2;
b = 2*x1 + xm1 - (5*x0 + x2) / 2;
c = (x1 - xm1) / 2;
// write out interpolated sample, increment output counter
g_temppaintbuffer[outpos++].right = a/8 + b/4 + c/2 + x0;
Assert( outpos <= TEMP_COPY_BUFFER_SIZE );
}
Assert(cfltmem >= 3);
// save last 3 samples from paintbuffer
pfiltermem[0] = pbuffer[upCount - 5];
pfiltermem[1] = pbuffer[upCount - 3];
pfiltermem[2] = pbuffer[upCount - 1];
// copy temppaintbuffer back into paintbuffer
for (i = 0; i < upCount; i++)
pbuffer[i] = g_temppaintbuffer[i];
}
// pass forward over passed in buffer and linearly interpolate all odd samples
// pbuffer: buffer to filter (in place)
// prevfilter: filter memory. NOTE: this must match the filtertype ie: filterlinear[] for FILTERTYPE_LINEAR
// if NULL then perform no filtering.
// count: how many samples to upsample. will become count*2 samples in buffer, in place.
void S_Interpolate2xLinear( portable_samplepair_t *pbuffer, portable_samplepair_t *pfiltermem, int cfltmem, int count )
{
int i, upCount = count<<1;
Assert (upCount <= PAINTBUFFER_SIZE);
Assert (cfltmem >= 1);
// use interpolation value from previous mix
pbuffer[0].left = (pfiltermem->left + pbuffer[0].left) >> 1;
pbuffer[0].right = (pfiltermem->right + pbuffer[0].right) >> 1;
for ( i = 2; i < upCount; i+=2)
{
// use linear interpolation for upsampling
pbuffer[i].left = (pbuffer[i].left + pbuffer[i-1].left) >> 1;
pbuffer[i].right = (pbuffer[i].right + pbuffer[i-1].right) >> 1;
}
// save last value to be played out in buffer
*pfiltermem = pbuffer[upCount - 1];
}
// Optimized routine. 2.27X faster than the above routine
void S_Interpolate2xLinear_2( int count, portable_samplepair_t *pbuffer, portable_samplepair_t *pfiltermem, int cfltmem )
{
Assert (cfltmem >= 1);
int sample = count-1;
int end = (count*2)-1;
portable_samplepair_t *pwrite = &pbuffer[end];
portable_samplepair_t *pread = &pbuffer[sample];
portable_samplepair_t last = pread[0];
pread--;
// PERFORMANCE: Unroll the loop 8 times. This improves speed quite a bit
for ( ;sample >= 8; sample -= 8 )
{
pwrite[0] = last;
pwrite[-1].left = (pread[0].left + last.left)>>1;
pwrite[-1].right = (pread[0].right + last.right)>>1;
last = pread[0];
pwrite[-2] = last;
pwrite[-3].left = (pread[-1].left + last.left)>>1;
pwrite[-3].right = (pread[-1].right + last.right)>>1;
last = pread[-1];
pwrite[-4] = last;
pwrite[-5].left = (pread[-2].left + last.left)>>1;
pwrite[-5].right = (pread[-2].right + last.right)>>1;
last = pread[-2];
pwrite[-6] = last;
pwrite[-7].left = (pread[-3].left + last.left)>>1;
pwrite[-7].right = (pread[-3].right + last.right)>>1;
last = pread[-3];
pwrite[-8] = last;
pwrite[-9].left = (pread[-4].left + last.left)>>1;
pwrite[-9].right = (pread[-4].right + last.right)>>1;
last = pread[-4];
pwrite[-10] = last;
pwrite[-11].left = (pread[-5].left + last.left)>>1;
pwrite[-11].right = (pread[-5].right + last.right)>>1;
last = pread[-5];
pwrite[-12] = last;
pwrite[-13].left = (pread[-6].left + last.left)>>1;
pwrite[-13].right = (pread[-6].right + last.right)>>1;
last = pread[-6];
pwrite[-14] = last;
pwrite[-15].left = (pread[-7].left + last.left)>>1;
pwrite[-15].right = (pread[-7].right + last.right)>>1;
last = pread[-7];
pread -= 8;
pwrite -= 16;
}
while ( pread >= pbuffer )
{
pwrite[0] = last;
pwrite[-1].left = (pread[0].left + last.left)>>1;
pwrite[-1].right = (pread[0].right + last.right)>>1;
last = pread[0];
pread--;
pwrite-=2;
}
pbuffer[1] = last;
pbuffer[0].left = (pfiltermem->left + last.left) >> 1;
pbuffer[0].right = (pfiltermem->right + last.right) >> 1;
*pfiltermem = pbuffer[end];
}
// upsample by 2x, optionally using interpolation
// count: how many samples to upsample. will become count*2 samples in buffer, in place.
// pbuffer: buffer to upsample into (in place)
// pfiltermem: filter memory. NOTE: this must match the filtertype ie: filterlinear[] for FILTERTYPE_LINEAR
// if NULL then perform no filtering.
// cfltmem: max number of sample pairs filter can use
// filtertype: FILTERTYPE_NONE, _LINEAR, _CUBIC etc. Must match prevfilter.
void S_MixBufferUpsample2x( int count, portable_samplepair_t *pbuffer, portable_samplepair_t *pfiltermem, int cfltmem, int filtertype )
{
// JAY: Optimized this routine. Test then remove old routine.
// NOTE: Has been proven equivalent by comparing output.
if ( filtertype == FILTERTYPE_LINEAR )
{
S_Interpolate2xLinear_2( count, pbuffer, pfiltermem, cfltmem );
return;
}
int i, j, upCount = count<<1;
// reverse through buffer, duplicating contents for 'count' samples
for (i = upCount - 1, j = count - 1; j >= 0; i-=2, j--)
{
pbuffer[i] = pbuffer[j];
pbuffer[i-1] = pbuffer[j];
}
// pass forward through buffer, interpolate all even slots
switch (filtertype)
{
default:
break;
case FILTERTYPE_LINEAR:
S_Interpolate2xLinear(pbuffer, pfiltermem, cfltmem, count);
break;
case FILTERTYPE_CUBIC:
S_Interpolate2xCubic(pbuffer, pfiltermem, cfltmem, count);
break;
}
}
//===============================================================================
// PAINTBUFFER ROUTINES
//===============================================================================
// Set current paintbuffer to pbuf.
// The set paintbuffer is used by all subsequent mixing, upsampling and dsp routines.
// Also sets the rear paintbuffer if paintbuffer has fsurround true.
// (otherwise, rearpaintbuffer is NULL)
void MIX_SetCurrentPaintbuffer(int ipaintbuffer)
{
// set front and rear paintbuffer
Assert(ipaintbuffer < g_paintBuffers.Count());
g_curpaintbuffer = g_paintBuffers[ipaintbuffer].pbuf;
if ( g_paintBuffers[ipaintbuffer].fsurround )
{
g_currearpaintbuffer = g_paintBuffers[ipaintbuffer].pbufrear;
g_curcenterpaintbuffer = NULL;
if ( g_paintBuffers[ipaintbuffer].fsurround_center )
g_curcenterpaintbuffer = g_paintBuffers[ipaintbuffer].pbufcenter;
}
else
{
g_currearpaintbuffer = NULL;
g_curcenterpaintbuffer = NULL;
}
Assert(g_curpaintbuffer != NULL);
}
// return index to current paintbuffer
int MIX_GetCurrentPaintbufferIndex( void )
{
int i;
for ( i = 0; i < g_paintBuffers.Count(); i++ )
{
if (g_curpaintbuffer == g_paintBuffers[i].pbuf)
return i;
}
return 0;
}
// return pointer to current paintbuffer struct
paintbuffer_t *MIX_GetCurrentPaintbufferPtr( void )
{
int ipaint = MIX_GetCurrentPaintbufferIndex();
Assert( ipaint < g_paintBuffers.Count() );
return &g_paintBuffers[ipaint];
}
// return pointer to front paintbuffer pbuf, given index
inline portable_samplepair_t *MIX_GetPFrontFromIPaint(int ipaintbuffer)
{
return g_paintBuffers[ipaintbuffer].pbuf;
}
paintbuffer_t *MIX_GetPPaintFromIPaint( int ipaintbuffer )
{
Assert( ipaintbuffer < g_paintBuffers.Count() );
return &g_paintBuffers[ipaintbuffer];
}
// return pointer to rear buffer, given index.
// returns null if fsurround is false;
inline portable_samplepair_t *MIX_GetPRearFromIPaint(int ipaintbuffer)
{
if ( g_paintBuffers[ipaintbuffer].fsurround )
return g_paintBuffers[ipaintbuffer].pbufrear;
return NULL;
}
// return pointer to center buffer, given index.
// returns null if fsurround_center is false;
inline portable_samplepair_t *MIX_GetPCenterFromIPaint(int ipaintbuffer)
{
if ( g_paintBuffers[ipaintbuffer].fsurround_center )
return g_paintBuffers[ipaintbuffer].pbufcenter;
return NULL;
}
// return index to paintbuffer, given buffer pointer
inline int MIX_GetIPaintFromPFront( portable_samplepair_t *pbuf )
{
int i;
for ( i = 0; i < g_paintBuffers.Count(); i++ )
{
if ( pbuf == g_paintBuffers[i].pbuf )
return i;
}
return 0;
}
// return pointer to paintbuffer struct, given ptr to buffer data
inline paintbuffer_t *MIX_GetPPaintFromPFront( portable_samplepair_t *pbuf )
{
int i;
i = MIX_GetIPaintFromPFront( pbuf );
return &g_paintBuffers[i];
}
// up convert mono buffer to full surround
inline void MIX_ConvertBufferToSurround( int ipaintbuffer )
{
paintbuffer_t *ppaint = &g_paintBuffers[ipaintbuffer];
// duplicate channel data as needed
if ( g_AudioDevice->IsSurround() )
{
// set buffer flags
ppaint->fsurround = g_AudioDevice->IsSurround();
ppaint->fsurround_center = g_AudioDevice->IsSurroundCenter();
portable_samplepair_t *pfront = MIX_GetPFrontFromIPaint( ipaintbuffer );
portable_samplepair_t *prear = MIX_GetPRearFromIPaint( ipaintbuffer );
portable_samplepair_t *pcenter = MIX_GetPCenterFromIPaint( ipaintbuffer );
// copy front to rear
Q_memcpy(prear, pfront, sizeof(portable_samplepair_t) * PAINTBUFFER_SIZE);
// copy front to center
if ( g_AudioDevice->IsSurroundCenter() )
Q_memcpy(pcenter, pfront, sizeof(portable_samplepair_t) * PAINTBUFFER_SIZE);
}
}
// Activate a paintbuffer. All active paintbuffers are mixed in parallel within
// MIX_MixChannelsToPaintbuffer, according to flags
inline void MIX_ActivatePaintbuffer(int ipaintbuffer)
{
Assert( ipaintbuffer < g_paintBuffers.Count() );
g_paintBuffers[ipaintbuffer].factive = true;
}
// Don't mix into this paintbuffer
inline void MIX_DeactivatePaintbuffer(int ipaintbuffer)
{
Assert( ipaintbuffer < g_paintBuffers.Count() );
g_paintBuffers[ipaintbuffer].factive = false;
}
// Don't mix into any paintbuffers
inline void MIX_DeactivateAllPaintbuffers(void)
{
int i;
for ( i = 0; i < g_paintBuffers.Count(); i++ )
g_paintBuffers[i].factive = false;
}
// set upsampling filter indexes back to 0
inline void MIX_ResetPaintbufferFilterCounters( void )
{
int i;
for ( i = 0; i < g_paintBuffers.Count(); i++ )
g_paintBuffers[i].ifilter = 0;
}
inline void MIX_ResetPaintbufferFilterCounter( int ipaintbuffer )
{
Assert ( ipaintbuffer < g_paintBuffers.Count() );
g_paintBuffers[ipaintbuffer].ifilter = 0;
}
// Change paintbuffer's flags
inline void MIX_SetPaintbufferFlags(int ipaintbuffer, int flags)
{
Assert( ipaintbuffer < g_paintBuffers.Count() );
g_paintBuffers[ipaintbuffer].flags = flags;
}
// zero out all paintbuffers
void MIX_ClearAllPaintBuffers( int SampleCount, bool clearFilters )
{
// g_paintBuffers can be NULL with -nosound
if ( g_paintBuffers.Count() <= 0 )
{
return;
}
int i;
int count = min(SampleCount, PAINTBUFFER_SIZE);
// zero out all paintbuffer data (ignore sampleCount)
for ( i = 0; i < g_paintBuffers.Count(); i++ )
{
if (g_paintBuffers[i].pbuf != NULL)
Q_memset(g_paintBuffers[i].pbuf, 0, (count+1) * sizeof(portable_samplepair_t));
if (g_paintBuffers[i].pbufrear != NULL)
Q_memset(g_paintBuffers[i].pbufrear, 0, (count+1) * sizeof(portable_samplepair_t));
if (g_paintBuffers[i].pbufcenter != NULL)
Q_memset(g_paintBuffers[i].pbufcenter, 0, (count+1) * sizeof(portable_samplepair_t));
if ( clearFilters )
{
Q_memset( g_paintBuffers[i].fltmem, 0, sizeof(g_paintBuffers[i].fltmem) );
Q_memset( g_paintBuffers[i].fltmemrear, 0, sizeof(g_paintBuffers[i].fltmemrear) );
Q_memset( g_paintBuffers[i].fltmemcenter, 0, sizeof(g_paintBuffers[i].fltmemcenter) );
}
}
if ( clearFilters )
{
MIX_ResetPaintbufferFilterCounters();
}
}
#define SWAP(a,b,t) {(t) = (a); (a) = (b); (b) = (t);}
#define AVG(a,b) (((a) + (b)) >> 1 )
#define AVG4(a,b,c,d) (((a) + (b) + (c) + (d)) >> 2 )
// Synthesize center channel from left/right values (average).
// Currently just averages, but could actually remove
// the center signal from the l/r channels...
inline void MIX_CenterFromLeftRight( int *pl, int *pr, int *pc )
{
int l = *pl;
int r = *pr;
int c = 0;
c = (l + r) / 2;
/*
l = l - c/2;
r = r - c/2;
if (l < 0)
{
l = 0;
r += (-l);
c += (-l);
}
else if (r < 0)
{
r = 0;
l += (-r);
c += (-r);
}
*/
*pc = c;
// *pl = l;
// *pr = r;
}
// mixes pbuf1 + pbuf2 into pbuf3, count samples
// fgain is output gain 0-1.0
// NOTE: pbuf3 may equal pbuf1 or pbuf2!
// mixing algorithms:
// destination 2ch:
// pb1 2ch + pb2 2ch -> pb3 2ch
// pb1 (4ch->2ch) + pb2 2ch -> pb3 2ch
// pb1 2ch + pb2 (4ch->2ch) -> pb3 2ch
// pb1 (4ch->2ch) + pb2 (4ch->2ch) -> pb3 2ch
// destination 4ch:
// pb1 4ch + pb2 4ch -> pb3 4ch
// pb1 (2ch->4ch) + pb2 4ch -> pb3 4ch
// pb1 4ch + pb2 (2ch->4ch) -> pb3 4ch
// pb1 (2ch->4ch) + pb2 (2ch->4ch) -> pb3 4ch
// if all buffers are 4 or 5 ch surround, mix rear & center channels into ibuf3 as well.
// NOTE: for performance, conversion and mixing are done in a single pass instead of
// a two pass channel convert + mix scheme.
void MIX_MixPaintbuffers(int ibuf1, int ibuf2, int ibuf3, int count, float fgain_out)
{
VPROF("Mixpaintbuffers");
int i;
portable_samplepair_t *pbuf1, *pbuf2, *pbuf3, *pbuft;
portable_samplepair_t *pbufrear1, *pbufrear2, *pbufrear3, *pbufreart;
portable_samplepair_t *pbufcenter1, *pbufcenter2, *pbufcenter3, *pbufcentert;
int cchan1, cchan2, cchan3, cchant;
int xl,xr;
int l,r,l2,r2,c, c2;
int gain_out;
gain_out = 256 * fgain_out;
Assert (count <= PAINTBUFFER_SIZE);
Assert (ibuf1 < g_paintBuffers.Count());
Assert (ibuf2 < g_paintBuffers.Count());
Assert (ibuf3 < g_paintBuffers.Count());
pbuf1 = g_paintBuffers[ibuf1].pbuf;
pbuf2 = g_paintBuffers[ibuf2].pbuf;
pbuf3 = g_paintBuffers[ibuf3].pbuf;
pbufrear1 = g_paintBuffers[ibuf1].pbufrear;
pbufrear2 = g_paintBuffers[ibuf2].pbufrear;
pbufrear3 = g_paintBuffers[ibuf3].pbufrear;
pbufcenter1 = g_paintBuffers[ibuf1].pbufcenter;
pbufcenter2 = g_paintBuffers[ibuf2].pbufcenter;
pbufcenter3 = g_paintBuffers[ibuf3].pbufcenter;
cchan1 = 2 + (g_paintBuffers[ibuf1].fsurround ? 2 : 0) + (g_paintBuffers[ibuf1].fsurround_center ? 1 : 0);
cchan2 = 2 + (g_paintBuffers[ibuf2].fsurround ? 2 : 0) + (g_paintBuffers[ibuf2].fsurround_center ? 1 : 0);
cchan3 = 2 + (g_paintBuffers[ibuf3].fsurround ? 2 : 0) + (g_paintBuffers[ibuf3].fsurround_center ? 1 : 0);
// make sure pbuf1 always has fewer or equal channels than pbuf2
// NOTE: pbuf3 may equal pbuf1 or pbuf2!
if ( cchan2 < cchan1 )
{
SWAP( cchan1, cchan2, cchant );
SWAP( pbuf1, pbuf2, pbuft );
SWAP( pbufrear1, pbufrear2, pbufreart );
SWAP( pbufcenter1, pbufcenter2, pbufcentert);
}
// UNDONE: implement fast mixing routines for each of the following sections
// destination buffer stereo - average n chans down to stereo
if ( cchan3 == 2 )
{
// destination 2ch:
// pb1 2ch + pb2 2ch -> pb3 2ch
// pb1 2ch + pb2 (4ch->2ch) -> pb3 2ch
// pb1 (4ch->2ch) + pb2 (4ch->2ch) -> pb3 2ch
if ( cchan1 == 2 && cchan2 == 2 )
{
// mix front channels
for (i = 0; i < count; i++)
{
pbuf3[i].left = pbuf1[i].left + pbuf2[i].left;
pbuf3[i].right = pbuf1[i].right + pbuf2[i].right;
}
goto gain2ch;
}
if ( cchan1 == 2 && cchan2 == 4 )
{
// avg rear chan l/r
for (i = 0; i < count; i++)
{
pbuf3[i].left = pbuf1[i].left + AVG( pbuf2[i].left, pbufrear2[i].left );
pbuf3[i].right = pbuf1[i].right + AVG( pbuf2[i].right, pbufrear2[i].right );
}
goto gain2ch;
}
if ( cchan1 == 4 && cchan2 == 4 )
{
// avg rear chan l/r
for (i = 0; i < count; i++)
{
pbuf3[i].left = AVG( pbuf1[i].left, pbufrear1[i].left) + AVG( pbuf2[i].left, pbufrear2[i].left );
pbuf3[i].right = AVG( pbuf1[i].right, pbufrear1[i].right) + AVG( pbuf2[i].right, pbufrear2[i].right );
}
goto gain2ch;
}
if ( cchan1 == 2 && cchan2 == 5 )
{
// avg rear chan l/r + center split into left/right
for (i = 0; i < count; i++)
{
l = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
pbuf3[i].left = pbuf1[i].left + AVG( l, pbufrear2[i].left );
pbuf3[i].right = pbuf1[i].right + AVG( r, pbufrear2[i].right );
}
goto gain2ch;
}
if ( cchan1 == 4 && cchan2 == 5)
{
for (i = 0; i < count; i++)
{
l = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
pbuf3[i].left = AVG( pbuf1[i].left, pbufrear1[i].left) + AVG( l, pbufrear2[i].left );
pbuf3[i].right = AVG( pbuf1[i].right, pbufrear1[i].right) + AVG( r, pbufrear2[i].right );
}
goto gain2ch;
}
if ( cchan1 == 5 && cchan2 == 5)
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left + ((pbufcenter1[i].left) >> 1);
r = pbuf1[i].right + ((pbufcenter1[i].left) >> 1);
l2 = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r2 = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
pbuf3[i].left = AVG( l, pbufrear1[i].left) + AVG( l2, pbufrear2[i].left );
pbuf3[i].right = AVG( r, pbufrear1[i].right) + AVG( r2, pbufrear2[i].right );
} goto gain2ch;
}
}
// destination buffer quad - duplicate n chans up to quad
if ( cchan3 == 4 )
{
// pb1 4ch + pb2 4ch -> pb3 4ch
// pb1 (2ch->4ch) + pb2 4ch -> pb3 4ch
// pb1 (2ch->4ch) + pb2 (2ch->4ch) -> pb3 4ch
if ( cchan1 == 4 && cchan2 == 4)
{
// mix front -> front, rear -> rear
for (i = 0; i < count; i++)
{
pbuf3[i].left = pbuf1[i].left + pbuf2[i].left;
pbuf3[i].right = pbuf1[i].right + pbuf2[i].right;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
}
goto gain4ch;
}
if ( cchan1 == 2 && cchan2 == 4)
{
for (i = 0; i < count; i++)
{
// split 2 ch left -> front left, rear left
// split 2 ch right -> front right, rear right
xl = pbuf1[i].left;
xr = pbuf1[i].right;
pbuf3[i].left = xl + pbuf2[i].left;
pbuf3[i].right = xr + pbuf2[i].right;
pbufrear3[i].left = xl + pbufrear2[i].left;
pbufrear3[i].right = xr + pbufrear2[i].right;
}
goto gain4ch;
}
if ( cchan1 == 2 && cchan2 == 2)
{
// mix l,r, split into front l, front r
for (i = 0; i < count; i++)
{
xl = pbuf1[i].left + pbuf2[i].left;
xr = pbuf1[i].right + pbuf2[i].right;
pbufrear3[i].left = pbuf3[i].left = xl;
pbufrear3[i].right = pbuf3[i].right = xr;
}
goto gain4ch;
}
if ( cchan1 == 2 && cchan2 == 5 )
{
for (i = 0; i < count; i++)
{
// split center of chan2 into left/right
l2 = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r2 = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
xl = pbuf1[i].left;
xr = pbuf1[i].right;
pbuf3[i].left = xl + l2;
pbuf3[i].right = xr + r2;
pbufrear3[i].left = xl + pbufrear2[i].left;
pbufrear3[i].right = xr + pbufrear2[i].right;
}
goto gain4ch;
}
if ( cchan1 == 4 && cchan2 == 5)
{
for (i = 0; i < count; i++)
{
l2 = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r2 = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
pbuf3[i].left = pbuf1[i].left + l2;
pbuf3[i].right = pbuf1[i].right + r2;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
}
goto gain4ch;
}
if ( cchan1 == 5 && cchan2 == 5 )
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left + ((pbufcenter1[i].left) >> 1);
r = pbuf1[i].right + ((pbufcenter1[i].left) >> 1);
l2 = pbuf2[i].left + ((pbufcenter2[i].left) >> 1);
r2 = pbuf2[i].right + ((pbufcenter2[i].left) >> 1);
pbuf3[i].left = l + l2;
pbuf3[i].right = r + r2;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
}
goto gain4ch;
}
}
// 5 channel destination
if (cchan3 == 5)
{
// up convert from 2 or 4 ch buffer to 5 ch buffer:
// center channel is synthesized from front left, front right
if (cchan1 == 2 && cchan2 == 2)
{
for (i = 0; i < count; i++)
{
// split 2 ch left -> front left, center, rear left
// split 2 ch right -> front right, center, rear right
l = pbuf1[i].left;
r = pbuf1[i].right;
MIX_CenterFromLeftRight(&l, &r, &c);
l2 = pbuf2[i].left;
r2 = pbuf2[i].right;
MIX_CenterFromLeftRight(&l2, &r2, &c2);
pbuf3[i].left = l + l2;
pbuf3[i].right = r + r2;
pbufrear3[i].left = pbuf1[i].left + pbuf2[i].left;
pbufrear3[i].right = pbuf1[i].right + pbuf2[i].right;
pbufcenter3[i].left = c + c2;
}
goto gain5ch;
}
if (cchan1 == 2 && cchan2 == 4)
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left;
r = pbuf1[i].right;
MIX_CenterFromLeftRight(&l, &r, &c);
l2 = pbuf2[i].left;
r2 = pbuf2[i].right;
MIX_CenterFromLeftRight(&l2, &r2, &c2);
pbuf3[i].left = l + l2;
pbuf3[i].right = r + r2;
pbufrear3[i].left = pbuf1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbuf1[i].right + pbufrear2[i].right;
pbufcenter3[i].left = c + c2;
}
goto gain5ch;
}
if (cchan1 == 2 && cchan2 == 5)
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left;
r = pbuf1[i].right;
MIX_CenterFromLeftRight(&l, &r, &c);
pbuf3[i].left = l + pbuf2[i].left;
pbuf3[i].right = r + pbuf2[i].right;
pbufrear3[i].left = pbuf1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbuf1[i].right + pbufrear2[i].right;
pbufcenter3[i].left = c + pbufcenter2[i].left;
}
goto gain5ch;
}
if (cchan1 == 4 && cchan2 == 4)
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left;
r = pbuf1[i].right;
MIX_CenterFromLeftRight(&l, &r, &c);
l2 = pbuf2[i].left;
r2 = pbuf2[i].right;
MIX_CenterFromLeftRight(&l2, &r2, &c2);
pbuf3[i].left = l + l2;
pbuf3[i].right = r + r2;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
pbufcenter3[i].left = c + c2;
}
goto gain5ch;
}
if (cchan1 == 4 && cchan2 == 5)
{
for (i = 0; i < count; i++)
{
l = pbuf1[i].left;
r = pbuf1[i].right;
MIX_CenterFromLeftRight(&l, &r, &c);
pbuf3[i].left = l + pbuf2[i].left;
pbuf3[i].right = r + pbuf2[i].right;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
pbufcenter3[i].left = c + pbufcenter2[i].left;
}
goto gain5ch;
}
if ( cchan2 == 5 && cchan1 == 5 )
{
for (i = 0; i < count; i++)
{
pbuf3[i].left = pbuf1[i].left + pbuf2[i].left;
pbuf3[i].right = pbuf1[i].right + pbuf2[i].right;
pbufrear3[i].left = pbufrear1[i].left + pbufrear2[i].left;
pbufrear3[i].right = pbufrear1[i].right + pbufrear2[i].right;
pbufcenter3[i].left = pbufcenter1[i].left + pbufcenter2[i].left;
}
goto gain5ch;
}
}
gain2ch:
if ( gain_out == 256) // KDB: perf
return;
for (i = 0; i < count; i++)
{
pbuf3[i].left = (pbuf3[i].left * gain_out) >> 8;
pbuf3[i].right = (pbuf3[i].right * gain_out) >> 8;
}
return;
gain4ch:
if ( gain_out == 256) // KDB: perf
return;
for (i = 0; i < count; i++)
{
pbuf3[i].left = (pbuf3[i].left * gain_out) >> 8;
pbuf3[i].right = (pbuf3[i].right * gain_out) >> 8;
pbufrear3[i].left = (pbufrear3[i].left * gain_out) >> 8;
pbufrear3[i].right = (pbufrear3[i].right * gain_out) >> 8;
}
return;
gain5ch:
if ( gain_out == 256) // KDB: perf
return;
for (i = 0; i < count; i++)
{
pbuf3[i].left = (pbuf3[i].left * gain_out) >> 8;
pbuf3[i].right = (pbuf3[i].right * gain_out) >> 8;
pbufrear3[i].left = (pbufrear3[i].left * gain_out) >> 8;
pbufrear3[i].right = (pbufrear3[i].right * gain_out) >> 8;
pbufcenter3[i].left = (pbufcenter3[i].left * gain_out) >> 8;
}
return;
}
// multiply all values in paintbuffer by fgain
void MIX_ScalePaintBuffer( int bufferIndex, int count, float fgain )
{
portable_samplepair_t *pbuf = g_paintBuffers[bufferIndex].pbuf;
portable_samplepair_t *pbufrear = g_paintBuffers[bufferIndex].pbufrear;
portable_samplepair_t *pbufcenter = g_paintBuffers[bufferIndex].pbufcenter;
int gain = 256 * fgain;
int i;
if (gain == 256)
return;
if ( !g_paintBuffers[bufferIndex].fsurround )
{
for (i = 0; i < count; i++)
{
pbuf[i].left = (pbuf[i].left * gain) >> 8;
pbuf[i].right = (pbuf[i].right * gain) >> 8;
}
}
else
{
for (i = 0; i < count; i++)
{
pbuf[i].left = (pbuf[i].left * gain) >> 8;
pbuf[i].right = (pbuf[i].right * gain) >> 8;
pbufrear[i].left = (pbufrear[i].left * gain) >> 8;
pbufrear[i].right = (pbufrear[i].right * gain) >> 8;
}
if (g_paintBuffers[bufferIndex].fsurround_center)
{
for (i = 0; i < count; i++)
{
pbufcenter[i].left = (pbufcenter[i].left * gain) >> 8;
// pbufcenter[i].right = (pbufcenter[i].right * gain) >> 8; mono center channel
}
}
}
}
// DEBUG peak detection values
#define _SDEBUG 1
#ifdef _SDEBUG
float sdebug_avg_in = 0.0;
float sdebug_in_count = 0.0;
float sdebug_avg_out = 0.0;
float sdebug_out_count = 0.0;
#define SDEBUG_TOTAL_COUNT (3*44100)
#endif // DEBUG
// DEBUG code - get and show peak value of specified paintbuffer
// DEBUG code - ibuf is buffer index, count is # samples to test, pppeakprev stores peak
void SDEBUG_GetAvgValue( int ibuf, int count, float *pav )
{
#ifdef _SDEBUG
if (snd_showstart.GetInt() != 4 )
return;
float av = 0.0;
for (int i = 0; i < count; i++)
av += (float)(abs(g_paintBuffers[ibuf].pbuf->left) + abs(g_paintBuffers[ibuf].pbuf->right))/2.0;
*pav = av / count;
#endif // DEBUG
}
void SDEBUG_GetAvgIn( int ibuf, int count)
{
float av = 0.0;
SDEBUG_GetAvgValue( ibuf, count, &av );
sdebug_avg_in = ((av * count ) + (sdebug_avg_in * sdebug_in_count)) / (count + sdebug_in_count);
sdebug_in_count += count;
}
void SDEBUG_GetAvgOut( int ibuf, int count)
{
float av = 0.0;
SDEBUG_GetAvgValue( ibuf, count, &av );
sdebug_avg_out = ((av * count ) + (sdebug_avg_out * sdebug_out_count)) / (count + sdebug_out_count);
sdebug_out_count += count;
}
void SDEBUG_ShowAvgValue()
{
#ifdef _SDEBUG
if (sdebug_in_count > SDEBUG_TOTAL_COUNT)
{
if ((int)sdebug_avg_in > 20.0 && (int)sdebug_avg_out > 20.0)
DevMsg("dsp avg gain:%1.2f in:%1.2f out:%1.2f 1/gain:%1.2f\n", sdebug_avg_out/sdebug_avg_in, sdebug_avg_in, sdebug_avg_out, sdebug_avg_in/sdebug_avg_out);
sdebug_avg_in = 0.0;
sdebug_avg_out = 0.0;
sdebug_in_count = 0.0;
sdebug_out_count = 0.0;
}
#endif // DEBUG
}
// clip all values in paintbuffer to 16bit.
// if fsurround is set for paintbuffer, also process rear buffer samples
void MIX_CompressPaintbuffer(int ipaint, int count)
{
VPROF("CompressPaintbuffer");
int i;
paintbuffer_t *ppaint = MIX_GetPPaintFromIPaint(ipaint);
portable_samplepair_t *pbf;
portable_samplepair_t *pbr;
portable_samplepair_t *pbc;
pbf = ppaint->pbuf;
pbr = ppaint->pbufrear;
pbc = ppaint->pbufcenter;
for (i = 0; i < count; i++)
{
pbf->left = CLIP(pbf->left);
pbf->right = CLIP(pbf->right);
pbf++;
}
if ( ppaint->fsurround )
{
Assert (pbr);
for (i = 0; i < count; i++)
{
pbr->left = CLIP(pbr->left);
pbr->right = CLIP(pbr->right);
pbr++;
}
}
if ( ppaint->fsurround_center )
{
Assert (pbc);
for (i = 0; i < count; i++)
{
pbc->left = CLIP(pbc->left);
//pbc->right = CLIP(pbc->right); mono center channel
pbc++;
}
}
}
// mix and upsample channels to 44khz 'ipaintbuffer'
// mix channels matching 'flags' (SOUND_MIX_DRY, SOUND_MIX_WET, SOUND_MIX_SPEAKER) into specified paintbuffer
// upsamples 11khz, 22khz channels to 44khz.
// NOTE: only call this on channels that will be mixed into only 1 paintbuffer
// and that will not be mixed until the next mix pass! otherwise, MIX_MixChannelsToPaintbuffer
// will advance any internal pointers on mixed channels; subsequent calls will be at
// incorrect offset.
void MIX_MixUpsampleBuffer( CChannelList &list, int ipaintbuffer, int end, int count, int flags )
{
VPROF("MixUpsampleBuffer");
int ipaintcur = MIX_GetCurrentPaintbufferIndex(); // save current paintbuffer
// reset paintbuffer upsampling filter index
MIX_ResetPaintbufferFilterCounter( ipaintbuffer );
// prevent other paintbuffers from being mixed
MIX_DeactivateAllPaintbuffers();
MIX_ActivatePaintbuffer( ipaintbuffer ); // operates on MIX_MixChannelsToPaintbuffer
MIX_SetCurrentPaintbuffer( ipaintbuffer ); // operates on MixUpSample
// mix 11khz channels to buffer
if ( list.m_has11kChannels )
{
MIX_MixChannelsToPaintbuffer( list, end, flags, SOUND_11k, SOUND_11k );
// upsample 11khz buffer by 2x
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_11k), FILTERTYPE_LINEAR );
}
if ( list.m_has22kChannels || list.m_has11kChannels )
{
// mix 22khz channels to buffer
MIX_MixChannelsToPaintbuffer( list, end, flags, SOUND_22k, SOUND_22k );
#if (SOUND_DMA_SPEED > SOUND_22k)
// upsample 22khz buffer by 2x
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_22k), FILTERTYPE_LINEAR );
#endif
}
// mix 44khz channels to buffer
MIX_MixChannelsToPaintbuffer( list, end, flags, SOUND_44k, SOUND_DMA_SPEED);
MIX_DeactivateAllPaintbuffers();
// restore previous paintbuffer
MIX_SetCurrentPaintbuffer( ipaintcur );
}
// upsample and mix sounds into final 44khz versions of the following paintbuffers:
// SOUND_BUFFER_ROOM, SOUND_BUFFER_FACING, IFACINGAWAY, SOUND_BUFFER_DRY, SOUND_BUFFER_SPEAKER, SOUND_BUFFER_SPECIALs
// dsp fx are then applied to these buffers by the caller.
// caller also remixes all into final SOUND_BUFFER_PAINT output.
void MIX_UpsampleAllPaintbuffers( CChannelList &list, int end, int count )
{
VPROF( "MixUpsampleAll" );
// 'dry' and 'speaker' channel sounds mix 100% into their corresponding buffers
// mix and upsample all 'dry' sounds (channels) to 44khz SOUND_BUFFER_DRY paintbuffer
if ( list.m_hasDryChannels )
MIX_MixUpsampleBuffer( list, SOUND_BUFFER_DRY, end, count, SOUND_MIX_DRY );
// mix and upsample all 'speaker' sounds (channels) to 44khz SOUND_BUFFER_SPEAKER paintbuffer
if ( list.m_hasSpeakerChannels )
MIX_MixUpsampleBuffer( list, SOUND_BUFFER_SPEAKER, end, count, SOUND_MIX_SPEAKER );
// mix and upsample all 'special dsp' sounds (channels) to 44khz SOUND_BUFFER_SPECIALs paintbuffer
for ( int iDSP = 0; iDSP < list.m_nSpecialDSPs.Count(); ++iDSP )
{
for ( int i = SOUND_BUFFER_SPECIAL_START; i < g_paintBuffers.Count(); ++i )
{
paintbuffer_t *pSpecialBuffer = MIX_GetPPaintFromIPaint( i );
if ( pSpecialBuffer->nSpecialDSP == list.m_nSpecialDSPs[ iDSP ] && pSpecialBuffer->idsp_specialdsp != -1 )
{
MIX_MixUpsampleBuffer( list, i, end, count, SOUND_MIX_SPECIAL_DSP );
break;
}
}
}
// 'room', 'facing' 'facingaway' sounds are mixed into up to 3 buffers:
// 11khz sounds are mixed into 3 buffers based on distance from listener, and facing direction
// These buffers are room, facing, facingaway
// These 3 mixed buffers are then each upsampled to 22khz.
// 22khz sounds are mixed into the 3 buffers based on distance from listener, and facing direction
// These 3 mixed buffers are then each upsampled to 44khz.
// 44khz sounds are mixed into the 3 buffers based on distance from listener, and facing direction
MIX_DeactivateAllPaintbuffers();
// set paintbuffer upsample filter indices to 0
MIX_ResetPaintbufferFilterCounters();
if ( !g_bDspOff )
{
// only mix to roombuffer if dsp fx are on KDB: perf
MIX_ActivatePaintbuffer(SOUND_BUFFER_ROOM); // operates on MIX_MixChannelsToPaintbuffer
}
MIX_ActivatePaintbuffer(SOUND_BUFFER_FACING);
if ( g_bdirectionalfx )
{
// mix to facing away buffer only if directional presets are set
MIX_ActivatePaintbuffer(SOUND_BUFFER_FACINGAWAY);
}
// mix 11khz sounds:
// pan sounds between 3 busses: facing, facingaway and room buffers
MIX_MixChannelsToPaintbuffer( list, end, SOUND_MIX_WET, SOUND_11k, SOUND_11k);
// upsample all 11khz buffers by 2x
if ( !g_bDspOff )
{
// only upsample roombuffer if dsp fx are on KDB: perf
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_ROOM); // operates on MixUpSample
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_11k), FILTERTYPE_LINEAR );
}
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_FACING);
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_11k), FILTERTYPE_LINEAR );
if ( g_bdirectionalfx )
{
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_FACINGAWAY);
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_11k), FILTERTYPE_LINEAR );
}
// mix 22khz sounds:
// pan sounds between 3 busses: facing, facingaway and room buffers
MIX_MixChannelsToPaintbuffer( list, end, SOUND_MIX_WET, SOUND_22k, SOUND_22k);
// upsample all 22khz buffers by 2x
#if ( SOUND_DMA_SPEED > SOUND_22k )
if ( !g_bDspOff )
{
// only upsample roombuffer if dsp fx are on KDB: perf
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_ROOM);
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_22k), FILTERTYPE_LINEAR );
}
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_FACING);
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_22k), FILTERTYPE_LINEAR );
if ( g_bdirectionalfx )
{
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_FACINGAWAY);
g_AudioDevice->MixUpsample( count / (SOUND_DMA_SPEED / SOUND_22k), FILTERTYPE_LINEAR );
}
#endif
// mix all 44khz sounds to all active paintbuffers
MIX_MixChannelsToPaintbuffer( list, end, SOUND_MIX_WET, SOUND_44k, SOUND_DMA_SPEED);
MIX_DeactivateAllPaintbuffers();
MIX_SetCurrentPaintbuffer(SOUND_BUFFER_PAINT);
}
ConVar snd_cull_duplicates("snd_cull_duplicates","0",FCVAR_ALLOWED_IN_COMPETITIVE,"If nonzero, aggressively cull duplicate sounds during mixing. The number specifies the number of duplicates allowed to be played.");
// Helper class for determining whether a given channel number should be culled from
// mixing, if snd_cull_duplicates is enabled (psychoacoustic quashing).
class CChannelCullList
{
public:
// default constructor
CChannelCullList() : m_numChans(0) {};
// call if you plan on culling channels - and not otherwise, it's a little expensive
// (that's why it's not in the constructor)
void Initialize( CChannelList &list );
// returns true if a given channel number has been marked for culling
inline bool ShouldCull( int channelNum )
{
return (m_numChans > channelNum) ? m_bShouldCull[channelNum] : false;
}
// an array of sound names and their volumes
// TODO: there may be a way to do this faster on 360 (eg, pad to 128bit, use SIMD)
struct sChannelVolData
{
int m_channelNum;
int m_vol; // max volume of sound. -1 means "do not cull, ever, do not even do the math"
unsigned int m_nameHash; // a unique id for a sound file
};
protected:
sChannelVolData m_channelInfo[MAX_CHANNELS];
bool m_bShouldCull[MAX_CHANNELS]; // in ChannelList order, not sorted order
int m_numChans;
};
// comparator for qsort as used below (eg a lambda)
// returns < 0 if a should come before b, > 0 if a should come after, 0 otherwise
static int __cdecl ChannelVolComparator ( const void * a, const void * b )
{
// greater numbers come first.
return static_cast<const CChannelCullList::sChannelVolData *>(b)->m_vol - static_cast<const CChannelCullList::sChannelVolData *>(a)->m_vol;
}
void CChannelCullList::Initialize( CChannelList &list )
{
VPROF("CChannelCullList::Initialize");
// First, build a sorted list of channels by decreasing volume, and by a hash of their wavname.
m_numChans = list.Count();
for ( int i = m_numChans - 1 ; i >= 0 ; --i )
{
channel_t *ch = list.GetChannel(i);
m_channelInfo[i].m_channelNum = i;
if ( ch && ch->pMixer->IsReadyToMix() )
{
m_channelInfo[i].m_vol = ChannelLoudestCurVolume(ch);
AssertMsg(m_channelInfo[i].m_vol >= 0, "Sound channel has a negative volume?");
m_channelInfo[i].m_nameHash = (unsigned int) ch->sfx;
}
else
{
m_channelInfo[i].m_vol = -1;
m_channelInfo[i].m_nameHash = NULL; // doesn't matter
}
}
// set the unused channels to invalid data
for ( int i = m_numChans ; i < MAX_CHANNELS ; ++i )
{
m_channelInfo[i].m_channelNum = -1;
m_channelInfo[i].m_vol = -1;
}
// Sort the list.
qsort( m_channelInfo, MAX_CHANNELS, sizeof(sChannelVolData), ChannelVolComparator );
// Then, determine if the given sound is less than the nth loudest of its hash. If so, mark its flag
// for removal.
// TODO: use an actual algorithm rather than this bogus quadratic technique.
// (I'm using it for now because we don't have convenient/fast hash table
// classes, which would be the linear-time way to deal with this).
const int cutoff = snd_cull_duplicates.GetInt();
for ( int i = 0 ; i < m_numChans ; ++i ) // i is index in original channel list
{
channel_t *ch = list.GetChannel(i);
// for each sound, determine where it ranks in loudness
int howManyLouder = 0;
for ( int j = 0 ;
m_channelInfo[j].m_channelNum != i && m_channelInfo[j].m_vol >= 0 && j < MAX_CHANNELS ;
++j )
{
// j steps through the sorted list until we find ourselves:
if (m_channelInfo[j].m_nameHash == (unsigned int)(ch->sfx))
{
// that's another channel playing this sound but louder than me
++howManyLouder;
}
}
if (howManyLouder >= cutoff)
{
// this sound should be culled
m_bShouldCull[i] = true;
}
else
{
// this sound should not be culled
m_bShouldCull[i] = false;
}
}
}
ConVar snd_mute_losefocus("snd_mute_losefocus", "1", FCVAR_ARCHIVE);
// build a list of channels that will actually do mixing in this update
// remove all active channels that won't mix for some reason
void MIX_BuildChannelList( CChannelList &list )
{
VPROF("MIX_BuildChannelList");
g_ActiveChannels.GetActiveChannels( list );
list.m_nSpecialDSPs.RemoveAll();
list.m_hasDryChannels = false;
list.m_hasSpeakerChannels = false;
list.m_has11kChannels = false;
list.m_has22kChannels = false;
list.m_has44kChannels = false;
bool delayStartServer = false;
bool delayStartClient = false;
bool bPaused = g_pSoundServices->IsGamePaused();
#ifdef POSIX
bool bActive = g_pSoundServices->IsGameActive();
bool bStopOnFocusLoss = !bActive && snd_mute_losefocus.GetBool();
#endif
CChannelCullList cullList;
if (snd_cull_duplicates.GetInt() > 0)
{
cullList.Initialize(list);
}
// int numQuashed = 0;
for ( int i = list.Count(); --i >= 0; )
{
channel_t *ch = list.GetChannel(i);
bool bRemove = false;
// Certain async loaded sounds lazily load into memory in the background, use this to determine
// if the sound is ready for mixing
CAudioSource *pSource = NULL;
if ( ch->pMixer->IsReadyToMix() )
{
pSource = S_LoadSound( ch->sfx, ch );
// Don't mix sound data for sounds with 'zero' volume. If it's a non-looping sound,
// just remove the sound when its volume goes to zero. If it's a 'dry' channel sound (ie: music)
// then assume bZeroVolume is fade in - don't restart
// To be 'zero' volume, all target volume and current volume values must all be less than 5
bool bZeroVolume = BChannelLowVolume( ch, 1 );
if ( !pSource || ( bZeroVolume && !pSource->IsLooped() && !ch->flags.bdry ) )
{
// NOTE: Since we've loaded the sound, check to see if it's a sentence. Play them at zero anyway
// to keep the character's lips moving and the captions happening.
if ( !pSource || pSource->GetSentence() == NULL )
{
S_FreeChannel( ch );
bRemove = true;
}
}
else if ( bZeroVolume )
{
bRemove = true;
}
// If the sound wants to stop when the game pauses, do so
if ( bPaused && SND_ShouldPause(ch) )
{
bRemove = true;
}
#ifdef POSIX
// If we aren't the active app and the option for background audio isn't on, mute the audio
// Windows has it's own system for background muting
if ( !bRemove && bStopOnFocusLoss )
{
bRemove = true;
// Free up the sound channels otherwise they start filling up
if ( pSource && ( !pSource->IsLooped() && !pSource->IsStreaming() ) )
{
S_FreeChannel( ch );
}
}
#endif
// On lowend, aggressively cull duplicate sounds.
if ( !bRemove && snd_cull_duplicates.GetInt() > 0 )
{
// We can't simply remove them, because then sounds will pile up waiting to finish later.
// We need to flag them for not mixing.
list.m_quashed[i] = cullList.ShouldCull(i);
/*
if (list.m_quashed[i])
{
numQuashed++;
// Msg("removed %i\n", i);
}
*/
}
else
{
list.m_quashed[i] = false;
}
}
else
{
bRemove = true;
}
if ( bRemove )
{
list.RemoveChannelFromList(i);
continue;
}
if ( ch->flags.bSpeaker )
{
list.m_hasSpeakerChannels = true;
}
if ( ch->special_dsp != 0 )
{
if ( list.m_nSpecialDSPs.Find( ch->special_dsp ) == -1 )
{
list.m_nSpecialDSPs.AddToTail( ch->special_dsp );
}
}
if ( ch->flags.bdry )
{
list.m_hasDryChannels = true;
}
int rate = pSource->SampleRate();
if ( rate == SOUND_11k )
{
list.m_has11kChannels = true;
}
else if ( rate == SOUND_22k )
{
list.m_has22kChannels = true;
}
else if ( rate == SOUND_44k )
{
list.m_has44kChannels = true;
}
if ( ch->flags.delayed_start && !SND_IsMouth(ch) )
{
if ( ch->flags.fromserver )
{
delayStartServer = true;
}
else
{
delayStartClient = true;
}
}
// get playback pitch
ch->pitch = ch->pMixer->ModifyPitch( ch->basePitch * 0.01f );
}
// DevMsg( "%d channels quashed.\n", numQuashed );
// This code will resync the delay calculation clock really often
// any time there are no scheduled waves or the game is paused
// we go ahead and reset the clock
// That way the clock is only used for short periods of time
// and we need no solution for drift
if ( bPaused || (host_frametime_unbounded > host_frametime) )
{
delayStartClient = false;
delayStartServer = false;
}
if (!delayStartServer)
{
S_SyncClockAdjust(CLOCK_SYNC_SERVER);
}
if (!delayStartClient)
{
S_SyncClockAdjust(CLOCK_SYNC_CLIENT);
}
}
// main mixing rountine - mix up to 'endtime' samples.
// All channels are mixed in a paintbuffer and then sent to
// hardware.
// A mix pass is performed, resulting in mixed sounds in SOUND_BUFFER_ROOM, SOUND_BUFFER_FACING, SOUND_BUFFER_FACINGAWAY, SOUND_BUFFER_DRY, SOUND_BUFFER_SPEAKER, SOUND_BUFFER_SPECIALs
// directional sounds are panned and mixed between SOUND_BUFFER_FACING and SOUND_BUFFER_FACINGAWAY
// omnidirectional sounds are panned 100% into SOUND_BUFFER_FACING
// sound sources far from player (ie: near back of room ) are mixed in proportion to this distance
// into SOUND_BUFFER_ROOM
// sounds with ch->bSpeaker set are mixed in mono into SOUND_BUFFER_SPEAKER
// sounds with ch->bSpecialDSP set are mixed in mono into SOUND_BUFFER_SPECIALs
// dsp_facingaway fx (2 or 4ch filtering) are then applied to the SOUND_BUFFER_FACINGAWAY
// dsp_speaker fx (1ch) are then applied to the SOUND_BUFFER_SPEAKER
// dsp_specialdsp fx (1ch) are then applied to the SOUND_BUFFER_SPECIALs
// dsp_room fx (1ch reverb) are then applied to the SOUND_BUFFER_ROOM
// All buffers are recombined into the SOUND_BUFFER_PAINT
// The dsp_water and dsp_player fx are applied in series to the SOUND_BUFFER_PAINT
// Finally, the SOUND_BUFFER_DRY buffer is mixed into the SOUND_BUFFER_PAINT
extern ConVar dsp_off;
extern ConVar snd_profile;
extern void DEBUG_StartSoundMeasure(int type, int samplecount );
extern void DEBUG_StopSoundMeasure(int type, int samplecount );
extern ConVar dsp_enhance_stereo;
extern ConVar dsp_volume;
extern ConVar dsp_vol_5ch;
extern ConVar dsp_vol_4ch;
extern ConVar dsp_vol_2ch;
extern void MXR_SetCurrentSoundMixer( const char *szsoundmixer );
extern ConVar snd_soundmixer;
void MIX_PaintChannels( int endtime, bool bIsUnderwater )
{
VPROF("MIX_PaintChannels");
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
int end;
int count;
bool b_spatial_delays = dsp_enhance_stereo.GetInt() != 0 ? true : false;
bool room_fsurround_sav;
bool room_fsurround_center_sav;
paintbuffer_t *proom = MIX_GetPPaintFromIPaint(SOUND_BUFFER_ROOM);
CheckNewDspPresets();
MXR_SetCurrentSoundMixer( snd_soundmixer.GetString() );
// dsp performance tuning
g_snd_profile_type = snd_profile.GetInt();
// dsp_off is true if no dsp processing is to run
// directional dsp processing is enabled if dsp_facingaway is non-zero
g_bDspOff = dsp_off.GetInt() ? 1 : 0;
CChannelList list;
MIX_BuildChannelList(list);
// get master dsp volume
g_dsp_volume = dsp_volume.GetFloat();
// attenuate master dsp volume by 2,4 or 5 ch settings
if ( g_AudioDevice->IsSurround() )
{
g_dsp_volume *= ( g_AudioDevice->IsSurroundCenter() ? dsp_vol_5ch.GetFloat() : dsp_vol_4ch.GetFloat() );
}
else
{
g_dsp_volume *= dsp_vol_2ch.GetFloat();
}
if ( !g_bDspOff )
{
g_bdirectionalfx = dsp_facingaway.GetInt() ? 1 : 0;
}
else
{
g_bdirectionalfx = 0;
}
// get dsp preset gain values, update gain crossfaders, used when mixing dsp processed buffers into paintbuffer
SDEBUG_ShowAvgValue();
// the cache needs to hold the audio in memory during mixing, so tell it that mixing is starting
wavedatacache->OnMixBegin();
while ( g_paintedtime < endtime )
{
VPROF("MIX_PaintChannels inner loop");
// mix a full 'paintbuffer' of sound
// clamp at paintbuffer size
end = endtime;
if (endtime - g_paintedtime > PAINTBUFFER_SIZE)
{
end = g_paintedtime + PAINTBUFFER_SIZE;
}
// number of 44khz samples to mix into paintbuffer, up to paintbuffer size
count = end - g_paintedtime;
// clear all mix buffers
g_AudioDevice->MixBegin( count );
// upsample all mix buffers.
// results in 44khz versions of:
// SOUND_BUFFER_ROOM, SOUND_BUFFER_FACING, SOUND_BUFFER_FACINGAWAY, SOUND_BUFFER_DRY, SOUND_BUFFER_SPEAKER, SOUND_BUFFER_SPECIALs
MIX_UpsampleAllPaintbuffers( list, end, count );
// apply appropriate dsp fx to each buffer, remix buffers into single quad output buffer
// apply 2 or 4ch filtering to IFACINGAWAY buffer
if ( g_bdirectionalfx )
{
g_AudioDevice->ApplyDSPEffects( idsp_facingaway, MIX_GetPFrontFromIPaint(SOUND_BUFFER_FACINGAWAY), MIX_GetPRearFromIPaint(SOUND_BUFFER_FACINGAWAY), MIX_GetPCenterFromIPaint(SOUND_BUFFER_FACINGAWAY), count );
}
if ( !g_bDspOff && list.m_hasSpeakerChannels )
{
// apply 1ch filtering to SOUND_BUFFER_SPEAKER
g_AudioDevice->ApplyDSPEffects( idsp_speaker, MIX_GetPFrontFromIPaint(SOUND_BUFFER_SPEAKER), MIX_GetPRearFromIPaint(SOUND_BUFFER_SPEAKER), MIX_GetPCenterFromIPaint(SOUND_BUFFER_SPEAKER), count );
// mix SOUND_BUFFER_SPEAKER with SOUND_BUFFER_ROOM and SOUND_BUFFER_FACING
MIX_ScalePaintBuffer( SOUND_BUFFER_SPEAKER, count, 0.7 );
MIX_MixPaintbuffers( SOUND_BUFFER_SPEAKER, SOUND_BUFFER_FACING, SOUND_BUFFER_FACING, count, 1.0 ); // +70% dry speaker
MIX_ScalePaintBuffer( SOUND_BUFFER_SPEAKER, count, 0.43 );
MIX_MixPaintbuffers( SOUND_BUFFER_SPEAKER, SOUND_BUFFER_ROOM, SOUND_BUFFER_ROOM, count, 1.0 ); // +30% wet speaker
}
if ( !g_bDspOff )
{
// apply 1ch filtering to SOUND_BUFFER_SPECIALs
for ( int iDSP = 0; iDSP < list.m_nSpecialDSPs.Count(); ++iDSP )
{
bool bFoundMixer = false;
for ( int i = SOUND_BUFFER_SPECIAL_START; i < g_paintBuffers.Count(); ++i )
{
paintbuffer_t *pSpecialBuffer = MIX_GetPPaintFromIPaint( i );
if ( pSpecialBuffer->nSpecialDSP == list.m_nSpecialDSPs[ iDSP ] && pSpecialBuffer->idsp_specialdsp != -1 )
{
g_AudioDevice->ApplyDSPEffects( pSpecialBuffer->idsp_specialdsp, MIX_GetPFrontFromIPaint( i ), MIX_GetPRearFromIPaint( i ), MIX_GetPCenterFromIPaint( i ), count );
// mix SOUND_BUFFER_SPECIALs with SOUND_BUFFER_ROOM and SOUND_BUFFER_FACING
MIX_ScalePaintBuffer( i, count, 0.7 );
MIX_MixPaintbuffers( i, SOUND_BUFFER_FACING, SOUND_BUFFER_FACING, count, 1.0 ); // +70% dry speaker
MIX_ScalePaintBuffer( i, count, 0.43 );
MIX_MixPaintbuffers( i, SOUND_BUFFER_ROOM, SOUND_BUFFER_ROOM, count, 1.0 ); // +30% wet speaker
bFoundMixer = true;
break;
}
}
// Couldn't find a mixer with the correct DSP, so make a new one!
if ( !bFoundMixer )
{
bool bSurroundCenter = g_AudioDevice->IsSurroundCenter();
bool bSurround = g_AudioDevice->IsSurround() || bSurroundCenter;
int nIndex = g_paintBuffers.AddToTail();
MIX_InitializePaintbuffer( &(g_paintBuffers[ nIndex ]), bSurround, bSurroundCenter );
g_paintBuffers[ nIndex ].flags = SOUND_BUSS_SPECIAL_DSP;
// special dsp buffer mixes to mono
g_paintBuffers[ nIndex ].fsurround = false;
g_paintBuffers[ nIndex ].fsurround_center = false;
g_paintBuffers[ nIndex ].idsp_specialdsp = -1;
g_paintBuffers[ nIndex ].nSpecialDSP = list.m_nSpecialDSPs[ iDSP ];
g_paintBuffers[ nIndex ].nPrevSpecialDSP = g_paintBuffers[ nIndex ].nSpecialDSP;
g_paintBuffers[ nIndex ].idsp_specialdsp = DSP_Alloc( g_paintBuffers[ nIndex ].nSpecialDSP, 300, 1 );
}
}
}
// apply dsp_room effects to room buffer
g_AudioDevice->ApplyDSPEffects( Get_idsp_room(), MIX_GetPFrontFromIPaint(SOUND_BUFFER_ROOM), MIX_GetPRearFromIPaint(SOUND_BUFFER_ROOM), MIX_GetPCenterFromIPaint(SOUND_BUFFER_ROOM), count );
// save room buffer surround status, in case we upconvert it
room_fsurround_sav = proom->fsurround;
room_fsurround_center_sav = proom->fsurround_center;
// apply left/center/right/lrear/rrear spatial delays to room buffer
if ( b_spatial_delays && !g_bDspOff && !DSP_RoomDSPIsOff() )
{
// upgrade mono room buffer to surround status so we can apply spatial delays to all channels
MIX_ConvertBufferToSurround( SOUND_BUFFER_ROOM );
g_AudioDevice->ApplyDSPEffects( idsp_spatial, MIX_GetPFrontFromIPaint(SOUND_BUFFER_ROOM), MIX_GetPRearFromIPaint(SOUND_BUFFER_ROOM), MIX_GetPCenterFromIPaint(SOUND_BUFFER_ROOM), count );
}
if ( g_bdirectionalfx ) // KDB: perf
{
// Recombine IFACING and IFACINGAWAY buffers into SOUND_BUFFER_PAINT
MIX_MixPaintbuffers( SOUND_BUFFER_FACING, SOUND_BUFFER_FACINGAWAY, SOUND_BUFFER_PAINT, count, DSP_NOROOM_MIX );
// Add in dsp room fx to paintbuffer, mix at 75%
MIX_MixPaintbuffers( SOUND_BUFFER_ROOM, SOUND_BUFFER_PAINT, SOUND_BUFFER_PAINT, count, DSP_ROOM_MIX );
}
else
{
// Mix IFACING buffer with SOUND_BUFFER_ROOM
// (SOUND_BUFFER_FACINGAWAY contains no data, IFACINGBBUFFER has full dry mix based on distance from listener)
// if dsp disabled, mix 100% facingbuffer, otherwise, mix 75% facingbuffer + roombuffer
float mix = g_bDspOff ? 1.0 : DSP_ROOM_MIX;
MIX_MixPaintbuffers( SOUND_BUFFER_ROOM, SOUND_BUFFER_FACING, SOUND_BUFFER_PAINT, count, mix );
}
// restore room buffer surround status, in case we upconverted it
proom->fsurround = room_fsurround_sav;
proom->fsurround_center = room_fsurround_center_sav;
// Apply underwater fx dsp_water (serial in-line)
if ( bIsUnderwater )
{
// BUG: if out of water, previous delays will be heard. must clear dly buffers.
g_AudioDevice->ApplyDSPEffects( idsp_water, MIX_GetPFrontFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPRearFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPCenterFromIPaint(SOUND_BUFFER_PAINT), count );
}
// find dsp gain
SDEBUG_GetAvgIn(SOUND_BUFFER_PAINT, count);
// Apply player fx dsp_player (serial in-line) - does nothing if dsp fx are disabled
g_AudioDevice->ApplyDSPEffects( idsp_player, MIX_GetPFrontFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPRearFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPCenterFromIPaint(SOUND_BUFFER_PAINT), count );
// display dsp gain
SDEBUG_GetAvgOut(SOUND_BUFFER_PAINT, count);
/*
// apply left/center/right/lrear/rrear spatial delays to paint buffer
if ( b_spatial_delays )
g_AudioDevice->ApplyDSPEffects( idsp_spatial, MIX_GetPFrontFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPRearFromIPaint(SOUND_BUFFER_PAINT), MIX_GetPCenterFromIPaint(SOUND_BUFFER_PAINT), count );
*/
// Add dry buffer, set output gain to water * player dsp gain (both 1.0 if not active)
MIX_MixPaintbuffers( SOUND_BUFFER_PAINT, SOUND_BUFFER_DRY, SOUND_BUFFER_PAINT, count, 1.0);
// clip all values > 16 bit down to 16 bit
// NOTE: This is required - the hardware buffer transfer routines no longer perform clipping.
MIX_CompressPaintbuffer( SOUND_BUFFER_PAINT, count );
// transfer SOUND_BUFFER_PAINT paintbuffer out to DMA buffer
MIX_SetCurrentPaintbuffer( SOUND_BUFFER_PAINT );
g_AudioDevice->TransferSamples( end );
g_paintedtime = end;
}
// the cache needs to hold the audio in memory during mixing, so tell it that mixing is complete
wavedatacache->OnMixEnd();
}
// Applies volume scaling (evenly) to all fl,fr,rl,rr volumes
// used for voice ducking and panning between various mix busses
// Ensures if mixing to speaker buffer, only speaker sounds pass through
// Called just before mixing wav data to current paintbuffer.
// a) if another player in a multiplayer game is speaking, scale all volumes down.
// b) if mixing to SOUND_BUFFER_ROOM, scale all volumes by ch.dspmix and dsp_room gain
// c) if mixing to SOUND_BUFFER_FACINGAWAY, scale all volumes by ch.dspface and dsp_facingaway gain
// d) If SURROUND_ON, but buffer is not surround, recombined front/rear volumes
// returns false if channel is to be entirely skipped.
bool MIX_ScaleChannelVolume( paintbuffer_t *ppaint, channel_t *pChannel, int volume[CCHANVOLUMES], int mixchans )
{
int i;
int mixflag = ppaint->flags;
float scale;
char wavtype = pChannel->wavtype;
float dspmix;
// copy current channel volumes into output array
ChannelCopyVolumes( pChannel, volume, 0, CCHANVOLUMES );
dspmix = pChannel->dspmix;
// if dsp is off, or room dsp is off, mix 0% to mono room buffer, 100% to facing buffer
if ( g_bDspOff || DSP_RoomDSPIsOff() )
dspmix = 0.0;
// duck all sound volumes except speaker's voice
#if !defined( NO_VOICE )
int duckScale = min((int)(g_DuckScale * 256), g_SND_VoiceOverdriveInt);
#else
int duckScale = (int)(g_DuckScale * 256);
#endif
if( duckScale < 256 )
{
if( pChannel->pMixer )
{
CAudioSource *pSource = pChannel->pMixer->GetSource();
if( !pSource->IsVoiceSource() )
{
// Apply voice overdrive..
for (i = 0; i < CCHANVOLUMES; i++)
volume[i] = (volume[i] * duckScale) >> 8;
}
}
}
// If mixing to the room buss, adjust volume based on channel's dspmix setting.
// dspmix is DSP_MIX_MAX (~0.78) if sound is far from player, DSP_MIX_MIN (~0.24) if sound is near player
if ( mixflag & SOUND_BUSS_ROOM )
{
// set dsp mix volume, scaled by global dsp_volume
float dspmixvol = fpmin(dspmix * g_dsp_volume, 1.0f);
// if dspmix is 1.0, 100% of sound goes to SOUND_BUFFER_ROOM and 0% to SOUND_BUFFER_FACING
for (i = 0; i < CCHANVOLUMES; i++)
volume[i] = (int)((float)(volume[i]) * dspmixvol);
}
// If global dsp volume is less than 1, reduce dspmix (ie: increase dry volume)
// If gloabl dsp volume is greater than 1, do not reduce dspmix
if (g_dsp_volume < 1.0)
dspmix *= g_dsp_volume;
// If mixing to facing/facingaway buss, adjust volume based on sound entity's facing direction.
// If sound directly faces player, ch->dspface = 1.0. If facing directly away, ch->dspface = -1.0.
// mix to lowpass buffer if facing away, to allpass if facing
// scale 1.0 - facing player, scale 0, facing away
scale = (pChannel->dspface + 1.0) / 2.0;
// UNDONE: get front cone % from channel to set this.
// bias scale such that 1.0 to 'cone' is considered facing. Facing cone narrows as cone -> 1.0
// and 'cone' -> 0.0 becomes 1.0 -> 0.0
float cone = 0.6f;
scale = scale * (1/cone);
scale = clamp( scale, 0.0f, 1.0f );
// pan between facing and facing away buffers
// if ( !g_bdirectionalfx || wavtype == CHAR_DOPPLER || wavtype == CHAR_OMNI || (wavtype == CHAR_DIRECTIONAL && mixchans == 2) )
if ( !g_bdirectionalfx || wavtype != CHAR_DIRECTIONAL )
{
// if no directional fx mix 0% to facingaway buffer
// if wavtype is DOPPLER, mix 0% to facingaway buffer - DOPPLER wavs have a custom mixer
// if wavtype is OMNI, mix 0% to facingaway buffer - OMNI wavs have no directionality
// if wavtype is DIRECTIONAL and stereo encoded, mix 0% to facingaway buffer - DIRECTIONAL STEREO wavs have a custom mixer
scale = 1.0;
}
if ( mixflag & SOUND_BUSS_FACING )
{
// facing player
// if dspface is 1.0, 100% of sound goes to SOUND_BUFFER_FACING
for (i = 0; i < CCHANVOLUMES; i++)
volume[i] = (int)((float)(volume[i]) * scale * (1.0 - dspmix));
}
else if ( mixflag & SOUND_BUSS_FACINGAWAY )
{
// facing away from player
// if dspface is 0.0, 100% of sound goes to SOUND_BUFFER_FACINGAWAY
for (i = 0; i < CCHANVOLUMES; i++)
volume[i] = (int)((float)(volume[i]) * (1.0 - scale) * (1.0 - dspmix));
}
// NOTE: this must occur last in this routine:
if ( g_AudioDevice->IsSurround() && !ppaint->fsurround )
{
// if 4ch or 5ch spatialization on, but current mix buffer is 2ch,
// recombine front + rear volumes (revert to 2ch spatialization)
volume[IFRONT_RIGHT] += volume[IREAR_RIGHT];
volume[IFRONT_LEFT] += volume[IREAR_LEFT];
volume[IFRONT_RIGHTD] += volume[IREAR_RIGHTD];
volume[IFRONT_LEFTD] += volume[IREAR_LEFTD];
// if 5 ch, recombine center channel vol
if ( g_AudioDevice->IsSurroundCenter() )
{
volume[IFRONT_RIGHT] += volume[IFRONT_CENTER] / 2;
volume[IFRONT_LEFT] += volume[IFRONT_CENTER] / 2;
volume[IFRONT_RIGHTD] += volume[IFRONT_CENTERD] / 2;
volume[IFRONT_LEFTD] += volume[IFRONT_CENTERD] / 2;
}
// clear rear & center volumes
volume[IREAR_RIGHT] = 0;
volume[IREAR_LEFT] = 0;
volume[IFRONT_CENTER] = 0;
volume[IREAR_RIGHTD] = 0;
volume[IREAR_LEFTD] = 0;
volume[IFRONT_CENTERD] = 0;
}
bool fzerovolume = true;
for (i = 0; i < CCHANVOLUMES; i++)
{
volume[i] = clamp(volume[i], 0, 255);
if (volume[i])
fzerovolume = false;
}
if ( fzerovolume )
{
// DevMsg ("Skipping mix of 0 volume sound! \n");
return false;
}
return true;
}
//===============================================================================
// Low level mixing routines
//===============================================================================
void Snd_WriteLinearBlastStereo16( void )
{
#if !id386
int i;
int val;
for ( i=0; i<snd_linear_count; i+=2 )
{
// scale and clamp left 16bit signed: [0x8000, 0x7FFF]
val = ( snd_p[i] * snd_vol )>>8;
if ( val > 32767 )
snd_out[i] = 32767;
else if ( val < -32768 )
snd_out[i] = -32768;
else
snd_out[i] = val;
// scale and clamp right 16bit signed: [0x8000, 0x7FFF]
val = ( snd_p[i+1] * snd_vol )>>8;
if ( val > 32767 )
snd_out[i+1] = 32767;
else if ( val < -32768 )
snd_out[i+1] = -32768;
else
snd_out[i+1] = val;
}
#else
__asm
{
// input data
mov ebx,snd_p
// output data
mov edi,snd_out
// iterate from end to beginning
mov ecx,snd_linear_count
// scale table
mov esi,snd_vol
// scale and clamp 16bit signed lsw: [0x8000, 0x7FFF]
WLBS16_LoopTop:
mov eax,[ebx+ecx*4-8]
imul eax,esi
sar eax,0x08
cmp eax,0x7FFF
jg WLBS16_ClampHigh
cmp eax,0xFFFF8000
jnl WLBS16_ClampDone
mov eax,0xFFFF8000
jmp WLBS16_ClampDone
WLBS16_ClampHigh:
mov eax,0x7FFF
WLBS16_ClampDone:
// scale and clamp 16bit signed msw: [0x8000, 0x7FFF]
mov edx,[ebx+ecx*4-4]
imul edx,esi
sar edx,0x08
cmp edx,0x7FFF
jg WLBS16_ClampHigh2
cmp edx,0xFFFF8000
jnl WLBS16_ClampDone2
mov edx,0xFFFF8000
jmp WLBS16_ClampDone2
WLBS16_ClampHigh2:
mov edx,0x7FFF
WLBS16_ClampDone2:
shl edx,0x10
and eax,0xFFFF
or edx,eax
mov [edi+ecx*2-4],edx
// two shorts per iteration
sub ecx,0x02
jnz WLBS16_LoopTop
}
#endif
}
void SND_InitScaletable (void)
{
int i, j;
for (i=0 ; i<SND_SCALE_LEVELS; i++)
for (j=0 ; j<256 ; j++)
snd_scaletable[i][j] = ((signed char)j) * i * (1<<SND_SCALE_SHIFT);
}
void SND_PaintChannelFrom8(portable_samplepair_t *pOutput, int *volume, byte *pData8, int count)
{
#if !id386
int data;
int *lscale, *rscale;
int i;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
for (i=0 ; i<count ; i++)
{
data = pData8[i];
pOutput[i].left += lscale[data];
pOutput[i].right += rscale[data];
}
#else
// portable_samplepair_t structure
#define psp_left 0
#define psp_right 4
#define psp_size 8
static int tempStore;
__asm
{
// prologue
push ebp
// esp = pOutput
mov eax, pOutput
mov tempStore, eax
xchg esp,tempStore
// ebx = volume
mov ebx,volume
// esi = pData8
mov esi,pData8
// ecx = count
mov ecx,count
// These values depend on the setting of SND_SCALE_BITS
// The mask must mask off all the lower bits you aren't using in the multiply
// so for 7 bits, the mask is 0xFE, 6 bits 0xFC, etc.
// The shift must multiply by the table size. There are 256 4-byte values in the table at each level.
// So each index must be shifted left by 10, but since the bits we use are in the MSB rather than LSB
// they must be shifted right by 8 - SND_SCALE_BITS. e.g., for a 7 bit number the left shift is:
// 10 - (8-7) = 9. For a 5 bit number it's 10 - (8-5) = 7.
mov eax,[ebx]
mov edx,[ebx + 4]
and eax,0xFE
and edx,0xFE
// shift up by 10 to index table, down by 1 to make the 7 MSB of the bytes an index
// eax = lscale
// edx = rscale
shl eax,0x09
shl edx,0x09
add eax,OFFSET snd_scaletable
add edx,OFFSET snd_scaletable
// ebx = data byte
sub ebx,ebx
mov bl,[esi+ecx-1]
// odd or even number of L/R samples
test ecx,0x01
jz PCF8_Loop
// process odd L/R sample
mov edi,[eax+ebx*4]
mov ebp,[edx+ebx*4]
add edi,[esp+ecx*psp_size-psp_size+psp_left]
add ebp,[esp+ecx*psp_size-psp_size+psp_right]
mov [esp+ecx*psp_size-psp_size+psp_left],edi
mov [esp+ecx*psp_size-psp_size+psp_right],ebp
mov bl,[esi+ecx-1-1]
dec ecx
jz PCF8_Done
PCF8_Loop:
// process L/R sample N
mov edi,[eax+ebx*4]
mov ebp,[edx+ebx*4]
add edi,[esp+ecx*psp_size-psp_size+psp_left]
add ebp,[esp+ecx*psp_size-psp_size+psp_right]
mov [esp+ecx*psp_size-psp_size+psp_left],edi
mov [esp+ecx*psp_size-psp_size+psp_right],ebp
mov bl,[esi+ecx-1-1]
// process L/R sample N-1
mov edi,[eax+ebx*4]
mov ebp,[edx+ebx*4]
add edi,[esp+ecx*psp_size-psp_size*2+psp_left]
add ebp,[esp+ecx*psp_size-psp_size*2+psp_right]
mov [esp+ecx*psp_size-psp_size*2+psp_left],edi
mov [esp+ecx*psp_size-psp_size*2+psp_right],ebp
mov bl,[esi+ecx-1-2]
// two L/R samples per iteration
sub ecx,0x02
jnz PCF8_Loop
PCF8_Done:
// epilogue
xchg esp,tempStore
pop ebp
}
#endif
}
//===============================================================================
// SOFTWARE MIXING ROUTINES
//===============================================================================
// UNDONE: optimize these
// grab samples from left source channel only and mix as if mono.
// volume array contains appropriate spatialization volumes for doppler left (incoming sound)
void SW_Mix8StereoDopplerLeft( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += lscale[pData[sampleIndex]];
pOutput[i].right += rscale[pData[sampleIndex]];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// grab samples from right source channel only and mix as if mono.
// volume array contains appropriate spatialization volumes for doppler right (outgoing sound)
void SW_Mix8StereoDopplerRight( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += lscale[pData[sampleIndex+1]];
pOutput[i].right += rscale[pData[sampleIndex+1]];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// grab samples from left source channel only and mix as if mono.
// volume array contains appropriate spatialization volumes for doppler left (incoming sound)
void SW_Mix16StereoDopplerLeft( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += (volume[0] * (int)(pData[sampleIndex]))>>8;
pOutput[i].right += (volume[1] * (int)(pData[sampleIndex]))>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// grab samples from right source channel only and mix as if mono.
// volume array contains appropriate spatialization volumes for doppler right (outgoing sound)
void SW_Mix16StereoDopplerRight( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += (volume[0] * (int)(pData[sampleIndex+1]))>>8;
pOutput[i].right += (volume[1] * (int)(pData[sampleIndex+1]))>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// mix left wav (front facing) with right wav (rear facing) based on soundfacing direction
void SW_Mix8StereoDirectional( float soundfacing, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int x;
int l,r;
signed char lb,rb;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
// if soundfacing -1.0, sound source is facing away from player
// if soundfacing 0.0, sound source is perpendicular to player
// if soundfacing 1.0, sound source is facing player
int frontmix = (int)(256.0f * ((1.f + soundfacing) / 2.f)); // 0 -> 256
for ( int i = 0; i < outCount; i++ )
{
lb = (pData[sampleIndex]); // get left byte
rb = (pData[sampleIndex+1]); // get right byte
l = ((int)lb);
r = ((int)rb);
x = ( r + ((( l - r ) * frontmix) >> 8) );
pOutput[i].left += lscale[x & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// mix left wav (front facing) with right wav (rear facing) based on soundfacing direction
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix8StereoDirectional_Interp( float soundfacing, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interpl, interpr;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
int x;
// if soundfacing -1.0, sound source is facing away from player
// if soundfacing 0.0, sound source is perpendicular to player
// if soundfacing 1.0, sound source is facing player
int frontmix = (int)(256.0f * ((1.f + soundfacing) / 2.f)); // 0 -> 256
for ( int i = 0; i < outCount; i++ )
{
// interpolate between first & second sample (the samples bordering sampleFrac12 fraction)
first = (int)((signed char)(pData[sampleIndex])); // left byte
second = (int)((signed char)(pData[sampleIndex+2]));
interpl = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
first = (int)((signed char)(pData[sampleIndex+1])); // right byte
second = (int)((signed char)(pData[sampleIndex+3]));
interpr = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
// crossfade between right/left based on directional mix
x = ( interpr + ((( interpl - interpr ) * frontmix) >> 8) );
pOutput[i].left += lscale[x & 0xFF]; // scale and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
// mix left wav (front facing) with right wav (rear facing) based on soundfacing direction
void SW_Mix16StereoDirectional( float soundfacing, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
fixedint sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int x;
int l, r;
// if soundfacing -1.0, sound source is facing away from player
// if soundfacing 0.0, sound source is perpendicular to player
// if soundfacing 1.0, sound source is facing player
int frontmix = (int)(256.0f * ((1.f + soundfacing) / 2.f)); // 0 -> 256
for ( int i = 0; i < outCount; i++ )
{
// get left, right samples
l = (int)(pData[sampleIndex]);
r = (int)(pData[sampleIndex+1]);
// crossfade between left & right based on front/rear facing
x = ( r + ((( l - r ) * frontmix) >> 8) );
pOutput[i].left += (volume[0] * x) >> 8;
pOutput[i].right += (volume[1] * x) >> 8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// mix left wav (front facing) with right wav (rear facing) based on soundfacing direction
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix16StereoDirectional_Interp( float soundfacing, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int x;
int first, second, interpl, interpr;
// if soundfacing -1.0, sound source is facing away from player
// if soundfacing 0.0, sound source is perpendicular to player
// if soundfacing 1.0, sound source is facing player
int frontmix = (int)(256.0f * ((1.f + soundfacing) / 2.f)); // 0 -> 256
for ( int i = 0; i < outCount; i++ )
{
// get interpolated left, right samples
first = (int)(pData[sampleIndex]);
second = (int)(pData[sampleIndex+2]);
interpl = first + (((second - first) * (int)sampleFrac14) >> 14);
first = (int)(pData[sampleIndex+1]);
second = (int)(pData[sampleIndex+3]);
interpr = first + (((second - first) * (int)sampleFrac14) >> 14);
// crossfade between left & right based on front/rear facing
x = ( interpr + ((( interpl - interpr ) * frontmix) >> 8) );
pOutput[i].left += (volume[0] * x) >> 8;
pOutput[i].right += (volume[1] * x) >> 8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
// distance variant wav (left is close, right is far)
void SW_Mix8StereoDistVar( float distmix, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int x;
int l,r;
signed char lb, rb;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
// distmix 0 - sound is near player (100% wav left)
// distmix 1.0 - sound is far from player (100% wav right)
int nearmix = (int)(256.0f * (1.0f - distmix));
int farmix = (int)(256.0f * distmix);
// if mixing at max or min range, skip crossfade (KDB: perf)
if (!nearmix)
{
for ( int i = 0; i < outCount; i++ )
{
rb = (pData[sampleIndex+1]); // get right byte
x = (int) rb;
pOutput[i].left += lscale[x & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
return;
}
if (!farmix)
{
for ( int i = 0; i < outCount; i++ )
{
lb = (pData[sampleIndex]); // get left byte
x = (int) lb;
pOutput[i].left += lscale[x & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
return;
}
// crossfade left/right
for ( int i = 0; i < outCount; i++ )
{
lb = (pData[sampleIndex]); // get left byte
rb = (pData[sampleIndex+1]); // get right byte
l = (int)lb;
r = (int)rb;
x = ( l + (((r - l) * farmix ) >> 8) );
pOutput[i].left += lscale[x & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// distance variant wav (left is close, right is far)
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix8StereoDistVar_Interp( float distmix, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int x;
// distmix 0 - sound is near player (100% wav left)
// distmix 1.0 - sound is far from player (100% wav right)
int nearmix = (int)(256.0f * (1.0f - distmix));
int farmix = (int)(256.0f * distmix);
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interpl, interpr;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
// if mixing at max or min range, skip crossfade (KDB: perf)
if (!nearmix)
{
for ( int i = 0; i < outCount; i++ )
{
first = (int)((signed char)(pData[sampleIndex+1])); // right sample
second = (int)((signed char)(pData[sampleIndex+3]));
interpr = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
pOutput[i].left += lscale[interpr & 0xFF]; // scale and convert to 16 bit
pOutput[i].right += rscale[interpr & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
return;
}
if (!farmix)
{
for ( int i = 0; i < outCount; i++ )
{
first = (int)((signed char)(pData[sampleIndex])); // left sample
second = (int)((signed char)(pData[sampleIndex+2]));
interpl = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
pOutput[i].left += lscale[interpl & 0xFF]; // scale and convert to 16 bit
pOutput[i].right += rscale[interpl & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
return;
}
// crossfade left/right
for ( int i = 0; i < outCount; i++ )
{
// interpolate between first & second sample (the samples bordering sampleFrac14 fraction)
first = (int)((signed char)(pData[sampleIndex]));
second = (int)((signed char)(pData[sampleIndex+2]));
interpl = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
first = (int)((signed char)(pData[sampleIndex+1]));
second = (int)((signed char)(pData[sampleIndex+3]));
interpr = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
// crossfade between left and right based on distance mix
x = ( interpl + (((interpr - interpl) * farmix ) >> 8) );
pOutput[i].left += lscale[x & 0xFF]; // scale and convert to 16 bit
pOutput[i].right += rscale[x & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
// distance variant wav (left is close, right is far)
void SW_Mix16StereoDistVar( float distmix, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int x;
int l,r;
// distmix 0 - sound is near player (100% wav left)
// distmix 1.0 - sound is far from player (100% wav right)
int nearmix = Float2Int(256.0f * (1.f - distmix));
int farmix = Float2Int(256.0f * distmix);
// if mixing at max or min range, skip crossfade (KDB: perf)
if (!nearmix)
{
for ( int i = 0; i < outCount; i++ )
{
x = pData[sampleIndex+1]; // right sample
pOutput[i].left += (volume[0] * x)>>8;
pOutput[i].right += (volume[1] * x)>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
return;
}
if (!farmix)
{
for ( int i = 0; i < outCount; i++ )
{
x = pData[sampleIndex]; // left sample
pOutput[i].left += (volume[0] * x)>>8;
pOutput[i].right += (volume[1] * x)>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
return;
}
// crossfade left/right
for ( int i = 0; i < outCount; i++ )
{
l = pData[sampleIndex];
r = pData[sampleIndex+1];
x = ( l + (((r - l) * farmix) >> 8) );
pOutput[i].left += (volume[0] * x)>>8;
pOutput[i].right += (volume[1] * x)>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// distance variant wav (left is close, right is far)
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix16StereoDistVar_Interp( float distmix, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int x;
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interpl, interpr;
// distmix 0 - sound is near player (100% wav left)
// distmix 1.0 - sound is far from player (100% wav right)
int nearmix = Float2Int(256.0f * (1.f - distmix));
int farmix = Float2Int(256.0f * distmix);
// if mixing at max or min range, skip crossfade (KDB: perf)
if (!nearmix)
{
for ( int i = 0; i < outCount; i++ )
{
first = (int)(pData[sampleIndex+1]); // right sample
second = (int)(pData[sampleIndex+3]);
interpr = first + (((second - first) * (int)sampleFrac14) >> 14);
pOutput[i].left += (volume[0] * interpr)>>8;
pOutput[i].right += (volume[1] * interpr)>>8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
return;
}
if (!farmix)
{
for ( int i = 0; i < outCount; i++ )
{
first = (int)(pData[sampleIndex]); // left sample
second = (int)(pData[sampleIndex+2]);
interpl = first + (((second - first) * (int)sampleFrac14) >> 14);
pOutput[i].left += (volume[0] * interpl)>>8;
pOutput[i].right += (volume[1] * interpl)>>8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
return;
}
// crossfade left/right
for ( int i = 0; i < outCount; i++ )
{
first = (int)(pData[sampleIndex]);
second = (int)(pData[sampleIndex+2]);
interpl = first + (((second - first) * (int)sampleFrac14) >> 14);
first = (int)(pData[sampleIndex+1]);
second = (int)(pData[sampleIndex+3]);
interpr = first + (((second - first) * (int)sampleFrac14) >> 14);
// crossfade between left & right samples
x = ( interpl + (((interpr - interpl) * farmix) >> 8) );
pOutput[i].left += (volume[0] * x) >> 8;
pOutput[i].right += (volume[1] * x) >> 8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
void SW_Mix8Mono( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
// Not using pitch shift?
if ( rateScaleFix == FIX(1) )
{
// native code
SND_PaintChannelFrom8( pOutput, volume, (byte *)pData, outCount );
return;
}
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += lscale[pData[sampleIndex]];
pOutput[i].right += rscale[pData[sampleIndex]];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac);
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix8Mono_Interp( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount)
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interp;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
// iterate 0th sample to outCount-1 sample
for (int i = 0; i < outCount; i++ )
{
// interpolate between first & second sample (the samples bordering sampleFrac12 fraction)
first = (int)((signed char)(pData[sampleIndex]));
second = (int)((signed char)(pData[sampleIndex+1]));
interp = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
pOutput[i].left += lscale[interp & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[interp & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14);
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
void SW_Mix8Stereo( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += lscale[pData[sampleIndex]];
pOutput[i].right += rscale[pData[sampleIndex+1]];
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix8Stereo_Interp( portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount)
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interpl, interpr;
int *lscale, *rscale;
lscale = snd_scaletable[volume[0] >> SND_SCALE_SHIFT];
rscale = snd_scaletable[volume[1] >> SND_SCALE_SHIFT];
// iterate 0th sample to outCount-1 sample
for (int i = 0; i < outCount; i++ )
{
// interpolate between first & second sample (the samples bordering sampleFrac12 fraction)
first = (int)((signed char)(pData[sampleIndex])); // left
second = (int)((signed char)(pData[sampleIndex+2]));
interpl = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
first = (int)((signed char)(pData[sampleIndex+1])); // right
second = (int)((signed char)(pData[sampleIndex+3]));
interpr = first + ( ((second - first) * (int)sampleFrac14) >> 14 );
pOutput[i].left += lscale[interpl & 0xFF]; // multiply by volume and convert to 16 bit
pOutput[i].right += rscale[interpr & 0xFF];
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
void SW_Mix16Mono_Shift( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int vol0 = volume[0];
int vol1 = volume[1];
#if !id386
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += (vol0 * (int)(pData[sampleIndex]))>>8;
pOutput[i].right += (vol1 * (int)(pData[sampleIndex]))>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac);
sampleFrac = FIX_FRACPART(sampleFrac);
}
#else
// in assembly, you can make this 32.32 instead of 4.28 and use the carry flag instead of masking
int rateScaleInt = FIX_INTPART(rateScaleFix);
unsigned int rateScaleFrac = FIX_FRACPART(rateScaleFix) << (32-FIX_BITS);
__asm
{
mov eax, volume ;
movq mm0, DWORD PTR [eax] ; vol1, vol0 (32-bits each)
packssdw mm0, mm0 ; pack and replicate... vol1, vol0, vol1, vol0 (16-bits each)
//pxor mm7, mm7 ; mm7 is my zero register...
xor esi, esi
mov eax, DWORD PTR [pOutput] ; store initial output ptr
mov edx, DWORD PTR [pData] ; store initial input ptr
mov ebx, inputOffset;
mov ecx, outCount;
BEGINLOAD:
movd mm2, WORD PTR [edx+2*esi] ; load first piece of data from pData
punpcklwd mm2, mm2 ; 0, 0, pData_1st, pData_1st
add ebx, rateScaleFrac ; do the crazy fixed integer math
adc esi, rateScaleInt
movd mm3, WORD PTR [edx+2*esi] ; load second piece of data from pData
punpcklwd mm3, mm3 ; 0, 0, pData_2nd, pData_2nd
punpckldq mm2, mm3 ; pData_2nd, pData_2nd, pData_2nd, pData_2nd
add ebx, rateScaleFrac ; do the crazy fixed integer math
adc esi, rateScaleInt
movq mm3, mm2 ; copy the goods
pmullw mm2, mm0 ; pData_2nd*vol1, pData_2nd*vol0, pData_1st*vol1, pData_1st*vol0 (bits 0-15)
pmulhw mm3, mm0 ; pData_2nd*vol1, pData_2nd*vol0, pData_1st*vol1, pData_1st*vol0 (bits 16-31)
movq mm4, mm2 ; copy
movq mm5, mm3 ; copy
punpcklwd mm2, mm3 ; pData_1st*vol1, pData_1st*vol0 (bits 0-31)
punpckhwd mm4, mm5 ; pData_2nd*vol1, pData_2nd*vol0 (bits 0-31)
psrad mm2, 8 ; shift right by 8
psrad mm4, 8 ; shift right by 8
add ecx, -2 ; decrement i-value
paddd mm2, QWORD PTR [eax] ; add to existing vals
paddd mm4, QWORD PTR [eax+8] ;
movq QWORD PTR [eax], mm2 ; store back
movq QWORD PTR [eax+8], mm4 ;
add eax, 10h ;
cmp ecx, 01h ; see if we can quit
jg BEGINLOAD ; Kipp Owens is a doof...
jl END ; Nick Shaffner is killing me...
movsx edi, WORD PTR [edx+2*esi] ; load first 16 bit val and zero-extend
imul edi, vol0 ; multiply pData[sampleIndex] by volume[0]
sar edi, 08h ; divide by 256
add DWORD PTR [eax], edi ; add to pOutput[i].left
movsx edi, WORD PTR [edx+2*esi] ; load same 16 bit val and zero-extend (cuz I thrashed the reg)
imul edi, vol1 ; multiply pData[sampleIndex] by volume[1]
sar edi, 08h ; divide by 256
add DWORD PTR [eax+04h], edi ; add to pOutput[i].right
END:
emms;
}
#endif
}
void SW_Mix16Mono_NoShift( portable_samplepair_t *pOutput, int *volume, short *pData, int outCount )
{
int vol0 = volume[0];
int vol1 = volume[1];
#if !id386
for ( int i = 0; i < outCount; i++ )
{
int x = *pData++;
pOutput[i].left += (x * vol0) >> 8;
pOutput[i].right += (x * vol1) >> 8;
}
#else
__asm
{
mov eax, volume ;
movq mm0, DWORD PTR [eax] ; vol1, vol0 (32-bits each)
packssdw mm0, mm0 ; pack and replicate... vol1, vol0, vol1, vol0 (16-bits each)
//pxor mm7, mm7 ; mm7 is my zero register...
mov eax, DWORD PTR [pOutput] ; store initial output ptr
mov edx, DWORD PTR [pData] ; store initial input ptr
mov ecx, outCount;
BEGINLOAD:
movd mm2, WORD PTR [edx] ; load first piece o data from pData
punpcklwd mm2, mm2 ; 0, 0, pData_1st, pData_1st
add edx,2 ; move to the next sample
movd mm3, WORD PTR [edx] ; load second piece o data from pData
punpcklwd mm3, mm3 ; 0, 0, pData_2nd, pData_2nd
punpckldq mm2, mm3 ; pData_2nd, pData_2nd, pData_2nd, pData_2nd
add edx,2 ; move to the next sample
movq mm3, mm2 ; copy the goods
pmullw mm2, mm0 ; pData_2nd*vol1, pData_2nd*vol0, pData_1st*vol1, pData_1st*vol0 (bits 0-15)
pmulhw mm3, mm0 ; pData_2nd*vol1, pData_2nd*vol0, pData_1st*vol1, pData_1st*vol0 (bits 16-31)
movq mm4, mm2 ; copy
movq mm5, mm3 ; copy
punpcklwd mm2, mm3 ; pData_1st*vol1, pData_1st*vol0 (bits 0-31)
punpckhwd mm4, mm5 ; pData_2nd*vol1, pData_2nd*vol0 (bits 0-31)
psrad mm2, 8 ; shift right by 8
psrad mm4, 8 ; shift right by 8
add ecx, -2 ; decrement i-value
paddd mm2, QWORD PTR [eax] ; add to existing vals
paddd mm4, QWORD PTR [eax+8] ;
movq QWORD PTR [eax], mm2 ; store back
movq QWORD PTR [eax+8], mm4 ;
add eax, 10h ;
cmp ecx, 01h ; see if we can quit
jg BEGINLOAD ; I can cut and paste code!
jl END ;
movsx edi, WORD PTR [edx] ; load first 16 bit val and zero-extend
mov esi,edi ; save a copy for the other channel
imul edi, vol0 ; multiply pData[sampleIndex] by volume[0]
sar edi, 08h ; divide by 256
add DWORD PTR [eax], edi ; add to pOutput[i].left
; esi has a copy, use it now
imul esi, vol1 ; multiply pData[sampleIndex] by volume[1]
sar esi, 08h ; divide by 256
add DWORD PTR [eax+04h], esi ; add to pOutput[i].right
END:
emms;
}
#endif
}
void SW_Mix16Mono( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
if ( rateScaleFix == FIX(1) )
{
SW_Mix16Mono_NoShift( pOutput, volume, pData, outCount );
}
else
{
SW_Mix16Mono_Shift( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
}
}
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix16Mono_Interp( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interp;
for ( int i = 0; i < outCount; i++ )
{
first = (int)(pData[sampleIndex]);
second = (int)(pData[sampleIndex+1]);
interp = first + (((second - first) * (int)sampleFrac14) >> 14);
pOutput[i].left += (volume[0] * interp) >> 8;
pOutput[i].right += (volume[1] * interp) >> 8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14);
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
void SW_Mix16Stereo( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
int sampleIndex = 0;
fixedint sampleFrac = inputOffset;
for ( int i = 0; i < outCount; i++ )
{
pOutput[i].left += (volume[0] * (int)(pData[sampleIndex]))>>8;
pOutput[i].right += (volume[1] * (int)(pData[sampleIndex+1]))>>8;
sampleFrac += rateScaleFix;
sampleIndex += FIX_INTPART(sampleFrac)<<1;
sampleFrac = FIX_FRACPART(sampleFrac);
}
}
// interpolating pitch shifter - sample(s) from preceding buffer are preloaded in
// pData buffer, ensuring we can always provide 'outCount' samples.
void SW_Mix16Stereo_Interp( portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
fixedint sampleIndex = 0;
fixedint rateScaleFix14 = FIX_28TO14(rateScaleFix); // convert 28 bit fixed point to 14 bit fixed point
fixedint sampleFrac14 = FIX_28TO14(inputOffset);
int first, second, interpl, interpr;
for ( int i = 0; i < outCount; i++ )
{
first = (int)(pData[sampleIndex]);
second = (int)(pData[sampleIndex+2]);
interpl = first + (((second - first) * (int)sampleFrac14) >> 14);
first = (int)(pData[sampleIndex+1]);
second = (int)(pData[sampleIndex+3]);
interpr = first + (((second - first) * (int)sampleFrac14) >> 14);
pOutput[i].left += (volume[0] * interpl) >> 8;
pOutput[i].right += (volume[1] * interpr) >> 8;
sampleFrac14 += rateScaleFix14;
sampleIndex += FIX_INTPART14(sampleFrac14)<<1;
sampleFrac14 = FIX_FRACPART14(sampleFrac14);
}
}
// return true if mixer should use high quality pitch interpolation for this sound
bool FUseHighQualityPitch( channel_t *pChannel )
{
// do not use interpolating pitch shifter if:
// low quality flag set on sound (ie: wave name is prepended with CHAR_FAST_PITCH)
// or pitch has no fractional part
// or snd_pitchquality is 0
if ( !snd_pitchquality.GetInt() || pChannel->flags.bfast_pitch )
return false;
return ( (pChannel->pitch != floor(pChannel->pitch)) );
}
//===============================================================================
// DISPATCHERS FOR MIXING ROUTINES
//===============================================================================
void Mix8MonoWavtype( channel_t *pChannel, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix8Mono_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix8Mono( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
}
void Mix16MonoWavtype( channel_t *pChannel, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix16Mono_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
// fast native coded mixers with lower quality pitch shift
SW_Mix16Mono( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
}
void Mix8StereoWavtype( channel_t *pChannel, portable_samplepair_t *pOutput, int *volume, byte *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
switch ( pChannel->wavtype )
{
case CHAR_DOPPLER:
SW_Mix8StereoDopplerLeft( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
SW_Mix8StereoDopplerRight( pOutput, &volume[IFRONT_LEFTD], pData, inputOffset, rateScaleFix, outCount );
break;
case CHAR_DIRECTIONAL:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix8StereoDirectional_Interp( pChannel->dspface, pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix8StereoDirectional( pChannel->dspface, pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
case CHAR_DISTVARIANT:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix8StereoDistVar_Interp( pChannel->distmix, pOutput, volume, pData, inputOffset, rateScaleFix, outCount);
else
SW_Mix8StereoDistVar( pChannel->distmix, pOutput, volume, pData, inputOffset, rateScaleFix, outCount);
break;
case CHAR_OMNI:
// non directional stereo - all channel volumes are the same
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix8Stereo_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix8Stereo( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
default:
case CHAR_SPATIALSTEREO:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix8Stereo_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix8Stereo( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
}
}
void Mix16StereoWavtype( channel_t *pChannel, portable_samplepair_t *pOutput, int *volume, short *pData, int inputOffset, fixedint rateScaleFix, int outCount )
{
switch ( pChannel->wavtype )
{
case CHAR_DOPPLER:
SW_Mix16StereoDopplerLeft( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
SW_Mix16StereoDopplerRight( pOutput, &volume[IFRONT_LEFTD], pData, inputOffset, rateScaleFix, outCount );
break;
case CHAR_DIRECTIONAL:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix16StereoDirectional_Interp( pChannel->dspface, pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix16StereoDirectional( pChannel->dspface, pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
case CHAR_DISTVARIANT:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix16StereoDistVar_Interp( pChannel->distmix, pOutput, volume, pData, inputOffset, rateScaleFix, outCount);
else
SW_Mix16StereoDistVar( pChannel->distmix, pOutput, volume, pData, inputOffset, rateScaleFix, outCount);
break;
case CHAR_OMNI:
// non directional stereo - all channel volumes are same
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix16Stereo_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix16Stereo( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
default:
case CHAR_SPATIALSTEREO:
if ( FUseHighQualityPitch( pChannel ) )
SW_Mix16Stereo_Interp( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
else
SW_Mix16Stereo( pOutput, volume, pData, inputOffset, rateScaleFix, outCount );
break;
}
}
//===============================================================================
// Client entity mouth movement code. Set entity mouthopen variable, based
// on the sound envelope of the voice channel playing.
// KellyB 10/22/97
//===============================================================================
extern IBaseClientDLL *g_ClientDLL;
// called when voice channel is first opened on this entity
static CMouthInfo *GetMouthInfoForChannel( channel_t *pChannel )
{
#ifndef DEDICATED
// If it's a sound inside the client UI, ask the client for the mouthinfo
if ( pChannel->soundsource == SOUND_FROM_UI_PANEL )
return g_ClientDLL ? g_ClientDLL->GetClientUIMouthInfo() : NULL;
#endif
int mouthentity = pChannel->speakerentity == -1 ? pChannel->soundsource : pChannel->speakerentity;
IClientEntity *pClientEntity = entitylist->GetClientEntity( mouthentity );
if( !pClientEntity )
return NULL;
return pClientEntity->GetMouth();
}
void SND_InitMouth( channel_t *pChannel )
{
if ( SND_IsMouth( pChannel ) )
{
CMouthInfo *pMouth = GetMouthInfoForChannel(pChannel);
// init mouth movement vars
if ( pMouth )
{
pMouth->mouthopen = 0;
pMouth->sndavg = 0;
pMouth->sndcount = 0;
if ( pChannel->sfx->pSource && pChannel->sfx->pSource->GetSentence() )
{
pMouth->AddSource( pChannel->sfx->pSource, pChannel->flags.m_bIgnorePhonemes );
}
}
}
}
// called when channel stops
void SND_CloseMouth(channel_t *pChannel)
{
if ( SND_IsMouth( pChannel ) )
{
CMouthInfo *pMouth = GetMouthInfoForChannel(pChannel);
if ( pMouth )
{
// shut mouth
int idx = pMouth->GetIndexForSource( pChannel->sfx->pSource );
if ( idx != UNKNOWN_VOICE_SOURCE )
{
pMouth->RemoveSourceByIndex(idx);
}
else
{
pMouth->ClearVoiceSources();
}
pMouth->mouthopen = 0;
}
}
}
#define CAVGSAMPLES 10
// need this to make the debug code below work.
//#include "snd_wave_source.h"
void SND_MoveMouth8( channel_t *ch, CAudioSource *pSource, int count )
{
int data;
char *pdata = NULL;
int i;
int savg;
int scount;
CMouthInfo *pMouth = GetMouthInfoForChannel( ch );
if ( !pMouth )
return;
if ( pSource->GetSentence() )
{
int idx = pMouth->GetIndexForSource( pSource );
if ( idx == UNKNOWN_VOICE_SOURCE )
{
if ( pMouth->AddSource( pSource, ch->flags.m_bIgnorePhonemes ) == NULL )
{
DevMsg( 1, "out of voice sources, won't lipsync %s\n", ch->sfx->getname() );
#if 0
for ( int i = 0; i < pMouth->GetNumVoiceSources(); i++ )
{
CVoiceData *pVoice = pMouth->GetVoiceSource(i);
CAudioSourceWave *pWave = dynamic_cast<CAudioSourceWave *>(pVoice->GetSource());
const char *pName = "unknown";
if ( pWave && pWave->GetName() )
pName = pWave->GetName();
Msg("Playing %s...\n", pName );
}
#endif
}
}
else
{
// Update elapsed time from mixer
CVoiceData *vd = pMouth->GetVoiceSource( idx );
Assert( vd );
if ( vd )
{
Assert( pSource->SampleRate() > 0 );
float elapsed = ( float )ch->pMixer->GetSamplePosition() / ( float )pSource->SampleRate();
vd->SetElapsedTime( elapsed );
}
}
}
if ( IsX360() )
{
// not supporting because data is assumed to be 8 bit and bypasses mixer (decoding)
return;
}
if ( pMouth->NeedsEnvelope() )
{
int availableSamples = pSource->GetOutputData((void**)&pdata, ch->pMixer->GetSamplePosition(), count, NULL );
if( pdata == NULL )
return;
i = 0;
scount = pMouth->sndcount;
savg = 0;
while ( i < availableSamples && scount < CAVGSAMPLES )
{
data = pdata[i];
savg += abs(data);
i += 80 + ((byte)data & 0x1F);
scount++;
}
pMouth->sndavg += savg;
pMouth->sndcount = (byte) scount;
if ( pMouth->sndcount >= CAVGSAMPLES )
{
pMouth->mouthopen = pMouth->sndavg / CAVGSAMPLES;
pMouth->sndavg = 0;
pMouth->sndcount = 0;
}
}
else
{
pMouth->mouthopen = 0;
}
}
void SND_UpdateMouth( channel_t *pChannel )
{
CMouthInfo *m = GetMouthInfoForChannel( pChannel );
if ( !m )
return;
if ( pChannel->sfx )
{
m->AddSource( pChannel->sfx->pSource, pChannel->flags.m_bIgnorePhonemes );
}
}
void SND_ClearMouth( channel_t *pChannel )
{
CMouthInfo *m = GetMouthInfoForChannel( pChannel );
if ( !m )
return;
if ( pChannel->sfx )
{
m->RemoveSource( pChannel->sfx->pSource );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pChannel -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool SND_IsMouth( channel_t *pChannel )
{
#ifndef DEDICATED
if ( pChannel->soundsource == SOUND_FROM_UI_PANEL )
return true;
#endif
if ( !entitylist )
{
return false;
}
if ( pChannel->entchannel == CHAN_VOICE || pChannel->entchannel == CHAN_VOICE2 )
{
return true;
}
if ( pChannel->sfx &&
pChannel->sfx->pSource &&
pChannel->sfx->pSource->GetSentence() )
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pChannel -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool SND_ShouldPause( channel_t *pChannel )
{
return pChannel->flags.m_bShouldPause;
}
//===============================================================================
// Movie recording support
//===============================================================================
void SND_RecordInit()
{
g_paintedtime = 0;
g_soundtime = 0;
// TMP Wave file supports stereo only, so force stereo
if ( snd_surround.GetInt() != 2 )
{
snd_surround.SetValue( 2 );
}
}
void SND_MovieStart( void )
{
if ( IsX360() )
return;
if ( !cl_movieinfo.IsRecording() )
return;
SND_RecordInit();
// 44k: engine playback rate is now 44100...changed from 22050
if ( cl_movieinfo.DoWav() )
{
WaveCreateTmpFile( cl_movieinfo.moviename, SOUND_DMA_SPEED, 16, 2 );
}
}
void SND_MovieEnd( void )
{
if ( IsX360() )
return;
if ( !cl_movieinfo.IsRecording() )
{
return;
}
if ( cl_movieinfo.DoWav() )
{
WaveFixupTmpFile( cl_movieinfo.moviename );
}
}
bool SND_IsRecording()
{
return ( ( IsReplayRendering() || cl_movieinfo.IsRecording() ) && !Con_IsVisible() );
}
extern IVideoRecorder *g_pVideoRecorder;
void SND_RecordBuffer( void )
{
if ( IsX360() )
return;
if ( !SND_IsRecording() )
return;
int i;
int val;
int bufferSize = snd_linear_count * sizeof(short);
short *tmp = (short *)_alloca( bufferSize );
for (i=0 ; i<snd_linear_count ; i+=2)
{
val = (snd_p[i]*snd_vol)>>8;
tmp[i] = CLIP(val);
val = (snd_p[i+1]*snd_vol)>>8;
tmp[i+1] = CLIP(val);
}
if ( IsReplayRendering() )
{
#if defined( REPLAY_ENABLED )
extern IClientReplayContext *g_pClientReplayContext;
IReplayMovieRenderer *pMovieRenderer = g_pClientReplayContext->GetMovieRenderer();
if ( IsReplayRendering() && pMovieRenderer && pMovieRenderer->IsAudioSyncFrame() )
{
pMovieRenderer->RenderAudio( (unsigned char *)tmp, bufferSize, snd_linear_count );
}
#endif
}
else
{
if ( cl_movieinfo.DoWav() )
{
WaveAppendTmpFile( cl_movieinfo.moviename, tmp, 16, snd_linear_count );
}
if ( cl_movieinfo.DoVideoSound() )
{
g_pVideoRecorder->AppendAudioSamples( tmp, bufferSize );
}
}
}
|