summaryrefslogtreecommitdiff
path: root/engine/audio/private/snd_dma.cpp
blob: 137bce7dd7cab757a2280bb2ef9542f728e9ce6b (plain) (blame)
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
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Main control for any streaming sound output device.
//
//===========================================================================//

#include "audio_pch.h"
#include "const.h"
#include "cdll_int.h"
#include "client_class.h"
#include "icliententitylist.h"
#include "tier0/vcrmode.h"
#include "con_nprint.h"
#include "tier0/icommandline.h"
#include "vox_private.h"
#include "../../traceinit.h"
#include "../../cmd.h"
#include "toolframework/itoolframework.h"
#include "vstdlib/random.h"
#include "vstdlib/jobthread.h"
#include "vaudio/ivaudio.h"
#include "../../client.h"
#include "../../cl_main.h"
#include "utldict.h"
#include "mempool.h"
#include "../../enginetrace.h"			// for traceline
#include "../../public/bspflags.h"		// for traceline
#include "../../public/gametrace.h"		// for traceline
#include "vphysics_interface.h"		// for surface props
#include "../../ispatialpartitioninternal.h"	// for entity enumerator
#include "../../debugoverlay.h"
#include "icliententity.h"
#include "../../cmodel_engine.h"
#include "../../staticpropmgr.h"
#include "../../server.h"
#include "edict.h"
#include "../../pure_server.h"
#include "filesystem/IQueuedLoader.h"
#include "voice.h"
#if defined( _X360 )
#include "xbox/xbox_console.h"
#include "xmp.h"
#endif

#include "replay/iclientreplaycontext.h"
#include "replay/ireplaymovierenderer.h"

#include "video/ivideoservices.h"
extern IVideoServices *g_pVideo;

/*
#include "gl_model_private.h"
#include "world.h"
#include "vphysics_interface.h"
#include "client_class.h"
#include "server_class.h"
*/

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

///////////////////////////////////
// DEBUGGING
//
// Turn this on to print channel output msgs.
//
//#define DEBUG_CHANNELS

#define SNDLVL_TO_DIST_MULT( sndlvl ) ( sndlvl ? ((pow( 10.0f, snd_refdb.GetFloat() / 20 ) / pow( 10.0f, (float)sndlvl / 20 )) / snd_refdist.GetFloat()) : 0 )
#define DIST_MULT_TO_SNDLVL( dist_mult ) (soundlevel_t)(int)( dist_mult ? ( 20 * log10( pow( 10.0f, snd_refdb.GetFloat() / 20 ) / (dist_mult * snd_refdist.GetFloat()) ) ) : 0 )

extern ConVar dsp_spatial;
extern IPhysicsSurfaceProps	*physprop;

extern bool IsReplayRendering();

static void S_Play( const CCommand &args );
static void S_PlayVol( const CCommand &args );
void S_SoundList(void);
static void S_Say ( const CCommand &args );
void S_Update_(float);
void S_StopAllSounds(bool clear);
void S_StopAllSoundsC(void);
void S_ShutdownMixThread();
const char *GetClientClassname( SoundSource soundsource );

float SND_GetGainObscured( channel_t *ch, bool fplayersound, bool flooping, bool bAttenuated );
void DSP_ChangePresetValue( int idsp, int channel, int iproc, float value );
bool DSP_CheckDspAutoEnabled( void );
void DSP_SetDspAuto( int dsp_preset );
float dB_To_Radius ( float db );
int dsp_room_GetInt ( void );

bool MXR_LoadAllSoundMixers( void );
void MXR_ReleaseMemory( void );
int MXR_GetMixGroupListFromDirName( const char *pDirname, byte *pList, int listMax );
void MXR_GetMixGroupFromSoundsource( channel_t *pchan, SoundSource soundsource,  soundlevel_t soundlevel);
float MXR_GetVolFromMixGroup( int rgmixgroupid[8], int *plast_mixgroupid );
char *MXR_GetGroupnameFromId( int mixgroupid );
int MXR_GetMixgroupFromName( const char *pszgroupname );
void MXR_DebugShowMixVolumes( void );
#ifdef _DEBUG
static void MXR_DebugSetMixGroupVolume( const CCommand &args );
#endif //_DEBUG
void MXR_UpdateAllDuckerVolumes( void );

void ChannelSetVolTargets( channel_t *pch, int *pvolumes, int ivol_offset, int cvol );
void ChannelUpdateVolXfade( channel_t *pch );
void ChannelClearVolumes( channel_t *pch );
float VOX_GetChanVol(channel_t *ch);
void ConvertListenerVectorTo2D( Vector *pvforward, Vector *pvright );
int ChannelGetMaxVol( channel_t *pch );

// Forceably ends voice tweak mode (only occurs during snd_restart
void VoiceTweak_EndVoiceTweakMode();
bool VoiceTweak_IsStillTweaking();
// Only does anything for voice tweak channel so if view entity changes it doesn't fade out to zero volume
void Voice_Spatialize( channel_t *channel );

// =======================================================================
// Internal sound data & structures
// =======================================================================

channel_t   channels[MAX_CHANNELS];

int	total_channels;
CActiveChannels g_ActiveChannels;
static double g_LastSoundFrame = 0.0f;		// last full frame of sound
static double g_LastMixTime = 0.0f;			// last time we did mixing
static float g_EstFrameTime = 0.1f;			// estimated frame time running average

// x360 override to fade out game music when the user is playing music through the dashboard
static float g_DashboardMusicMixValue = 1.0f;
static float g_DashboardMusicMixTarget = 1.0f;
const float g_DashboardMusicFadeRate = 0.5f;	// Fades one half full-scale volume per second (two seconds for complete fadeout)

// sound mixers
int g_csoundmixers	= 0;					// total number of soundmixers found
int g_cgrouprules	= 0;					// total number of group rules found
int g_cgroupclass	= 0;

// this is used to enable/disable music playback on x360 when the user selects his own soundtrack to play
void S_EnableMusic( bool bEnable )
{
	if ( bEnable )
	{
		g_DashboardMusicMixTarget = 1.0f;
	}
	else
	{
		g_DashboardMusicMixTarget = 0.0f;
	}
}

bool IsSoundSourceLocalPlayer( int soundsource )
{
	if ( soundsource == SOUND_FROM_UI_PANEL )
		return true;

	return ( soundsource == g_pSoundServices->GetViewEntity() );
}

CThreadMutex g_SndMutex;

#define THREAD_LOCK_SOUND() AUTO_LOCK( g_SndMutex )

const int MASK_BLOCK_AUDIO = CONTENTS_SOLID|CONTENTS_MOVEABLE|CONTENTS_WINDOW;

void CActiveChannels::Add( channel_t *pChannel )
{
	Assert( pChannel->activeIndex == 0 );
	m_list[m_count] = pChannel - channels;
	m_count++;
	pChannel->activeIndex = m_count;
}

void CActiveChannels::Remove( channel_t *pChannel )
{
	if ( pChannel->activeIndex == 0 )
		return;
	int activeIndex = pChannel->activeIndex - 1;
	Assert( activeIndex >= 0 && activeIndex < m_count );
	Assert( pChannel == &channels[m_list[activeIndex]] );
	m_count--;
	// Not the last one?  Swap the last one with this one and fix its index
	if ( activeIndex < m_count )
	{
		m_list[activeIndex] = m_list[m_count];
		channels[m_list[activeIndex]].activeIndex = activeIndex+1;
	}
	pChannel->activeIndex = 0;
}


void CActiveChannels::GetActiveChannels( CChannelList &list )
{
	list.m_count = m_count;
	if ( m_count )
	{
		Q_memcpy( list.m_list, m_list, sizeof(m_list[0])*m_count );
	}

	for ( int i = SOUND_BUFFER_SPECIAL_START; i < g_paintBuffers.Count(); ++i )
	{
		paintbuffer_t *pSpecialBuffer = MIX_GetPPaintFromIPaint( i );
		if ( pSpecialBuffer->nSpecialDSP != 0 )
		{
			list.m_nSpecialDSPs.AddToTail( pSpecialBuffer->nSpecialDSP );
		}
	}

	list.m_hasSpeakerChannels = true;
	list.m_has11kChannels = true;
	list.m_has22kChannels = true;
	list.m_has44kChannels = true;
	list.m_hasDryChannels = true;
}

void CActiveChannels::Init()
{
	m_count = 0;
}

bool				snd_initialized = false;

Vector				listener_origin;
static Vector		listener_forward;
Vector				listener_right;
static Vector		listener_up;
static bool			s_bIsListenerUnderwater;
static vec_t		sound_nominal_clip_dist=SOUND_NORMAL_CLIP_DIST;

// @TODO (toml 05-08-02): put this somewhere more reasonable
vec_t S_GetNominalClipDist()
{
	return sound_nominal_clip_dist;
}

int				g_soundtime = 0;		// sample PAIRS output since start
int   			g_paintedtime = 0; 		// sample PAIRS mixed since start

float			g_ReplaySoundTimeFracAccumulator = 0.0f;	// Used by replay

float			g_ClockSyncArray[NUM_CLOCK_SYNCS] = {0};
int				g_SoundClockPaintTime[NUM_CLOCK_SYNCS] = {0};

// default 10ms
ConVar snd_delay_sound_shift("snd_delay_sound_shift","0.01");
// this forces the clock to resync on the next delayed/sync sound
void S_SyncClockAdjust( clocksync_index_t syncIndex )
{
	g_ClockSyncArray[syncIndex] = 0;
	g_SoundClockPaintTime[syncIndex] = 0;
}

float S_ComputeDelayForSoundtime( float soundtime, clocksync_index_t syncIndex )
{
	// reset clock and return 0
	if ( g_ClockSyncArray[syncIndex] == 0 )
	{
		// Put the current time marker one tick back to impose a minimum delay on the first sample
		// this shifts the drift over so the sounds are more likely to delay (rather than skip)
		// over the burst
		// NOTE: The first sound after a sync MUST have a non-zero delay for the delay channel
		// detection logic to work (otherwise we keep resetting the clock)
		g_ClockSyncArray[syncIndex] = soundtime - host_state.interval_per_tick;
		g_SoundClockPaintTime[syncIndex] = g_paintedtime;
	}

	// how much time has passed in the game since we did a clock sync?
	float gameDeltaTime = soundtime - g_ClockSyncArray[syncIndex];

	// how many samples have been mixed since we did a clock sync?
	int paintedSamples = g_paintedtime - g_SoundClockPaintTime[syncIndex];
	int dmaSpeed = g_AudioDevice->DeviceDmaSpeed();
	int gameSamples = (gameDeltaTime * dmaSpeed);
	int delaySamples = gameSamples - paintedSamples;
	float delay = delaySamples / float(dmaSpeed);

	if ( gameDeltaTime < 0 || fabs(delay) > 0.500f )
	{
		// Note that the equations assume a correlation between game time and real time
		// some kind of clock error.  This can happen with large host_timescale or when the 
		// framerate hitches drastically (game time is a smaller clamped value wrt real time).  
		// The current sync estimate has probably drifted due to this or some other problem, recompute.
		//Msg("Clock ERROR!: %.2f %.2f\n", gameDeltaTime, delay);
		S_SyncClockAdjust(syncIndex);
		return 0;
	}
	return delay + snd_delay_sound_shift.GetFloat();
}

static	int		s_buffers = 0;
static	int		s_oldsampleOutCount = 0;
static	float	s_lastsoundtime = 0.0f;

bool s_bOnLoadScreen = false;

static CClassMemoryPool< CSfxTable > s_SoundPool( MAX_SFX );
struct SfxDictEntry
{
	CSfxTable *pSfx;
};

static CUtlMap< FileNameHandle_t, SfxDictEntry > s_Sounds( 0, 0, DefLessFunc( FileNameHandle_t ) );

class CDummySfx : public CSfxTable
{
public:
	virtual const char *getname()
	{
		return name;
	}

	void setname( const char *pName )
	{
		Q_strncpy( name, pName, sizeof( name ) );
		OnNameChanged(name);
	}

private:
	char name[MAX_PATH];
};

static CDummySfx dummySfx;

// returns true if ok to procede with TraceRay calls
bool SND_IsInGame( void )
{
	return cl.IsActive();
}


CSfxTable::CSfxTable()
{
	m_namePoolIndex = s_Sounds.InvalidIndex();
	pSource = NULL;
	m_bUseErrorFilename = false;
	m_bIsUISound = false;
	m_bIsLateLoad = false;
	m_bMixGroupsCached = false;
	m_pDebugName = NULL;
}


void CSfxTable::SetNamePoolIndex( int index )
{
	m_namePoolIndex = index;
	if ( m_namePoolIndex != s_Sounds.InvalidIndex() )
	{
		OnNameChanged(getname());
	}
#ifdef _DEBUG
	m_pDebugName = strdup( getname() );
#endif
}

void CSfxTable::OnNameChanged( const char *pName )
{
	if ( pName && g_cgrouprules )
	{
		char szString[MAX_PATH];
		Q_strncpy( szString, pName, sizeof(szString) );
		Q_FixSlashes( szString, '/' );
		m_mixGroupCount = MXR_GetMixGroupListFromDirName( szString, m_mixGroupList, ARRAYSIZE(m_mixGroupList) );
		m_bMixGroupsCached = true;
	}
}
//-----------------------------------------------------------------------------
// Purpose: Wrapper for sfxtable->getname()
// Output : char const
//-----------------------------------------------------------------------------
const char *CSfxTable::getname()
{
	if ( s_Sounds.InvalidIndex() != m_namePoolIndex )
	{
		char* pString = tmpstr512();
		if ( g_pFileSystem )
			g_pFileSystem->String( s_Sounds.Key( m_namePoolIndex ), pString, 512 );
		else 
		{
			pString[0] = 0;
		}
		return pString;
	}
	
	return NULL;
}

FileNameHandle_t CSfxTable::GetFileNameHandle()
{
	if ( s_Sounds.InvalidIndex() != m_namePoolIndex )
	{
		return s_Sounds.Key( m_namePoolIndex );
	}
	return NULL;
}

const char *CSfxTable::GetFileName()
{
	if ( IsX360() && m_bUseErrorFilename )
	{
		// Redirecting error sounds to a valid empty wave, prevents a bad loading retry pattern during gameplay
		// which may event sounds skipped by preload, because they don't exist.
		return "common/null.wav";
	}

	const char *pName = getname();
	return pName ? PSkipSoundChars( pName ) : NULL;	
}

bool CSfxTable::IsPrecachedSound()
{
	const char *pName = getname();

	if ( sv.IsActive() )
	{
		// Server uses zero to mark invalid sounds
		return sv.LookupSoundIndex( pName ) != 0 ? true : false;
	}

	// Client uses -1
	// WE SHOULD FIX THIS!!!
	return ( cl.LookupSoundIndex( pName ) != -1 ) ? true : false;
}

float		g_DuckScale = 1.0f;

// Structure used for fading in and out client sound volume.
typedef struct
{
	float		initial_percent;

	// How far to adjust client's volume down by.
	float		percent;  

	// GetHostTime() when we started adjusting volume
	float		starttime;   

	// # of seconds to get to faded out state
	float		fadeouttime; 
    // # of seconds to hold
	float		holdtime;  
	// # of seconds to restore
	float		fadeintime;
} soundfade_t;

static soundfade_t soundfade;  // Client sound fading singleton object

// 0)headphones 2)stereo speakers 4)quad 5)5point1 
// autodetected from windows settings
ConVar snd_surround( "snd_surround_speakers", "-1", FCVAR_INTERNAL_USE );
ConVar snd_legacy_surround( "snd_legacy_surround", "0", FCVAR_ARCHIVE );
ConVar snd_noextraupdate( "snd_noextraupdate", "0" );
ConVar snd_show( "snd_show", "0", FCVAR_CHEAT, "Show sounds info" );
ConVar snd_visualize ("snd_visualize", "0", FCVAR_CHEAT, "Show sounds location in world" );
ConVar snd_pitchquality( "snd_pitchquality", "1", FCVAR_ARCHIVE );		// 1) use high quality pitch shifters

// master volume
static ConVar volume( "volume", "1.0", FCVAR_ARCHIVE | FCVAR_ARCHIVE_XBOX, "Sound volume", true, 0.0f, true, 1.0f );
// user configurable music volume
ConVar snd_musicvolume( "snd_musicvolume", "1.0", FCVAR_ARCHIVE | FCVAR_ARCHIVE_XBOX, "Music volume", true, 0.0f, true, 1.0f );	

ConVar snd_mixahead( "snd_mixahead", "0.1", FCVAR_ARCHIVE );
ConVar snd_mix_async( "snd_mix_async", "0" );
#ifdef _DEBUG
static ConCommand snd_mixvol("snd_mixvol", MXR_DebugSetMixGroupVolume, "Set named Mixgroup to mix volume.");
#endif

// vaudio DLL
IVAudio *vaudio = NULL;
CSysModule *g_pVAudioModule = NULL;

//-----------------------------------------------------------------------------
// Resource loading for sound
//-----------------------------------------------------------------------------
class CResourcePreloadSound : public CResourcePreload
{
public:
	CResourcePreloadSound() : m_SoundNames( 0, 0, true )
	{
	}

	virtual bool CreateResource( const char *pName )
	{
		CSfxTable *pSfx = S_PrecacheSound( pName );
		if ( !pSfx )
		{
			return false;
		}
		
		m_SoundNames.AddString( pSfx->GetFileName() );
		return true;
	}

	virtual void PurgeUnreferencedResources()
	{
		bool bSpew = ( g_pQueuedLoader->GetSpewDetail() & LOADER_DETAIL_PURGES ) != 0;

		for ( int i = s_Sounds.FirstInorder(); i != s_Sounds.InvalidIndex(); i = s_Sounds.NextInorder( i ) )
		{
			// the master sound table grows forever
			// remove sound sources from the master sound table that were not in the preload list
			CSfxTable *pSfx = s_Sounds[i].pSfx;
			if ( pSfx && pSfx->pSource )
			{
				if ( pSfx->m_bIsUISound )
				{
					// never purge ui
					continue;
				}

				UtlSymId_t symbol = m_SoundNames.Find( pSfx->GetFileName() );
				if ( symbol == UTL_INVAL_SYMBOL )
				{
					// sound was not part of preload, purge it
					if ( bSpew )
					{
						Msg( "CResourcePreloadSound: Purging: %s\n", pSfx->GetFileName() );
					}

					pSfx->pSource->CacheUnload();
					delete pSfx->pSource;
					pSfx->pSource = NULL;
				}
			}
		}

		m_SoundNames.RemoveAll();

		if ( !g_pQueuedLoader->IsSameMapLoading() )
		{
			wavedatacache->Flush();
		}
	}

	virtual void PurgeAll()
	{
		bool bSpew = ( g_pQueuedLoader->GetSpewDetail() & LOADER_DETAIL_PURGES ) != 0;

		for ( int i = s_Sounds.FirstInorder(); i != s_Sounds.InvalidIndex(); i = s_Sounds.NextInorder( i ) )
		{
			// the master sound table grows forever
			// remove sound sources from the master sound table that were not in the preload list
			CSfxTable *pSfx = s_Sounds[i].pSfx;
			if ( pSfx && pSfx->pSource )
			{
				if ( pSfx->m_bIsUISound )
				{
					// never purge ui
					if ( bSpew )
					{
						Msg( "CResourcePreloadSound: Skipping: %s\n", pSfx->GetFileName() );
					}
					continue;
				}

				// sound was not part of preload, purge it
				if ( bSpew )
				{
					Msg( "CResourcePreloadSound: Purging: %s\n", pSfx->GetFileName() );
				}
				
				pSfx->pSource->CacheUnload();
				delete pSfx->pSource;
				pSfx->pSource = NULL;
			}
		}

		m_SoundNames.RemoveAll();

		wavedatacache->Flush();
	}
	
private:
	CUtlSymbolTable	m_SoundNames;
};
static CResourcePreloadSound s_ResourcePreloadSound;

//-----------------------------------------------------------------------------
// Purpose: 
// Output : float
//-----------------------------------------------------------------------------
float S_GetMasterVolume( void )
{
	float scale = 1.0f;
	if ( soundfade.percent != 0 )
	{
		scale = clamp( (float)soundfade.percent / 100.0f, 0.0f, 1.0f );
		scale = 1.0f - scale;
	}
	return volume.GetFloat() * scale;
}


void S_SoundInfo_f(void)
{
	if ( !g_AudioDevice->IsActive() )
	{
		Msg( "Sound system not started\n" );
		return;
	}

	Msg( "Sound Device:   %s\n", g_AudioDevice->DeviceName() );
    Msg( "  Channels:     %d\n", g_AudioDevice->DeviceChannels() );
    Msg( "  Samples:      %d\n", g_AudioDevice->DeviceSampleCount() );
    Msg( "  Bits/Sample:  %d\n", g_AudioDevice->DeviceSampleBits() );
    Msg( "  Rate:         %d\n", g_AudioDevice->DeviceDmaSpeed() );
	Msg( "total_channels: %d\n", total_channels);

	if ( IsX360() )
	{
		// dump a glimpse of the mixing state
		CChannelList list;
		g_ActiveChannels.GetActiveChannels( list );

		Msg( "\nActive Channels: (%d)\n", list.Count() );
		for ( int i = 0; i < list.Count(); i++ )
		{
			channel_t *pChannel = list.GetChannel(i);
			Msg( "%s (Mixer: 0x%p)\n", pChannel->sfx->GetFileName(), pChannel->pMixer );
		}
	}
}


/*
================
S_Startup
================
*/

void S_Startup( void )
{
	if ( !snd_initialized )
		return;

	if ( !g_AudioDevice || g_AudioDevice == Audio_GetNullDevice() )
	{
		g_AudioDevice = IAudioDevice::AutoDetectInit( CommandLine()->CheckParm( "-wavonly" ) != 0 );
		if ( !g_AudioDevice )
		{
			Error( "Unable to init audio" );
		}
	}
}

static ConCommand play("play", S_Play, "Play a sound.", FCVAR_SERVER_CAN_EXECUTE );
static ConCommand playflush( "playflush", S_Play, "Play a sound, reloading from disk in case of changes." );
static ConCommand playvol( "playvol", S_PlayVol, "Play a sound at a specified volume." );
static ConCommand speak( "speak", S_Say, "Play a constructed sentence." );
static ConCommand stopsound( "stopsound", S_StopAllSoundsC, 0, FCVAR_CHEAT);		// Marked cheat because it gives an advantage to players minimising ambient noise.
static ConCommand soundlist( "soundlist", S_SoundList, "List all known sounds." );
static ConCommand soundinfo( "soundinfo", S_SoundInfo_f, "Describe the current sound device." );

bool IsValidSampleRate( int rate )
{
	return rate == SOUND_11k || rate == SOUND_22k || rate == SOUND_44k;
}

void VAudioInit()
{
	if ( IsPC() )
	{
		if ( !IsPosix() )
		{
			// vaudio_miles.dll will load this...
			g_pFileSystem->GetLocalCopy( "mss32.dll" );
		}

		g_pVAudioModule = FileSystem_LoadModule( "vaudio_miles" );
		if ( g_pVAudioModule )
		{
			CreateInterfaceFn vaudioFactory = Sys_GetFactory( g_pVAudioModule );
			vaudio = (IVAudio *)vaudioFactory( VAUDIO_INTERFACE_VERSION, NULL );
		}
	}
}

/*
================
S_Init
================
*/
void S_Init( void )
{
	if ( sv.IsDedicated() && !CommandLine()->CheckParm( "-forcesound" ) )
		return;

	DevMsg( "Sound Initialization: Start\n" );

	// KDB: init sentence array
	TRACEINIT( VOX_Init(), VOX_Shutdown() );

	VAudioInit();

	if ( CommandLine()->CheckParm( "-nosound" ) )
	{
		g_AudioDevice = Audio_GetNullDevice();
		TRACEINIT( audiosourcecache->Init( host_parms.memsize >> 2 ), audiosourcecache->Shutdown() );
		return;
	}

	snd_initialized = true;

	g_ActiveChannels.Init();
	S_Startup();

	MIX_InitAllPaintbuffers();

	SND_InitScaletable();

	MXR_LoadAllSoundMixers();

	S_StopAllSounds( true );

	TRACEINIT( audiosourcecache->Init( host_parms.memsize >> 2 ), audiosourcecache->Shutdown() );

	AllocDsps( true );

	if ( IsX360() )
	{
		g_pQueuedLoader->InstallLoader( RESOURCEPRELOAD_SOUND, &s_ResourcePreloadSound );
	}

	DevMsg( "Sound Initialization: Finish, Sampling Rate: %i\n", g_AudioDevice->DeviceDmaSpeed() );

#ifdef _X360
	BOOL bPlaybackControl;
	// get initial state of the x360 media player
	if ( XMPTitleHasPlaybackControl( &bPlaybackControl ) == ERROR_SUCCESS )
	{
		S_EnableMusic(bPlaybackControl!=0);
	}

	Assert( g_pVideo != NULL );
	
	if ( g_pVideo != NULL )
	{
		if ( g_pVideo->SoundDeviceCommand( VideoSoundDeviceOperation::HOOK_X_AUDIO, NULL ) != VideoResult::SUCCESS )
		{
			Assert( 0 );
		}
	}
#endif
}


// =======================================================================
// Shutdown sound engine
// =======================================================================
void S_Shutdown(void)
{
#if !defined( _X360 )
	if ( VoiceTweak_IsStillTweaking() )
	{
		VoiceTweak_EndVoiceTweakMode();
	}
#endif

	S_StopAllSounds( true );
	S_ShutdownMixThread();

	TRACESHUTDOWN( audiosourcecache->Shutdown() );

	SNDDMA_Shutdown();

	for ( int i = s_Sounds.FirstInorder(); i != s_Sounds.InvalidIndex(); i = s_Sounds.NextInorder( i ) )
	{
		if ( s_Sounds[i].pSfx )
		{
			delete s_Sounds[i].pSfx->pSource;
			s_Sounds[i].pSfx->pSource = NULL;
		}
	}
	s_Sounds.RemoveAll();
	s_SoundPool.Clear();

	// release DSP resources
	FreeDsps( true );

	MXR_ReleaseMemory();

	// release sentences resources
	TRACESHUTDOWN( VOX_Shutdown() );
	
	if ( IsPC() )
	{
		// shutdown vaudio
		if ( vaudio )
			delete vaudio;
		FileSystem_UnloadModule( g_pVAudioModule );
		g_pVAudioModule = NULL;
		vaudio = NULL;
	}

	MIX_FreeAllPaintbuffers();
	snd_initialized = false;
	g_paintedtime = 0;
	g_soundtime = 0;
	g_ReplaySoundTimeFracAccumulator = 0.0f;
	s_buffers = 0;
	s_oldsampleOutCount = 0;
	s_lastsoundtime = 0.0f;
#if !defined( _X360 )
	Voice_Deinit();
#endif
}

bool S_IsInitted()
{
	return snd_initialized;
}

// =======================================================================
// Load a sound
// =======================================================================

//-----------------------------------------------------------------------------
// Return sfx and set pfInCache to 1 if 
// name is in name cache. Otherwise, alloc
// a new spot in name cache and return 0 
// in pfInCache.
//-----------------------------------------------------------------------------
CSfxTable *S_FindName( const char *szName, int *pfInCache )
{
	int			i;
	CSfxTable	*sfx = NULL;
	char		szBuff[MAX_PATH];
	const char	*pName;

	if ( !szName )
	{
		Error( "S_FindName: NULL\n" );
	}

	pName = szName;
	if ( IsX360() )
	{
		Q_strncpy( szBuff, pName, sizeof( szBuff ) );
		int len = Q_strlen( szBuff )-4;
		if ( len > 0 && !Q_strnicmp( szBuff+len, ".mp3", 4 ) )
		{
			// convert unsupported .mp3 to .wav
			Q_strcpy( szBuff+len, ".wav" );
		}
		pName = szBuff;

		if ( pName[0] == CHAR_STREAM )
		{
			// streaming (or not) is hardcoded to alternate criteria
			// prevent the same sound from creating disparate instances
			pName++;
		}
	}

	// see if already loaded
	FileNameHandle_t fnHandle = g_pFileSystem->FindOrAddFileName( pName );
	i = s_Sounds.Find( fnHandle );
	if ( i != s_Sounds.InvalidIndex() )
	{
		sfx = s_Sounds[i].pSfx;
		Assert( sfx );
		if ( pfInCache )
		{
			// indicate whether or not sound is currently in the cache.
			*pfInCache = ( sfx->pSource && sfx->pSource->IsCached() ) ? 1 : 0;
		}
		return sfx;
	}
	else
	{
		SfxDictEntry entry;
		entry.pSfx = ( CSfxTable * )s_SoundPool.Alloc();

		Assert( entry.pSfx );

		i = s_Sounds.Insert( fnHandle, entry );
		sfx = s_Sounds[i].pSfx;

		sfx->SetNamePoolIndex( i );
		sfx->pSource = NULL;

		if ( pfInCache )
		{
			*pfInCache = 0;
		}
	}
	return sfx;
}

//-----------------------------------------------------------------------------
// S_LoadSound
//
// Check to see if wave data is in the cache. If so, return pointer to data.
// If not, allocate cache space for wave data, load wave file into temporary heap
// space, and dump/convert file data into cache.
//-----------------------------------------------------------------------------
double g_flAccumulatedSoundLoadTime = 0.0f;
CAudioSource *S_LoadSound( CSfxTable *pSfx, channel_t *ch )
{
	tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );

	VPROF("S_LoadSound");
	if ( !pSfx->pSource )
	{
		if ( IsX360() )
		{
			if ( SND_IsInGame() && !g_pQueuedLoader->IsMapLoading() )
			{
				// sound should be present (due to reslists), but NOT allowing a load hitch during gameplay 
				// loading a sound during gameplay is a bad experience, causes a very expensive sync i/o to fetch the header
				// and in the case of a memory wave, the actual audio data
				bool bFound = false;
				if ( !pSfx->m_bIsLateLoad )
				{
					if ( pSfx->getname() != PSkipSoundChars( pSfx->getname() ) )
					{
						// the sound might already exist as an undecorated audio source
						FileNameHandle_t fnHandle = g_pFileSystem->FindOrAddFileName( pSfx->GetFileName() );
						int i = s_Sounds.Find( fnHandle );
						if ( i != s_Sounds.InvalidIndex() )
						{
							CSfxTable *pOtherSfx = s_Sounds[i].pSfx;
							Assert( pOtherSfx );
							CAudioSource *pOtherSource = pOtherSfx->pSource;
							if ( pOtherSource && pOtherSource->IsCached() )
							{
								// Can safely let the "load" continue because the headers are expected to be in the preload
								// that are now persisted and the wave data cache will find an existing audio buffer match,
								// so no sync i/o should occur from either.
								bFound = true;
							}
						}
					}

					if ( !bFound )
					{
						// warn once
						DevWarning( "S_LoadSound: Late load '%s', skipping.\n", pSfx->getname() ); 
						pSfx->m_bIsLateLoad = true;
					}
				}

				if ( !bFound )
				{
					return NULL;
				}
			}
			else if ( pSfx->m_bIsLateLoad )
			{
				// outside of gameplay, let the load happen
				pSfx->m_bIsLateLoad = false;
			}
		}

		double st = Plat_FloatTime();

		bool bStream = false;
		bool bUserVox = false;

		// sound chars can explicitly categorize usage
		bStream = TestSoundChar( pSfx->getname(), CHAR_STREAM );
		if ( !bStream )
		{
			bUserVox = TestSoundChar( pSfx->getname(), CHAR_USERVOX );
		}

		// override streaming
		if ( IsX360() )
		{
			const char *s_CriticalSounds[] = 
			{
				"common",
				"items",
				"physics",
				"player",
				"ui",
				"weapons",
			};

			// stream everything but critical sounds
			bStream = true;
			const char *pFileName = pSfx->GetFileName();
			for ( int i = 0; i<ARRAYSIZE( s_CriticalSounds ); i++ )
			{
				size_t len = strlen( s_CriticalSounds[i] );
				if ( !Q_strnicmp( pFileName, s_CriticalSounds[i], len ) && ( pFileName[len] == '\\' || pFileName[len] == '/' ) )
				{
					// never stream these, regardless of sound chars
					bStream = false;
					break;
				}
			}
		}

		if ( bStream )
		{
			// setup as a streaming resource
			pSfx->pSource = Audio_CreateStreamedWave( pSfx );
		}
		else
		{
			if ( bUserVox )
			{
				if ( !IsX360() )
				{
					pSfx->pSource = Voice_SetupAudioSource( ch->soundsource, ch->entchannel );
				}
				else
				{
					// not supporting
					Assert( 0 );
				}
			}
			else
			{
				// load all into memory directly
				pSfx->pSource = Audio_CreateMemoryWave( pSfx );
			}
		}

		double ed = Plat_FloatTime();
		g_flAccumulatedSoundLoadTime += ( ed - st );
	}
	else 
	{
		pSfx->pSource->CheckAudioSourceCache();
	}

	if ( !pSfx->pSource )
	{
		return NULL;
	}

	// first time to load?  Create the mixer
	if ( ch && !ch->pMixer )
	{
		ch->pMixer = pSfx->pSource->CreateMixer( ch->initialStreamPosition );
		if ( !ch->pMixer )
		{
			return NULL;
		}
	}

	return pSfx->pSource;
}

//-----------------------------------------------------------------------------
//	S_PrecacheSound
//
//	Reserve space for the name of the sound in a global array.
//	Load the data for the non-streaming sound. Streaming sounds
//	defer loading of data until just before playback.
//-----------------------------------------------------------------------------
CSfxTable *S_PrecacheSound( const char *name )
{
	tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );

	if ( !g_AudioDevice )
		return NULL;

	if ( !g_AudioDevice->IsActive() )
		return NULL;

	CSfxTable *sfx = S_FindName( name, NULL );
	if ( sfx )
	{
		// cache sound
		S_LoadSound( sfx, NULL );
	}
	else
	{
		Assert( !"S_PrecacheSound:  Failed to create sfx" );
	}

	return sfx;
}


void S_InternalReloadSound( CSfxTable *sfx )
{
	if ( !sfx || !sfx->pSource )
		return;

	sfx->pSource->CacheUnload();

	delete sfx->pSource;
	sfx->pSource = NULL;

	char pExt[10];
	Q_ExtractFileExtension( sfx->getname(), pExt, sizeof(pExt) );
	int nSource = !Q_stricmp( pExt, "mp3" ) ? CAudioSource::AUDIO_SOURCE_MP3 : CAudioSource::AUDIO_SOURCE_WAV;
//	audiosourcecache->RebuildCacheEntry( nSource, sfx->IsPrecachedSound(), sfx );
	audiosourcecache->GetInfo( nSource, sfx->IsPrecachedSound(), sfx ); // Do a size/date check and rebuild the cache entry if necessary.
}


//-----------------------------------------------------------------------------
//	Refresh a sound in the cache
//-----------------------------------------------------------------------------
void S_ReloadSound( const char *name )
{
	if ( IsX360() )
	{
		// not supporting
		Assert( 0 );
		return;
	}

	if ( !g_AudioDevice )
		return;

	if ( !g_AudioDevice->IsActive() )
		return;

	CSfxTable *sfx = S_FindName( name, NULL );
#ifdef _DEBUG
	if ( sfx )
	{
		Assert( Q_stricmp( sfx->getname(), name ) == 0 );
	}
#endif
	
	S_InternalReloadSound( sfx );
}


// See comments on CL_HandlePureServerWhitelist for details of what we're doing here.
void S_ReloadFilesInList( IFileList *pFilesToReload )
{
	if ( !IsPC() )
		return;

	S_StopAllSounds( true );
	wavedatacache->Flush();
	audiosourcecache->ForceRecheckDiskInfo();	// Force all cached audio data to recheck size/date info next time it's accessed.
	

	CUtlVector< CSfxTable * > processed;

	int iLast = s_Sounds.LastInorder();
	for ( int i = s_Sounds.FirstInorder(); i != iLast; i = s_Sounds.NextInorder( i ) )
	{
		FileNameHandle_t fnHandle = s_Sounds.Key( i );
		char filename[MAX_PATH * 3];
		if ( !g_pFileSystem->String( fnHandle, filename, sizeof( filename ) ) )
		{
			Assert( !"S_HandlePureServerWhitelist - can't get a filename." );
			continue;
		}
	
		// If the file isn't cached in yet, then the filesystem hasn't touched its file, so don't bother.
		CSfxTable *sfx = s_Sounds[i].pSfx;
		if ( sfx && ( processed.Find( sfx ) == processed.InvalidIndex() ) )
		{
			char fullFilename[MAX_PATH*2];
			if ( IsSoundChar( filename[0] ) )
				Q_snprintf( fullFilename, sizeof( fullFilename ), "sound/%s", &filename[1] );
			else
				Q_snprintf( fullFilename, sizeof( fullFilename ), "sound/%s", filename );

			
			if ( !pFilesToReload->IsFileInList( fullFilename ) )
				continue;

			processed.AddToTail( sfx );

			S_InternalReloadSound( sfx );
		}
	}
}

//-----------------------------------------------------------------------------
// Unfortunate confusing terminology.
// Here prefetching means hinting to the audio source (which may be a stream)
// to get its async data in flight.
//-----------------------------------------------------------------------------
void S_PrefetchSound( char const *name, bool bPlayOnce )
{
	CSfxTable	*sfx;

	if ( !g_AudioDevice )
		return;

	if ( !g_AudioDevice->IsActive() )
		return;

	sfx = S_FindName( name, NULL );
	if ( sfx )
	{
		// cache sound
		S_LoadSound( sfx, NULL );
	}

	if ( !sfx || !sfx->pSource )
	{
		return;
	}

	// hint the sound to start loading
	sfx->pSource->Prefetch();

	if ( bPlayOnce )
	{
		sfx->pSource->SetPlayOnce( true );
	}
}

void S_MarkUISound( CSfxTable *pSfx )
{
	pSfx->m_bIsUISound = true;
}

unsigned int RemainingSamples( channel_t *pChannel )
{
	if ( !pChannel || !pChannel->sfx || !pChannel->sfx->pSource )
		return 0;

	unsigned int timeleft = pChannel->sfx->pSource->SampleCount();

	if ( pChannel->sfx->pSource->IsLooped() )
	{
		return pChannel->sfx->pSource->SampleRate();
	}

	if ( pChannel->pMixer )
	{
		timeleft -= pChannel->pMixer->GetSamplePosition();
	}

	return timeleft;
}

// chooses the voice stealing algorithm
ConVar voice_steal("voice_steal", "2");

/*
=================
SND_StealDynamicChannel
Select a channel from the dynamic channel allocation area.  For the given entity, 
override any other sound playing on the same channel (see code comments below for
exceptions).
=================
*/
channel_t *SND_StealDynamicChannel(SoundSource soundsource, int entchannel, const Vector &origin, CSfxTable *sfx, float flDelay, bool bDoNotOverwriteExisting)
{
	int canSteal[MAX_DYNAMIC_CHANNELS];
	int canStealCount = 0;

	int sameSoundCount = 0;
	unsigned int sameSoundRemaining = 0xFFFFFFFF;
	int sameSoundIndex = -1;
	int sameVol = 0xFFFF;
	int availableChannel = -1;
	bool bDelaySame = false;

	int nExactMatch[MAX_DYNAMIC_CHANNELS];
	int nExactCount = 0;
	// first pass to replace sounds on same ent/channel, and search for free or stealable channels otherwise
	for ( int ch_idx = 0; ch_idx < MAX_DYNAMIC_CHANNELS ; ch_idx++)
	{
		channel_t *ch = &channels[ch_idx];
		
		if ( ch->activeIndex )
		{
			// channel CHAN_AUTO never overrides sounds on same channel
			if ( entchannel != CHAN_AUTO )
			{
				int checkChannel = entchannel;
				if ( checkChannel == -1 )
				{
					if ( ch->entchannel != CHAN_STREAM && ch->entchannel != CHAN_VOICE && ch->entchannel != CHAN_VOICE2 )
					{
						checkChannel = ch->entchannel;
					}
				}
				if ( ch->soundsource == soundsource && (soundsource != -1) && ch->entchannel == checkChannel )
				{
					// we found an exact match for this entity and this channel, but the sound we want to play is considered
					// low priority so instead of stomping this entry pretend we couldn't find a free slot to play and let
					// the existing sound keep going
					if ( bDoNotOverwriteExisting )
						return NULL;

					if ( ch->flags.delayed_start )
					{
						nExactMatch[nExactCount] = ch_idx;
						nExactCount++;
						continue;
					}
					return ch;	// always override sound from same entity
				}
			}

			// Never steal the channel of a streaming sound that is currently playing or
			// voice over IP data that is playing or any sound on CHAN_VOICE( acting )
			if ( ch->entchannel == CHAN_STREAM || ch->entchannel == CHAN_VOICE || ch->entchannel == CHAN_VOICE2 )
				continue;

			// don't let monster sounds override player sounds
			if ( g_pSoundServices->IsPlayer( ch->soundsource ) && !g_pSoundServices->IsPlayer(soundsource) )
				continue;

			if ( ch->sfx == sfx )
			{
				bDelaySame = ch->flags.delayed_start ? true : bDelaySame;
				sameSoundCount++;
				int maxVolume = ChannelGetMaxVol( ch );
				unsigned int remaining = RemainingSamples(ch);
				if ( maxVolume < sameVol || (maxVolume == sameVol && remaining < sameSoundRemaining) )
				{
					sameSoundIndex = ch_idx;
					sameVol = maxVolume;
					sameSoundRemaining = remaining;
				}
			}
			canSteal[canStealCount++] = ch_idx;
		}
		else
		{
			if ( availableChannel < 0 )
			{
				availableChannel = ch_idx;
			}
		}
	}


	// coalesce the timeline for this channel
	if ( nExactCount > 0 )
	{
		uint nFreeSampleTime = g_paintedtime + (flDelay * SOUND_DMA_SPEED);
		channel_t *pReturn = &channels[nExactMatch[0]];
		uint nMinRemaining = RemainingSamples( pReturn );
		if ( pReturn->nFreeChannelAtSampleTime == 0 || pReturn->nFreeChannelAtSampleTime > nFreeSampleTime )
		{
			pReturn->nFreeChannelAtSampleTime = nFreeSampleTime;
		}
		for ( int i = 1; i < nExactCount; i++ )
		{
			channel_t *pChannel = &channels[nExactMatch[i]];
			if ( pChannel->nFreeChannelAtSampleTime == 0 || pChannel->nFreeChannelAtSampleTime > nFreeSampleTime )
			{
				pChannel->nFreeChannelAtSampleTime = nFreeSampleTime;
			}
			uint nRemain = RemainingSamples( pChannel );
			if ( nRemain < nMinRemaining )
			{
				pReturn = pChannel;
				nMinRemaining = nRemain;
			}
		}
		// if there's only one, mark it to be freed but don't reuse it.
		// otherwise mark all others to be freed and use the closest one to being done
		if ( nExactCount > 1 )
		{
			return pReturn;
		}
	}

	// Limit the number of times a given sfx/wave can play simultaneously
	if ( voice_steal.GetInt() > 1 && sameSoundIndex >= 0 )
	{
		// if sounds of this type are normally delayed, then add an extra slot for stealing
		// NOTE: In HL2 these are usually NPC gunshot sounds - and stealing too soon will cut
		// them off early.  This is a safe heuristic to avoid that problem.  There's probably a better
		// long-term solution involving only counting channels that are actually going to play (delay included)
		// at the same time as this one.
		int maxSameSounds = bDelaySame ? 5 : 4;
		float distSqr = 0.0f;
		if ( sfx->pSource )
		{
			distSqr = origin.DistToSqr(listener_origin);
			if ( sfx->pSource->IsLooped() )
			{
				maxSameSounds = 3;
			}
		}

		// don't play more than N copies of the same sound, steal the quietest & closest one otherwise
		if ( sameSoundCount >= maxSameSounds )
		{
			channel_t *ch = &channels[sameSoundIndex];
			// you're already playing a closer version of this sound, don't steal
			if ( distSqr > 0.0f && ch->origin.DistToSqr(listener_origin) < distSqr && entchannel != CHAN_WEAPON )
				return NULL;

			//Msg("Sound playing %d copies, stole %s (%d)\n", sameSoundCount, ch->sfx->getname(), sameVol );
			return ch;
		}
	}

	// if there's a free channel, just take that one - don't steal
	if ( availableChannel >= 0 )
		return &channels[availableChannel];

	// Still haven't found a suitable channel, so choose the one with the least amount of time left to play
	float life_left = FLT_MAX;
	int first_to_die = -1;
	bool bAllowVoiceSteal = voice_steal.GetBool();

	for ( int i = 0; i < canStealCount; i++ )
	{
		int ch_idx = canSteal[i];
		channel_t *ch = &channels[ch_idx];
		float timeleft = 0;
		if ( bAllowVoiceSteal )
		{
			int maxVolume = ChannelGetMaxVol( ch );
			if ( maxVolume < 5 )
			{
				//Msg("Sound quiet, stole %s for %s\n", ch->sfx->getname(), sfx->getname() );
				return ch;
			}

			if ( ch->sfx && ch->sfx->pSource )
			{
				unsigned int sampleCount = RemainingSamples( ch );
				timeleft = (float)sampleCount / (float)ch->sfx->pSource->SampleRate();
			}
		}
		else
		{
			// UNDONE: Kill this when voice_steal 0,1,2 has been tested
			// UNDONE: This is the old buggy code that we're trying to replace
			if ( ch->sfx )
			{
				// basically steals the first one you come to
				timeleft = 1;	//ch->end - paintedtime
			}
		}

		if ( timeleft < life_left )
		{
			life_left = timeleft;
			first_to_die = ch_idx;
		}
	}
	if ( first_to_die >= 0 )
	{
		//Msg("Stole %s, timeleft %d\n", channels[first_to_die].sfx->getname(), life_left );
		return &channels[first_to_die];
	}

	return NULL;
}

channel_t *SND_PickDynamicChannel(SoundSource soundsource, int entchannel, const Vector &origin, CSfxTable *sfx, float flDelay, bool bDoNotOverwriteExisting)
{
	channel_t *pChannel = SND_StealDynamicChannel( soundsource, entchannel, origin, sfx, flDelay, bDoNotOverwriteExisting );
	if ( !pChannel )
		return NULL;

	if (pChannel->sfx)
	{
		// Don't restart looping sounds for the same entity
		CAudioSource *pSource = pChannel->sfx->pSource;
		if ( pSource )
		{
			if ( pSource->IsLooped() )
			{
				if ( pChannel->soundsource == soundsource && pChannel->entchannel == entchannel && pChannel->sfx == sfx )
				{
					// same looping sound, same ent, same channel, don't restart the sound
					return NULL;
				}
			}
		}
		// be sure and release previous channel
		// if sentence.
		//	("Stealing channel from %s\n", channels[first_to_die].sfx->getname() );
		S_FreeChannel(pChannel);
	}

	return pChannel;
}

  			

/*
=====================
SND_PickStaticChannel
=====================
Pick an empty channel from the static sound area, or allocate a new
channel.  Only fails if we're at max_channels (128!!!) or if 
we're trying to allocate a channel for a stream sound that is 
already playing.

*/
channel_t *SND_PickStaticChannel(int soundsource, CSfxTable *pSfx)
{
	int i;
	channel_t *ch = NULL;

	// Check for replacement sound, or find the best one to replace
 	for (i = MAX_DYNAMIC_CHANNELS; i<total_channels; i++)
		if (channels[i].sfx == NULL)
			break;

	if (i < total_channels) 
	{
		// reuse an empty static sound channel
		ch = &channels[i];
	}
	else
	{
		// no empty slots, alloc a new static sound channel
		if (total_channels == MAX_CHANNELS)
		{
			DevMsg ("total_channels == MAX_CHANNELS\n");
			return NULL;
		}

		// get a channel for the static sound
		ch = &channels[total_channels];
		total_channels++;
	}

	return ch;
}


void S_SpatializeChannel( int pVolume[CCHANVOLUMES/2], int master_vol, const Vector *psourceDir, float gain, float mono )
{
	float lscale, rscale, scale;
	vec_t dotRight;
	Vector sourceDir = *psourceDir;

	dotRight = DotProduct(listener_right, sourceDir);

	// clear volumes
	for (int i = 0; i < CCHANVOLUMES/2; i++)
		pVolume[i] = 0;

	if (mono > 0.0)
	{
		// sound has radius, within which spatialization becomes mono:

		// mono is 0.0 -> 1.0, from radius 100% to radius 50%

		// at radius * 0.5, dotRight is 0 (ie: sound centered left/right)
		// at radius * 1.0, dotRight == dotRight

		dotRight   *= (1.0 - mono);
	}

	rscale = 1.0 + dotRight;
	lscale = 1.0 - dotRight;

 // add in distance effect
	scale = gain * rscale / 2;
	pVolume[IFRONT_RIGHT] = (int) (master_vol * scale);

	scale = gain * lscale / 2;
	pVolume[IFRONT_LEFT] = (int) (master_vol * scale);

	pVolume[IFRONT_RIGHT] = clamp( pVolume[IFRONT_RIGHT], 0, 255 );
	pVolume[IFRONT_LEFT] = clamp( pVolume[IFRONT_LEFT], 0, 255 );

}

bool S_IsMusic( channel_t *pChannel )
{
	if ( !pChannel->flags.bdry )
		return false;

	CSfxTable *sfx = pChannel->sfx;
	if ( !sfx )
		return false;

	CAudioSource *source = sfx->pSource;
	if ( !source )
		return false;

	// Don't save restore looping sounds as you can end up with an entity restarting them again and have 
	//  them accumulate, etc.
	if ( source->IsLooped() )
		return false;

	CAudioMixer *pMixer = pChannel->pMixer;
	if ( !pMixer )
		return false;

	for ( int i = 0; i < 8; i++ )
	{
		if ( pChannel->mixgroups[i] != -1 )
		{
			char *pGroupName = MXR_GetGroupnameFromId( pChannel->mixgroups[i] );
			if ( !Q_strcmp( pGroupName, "Music" ) )
			{
				return true;
			}
		}
	}
	return false;
}

//-----------------------------------------------------------------------------
// Purpose: For save/restore of currently playing music
// Input  : list - 
//-----------------------------------------------------------------------------
void S_GetCurrentlyPlayingMusic( CUtlVector< musicsave_t >& musiclist )
{
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( int i = 0; i < list.Count(); i++ )
	{
		channel_t *pChannel = &channels[list.GetChannelIndex(i)];
		if ( !S_IsMusic( pChannel ) )
			continue;

		musicsave_t song;
		Q_strncpy( song.songname, pChannel->sfx->getname(), sizeof( song.songname ) );
		song.sampleposition = pChannel->pMixer->GetPositionForSave();
		song.master_volume = pChannel->master_vol;

		musiclist.AddToTail( song );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *song - 
//-----------------------------------------------------------------------------
void S_RestartSong( const musicsave_t *song )
{
	Assert( song );

	// Start the song
	CSfxTable *pSound = S_PrecacheSound( song->songname );
	if ( pSound )
	{
		StartSoundParams_t params;
		params.staticsound = true;
		params.soundsource = SOUND_FROM_WORLD;
		params.entchannel = CHAN_STATIC;
		params.pSfx = pSound;
		params.origin = vec3_origin;
		params.fvol = ( (float)song->master_volume / 255.0f );
		params.soundlevel = SNDLVL_NONE;
		params.flags = SND_NOFLAGS;
		params.pitch = PITCH_NORM;
		params.initialStreamPosition = song->sampleposition;

		S_StartSound( params );

		if ( IsPC() )
		{
			// Now find the channel this went on and skip ahead in the mixer
			for (int i = 0; i < total_channels; i++)
			{
				channel_t *ch = &channels[i]; 

				if ( !ch->pMixer ||
					 !ch->pMixer->GetSource() )
				{
					continue;
				}

				if ( ch->pMixer->GetSource() != pSound->pSource )
				{
					continue;
				}

				ch->pMixer->SetPositionFromSaved( song->sampleposition );
				break;
			}
		}
	}
}

soundlevel_t SND_GetSndlvl ( channel_t *pchannel );

// calculate ammount of sound to be mixed to dsp, based on distance from listener


ConVar dsp_dist_min("dsp_dist_min", "0.0", FCVAR_DEMO|FCVAR_CHEAT);		// range at which sounds are mixed at dsp_mix_min
ConVar dsp_dist_max("dsp_dist_max", "1440.0", FCVAR_DEMO|FCVAR_CHEAT);	// range at which sounds are mixed at dsp_mix_max	

ConVar dsp_mix_min("dsp_mix_min", "0.2", FCVAR_DEMO );		// dsp mix at dsp_dist_min distance "near"
ConVar dsp_mix_max("dsp_mix_max", "0.8", FCVAR_DEMO );		// dsp mix at dsp_dist_max distance "far"
ConVar dsp_db_min("dsp_db_min", "80", FCVAR_DEMO );			// sounds with sndlvl below this get dsp_db_mixdrop % less dsp mix
ConVar dsp_db_mixdrop("dsp_db_mixdrop", "0.5", FCVAR_DEMO );	// sounds with sndlvl below dsp_db_min get dsp_db_mixdrop % less mix

float	DSP_ROOM_MIX	= 1.0;	// mix volume of dsp_room sounds when added back to 'dry' sounds
float	DSP_NOROOM_MIX	= 1.0;	// mix volume of facing + facing away sounds. added to dsp_room_mix sounds

extern ConVar dsp_off;

// returns 0-1.0 dsp mix value.  If sound source is at a range >= DSP_DIST_MAX, return a mix value of
// DSP_MIX_MAX.  This mix value is used later to determine wet/dry mix ratio of sounds.

// This ramp changes with db level of sound source,  and is set in the dsp room presets by room size
// empirical data: 0.78 is nominal mix for sound 100% at far end of room, 0.24 is mix for sound 25% into room

float SND_GetDspMix( channel_t *pchannel, int idist)
{
	float mix;
	float dist = (float)idist;
	float dist_min = dsp_dist_min.GetFloat();
	float dist_max = dsp_dist_max.GetFloat();
	float mix_min;
	float mix_max;

	// only set dsp mix_min & mix_max when sound is first started

	if ( pchannel->dsp_mix_min < 0 && pchannel->dsp_mix_max < 0 )
	{
		mix_min  = dsp_mix_min.GetFloat();		// set via dsp_room preset
		mix_max  = dsp_mix_max.GetFloat();		// set via dsp_room preset

		// set mix_min & mix_max based on db level of sound:
		// sounds below dsp_db_min decrease dsp_mix_min & dsp_mix_max by N%
		// ie: quiet sounds get less dsp mix than loud sounds

		soundlevel_t sndlvl = SND_GetSndlvl( pchannel ); 
		soundlevel_t sndlvl_min = (soundlevel_t)(dsp_db_min.GetInt());
		
		if (sndlvl <= sndlvl_min)
		{
			mix_min *= dsp_db_mixdrop.GetFloat();
			mix_max *= dsp_db_mixdrop.GetFloat();
		}

		pchannel->dsp_mix_min = mix_min;
		pchannel->dsp_mix_max = mix_max;
	}
	else
	{
		mix_min = pchannel->dsp_mix_min;
		mix_max = pchannel->dsp_mix_max;
	}

	// dspmix is 0 (100% mix to facing buffer) if dsp_off

	if ( dsp_off.GetInt() )
		return 0.0;

	// doppler wavs are mixed dry

	if ( pchannel->wavtype == CHAR_DOPPLER )
		return 0.0;

	// linear ramp - get dry mix %
	
	// dist: 0->(max - min)

	dist = clamp( dist, dist_min, dist_max ) - dist_min;

	// dist: 0->1.0

	dist = dist / (dist_max - dist_min);

	// mix: min->max

	mix = ((mix_max - mix_min) * dist) + mix_min;
				
	return mix;
}

// calculate crossfade between wav left (close sound) and wav right (far sound) based on
// distance fron listener

#define DVAR_DIST_MIN	(20.0  * 12.0)		// play full 'near' sound at 20' or less
#define DVAR_DIST_MAX	(110.0 * 12.0)		// play full 'far' sound at 110' or more
#define DVAR_MIX_MIN	0.0
#define DVAR_MIX_MAX	1.0

// calculate mixing parameter for CHAR_DISTVAR wavs
// returns 0 - 1.0, 1.0 is 100% far sound (wav right)

float SND_GetDistanceMix( channel_t *pchannel, int idist)
{
	float mix;
	float dist = (float)idist;
	
	// doppler wavs are 100% near - their spatialization is calculated later.

	if ( pchannel->wavtype == CHAR_DOPPLER )
		return 0.0;

	// linear ramp - get dry mix %
	
	// dist 0->(max - min)

	dist = clamp( dist, (float) DVAR_DIST_MIN, (float) DVAR_DIST_MAX ) - (float) DVAR_DIST_MIN;

	// dist 0->1.0

	dist = dist / (DVAR_DIST_MAX - DVAR_DIST_MIN);

	// mix min->max

	mix = ((DVAR_MIX_MAX - DVAR_MIX_MIN) * dist) + DVAR_MIX_MIN;
	
	return mix;
}

// given facing direction of source, and channel, 
// return -1.0 - 1.0, where -1.0 is source facing away from listener
// and 1.0 is source facing listener


float SND_GetFacingDirection( channel_t *pChannel, const QAngle &source_angles )
{
	Vector SF;				// sound source forward direction unit vector
	Vector SL;				// sound -> listener unit vector
	float dotSFSL;

	// no facing direction unless wavtyp CHAR_DIRECTIONAL

	if ( pChannel->wavtype != CHAR_DIRECTIONAL )
		return 1.0;
	
	VectorSubtract(listener_origin, pChannel->origin, SL);
	VectorNormalize(SL);

	// compute forward vector for sound entity

	AngleVectors( source_angles, &SF, NULL, NULL );

	// dot source forward unit vector with source to listener unit vector to get -1.0 - 1.0 facing.
	// ie: projection of SF onto SL

	dotSFSL = DotProduct( SF, SL );
		
	return dotSFSL;
}

// calculate point of closest approach - caller must ensure that the 
// forward facing vector of the entity playing this sound points in exactly the direction of 
// travel of the sound. ie: for bullets or tracers, forward vector must point in traceline direction.
// return true if sound is to be played, false if sound cannot be heard (shot away from player)

bool SND_GetClosestPoint( channel_t *pChannel, QAngle &source_angles, Vector &vnearpoint )
{
	// S - sound source origin
	// L - listener origin
	
	Vector SF;				// sound source forward direction unit vector
	Vector SL;				// sound -> listener vector
	Vector SD;				// sound->closest point vector
	vec_t dSLSF;			// magnitude of project of SL onto SF

	// P = SF (SF . SL) + S

	// only perform this calculation for doppler wavs

	if ( pChannel->wavtype != CHAR_DOPPLER )
		return false;

	// get vector 'SL' from sound source to listener

	VectorSubtract(listener_origin, pChannel->origin, SL);

	// compute sound->forward vector 'SF' for sound entity

	AngleVectors( source_angles, &SF );
	VectorNormalize( SF );
	
	dSLSF = DotProduct( SL, SF );


	if ( dSLSF <= 0 && !toolframework->IsToolRecording() )
	{
		// source is pointing away from listener, don't play anything
		// unless we're recording in the tool, since we may play back from in front of the source
		return false;
	}
		
	// project dSLSF along forward unit vector from sound source
	
	VectorMultiply( SF, dSLSF, SD );

	// output vector - add SD to sound source origin

	VectorAdd( SD, pChannel->origin, vnearpoint );

	return true;
}


// given point of nearest approach and sound source facing angles, 
// return vector pointing into quadrant in which to play 
// doppler left wav (incomming) and doppler right wav (outgoing).

// doppler left is point in space to play left doppler wav
// doppler right is point in space to play right doppler wav

// Also modifies channel pitch based on distance to nearest approach point

#define DOPPLER_DIST_LEFT_TO_RIGHT	(4*12)		// separate left/right sounds by 4'

#define DOPPLER_DIST_MAX			(20*12)		// max distance - causes min pitch
#define DOPPLER_DIST_MIN			(1*12)		// min distance - causes max pitch
#define DOPPLER_PITCH_MAX			1.5			// max pitch change due to distance
#define DOPPLER_PITCH_MIN			0.25		// min pitch change due to distance

#define DOPPLER_RANGE_MAX			(10*12)		// don't play doppler wav unless within this range
												// UNDONE: should be set by caller!

void SND_GetDopplerPoints( channel_t *pChannel, QAngle &source_angles, Vector &vnearpoint, Vector &source_doppler_left, Vector &source_doppler_right)
{
	Vector SF;			// direction sound source is facing (forward)
	Vector LN;			// vector from listener to closest approach point
	Vector DL;
	Vector DR;

	// nearpoint is closest point of approach, when playing CHAR_DOPPLER sounds

	// SF is normalized vector in direction sound source is facing

	AngleVectors( source_angles, &SF );
	VectorNormalize( SF );

	// source_doppler_left - location in space to play doppler left wav (incomming)
	// source_doppler_right	- location in space to play doppler right wav (outgoing)
	
	VectorMultiply( SF, -1*DOPPLER_DIST_LEFT_TO_RIGHT, DL );
	VectorMultiply( SF, DOPPLER_DIST_LEFT_TO_RIGHT, DR );

	VectorAdd( vnearpoint, DL, source_doppler_left );
	VectorAdd( vnearpoint, DR, source_doppler_right );
	
	// set pitch of channel based on nearest distance to listener

	// LN is vector from listener to closest approach point

	VectorSubtract(vnearpoint, listener_origin, LN);

	float pitch;
	float dist = VectorLength( LN );
	
	// dist varies 0->1

	dist = clamp(dist, (float)DOPPLER_DIST_MIN, (float)DOPPLER_DIST_MAX);
	dist = (dist - DOPPLER_DIST_MIN) / (DOPPLER_DIST_MAX - DOPPLER_DIST_MIN);

	// pitch varies from max to min

	pitch = DOPPLER_PITCH_MAX - dist * (DOPPLER_PITCH_MAX - DOPPLER_PITCH_MIN);
	
	pChannel->basePitch = (int)(pitch * 100.0);
}

// console variables used to construct gain curve - don't change these!

extern ConVar snd_foliage_db_loss; 
extern ConVar snd_gain;
extern ConVar snd_refdb;
extern ConVar snd_refdist;
extern ConVar snd_gain_max;
extern ConVar snd_gain_min;

ConVar snd_showstart( "snd_showstart", "0", FCVAR_CHEAT );	// showstart always skips info on player footsteps!
												// 1 - show sound name, channel, volume, time 
												// 2 - show dspmix, distmix, dspface, l/r/f/r vols
												// 3 - show sound origin coords
												// 4 - show gain of dsp_room
												// 5 - show dB loss due to obscured sound
												// 6 - reserved
												// 7 - show 2 and total gain & dist in ft. to sound source

#define SND_DB_MAX				140.0	// max db of any sound source
#define SND_DB_MED				90.0	// db at which compression curve changes
#define SND_DB_MIN				60.0	// min db of any sound source

#define SND_GAIN_PLAYER_WEAPON_DB 2.0	// increase player weapon gain by N dB

// dB = 20 log (amplitude/32768)		0 to -90.3dB
// amplitude = 32768 * 10 ^ (dB/20)		0 to +/- 32768
// gain = amplitude/32768				0 to 1.0

float Gain_To_dB ( float gain )
{
	float dB = 20 * log ( gain );
	return dB;
}

float dB_To_Gain ( float dB )
{
	float gain = powf (10, dB / 20.0);
	return gain;
}

float Gain_To_Amplitude ( float gain )
{
	return gain * 32768;
}

float Amplitude_To_Gain ( float amplitude )
{
	return amplitude / 32768;
}

soundlevel_t SND_GetSndlvl ( channel_t *pchannel )
{
	return DIST_MULT_TO_SNDLVL( pchannel->dist_mult );
}


// The complete gain calculation, with SNDLVL given in dB is:
//
// GAIN = 1/dist * snd_refdist * 10 ^ ( ( SNDLVL - snd_refdb - (dist * snd_foliage_db_loss / 1200)) / 20 )
// 
//		for gain > SND_GAIN_THRESH, start curve smoothing with
//
// GAIN = 1 - 1 / (Y * GAIN ^ SND_GAIN_POWER)
// 
//		 where Y = -1 / ( (SND_GAIN_THRESH ^ SND_GAIN_POWER) * (SND_GAIN_THRESH - 1) )
//

float SND_GetGainFromMult( float gain, float dist_mult, vec_t dist );

// gain curve construction

float SND_GetGain( channel_t *ch, bool fplayersound, bool fmusicsound, bool flooping, vec_t dist, bool bAttenuated )
{
	VPROF_("SND_GetGain",2,VPROF_BUDGETGROUP_OTHER_SOUND,false,BUDGETFLAG_OTHER);
	if ( ch->flags.m_bCompatibilityAttenuation )
	{
		// Convert to the original attenuation value.
		soundlevel_t soundlevel = DIST_MULT_TO_SNDLVL( ch->dist_mult );
		float flAttenuation = SNDLVL_TO_ATTN( soundlevel );

		// Now get the goldsrc dist_mult and use the same calculation it uses in SND_Spatialize.
		// Straight outta Goldsrc!!!
		vec_t nominal_clip_dist = 1000.0;
		float flGoldsrcDistMult = flAttenuation / nominal_clip_dist;
		dist *= flGoldsrcDistMult;
		float flReturnValue = 1.0f - dist;
		flReturnValue = clamp( flReturnValue, 0.f, 1.f );
		return flReturnValue;
	}
	else
	{
		float gain = snd_gain.GetFloat();

		if ( fmusicsound )
		{
			gain = gain * snd_musicvolume.GetFloat();
			gain = gain * g_DashboardMusicMixValue;
		}

		if ( ch->dist_mult )
		{
			gain = SND_GetGainFromMult( gain, ch->dist_mult, dist );
		}

		if ( fplayersound )
		{
			
			// player weapon sounds get extra gain - this compensates
			// for npc distance effect weapons which mix louder as L+R into L,R
			// Hack.

			if ( ch->entchannel == CHAN_WEAPON )
				gain = gain * dB_To_Gain( SND_GAIN_PLAYER_WEAPON_DB );
		}

		// modify gain if sound source not visible to player

		gain = gain * SND_GetGainObscured( ch, fplayersound, flooping, bAttenuated );

		if (snd_showstart.GetInt() == 6)
		{
			DevMsg( "(gain %1.3f : dist ft %1.1f) ", gain, (float)dist/12.0 );
			snd_showstart.SetValue(5);	// display once
		}

		return gain; 
	}
}

// always ramp channel gain changes over time
// returns ramped gain, given new target gain

#define SND_GAIN_FADE_TIME	0.25		// xfade seconds between obscuring gain changes

float SND_FadeToNewGain( channel_t *ch, float gain_new )
{

	if ( gain_new == -1.0 )
	{
		// if -1 passed in, just keep fading to existing target

		gain_new = ch->ob_gain_target;
	}

	// if first time updating, store new gain into gain & target, return
	// if gain_new is close to existing gain, store new gain into gain & target, return

	if ( ch->flags.bfirstpass || (fabs (gain_new - ch->ob_gain) < 0.01))
	{
		ch->ob_gain			= gain_new;
		ch->ob_gain_target	= gain_new;
		ch->ob_gain_inc		= 0.0;
		return gain_new;
	}

	// set up new increment to new target
	
	float frametime = g_pSoundServices->GetHostFrametime();
	float speed;
	speed = ( frametime / SND_GAIN_FADE_TIME ) * (gain_new - ch->ob_gain);

	ch->ob_gain_inc = fabs(speed);

	// ch->ob_gain_inc = fabs(gain_new - ch->ob_gain) / 10.0;
	
	ch->ob_gain_target = gain_new;

	// if not hit target, keep approaching
	
	if ( fabs( ch->ob_gain - ch->ob_gain_target ) > 0.01 )
	{
		ch->ob_gain = Approach( ch->ob_gain_target, ch->ob_gain, ch->ob_gain_inc );
	}
	else
	{
		// close enough, set gain = target
		ch->ob_gain = ch->ob_gain_target;
	}

	return ch->ob_gain;
}

#define SND_TRACE_UPDATE_MAX  2			// max of N channels may be checked for obscured source per frame

static int g_snd_trace_count = 0;		// total tracelines for gain obscuring made this frame

// All new sounds must traceline once,
// but cap the max number of tracelines performed per frame
// for longer or looping sounds to SND_TRACE_UPDATE_MAX.

bool SND_ChannelOkToTrace( channel_t *ch )
{
	// always trace first time sound is spatialized (doesn't update counter)

	if ( ch->flags.bfirstpass )
	{
		ch->flags.bTraced = true;
		return true;
	}

	// if already traced max channels this frame, return

	if ( g_snd_trace_count >= SND_TRACE_UPDATE_MAX )
		return false;

	// ok to trace if this sound hasn't yet been traced in this round

	if ( ch->flags.bTraced )
		return false;

	// set flag - don't traceline this sound again until all others have
	// been traced

	ch->flags.bTraced = true;

	g_snd_trace_count++;				// total traces this frame

	return true;
}

// determine if we need to reset all flags for traceline limiting - 
// this happens if we hit a frame whein no tracelines occur ie: all currently 
// playing sounds are blocked.

void SND_ChannelTraceReset( void )
{
	if ( g_snd_trace_count )
		return;

	// if no tracelines performed this frame, then reset all 
	// trace flags

	for (int i = 0; i < total_channels; i++)
		channels[i].flags.bTraced = false; 
}

bool SND_IsLongWave( channel_t *pChannel )
{
	CAudioSource *pSource = pChannel->sfx ? pChannel->sfx->pSource : NULL;
	if ( pSource )
	{
		if ( pSource->IsStreaming() )
			return true;

	// UNDONE: Do this on long wave files too?
#if 0
		float length = (float)pSource->SampleCount() / (float)pSource->SampleRate();
		if ( length > 0.75f )
			return true;
#endif
	}

	return false;
}


ConVar snd_obscured_gain_db( "snd_obscured_gain_dB", "-2.70", FCVAR_CHEAT ); // dB loss due to obscured sound source

// drop gain on channel if sound emitter obscured by
// world, unbroken windows, closed doors, large solid entities etc.

float SND_GetGainObscured( channel_t *ch, bool fplayersound, bool flooping, bool bAttenuated )
{
	float gain = 1.0;
	int count = 1;
	float snd_gain_db;					// dB loss due to obscured sound source

	// Unattenuated sounds don't get obscured.
	if ( !bAttenuated )
		return 1.0f;

	if ( fplayersound )
		return gain;

	// During signon just apply regular state machine since world hasn't been
	//  created or settled yet...

	if ( !SND_IsInGame() )
	{
		if ( !toolframework->InToolMode() )
		{
			gain = SND_FadeToNewGain( ch, -1.0 );
		}

		return gain;
	}

	// don't do gain obscuring more than once on short one-shot sounds

	if ( !ch->flags.bfirstpass && !ch->flags.isSentence && !flooping && !SND_IsLongWave(ch) )
	{
		gain = SND_FadeToNewGain( ch, -1.0 );
		return gain;
	}

	snd_gain_db = snd_obscured_gain_db.GetFloat();

	// if long or looping sound, process N channels per frame - set 'processed' flag, clear by
	// cycling through all channels - this maintains a cap on traces per frame

	if ( !SND_ChannelOkToTrace( ch ) )
	{
		// just keep updating fade to existing target gain - no new trace checking

		gain = SND_FadeToNewGain( ch, -1.0 );
		return gain;
	}
	// set up traceline from player eyes to sound emitting entity origin

	Vector endpoint = ch->origin;
	
	trace_t tr;
	CTraceFilterWorldOnly filter;	// UNDONE: also test for static props?
	Ray_t ray;
	ray.Init( MainViewOrigin(), endpoint );
	g_pEngineTraceClient->TraceRay( ray, MASK_BLOCK_AUDIO, &filter, &tr );

	if (tr.DidHit() && tr.fraction < 0.99)
	{
		// can't see center of sound source:
		// build extents based on dB sndlvl of source,
		// test to see how many extents are visible,
		// drop gain by snd_gain_db per extent hidden

		Vector endpoints[4];
		soundlevel_t sndlvl = DIST_MULT_TO_SNDLVL( ch->dist_mult );
		float radius;
		Vector vsrc_forward;
		Vector vsrc_right;
		Vector vsrc_up;
		Vector vecl;
		Vector vecr;
		Vector vecl2;
		Vector vecr2;
		int i;

		// get radius
		
		if ( ch->radius > 0 )
			radius = ch->radius;
		else
			radius = dB_To_Radius( sndlvl);		// approximate radius from soundlevel
		
		// set up extent endpoints - on upward or downward diagonals, facing player

		for (i = 0; i < 4; i++)
			endpoints[i] = endpoint;

		// vsrc_forward is normalized vector from sound source to listener

		VectorSubtract( listener_origin, endpoint, vsrc_forward );
		VectorNormalize( vsrc_forward );
		VectorVectors( vsrc_forward, vsrc_right, vsrc_up );

		VectorAdd( vsrc_up, vsrc_right, vecl );
		
		// if src above listener, force 'up' vector to point down - create diagonals up & down

		if ( endpoint.z > listener_origin.z + (10 * 12) )
			vsrc_up.z = -vsrc_up.z;

		VectorSubtract( vsrc_up, vsrc_right, vecr );
		VectorNormalize( vecl );
		VectorNormalize( vecr );

		// get diagonal vectors from sound source 

		vecl2 = radius * vecl;
		vecr2 = radius * vecr;
		vecl = (radius / 2.0) * vecl;
		vecr = (radius / 2.0) * vecr;

		// endpoints from diagonal vectors

		endpoints[0] += vecl;
		endpoints[1] += vecr;
		endpoints[2] += vecl2;
		endpoints[3] += vecr2;

		// drop gain for each point on radius diagonal that is obscured

		for (count = 0, i = 0; i < 4; i++)
		{
			// UNDONE: some endpoints are in walls - in this case, trace from the wall hit location

			ray.Init( MainViewOrigin(), endpoints[i] );
			g_pEngineTraceClient->TraceRay( ray, MASK_BLOCK_AUDIO, &filter, &tr );
			
			if (tr.DidHit() && tr.fraction < 0.99 && !tr.startsolid )
			{
				count++;	// skip first obscured point: at least 2 points + center should be obscured to hear db loss
				if (count > 1)
					gain = gain * dB_To_Gain( snd_gain_db );
			}
		}
	}

	
	if ( flooping && snd_showstart.GetInt() == 7)
	{
		static float g_drop_prev = 0;
		float drop = (count-1) * snd_gain_db;

		if (drop != g_drop_prev)
		{
			DevMsg( "dB drop: %1.4f \n", drop);
			g_drop_prev = drop;
		}
	}

	// crossfade to new gain

	gain = SND_FadeToNewGain( ch, gain );

	return gain;
}

// convert sound db level to approximate sound source radius,
// used only for determining how much of sound is obscured by world

#define SND_RADIUS_MAX		(20.0 * 12.0)	// max sound source radius
#define SND_RADIUS_MIN		(2.0 * 12.0)	// min sound source radius

inline float dB_To_Radius ( float db )
{
	float radius = SND_RADIUS_MIN + (SND_RADIUS_MAX - SND_RADIUS_MIN) * (db - SND_DB_MIN) / (SND_DB_MAX - SND_DB_MIN);

	return radius;
}

struct snd_spatial_t
{
	int chan;			// 0..4 cycles through up to 5 channels
	int cycle;			// 0..2 cycles through 3 vectors per channel
	int dist[5][3];		// stores last 3 channel distance values [channel][cycle]

	float value_prev[5];	// previous value per channel

	double last_change;
};

bool g_ssp_init = false;
snd_spatial_t g_ssp;

// return 0..1 percent difference between a & b

float PercentDifference( float a, float b )
{
	float vp;

	if (!(int)a && !(int)b)
		return 0.0;

	if (!(int)a || !(int)b)
		return 1.0;

	if (a > b)
		vp = b / a;
	else
		vp = a / b;

	return (1.0 - vp);
}

// NOTE: Do not change SND_WALL_TRACE_LEN without also changing PRC_MDY6 delay value in snd_dsp.cpp!

#define SND_WALL_TRACE_LEN (100.0*12.0)		// trace max of 100' = max of 100 milliseconds of linear delay
#define SND_SPATIAL_WAIT	(0.25)			// seconds to wait between traces

// change mod delay value on chan 0..3 to v (inches)

void DSP_SetSpatialDelay( int chan, float v )
{
	// remap delay value 0..1200 to 1.0 to -1.0 for modulation

	float value = ( v / SND_WALL_TRACE_LEN) - 1.0;					// -1.0...0
	value = value * 2.0;											// -2.0...0
	value += 1.0;													// -1.0...1.0 (0...1200)
	value *= -1.0;													// 1.0...-1.0 (0...1200)

	// assume first processor in dsp_spatial is the modulating delay unit for DSP_ChangePresetValue

	int iproc = 0;

	DSP_ChangePresetValue( idsp_spatial, chan, iproc, value );		
/*

	if (chan & 0x01)
		DevMsg("RDly: %3.0f \n", v/12 );
	else
		DevMsg("LDly: %3.0f \n", v/12 );
*/
}

// use non-feedback delay to stereoize (or make quad, or quad + center) the mono dsp_room fx, 
// This simulates the average sum of delays caused by reflections
// from the left and right walls relative to the player.  The average delay
// difference between left & right wall is (l + r)/2.  This becomes the average
// delay difference between left & right ear. 
// call at most once per frame to update player->wall spatial delays

void SND_SetSpatialDelays()
{
	VPROF("SoundSpatialDelays");
	float dist, v, vp;
	Vector v_dir, v_dir2;
	int chan_max = (g_AudioDevice->IsSurround() ? 4 : 2) + (g_AudioDevice->IsSurroundCenter() ? 1 : 0);  // 2, 4, 5 channels
	
	// use listener_forward2d, which doesn't change when player looks up/down.

	Vector listener_forward2d;
	
	ConvertListenerVectorTo2D( &listener_forward2d, &listener_right );

	// init struct if 1st time through

	if ( !g_ssp_init )
	{
		Q_memset(&g_ssp, 0, sizeof(snd_spatial_t));
		g_ssp_init = true;
	}

	// return if dsp_spatial is 0
	
	if ( !dsp_spatial.GetInt() ) 
		return;
	
	// if listener has not been updated, do nothing

	if ((listener_origin  == vec3_origin) && 
		(listener_forward == vec3_origin) &&
		(listener_right	  == vec3_origin) &&
		(listener_up	  == vec3_origin) )
		return;

	if ( !SND_IsInGame() )
		return;

	// get time
	
	double dtime = g_pSoundServices->GetHostTime();
	
	// compare to previous time - if starting new check - don't check for new room until timer expires

	if (!g_ssp.chan && !g_ssp.cycle)
	{
		if (fabs(dtime - g_ssp.last_change) < SND_SPATIAL_WAIT)
			return;
	}

	// cycle through forward, left, rearward vectors, averaging to get left/right delay
	// count[chan][cycle] 0,1 0,2 0,3   1,1 1,2 1,3    2,1 2,2 2,3 ...

	g_ssp.cycle++;
	
	if (g_ssp.cycle == 3)
	{
		g_ssp.cycle = 0;

		// cycle through front left, front right, rear left, rear right, front center delays

		g_ssp.chan++;
	
		if (g_ssp.chan >= chan_max )
			g_ssp.chan = 0;
	}

	// set up traceline from player eyes to surrounding walls

	switch( g_ssp.chan )
	{
	default:
	case 0: // front left: trace max 100' 'cone' to player's left
		if ( g_AudioDevice->IsSurround() )
		{
			// 4-5 speaker case - front left
			v_dir = (-listener_right + listener_forward2d) / 2.0;
			v_dir = g_ssp.cycle ? (g_ssp.cycle == 1 ? -listener_right * 0.5: listener_forward2d * 0.5) : v_dir;
		}
		else
		{
			// 2 speaker case - left
			v_dir = listener_right * -1.0;
			v_dir2 = g_ssp.cycle ? (g_ssp.cycle == 1 ? listener_forward2d * 0.5 : -listener_forward2d * 0.5) : v_dir;
			v_dir = (v_dir + v_dir2) / 2.0;
		}
		break;

	case 1: // front right: trace max 100' 'cone' to player's right
		if ( g_AudioDevice->IsSurround() )
		{
			// 4-5 speaker case - front right
			v_dir = (listener_right + listener_forward2d) / 2.0;
			v_dir = g_ssp.cycle ? (g_ssp.cycle == 1 ? listener_right * 0.5: listener_forward2d * 0.5) : v_dir;
		}
		else
		{
			// 2 speaker case - right
			v_dir = listener_right;
			v_dir2 = g_ssp.cycle ? (g_ssp.cycle == 1 ? listener_forward2d * 0.5 : -listener_forward2d * 0.5) : v_dir;
			v_dir = (v_dir + v_dir2) / 2.0;
		}
		break;

	case 2: // rear left: trace max 100' 'cone' to player's rear left
		v_dir = (listener_right + listener_forward2d) / -2.0;
		v_dir = g_ssp.cycle ? (g_ssp.cycle == 1 ? -listener_right * 0.5 : -listener_forward2d * 0.5) : v_dir;
		break;

	case 3: // rear right: trace max 100' 'cone' to player's rear right
		v_dir = (listener_right - listener_forward2d) / 2.0;
		v_dir = g_ssp.cycle ? (g_ssp.cycle == 1 ? listener_right * 0.5: -listener_forward2d * 0.5) : v_dir;
		break;
		
	case 4: // front center: trace max 100' 'cone' to player's front
		v_dir = listener_forward2d;
		v_dir2 = g_ssp.cycle ? (g_ssp.cycle == 1 ? listener_right * 0.15 : -listener_right * 0.15) : v_dir;
		v_dir = (v_dir + v_dir2);
		break;
	}

	Vector endpoint;	
	trace_t tr;
	CTraceFilterWorldOnly filter;

	endpoint = MainViewOrigin() + v_dir * SND_WALL_TRACE_LEN;
	Ray_t ray;
	ray.Init( MainViewOrigin(), endpoint );
	g_pEngineTraceClient->TraceRay( ray, MASK_BLOCK_AUDIO, &filter, &tr );

	dist = SND_WALL_TRACE_LEN;

	if ( tr.DidHit() )
	{
		dist = VectorLength( tr.endpos - MainViewOrigin() );	
	}
	
	g_ssp.dist[g_ssp.chan][g_ssp.cycle] = dist;

	// set new result in dsp_spatial delay params when all delay values have been filled in

	if (!g_ssp.cycle && !g_ssp.chan)
	{
		// update delay for each channel

		for (int chan = 0; chan < chan_max; chan++)
		{
			// compute average of 3 traces per channel

			v = (g_ssp.dist[chan][0] + g_ssp.dist[chan][1] + g_ssp.dist[chan][2]) / 3.0;
			vp = g_ssp.value_prev[chan];

			// only change if 10% difference from previous

			if ((vp != v) && int(v) && (PercentDifference( v, vp ) >= 0.1))		
			{
				// update when we have data for all L/R && RL/RR channels...

				if (chan & 0x1)
				{
					float vr = fpmin( v, (50*12.0f) );
					float vl = fpmin(g_ssp.value_prev[chan-1], (50*12.0f));

/* UNDONE: not needed, now that this applies only to dsp 'room' buffer

					// ensure minimum separation = average distance to walls

					float dmin = (vl + vr) / 2.0;		// average distance to walls
					float d = vl - vr;					// l/r separation

					// if separation is less than average, increase min

					if (abs(d) < dmin/2)
					{
						if (vl > vr)
							vl += dmin/2 - d;
						else
							vr += dmin/2 - d;
					}
*/
					DSP_SetSpatialDelay(chan-1, vl);
					DSP_SetSpatialDelay(chan, vr);
				}
				
				// update center chan

				if (chan == 4)
				{
					float vl = fpmin( v, (50*12.0f) );
					DSP_SetSpatialDelay(chan, vl);
				}
			}
			
			g_ssp.value_prev[chan] = v;

		}

		// update wait timer now that all values have been checked

		g_ssp.last_change = dtime;		
	}	
}

// Dsp Automatic Selection:

//	a) enabled by setting dsp_room to DSP_AUTOMATIC.  Subsequently, dsp_automatic is the actual dsp value for dsp_room.
//	b) disabled by setting dsp_room to anything else

//	c) while enabled, detection nodes are placed as player moves into a new space
//     i.	at each node, a new dsp setting is calculated and dsp_automatic is set to an appropriate preset
//     ii.	new nodes are set when player moves out of sight of previous node
//     iii. moving into line of sight of a detection node causes closest node to player to set dsp_automatic

// see void DAS_CheckNewRoomDSP( ) for main entrypoint

ConVar das_debug( "adsp_debug", "0", FCVAR_ARCHIVE );
												// >0: draw blue dsp detection node location
												// >1: draw green room trace height detection bars
												// 3: draw yellow horizontal trace bars for room width/depth detection
												// 4: draw yellow upward traces for height detection
												// 5: draw teal box around all props around player
												// 6: draw teal box around room as detected

#define DAS_CWALLS				20				// # of wall traces to save for calculating room dimensions
#define DAS_ROOM_TRACE_LEN		(400.0*12.0)	// max size of trace to check for room dimensions

#define DAS_AUTO_WAIT	0.25					// wait min of n seconds between dsp_room changes and update checks

#define DAS_WIDTH_MIN	0.4						// min % change in avg width of any wall pair to cause new dsp
#define DAS_REFL_MIN	0.5						// min % change in avg refl of any wall to cause new dsp
#define DAS_SKYHIT_MIN	0.8						// min % change in # of sky hits per wall

#define DAS_DIST_MIN	(4.0 * 12.0)			// min distance between room dsp changes
#define DAS_DIST_MAX	(40.0 * 12.0)			// max distance to preserve room dsp changes

#define DAS_DIST_MIN_OUTSIDE	(6.0 * 12.0)	// min distance between room dsp changes outside
#define DAS_DIST_MAX_OUTSIDE	(100.0 * 12.0)	// max distance to preserve room dsp changes outside

#define IVEC_DIAG_UP	8						// start of diagonal up vectors
#define IVEC_UP			18						// up vector
#define IVEC_DOWN		19						// down vector

#define DAS_REFLECTIVITY_NORM	0.5
#define DAS_REFLECTIVITY_SKY	0.0

// auto dsp room struct

struct das_room_t
{
	int   dist[DAS_CWALLS];		// distance in units from player to axis aligned and diagonal walls
	float reflect[DAS_CWALLS];	// acoustic reflectivity per wall
	float skyhits[DAS_CWALLS];	// every sky hit adds 0.1
	Vector hit[DAS_CWALLS];		// location of trace hit on wall - used for calculating average centers
	Vector norm[DAS_CWALLS];	// wall normal at hit location

	Vector vplayer;				// 'frozen' location above player's head

	Vector vplayer_eyes;		// 'frozen' location player's eyes

	int width_max;				// max width
	int length_max;				// max length
	int height_max;				// max height
		
	float refl_avg;				// running average of reflectivity of all walls
	float refl_walls[6];		// left,right,front,back,ceiling,floor reflectivities	

	float sky_pct;				// percent of sky hits
	
	Vector room_mins;			// room bounds
	Vector room_maxs;

	double last_dsp_change;		// time since last dsp change

	float diffusion;			// 0..1.0 check radius (avg of width_avg) for # of props - scale diffusion based on # found
	short iwall;					// cycles through walls 0..5, ensuring only one trace per frame
	short ent_count;	 			// count of entities found in radius
	bool bskyabove;				// true if sky found above player (ie: outside)
	bool broomready;			// true if all distances are filled in and room is ready to check
	short lowceiling;				// if non-zero, ceiling directly above player if < 112 units
};

// dsp detection node

struct das_node_t
{
	Vector vplayer;				// position

	bool fused;					// true if valid node
	bool fseesplayer;			// true if node sees player on last check
	short dsp_preset;				// preset
		
	int range_min;				// min,max detection ranges
	int range_max;
	
	int dist;					// last distance to player

	// room parameters when node was created:

	das_room_t room;
};

#define DAS_CNODES	40					// keep around last n nodes - must be same as DSP_CAUTO_PRESETS!!!

das_node_t g_das_nodes[DAS_CNODES];		// all dsp detection nodes
das_node_t *g_pdas_last_node = NULL;	// last node that saw player

int g_das_check_next;					// next node to check
int g_das_store_next;					// next place to store node
bool g_das_all_checked;					// true if all nodes checked
int g_das_checked_count;				// count of nodes checked in latest pass

das_room_t g_das_room;					// room detector

bool g_bdas_room_init = 0;
bool g_bdas_init_nodes = 0;
bool g_bdas_create_new_node = 0;

bool DAS_TraceNodeToPlayer( das_room_t *proom, das_node_t *pnode );
void DAS_InitAutoRoom( das_room_t *proom);
void DAS_DebugDrawTrace ( trace_t *ptr, int r, int g, int b, float duration, int imax );

Vector g_das_vec3[DAS_CWALLS];	// trace vectors to walls, ceiling, floor

void DAS_InitNodes( void )
{
	Q_memset(g_das_nodes, 0, sizeof(das_node_t) * DAS_CNODES);
	g_das_check_next = 0;
	g_das_store_next = 0;
	g_das_all_checked = 0;
	g_das_checked_count = 0;

	// init all rooms

	for (int i = 0; i < DAS_CNODES; i++)
		DAS_InitAutoRoom( &(g_das_nodes[i].room) );

	// init trace vectors
	// set up trace vectors for max, min width
	float vl = DAS_ROOM_TRACE_LEN;
	float vlu = DAS_ROOM_TRACE_LEN * 0.52;
	float vlu2 = DAS_ROOM_TRACE_LEN * 0.48;	// don't use 'perfect' diagonals

	g_das_vec3[0].Init(vl, 0.0, 0.0);				// x left
	g_das_vec3[1].Init(-vl, 0.0, 0.0);				// x right

	g_das_vec3[2].Init(0.0, vl, 0.0);				// y front
	g_das_vec3[3].Init(0.0, -vl, 0.0);				// y back

	g_das_vec3[4].Init(-vlu, vlu2, 0.0);			// diagonal front left
	g_das_vec3[5].Init(vlu, -vlu2, 0.0);			// diagonal rear right

	g_das_vec3[6].Init(vlu, vlu2, 0.0);				// diagonal front right
	g_das_vec3[7].Init(-vlu, -vlu2, 0.0);			// diagonal rear left

	// set up trace vectors for max height - on x=y diagonal

	g_das_vec3[8].Init(vlu, vlu2, vlu/2.0);			// front right up A x,y,z/2		(IVEC_DIAG_UP)
	g_das_vec3[9].Init(vlu, vlu2, vlu);				// front right up B x,y,z
	g_das_vec3[10].Init(vlu/2.0, vlu2/2.0, vlu);	// front right up C x/2,y/2,z

	g_das_vec3[11].Init(-vlu, -vlu2, vlu/2.0);		// rear left up A -x,-y,z/2
	g_das_vec3[12].Init(-vlu, -vlu2, vlu);			// rear left up B -x,-y,z
	g_das_vec3[13].Init(-vlu/2.0, -vlu2/2.0, vlu);	// rear left up C -x/2,-y/2,z

	// set up trace vectors for max height - on x axis & y axis

	g_das_vec3[14].Init(-vlu, 0, vlu);				// left up B -x,0,z
	g_das_vec3[15].Init(0, vlu/2.0, vlu);			// front up C -x/2,0,z

	g_das_vec3[16].Init(0, -vlu, vlu);				// rear up B x,0,z
	g_das_vec3[17].Init(vlu/2.0, 0, vlu);			// right up C x/2,0,z

	g_das_vec3[18].Init(0.0, 0.0, vl);				// up	(IVEC_UP)
	g_das_vec3[19].Init(0.0, 0.0, -vl);				// down (IVEC_DOWN)
}

void DAS_InitAutoRoom( das_room_t *proom)
{
		Q_memset(proom, 0, sizeof (das_room_t));
}

// reset all nodes for next round of visibility checks between player & nodes

void DAS_ResetNodes( void )
{
	for (int i = 0; i < DAS_CNODES; i++)
	{
		g_das_nodes[i].fseesplayer = false;
		g_das_nodes[i].dist = 0;
	}

	g_das_all_checked = false;
	g_das_checked_count = 0;
	g_bdas_create_new_node = false;
}

// utility function - return next index, wrap at max

int DAS_GetNextIndex( int *pindex, int max )
{
	int i = *pindex;
	int j;

	j = i+1;
	if ( j >= max )
		j = 0;

	*pindex = j;

	return i;
}

// returns true if dsp node is within range of player

bool DAS_NodeInRange( das_room_t *proom, das_node_t *pnode )
{
	float dist;

	dist = VectorLength( proom->vplayer - pnode->vplayer );

	// player can still see previous room selection point, and it's less than n feet away, 
	// then flag this node as visible

	pnode->dist = dist;
	
	return ( dist <= pnode->range_max );
}

// update next valid node - set up internal node state if it can see player
// called once per frame
// returns true if all nodes have been checked

bool DAS_CheckNextNode( das_room_t *proom )
{
	int i, j;

	if ( g_das_all_checked )
		return true;

	// find next valid node

	for (j = 0; j < DAS_CNODES; j++)
	{
		// track number of nodes checked

		g_das_checked_count++;

		// get next node in range to check

		i = DAS_GetNextIndex( &g_das_check_next, DAS_CNODES );

		if ( g_das_nodes[i].fused && DAS_NodeInRange( proom, &(g_das_nodes[i]) ) )
		{
			// trace to see if player can still see node, 
			// if so stop checking

			if ( DAS_TraceNodeToPlayer( proom, &(g_das_nodes[i]) ))
				goto checknode_exit;
		}
	}

checknode_exit:

	// flag that all nodes have been checked

	if ( g_das_checked_count >= DAS_CNODES )
		g_das_all_checked = true;	

	return g_das_all_checked;
}


int DAS_GetNextNodeIndex()
{
	return g_das_store_next;
}
// store new node for room

void DAS_StoreNode( das_room_t *proom, int dsp_preset)
{
	// overwrite node in cyclic list
	
	int i = DAS_GetNextIndex( &g_das_store_next, DAS_CNODES );

	g_das_nodes[i].dsp_preset = dsp_preset;
	g_das_nodes[i].fused = true;
	g_das_nodes[i].vplayer = proom->vplayer;

	// calculate node scanning range_max based on room size
	
	if ( !proom->bskyabove )
	{
		// inside range - halls & tunnels have nodes every 5*width
		g_das_nodes[i].range_max = fpmin((int)DAS_DIST_MAX, min(proom->width_max * 5, proom->length_max) ); 
		g_das_nodes[i].range_min = DAS_DIST_MIN;
	}
	else
	{
		// outside range
		g_das_nodes[i].range_max = DAS_DIST_MAX_OUTSIDE;
		g_das_nodes[i].range_min = DAS_DIST_MIN_OUTSIDE;
	}

	g_das_nodes[i].fseesplayer = false;
	g_das_nodes[i].dist = 0;

	g_das_nodes[i].room = *proom;

	// update last node visible as this node

	g_pdas_last_node = &(g_das_nodes[i]);
}

// check all updated nodes,
// return dsp_preset of largest node (by area) that can see player
// return -1 if no preset found

// NOTE: outside nodes can't see player if player is inside and vice versa
// foutside is true if player is outside

int DAS_GetDspPreset( bool foutside )
{
	int dsp_preset = -1;

	int i;
	// int dist_min = 100000;
	int area_max = 0;
	int area;

	// find node that represents room with greatest floor area, return its preset.
	
	for (i = 0; i < DAS_CNODES; i++)
	{
		if (g_das_nodes[i].fused && g_das_nodes[i].fseesplayer)
		{
			area = (g_das_nodes[i].room.width_max * g_das_nodes[i].room.length_max);
			
			if ( g_das_nodes[i].room.bskyabove == foutside )
			{
				if (area > area_max)
				{
					area_max = area;
					dsp_preset = g_das_nodes[i].dsp_preset;

					// save pointer to last node that saw player

					g_pdas_last_node = &(g_das_nodes[i]);
				}
			}			
/*		

			// find nearest node, return its preset

			if (g_das_nodes[i].dist < dist_min)
			{
				if ( g_das_nodes[i].room.bskyabove == foutside )
				{
					dist_min = g_das_nodes[i].dist;
					dsp_preset = g_das_nodes[i].dsp_preset;

					// save pointer to last node that saw player

					g_pdas_last_node = &(g_das_nodes[i]);
		
				}
			}
*/
		}
	}
	
	return dsp_preset;
}

// custom trace filter: 
// a) never hit player or monsters or entities
// b) always hit world, or moveables or static props

class CTraceFilterDAS : public ITraceFilter
{
public:
	bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask )
	{
		IClientUnknown *pUnk = static_cast<IClientUnknown*>(pHandleEntity);
		IClientEntity *pEntity;

		if ( !pUnk )
			return false;

		// don't hit non-collideable props

		if ( StaticPropMgr()->IsStaticProp( pHandleEntity ) )
		{

			ICollideable *pCollide = StaticPropMgr()->GetStaticProp( pHandleEntity);
			if (!pCollide)
				return false;
		}

		// don't hit any ents

		pEntity = pUnk->GetIClientEntity();
		
		if ( pEntity )
			return false;

		return true;
	}

	virtual TraceType_t	GetTraceType() const
	{
		return TRACE_EVERYTHING_FILTER_PROPS;
	}
};

#define DAS_TRACE_MASK		(CONTENTS_SOLID|CONTENTS_MOVEABLE|CONTENTS_WINDOW)

// returns true if clear line exists between node and player
// if node can see player, sets up node distance and flag fseesplayer

bool DAS_TraceNodeToPlayer( das_room_t *proom, das_node_t *pnode )
{
	trace_t trP;
	CTraceFilterDAS filterP;
	bool fseesplayer = false;
	float dist;
	Ray_t ray;
	ray.Init( proom->vplayer, pnode->vplayer );
	
	g_pEngineTraceClient->TraceRay( ray, DAS_TRACE_MASK, &filterP, &trP );
	dist = VectorLength( proom->vplayer - pnode->vplayer );

	// player can still see previous room selection point, and it's less than n feet away, 
	// then flag this node as visible

	if ( !trP.DidHit() && (dist <= DAS_DIST_MAX) )
	{
		fseesplayer = true;
		pnode->dist = dist;
	}
	
	pnode->fseesplayer = fseesplayer;

	return fseesplayer;
}

// update room boundary maxs, mins

void DAS_SetRoomBounds( das_room_t *proom, Vector &hit, bool bheight )
{
	Vector maxs, mins;

	maxs = proom->room_maxs;
	mins = proom->room_mins;

	if (!bheight)
	{
		if (hit.x > maxs.x)
			maxs.x = hit.x;

		if (hit.x < mins.x)
			mins.x = hit.x;

		if (hit.z > maxs.z)
			maxs.z = hit.z;

		if (hit.z < mins.z)
			mins.z = hit.z;
	}

	if (bheight)
	{
		if (hit.y > maxs.y)
			maxs.y = hit.y;

		if (hit.y < mins.y)
			mins.y = hit.y;
	}

	proom->room_maxs = maxs;
	proom->room_mins = mins;
}

// when all walls are updated, calculate max length, width, height, reflectivity, sky hit%, room center
// returns true if room parameters are in good location to place a node
// returns false if room parameters are not in good location to place a node
// note: false occurs if up vector doesn't hit sky, but one or more up diagonal vectors do hit sky

bool DAS_CalcRoomProps( das_room_t *proom )
{
	int length_max = 0;
	int width_max = 0;
	int height_max = 0;
	int dist[4];
	float area1, area2;
	int height;
	int i;
	int j;
	int k;
	bool b_diaghitsky = false;

	// reject this location if up vector doesn't hit sky, but 
	// one or more up diagonals do hit sky - 
	// in this case, player is under a slight overhang, narrow bridge, or
	// standing just inside a window or doorway. keep looking for better node location
	
	for (i = IVEC_DIAG_UP; i < IVEC_UP; i++)
	{
		if (proom->skyhits[i] > 0.0)
			b_diaghitsky = true;
	}

	if (b_diaghitsky && !(proom->skyhits[IVEC_UP] > 0.0))
		return false;

	// get all distance pairs

	for (i = 0; i < IVEC_DIAG_UP; i+=2)
		dist[i/2] = proom->dist[i] + proom->dist[i+1];	// 1st pair is width
	
	// if areas differ by more than 25%
	// select the pair with the greater area

	// if areas do not differ by more than 25%, select the pair with the 
	// longer measured distance. Filters incorrect selection due to diagonals.

	area1 = (float)(dist[0] * dist[1]);
	area2 = (float)(dist[2] * dist[3]);

	area1 = (int)area1 == 0 ? 1.0 : area1;
	area2 = (int)area2 == 0 ? 1.0 : area2;
	
	if ( PercentDifference(area1, area2) > 0.25 )
	{
		// areas are more than 25% different - select pair with greater area

		j = area1 > area2 ? 0 : 2;
	}
	else
	{
		// select pair with longer measured distance

		int iMaxDist = 0; // index to max dist
		int dmax = 0;

		for (i = 0; i < 4; i++)
		{
			if (dist[i] > dmax)
			{
				dmax = dist[i];
				iMaxDist = i;
			}
		}

		j = iMaxDist > 1 ? 2 : 0;
	}

	
	// width is always the smaller of the dimensions

	width_max = min (dist[j], dist[j+1]);
	length_max = max (dist[j], dist[j+1]);

	// get max height

	for (i = IVEC_DIAG_UP; i < IVEC_DOWN; i++)
	{
		height = proom->dist[i];

		if (height > height_max)
			height_max = height;
	}

	proom->length_max = length_max;
	proom->width_max = width_max;
	proom->height_max = height_max;
			
	// get room max,min from chosen width, depth
	// 0..3 or 4..7

	for ( i = j*2; i < 4+(j*2); i++)
		DAS_SetRoomBounds( proom, proom->hit[i], false );
	
	// get room height min from down trace

	proom->room_mins.z = proom->hit[IVEC_DOWN].z;

	// reset room height max to player trace height

	proom->room_maxs.z = proom->vplayer.z;

	// draw box around room max,min

	if (das_debug.GetInt() == 6)
	{
		// draw box around all objects detected
		Vector maxs = proom->room_maxs;
		Vector mins = proom->room_mins;
		Vector orig = (maxs + mins) / 2.0;
		Vector absMax = maxs - orig;
		Vector absMin = mins - orig;

		CDebugOverlay::AddBoxOverlay( orig, absMax, absMin, vec3_angle, 255, 0, 255, 0, 60.0f );
	}
	// calculate average reflectivity

	float refl = 0.0;

	// average reflectivity for walls

	// 0..3 or 4..7

	for ( k = 0, i = j*2; i < 4+(j*2); i++, k++)
	{
		refl += proom->reflect[i];
		proom->refl_walls[k] = proom->reflect[i];
	}
	
	// assume ceiling is open

	proom->refl_walls[4] = 0.0;	 

	// get ceiling reflectivity, if any non zero

	for ( i = IVEC_DIAG_UP; i < IVEC_DOWN; i++)
	{
		if (proom->reflect[i] == 0.0)
		{
			// if any upward trace hit sky, exit;
			// ceiling reflectivity is 0.0

			proom->refl_walls[4] = 0.0;

			i = IVEC_DOWN;	// exit loop
		}
		else
		{

			// upward trace didn't hit sky, keep checking

			proom->refl_walls[4] = proom->reflect[i];	
		}
	}

	// add in ceiling reflectivity, if any
	
	refl += proom->refl_walls[4];

	// get floor reflectivity
		
	refl += proom->reflect[IVEC_DOWN];
	proom->refl_walls[5] = proom->reflect[IVEC_DOWN];

	proom->refl_avg = refl / 6.0;

	// calculate sky hit percent for this wall

	float sky_pct = 0.0;

	// 0..3 or 4..7

	for ( i = j*2; i < 4+(j*2); i++)
		sky_pct += proom->skyhits[i];
	
	for ( i = IVEC_DIAG_UP; i < IVEC_DOWN; i++)
	{
		if (proom->skyhits[i] > 0.0)
		{
			// if any upward trace hit sky, exit loop
			sky_pct += proom->skyhits[i];
			i = IVEC_DOWN;
		}
	}

	// get floor skyhit

	sky_pct += proom->skyhits[IVEC_DOWN];

	proom->sky_pct = sky_pct;

	// check for sky above
	proom->bskyabove = false;

	for (i = IVEC_DIAG_UP; i < IVEC_DOWN; i++)
	{
		if (proom->skyhits[i] > 0.0)
			proom->bskyabove = true;
	}

	return true;
}

// return true if trace hit solid
// return false if trace hit sky or didn't hit anything

bool DAS_HitSolid( trace_t *ptr )
{
	// if hit nothing return false

	if (!ptr->DidHit())
		return false;
	
	// if hit sky, return false (not solid)
	if (ptr->surface.flags & SURF_SKY)
		return false;

	return true;
}

// returns true if trace hit sky

bool DAS_HitSky( trace_t *ptr )
{
	if (ptr->DidHit() && (ptr->surface.flags & SURF_SKY))
		return true;
	if (!ptr->DidHit() )
	{
		float dz = ptr->endpos.z - ptr->startpos.z;
		if ( dz > 200*12.0f )
			return true;
	}
	return false;
}


bool DAS_ScanningForHeight( das_room_t *proom )
{
	return (proom->iwall >= IVEC_DIAG_UP);
}

bool DAS_ScanningForWidth( das_room_t *proom )
{
	return (proom->iwall < IVEC_DIAG_UP);
}

bool DAS_ScanningForFloor( das_room_t *proom )
{
	return (proom->iwall == IVEC_DOWN);
}

ConVar das_door_height("adsp_door_height", "112"); // standard door height hl2
ConVar das_wall_height("adsp_wall_height", "128"); // standard wall height hl2
ConVar das_low_ceiling("adsp_low_ceiling", "108"); // low ceiling height hl2


// set origin for tracing out to walls to point above player's head
// allows calculations over walls and floor obstacles, and above door openings

// WARNING: the current settings are optimal for skipping floor and ceiling clutter,
// and for detecting rooms without 'looking' through doors or windows. Don't change these cvars for hl2!

void DAS_SetTraceHeight( das_room_t *proom, trace_t *ptrU, trace_t *ptrD )
{
	// NOTE: when tracing down through player's box, endpos and startpos are reversed and 
	// startsolid and allsolid are true.

	int zup = abs(ptrU->endpos.z - ptrU->startpos.z);		// height above player's head
	int zdown = abs(ptrD->endpos.z - ptrD->startpos.z);		// distance to floor from player's head
	int h;
	h = zup + zdown;
	
	int door_height = das_door_height.GetInt();
	int wall_height = das_wall_height.GetInt();
	int low_ceiling = das_low_ceiling.GetInt();
	
	if (h > low_ceiling && h <= wall_height)
	{
		// low ceiling - trace out just above standard door height @ 112
		if (h > door_height)
			proom->vplayer.z = fpmin(ptrD->endpos.z, ptrD->startpos.z) + door_height + 1;	
		else
			proom->vplayer.z = fpmin(ptrD->endpos.z, ptrD->startpos.z) + h - 1;
	}
	else if ( h > wall_height )
	{
		// tall ceiling - trace out over standard walls @ 128

		proom->vplayer.z = fpmin(ptrD->endpos.z, ptrD->startpos.z) + wall_height + 1;
	}
	else
	{
		// very low ceiling, trace out from just below ceiling
		proom->vplayer.z = fpmin(ptrD->endpos.z, ptrD->startpos.z) + h - 1;
		proom->lowceiling = h;
	}		

	Assert (proom->vplayer.z <= ptrU->endpos.z);

	if (das_debug.GetInt() > 1)
	{
		// draw line to height, and between floor and ceiling

		CDebugOverlay::AddLineOverlay( ptrD->endpos, ptrU->endpos, 0, 255, 0, 255, false, 20 );
		
		Vector mins;
		Vector maxs;
		mins.Init(-1,-1,-2.0);
		maxs.Init(1,1,0);

		CDebugOverlay::AddBoxOverlay( proom->vplayer, mins, maxs, vec3_angle, 255, 0, 0, 0, 20 );

		CDebugOverlay::AddBoxOverlay( ptrU->endpos, mins, maxs, vec3_angle, 0, 255, 0, 0, 20 );
		CDebugOverlay::AddBoxOverlay( ptrD->endpos, mins, maxs, vec3_angle, 0, 255, 0, 0, 20 );

	}
}

// prepare room struct for new round of checks:
// clear out struct,
// init trace height origin by finding space above player's head
// returns true if player is in valid position to begin checks from 

bool DAS_StartTraceChecks( das_room_t *proom )
{
	// starting new check: store player position, init maxs, mins

	proom->vplayer_eyes = MainViewOrigin();
	proom->vplayer = MainViewOrigin();

	proom->height_max = 0;
	proom->width_max = 0;
	proom->length_max = 0;
	proom->room_maxs.Init (0.0, 0.0, 0.0);
	proom->room_mins.Init (10000.0, 10000.0, 10000.0);

	proom->lowceiling = 0;

	// find point between player's head and ceiling - trace out to walls from here

	trace_t trU, trD;	
	CTraceFilterDAS filterU, filterD;

	Vector v_dir = g_das_vec3[IVEC_DOWN];	// down - find floor

	Vector endpoint = proom->vplayer + v_dir;

	Ray_t ray;
	ray.Init( proom->vplayer, endpoint );
	
	g_pEngineTraceClient->TraceRay( ray,  DAS_TRACE_MASK, &filterD, &trD );

	// if player jumping or in air, don't continue

	if (trD.DidHit() && abs(trD.endpos.z - trD.startpos.z) > 72)
		return false;

	v_dir = g_das_vec3[IVEC_UP];			// up - find ceiling

	endpoint = proom->vplayer + v_dir;

	ray.Init( proom->vplayer, endpoint );

	g_pEngineTraceClient->TraceRay( ray, DAS_TRACE_MASK, &filterU, &trU );

	// if down trace hits floor, set trace height, otherwise default is player eye location

	if ( DAS_HitSolid( &trD) )
		DAS_SetTraceHeight( proom, &trU, &trD );
	
	return true;
}

void DAS_DebugDrawTrace ( trace_t *ptr, int r, int g, int b, float duration, int imax)
{

	// das_debug == 3: draw horizontal trace bars for room width/depth detection
	// das_debug == 4: draw upward traces for height detection

	if (das_debug.GetInt() != imax)
		return;

	CDebugOverlay::AddLineOverlay( ptr->startpos, ptr->endpos, r, g, b, 255, false, duration );
	
	Vector mins;
	Vector maxs;
	mins.Init(-1,-1,-2.0);
	maxs.Init(1,1,0);

	CDebugOverlay::AddBoxOverlay( ptr->endpos, mins, maxs, vec3_angle, r, g, b, 0, duration );

}

// wall surface data

struct das_surfdata_t
{
	float dist;				// distance to player
	float reflectivity;		// acoustic reflectivity of material on surface
	Vector hit;				// trace hit location
	Vector norm;			// wall normal at hit location
};

// trace hit wall surface, get info about surface and store in surfdata struct
// if scanning for height, bounce a second trace off of ceiling and get dist to floor

void DAS_GetSurfaceData( das_room_t *proom, trace_t *ptr, das_surfdata_t *psurfdata )
{

	float dist;				// distance to player
	float reflectivity;		// acoustic reflectivity of material on surface
	Vector hit;				// trace hit location
	Vector norm;			// wall normal at hit location
	surfacedata_t *psurf;

	psurf = physprop->GetSurfaceData( ptr->surface.surfaceProps );
	
	reflectivity = psurf ? psurf->audio.reflectivity : DAS_REFLECTIVITY_NORM;

	// keep wall hit location and normal, to calc room bounds and center

	norm = ptr->plane.normal;

	// get length to hit location

	dist = VectorLength(ptr->endpos - ptr->startpos);

	// if started tracing from within player box, startpos & endpos may be flipped

	if (ptr->endpos.z >= ptr->startpos.z)
		hit = ptr->endpos;	
	else
		hit = ptr->startpos;

	// if checking for max height by bouncing several vectors off of ceiling:
	// ignore returned normal from 1st bounce, just search straight down from trace hit location

	if ( DAS_ScanningForHeight( proom ) && !DAS_ScanningForFloor( proom ) )
	{
		trace_t tr2;
		CTraceFilterDAS filter2;

		norm.Init(0.0, 0.0, -1.0);

		Vector endpoint = hit + ( norm * DAS_ROOM_TRACE_LEN );
		
		Ray_t ray;
		ray.Init( hit, endpoint );

		g_pEngineTraceClient->TraceRay( ray, DAS_TRACE_MASK, &filter2, &tr2 );

		//DAS_DebugDrawTrace( &tr2, 255, 255, 0, 10, 1);

		if (tr2.DidHit())
		{
			// get distance between surfaces

			dist = VectorLength(tr2.endpos - tr2.startpos);
		}
	}

	// set up surface struct and return

	psurfdata->dist = dist;
	psurfdata->hit = hit;
	psurfdata->norm = norm;
	psurfdata->reflectivity = reflectivity;

}


// algorithm for detecting approximate size of space around player. Handles player in corner & non-axis aligned rooms.
// also handles player on catwalk or player under small bridge/overhang.  
// The goal is to only change the dsp room description if the the player moves into 
// a space which is SIGNIFICANTLY different from the previously set dsp space.

// save player position. find a point above player's head and trace out from here.

// from player position, get max width and max length:

// from player position, 
// a) trace x,-x, y,-y axes 
// b) trace xy, -xy, x-y, -x-y diagonals
// c) select largest room size detected from max width, max length


// from player position, get height
// a) trace out along front-up (or left-up, back-up, right-up), save hit locations
// b) trace down -z from hit locations
// c) save max height

// when max width, max length, max height all updated, get new player position

// get average room size & wall materials:
// update averages with one traceline per frame only
// returns true if room is fully updated and ready to check

bool DAS_UpdateRoomSize( das_room_t *proom )
{
	Vector endpoint;
	Vector startpoint;
	Vector v_dir;
	int iwall;
	bool bskyhit = false;
	das_surfdata_t surfdata;

	// do nothing if room already fully checked

	if ( proom->broomready )
		return true;

	// cycle through all walls, floor, ceiling
	// get wall index 

	iwall = proom->iwall;
	
	// get height above player and init proom for new round of checks

	if (iwall == 0)
	{
		if (!DAS_StartTraceChecks( proom ))
			return false;		// bad location to check room - player is jumping etc.
	}

	// get trace vector

	v_dir = g_das_vec3[iwall];

	// trace out from trace origin, in axis-aligned direction or along diagonals

	// if looking for max height, trace from top of player's eyes

	if ( DAS_ScanningForHeight( proom ) )
	{
		startpoint = proom->vplayer_eyes;
		endpoint = proom->vplayer_eyes + v_dir;
	}
	else
	{
		startpoint = proom->vplayer;
		endpoint = proom->vplayer + v_dir;
	}

	// try less expensive world-only trace first (no props, no ents - just try to hit walls)

	trace_t tr;
	CTraceFilterWorldOnly filter;

	Ray_t ray;
	ray.Init( startpoint, endpoint );

	g_pEngineTraceClient->TraceRay( ray, CONTENTS_SOLID, &filter, &tr );

	// if didn't hit world, or we hit sky when looking horizontally,
	// retrace, this time including props

	if ( !DAS_HitSolid( &tr ) && DAS_ScanningForWidth( proom ) )
	{
		CTraceFilterDAS filterDas;

		ray.Init( startpoint, endpoint );
		g_pEngineTraceClient->TraceRay( ray, DAS_TRACE_MASK, &filterDas, &tr );
	}
	
	if (das_debug.GetInt() > 2)
	{
		// draw trace lines

		if ( DAS_HitSolid( &tr ) )
			DAS_DebugDrawTrace( &tr, 0, 255, 255, 10, DAS_ScanningForHeight( proom ) + 3);	
		else
			DAS_DebugDrawTrace( &tr, 255, 0, 0, 10, DAS_ScanningForHeight( proom ) + 3);	// red lines if sky hit or no hit
	}

	// init surface data with defaults, in case we didn't hit world

	surfdata.dist			= DAS_ROOM_TRACE_LEN;
	surfdata.reflectivity	= DAS_REFLECTIVITY_SKY;	// assume sky or open area
	surfdata.hit			= endpoint;				// trace hit location
	surfdata.norm			= -v_dir;

	// check for sky hits

	if ( DAS_HitSky( &tr ) )
	{
		bskyhit = true;

		if ( DAS_ScanningForWidth( proom ) )
			// ignore horizontal sky hits for distance calculations
			surfdata.dist = 1.0;
		else
			surfdata.dist = surfdata.dist; // debug
	}

	// get length of trace if it hit world

	// if hit solid and not sky (tr.DidHit() && !bskyhit)
	// get surface information

	if ( DAS_HitSolid( &tr) )
		DAS_GetSurfaceData( proom, &tr, &surfdata );
	
	// store surface data

	proom->dist[iwall]		= surfdata.dist;
	proom->reflect[iwall]	= clamp(surfdata.reflectivity, 0.0f, 1.0f);
	proom->skyhits[iwall]	= bskyhit ? 0.1 : 0.0;
	proom->hit[iwall]		= surfdata.hit;
	proom->norm[iwall]		= surfdata.norm;

	// update wall counter

	proom->iwall++;
	
	if (proom->iwall == DAS_CWALLS)
	{
		bool b_good_node_location;

		// calculate room mins, maxs, reflectivity etc

		b_good_node_location = DAS_CalcRoomProps( proom );

		// reset wall counter

		proom->iwall = 0;
		proom->broomready = b_good_node_location;	// room ready to check if good node location

		return b_good_node_location;	
	}

	return false;			// room not yet fully updated
}

// create entity enumerator for counting ents & summing volume of ents in room

class CDasEntEnum : public IPartitionEnumerator
{
	public:
	int m_count;		// # of ents in space
	float m_volume;		// space occupied by ents

	public:
	
	void Reset()
	{
		m_count = 0;
		m_volume = 0.0;
	}

	// called with each handle...

	IterationRetval_t EnumElement( IHandleEntity *pHandleEntity )
	{		
		float vol;

		// get bounding box of entity
		// Generate a collideable
		
		ICollideable *pCollideable = g_pEngineTraceClient->GetCollideable( pHandleEntity );

		if ( !pCollideable )
			return ITERATION_CONTINUE;

		// Check for solid

		if ( !IsSolid( pCollideable->GetSolid(), pCollideable->GetSolidFlags() ) )
			return ITERATION_CONTINUE;
		
		m_count++;
		
		// compute volume of space occupied by entity
		Vector mins = pCollideable->OBBMins();
		Vector maxs = pCollideable->OBBMaxs();
		
		vol = fabs((maxs.x - mins.x) * (maxs.y - mins.y) * (maxs.z - mins.z));

		m_volume += vol;	// add to total vol

		if (das_debug.GetInt() == 5)
		{
			// draw box around all objects detected

			Vector orig = pCollideable->GetCollisionOrigin();
			CDebugOverlay::AddBoxOverlay( orig, mins, maxs, pCollideable->GetCollisionAngles(), 255, 0, 255, 0, 60.0f );
		}

		return ITERATION_CONTINUE;
	}
};

// determine # of solid ents/props within detected room boundaries
// and set diffusion based on count of ents and spatial volume of ents

void DAS_SetDiffusion( das_room_t *proom )
{
	// BRJ 7/12/05
	// This was commented out because the y component of proom->room_mins, proom->room_maxs was never
	// being computed, causing a bogus box to be sent to the partition system. The results of
	// this computation (namely the diffusion + ent_count fields of das_room_t) were never being used. 
	// Therefore, we'll avoid the enumeration altogether

	proom->diffusion = 0.0f;
	proom->ent_count = 0;

	/*
	CDasEntEnum enumerator;
	SpatialPartitionListMask_t mask = PARTITION_CLIENT_SOLID_EDICTS;	// count only solid ents in room
	int count;
	float vol;
	float volroom;
	float dfn;
	
	enumerator.Reset();
	
	SpatialPartition()->EnumerateElementsInBox(mask, proom->room_mins, proom->room_maxs, true, &enumerator );	
	
	count = enumerator.m_count;
	vol = enumerator.m_volume;

	// compute diffusion from volume
	
	// how much space around player is filled with props?

	volroom = (proom->room_maxs.x - proom->room_mins.x) * (proom->room_maxs.y - proom->room_mins.y) * (proom->room_maxs.z - proom->room_mins.z);
	volroom = fabs(volroom);

	if ( !(int)volroom )
		volroom = 1.0;

	dfn = vol / volroom;		// % of total volume occupied by props

	dfn = clamp (dfn, 0.0, 1.0);

	proom->diffusion = dfn;
	proom->ent_count = count;
	*/
}

// debug routine to display current room params

void DAS_DisplayRoomDEBUG( das_room_t *proom, bool fnew, float preset )
{
	float dx,dy,dz;
	Vector ctr;
	float count;

	if (das_debug.GetInt() == 0)
		return;

	dx = proom->length_max / 12.0;
	dy = proom->width_max / 12.0;
	dz = proom->height_max / 12.0;
	
	float refl = proom->refl_avg;
	
	count = (float)(proom->ent_count);
	float fsky = (proom->bskyabove ? 1.0 : 0.0);

	if (fnew)
		DevMsg( "NEW DSP NODE: size:(%.0f,%.0f) height:(%.0f) dif %.4f : refl %.4f : cobj: %.0f : sky %.0f \n", dx, dy, dz, proom->diffusion, refl, count, fsky);

	if (!fnew && preset < 0.0)
		return;

	if (preset >= 0.0)
	{
		if (proom == NULL)
			return;

		DevMsg( "DSP PRESET: %.0f size:(%.0f,%.0f) height:(%.0f) dif %.4f : refl %.4f : cobj: %.0f : sky %.0f \n", preset, dx, dy, dz, proom->diffusion, refl, count, fsky);
		return;
	}

	// draw box around new node location

	Vector mins;
	Vector maxs;
	mins.Init(-8,-8,-16);
	maxs.Init(8,8,0);

	CDebugOverlay::AddBoxOverlay( proom->vplayer, mins, maxs, vec3_angle, 0, 0, 255, 0, 1000.0f );

	// draw red box around node origin

	mins.Init(-0.5,-0.5,-1.0);
	maxs.Init(0.5,0.5,0);

	CDebugOverlay::AddBoxOverlay( proom->vplayer, mins, maxs, vec3_angle, 255, 0, 0, 0, 1000.0f );

	CDebugOverlay::AddTextOverlay( proom->vplayer, 0, 10, 1.0, "DSP NODE" );
}

// check newly calculated room parameters against current stored params.
// if different, return true.
// NOTE: only call when all proom params have been calculated.
// return false if this is not a good location for creating a new node

bool DAS_CheckNewRoom( das_room_t *proom )
{	
	bool bnewroom;
	float dw,dw2,dr,ds,dh;
	int cchanged = 0;
	das_room_t *proom_prev = NULL;
	Vector2D v2d;
	Vector v3d;
	float dist;

	// player can't see previous node, determine if this is a good place to lay down 
	// a new node.  Get room at last seen node for comparison

	if (g_pdas_last_node)
		proom_prev = &(g_pdas_last_node->room);

	// no previous room node saw player, go create new room node

	if (!proom_prev)
	{
		bnewroom = true;
		goto check_ret;
	}

	// if player not at least n feet from last node, return false
	
	v3d = proom->vplayer - proom_prev->vplayer;
	v2d.Init(v3d.x, v3d.y);

	dist = Vector2DLength(v2d);

	if (dist <= DAS_DIST_MIN)
		return false;

	// see if room size has changed significantly since last node

	bnewroom = true;

	dw = 0.0;
	dw2 = 0.0;
	dh = 0.0;
	dr = 0.0;

	if ( proom_prev->width_max != 0 )
		dw = (float)proom->width_max / (float)proom_prev->width_max;	// max width delta

	if ( proom_prev->length_max != 0 )
		dw2 = (float)proom->length_max / (float)proom_prev->length_max;	// max length delta

	if ( proom_prev->height_max != 0 )
		dh = (float)proom->height_max / (float)proom_prev->height_max;	// max height delta

	if ( proom_prev->refl_avg != 0.0 )
		dr = proom->refl_avg / proom_prev->refl_avg;					// reflectivity delta

	ds = fabs( proom->sky_pct - proom_prev->sky_pct);					// sky hits delta

	if (dw > 1.0) dw = 1.0 / dw;
	if (dw2 > 1.0) dw = 1.0 / dw2;
	if (dh > 1.0) dh = 1.0 / dh;
	if (dr > 1.0) dr = 1.0 / dr;

	if ( (1.0 - dw) >= DAS_WIDTH_MIN )
		cchanged++;
		
	if ( (1.0 - dw2) >= DAS_WIDTH_MIN )
		cchanged++;

//	if ( (1.0 - dh) >= DAS_WIDTH_MIN )	// don't change room based on height change
//		cchanged++;

	// new room only if at least 1 changed

	if (cchanged >= 1)
		goto check_ret;

//	if ( (1.0 - dr) >= DAS_REFL_MIN )	// don't change room based on reflectivity change
//		goto check_ret;

//	if (ds >= DAS_SKYHIT_MIN )
//		goto check_ret;

	// new room if sky above changes state

	if (proom->bskyabove != proom_prev->bskyabove)
		goto check_ret;

	// room didn't change significantly, return false

	bnewroom = false;

check_ret:
	
	if ( bnewroom )
	{
		// if low ceiling detected < 112 units, and max height is > low ceiling height by 20%, discard - no change
		// this detects player in doorway, under pipe or narrow bridge
		
		if ( proom->lowceiling && (proom->lowceiling < proom->height_max))
		{
			float h = (float)(proom->lowceiling) / (float)proom->height_max;

			if (h < 0.8)
				return false;
		}

		DAS_SetDiffusion( proom );
	}

	DAS_DisplayRoomDEBUG( proom, bnewroom, -1.0 );

	return bnewroom;
}


extern int DSP_ConstructPreset( bool bskyabove, int width, int length, int height, float fdiffusion, float freflectivity, float *psurf_refl, int inode, int cnodes );

// select new dsp_room based on size, wall materials
// (or modulate params for current dsp)
// returns new preset # for dsp_automatic

int DAS_GetRoomDSP( das_room_t *proom, int inode )
{
	
	// preset constructor
	// call dsp module with params, get dsp preset back

	bool bskyabove		= proom->bskyabove;
	int width			= proom->width_max;
	int length			= proom->length_max;
	int height			= proom->height_max;
	float fdiffusion	= proom->diffusion;
	float freflectivity = proom->refl_avg;
	float surf_refl[6];

	// fill array of surface reflectivities - for left,right,front,back,ceiling,floor

	for (int i = 0; i < 6; i++)
		surf_refl[i] = proom->refl_walls[i];	
	
	return DSP_ConstructPreset( bskyabove, width, length, height, fdiffusion, freflectivity, surf_refl, inode, DAS_CNODES );

}


// main entry point: call once per frame to update dsp_automatic
// for automatic room detection.  dsp_room must be set to DSP_AUTOMATIC to enable.
// NOTE: this routine accumulates traceline information over several frames - it
// never traces more than 3 times per call, and normally just once per call.

void DAS_CheckNewRoomDSP( )
{
	VPROF("DAS_CheckNewRoomDSP");
	das_room_t *proom = &g_das_room;
	int dsp_preset;
	bool bRoom_ready = false;

	// if listener has not been updated, do nothing

	if ((listener_origin  == vec3_origin) && 
		(listener_forward == vec3_origin) &&
		(listener_right	  == vec3_origin) &&
		(listener_up	  == vec3_origin) )
		return;

	if ( !SND_IsInGame() )
		return;

	// make sure we init nodes & vectors first time this is called

	if ( !g_bdas_init_nodes )
	{
		g_bdas_init_nodes = 1;
		DAS_InitNodes();
	}

	if ( !DSP_CheckDspAutoEnabled())
	{
		// make sure room params are reinitialized each time autoroom is selected

		g_bdas_room_init = 0;		
		return;
	}

	if ( !g_bdas_room_init )
	{
		g_bdas_room_init = 1;

		DAS_InitAutoRoom( proom );
	}

	// get time
	
	double dtime = g_pSoundServices->GetHostTime();
	
	// compare to previous time - don't check for new room until timer expires
	// ie: wait at least DAS_AUTO_WAIT seconds between preset changes

	if ( fabs(dtime - proom->last_dsp_change) < DAS_AUTO_WAIT )
		return;

	// first, update room size parameters, see if room is ready to check - if room is updated, return true right away

	// 3 traces per frame while accumulating room size info

	for (int i = 0 ; i < 3; i++)
		bRoom_ready = DAS_UpdateRoomSize( proom );

	if (!bRoom_ready)
		return;
		

	if ( !g_bdas_create_new_node )
	{
		// next, check all nodes for line of sight to player - if all checked, return true right away

		if ( !DAS_CheckNextNode( proom ) )
		{
			// check all nodes first

			return;
		}

		// find out if any previously stored nodes can see player,
		// if so, get closest node's dsp preset

		dsp_preset = DAS_GetDspPreset( proom->bskyabove );

		if (dsp_preset != -1)
		{
			// an existing node can see player - just set preset and return
				
			if (dsp_preset != dsp_room_GetInt())
			{		
				// changed preset, so update timestamp

				proom->last_dsp_change = g_pSoundServices->GetHostTime();

				if (g_pdas_last_node)
					DAS_DisplayRoomDEBUG( &(g_pdas_last_node->room), false, (float)dsp_preset );
			}

			DSP_SetDspAuto( dsp_preset );

			goto check_new_room_exit;
		} 
	}

	g_bdas_create_new_node = true;

	// no nodes can see player, need to try to create a new one

	// check for 'new' room around player

	if ( DAS_CheckNewRoom( proom ) )
	{
		// new room found - update dsp_automatic

		dsp_preset = DAS_GetRoomDSP( proom, DAS_GetNextNodeIndex() );

		DSP_SetDspAuto( dsp_preset );
				
		// changed preset, so update timestamp

		proom->last_dsp_change = g_pSoundServices->GetHostTime();

		// save room as new node

		DAS_StoreNode( proom, dsp_preset );

		goto check_new_room_exit;
	}

check_new_room_exit:

	// reset new node creation flag - start checking for visible nodes again

	g_bdas_create_new_node = false;

	// reset room checking flag - start checking room around player again

	proom->broomready = false;

	// reset node checking flag - start checking nodes around player again

	DAS_ResetNodes();

	return;
}

// remap contents of volumes[] arrary if sound originates from player, or is music, and is 100% 'mono' 
// ie: same volume in all channels

void RemapPlayerOrMusicVols(  channel_t *ch, int volumes[CCHANVOLUMES/2], bool fplayersound, bool fmusicsound, float mono )
{
	VPROF_("RemapPlayerOrMusicVols", 2, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );

	if ( !fplayersound && !fmusicsound )
		return;	// no remapping

	if ( ch->flags.bSpeaker )
		return; // don't remap speaker sounds rebroadcast on player

	// get total volume

	float vol_total = 0.0;
	int k;

	for (k = 0; k < CCHANVOLUMES/2; k++)
		vol_total += (float)volumes[k];

	if ( !g_AudioDevice->IsSurround() )
	{
		if (mono < 1.0)
			return;

		// remap 2 chan non-spatialized versions of player and music sounds
		// note: this is required to keep volumes same as 4 & 5 ch cases!
		
		float vol_dist_music[] =  {1.0, 1.0};	// FL, FR music volumes
		float vol_dist_player[] = {1.0, 1.0};	// FL, FR player volumes
		float *pvol_dist;

		pvol_dist = (fplayersound ? vol_dist_player : vol_dist_music);

		for (k = 0; k < 2; k++)
			volumes[k] = clamp((int)(vol_total * pvol_dist[k]), 0, 255);

		return;
	}

	// surround sound configuration...

	if ( fplayersound ) // && (ch->bstereowav && ch->wavtype != CHAR_DIRECTIONAL && ch->wavtype != CHAR_DISTVARIANT) )
	{
		// NOTE: player sounds also get n% overall volume boost.
		
		//float vol_dist5[]   = {0.29, 0.29, 0.09, 0.09, 0.63};	// FL, FR, RL, RR, FC - 5 channel (mono source) volume distribution		
		//float vol_dist5st[] = {0.29, 0.29, 0.09, 0.09, 0.63};	// FL, FR, RL, RR, FC - 5 channel (stereo source) volume distribution
		
		float vol_dist5[]   = {0.30, 0.30, 0.09, 0.09, 0.59};	// FL, FR, RL, RR, FC - 5 channel (mono source) volume distribution		
		float vol_dist5st[] = {0.30, 0.30, 0.09, 0.09, 0.59};	// FL, FR, RL, RR, FC - 5 channel (stereo source) volume distribution
		
		float vol_dist4[]   = {0.50, 0.50, 0.15, 0.15, 0.00};	// FL, FR, RL, RR, 0  - 4 channel (mono source) volume distribution
		float vol_dist4st[] = {0.50, 0.50, 0.15, 0.15, 0.00};	// FL, FR, RL, RR, 0  - 4 channel (stereo source)volume distribution

		float *pvol_dist;
		
		if ( ch->flags.bstereowav && (ch->wavtype == CHAR_OMNI || ch->wavtype == CHAR_SPATIALSTEREO || ch->wavtype == 0))
		{
			pvol_dist = (g_AudioDevice->IsSurroundCenter() ? vol_dist5st : vol_dist4st);	
		}
		else
		{
			pvol_dist = (g_AudioDevice->IsSurroundCenter() ? vol_dist5 : vol_dist4);
		}

		for (k = 0; k < 5; k++)
			volumes[k] = clamp((int)(vol_total * pvol_dist[k]), 0, 255);

		return;
	}

	// Special case for music in surround mode

	if ( fmusicsound )
	{
		float vol_dist5[] = {0.5, 0.5, 0.25, 0.25, 0.0};	// FL, FR, RL, RR, FC - 5 channel distribution
		float vol_dist4[] = {0.5, 0.5, 0.25, 0.25, 0.0};	// FL, FR, RL, RR, 0  - 4 channel distribution
		float *pvol_dist;

		pvol_dist = (g_AudioDevice->IsSurroundCenter() ? vol_dist5 : vol_dist4);

		for (k = 0; k < 5; k++)
			volumes[k] = clamp((int)(vol_total * pvol_dist[k]), 0, 255);

		return;
	}

	return;
}

static int s_nSoundGuid = 0;

void SND_ActivateChannel( channel_t *pChannel )
{
	Q_memset( pChannel, 0, sizeof(*pChannel) );
	g_ActiveChannels.Add( pChannel );
	pChannel->guid = ++s_nSoundGuid;
}

/*
=================
SND_Spatialize
=================
*/
void SND_Spatialize(channel_t *ch)
{
	VPROF("SND_Spatialize");

    vec_t dist;
    Vector source_vec;
	Vector source_vec_DL;
	Vector source_vec_DR;
	Vector source_doppler_left;
	Vector source_doppler_right;

	bool fdopplerwav = false;
	bool fplaydopplerwav = false;
	bool fvalidentity;
	float gain;
	float scale = 1.0;
	bool fplayersound = false;
	bool fmusicsound = false;
	float mono = 0.0;
	bool bAttenuated = true;

	ch->dspface = 1.0;				// default facing direction: always facing player
	ch->dspmix = 0;					// default mix 0% dsp_room fx
	ch->distmix = 0;				// default 100% left (near) wav

#if !defined( _X360 )
	if ( ch->sfx && 
		ch->sfx->pSource && 
		ch->sfx->pSource->GetType() == CAudioSource::AUDIO_SOURCE_VOICE )
	{
		Voice_Spatialize( ch );
	}
#endif

	if ( IsSoundSourceLocalPlayer( ch->soundsource ) && !toolframework->InToolMode() )
	{
		// sounds coming from listener actually come from a short distance directly in front of listener
		// in tool mode however, the view entity is meaningless, since we're viewing from arbitrary locations in space
		fplayersound = true;
	}

	// assume 'dry', playeverwhere sounds are 'music' or 'voiceover'

	if ( ch->flags.bdry && ch->dist_mult <= 0 )
	{
		fmusicsound = true;
		fplayersound = false;
	}

	// update channel's position in case ent that made the sound is moving.
	QAngle source_angles;
	source_angles.Init(0.0, 0.0, 0.0);
	Vector entOrigin = ch->origin;
	
	bool looping = false;

	CAudioSource *pSource = ch->sfx ? ch->sfx->pSource : NULL;
	if ( pSource )
	{
		looping = pSource->IsLooped();
	}
	
	SpatializationInfo_t si;
	si.info.Set( 
		ch->soundsource,
		ch->entchannel,
		ch->sfx ? ch->sfx->getname() : "",
		ch->origin,
		ch->direction,
		ch->master_vol,
		DIST_MULT_TO_SNDLVL( ch->dist_mult ),
		looping,
		ch->pitch,
		listener_origin,
		ch->speakerentity );

	si.type = SpatializationInfo_t::SI_INSPATIALIZATION;
	si.pOrigin = &entOrigin;
	si.pAngles = &source_angles;
	si.pflRadius = NULL;
	if ( ch->soundsource != 0 && ch->radius == 0 )
	{
		si.pflRadius = &ch->radius;
	}

	{
		VPROF_("SoundServices->GetSoundSpatializtion", 2, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );
		fvalidentity = g_pSoundServices->GetSoundSpatialization( ch->soundsource, si );	
	}

	if ( ch->flags.bUpdatePositions )
	{
		AngleVectors( source_angles, &ch->direction );
		ch->origin = entOrigin;
	}
	else
	{
		VectorAngles( ch->direction, source_angles );
	}

	if ( ch->userdata != 0 )
	{
		g_pSoundServices->GetToolSpatialization( ch->userdata, ch->guid, si );
		if ( ch->flags.bUpdatePositions )
		{
			AngleVectors( source_angles, &ch->direction );
			ch->origin = entOrigin;
		}
	}

#if 0
	// !!!UNDONE - above code assumes the ENT hasn't been removed or respawned as another ent!
	// !!!UNDONE - fix this by flagging some entities (ie: glass) as immobile.  Don't spatialize them.
	if ( !fvalidendity)
	{
		// Turn off the sound while the entity doesn't exist or is not in the PVS.
		goto ClearAllVolumes;
	}
#endif // 0

	
	fdopplerwav = ((ch->wavtype == CHAR_DOPPLER) && !fplayersound);
	if ( fdopplerwav )
	{
		VPROF_("SND_Spatialize doppler", 2, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );
		Vector vnearpoint;				// point of closest approach to listener, 
										// along sound source forward direction (doppler wavs)

		vnearpoint = ch->origin;		// default nearest sound approach point

		// calculate point of closest approach for CHAR_DOPPLER wavs, replace source_vec

		fplaydopplerwav = SND_GetClosestPoint( ch, source_angles, vnearpoint );

		// if doppler sound was 'shot' away from listener, don't play it

		if ( !fplaydopplerwav )
			goto ClearAllVolumes;

		// find location of doppler left & doppler right points

		SND_GetDopplerPoints( ch, source_angles, vnearpoint, source_doppler_left, source_doppler_right);
	
		// source_vec_DL is vector from listener to doppler left point
		// source_vec_DR is vector from listener to doppler right point

		VectorSubtract(source_doppler_left, listener_origin, source_vec_DL );
		VectorSubtract(source_doppler_right, listener_origin, source_vec_DR );

		// normalized vectors to left and right doppler locations

		dist = VectorNormalize( source_vec_DL );
		VectorNormalize( source_vec_DR );

		// don't play doppler if out of range
		// unless recording in the tool, since we may play back in range
		if ( dist > DOPPLER_RANGE_MAX && !toolframework->IsToolRecording() )
			goto ClearAllVolumes;
	}
	else
	{
		// source_vec is vector from listener to sound source

		if ( fplayersound )
		{
			// get 2d forward direction vector, ignoring pitch angle
			Vector listener_forward2d;

			ConvertListenerVectorTo2D( &listener_forward2d, &listener_right );

			// player sounds originate from 1' in front of player, 2d

			VectorMultiply(listener_forward2d, 12.0, source_vec );
		}
		else
		{
			VectorSubtract(ch->origin, listener_origin, source_vec);
		}

		// normalize source_vec and get distance from listener to source

		dist = VectorNormalize( source_vec );
	}
	
	// calculate dsp mix based on distance to listener & sound level (linear approximation)

	ch->dspmix = SND_GetDspMix( ch, dist );

	// calculate sound source facing direction for CHAR_DIRECTIONAL wavs

	if ( !fplayersound )
	{
		ch->dspface = SND_GetFacingDirection( ch, source_angles );
		
		// calculate mixing parameter for CHAR_DISTVAR wavs

		ch->distmix = SND_GetDistanceMix( ch, dist );
	}

	// for sounds with a radius, spatialize left/right/front/rear evenly within the radius

	if ( ch->radius > 0 && dist < ch->radius && !fdopplerwav )
	{
		float interval = ch->radius * 0.5;
		mono = dist - interval;
		if ( mono < 0.0 )
			mono = 0.0;
		mono /= interval;
		
		mono = 1.0 - mono;

		// mono is 0.0 -> 1.0 from radius 100% to radius 50%
	}

	// don't pan sounds with no attenuation
	if ( ch->dist_mult <= 0 && !fdopplerwav )
	{
		// sound is centered left/right/front/back
		
		mono = 1.0;
		bAttenuated = false;
	}

	if ( ch->wavtype == CHAR_OMNI )
	{
		// omni directional sound sources are mono mix, all speakers
		// ie: they only attenuate by distance, not by source direction.

		mono = 1.0;
		bAttenuated = false;
	}

	// calculate gain based on distance, atmospheric attenuation, interposed objects
	// perform compression as gain approaches 1.0

	gain = SND_GetGain( ch, fplayersound, fmusicsound, looping, dist, bAttenuated );

	// map gain through global mixer by soundtype

	// gain *= SND_GetVolFromSoundtype( ch->soundtype );
	int last_mixgroupid;

	gain *= MXR_GetVolFromMixGroup( ch->mixgroups, &last_mixgroupid );

	// if playing a word, get volume scale of word - scale gain
		
	scale = VOX_GetChanVol(ch);

	gain *= scale;

	// save spatialized volume and mixgroupid for display later

	ch->last_mixgroupid = last_mixgroupid;

	if ( fdopplerwav )
	{
		VPROF_("SND_Spatialize doppler", 2, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );
		// fill out channel volumes for both doppler sound source locations
		int volumes[CCHANVOLUMES/2];

		// left doppler location

		g_AudioDevice->SpatializeChannel( volumes,  ch->master_vol, source_vec_DL, gain, mono );
		
		// load volumes into channel as crossfade targets

		ChannelSetVolTargets( ch, volumes, IFRONT_LEFT, CCHANVOLUMES/2 );

		// right doppler location

		g_AudioDevice->SpatializeChannel( volumes, ch->master_vol, source_vec_DR, gain, mono );		
		
		// load volumes into channel as crossfade targets

		ChannelSetVolTargets( ch, volumes, IFRONT_LEFTD, CCHANVOLUMES/2 );
	}
	else
	{
		// fill out channel volumes for single sound source location
		int volumes[CCHANVOLUMES/2];

		g_AudioDevice->SpatializeChannel( volumes, ch->master_vol, source_vec, gain, mono );
		
		// Special case for stereo sounds originating from player in surround mode
		// and special case for musci: remap volumes directly to channels.
	
		RemapPlayerOrMusicVols( ch, volumes, fplayersound, fmusicsound, mono );

		// load volumes into channel as crossfade volume targets

		ChannelSetVolTargets( ch, volumes, IFRONT_LEFT, CCHANVOLUMES/2 );
	}


	// prevent left/right/front/rear/center volumes from changing too quickly & producing pops

	ChannelUpdateVolXfade( ch );

	// end of first time spatializing sound

	if ( SND_IsInGame() || toolframework->InToolMode() )
	{
		ch->flags.bfirstpass = false;
	}

	// calculate total volume for display later
	ch->last_vol = gain * (ch->master_vol/255.0);

	return;

ClearAllVolumes:

	// Clear all volumes and return. 
	// This shuts the sound off permanently.

	ChannelClearVolumes( ch );

	// end of first time spatializing sound

	ch->flags.bfirstpass = false;
}           

ConVar snd_defer_trace("snd_defer_trace","1");
void SND_SpatializeFirstFrameNoTrace( channel_t *pChannel)
{
	if ( snd_defer_trace.GetBool() )
	{
		// set up tracing state to be non-obstructed
		pChannel->flags.bfirstpass = false;
		pChannel->flags.bTraced = true;
		pChannel->ob_gain = 1.0;
		pChannel->ob_gain_inc = 1.0;
		pChannel->ob_gain_target = 1.0;
		// now spatialize without tracing
		SND_Spatialize(pChannel);
		// now reset tracing state to firstpass so the trace gets done on next spatialize
		pChannel->ob_gain = 0.0;
		pChannel->ob_gain_inc = 0.0;
		pChannel->ob_gain_target = 0.0;
		pChannel->flags.bfirstpass = true;
		pChannel->flags.bTraced = false;
	}
	else
	{
		pChannel->ob_gain = 0.0;
		pChannel->ob_gain_inc = 0.0;
		pChannel->ob_gain_target = 0.0;
		pChannel->flags.bfirstpass = true;
		pChannel->flags.bTraced = false;
		SND_Spatialize(pChannel);
	}
}


// search through all channels for a channel that matches this
// soundsource, entchannel and sfx, and perform alteration on channel
// as indicated by 'flags' parameter. If shut down request and
// sfx contains a sentence name, shut off the sentence.
// returns TRUE if sound was altered,
// returns FALSE if sound was not found (sound is not playing)

int S_AlterChannel( int soundsource, int entchannel, CSfxTable *sfx, int vol, int pitch, int flags )
{
	THREAD_LOCK_SOUND();
	int ch_idx;

	const char *name = sfx->getname();
	if ( name && TestSoundChar( name, CHAR_SENTENCE ) )
	{
		// This is a sentence name.
		// For sentences: assume that the entity is only playing one sentence
		// at a time, so we can just shut off
		// any channel that has ch->isentence >= 0 and matches the
		// soundsource.

		CChannelList list;
		g_ActiveChannels.GetActiveChannels( list );
		for ( int i = 0; i < list.Count(); i++ )
		{
			ch_idx = list.GetChannelIndex(i);
			if (channels[ch_idx].soundsource == soundsource
				&& channels[ch_idx].entchannel == entchannel
				&& channels[ch_idx].sfx != NULL )
			{

				if (flags & SND_CHANGE_PITCH)
					channels[ch_idx].basePitch = pitch;
				
				if (flags & SND_CHANGE_VOL)
					channels[ch_idx].master_vol = vol;
				
				if (flags & SND_STOP)
				{
					S_FreeChannel(&channels[ch_idx]);
				}
			
				return TRUE;
			}
		}
		// channel not found
		return FALSE;

	}

	// regular sound or streaming sound
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );

	bool bSuccess = false;

	for ( int i = 0; i < list.Count(); i++ )
	{
		ch_idx = list.GetChannelIndex(i);
		if ( channels[ch_idx].soundsource == soundsource && 
			 ( ( flags & SND_IGNORE_NAME ) || 
			   ( channels[ch_idx].entchannel == entchannel && channels[ch_idx].sfx == sfx ) ) )
		{
			if (flags & SND_CHANGE_PITCH)
				channels[ch_idx].basePitch = pitch;
			
			if (flags & SND_CHANGE_VOL)
				channels[ch_idx].master_vol = vol;
			
			if (flags & SND_STOP)
			{
				S_FreeChannel(&channels[ch_idx]);
			}
		
			if ( ( flags & SND_IGNORE_NAME ) == 0 )
				return TRUE;
			else
				bSuccess = true;
		}
   }

	return ( bSuccess ) ? ( TRUE ) : ( FALSE );
}

// set channel flags during initialization based on 
// source name

void S_SetChannelWavtype( channel_t *target_chan, CSfxTable *pSfx )
{	
	// if 1st or 2nd character of name is CHAR_DRYMIX, sound should be mixed dry with no dsp (ie: music)

	if ( TestSoundChar(pSfx->getname(), CHAR_DRYMIX) )
		target_chan->flags.bdry = true;
	else
		target_chan->flags.bdry = false;

	if ( TestSoundChar(pSfx->getname(), CHAR_FAST_PITCH) )
		target_chan->flags.bfast_pitch = true;
	else
		target_chan->flags.bfast_pitch = false;

	// get sound spatialization encoding
	
	target_chan->wavtype = 0;

	if ( TestSoundChar( pSfx->getname(), CHAR_DOPPLER ))
		target_chan->wavtype = CHAR_DOPPLER;
	
	if ( TestSoundChar( pSfx->getname(), CHAR_DIRECTIONAL ))
		target_chan->wavtype = CHAR_DIRECTIONAL;

	if ( TestSoundChar( pSfx->getname(), CHAR_DISTVARIANT ))
		target_chan->wavtype = CHAR_DISTVARIANT;

	if ( TestSoundChar( pSfx->getname(), CHAR_OMNI ))
		target_chan->wavtype = CHAR_OMNI;

	if ( TestSoundChar( pSfx->getname(), CHAR_SPATIALSTEREO ))
		target_chan->wavtype = CHAR_SPATIALSTEREO;
}


// Sets bstereowav flag in channel if source is true stere wav
// sets default wavtype for stereo wavs to CHAR_DISTVARIANT - 
// ie: sound varies with distance (left is close, right is far)
// Must be called after S_SetChannelWavtype

void S_SetChannelStereo( channel_t *target_chan, CAudioSource *pSource )
{
	if ( !pSource )
	{
		target_chan->flags.bstereowav = false;
		return;
	}
	
	// returns true only if source data is a stereo wav file. 
	// ie: mp3, voice, sentence are all excluded.

	target_chan->flags.bstereowav = pSource->IsStereoWav();

	// Default stereo wavtype:

	// just player standard stereo wavs on player entity - no override.

	if ( IsSoundSourceLocalPlayer( target_chan->soundsource ) )
		return;
	
	// default wavtype for stereo wavs is OMNI - except for drymix or sounds with 0 attenuation

	if ( target_chan->flags.bstereowav && !target_chan->wavtype && !target_chan->flags.bdry && target_chan->dist_mult )
		// target_chan->wavtype = CHAR_DISTVARIANT;
		target_chan->wavtype = CHAR_OMNI;
}

// =======================================================================
// Channel volume management routines:

// channel volumes crossfade between values over time
// to prevent pops due to rapid spatialization changes
// =======================================================================

// return true if all volumes and target volumes for channel are less/equal to 'vol'

bool BChannelLowVolume( channel_t *pch, int vol_min )
{
	int max = -1;
	int max_target = -1;
	int vol;
	int vol_target;

	for (int i = 0; i < CCHANVOLUMES; i++)
	{
		vol = (int)(pch->fvolume[i]);
		vol_target = (int)(pch->fvolume_target[i]);

		if (vol > max)
			max = vol;

		if (vol_target > max_target)
			max_target = vol_target;
	}
		
	return (max <= vol_min && max_target <= vol_min);
}

// Get the loudest actual volume for a channel (not counting targets).
float ChannelLoudestCurVolume( const channel_t * RESTRICT pch )
{
	float loudest = pch->fvolume[0];
	for (int i = 1; i < CCHANVOLUMES; i++)
	{
		loudest = fpmax(loudest, pch->fvolume[i]);
	}
	return loudest;
}

// clear all volumes, targets, crossfade increments

void ChannelClearVolumes( channel_t *pch )
{
	for (int i = 0; i < CCHANVOLUMES; i++)
	{
		pch->fvolume[i] = 0.0;
		pch->fvolume_target[i] = 0.0;
		pch->fvolume_inc[i] = 0.0;
	}
}

// return current volume as integer

int ChannelGetVol( channel_t *pch, int ivol )
{
	Assert(ivol < CCHANVOLUMES);
	return (int)(pch->fvolume[ivol]);	
}

// return maximum current output volume 

int ChannelGetMaxVol( channel_t *pch )
{
	float max = 0.0;
	
	for (int i = 0; i < CCHANVOLUMES; i++)
	{
		if (pch->fvolume[i] > max)
			max = pch->fvolume[i];
	}

	return (int)max;
}

// set current volume (clears crossfading - instantaneous value change)

void ChannelSetVol( channel_t *pch, int ivol, int vol )
{
	Assert(ivol < CCHANVOLUMES);
	
	pch->fvolume[ivol] = (float)(clamp(vol, 0, 255));	

	pch->fvolume_target[ivol] = pch->fvolume[ivol];
	pch->fvolume_inc[ivol] = 0.0;
}

// copy current channel volumes into target array, starting at ivol, copying cvol entries

void ChannelCopyVolumes( channel_t *pch, int *pvolume_dest, int ivol_start, int cvol )
{
	Assert (ivol_start < CCHANVOLUMES);
	Assert (ivol_start + cvol <= CCHANVOLUMES);

	for (int i = 0; i < cvol; i++)
		pvolume_dest[i] = (int)(pch->fvolume[i + ivol_start]);
}

// volume has hit target, shut off crossfading increment

inline void ChannelStopVolXfade( channel_t *pch, int ivol )
{
	pch->fvolume[ivol] = pch->fvolume_target[ivol];
	pch->fvolume_inc[ivol] = 0.0;
}

#define	VOL_XFADE_TIME	0.070	// channel volume crossfade time in seconds

#define VOL_INCR_MAX	20.0	// never change volume by more than +/-N units per frame

// set volume target and volume increment (for crossfade) for channel & speaker

void ChannelSetVolTarget( channel_t *pch, int ivol, int volume_target )
{
	float frametime = g_pSoundServices->GetHostFrametime();
	float speed;
	float vol_target = (float)(clamp(volume_target, 0, 255));
	float vol_current;

	Assert(ivol < CCHANVOLUMES);
	
	// set volume target

	pch->fvolume_target[ivol] = vol_target;

	// current volume

	vol_current = pch->fvolume[ivol];

	// if first time spatializing, set target = volume with no crossfade
	// if current & target volumes are close - don't bother crossfading

	if ( pch->flags.bfirstpass || (fabs(vol_target - vol_current) < 5.0)) 
	{
		// set current volume = target, no increment

		ChannelStopVolXfade( pch, ivol);
		return;
	}

	// get crossfade increment 'speed' (volume change per frame)

	speed = ( frametime / VOL_XFADE_TIME ) * (vol_target - vol_current);

	// make sure we never increment by more than +/- VOL_INCR_MAX volume units per frame
	
	speed = clamp(speed, (float) -VOL_INCR_MAX, (float) VOL_INCR_MAX);

	pch->fvolume_inc[ivol] = speed;	
}

// set volume targets, using array pvolume as source volumes.
// set into channel volumes starting at ivol_offset index
// set cvol volumes

void ChannelSetVolTargets( channel_t *pch, int *pvolumes, int ivol_offset, int cvol )
{
	int volume_target;
	
	Assert(ivol_offset + cvol <= CCHANVOLUMES);

	for (int i = 0; i < cvol; i++)
	{
		volume_target = pvolumes[i];

		ChannelSetVolTarget( pch, ivol_offset + i, volume_target );
	}
}


// Call once per frame, per channel:
// update all volume crossfades, from fvolume -> fvolume_target
// if current volume reaches target, set increment to 0

void ChannelUpdateVolXfade( channel_t *pch )
{
	float fincr;

	for (int i = 0; i < CCHANVOLUMES; i++)
	{
		fincr = pch->fvolume_inc[i];

		if (fincr != 0.0)
		{
			pch->fvolume[i] += fincr;

			// test for hit target

			if (fincr > 0.0)
			{
				if (pch->fvolume[i] >= pch->fvolume_target[i])
					ChannelStopVolXfade( pch, i );
			}
			else
			{
				if (pch->fvolume[i] <= pch->fvolume_target[i])
					ChannelStopVolXfade( pch, i );
			}
		}
	}
}

// =======================================================================
// S_StartDynamicSound
// =======================================================================
// Start a sound effect for the given entity on the given channel (ie; voice, weapon etc).  
// Try to grab a channel out of the 8 dynamic spots available.
// Currently used for looping sounds, streaming sounds, sentences, and regular entity sounds.
// NOTE: volume is 0.0 - 1.0 and attenuation is 0.0 - 1.0 when passed in.
// Pitch changes playback pitch of wave by % above or below 100.  Ignored if pitch == 100

// NOTE: it's not a good idea to play looping sounds through StartDynamicSound, because
// if the looping sound starts out of range, or is bumped from the buffer by another sound
// it will never be restarted.  Use StartStaticSound (pass CHAN_STATIC to EMIT_SOUND or
// SV_StartSound.

int S_StartDynamicSound( StartSoundParams_t& params )
{
	Assert( params.staticsound == false );

	channel_t *target_chan;
	int		vol;

	if ( !g_AudioDevice || !g_AudioDevice->IsActive())
		return 0;

	if (!params.pSfx)
		return 0;

	// For debugging to see the actual name of the sound...
	char sndname[ MAX_OSPATH ];
	Q_strncpy( sndname, params.pSfx->getname(), sizeof( sndname ) );

	// Msg("Start sound %s\n", pSfx->getname() );

	// override the entchannel to CHAN_STREAM if this is a 
	// non-voice stream sound.
	if ( TestSoundChar(sndname, CHAR_STREAM ) && params.entchannel != CHAN_VOICE && params.entchannel != CHAN_VOICE2 )
		params.entchannel = CHAN_STREAM;
	
	vol = params.fvol*255;
	
	if (vol > 255)
	{
		DevMsg("S_StartDynamicSound: %s volume > 255", sndname );
		vol = 255;
	}

	THREAD_LOCK_SOUND();

	if ( params.flags & (SND_STOP|SND_CHANGE_VOL|SND_CHANGE_PITCH) )
	{
		if ( S_AlterChannel( params.soundsource, params.entchannel, params.pSfx, vol, params.pitch, params.flags) )
			return 0;
		if ( params.flags & SND_STOP )
			return 0;
		// fall through - if we're not trying to stop the sound, 
		// and we didn't find it (it's not playing), go ahead and start it up
	}

	if (params.pitch == 0)
	{
		DevMsg ("Warning: S_StartDynamicSound (%s) Ignored, called with pitch 0\n", sndname );
		return 0;
	}

	// pick a channel to play on
	target_chan = SND_PickDynamicChannel(params.soundsource, params.entchannel, params.origin, params.pSfx, params.delay, (params.flags & SND_DO_NOT_OVERWRITE_EXISTING_ON_CHANNEL) != 0 );
	if ( !target_chan )
		return 0;

	int channelIndex = (int)( target_chan - channels );
	g_AudioDevice->ChannelReset( params.soundsource, channelIndex, target_chan->dist_mult );

#ifdef DEBUG_CHANNELS
	{
		char szTmp[128];
		Q_snprintf(szTmp, sizeof( szTmp ), "Sound %s playing on Dynamic game channel %d\n", sndname, IWavstreamOfCh(target_chan));
		Plat_DebugString(szTmp);
	}
#endif
	
	bool bIsSentence = TestSoundChar( sndname, CHAR_SENTENCE );

	SND_ActivateChannel( target_chan );
	ChannelClearVolumes( target_chan );

	target_chan->userdata = params.userdata;
	target_chan->initialStreamPosition = params.initialStreamPosition;

	VectorCopy(params.origin, target_chan->origin);
	VectorCopy(params.direction, target_chan->direction);
	
	// never update positions if source entity is 0
	target_chan->flags.bUpdatePositions = params.bUpdatePositions && (params.soundsource == 0 ? 0 : 1);

	// reference_dist / (reference_power_level / actual_power_level)
	target_chan->flags.m_bCompatibilityAttenuation = SNDLEVEL_IS_COMPATIBILITY_MODE( params.soundlevel );
	if ( target_chan->flags.m_bCompatibilityAttenuation )
	{
		// Translate soundlevel from its 'encoded' value to a real soundlevel that we can use in the sound system.
		params.soundlevel = SNDLEVEL_FROM_COMPATIBILITY_MODE( params.soundlevel );
	}

	target_chan->dist_mult = SNDLVL_TO_DIST_MULT( params.soundlevel );

	S_SetChannelWavtype( target_chan, params.pSfx );

	target_chan->master_vol = vol;
	target_chan->soundsource = params.soundsource;
	target_chan->entchannel = params.entchannel;
	target_chan->basePitch = params.pitch;
	target_chan->flags.isSentence = false;
	target_chan->radius = 0;
	target_chan->sfx = params.pSfx;
	target_chan->special_dsp = params.specialdsp;
	target_chan->flags.fromserver = params.fromserver;
	target_chan->flags.bSpeaker = (params.flags & SND_SPEAKER) ? 1 : 0;
	target_chan->speakerentity = params.speakerentity;

	target_chan->flags.m_bShouldPause = (params.flags & SND_SHOULDPAUSE) ? 1 : 0;

	// initialize dsp room mixing params
	target_chan->dsp_mix_min = -1;
	target_chan->dsp_mix_max = -1;

	CAudioSource *pSource = NULL;

	if ( bIsSentence )
	{
		// this is a sentence
		// link all words and load the first word

		// NOTE: sentence names stored in the cache lookup are
		// prepended with a '!'.  Sentence names stored in the
		// sentence file do not have a leading '!'. 
		VOX_LoadSound( target_chan, PSkipSoundChars( sndname ) );
	}
	else
	{
		// regular or streamed sound fx
		pSource = S_LoadSound( params.pSfx, target_chan );
		if ( pSource && !IsValidSampleRate( pSource->SampleRate() ) )
		{
			Warning( "*** Invalid sample rate (%d) for sound '%s'.\n", pSource->SampleRate(), sndname );
		}

		if ( !pSource && !params.pSfx->m_bIsLateLoad )
		{
			Warning( "Failed to load sound \"%s\", file probably missing from disk/repository\n", sndname );
		}

	}

	if (!target_chan->pMixer)
	{
		// couldn't load the sound's data, or sentence has 0 words (this is not an error)
		S_FreeChannel( target_chan );
		return 0;
	}

	int nSndShowStart = snd_showstart.GetInt();

	// TODO: Support looping sounds through speakers.
	// If the sound is from a speaker, and it's looping, ignore it.
	if ( target_chan->flags.bSpeaker )
	{
		if ( params.pSfx->pSource && params.pSfx->pSource->IsLooped() )
		{
			if (nSndShowStart > 0 && nSndShowStart < 7 && nSndShowStart != 4)
			{
				DevMsg("DynamicSound : Speaker ignored looping sound: %s\n", sndname );
			}

			S_FreeChannel( target_chan );
			return 0;
		}
	}

	S_SetChannelStereo( target_chan, pSource );

	if (nSndShowStart == 5)	
	{
		snd_showstart.SetValue(6);		// debug: show gain for next spatialize only
		nSndShowStart = 6;
	}

	// get sound type before we spatialize
	MXR_GetMixGroupFromSoundsource( target_chan, params.soundsource, params.soundlevel );

	// skip the trace on the first spatialization.  This channel may be stolen
	// by another sound played this frame.  Defer the trace to the mix loop
	SND_SpatializeFirstFrameNoTrace(target_chan);

	if (nSndShowStart > 0 && nSndShowStart < 7 && nSndShowStart != 4)
	{
		channel_t *pTargetChan = target_chan;

		DevMsg( "DynamicSound %s : src %d : channel %d : %d dB : vol %.2f : time %.3f\n", sndname, params.soundsource, params.entchannel, params.soundlevel, params.fvol, g_pSoundServices->GetHostTime() );
		if (nSndShowStart == 2 || nSndShowStart == 5)
			DevMsg( "\t dspmix %1.2f : distmix %1.2f : dspface %1.2f : lvol %1.2f : cvol %1.2f : rvol %1.2f : rlvol %1.2f : rrvol %1.2f\n", 
				pTargetChan->dspmix, pTargetChan->distmix, pTargetChan->dspface,
				pTargetChan->fvolume[IFRONT_LEFT], pTargetChan->fvolume[IFRONT_CENTER], pTargetChan->fvolume[IFRONT_RIGHT], pTargetChan->fvolume[IREAR_LEFT], pTargetChan->fvolume[IREAR_RIGHT] );
		if (nSndShowStart == 3)
			DevMsg( "\t x: %4f y: %4f z: %4f\n", pTargetChan->origin.x, pTargetChan->origin.y, pTargetChan->origin.z );

		if ( snd_visualize.GetInt() )
		{
			CDebugOverlay::AddTextOverlay( pTargetChan->origin, 2.0f, sndname );
		}
	}

	// If a client can't hear a sound when they FIRST receive the StartSound message,
	// the client will never be able to hear that sound. This is so that out of 
	// range sounds don't fill the playback buffer.  For streaming sounds, we bypass this optimization.
	
	if ( BChannelLowVolume( target_chan, 0 ) && !toolframework->IsToolRecording() )
	{
		// Looping sounds don't use this optimization because they should stick around until they're killed.
		// Also bypass for speech (GetSentence)
		if ( !params.pSfx->pSource || (!params.pSfx->pSource->IsLooped() && !params.pSfx->pSource->GetSentence()) )
		{
			// if this is long sound, play the whole thing.
			if (!SND_IsLongWave( target_chan ))
			{
				// DevMsg("S_StartDynamicSound: spatialized to 0 vol & ignored %s", sndname);
				S_FreeChannel( target_chan );
				return 0;		// not audible at all
			}
		}
	}

	// Init client entity mouth movement vars
	target_chan->flags.m_bIgnorePhonemes = ( params.flags & SND_IGNORE_PHONEMES ) != 0;
	SND_InitMouth(target_chan);

	if ( IsX360() && params.delay < 0 )
	{
		params.delay = 0;
		target_chan->flags.delayed_start = true;
	}

	// Pre-startup delay.  Compute # of samples over which to mix in zeros from data source before
	//  actually reading first set of samples
	if ( params.delay != 0.0f )
	{
		Assert( target_chan->sfx );
		Assert( target_chan->sfx->pSource );

		// delay count is computed at the sampling rate of the source because the output rate will 
		// match the source rate when the sound is mixed
		float rate = target_chan->sfx->pSource->SampleRate();
		int delaySamples = (int)( params.delay * rate );

		if ( params.delay > 0 )
		{
			target_chan->pMixer->SetStartupDelaySamples( delaySamples );
			target_chan->flags.delayed_start = true;
		}
		else
		{
			int skipSamples = -delaySamples;
			int totalSamples = target_chan->sfx->pSource->SampleCount();
			if ( target_chan->sfx->pSource->IsLooped() )
			{
				skipSamples = skipSamples % totalSamples;
			}
			if ( skipSamples >= totalSamples )
			{
				S_FreeChannel( target_chan );
				return 0;
			}
			target_chan->pitch = target_chan->basePitch * 0.01f;
			target_chan->pMixer->SkipSamples( target_chan, skipSamples, rate, 0 );
			target_chan->ob_gain_target		= 1.0f;
			target_chan->ob_gain			= 1.0f;
			target_chan->ob_gain_inc		= 0.0;
			target_chan->flags.bfirstpass	= false;
			target_chan->flags.delayed_start = true;
		}
	}

	g_pSoundServices->OnSoundStarted( target_chan->guid, params, sndname );
	return target_chan->guid;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *name - 
// Output : CSfxTable
//-----------------------------------------------------------------------------
CSfxTable *S_DummySfx( const char *name )
{
	dummySfx.setname( name );
	return &dummySfx;
}

/*
=================
S_StartStaticSound
=================
Start playback of a sound, loaded into the static portion of the channel array.
Currently, this should be used for looping ambient sounds, looping sounds
that should not be interrupted until complete, non-creature sentences,
and one-shot ambient streaming sounds.  Can also play 'regular' sounds one-shot,
in case designers want to trigger regular game sounds.
Pitch changes playback pitch of wave by % above or below 100.  Ignored if pitch == 100

  NOTE: volume is 0.0 - 1.0 and attenuation is 0.0 - 1.0 when passed in.
*/

int S_StartStaticSound( StartSoundParams_t& params )
{
	Assert( params.staticsound == true );

	channel_t *ch;
	CAudioSource *pSource = NULL;

	if ( !g_AudioDevice->IsActive() )
		return 0;

	if ( !params.pSfx )
		return 0;

	// For debugging to see the actual name of the sound...
	char sndname[ MAX_OSPATH ];
	Q_strncpy( sndname, params.pSfx->getname(), sizeof( sndname ) );
//	Msg("Start static sound %s\n", pSfx->getname() );

	int vol = params.fvol * 255;
	if ( vol > 255 )
	{
		DevMsg( "S_StartStaticSound: %s volume > 255", sndname );
		vol = 255;
	}

	int nSndShowStart = snd_showstart.GetInt();

	if ((params.flags & SND_STOP) && nSndShowStart > 0)
		DevMsg("S_StartStaticSound: %s Stopped.\n", sndname);

	if ((params.flags & SND_STOP) || (params.flags & SND_CHANGE_VOL) || (params.flags & SND_CHANGE_PITCH))
	{
		if (S_AlterChannel(params.soundsource, params.entchannel, params.pSfx, vol, params.pitch, params.flags) || (params.flags & SND_STOP))
			return 0;
	}
	
	if ( params.pitch == 0 )
	{
		DevMsg( "Warning: S_StartStaticSound Ignored, called with pitch 0\n");
		return 0;
	}
	
	// First, make sure the sound source entity is even in the PVS.
	float flSoundRadius = 0.0f;	
	
	bool looping = false;

	/*
	CAudioSource *pSource = pSfx ? pSfx->pSource : NULL;
	if ( pSource )
	{
		looping = pSource->IsLooped();
	}
	*/

	SpatializationInfo_t si;
	si.info.Set( 
		params.soundsource,
		params.entchannel,
		params.pSfx ? sndname : "",
		params.origin,
		params.direction,
		vol,
		params.soundlevel,
		looping,
		params.pitch,
		listener_origin,
		params.speakerentity );

	si.type = SpatializationInfo_t::SI_INCREATION;
	
	si.pOrigin = NULL;
	si.pAngles = NULL;
	si.pflRadius = &flSoundRadius;

	g_pSoundServices->GetSoundSpatialization( params.soundsource, si );

	// pick a channel to play on from the static area
	THREAD_LOCK_SOUND();

	ch = SND_PickStaticChannel(params.soundsource, params.pSfx); // Autolooping sounds are always fixed origin(?)
	if ( !ch )
		return 0;

	SND_ActivateChannel( ch );
	ChannelClearVolumes( ch );

	ch->userdata = params.userdata;
	ch->initialStreamPosition = params.initialStreamPosition;

	if ( ch->userdata != 0 )
	{
		g_pSoundServices->GetToolSpatialization( ch->userdata, ch->guid, si );
	}

	int channelIndex = ch - channels;
	g_AudioDevice->ChannelReset( params.soundsource, channelIndex, ch->dist_mult );

#ifdef DEBUG_CHANNELS
	{
		char szTmp[128];
		Q_snprintf(szTmp, sizeof( szTmp ), "Sound %s playing on Static game channel %d\n", sfxin->name, IWavstreamOfCh(ch));
		Plat_DebugString(szTmp);
	}
#endif
	
	if ( TestSoundChar(sndname, CHAR_SENTENCE) )
	{
		// this is a sentence. link words to play in sequence.

		// NOTE: sentence names stored in the cache lookup are
		// prepended with a '!'.  Sentence names stored in the
		// sentence file do not have a leading '!'. 

		// link all words and load the first word
		VOX_LoadSound( ch, PSkipSoundChars(sndname) );
	}
	else
	{
		// load regular or stream sound
		pSource = S_LoadSound( params.pSfx, ch );
		if ( pSource && !IsValidSampleRate( pSource->SampleRate() ) )
		{
			Warning( "*** Invalid sample rate (%d) for sound '%s'.\n", pSource->SampleRate(), sndname );
		}

		if ( !pSource && !params.pSfx->m_bIsLateLoad )
		{
			Warning( "Failed to load sound \"%s\", file probably missing from disk/repository\n", sndname );
		}

		ch->sfx = params.pSfx;
		ch->flags.isSentence = false;
	}

	if ( !ch->pMixer )
	{
		// couldn't load sounds' data, or sentence has 0 words (not an error)
		S_FreeChannel( ch );
		return 0;
	}

	VectorCopy (params.origin, ch->origin);
	VectorCopy (params.direction, ch->direction);

	// never update positions if source entity is 0
	ch->flags.bUpdatePositions = params.bUpdatePositions && (params.soundsource == 0 ? 0 : 1);

	ch->master_vol = vol;

	ch->flags.m_bCompatibilityAttenuation = SNDLEVEL_IS_COMPATIBILITY_MODE( params.soundlevel );
	if ( ch->flags.m_bCompatibilityAttenuation )
	{
		// Translate soundlevel from its 'encoded' value to a real soundlevel that we can use in the sound system.
		params.soundlevel = SNDLEVEL_FROM_COMPATIBILITY_MODE( params.soundlevel );
	}

	ch->dist_mult = SNDLVL_TO_DIST_MULT( params.soundlevel );
	
	S_SetChannelWavtype( ch, params.pSfx );

	ch->basePitch = params.pitch;
	ch->soundsource = params.soundsource;
	ch->entchannel = params.entchannel;
	ch->special_dsp = params.specialdsp;
	ch->flags.fromserver = params.fromserver;
	ch->flags.bSpeaker = (params.flags & SND_SPEAKER) ? 1 : 0;
	ch->speakerentity = params.speakerentity;

	ch->flags.m_bShouldPause = (params.flags & SND_SHOULDPAUSE) ? 1 : 0;

	// TODO: Support looping sounds through speakers.
	// If the sound is from a speaker, and it's looping, ignore it.
	if ( ch->flags.bSpeaker )
	{
		if ( params.pSfx->pSource && params.pSfx->pSource->IsLooped() )
		{
			if (nSndShowStart > 0 && nSndShowStart < 7 && nSndShowStart != 4)
			{
				DevMsg("StaticSound : Speaker ignored looping sound: %s\n", sndname);
			}

			S_FreeChannel( ch );
			return 0;
		}
	}

	// set the default radius
	ch->radius = flSoundRadius;

	S_SetChannelStereo( ch, pSource );

	// initialize dsp room mixing params
	ch->dsp_mix_min = -1;
	ch->dsp_mix_max = -1;

	if (nSndShowStart == 5)
	{
		snd_showstart.SetValue(6);		// display gain once only
		nSndShowStart = 6;
	}

	// get sound type before we spatialize

	MXR_GetMixGroupFromSoundsource( ch, params.soundsource, params.soundlevel );

	// skip the trace on the first spatialization.  This channel may be stolen
	// by another sound played this frame.  Defer the trace to the mix loop
	SND_SpatializeFirstFrameNoTrace(ch);

	// Init client entity mouth movement vars
	ch->flags.m_bIgnorePhonemes = ( params.flags & SND_IGNORE_PHONEMES ) != 0;
	SND_InitMouth( ch );

	if ( IsX360() && params.delay < 0 )
	{
		// X360TEMP: Can't support yet, but going to.
		params.delay = 0;
	}

	// Pre-startup delay.  Compute # of samples over which to mix in zeros from data source before
	// actually reading first set of samples
	if ( params.delay != 0.0f )
	{
		Assert( ch->sfx );
		Assert( ch->sfx->pSource );
		
		float rate = ch->sfx->pSource->SampleRate();

		int delaySamples = (int)( params.delay * rate * params.pitch * 0.01f );

		ch->pMixer->SetStartupDelaySamples( delaySamples );

		if ( params.delay > 0 )
		{
			ch->pMixer->SetStartupDelaySamples( delaySamples );
			ch->flags.delayed_start = true;
		}
		else
		{
			int skipSamples = -delaySamples;
			int totalSamples = ch->sfx->pSource->SampleCount();

			if ( ch->sfx->pSource->IsLooped() )
			{
				skipSamples = skipSamples % totalSamples;
			}

			if ( skipSamples >= totalSamples )
			{
				S_FreeChannel( ch );
				return 0;
			}

			ch->pitch = ch->basePitch * 0.01f;
			ch->pMixer->SkipSamples( ch, skipSamples, rate, 0 );
			ch->ob_gain_target	= 1.0f;
			ch->ob_gain			= 1.0f;
			ch->ob_gain_inc		= 0.0f;
			ch->flags.bfirstpass = false;
		}
	}

	if ( S_IsMusic( ch ) )
	{
		// See if we have "music" of same name playing from "world" which means we save/restored this sound already.  If so,
		//  kill the new version and update the soundsource
		CChannelList list;
		g_ActiveChannels.GetActiveChannels( list );
		for ( int i = 0; i < list.Count(); i++ )
		{
			channel_t *pChannel = list.GetChannel(i);
			// Don't mess with the channel we just created, of course
			if ( ch == pChannel )
				continue;
			if ( ch->sfx != pChannel->sfx )
				continue;
			if ( pChannel->soundsource != SOUND_FROM_WORLD )
				continue;
			if ( !S_IsMusic( pChannel ) )
				continue;

			DevMsg( 1, "Hooking duplicate restored song track %s\n", sndname );

			// the new channel will have an updated soundsource and probably
			// has an updated pitch or volume since we are receiving this sound message
			// after the sound has started playing (usually a volume change)
			// copy that data out of the source
			pChannel->soundsource = ch->soundsource;
			pChannel->master_vol = ch->master_vol;
			pChannel->basePitch = ch->basePitch;
			pChannel->pitch = ch->pitch;
			S_FreeChannel( ch );
			
			return 0;
		}
	}

	g_pSoundServices->OnSoundStarted( ch->guid, params, sndname );

	if (nSndShowStart > 0 && nSndShowStart < 7 && nSndShowStart != 4)
	{
		DevMsg( "StaticSound %s : src %d : channel %d : %d dB : vol %.2f : radius %.0f : time %.3f\n", sndname, params.soundsource, params.entchannel, params.soundlevel, params.fvol, flSoundRadius, g_pSoundServices->GetHostTime() );
		if (nSndShowStart == 2 || nSndShowStart == 5)
			DevMsg( "\t dspmix %1.2f : distmix %1.2f : dspface %1.2f : lvol %1.2f : cvol %1.2f : rvol %1.2f : rlvol %1.2f : rrvol %1.2f\n", 
				ch->dspmix, ch->distmix, ch->dspface, 
				ch->fvolume[IFRONT_LEFT], ch->fvolume[IFRONT_CENTER], ch->fvolume[IFRONT_RIGHT], ch->fvolume[IREAR_LEFT], ch->fvolume[IREAR_RIGHT] );
		if (nSndShowStart == 3)
			DevMsg( "\t x: %4f y: %4f z: %4f\n", ch->origin.x, ch->origin.y, ch->origin.z );
	}

	return ch->guid;
}

#ifdef STAGING_ONLY
static ConVar snd_filter( "snd_filter", "", FCVAR_CHEAT );
#endif // STAGING_ONLY

int S_StartSound( StartSoundParams_t& params )
{

	if( ! params.pSfx )
	{
		return 0;
	}

#ifdef STAGING_ONLY
	if ( snd_filter.GetString()[ 0 ] && !Q_stristr( params.pSfx->getname(), snd_filter.GetString() ) )
	{
		return 0;
	}
#endif // STAGING_ONLY

	if ( IsX360() && params.delay < 0 && !params.initialStreamPosition && params.pSfx )
	{
		// calculate an initial stream position from the expected sample position
		float rate = params.pSfx->pSource->SampleRate();
		int samplePosition = (int)( -params.delay * rate * params.pitch * 0.01f );
		params.initialStreamPosition = params.pSfx->pSource->SampleToStreamPosition( samplePosition );
	}

	if ( params.staticsound )
	{
		VPROF_( "StartStaticSound", 0, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );	
		return S_StartStaticSound( params );
	}
	else
	{
		VPROF_( "StartDynamicSound", 0, VPROF_BUDGETGROUP_OTHER_SOUND, false, BUDGETFLAG_OTHER );
		return S_StartDynamicSound( params );
	}
}

// Restart all the sounds on the specified channel
inline bool IsChannelLooped( int iChannel )
{
	return (channels[iChannel].sfx &&
			channels[iChannel].sfx->pSource && 
			channels[iChannel].sfx->pSource->IsLooped() );
}

int S_GetCurrentStaticSounds( SoundInfo_t *pResult, int nSizeResult, int entchannel )
{
	int nSpaceRemaining = nSizeResult;
	for (int i = MAX_DYNAMIC_CHANNELS; i < total_channels && nSpaceRemaining; i++)
	{
		if ( channels[i].entchannel == entchannel && channels[i].sfx )
		{
			pResult->Set( channels[i].soundsource, 
						  channels[i].entchannel, 
						  channels[i].sfx->getname(), 
						  channels[i].origin,
						  channels[i].direction,
						  ( (float)channels[i].master_vol / 255.0 ),
						  DIST_MULT_TO_SNDLVL( channels[i].dist_mult ),
						  IsChannelLooped( i ),
						  channels[i].basePitch,
						  listener_origin,
						  channels[i].speakerentity );
			pResult++;
			nSpaceRemaining--;
		}
	}
	return (nSizeResult - nSpaceRemaining);
}


// Stop all sounds for entity on a channel.
void S_StopSound(int soundsource, int entchannel)
{
	THREAD_LOCK_SOUND();
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( int i = 0; i < list.Count(); i++ )
	{
		channel_t *pChannel = list.GetChannel(i);
		if (pChannel->soundsource == soundsource
			&& pChannel->entchannel == entchannel)
		{
			S_FreeChannel( pChannel );
		}
	}
}

channel_t *S_FindChannelByGuid( int guid )
{
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( int i = 0; i < list.Count(); i++ )
	{
		channel_t *pChannel = list.GetChannel(i);
		if ( pChannel->guid == guid )
		{
			return pChannel;
		}
	}
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : guid - 
//-----------------------------------------------------------------------------
void S_StopSoundByGuid( int guid )
{
	THREAD_LOCK_SOUND();
	channel_t *pChannel = S_FindChannelByGuid( guid );
	if ( pChannel )
	{
		S_FreeChannel( pChannel );
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : guid - 
//-----------------------------------------------------------------------------
float S_SoundDurationByGuid( int guid )
{
	channel_t *pChannel = S_FindChannelByGuid( guid );
	if ( !pChannel || !pChannel->sfx )
		return 0.0f;

	// NOTE: Looping sounds will return the length of a single loop
	// Use S_IsLoopingSoundByGuid to see if they are looped
	float flRate = pChannel->sfx->pSource->SampleRate() * pChannel->basePitch * 0.01f;
	int nTotalSamples = pChannel->sfx->pSource->SampleCount();
	return (flRate != 0.0f) ? nTotalSamples / flRate : 0.0f;
}


//-----------------------------------------------------------------------------
// Is this sound a looping sound?
//-----------------------------------------------------------------------------
bool S_IsLoopingSoundByGuid( int guid )
{
	channel_t *pChannel = S_FindChannelByGuid( guid );
	if ( !pChannel || !pChannel->sfx )
		return false;

	return( pChannel->sfx->pSource->IsLooped() );
}


//-----------------------------------------------------------------------------
// Purpose: Note that the guid is preincremented, so we can just return the current value as the "last sound" indicator
// Input  :  - 
// Output : int
//-----------------------------------------------------------------------------
int S_GetGuidForLastSoundEmitted()
{
	return s_nSoundGuid;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : guid - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool S_IsSoundStillPlaying( int guid )
{
	channel_t *pChannel = S_FindChannelByGuid( guid );
	return pChannel != NULL ? true : false;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : guid - 
//			fvol - 
//-----------------------------------------------------------------------------
void S_SetVolumeByGuid( int guid, float fvol )
{
	channel_t *pChannel = S_FindChannelByGuid( guid );
	pChannel->master_vol = 255.0f * clamp( fvol, 0.0f, 1.0f );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : guid - 
// Output : float
//-----------------------------------------------------------------------------
float S_GetElapsedTimeByGuid( int guid )
{
	channel_t *pChannel = S_FindChannelByGuid( guid );
	if ( !pChannel )
		return 0.0f;

	CAudioMixer *mixer = pChannel->pMixer;
	if ( !mixer )
		return 0.0f;

	float elapsed = mixer->GetSamplePosition() / ( mixer->GetSource()->SampleRate() * pChannel->pitch * 0.01f );
	return elapsed;
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : sndlist - 
//-----------------------------------------------------------------------------
void S_GetActiveSounds( CUtlVector< SndInfo_t >& sndlist )
{
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( int i = 0; i < list.Count(); i++ )
	{
		channel_t *ch = list.GetChannel(i);

		SndInfo_t info;

		info.m_nGuid			= ch->guid;
		info.m_filenameHandle	= ch->sfx ? ch->sfx->GetFileNameHandle() : NULL;
		info.m_nSoundSource		= ch->soundsource;
		info.m_nChannel			= ch->entchannel;
		// If a sound is being played through a speaker entity (e.g., on a monitor,), this is the
		//  entity upon which to show the lips moving, if the sound has sentence data
		info.m_nSpeakerEntity	= ch->speakerentity;
		info.m_flVolume			= (float)ch->master_vol / 255.0f;
		info.m_flLastSpatializedVolume = ch->last_vol;
		// Radius of this sound effect (spatialization is different within the radius)
		info.m_flRadius			= ch->radius;
		info.m_nPitch			= ch->basePitch;
		info.m_pOrigin			= &ch->origin;
		info.m_pDirection		= &ch->direction;

		// if true, assume sound source can move and update according to entity
		info.m_bUpdatePositions = ch->flags.bUpdatePositions;
		// true if playing linked sentence
		info.m_bIsSentence		= ch->flags.isSentence;
		// if true, bypass all dsp processing for this sound (ie: music)	
		info.m_bDryMix			= ch->flags.bdry;
		// true if sound is playing through in-game speaker entity.
		info.m_bSpeaker			= ch->flags.bSpeaker;
		// true if sound is using special DSP effect
		info.m_bSpecialDSP		= ( ch->special_dsp != 0 );
		// for snd_show, networked sounds get colored differently than local sounds
		info.m_bFromServer		= ch->flags.fromserver; 

		sndlist.AddToTail( info );
	}
}

void S_StopAllSounds( bool bClear )
{
	THREAD_LOCK_SOUND();
	int		i;

	if ( !g_AudioDevice )
		return;

	if ( !g_AudioDevice->IsActive() )
		return;

	total_channels = MAX_DYNAMIC_CHANNELS;	// no statics

	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( i = 0; i < list.Count(); i++ )
	{
		channel_t *pChannel = list.GetChannel(i);
		if ( channels[i].sfx )
		{
			DevMsg( 1, "%2d:Stopped sound %s\n", i, channels[i].sfx->getname() );
		}
		S_FreeChannel( pChannel );
	}

	Q_memset( channels, 0, MAX_CHANNELS * sizeof(channel_t) );

	if ( bClear )
	{
		S_ClearBuffer();
	}

	// Clear any remaining soundfade
	memset( &soundfade, 0, sizeof( soundfade ) );

	g_AudioDevice->StopAllSounds();
	Assert( g_ActiveChannels.GetActiveCount() == 0 );
}

void S_StopAllSoundsC( void )
{
	S_StopAllSounds( true );
}

void S_OnLoadScreen( bool value )
{
	s_bOnLoadScreen = value;
}

void S_ClearBuffer( void )
{
	if ( !g_AudioDevice )
		return;

	g_AudioDevice->ClearBuffer();
	DSP_ClearState();
	MIX_ClearAllPaintBuffers( PAINTBUFFER_SIZE, true );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : percent - 
//			holdtime - 
//			intime - 
//			outtime - 
//-----------------------------------------------------------------------------
void S_SoundFade( float percent, float holdtime, float intime, float outtime )
{
	soundfade.starttime				= g_pSoundServices->GetHostTime();  

	soundfade.initial_percent		= percent;       
	soundfade.fadeouttime			= outtime;    
	soundfade.holdtime				= holdtime;   
	soundfade.fadeintime			= intime;
}

//-----------------------------------------------------------------------------
// Purpose: Modulates sound volume on the client.
//-----------------------------------------------------------------------------
void S_UpdateSoundFade(void)
{
	float	totaltime;
	float	f;
	// Determine current fade value.

	// Assume no fading remains
	soundfade.percent = 0;  

	totaltime = soundfade.fadeouttime + soundfade.fadeintime + soundfade.holdtime;

	float elapsed = g_pSoundServices->GetHostTime() - soundfade.starttime;

	// Clock wrapped or reset (BUG) or we've gone far enough
	if ( elapsed < 0.0f || elapsed >= totaltime || totaltime <= 0.0f )
	{
		return;
	}

	// We are in the fade time, so determine amount of fade.
	if ( soundfade.fadeouttime > 0.0f && ( elapsed < soundfade.fadeouttime ) )
	{
		// Ramp up
		f = elapsed / soundfade.fadeouttime;
	}
	// Inside the hold time
	else if ( elapsed <= ( soundfade.fadeouttime + soundfade.holdtime ) )
	{
		// Stay
		f = 1.0f;
	}
	else
	{
		// Ramp down
		f = ( elapsed - ( soundfade.fadeouttime + soundfade.holdtime ) ) / soundfade.fadeintime;
		// backward interpolated...
		f = 1.0f - f;
	}

	// Spline it.
	f = SimpleSpline( f );
	f = clamp( f, 0.0f, 1.0f );

	soundfade.percent = soundfade.initial_percent * f;
}


//=============================================================================

// Global Voice Ducker - enabled in vcd scripts, when characters deliver important dialog.  Overrides all
// other mixer ducking, and ducks all other sounds except dialog.

ConVar snd_ducktovolume( "snd_ducktovolume", "0.55", FCVAR_ARCHIVE );
ConVar snd_duckerattacktime( "snd_duckerattacktime", "0.5", FCVAR_ARCHIVE );
ConVar snd_duckerreleasetime( "snd_duckerreleasetime", "2.5", FCVAR_ARCHIVE );
ConVar snd_duckerthreshold("snd_duckerthreshold", "0.15", FCVAR_ARCHIVE );

static void S_UpdateVoiceDuck( int voiceChannelCount, int voiceChannelMaxVolume, float frametime )
{
	float volume_when_ducked = snd_ducktovolume.GetFloat();
	int volume_threshold = (int)(snd_duckerthreshold.GetFloat() * 255.0);

	float duckTarget = 1.0;
	if ( voiceChannelCount > 0 )
	{
		voiceChannelMaxVolume = clamp(voiceChannelMaxVolume, 0, 255);
		
		// duckTarget = RemapVal( voiceChannelMaxVolume, 0, 255, 1.0, volume_when_ducked );
	
		// KB: Change: ducker now active if any character is speaking above threshold volume.
		// KB: Active ducker drops all volumes to volumes * snd_duckvolume

		if ( voiceChannelMaxVolume > volume_threshold )
			duckTarget = volume_when_ducked;
	}
	float rate = ( duckTarget < g_DuckScale ) ? snd_duckerattacktime.GetFloat() : snd_duckerreleasetime.GetFloat();
	g_DuckScale = Approach( duckTarget, g_DuckScale, frametime * ((1-volume_when_ducked) / rate) );
}

// set 2d forward vector, given 3d right vector.
// NOTE: this should only be used for a listener forward
// vector from a listener right vector. It is not a general use routine.

void ConvertListenerVectorTo2D( Vector *pvforward, Vector *pvright )
{
	// get 2d forward direction vector, ignoring pitch angle
	QAngle angles2d;
	Vector source2d;
	Vector listener_forward2d;

	source2d = *pvright;
	source2d.z = 0.0;

	VectorNormalize(source2d);

	// convert right vector to euler angles (yaw & pitch)

	VectorAngles(source2d, angles2d);

	// get forward angle of listener

	angles2d[PITCH]	= 0;
	angles2d[YAW] += 90; // rotate 90 ccw
	angles2d[ROLL] = 0;
	
	if (angles2d[YAW] >= 360)
		angles2d[YAW] -= 360;

	AngleVectors(angles2d, &listener_forward2d);

	VectorNormalize(listener_forward2d);

	*pvforward = listener_forward2d;
}

// If this is nonzero, we will only spatialize some of the static 
// channels each frame. The round robin will spatialize 1 / (2 ^ x) 
// of the spatial channels each frame.
ConVar snd_spatialize_roundrobin( "snd_spatialize_roundrobin", "0", FCVAR_ALLOWED_IN_COMPETITIVE, "Lowend optimization: if nonzero, spatialize only a fraction of sound channels each frame. 1/2^x of channels will be spatialized per frame." );
/*
============
S_Update

Called once each time through the main loop
============
*/
void S_Update( const AudioState_t *pAudioState )
{
	VPROF("S_Update");
	channel_t	*ch;
	channel_t	*combine;
	static unsigned int s_roundrobin = 0 ; ///< number of times this function is called.
									  ///< used instead of host_frame because that number
									  ///< isn't necessarily available here (sez Yahn).

	if ( !g_AudioDevice->IsActive() )
		return;

	g_SndMutex.Lock();

	// Update any client side sound fade
	S_UpdateSoundFade();

	if ( pAudioState )
	{
		VectorCopy( pAudioState->m_Origin, listener_origin );
		AngleVectors( pAudioState->m_Angles, &listener_forward, &listener_right, &listener_up ); 
		s_bIsListenerUnderwater = pAudioState->m_bIsUnderwater;
	}
	else
	{
		VectorCopy( vec3_origin, listener_origin );
		VectorCopy( vec3_origin, listener_forward );
		VectorCopy( vec3_origin, listener_right );
		VectorCopy( vec3_origin, listener_up );
		s_bIsListenerUnderwater = false;
	}

	g_AudioDevice->UpdateListener( listener_origin, listener_forward, listener_right, listener_up );
 
	combine = NULL;

	int voiceChannelCount = 0;
	int voiceChannelMaxVolume = 0;

	// reset traceline counter for this frame
	g_snd_trace_count = 0;

	// calculate distance to nearest walls, update dsp_spatial
	// updates one wall only per frame (one trace per frame)
	SND_SetSpatialDelays();

	// updates dsp_room if automatic room detection enabled
	DAS_CheckNewRoomDSP();

	// update spatialization for static and dynamic sounds	
	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );

	if (snd_spatialize_roundrobin.GetInt() == 0)
	{
		// spatialize each channel each time
		for ( int i = 0; i < list.Count(); i++ )
		{
			ch = list.GetChannel(i);
			Assert(ch->sfx);
			Assert(ch->activeIndex > 0);

			SND_Spatialize(ch);         // respatialize channel

			if ( ch->sfx->pSource && ch->sfx->pSource->IsVoiceSource() )
			{
				voiceChannelCount++;
				voiceChannelMaxVolume = max(voiceChannelMaxVolume, ChannelGetMaxVol( ch) );
			}
		}
	}
	else	// lowend performance improvement: spatialize only some  channels each frame.
	{
		unsigned int robinmask = (1 << snd_spatialize_roundrobin.GetInt()) - 1;

		// now do static channels
		for ( int i = 0 ; i < list.Count() ; ++i )
		{
			ch = list.GetChannel(i);
			Assert(ch->sfx);
			Assert(ch->activeIndex > 0);

			// need to check bfirstpass because sound tracing may have been deferred
			if ( ch->flags.bfirstpass || (robinmask & s_roundrobin) == ( i & robinmask ) )
			{
				SND_Spatialize(ch);         // respatialize channel
			}

			if ( ch->sfx->pSource && ch->sfx->pSource->IsVoiceSource() )
			{
				voiceChannelCount++;
				voiceChannelMaxVolume = max( voiceChannelMaxVolume, ChannelGetMaxVol( ch) );
			}
		}

		++s_roundrobin;
	}



	SND_ChannelTraceReset();

	// set new target for voice ducking
	float frametime = g_pSoundServices->GetHostFrametime();
	S_UpdateVoiceDuck( voiceChannelCount, voiceChannelMaxVolume, frametime );

	// update x360 music volume
	g_DashboardMusicMixValue = Approach( g_DashboardMusicMixTarget, g_DashboardMusicMixValue, g_DashboardMusicFadeRate * frametime );

	//
	// debugging output
	//
	if (snd_show.GetInt())
	{
		con_nprint_t np;
		np.time_to_live = 2.0f;
		np.fixed_width_font = true;

		int total = 0;

		CChannelList activeChannels;
		g_ActiveChannels.GetActiveChannels( activeChannels );
		for ( int i = 0; i < activeChannels.Count(); i++ )
		{
			channel_t *channel = activeChannels.GetChannel(i);
			if ( !channel->sfx )
				continue;

			np.index = total + 2;
			if ( channel->flags.fromserver )
			{
				np.color[0] = 1.0;
				np.color[1] = 0.8;
				np.color[2] = 0.1;
			}
			else
			{
				np.color[0] = 0.1;
				np.color[1] = 0.9;
				np.color[2] = 1.0;
			}

			unsigned int sampleCount = RemainingSamples( channel );
			float timeleft = (float)sampleCount / (float)channel->sfx->pSource->SampleRate();
			bool bLooping = channel->sfx->pSource->IsLooped();

			if (snd_surround.GetInt() < 4)
			{
				Con_NXPrintf ( &np, "%02i l(%03d) r(%03d) vol(%03d) ent(%03d) pos(%6d %6d %6d) timeleft(%f) looped(%d) %50s", 
					total+ 1, 
					(int)channel->fvolume[IFRONT_LEFT],
					(int)channel->fvolume[IFRONT_RIGHT],
					channel->master_vol,
					channel->soundsource,
					(int)channel->origin[0],
					(int)channel->origin[1],
					(int)channel->origin[2],
					timeleft,
					bLooping, 
					channel->sfx->getname());
			}
			else
			{
				Con_NXPrintf ( &np, "%02i l(%03d) c(%03d) r(%03d) rl(%03d) rr(%03d) vol(%03d) ent(%03d) pos(%6d %6d %6d) timeleft(%f) looped(%d) %50s", 
					total+ 1, 
					(int)channel->fvolume[IFRONT_LEFT],
					(int)channel->fvolume[IFRONT_CENTER],
					(int)channel->fvolume[IFRONT_RIGHT],
					(int)channel->fvolume[IREAR_LEFT],
					(int)channel->fvolume[IREAR_RIGHT],
					channel->master_vol,
					channel->soundsource,
					(int)channel->origin[0],
					(int)channel->origin[1],
					(int)channel->origin[2],
					timeleft,
					bLooping,
					channel->sfx->getname());
			}

			if ( snd_visualize.GetInt() )
			{
				CDebugOverlay::AddTextOverlay( channel->origin, 0.05f, channel->sfx->getname() );
			}

			total++;
		}

		while ( total <= 128 )
		{
			Con_NPrintf( total + 2, "" );
			total++;
		}
	}

	g_SndMutex.Unlock();

	if ( s_bOnLoadScreen )
		return;

	// not time to update yet?
	double tNow = Plat_FloatTime();
	// this is the last time we ran a sound frame
	g_LastSoundFrame = tNow;
	// this is the last time we did mixing (extraupdate also advances this if it mixes)
	g_LastMixTime = tNow;
	// mix some sound
	// try to stay at least one frame + mixahead ahead in the mix.
	g_EstFrameTime = (g_EstFrameTime * 0.9f) + (g_pSoundServices->GetHostFrametime() * 0.1f);
	S_Update_( g_EstFrameTime + snd_mixahead.GetFloat() );
}

CON_COMMAND( snd_dumpclientsounds, "Dump sounds to VXConsole" )
{
	con_nprint_t np;
	np.time_to_live = 2.0f;
	np.fixed_width_font = true;

	int total = 0;

	CChannelList list;
	g_ActiveChannels.GetActiveChannels( list );
	for ( int i = 0; i < list.Count(); i++ )
	{
		channel_t *ch = list.GetChannel(i);
		if ( !ch->sfx )
			continue;

		unsigned int sampleCount = RemainingSamples( ch );
		float timeleft = (float)sampleCount / (float)ch->sfx->pSource->SampleRate();
		bool bLooping = ch->sfx->pSource->IsLooped();
		const char *pszclassname = GetClientClassname(ch->soundsource);

		Msg( "%02i %s l(%03d) c(%03d) r(%03d) rl(%03d) rr(%03d) vol(%03d) pos(%6d %6d %6d) timeleft(%f) looped(%d) %50s chan:%d ent(%03d):%s\n", 
			total+ 1, 
			ch->flags.fromserver ? "SERVER" : "CLIENT",
			(int)ch->fvolume[IFRONT_LEFT], 
			(int)ch->fvolume[IFRONT_CENTER],
			(int)ch->fvolume[IFRONT_RIGHT], 
			(int)ch->fvolume[IREAR_LEFT], 
			(int)ch->fvolume[IREAR_RIGHT], 
			ch->master_vol,
			(int)ch->origin[0],
			(int)ch->origin[1],
			(int)ch->origin[2],
			timeleft,
			bLooping, 
			ch->sfx->getname(),
			ch->entchannel,
			ch->soundsource,
			pszclassname ? pszclassname : "NULL" );

		total++;
	}
}

//-----------------------------------------------------------------------------
// Set g_soundtime to number of full samples that have been transfered out to hardware
// since start.
//-----------------------------------------------------------------------------
void GetSoundTime(void)
{
	int		fullsamples;
	int		sampleOutCount;

	// size of output buffer in *full* 16 bit samples
	// A 2 channel device has a *full* sample consisting of a 16 bit LR pair.
	// A 1 channel device has a *full* sample consiting of a 16 bit single sample.
	fullsamples = g_AudioDevice->DeviceSampleCount() / g_AudioDevice->DeviceChannels();

	// NOTE: it is possible to miscount buffers if it has wrapped twice between
	// calls to S_Update.  However, since the output buffer size is > 1 second of sound, 
	// this should only occur for framerates lower than 1hz

	// sampleOutCount is counted in 16 bit *full* samples, of number of samples output to hardware
	// for current output buffer
	sampleOutCount = g_AudioDevice->GetOutputPosition();
	if ( sampleOutCount < s_oldsampleOutCount )
	{
		// buffer wrapped
		s_buffers++;
		if ( g_paintedtime > 0x70000000 )
		{	
			// time to chop things off to avoid 32 bit limits
			s_buffers = 0;
			g_paintedtime = fullsamples;
			S_StopAllSounds( true );
		}
	}
	
	s_oldsampleOutCount = sampleOutCount;

	if ( cl_movieinfo.IsRecording() || IsReplayRendering() )
	{
		// when recording a replay, we look at the record frame rate, not the engine frame rate
		
#if defined( REPLAY_ENABLED )
		extern IClientReplayContext *g_pClientReplayContext;
		if ( IsReplayRendering() )
		{
			IReplayMovieRenderer *pMovieRenderer = (g_pClientReplayContext != NULL) ? g_pClientReplayContext->GetMovieRenderer() : NULL;
		
			if ( pMovieRenderer && pMovieRenderer->IsAudioSyncFrame() )
			{
				float t = g_pSoundServices->GetHostTime();
				if ( s_lastsoundtime != t )
				{
					float frameTime = pMovieRenderer->GetRecordingFrameDuration();
					float fSamples = frameTime * (float) g_AudioDevice->DeviceDmaSpeed() + g_ReplaySoundTimeFracAccumulator;
					
					float intPart = (float) floor( fSamples );
					g_ReplaySoundTimeFracAccumulator = fSamples - intPart;
					
					g_soundtime += (int) intPart;
					s_lastsoundtime = t;
				}
			}
		
		}
		else	// cl_movieinfo.IsRecording() 
				// in movie, just mix one frame worth of sound
#endif
		{
		
			float t = g_pSoundServices->GetHostTime();
			if ( s_lastsoundtime != t )
			{
				g_soundtime += g_pSoundServices->GetHostFrametime() * g_AudioDevice->DeviceDmaSpeed();
				
				s_lastsoundtime = t;
			}
		}
	}
	else
	{
		// g_soundtime indicates how many *full* samples have actually been
		// played out to dma
		g_soundtime = s_buffers*fullsamples + sampleOutCount;
	}
}

void S_ExtraUpdate( void )
{
	if ( !g_AudioDevice || !g_pSoundServices )
		return;

	if ( !g_AudioDevice->IsActive() )
		return;

	if ( s_bOnLoadScreen )
		return;

	if ( snd_noextraupdate.GetInt() || cl_movieinfo.IsRecording() || IsReplayRendering() )
		return;		// don't pollute timings

	// If listener position and orientation has not yet been updated (ie: no call to S_Update since level load)
	// then don't mix.  Important - mixing with listener at 'false' origin causes
	// some sounds to incorrectly spatialize to 0 volume, killing them before they can play.

	if ((listener_origin  == vec3_origin) && 
		(listener_forward == vec3_origin) &&
		(listener_right	  == vec3_origin) &&
		(listener_up	  == vec3_origin) )
		return;

	// Only mix if you have used up 90% of the mixahead buffer
	double tNow = Plat_FloatTime();
	float delta = (tNow - g_LastMixTime);
	// we know we were at least snd_mixahead seconds ahead of the output the last time we did mixing
	// if we're not close to running out just exit to avoid small mix batches
	if ( delta > 0 && delta < (snd_mixahead.GetFloat() * 0.9f) )
		return;
	g_LastMixTime = tNow;

	g_pSoundServices->OnExtraUpdate();
	// Shouldn't have to do any work here if your framerate hasn't dropped
	S_Update_( snd_mixahead.GetFloat() );
}

extern void DEBUG_StartSoundMeasure(int type, int samplecount );
extern void DEBUG_StopSoundMeasure(int type, int samplecount );

void S_Update_Guts( float mixAheadTime )
{
	VPROF( "S_Update_Guts" );
	tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );

	DEBUG_StartSoundMeasure(4, 0);

	// Update our perception of audio time.
	// 'g_soundtime' tells how many samples have
	// been played out of the dma buffer since sound system startup.
	// 'g_paintedtime' indicates how many samples we've actually mixed
	// and sent to the dma buffer since sound system startup.
	GetSoundTime();

//	if ( g_soundtime > g_paintedtime )
//	{
//		// if soundtime > paintedtime, then the dma buffer
//		// has played out more sound than we've actually
//		// mixed.  We need to call S_Update_ more often.
//
//		DevMsg ("S_Update_ : Underflow\n"); 
//		paintedtime = g_soundtime;		
//	}
//	(kdb) above code doesn't handle underflow correctly 
//	should actually zero out the paintbuffer to advance to the new
//	time.

	// mix ahead of current position
	unsigned endtime = g_AudioDevice->PaintBegin( mixAheadTime, g_soundtime, g_paintedtime );

	int samples = endtime - g_paintedtime;
	samples = samples < 0 ? 0 : samples;
	if ( samples )
	{
		THREAD_LOCK_SOUND();

		DEBUG_StartSoundMeasure( 2, samples );

		MIX_PaintChannels( endtime, s_bIsListenerUnderwater );

		MXR_DebugShowMixVolumes();

		MXR_UpdateAllDuckerVolumes();

		DEBUG_StopSoundMeasure( 2, 0 );
	}

	g_AudioDevice->PaintEnd();
	DEBUG_StopSoundMeasure( 4, samples );
}

#if !defined( _X360 )
#define THREADED_MIX_TIME 33
#else
#define THREADED_MIX_TIME XMA_POLL_RATE
#endif

ConVar snd_ShowThreadFrameTime( "snd_ShowThreadFrameTime", "0" );

bool g_bMixThreadExit;
ThreadHandle_t g_hMixThread;
void S_Update_Thread()
{
	float frameTime = THREADED_MIX_TIME * 0.001f;
	double lastFrameTime = Plat_FloatTime();

	while ( !g_bMixThreadExit )
	{
		// mixing (for 360) needs to be updated at a steady rate
		// large update times causes the mixer to demand more audio data
		// the 360 decoder has finite latency and cannot fulfill spike requests
		float t0 = Plat_FloatTime();
		S_Update_Guts( frameTime + snd_mixahead.GetFloat() );
		int updateTime = ( Plat_FloatTime() - t0 ) * 1000.0f;

		// try to maintain a steadier rate by compensating for fluctuating mix times
		int sleepTime = THREADED_MIX_TIME - updateTime;
		if ( sleepTime > 0 )
		{
			ThreadSleep( sleepTime );
		}

		// mimic a frametime needed for sound update
		double t1 = Plat_FloatTime();
		frameTime = t1 - lastFrameTime;
		lastFrameTime = t1;

		if ( snd_ShowThreadFrameTime.GetBool() )
		{
			Msg( "S_Update_Thread: frameTime: %d ms\n", (int)( frameTime * 1000.0f ) );
		}
	}
}

void S_ShutdownMixThread()
{
	if ( g_hMixThread )
	{
		g_bMixThreadExit = true;
		ThreadJoin( g_hMixThread );
		ReleaseThreadHandle( g_hMixThread );
		g_hMixThread = NULL;
	}
}

void S_Update_( float mixAheadTime )
{
	if ( !IsConsole() || !snd_mix_async.GetBool() )
	{
		S_ShutdownMixThread();
		S_Update_Guts( mixAheadTime );
	}
	else
	{
		if ( !g_hMixThread )
		{
			g_bMixThreadExit = false;
			g_hMixThread = ThreadExecuteSolo( "SndMix", S_Update_Thread );
			if ( IsX360() )
			{
				ThreadSetAffinity( g_hMixThread, XBOX_PROCESSOR_5 );
			}
		}
	}
}

//-----------------------------------------------------------------------------
// Threaded mixing enable. Purposely hiding enable/disable details.
//-----------------------------------------------------------------------------
void S_EnableThreadedMixing( bool bEnable )
{
	if ( snd_mix_async.GetBool() != bEnable )
	{
		snd_mix_async.SetValue( bEnable );
	}
}

/*
===============================================================================

console functions

===============================================================================
*/
extern void DSP_DEBUGSetParams(int ipreset, int iproc, float *pvalues, int cparams);
extern void DSP_DEBUGReloadPresetFile( void );

void S_DspParms( const CCommand &args )
{
	if ( args.ArgC() == 1)
	{
		// if dsp_parms with no arguments, reload entire preset file

		 DSP_DEBUGReloadPresetFile();

		 return;
	}

	if ( args.ArgC() < 4 )
	{
		Msg( "Usage: dsp_parms PRESET# PROC# param0 param1 ...up to param15 \n" );
		return;
	}
	
	int cparam = min( args.ArgC() - 4, 16);

	float params[16];
	Q_memset( params, 0, sizeof(float) * 16 );

	// get preset & proc
	int idsp, iproc;
	idsp = Q_atof( args[1] );
	iproc = Q_atof( args[2] );

	// get params
	for (int i = 0; i < cparam; i++)
	{
		params[i] = Q_atof( args[i+4] );
	}

	// set up params & switch preset
	DSP_DEBUGSetParams(idsp, iproc, params, cparam);
}

static ConCommand dsp_parm("dsp_reload", S_DspParms );

void S_Play( const char *pszName, bool flush = false )
{
	int			inCache;
	char		szName[256];
	CSfxTable	*pSfx;
	
	Q_strncpy( szName, pszName, sizeof( szName ) );
	if ( !Q_strrchr( pszName, '.' ) )
	{
		Q_strncat( szName, ".wav", sizeof( szName ), COPY_ALL_CHARACTERS );
	}

	pSfx = S_FindName( szName, &inCache );
	if ( inCache && flush )
	{
		pSfx->pSource->CacheUnload();
	}

	StartSoundParams_t params;
	params.staticsound = false;
	params.soundsource = g_pSoundServices->GetViewEntity();
	params.entchannel = CHAN_REPLACE;
	params.pSfx = pSfx;
	params.origin = listener_origin;
	params.fvol = 1.0f;
	params.soundlevel = SNDLVL_NONE;
	params.flags = 0;
	params.pitch = PITCH_NORM;

	S_StartSound( params );
}

static void S_Play( const CCommand &args )
{
	bool bFlush = !Q_stricmp( args[0], "playflush" );
	for ( int i = 1; i < args.ArgC(); ++i )
	{
		S_Play( args[i], bFlush );
	}
}

static void S_PlayVol( const CCommand &args )
{
	static int hash=543;
	float vol;
	char name[256];
	CSfxTable *pSfx;
	
	for ( int i = 1; i<args.ArgC(); i += 2 )
	{
		if ( !Q_strrchr( args[i], '.') )
		{
			Q_strncpy( name, args[i], sizeof( name ) );
			Q_strncat( name, ".wav", sizeof( name ), COPY_ALL_CHARACTERS );
		}
		else
		{
			Q_strncpy( name, args[i], sizeof( name ) );
		}

		pSfx = S_PrecacheSound( name );
		vol = Q_atof( args[i+1] );

		StartSoundParams_t params;
		params.staticsound = false;
		params.soundsource = hash++;
		params.entchannel = CHAN_AUTO;
		params.pSfx = pSfx;
		params.origin = listener_origin;
		params.fvol = vol;
		params.soundlevel = SNDLVL_NONE;
		params.flags = 0;
		params.pitch = PITCH_NORM;

		S_StartDynamicSound( params );
	}
}

static void S_PlayDelay( const CCommand &args )
{
	if ( args.ArgC() != 3 )
	{
		Msg( "Usage:  sndplaydelay delay_in_sec (negative to skip ahead) soundname\n" );
		return;
	}

	char szName[256];
	CSfxTable *pSfx;

	float delay = Q_atof( args[ 1 ] );
	
	Q_strncpy(szName, args[ 2 ], sizeof( szName ) );
	if ( !Q_strrchr( args[ 2 ], '.' ) )
	{
		Q_strncat( szName, ".wav", sizeof( szName ), COPY_ALL_CHARACTERS );
	}

	pSfx = S_FindName( szName, NULL );
	
	StartSoundParams_t params;
	params.staticsound = false;
	params.soundsource = g_pSoundServices->GetViewEntity();
	params.entchannel = CHAN_REPLACE;
	params.pSfx = pSfx;
	params.origin = listener_origin;
	params.fvol = 1.0f;
	params.soundlevel = SNDLVL_NONE;
	params.flags = 0;
	params.pitch = PITCH_NORM;
	params.delay = delay;

	S_StartSound( params );

}
static ConCommand sndplaydelay( "sndplaydelay", S_PlayDelay, "Usage:  sndplaydelay delay_in_sec (negative to skip ahead) soundname", FCVAR_SERVER_CAN_EXECUTE );

static bool SortByNameLessFunc( const int &lhs, const int &rhs )
{
	CSfxTable *pSfx1 = s_Sounds[lhs].pSfx;
	CSfxTable *pSfx2 = s_Sounds[rhs].pSfx;

	return CaselessStringLessThan( pSfx1->getname(), pSfx2->getname() );
}

void S_SoundList(void)
{
	CSfxTable		*sfx;
	CAudioSource	*pSource;
	int				size, total;

	total = 0;
	for ( int i = s_Sounds.FirstInorder(); i != s_Sounds.InvalidIndex(); i = s_Sounds.NextInorder( i ) )
	{
		sfx = s_Sounds[i].pSfx;

		pSource = sfx->pSource;
		if ( !pSource || !pSource->IsCached() )
			continue;

		size = pSource->SampleSize() * pSource->SampleCount();
		total += size;

		if ( pSource->IsLooped() )
			Msg ("L");
		else
			Msg (" ");
		Msg("(%2db) %6i : %s\n", pSource->SampleSize(),  size, sfx->getname());
	}
	Msg( "Total resident: %i\n", total );
}

#if defined( _X360 )
CON_COMMAND( vx_soundlist, "Dump sounds to VXConsole" )
{
	CSfxTable		*sfx;
	CAudioSource	*pSource;
	int				dataSize;
	char			*pFormatStr;
	int				sampleRate;
	int				sampleBits;
	int				streamed;
	int				looped;
	int				channels;
	int				numSamples;

	int numSounds = s_Sounds.Count();
	xSoundList_t* pSoundList = new xSoundList_t[numSounds];

	int i = 0;
	for ( int iSrcSound=s_Sounds.FirstInorder(); iSrcSound != s_Sounds.InvalidIndex(); iSrcSound = s_Sounds.NextInorder( iSrcSound ) )
	{
		dataSize = -1;
		sampleRate = -1;
		sampleBits = -1;
		pFormatStr = "???";
		streamed = -1;
		looped = -1;
		channels = -1;
		numSamples = -1;

		sfx     = s_Sounds[iSrcSound].pSfx;
		pSource = sfx->pSource;
		if ( pSource && pSource->IsCached() )
		{
			numSamples = pSource->SampleCount();
			dataSize = pSource->DataSize();
			sampleRate = pSource->SampleRate();
			streamed = pSource->IsStreaming();
			looped = pSource->IsLooped();
			channels = pSource->IsStereoWav() ? 2 : 1;

			if ( pSource->Format() == WAVE_FORMAT_ADPCM )
			{
				pFormatStr = "ADPCM";
				sampleBits = 16;
			}
			else if ( pSource->Format() == WAVE_FORMAT_PCM )
			{
				pFormatStr = "PCM";
				sampleBits = (pSource->SampleSize() * 8)/channels;
			}
			else if ( pSource->Format() == WAVE_FORMAT_XMA )
			{
				pFormatStr = "XMA";
				sampleBits = 16;
			}
		}

		V_strncpy( pSoundList[i].name, sfx->getname(), sizeof( pSoundList[i].name ) );
		V_strncpy( pSoundList[i].formatName, pFormatStr, sizeof( pSoundList[i].formatName ) );
		pSoundList[i].rate = sampleRate;
		pSoundList[i].bits = sampleBits;
		pSoundList[i].channels = channels;
		pSoundList[i].looped = looped;
		pSoundList[i].dataSize = dataSize;
		pSoundList[i].numSamples = numSamples;
		pSoundList[i].streamed = streamed;
		++i;
	}

	XBX_rSoundList( numSounds, pSoundList );
	delete [] pSoundList;
}
#endif

extern unsigned g_snd_time_debug;
extern unsigned g_snd_call_time_debug;
extern unsigned g_snd_count_debug;
extern unsigned g_snd_samplecount;
extern unsigned g_snd_frametime;
extern unsigned g_snd_frametime_total;
extern int g_snd_profile_type;

// start measuring sound perf, 100 reps
// type 1 - dsp, 2 - mix, 3 - load sound, 4 - all sound
// set type via ConVar snd_profile

void DEBUG_StartSoundMeasure(int type, int samplecount )
{
	if (type != g_snd_profile_type)
		return;

	if (samplecount)
		g_snd_samplecount += samplecount;

	g_snd_call_time_debug = Plat_MSTime();
}

// show sound measurement after 25 reps - show as % of total frame
// type 1 - dsp, 2 - mix, 3 - load sound, 4 - all sound

// BUGBUG: snd_profile 4 reports a lower average because it's average cost
// PER CALL and most calls (via SoundExtraUpdate()) don't do any work and 
// bring the average down.  If you want an average PER FRAME instead, it's generally higher.
void DEBUG_StopSoundMeasure(int type, int samplecount )
{
	if (type != g_snd_profile_type)
		return;

	if (samplecount)
		g_snd_samplecount += samplecount;

	// add total time since last frame

	g_snd_frametime_total += Plat_MSTime() - g_snd_frametime;

	// performance timing

	g_snd_time_debug += Plat_MSTime() - g_snd_call_time_debug;

	if (++g_snd_count_debug >= 100)
	{
		switch (g_snd_profile_type)
		{
		case 1: 
			Msg("dsp: (%2.2f) millisec   ", ((float)g_snd_time_debug) / 100.0); 
			Msg("(%2.2f) pct of frame \n", 100.0 * ((float)g_snd_time_debug) / ((float)g_snd_frametime_total)); 
			break;
		case 2: 
			Msg("mix+dsp:(%2.2f) millisec   ", ((float)g_snd_time_debug) / 100.0);
			Msg("(%2.2f) pct of frame \n", 100.0 * ((float)g_snd_time_debug) / ((float)g_snd_frametime_total)); 
			break;
		case 3: 
			//if ( (((float)g_snd_time_debug) / 100.0) < 0.01 )
			//	break;
			Msg("snd load: (%2.2f) millisec   ", ((float)g_snd_time_debug) / 100.0); 
			Msg("(%2.2f) pct of frame \n", 100.0 * ((float)g_snd_time_debug) / ((float)g_snd_frametime_total)); 
			break;
		case 4: 
			Msg("sound: (%2.2f) millisec   ", ((float)g_snd_time_debug) / 100.0); 
			Msg("(%2.2f) pct of frame (%d samples) \n", 100.0 * ((float)g_snd_time_debug) / ((float)g_snd_frametime_total), g_snd_samplecount); 
			break;
		}
		
		g_snd_count_debug = 0;
		g_snd_time_debug = 0;
		g_snd_samplecount = 0;	
		g_snd_frametime_total = 0;
	}

	g_snd_frametime = Plat_MSTime();
}

// speak a sentence from console; works by passing in "!sentencename"
// or "sentence"

extern ConVar dsp_room;

static void S_Say( const CCommand &args )
{
	CSfxTable *pSfx;

	if ( !g_AudioDevice->IsActive() )
		return;

	char sound[256];
	Q_strncpy( sound, args[1], sizeof( sound ) );		
	
	// DEBUG - test performance of dsp code
	if ( !Q_stricmp( sound, "dsp" ) )
	{
		unsigned time;
		int i;
		int count = 10000;
		int idsp; 

		for (i = 0; i < PAINTBUFFER_SIZE; i++)
		{
			g_paintbuffer[i].left = RandomInt(0,2999);
			g_paintbuffer[i].right = RandomInt(0,2999);
		}

		Msg ("Start profiling 10,000 calls to DSP\n");
		
		idsp = dsp_room.GetInt();
		
		// get system time

		time = Plat_MSTime();
		
		for (i = 0; i < count; i++)
		{
			// SX_RoomFX(PAINTBUFFER_SIZE, TRUE, TRUE);

			DSP_Process(idsp, g_paintbuffer, NULL, NULL, PAINTBUFFER_SIZE);

		}
		// display system time delta 
		Msg("%d milliseconds \n", Plat_MSTime() - time);
		return;
	} 
	
	if ( !Q_stricmp(sound, "paint") )
	{
		unsigned time;
		int count = 10000;
		static int hash=543;
		int psav = g_paintedtime;

		Msg ("Start profiling MIX_PaintChannels\n");
		
		pSfx = S_PrecacheSound("ambience/labdrone1.wav");

		StartSoundParams_t params;
		params.staticsound = false;
		params.soundsource = hash++;
		params.entchannel = CHAN_AUTO;
		params.pSfx = pSfx;
		params.origin = listener_origin;
		params.fvol = 1.0f;
		params.soundlevel = SNDLVL_NONE;
		params.flags = 0;
		params.pitch = PITCH_NORM;

		S_StartDynamicSound( params );

		// get system time
		time = Plat_MSTime();

		// paint a boatload of sound

		MIX_PaintChannels( g_paintedtime + 512*count, s_bIsListenerUnderwater );		

		// display system time delta 
		Msg("%d milliseconds \n", Plat_MSTime() - time);
		g_paintedtime = psav;
		return;
	}

	// DEBUG
	if ( !TestSoundChar( sound, CHAR_SENTENCE ) )
	{
		// build a fake sentence name, then play the sentence text

		Q_strncpy(sound, "xxtestxx ", sizeof( sound ) );
		Q_strncat(sound, args[1], sizeof( sound ), COPY_ALL_CHARACTERS );

		int addIndex = g_Sentences.AddToTail();
		sentence_t *pSentence = &g_Sentences[addIndex];
		pSentence->pName = sound;
		pSentence->length = 0;

		// insert null terminator after sentence name
		sound[8] = 0;

		pSfx = S_PrecacheSound ("!xxtestxx");
		if (!pSfx)
		{
			Msg ("S_Say: can't cache %s\n", sound);
			return;
		}

		StartSoundParams_t params;
		params.staticsound = false;
		params.soundsource = g_pSoundServices->GetViewEntity();
		params.entchannel = CHAN_REPLACE;
		params.pSfx = pSfx;
		params.origin = vec3_origin;
		params.fvol = 1.0f;
		params.soundlevel = SNDLVL_NONE;
		params.flags = 0;
		params.pitch = PITCH_NORM;

		S_StartDynamicSound ( params );
		
		// remove last
		g_Sentences.Remove( g_Sentences.Size() - 1 );
	}
	else
	{
		pSfx = S_FindName(sound, NULL);
		if (!pSfx)
		{
			Msg ("S_Say: can't find sentence name %s\n", sound);
			return;
		}

		StartSoundParams_t params;
		params.staticsound = false;
		params.soundsource = g_pSoundServices->GetViewEntity();
		params.entchannel = CHAN_REPLACE;
		params.pSfx = pSfx;
		params.origin = vec3_origin;
		params.fvol = 1.0f;
		params.soundlevel = SNDLVL_NONE;
		params.flags = 0;
		params.pitch = PITCH_NORM;

		S_StartDynamicSound( params );
	}
}


//------------------------------------------------------------------------------
//
// Sound Mixers
//
// Sound mixers are referenced by name from Soundscapes, and are used to provide
// custom volume control over various sound categories, called 'mix groups'
//
// see scripts/soundmixers.txt for data format
//------------------------------------------------------------------------------

#define CMXRGROUPMAX		64					// up to n mixgroups
#define CMXRGROUPRULESMAX	(CMXRGROUPMAX + 16)	// max number of group rules
#define	CMXRSOUNDMIXERSMAX	32					// up to n sound mixers per project

// mix groups - these equivalent to submixes on an audio mixer

// list of rules for determining sound membership in mix groups.
// All conditions which are not null are ANDed together 
#define CMXRCLASSMAX	16
#define CMXRNAMEMAX		32

struct classlistelem_t
{
	char			szclassname[CMXRNAMEMAX];	// name of entities' class, such as CAI_BaseNPC or CHL2_Player
};


struct grouprule_t
{	
	char			szmixgroup[CMXRNAMEMAX];	// mix group name
	int				mixgroupid;					// mix group unique id
	char			szdir[CMXRNAMEMAX];			// substring to search for in ch->sfx
	int				classId;					// index of classname
	int				chantype;					// channel type (CHAN_WEAPON, etc)
	int				soundlevel_min;				// min soundlevel
	int				soundlevel_max;				// max soundlevel

	int				priority;					// 0..100 higher priority sound groups duck all lower pri groups if enabled
	int				is_ducked;					// if 1, sound group is ducked by all higher priority 'causes_duck" sounds
	int				causes_ducking;				// if 1, sound group ducks other 'is_ducked' sounds of lower priority
	float			duck_target_pct;			// if sound group is ducked, target percent of original volume

	float			total_vol;					// total volume of all sounds in this group, if group can cause ducking
	float			ducker_threshold;			// ducking is caused by this group if total_vol > ducker_threshold
												// and causes_ducking is enabled.
	float			duck_target_vol;			// target volume while ducking	
	float			duck_ramp_val;				// current value of ramp - moves towards duck_target_vol
};

// sound mixer

struct soundmixer_t
{
	char			szsoundmixer[CMXRNAMEMAX];					// name of this soundmixer
	float			mapMixgroupidToValue[CMXRGROUPMAX];			// sparse array of mix group values for this soundmixer
};

int g_mapMixgroupidToGrouprulesid[CMXRGROUPMAX];				// map mixgroupid (one per unique group name)
																// back to 1st entry of this name in g_grouprules

// sound mixer globals

classlistelem_t g_groupclasslist[CMXRCLASSMAX];
soundmixer_t	g_soundmixers[CMXRSOUNDMIXERSMAX];	// all sound mixers	
grouprule_t		g_grouprules[CMXRGROUPRULESMAX];	// all rules for determining mix group membership


// set current soundmixer index g_isoundmixer, search for match in soundmixers
// Only change current soundmixer if new name is different from current name.

int g_isoundmixer = -1;								// index of current sound mixer
char g_szsoundmixer_cur[64];						// current soundmixer name

ConVar snd_soundmixer("snd_soundmixer", "Default_Mix");		// current soundmixer name


void MXR_SetCurrentSoundMixer( const char *szsoundmixer )
{
	// if soundmixer name is not different from current name, return

	if ( !Q_stricmp(szsoundmixer, g_szsoundmixer_cur) )
	{
		return;
	}

	for (int i = 0; i < g_csoundmixers; i++)
	{
		if ( !Q_stricmp(g_soundmixers[i].szsoundmixer, szsoundmixer) )
		{
			g_isoundmixer = i;

			// save new current sound mixer name
			V_strcpy_safe(g_szsoundmixer_cur, szsoundmixer);

			return;
		}
	}
}

ConVar snd_showclassname("snd_showclassname", "0");		// if 1, show classname of ent making sound
														// if 2, show all mixgroup matches
														// if 3, show all mixgroup matches with current soundmixer for ent
// get the client class name if an entity was specified
const char *GetClientClassname( SoundSource soundsource )
{
	IClientEntity *pClientEntity = NULL;
	if ( entitylist )
	{
		pClientEntity = entitylist->GetClientEntity( soundsource );
		if ( pClientEntity )
		{
			ClientClass *pClientClass = pClientEntity->GetClientClass();
			// check npc sounds 
			if ( pClientClass )
			{
				return pClientClass->GetName();
			}
		}
	}

	return NULL;
}

// builds a cached list of rules that match the directory name on the sound
int MXR_GetMixGroupListFromDirName( const char *pDirname, byte *pList, int listMax )
{
	// if we call this before the groups are parsed we'll get bad data
	Assert(g_cgrouprules>0);
	int count = 0;
	for ( int i = 0; i < listMax; i++ )
	{
		pList[i] = 255;
	}

	for ( int i = 0; i < g_cgrouprules; i++ )
	{
		grouprule_t *prule = &g_grouprules[i];
		if ( prule->szdir[ 0 ] && Q_stristr( pDirname, prule->szdir ) )
		{
			pList[count] = i;
			count++;
			if ( count >= listMax )
				return count;
		}
	}
	return count;
}


// determine which mixgroups sound is in, and save those mixgroupids in sound.
// use current soundmixer indicated with g_isoundmixer, and contents of g_rgpgrouprules.
// Algorithm: 
//		1. all conditions in a row are AND conditions, 
//		2. all rows sharing the same groupname are OR conditions.
// so - if a sound matches all conditions of a row, it is given that row's mixgroup id
//		if a sound doesn't match all conditions of a row, the next row is checked.

// returns 0, default mixgroup if no match
void MXR_GetMixGroupFromSoundsource( channel_t *pchan, SoundSource soundsource,  soundlevel_t soundlevel)
{
	int i;
	grouprule_t *prule;
	bool fmatch;
	bool classMatch[CMXRCLASSMAX];

	// init all mixgroups for channel
	for ( i = 0; i < 8; i++ )
	{
		pchan->mixgroups[i] = -1;
	}

	char sndname[MAX_OSPATH];
	Q_strncpy( sndname, pchan->sfx->getname(), sizeof( sndname ) );
	// Use forward slashes here
	Q_FixSlashes( sndname, '/' );
	const char *pszclassname = GetClientClassname(soundsource);

	for ( i = 0; i < g_cgroupclass; i++ )
	{
		classMatch[i] = false;
		if ( pszclassname && Q_stristr(pszclassname, g_groupclasslist[i].szclassname ) )
		{
			classMatch[i] = true;
		}
	}

	if ( snd_showclassname.GetInt() == 1)
	{
		// utility: show classname of ent making sound

		if (pszclassname)
		{
			DevMsg("(%s:%s) \n", pszclassname, sndname);	
		}
	}

	// check all group rules for a match, save
	// up to 8 matches in channel mixgroup.

	int cmixgroups = 0;
	if (!pchan->sfx->m_bMixGroupsCached)
	{
		pchan->sfx->OnNameChanged( pchan->sfx->getname() );
	}
	
	// since this is a sorted list (in group rule order) we only need to test against the next matching rule
	// this avoids a search inside the loop
	int currentDirRuleIndex = 0;
	int currentDirRule = pchan->sfx->m_mixGroupList[0];

	for (i = 0; i < g_cgrouprules; i++)
	{
		prule = &g_grouprules[i];
		fmatch = true;

		// check directory or name substring
#if _DEBUG
		// check dir table is correct in CSfxTable cache
		if ( prule->szdir[ 0 ] && Q_stristr( sndname, prule->szdir ) )
		{
			Assert(currentDirRule == i);
		}
		else
		{
			Assert(currentDirRule != i);
		}
		if ( prule->classId >= 0 )
		{
			// rule has a valid class id and table is correct
			Assert(prule->classId < g_cgroupclass);
			if ( pszclassname && Q_stristr(pszclassname, g_groupclasslist[prule->classId].szclassname) )
			{
				Assert(classMatch[prule->classId] == true);
			}
			else
			{
				Assert(classMatch[prule->classId] == false);
			}
		}
#endif
		// this is the next matching dir for this sound, no need to search
		// becuse the list is sorted and we visit all elements
		if ( currentDirRule == i )
		{
			Assert(prule->szdir[0]);
			currentDirRuleIndex++;
			currentDirRule = 255;
			if ( currentDirRuleIndex < pchan->sfx->m_mixGroupCount )
			{
				currentDirRule = pchan->sfx->m_mixGroupList[currentDirRuleIndex];
			}
		}
		else if ( prule->szdir[ 0 ] )
		{
			fmatch = false;	// substring doesn't match, keep looking
		}

		// check class name

		if ( fmatch && prule->classId >= 0 )
		{
			fmatch = classMatch[prule->classId];
		}

		// check channel type

		if ( fmatch && prule->chantype >= 0)
		{
			if ( pchan->entchannel != prule->chantype  )
				fmatch = false;	// channel type doesn't match, keep looking
		}

		// check sndlvlmin/max

		if ( fmatch && prule->soundlevel_min >= 0)
		{
			if ( soundlevel < prule->soundlevel_min )
				fmatch = false;	// soundlevel is less than min, keep looking
		}

		if ( fmatch && prule->soundlevel_max >= 0)
		{
			if ( soundlevel > prule->soundlevel_max )
				fmatch = false; // soundlevel is greater than max, keep looking
		}

		if ( fmatch )
		{
			pchan->mixgroups[cmixgroups] = prule->mixgroupid;
			cmixgroups++;
			if (cmixgroups >= 8)
				return;		// too many matches, stop looking
		}
		
		if (fmatch && snd_showclassname.GetInt() >= 2)
		{
			// show all mixgroups for this sound
			if (cmixgroups == 1)
			{
				DevMsg("\n%s:%s: ", g_szsoundmixer_cur, sndname);	
			}
			if (prule->szmixgroup[0])
			{
			//	int rgmixgroupid[8];
			//	for (int i = 0; i < 8; i++)
			//		rgmixgroupid[i] = -1;
			//	rgmixgroupid[0] = prule->mixgroupid;
			//	float vol = MXR_GetVolFromMixGroup( rgmixgroupid );
			//	DevMsg("%s(%1.2f) ", prule->szmixgroup, vol);
				DevMsg("%s ", prule->szmixgroup);
			}
		}
	}
}

struct debug_showvols_t
{
	char *psz;			// group name
	int	  mixgroupid;	// groupid
	float vol;			// group volume
	float totalvol;		// total volume of all sounds playing in this group
};


// display routine for MXR_DebugShowMixVolumes

#define MXR_DEBUG_INCY	(1.0/40.0)			// vertical text spacing
#define MXR_DEBUG_GREENSTART 0.3			// start position on screen of bar

#define MXR_DEBUG_MAXVOL		1.0			// max volume scale
#define MXR_DEBUG_REDLIMIT		1.0			// volume limit into yellow
#define MXR_DEBUG_YELLOWLIMIT	0.7			// volume limit into red

#define MXR_DEBUG_VOLSCALE 48				// length of graph in characters
#define MXR_DEBUG_CHAR			'-'			// bar character

extern ConVar dsp_volume;
int g_debug_mxr_displaycount = 0;

void MXR_DebugGraphMixVolumes( debug_showvols_t *groupvols, int cgroups)
{
	float flXpos, flYpos, flXposBar, duration;
	int r,g,b,a;
	int rb, gb, bb, ab;
	flXpos = 0;
	flYpos = 0;
	char text[128];
	char bartext[MXR_DEBUG_VOLSCALE*3];

	duration = 0.01;

	g_debug_mxr_displaycount++;

	if (!(g_debug_mxr_displaycount % 10))
		return;		// only display every 10 frames


	r = 96; g = 86; b = 226; a = 255; ab = 255;
	
	// show volume, dsp_volume

	Q_snprintf( text, 128, "Game Volume: %1.2f", volume.GetFloat());
	CDebugOverlay::AddScreenTextOverlay(flXpos, flYpos, duration, r, g, b,a,  text); 
	flYpos += MXR_DEBUG_INCY;

	Q_snprintf( text, 128, "DSP Volume: %1.2f", dsp_volume.GetFloat());
	CDebugOverlay::AddScreenTextOverlay(flXpos, flYpos, duration, r, g, b,a,  text); 
	flYpos += MXR_DEBUG_INCY;
	
	for (int i = 0; i < cgroups; i++)
	{	
		// r += 64; g += 64; b += 16;

		r = r % 255; g = g % 255; b = b % 255;
		
		Q_snprintf( text, 128, "%s: %1.2f (%1.2f)", groupvols[i].psz, 
					groupvols[i].vol * g_DuckScale, groupvols[i].totalvol * g_DuckScale);

		CDebugOverlay::AddScreenTextOverlay(flXpos, flYpos, duration, r, g, b,a,  text);

		// draw volume bar graph
		
		float vol = (groupvols[i].totalvol * g_DuckScale) / MXR_DEBUG_MAXVOL;
		
		// draw first 70% green
		float vol1 = 0.0;
		float vol2 = 0.0;
		float vol3 = 0.0;
		int cbars;

		vol1 = clamp(vol, 0.0f, 0.7f);
		vol2 = clamp(vol, 0.0f, 0.95f);
		vol3 = vol;

		flXposBar = flXpos + MXR_DEBUG_GREENSTART;

		if (vol1 > 0.0)
		{
			//flXposBar = flXpos + MXR_DEBUG_GREENSTART;

			rb = 0; gb= 255; bb = 0;		// green bar
			Q_memset(bartext, 0, sizeof(bartext));

			cbars = (int)((float)vol1 * (float)MXR_DEBUG_VOLSCALE);
			cbars = clamp(cbars, 0, MXR_DEBUG_VOLSCALE*3-1);
			Q_memset(bartext, MXR_DEBUG_CHAR, cbars);

			CDebugOverlay::AddScreenTextOverlay(flXposBar, flYpos, duration, rb, gb, bb,ab,  bartext);
		}

		
		// yellow bar
		if (vol2 > MXR_DEBUG_YELLOWLIMIT)	
		{
			rb = 255; gb = 255; bb = 0;	
			Q_memset(bartext, 0, sizeof(bartext));

			cbars = (int)((float)vol2 * (float)MXR_DEBUG_VOLSCALE);
			cbars = clamp(cbars, 0, MXR_DEBUG_VOLSCALE*3-1);
			Q_memset(bartext, MXR_DEBUG_CHAR, cbars);

			CDebugOverlay::AddScreenTextOverlay(flXposBar, flYpos, duration, rb, gb, bb,ab,  bartext);
		}

		// red bar
		if (vol3 > MXR_DEBUG_REDLIMIT)
		{
			//flXposBar = flXpos + MXR_DEBUG_REDSTART;
			rb = 255; gb = 0; bb = 0;
			Q_memset(bartext, 0, sizeof(bartext));

			cbars = (int)((float)vol3 * (float)MXR_DEBUG_VOLSCALE);
			cbars = clamp(cbars, 0, MXR_DEBUG_VOLSCALE*3-1);
			Q_memset(bartext, MXR_DEBUG_CHAR, cbars);

			CDebugOverlay::AddScreenTextOverlay(flXposBar, flYpos, duration, rb, gb, bb,ab,  bartext);
		}

		flYpos += MXR_DEBUG_INCY;
	}
}

ConVar snd_disable_mixer_duck("snd_disable_mixer_duck", "0");	// if 1, soundmixer ducking is disabled

// given mix group id, return current duck volume

float MXR_GetDuckVolume( int mixgroupid )
{

	if ( snd_disable_mixer_duck.GetInt() )
		return 1.0;

	Assert ( mixgroupid < g_cgrouprules );

	int	grouprulesid = g_mapMixgroupidToGrouprulesid[mixgroupid];

	// if this mixgroup is not ducked, return 1.0

	if ( !g_grouprules[grouprulesid].is_ducked )
		return 1.0;

	// return current duck value for this group, scaled by current fade in/out ramp

	return g_grouprules[grouprulesid].duck_ramp_val;

}

#define SND_DUCKER_UPDATETIME	0.1		// seconds to wait between ducker updates

double g_mxr_ducktime = 0.0;			// time of last update to ducker

// Get total volume currently playing in all groups,
// process duck volumes for all groups
// Call once per frame - updates occur at 10hz

void MXR_UpdateAllDuckerVolumes( void )
{
	if ( snd_disable_mixer_duck.GetInt() )
		return;

	// check timer since last update, only update at 10hz

	int i;
	double dtime = g_pSoundServices->GetHostTime();
	
	// don't update until timer expires

	if (fabs(dtime - g_mxr_ducktime) < SND_DUCKER_UPDATETIME)
			return;
	
	g_mxr_ducktime = dtime;

	// clear out all total volume values for groups

	for ( i = 0; i < g_cgrouprules; i++)
		g_grouprules[i].total_vol = 0.0;
	
	// for every channel in a mix group which can cause ducking:
	// get total volume, store total in grouprule:
	
	CChannelList list;
	int ch_idx;

	channel_t *pchan;
	bool b_found_ducked_channel = false;

	g_ActiveChannels.GetActiveChannels( list );

	for ( i = 0; i < list.Count(); i++ )
	{
		ch_idx = list.GetChannelIndex(i);
		pchan = &channels[ch_idx];

		if (pchan->last_vol > 0.0)
		{
			// account for all mix groups this channel belongs to...

			for (int j = 0; j < 8; j++)
			{
				int imixgroup = pchan->mixgroups[j];

				if (imixgroup < 0)
					continue;
				
				int	grouprulesid = g_mapMixgroupidToGrouprulesid[imixgroup];
			
				if (g_grouprules[grouprulesid].causes_ducking)
					g_grouprules[grouprulesid].total_vol += pchan->last_vol;

				if (g_grouprules[grouprulesid].is_ducked)
					b_found_ducked_channel = true;
			}
		}		
	}
	
	// if no channels playing which may be ducked, do nothing

	if ( !b_found_ducked_channel )
		return;

	// for all groups that can be ducked:
	// see if a higher priority sound group has a volume > threshold, 
	// if so, then duck this group by setting duck_target_vol to duck_target_pct.
	// if no sound group is causing ducking in this group, reset duck_target_vol to 1.0

	for (i = 0; i < g_cgrouprules; i++)
	{
		if (g_grouprules[i].is_ducked)
		{
			int priority = g_grouprules[i].priority;

			float duck_volume = 1.0;				// clear to 1.0 if no channel causing ducking

			// make sure we interact appropriately with global voice ducking...
			// if global voice ducking is active, skip sound group ducking and just set duck_volume target to 1.0

			if ( g_DuckScale >= 1.0 )
			{	
				// check all sound groups for higher priority duck trigger

				for (int j = 0; j < g_cgrouprules; j++)
				{
					if (g_grouprules[j].priority > priority && 
						g_grouprules[j].causes_ducking &&
						g_grouprules[j].total_vol > g_grouprules[j].ducker_threshold)
					{
						// a higher priority group is causing this group to be ducked
						// set duck volume target to the ducked group's duck target percent
						// and break

						duck_volume = g_grouprules[i].duck_target_pct;
						
						// UNDONE: to prevent edge condition caused by crossing threshold, may need to have secondary
						// UNDONE: timer which allows ducking at 0.2 hz

						break;
					}
				}
			}

			g_grouprules[i].duck_target_vol = duck_volume; 
		}
	}

	// update all ducker ramps if current duck value is not target
	// if ramp is greater than duck_volume, approach at 'attack rate'
	// if ramp is less than duck_volume, approach at 'decay rate'

	for (i = 0; i < g_cgrouprules; i++)
	{
		float target	= g_grouprules[i].duck_target_vol;
		float current	= g_grouprules[i].duck_ramp_val;
			
		if (g_grouprules[i].is_ducked && (current != target))
		{

			float ramptime = target < current ? snd_duckerattacktime.GetFloat() : snd_duckerreleasetime.GetFloat();

			// delta is volume change per update (we can do this 
			// since we run at an approximate fixed update rate of 10hz)

			float delta	= (1.0 - g_grouprules[i].duck_target_pct);
			
			delta *= ( SND_DUCKER_UPDATETIME / ramptime );
			
			if (current > target)
				delta = -delta;

			// update ramps

			current += delta;

			if (current < target && delta < 0)
				current = target;
			if (current > target && delta > 0)
				current = target;

			g_grouprules[i].duck_ramp_val = current;
		}
	}

}

ConVar snd_showmixer("snd_showmixer", "0");	// set to 1 to show mixer every frame

// show the current soundmixer output

void MXR_DebugShowMixVolumes( void )
{
	if (snd_showmixer.GetInt() == 0)
		return;

	// for the current soundmixer:
	// make a totalvolume bucket for each mixgroup type in the soundmixer.
	// for every active channel, add its spatialized volume to 
	// totalvolume bucket for that channel's selected mixgroup
	
	// display all mixgroup/volume/totalvolume values as horizontal bars

	debug_showvols_t groupvols[CMXRGROUPMAX];

	int i;
	int cgroups = 0;

	if (g_isoundmixer < 0)
	{
		DevMsg("No sound mixer selected!");
		return;
	}
	
	soundmixer_t *pmixer = &g_soundmixers[g_isoundmixer];

	// for every entry in mapMixgroupidToValue which is not -1, 
	// set up groupvols

	for (i = 0; i < CMXRGROUPMAX; i++)
	{
		if (pmixer->mapMixgroupidToValue[i] >= 0)
		{
			groupvols[cgroups].mixgroupid = i;
			groupvols[cgroups].psz = MXR_GetGroupnameFromId( i );
			groupvols[cgroups].totalvol = 0.0;
			groupvols[cgroups].vol = pmixer->mapMixgroupidToValue[i];
			cgroups++;
		}
	}

	// for every active channel, get its volume and 
	// the selected mixgroupid, add to groupvols totalvol

	CChannelList list;
	int ch_idx;
	channel_t *pchan;

	g_ActiveChannels.GetActiveChannels( list );

	for ( i = 0; i < list.Count(); i++ )
	{
		ch_idx = list.GetChannelIndex(i);
		pchan = &channels[ch_idx];
		if (pchan->last_vol > 0.0)
		{
			// find entry in groupvols
			for (int j = 0; j < CMXRGROUPMAX; j++)
			{
				if (pchan->last_mixgroupid == groupvols[j].mixgroupid)
				{
					groupvols[j].totalvol += pchan->last_vol;
					break;
				}
			}
		}	
	}

	// groupvols is now fully initialized - just display it

	MXR_DebugGraphMixVolumes( groupvols, cgroups);
}

#ifdef _DEBUG

// set the named mixgroup volume to vol for the current soundmixer
static void MXR_DebugSetMixGroupVolume( const CCommand &args )
{
	if ( args.ArgC() != 3 )
	{
		DevMsg("Parameters: mix group name, volume");
		return;
	}

	const char *szgroupname = args[1];
	float vol = atof( args[2] );

	int imixgroup = MXR_GetMixgroupFromName( szgroupname );

	if ( g_isoundmixer < 0 )
		return;
	
	soundmixer_t *pmixer = &g_soundmixers[g_isoundmixer];

	pmixer->mapMixgroupidToValue[imixgroup] = vol;
}

#endif //_DEBUG

// given array of groupids (ie: the sound is in these groups),
// return a mix volume.

// return first mixgroup id in the provided array
// which maps to a non -1 volume value for this
// sound mixer

float MXR_GetVolFromMixGroup( int rgmixgroupid[8], int *plast_mixgroupid )
{
	
	// if no soundmixer currently set, return 1.0 volume

	if (g_isoundmixer < 0)
	{
		*plast_mixgroupid = 0;
		return 1.0;
	}

	float duckgain = 1.0;

	if (g_csoundmixers)
	{
		soundmixer_t *pmixer = &g_soundmixers[g_isoundmixer];

		if (pmixer)
		{
			// search mixgroupid array, return first match (non -1)

			for (int i = 0; i < 8; i++)
			{
				int imixgroup = rgmixgroupid[i];

				if (imixgroup < 0)
					continue;

				// save lowest duck gain value for any of the mix groups this sound is in

				float duckgain_new = MXR_GetDuckVolume( imixgroup );

				if ( duckgain_new < duckgain)
					duckgain = duckgain_new;

				Assert(imixgroup < CMXRGROUPMAX);
				
				// return first mixgroup id in the passed in array
				// that maps to a non -1 volume value for this
				// sound mixer

				if ( pmixer->mapMixgroupidToValue[imixgroup] >= 0)
				{
					*plast_mixgroupid = imixgroup;
					
					// get gain due to mixer settings

					float gain = pmixer->mapMixgroupidToValue[imixgroup];
	
					// modify gain with ducker settings for this group

					return gain * duckgain;
				}
			}
		}
	}

	*plast_mixgroupid = 0;
	return duckgain;
}

// get id of mixgroup name

int MXR_GetMixgroupFromName( const char *pszgroupname )
{
	// scan group rules for mapping from name to id
	if ( !pszgroupname )
		return -1;
	
	if ( Q_strlen(pszgroupname) == 0 )
		return -1;

	for (int i = 0; i < g_cgrouprules; i++)
	{
		if ( !Q_stricmp(g_grouprules[i].szmixgroup, pszgroupname ) )
			return g_grouprules[i].mixgroupid;
	}	

	return -1;
}

// get mixgroup name from id
char *MXR_GetGroupnameFromId( int mixgroupid)
{
	// scan group rules for mapping from name to id
	if (mixgroupid < 0)
		return NULL;

	for (int i = 0; i < g_cgrouprules; i++)
	{
		if ( g_grouprules[i].mixgroupid == mixgroupid)
			return g_grouprules[i].szmixgroup;
	}	

	return NULL;
}

// assign a unique mixgroup id to each unique named mix group
// within grouprules. Note: all mixgroupids in grouprules must be -1
// when this routine starts.

void MXR_AssignGroupIds( void )
{
	int cmixgroupid = 0;

	for (int i = 0; i < g_cgrouprules; i++)
	{
		int mixgroupid = MXR_GetMixgroupFromName( g_grouprules[i].szmixgroup );

		if (mixgroupid == -1)
		{
			// groupname is not yet assigned, provide a unique mixgroupid.

			g_grouprules[i].mixgroupid = cmixgroupid;

			// save reverse mapping, from mixgroupid to the first grouprules entry for this name

			g_mapMixgroupidToGrouprulesid[cmixgroupid] = i;

			cmixgroupid++;
		}	
	}
}

int MXR_AddClassname( const char *pName )
{
	char szclassname[CMXRNAMEMAX];
	Q_strncpy( szclassname, pName, CMXRNAMEMAX );
	for ( int i = 0; i < g_cgroupclass; i++ )
	{
		if ( !Q_stricmp( szclassname, g_groupclasslist[i].szclassname ) )
			return i;
	}
	if ( g_cgroupclass >= CMXRCLASSMAX )
	{
		Assert(g_cgroupclass < CMXRCLASSMAX);
		return -1;
	}
	Q_memcpy(g_groupclasslist[g_cgroupclass].szclassname, pName, min((size_t)CMXRNAMEMAX-1, strlen(pName)));
	g_cgroupclass++;
	return g_cgroupclass-1;
}

#define CHAR_LEFT_PAREN		'{'
#define CHAR_RIGHT_PAREN	'}'

// load group rules and sound mixers from file

bool MXR_LoadAllSoundMixers( void )
{
	// init soundmixer globals

	g_isoundmixer = -1;
	g_szsoundmixer_cur[0] = 0;

	g_csoundmixers	= 0;					// total number of soundmixers found
	g_cgrouprules	= 0;					// total number of group rules found

	Q_memset(g_soundmixers, 0, sizeof(g_soundmixers));
	Q_memset(g_grouprules, 0, sizeof(g_grouprules));

	// load file

	// build rules

	// build array of sound mixers

	char szFile[MAX_OSPATH];
	const char *pstart;
	bool bResult = false;
	char *pbuffer;

	Q_snprintf( szFile, sizeof( szFile ), "scripts/soundmixers.txt" );

	pbuffer = (char *)COM_LoadFile( szFile, 5, NULL ); // Use malloc - free at end of this routine
	if ( !pbuffer )
	{
		Error( "MXR_LoadAllSoundMixers: unable to open '%s'\n", szFile );
		return bResult;
	}

	pstart = pbuffer;
	
	// first pass: load g_grouprules[]

	// starting at top of file, 
	// scan for first '{', skipping all comment lines
		// get strings for: groupname, directory, classname, chan, sndlvl_min, sndlvl_max
		// convert chan to CHAN_ lookup
		// convert sndlvl_min, sndl_max to ints
		// store all in g_grouprules, update g_cgrouprules;
	// get next line
	// when hit '}' we're done with grouprules

	// check for first CHAR_LEFT_PAREN

	while (1)
	{
		pstart = COM_Parse( pstart );
	
		if ( strlen(com_token) <= 0)
			break; // eof

		if ( com_token[0] != CHAR_LEFT_PAREN )
			continue;

		break;
	}

	while (1)
	{
		pstart = COM_Parse( pstart );
		if (com_token[0] == CHAR_RIGHT_PAREN)
			break;
		
		grouprule_t *pgroup = &g_grouprules[g_cgrouprules];

		// copy mixgroup name, directory, classname
		// if no value specified, set to 0 length string

		if (com_token[0])
			Q_memcpy(pgroup->szmixgroup, com_token, min((size_t)CMXRNAMEMAX-1, strlen(com_token)));
		
		pstart = COM_Parse( pstart );
		if (com_token[0])
			Q_memcpy(pgroup->szdir, com_token, min((size_t)CMXRNAMEMAX-1, strlen(com_token)));

		pgroup->classId = -1;
		pstart = COM_Parse( pstart );
		if (com_token[0])
		{
			pgroup->classId = MXR_AddClassname( com_token );
		}

		// make sure all copied strings are null terminated
		pgroup->szmixgroup[CMXRNAMEMAX-1]	= 0;
		pgroup->szdir[CMXRNAMEMAX-1]		= 0;

		// lookup chan
		pstart = COM_Parse( pstart );
		if (com_token[0])
		{
			if (!Q_stricmp(com_token, "CHAN_STATIC"))
				pgroup->chantype = CHAN_STATIC;
			else if (!Q_stricmp(com_token, "CHAN_WEAPON"))
				pgroup->chantype = CHAN_WEAPON;
			else if (!Q_stricmp(com_token, "CHAN_VOICE"))
				pgroup->chantype = CHAN_VOICE;
			else if (!Q_stricmp(com_token, "CHAN_VOICE2"))
				pgroup->chantype = CHAN_VOICE2;
			else if (!Q_stricmp(com_token, "CHAN_BODY"))
				pgroup->chantype = CHAN_BODY;
			else if (!Q_stricmp(com_token, "CHAN_ITEM"))
				pgroup->chantype = CHAN_ITEM;
		}
		else
			pgroup->chantype = -1;

		// get sndlvls
		
		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->soundlevel_min = atoi(com_token);
		else
			pgroup->soundlevel_min = -1;

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->soundlevel_max = atoi(com_token);
		else
			pgroup->soundlevel_max = -1;

		// get duck priority, IsDucked, Causes_ducking, duck_target_pct

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->priority = atoi(com_token);
		else
			pgroup->priority = 50;

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->is_ducked = atoi(com_token);
		else
			pgroup->is_ducked = 0;

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->causes_ducking = atoi(com_token);
		else
			pgroup->causes_ducking = 0;

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->duck_target_pct = ((float)(atoi(com_token))) / 100.0f;
		else
			pgroup->duck_target_pct = 0.5f;

		pstart = COM_Parse( pstart );
		if (com_token[0])
			pgroup->ducker_threshold = ((float)(atoi(com_token))) / 100.0f;
		else
			pgroup->ducker_threshold = 0.5f;

		pgroup->duck_ramp_val = 1.0;
		pgroup->duck_target_vol = 1.0;
		pgroup->total_vol = 0.0;

		// set mixgroup id to -1
		pgroup->mixgroupid = -1;

		// update rule count

		g_cgrouprules++;
		
		if (g_cgrouprules >= CMXRGROUPRULESMAX)
		{
			// UNDONE: error! too many rules
			break;
		}
	}
	
	// now process all groupids in groups, such that
	// each mixgroup gets a unique id.

	MXR_AssignGroupIds();

	// now load g_soundmixers

	// while not at end of file...
		// scan for "<name>", if found save as new soundmixer name
			// while not '}'
			// scan for "<name>", save as groupname
			// scan for "<num>", save as mix value
	
	while(1)
	{
		pstart = COM_Parse( pstart );

		if ( strlen(com_token) <= 0)
			break;	// eof

		// save name in soundmixer

		soundmixer_t *pmixer = &g_soundmixers[g_csoundmixers];

		Q_memcpy(pmixer->szsoundmixer, com_token, min((size_t)CMXRNAMEMAX-1, strlen(com_token)));

		// init all mixer values to -1.

		for (int j = 0; j < CMXRGROUPMAX; j++)
		{
			pmixer->mapMixgroupidToValue[j] = -1.0;
		}

		// load all groupnames for this soundmixer

		while (1)
		{
			pstart = COM_Parse( pstart );

			if (com_token[0] == CHAR_LEFT_PAREN)
				continue;	// skip {

			if (com_token[0] == CHAR_RIGHT_PAREN)
				break;	// finished with this sounmixer
			
			// lookup mixgroupid for groupname
			int mixgroupid = MXR_GetMixgroupFromName( com_token );
			float value;
			
			// get mix value
			pstart = COM_Parse( pstart );
			value = atof( com_token );

			// store value for mixgroupid
			Assert(mixgroupid <= CMXRGROUPMAX);

			pmixer->mapMixgroupidToValue[mixgroupid] = value;
		}

		g_csoundmixers++;
		if (g_csoundmixers >= CMXRSOUNDMIXERSMAX)
		{
			// UNDONE: error! to many sound mixers
			break;
		}
	}

	bResult = true;

// loadmxr_exit:
	free( pbuffer );
	return bResult;
}

void MXR_ReleaseMemory( void )
{
	// free all resources
}

float S_GetMono16Samples( const char *pszName, CUtlVector< short >& sampleList )
{
	CSfxTable *pSfx = S_PrecacheSound( PSkipSoundChars( pszName ) );
	if ( !pSfx )
		return 0.0f;

	CAudioSource *pWave = pSfx->pSource;
	if ( !pWave )
		return 0.0f;

	int nType = pWave->GetType();
	if ( nType != CAudioSource::AUDIO_SOURCE_WAV )
		return 0.0f;

	CAudioMixer *pMixer = pWave->CreateMixer();
	if ( !pMixer )
		return 0.0f;

	float duration = AudioSource_GetSoundDuration( pSfx );

	// Determine start/stop positions
	int totalsamples = (int)( duration * pWave->SampleRate() );
	if ( totalsamples <= 0 )
		return 0;

	bool bStereo = pWave->IsStereoWav();
	int mix_sample_size = pMixer->GetMixSampleSize();
	int nNumChannels = bStereo ? 2 : 1;

	char *pData = NULL;

	int pos = 0;
	int remaining = totalsamples;
	while ( remaining > 0 )
	{
		int blockSize = min( remaining, 1000 );

		char copyBuf[AUDIOSOURCE_COPYBUF_SIZE];
		int copied = pWave->GetOutputData( (void **)&pData, pos, blockSize, copyBuf );
		if ( !copied )
		{
			break;
		}

		remaining -= copied;
		pos += copied;

		// Now get samples out of output data
		switch ( nNumChannels )
		{
		default:
		case 1:
			{
				for ( int i = 0; i < copied; ++i )
				{
					int offset = i * mix_sample_size;

					short sample = 0;
					if ( mix_sample_size == 1 )
					{
						char s = *( char * )( pData + offset );
						// Upscale it to fit into a short
						sample = s << 8;
					}
					else if ( mix_sample_size == 2 )
					{
						sample = *( short * )( pData + offset );
					}
					else if ( mix_sample_size == 4 )
					{
						// Not likely to have 4 bytes mono!!!
						Assert( 0 );

						int s = *( int * )( pData + offset );
						sample = s >> 16;
					}
					else
					{
						Assert( 0 );
					}

					sampleList.AddToTail( sample );
				}
			}
			break;

		case 2:
			{
				for ( int i = 0; i < copied; ++i )
				{
					int offset = i * mix_sample_size;

					short left = 0;
					short right = 0;
					
					if ( mix_sample_size == 1 )
					{
						// Not possible!!!, must be at least 2 bytes!!!
						Assert( 0 );

						char v = *( char * )( pData + offset );
						left = right = ( v << 8 );
					}
					else if ( mix_sample_size == 2 )
					{
						// One byte per channel
						left  = (short)( ( *(char *)( pData + offset ) ) << 8 );
						right = (short)( ( *(char *)( pData + offset + 1 ) ) << 8 );
					}
					else if ( mix_sample_size == 4 )
					{
						// 2 bytes per channel
						left = *( short * )( pData + offset );
						right = *( short * )( pData + offset + 2 );
					}
					else
					{
						Assert( 0 );
					}

					short sample = ( left + right ) >> 1;
					sampleList.AddToTail( sample );
				}
			}
			break;
		}
	}

	delete pMixer;

	return duration;
}