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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001/2002 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*/
#include "stdafx.h"
#include "scumm/scumm.h"
#include "sound/fmopl.h"
#include "sound/mididrv.h"
#include "scumm/imuse.h"
#include "scumm/saveload.h"
#include "scumm/sound.h"
#include "common/util.h"
/*
* Some constants
*/
#define TICKS_PER_BEAT 480
#define IMUSE_SYSEX_ID 0x7D
#define ROLAND_SYSEX_ID 0x41
#define PERCUSSION_CHANNEL 9
#define TRIGGER_ID 0
#define COMMAND_ID 1
#define MDPG_TAG "MDpg"
#define MDHD_TAG "MDhd"
/* Roland to General Midi patch table. Still needs some work. */
static const byte mt32_to_gmidi[128] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0, 1, 0, 2, 4, 4, 5, 3, 16, 17, 18, 16, 16, 19, 20, 21, // 0x
6, 6, 6, 7, 7, 7, 8, 112, 62, 62, 63, 63, 38, 38, 39, 39, // 1x
88, 54, 52, 98, 97, 99, 14, 54, 102, 96, 53, 102, 81, 100, 14, 80, // 2x
48, 48, 49, 45, 41, 40, 42, 42, 43, 46, 45, 24, 25, 28, 27, 104, // 3x
32, 32, 34, 33, 36, 37, 35, 35, 79, 73, 72, 72, 74, 75, 64, 65, // 4x
66, 67, 71, 71, 68, 69, 70, 22, 56, 59, 57, 57, 60, 60, 58, 61, // 5x
61, 11, 11, 98, 14, 9, 14, 13, 12, 107, 107, 77, 78, 78, 76, 76, // 6x
47, 117, 127, 118, 118, 116, 115, 119, 115, 112, 55, 124, 123, 0, 14, 117 // 7x
};
/* Put IMUSE specific classes here, instead of in a .h file
* they will only be used from this file, so it will reduce
* compile time */
class IMuseDriver;
struct Part;
struct MidiChannelAdl;
struct MidiChannelGM;
struct Instrument;
struct HookDatas {
byte _jump, _transpose;
byte _part_onoff[16];
byte _part_volume[16];
byte _part_program[16];
byte _part_transpose[16];
int query_param(int param, byte chan);
int set(byte cls, byte value, byte chan);
};
struct Player {
IMuseInternal *_se;
Part *_parts;
bool _active;
bool _scanning;
int _id;
byte _priority;
byte _volume;
int8 _pan;
int8 _transpose;
int8 _detune;
uint _vol_chan;
byte _vol_eff;
uint _song_index;
uint _track_index;
uint _timer_counter;
uint _loop_to_beat;
uint _loop_from_beat;
uint _loop_counter;
uint _loop_to_tick;
uint _loop_from_tick;
uint32 _tempo;
uint32 _tempo_eff; /* NoSave */
uint32 _cur_pos;
uint32 _next_pos;
uint32 _song_offset;
uint32 _timer_speed; /* NoSave */
uint _tick_index;
uint _beat_index;
uint _ticks_per_beat;
byte _speed; /* NoSave */
bool _abort;
HookDatas _hook;
byte _marker; // Sam & Max: SysEx marker
bool _mt32emulate;
bool _isGM;
/* Player part */
void hook_clear();
void clear();
bool start_sound(int sound);
void uninit_parts();
byte *parse_midi(byte *s);
void key_off(uint8 chan, byte data);
void key_on(uint8 chan, byte data, byte velocity);
void part_set_transpose(uint8 chan, byte relative, int8 b);
void parse_sysex(byte *p, uint len);
void maybe_jump (byte cmd, uint track, uint beat, uint tick);
void maybe_set_transpose(byte *data);
void maybe_part_onoff(byte *data);
void maybe_set_volume(byte *data);
void maybe_set_program(byte *data);
void maybe_set_transpose_part(byte *data);
uint update_actives();
Part *get_part(uint8 part);
void turn_off_pedals();
int set_vol(byte vol);
int get_param(int param, byte chan);
int query_part_param(int param, byte chan);
int set_transpose(byte relative, int b);
void set_priority(int pri);
void set_pan(int pan);
void set_detune(int detune);
void turn_off_parts();
void play_active_notes();
void cancel_volume_fade();
static void decode_sysex_bytes(byte *src, byte *dst, int len);
void clear_active_note(int chan, byte note);
void set_active_note(int chan, byte note);
void clear_active_notes();
/* Sequencer part */
bool set_loop(uint count, uint tobeat, uint totick, uint frombeat, uint fromtick);
void clear_loop();
void set_speed(byte speed);
bool jump(uint track, uint beat, uint tick);
void uninit_seq();
void set_tempo(uint32 data);
int start_seq_sound(int sound);
void find_sustaining_notes(byte *a, byte *b, uint32 l);
int scan(uint totrack, uint tobeat, uint totick);
int query_param(int param);
int fade_vol(byte vol, int time);
bool is_fading_out();
void sequencer_timer();
};
struct VolumeFader {
Player *player;
bool active;
byte curvol;
uint16 speed_lo_max, num_steps;
int8 speed_hi;
int8 direction;
int8 speed_lo;
uint16 speed_lo_counter;
void initialize() {
active = false;
}
void on_timer(bool probe);
byte fading_to();
};
struct SustainingNotes {
SustainingNotes *next;
SustainingNotes *prev;
Player *player;
byte note, chan;
uint32 off_pos;
uint32 pos;
uint16 counter;
};
struct CommandQueue {
uint16 array[8];
};
struct IsNoteCmdData {
byte chan;
byte note;
byte vel;
};
struct MidiChannel {
Part *_part;
MidiChannelAdl *adl() { return (MidiChannelAdl *)this; }
MidiChannelGM *gm() { return (MidiChannelGM *)this; }
};
struct MidiChannelGM : MidiChannel {
byte _chan;
uint16 _actives[8];
};
struct Part {
int _slot;
IMuseDriver *_drv;
Part *_next, *_prev;
MidiChannel *_mc;
Player *_player;
int16 _pitchbend;
byte _pitchbend_factor;
int8 _transpose, _transpose_eff;
byte _vol, _vol_eff;
int8 _detune, _detune_eff;
int8 _pan, _pan_eff;
bool _on;
byte _modwheel;
bool _pedal;
byte _program;
int8 _pri;
byte _pri_eff;
byte _chan;
byte _effect_level;
byte _chorus;
byte _percussion;
byte _bank;
void key_on(byte note, byte velocity);
void key_off(byte note);
void set_param(byte param, int value);
void init(IMuseDriver * _driver);
void setup(Player *player);
void uninit();
void off();
void silence();
void set_instrument(uint b);
void set_instrument(Instrument * data);
void set_transpose(int8 transpose);
void set_vol(uint8 volume);
void set_detune(int8 detune);
void set_pri(int8 pri);
void set_pan(int8 pan);
void set_modwheel(uint value);
void set_pedal(bool value);
void set_pitchbend(int value);
void release_pedal();
void set_program(byte program);
void set_chorus(uint chorus);
void set_effect_level(uint level);
int update_actives(uint16 *active);
void set_pitchbend_factor(uint8 value);
void set_onoff(bool on);
void fix_after_load();
void update_pris();
void changed(uint16 what);
};
struct ImTrigger {
int sound;
byte id;
byte command [4];
};
/* Abstract IMuseInternal driver class */
class IMuseDriver {
public:
enum {
pcMod = 1,
pcVolume = 2,
pcPedal = 4,
pcModwheel = 8,
pcPan = 16,
pcEffectLevel = 32,
pcProgram = 64,
pcChorus = 128,
pcPitchBendFactor = 256,
pcAll = 511,
};
virtual void on_timer() = 0;
virtual uint32 get_base_tempo() = 0;
virtual byte get_hardware_type() = 0;
virtual void init(IMuseInternal *eng, OSystem *syst) = 0;
virtual void uninit() = 0;
virtual void update_pris() = 0;
virtual void set_instrument(uint slot, byte *instr) = 0;
virtual void part_set_instrument(Part *part, Instrument * instr) = 0;
virtual void part_key_on(Part *part, byte note, byte velocity) = 0;
virtual void part_key_off(Part *part, byte note) = 0;
virtual void part_off(Part *part) = 0;
virtual void part_changed(Part *part, uint16 what) = 0;
virtual void part_set_param(Part *part, byte param, int value) = 0;
virtual int part_update_active(Part *part, uint16 *active) = 0;
virtual byte get_channel_program (byte channel) = 0;
};
// WARNING: This is the internal variant of the IMUSE class.
// imuse.h contains a public version of the same class.
// the public version, only contains a set of methods.
class IMuseInternal : public IMuse {
friend struct Player;
private:
IMuseDriver * _driver;
byte **_base_sounds;
byte _locked;
byte _hardware_type;
private:
bool _paused;
bool _active_volume_faders;
bool _initialized;
byte _volume_fader_counter;
int _game_tempo;
uint _queue_end, _queue_pos, _queue_sound;
byte _queue_adding;
SustainingNotes *_sustain_notes_used;
SustainingNotes *_sustain_notes_free;
SustainingNotes *_sustain_notes_head;
byte _queue_marker;
byte _queue_cleared;
byte _master_volume; /* Master volume. 0-127 */
byte _music_volume; /* Global music volume. 0-128 */
uint16 _trigger_count;
ImTrigger _snm_triggers[16]; // Sam & Max triggers
uint16 _channel_volume[8];
uint16 _channel_volume_eff[8]; /* NoSave */
uint16 _volchan_table[8];
Player _players[8];
SustainingNotes _sustaining_notes[24];
VolumeFader _volume_fader[8];
Part _parts[32];
uint16 _active_notes[128];
CommandQueue _cmd_queue[64];
byte *findTag(int sound, char *tag, int index);
bool isMT32(int sound);
bool isGM(int sound);
int get_queue_sound_status(int sound);
Player *allocate_player(byte priority);
void handle_marker(uint id, byte data);
int get_channel_volume(uint a);
void init_players();
void init_parts();
void init_volume_fader();
void init_sustaining_notes();
void init_queue();
void sequencer_timers();
void expire_sustain_notes();
void expire_volume_faders();
Part *allocate_part(byte pri);
int enqueue_command(int a, int b, int c, int d, int e, int f, int g);
int enqueue_trigger(int sound, int marker);
int query_queue(int param);
Player *get_player_byid(int id);
int get_volchan_entry(uint a);
int set_volchan_entry(uint a, uint b);
int set_channel_volume(uint chan, uint vol);
void update_volumes();
void reset_tick();
VolumeFader *allocate_volume_fader();
int set_volchan(int sound, int volchan);
void fix_parts_after_load();
void fix_players_after_load(Scumm *scumm);
static int saveReference(IMuseInternal *me, byte type, void *ref);
static void *loadReference(IMuseInternal *me, byte type, int ref);
void lock();
void unlock();
int set_master_volume_intern(uint vol);
public:
~IMuseInternal();
Part *parts_ptr() {
return _parts;
}
IMuseDriver *driver() {
return _driver;
}
int initialize(OSystem *syst, MidiDriver *midi, SoundMixer *mixer);
/* Public interface */
void on_timer();
void pause(bool paused);
int terminate();
int save_or_load(Serializer *ser, Scumm *scumm);
int set_music_volume(uint vol);
int get_music_volume();
int set_master_volume(uint vol);
int get_master_volume();
byte get_channel_program (byte channel) { return _driver->get_channel_program (channel); }
bool start_sound(int sound);
int stop_sound(int sound);
int stop_all_sounds();
int get_sound_status(int sound);
int32 do_command(int a, int b, int c, int d, int e, int f, int g, int h);
int clear_queue();
void setBase(byte **base);
uint32 property(int prop, uint32 value);
static IMuseInternal *create(OSystem *syst, MidiDriver *midi, SoundMixer *mixer);
};
/* IMuseAdlib classes */
struct Struct10 {
byte active;
int16 cur_val;
int16 count;
uint16 param;
int16 start_value;
byte loop;
byte table_a[4];
byte table_b[4];
int8 unk3;
int8 modwheel;
int8 modwheel_last;
uint16 speed_lo_max;
uint16 num_steps;
int16 speed_hi;
int8 direction;
uint16 speed_lo;
uint16 speed_lo_counter;
};
struct Struct11 {
int16 modify_val;
byte param, flag0x40, flag0x10;
Struct10 *s10;
};
struct InstrumentExtra {
byte a, b, c, d, e, f, g, h;
};
struct Instrument {
byte flags_1;
byte oplvl_1;
byte atdec_1;
byte sustrel_1;
byte waveform_1;
byte flags_2;
byte oplvl_2;
byte atdec_2;
byte sustrel_2;
byte waveform_2;
byte feedback;
byte flags_a;
InstrumentExtra extra_a;
byte flags_b;
InstrumentExtra extra_b;
byte duration;
};
struct MidiChannelAdl : MidiChannel {
MidiChannelAdl *_next, *_prev;
byte _waitforpedal;
byte _note;
byte _channel;
byte _twochan;
byte _vol_1, _vol_2;
int16 _duration;
Struct10 _s10a;
Struct11 _s11a;
Struct10 _s10b;
Struct11 _s11b;
};
class IMuseAdlib:public IMuseDriver {
private:
FM_OPL *_opl;
byte *_adlib_reg_cache;
IMuseInternal *_se;
SoundMixer *_mixer;
int _adlib_timer_counter;
uint16 channel_table_2[9];
int _midichan_index;
int _next_tick;
uint16 curnote_table[9];
MidiChannelAdl _midi_channels[9];
Instrument _part_instr[32];
Instrument _glob_instr[32];
void adlib_key_off(int chan);
void adlib_note_on(int chan, byte note, int mod);
void adlib_note_on_ex(int chan, byte note, int mod);
int adlib_read_param(int chan, byte data);
void adlib_setup_channel(int chan, Instrument * instr, byte vol_1, byte vol_2);
byte adlib_read(byte port) {
return _adlib_reg_cache[port];
}
void adlib_set_param(int channel, byte param, int value);
void adlib_key_onoff(int channel);
void adlib_write(byte port, byte value);
void adlib_playnote(int channel, int note);
MidiChannelAdl *allocate_midichan(byte pri);
void reset_tick();
void mc_off(MidiChannel * mc);
static void link_mc(Part *part, MidiChannelAdl *mc);
static void mc_inc_stuff(MidiChannelAdl *mc, Struct10 * s10, Struct11 * s11);
static void mc_init_stuff(MidiChannelAdl *mc, Struct10 * s10, Struct11 * s11, byte flags,
InstrumentExtra * ie);
static void struct10_init(Struct10 * s10, InstrumentExtra * ie);
static byte struct10_ontimer(Struct10 * s10, Struct11 * s11);
static void struct10_setup(Struct10 * s10);
static int random_nr(int a);
void mc_key_on(MidiChannel * mc, byte note, byte velocity);
static void premix_proc(void *param, int16 *buf, uint len);
public:
IMuseAdlib(SoundMixer *mixer) {
_mixer = mixer;
}
void uninit() {}
void init(IMuseInternal *eng, OSystem *syst);
void update_pris() {
}
void generate_samples(int16 *buf, int len);
void on_timer();
void set_instrument(uint slot, byte *instr);
void part_set_instrument(Part *part, Instrument * instr);
void part_key_on(Part *part, byte note, byte velocity);
void part_key_off(Part *part, byte note);
void part_set_param(Part *part, byte param, int value);
void part_changed(Part *part, uint16 what);
void part_off(Part *part);
int part_update_active(Part *part, uint16 *active);
byte get_channel_program (byte) { return 255; }
void adjust_priorities() {
}
uint32 get_base_tempo() {
#ifdef _WIN32_WCE
return 0x1F0000 * 2; // Sampled down to 11 kHz
#else //_WIN32_WCE
return 0x1F0000; // Was: 0x1924E0;
#endif //_WIN32_WCE
}
byte get_hardware_type() {
return 1;
}
};
/* IMuseGM classes */
class IMuseGM : public IMuseDriver {
IMuseInternal *_se;
OSystem *_system;
MidiDriver *_md;
MidiChannelGM _midi_channels[16];
byte _midi_program_last[16];
int16 _midi_pitchbend_last[16];
byte _midi_pitchbend_factor_last[16];
uint8 _midi_volume_last[16];
bool _midi_pedal_last[16];
byte _midi_modwheel_last[16];
byte _midi_effectlevel_last[16];
byte _midi_chorus_last[16];
int8 _midi_pan_last[16];
void midiPitchBend(byte chan, int16 pitchbend);
void midiPitchBendFactor (byte chan, byte factor);
void midiVolume(byte chan, byte volume);
void midiPedal(byte chan, bool pedal);
void midiModWheel(byte chan, byte modwheel);
void midiEffectLevel(byte chan, byte level);
void midiChorus(byte chan, byte chorus);
void midiControl0(byte chan, byte value);
void midiProgram(byte chan, byte program, bool mt32emulate);
void midiPan(byte chan, int8 pan);
void midiNoteOn(byte chan, byte note, byte velocity);
void midiNoteOff(byte chan, byte note);
void midiSilence(byte chan);
void midiInit();
public:
IMuseGM(MidiDriver *midi) { _md = midi; }
void uninit();
void init(IMuseInternal *eng, OSystem *os);
void update_pris();
void part_off(Part *part);
int part_update_active(Part *part, uint16 *active);
void on_timer() {}
void set_instrument(uint slot, byte *instr) {}
void part_set_instrument(Part *part, Instrument * instr) {}
void part_set_param(Part *part, byte param, int value) {}
void part_key_on(Part *part, byte note, byte velocity);
void part_key_off(Part *part, byte note);
void part_changed(Part *part, uint16 what);
byte get_channel_program (byte channel) { return _midi_program_last [channel]; }
static int midi_driver_thread(void *param);
uint32 get_base_tempo() { return 0x460000; }
byte get_hardware_type() { return 5; }
};
//*********************************
//**** IMUSE helper functions ****
//*********************************
static int clamp(int val, int min, int max)
{
if (val < min)
return min;
if (val > max)
return max;
return val;
}
static int transpose_clamp(int a, int b, int c)
{
if (b > a)
a += (b - a + 11) / 12 * 12;
if (c < a)
a -= (a - c + 11) / 12 * 12;
return a;
}
static uint32 get_delta_time(byte **s)
{
byte *d = *s, b;
uint32 time = 0;
do {
b = *d++;
time = (time << 7) | (b & 0x7F);
} while (b & 0x80);
*s = d;
return time;
}
static uint read_word(byte *a)
{
return (a[0] << 8) + a[1];
}
static void skip_midi_cmd(byte **song_ptr)
{
byte *s, code;
const byte num_skip[] = {
2, 2, 2, 2, 1, 1, 2
};
s = *song_ptr;
code = *s++;
if (code < 0x80) {
s = NULL;
} else if (code < 0xF0) {
s += num_skip[(code & 0x70) >> 4];
} else {
if (code == 0xF0 || code == 0xF7 || code == 0xFF && *s++ != 0x2F) {
s += get_delta_time(&s);
} else {
s = NULL;
}
}
*song_ptr = s;
}
static int is_note_cmd(byte **a, IsNoteCmdData * isnote)
{
byte *s = *a;
byte code;
code = *s++;
switch (code >> 4) {
case 8: /* key off */
isnote->chan = code & 0xF;
isnote->note = *s++;
isnote->vel = *s++;
*a = s;
return 1;
case 9: /* key on */
isnote->chan = code & 0xF;
isnote->note = *s++;
isnote->vel = *s++;
*a = s;
if (isnote->vel)
return 2;
return 1;
case 0xA:
case 0xB:
case 0xE:
s++;
case 0xC:
case 0xD:
s++;
break;
case 0xF:
if (code == 0xF0 || code == 0xF7 || code == 0xFF && *s++ != 0x2F) {
s += get_delta_time(&s);
break;
}
return -1;
default:
return -1;
}
*a = s;
return 0;
}
/**********************************************************************/
IMuseInternal::~IMuseInternal() {
terminate();
}
void IMuseInternal::lock()
{
_locked++;
}
void IMuseInternal::unlock()
{
_locked--;
}
byte *IMuseInternal::findTag(int sound, char *tag, int index)
{
byte *ptr = NULL;
int32 size, pos;
if (_base_sounds) {
// FIXME: This is a hack to make certain parts of Sam & Max work.
// It's a NASTY HACK because it has to specifically skip sound 1.
// For some reason (maybe a script parse bug?), sound 1 is being
// played at the very beginning (opening logo). Until this "fix",
// the sound was never found and thus never played. It SHOULDN'T
// be played.
if (!_base_sounds[sound] && (sound > 1 || g_scumm->_gameId != GID_SAMNMAX))
g_scumm->ensureResourceLoaded (rtSound, sound);
ptr = _base_sounds[sound];
}
if (ptr == NULL) {
debug(1, "IMuseInternal::findTag completely failed finding sound %d", sound);
return NULL;
}
ptr += 8;
size = READ_BE_UINT32_UNALIGNED(ptr);
ptr += 4;
pos = 0;
while (pos < size) {
if (!memcmp(ptr + pos, tag, 4) && !index--)
return ptr + pos + 8;
pos += READ_BE_UINT32_UNALIGNED(ptr + pos + 4) + 8;
}
debug(3, "IMuseInternal::findTag failed finding sound %d", sound);
return NULL;
}
bool IMuseInternal::isMT32(int sound)
{
byte *ptr = NULL;
uint32 tag;
if (_base_sounds)
ptr = _base_sounds[sound];
if (ptr == NULL)
return false;
tag = *(((uint32 *)ptr) + 1);
switch (tag) {
case MKID('ADL '):
return false;
case MKID('ROL '):
return true;
case MKID('GMD '):
return false;
case MKID('MAC '):
return true;
case MKID('SPK '):
return false;
}
return false;
}
bool IMuseInternal::isGM(int sound)
{
byte *ptr = NULL;
uint32 tag;
if (_base_sounds)
ptr = _base_sounds[sound];
if (ptr == NULL)
return false;
tag = *(((uint32 *)ptr) + 1);
switch (tag) {
case MKID('ADL '):
return false;
case MKID('ROL '):
return true; // Yeah... for our purposes, this is GM
case MKID('GMD '):
return true;
case MKID('MIDI'):
return true;
case MKID('MAC '):
return true; // I guess this one too, since it qualifies under isMT32()
case MKID('SPK '):
return false;
}
return false;
}
bool IMuseInternal::start_sound(int sound)
{
Player *player;
void *mdhd;
mdhd = findTag(sound, MDHD_TAG, 0);
if (!mdhd) {
mdhd = findTag(sound, MDPG_TAG, 0);
if (!mdhd) {
warning("SE::start_sound failed: Couldn't find sound %d", sound);
return false;
}
}
// If the requested sound is already playing, start it over
// from scratch. This was originally a hack to prevent Sam & Max
// iMuse messiness while upgrading the iMuse engine, but it
// is apparently necessary to deal with fade-and-restart
// race conditions that were observed in MI2. Reference
// Bug #590511 and Patch #607175 (which was reversed to fix
// an FOA regression: Bug #622606).
int i;
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == sound)
break;
}
if (!i)
player = allocate_player(128);
if (!player)
return false;
player->clear();
return player->start_sound(sound);
}
Player *IMuseInternal::allocate_player(byte priority)
{
Player *player = _players, *best = NULL;
int i;
byte bestpri = 255;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
if (!player->_active)
return player;
if (player->_priority < bestpri) {
best = player;
bestpri = player->_priority;
}
}
if (bestpri < priority)
return best;
debug(1, "Denying player request");
return NULL;
}
void IMuseInternal::init_players()
{
Player *player = _players;
int i;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
player->_active = false;
player->_se = this;
}
}
void IMuseInternal::init_sustaining_notes()
{
SustainingNotes *next = NULL, *sn = _sustaining_notes;
int i;
_sustain_notes_used = NULL;
_sustain_notes_head = NULL;
for (i = ARRAYSIZE(_sustaining_notes); i != 0; i--, sn++) {
sn->next = next;
next = sn;
}
_sustain_notes_free = next;
}
void IMuseInternal::init_volume_fader()
{
VolumeFader *vf = _volume_fader;
int i;
for (i = ARRAYSIZE(_volume_fader); i != 0; i--, vf++)
vf->initialize();
_active_volume_faders = false;
}
void IMuseInternal::init_parts()
{
Part *part;
int i;
for (i = 0, part = _parts; i != ARRAYSIZE(_parts); i++, part++) {
part->init(_driver);
part->_slot = i;
}
}
int IMuseInternal::stop_sound(int sound)
{
Player *player = _players;
int i;
int r = -1;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
if (player->_active && player->_id == sound) {
player->clear();
r = 0;
}
}
return r;
}
int IMuseInternal::stop_all_sounds()
{
Player *player = _players;
int i;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
if (player->_active)
player->clear();
}
return 0;
}
void IMuseInternal::on_timer()
{
if (_locked || _paused)
return;
lock();
sequencer_timers();
expire_sustain_notes();
expire_volume_faders();
_driver->on_timer();
unlock();
}
void IMuseInternal::sequencer_timers()
{
Player *player = _players;
int i;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
if (player->_active)
player->sequencer_timer();
}
}
void IMuseInternal::handle_marker(uint id, byte data)
{
uint16 *p;
uint pos;
pos = _queue_end;
if (pos == _queue_pos)
return;
if (_queue_adding && _queue_sound == id && data == _queue_marker)
return;
p = _cmd_queue[pos].array;
if (p[0] != TRIGGER_ID || p[1] != id || p[2] != data)
return;
_trigger_count--;
_queue_cleared = false;
do {
pos = (pos + 1) & (ARRAYSIZE(_cmd_queue) - 1);
if (_queue_pos == pos)
break;
p = _cmd_queue[pos].array;
if (*p++ != COMMAND_ID)
break;
_queue_end = pos;
do_command(p[0], p[1], p[2], p[3], p[4], p[5], p[6], 0);
if (_queue_cleared)
return;
pos = _queue_end;
} while (1);
_queue_end = pos;
}
int IMuseInternal::get_channel_volume(uint a)
{
if (a < 8)
return _channel_volume_eff[a];
return _master_volume;
}
Part *IMuseInternal::allocate_part(byte pri)
{
Part *part, *best = NULL;
int i;
for (i = ARRAYSIZE(_parts), part = _parts; i != 0; i--, part++) {
if (!part->_player)
return part;
if (pri >= part->_pri_eff) {
pri = part->_pri_eff;
best = part;
}
}
if (best)
best->uninit();
else
debug(1, "Denying part request");
return best;
}
void IMuseInternal::expire_sustain_notes()
{
SustainingNotes *sn, *next;
Player *player;
uint32 counter;
for (sn = _sustain_notes_head; sn; sn = next) {
next = sn->next;
player = sn->player;
counter = sn->counter + player->_timer_speed;
sn->pos += counter >> 16;
sn->counter = (unsigned short)counter & 0xFFFF;
if (sn->pos >= sn->off_pos) {
player->key_off(sn->chan, sn->note);
/* Unlink the node */
if (next)
next->prev = sn->prev;
if (sn->prev)
sn->prev->next = next;
else
_sustain_notes_head = next;
/* And put it in the free list */
sn->next = _sustain_notes_free;
_sustain_notes_free = sn;
}
}
}
void IMuseInternal::expire_volume_faders()
{
VolumeFader *vf;
int i;
if (++_volume_fader_counter & 7)
return;
if (!_active_volume_faders)
return;
_active_volume_faders = false;
vf = _volume_fader;
for (i = ARRAYSIZE(_volume_fader); i != 0; i--, vf++) {
if (vf->active) {
_active_volume_faders = true;
vf->on_timer(false);
}
}
}
void VolumeFader::on_timer(bool probe)
{
byte newvol;
newvol = curvol + speed_hi;
speed_lo_counter += speed_lo;
if (speed_lo_counter >= speed_lo_max) {
speed_lo_counter -= speed_lo_max;
newvol += direction;
}
if (curvol != newvol) {
curvol = newvol;
if (!newvol) {
if (!probe)
player->clear();
active = false;
return;
}
if (!probe)
player->set_vol(newvol);
}
if (!--num_steps) {
active = false;
}
}
byte VolumeFader::fading_to()
{
byte newvol;
byte orig_curvol;
uint16 orig_speed_lo_counter, orig_num_steps;
if (!active)
return 127;
// It would be so much easier to just store the fade-to volume in a
// variable, but then we'd have to break savegame compatibility. So
// instead we do a "dry run" fade.
orig_speed_lo_counter = speed_lo_counter;
orig_num_steps = num_steps;
orig_curvol = curvol;
while (active)
on_timer(true);
active = true;
newvol = curvol;
speed_lo_counter = orig_speed_lo_counter;
num_steps = orig_num_steps;
curvol = orig_curvol;
return newvol;
}
int IMuseInternal::get_sound_status(int sound)
{
int i;
Player *player;
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)sound) {
// Assume that anyone asking for the sound status is
// really asking "is it ok if I start playing this
// sound now?" So if the sound is about to fade out,
// pretend it's not playing.
if (player->is_fading_out())
continue;
return 1;
}
}
return get_queue_sound_status(sound);
}
int IMuseInternal::get_queue_sound_status(int sound)
{
uint16 *a;
int i, j;
j = _queue_pos;
i = _queue_end;
while (i != j) {
a = _cmd_queue[i].array;
if (a[0] == COMMAND_ID && a[1] == 8 && a[2] == (uint16)sound)
return 2;
i = (i + 1) & (ARRAYSIZE(_cmd_queue) - 1);
}
return 0;
}
int IMuseInternal::set_volchan(int sound, int volchan)
{
int r;
int i;
int num;
Player *player, *best, *sameid;
r = get_volchan_entry(volchan);
if (r == -1)
return -1;
if (r >= 8) {
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)sound && player->_vol_chan != (uint16)volchan) {
player->_vol_chan = volchan;
player->set_vol(player->_volume);
return 0;
}
}
return -1;
} else {
best = NULL;
num = 0;
sameid = NULL;
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active) {
if (player->_vol_chan == (uint16)volchan) {
num++;
if (!best || player->_priority <= best->_priority)
best = player;
} else if (player->_id == (uint16)sound) {
sameid = player;
}
}
}
if (sameid == NULL)
return -1;
if (num >= r)
best->clear();
player->_vol_chan = volchan;
player->set_vol(player->_volume);
return 0;
}
}
int IMuseInternal::clear_queue()
{
_queue_adding = false;
_queue_cleared = true;
_queue_pos = 0;
_queue_end = 0;
_trigger_count = 0;
return 0;
}
int IMuseInternal::enqueue_command(int a, int b, int c, int d, int e, int f, int g)
{
uint16 *p;
uint i;
i = _queue_pos;
if (i == _queue_end)
return -1;
if (a == -1) {
_queue_adding = false;
_trigger_count++;
return 0;
}
p = _cmd_queue[_queue_pos].array;
p[0] = COMMAND_ID;
p[1] = a;
p[2] = b;
p[3] = c;
p[4] = d;
p[5] = e;
p[6] = f;
p[7] = g;
i = (i + 1) & (ARRAYSIZE(_cmd_queue) - 1);
if (_queue_end != i) {
_queue_pos = i;
return 0;
} else {
_queue_pos = (i - 1) & (ARRAYSIZE(_cmd_queue) - 1);
return -1;
}
}
int IMuseInternal::query_queue(int param)
{
switch (param) {
case 0: /* get trigger count */
return _trigger_count;
case 1: /* get trigger type */
if (_queue_end == _queue_pos)
return -1;
return _cmd_queue[_queue_end].array[1];
case 2: /* get trigger sound */
if (_queue_end == _queue_pos)
return 0xFF;
return _cmd_queue[_queue_end].array[2];
default:
return -1;
}
}
int IMuseInternal::get_music_volume()
{
return _music_volume * 2;
}
int IMuseInternal::set_music_volume(uint vol)
{
if (vol > 256)
vol = 256;
else if (vol < 0)
vol = 0;
_music_volume = vol / 2;
return 0;
}
int IMuseInternal::set_master_volume_intern(uint vol)
{
if (vol > 127)
return -1;
vol = vol * _music_volume / 128;
_master_volume = vol;
for (int i = 0; i != 8; i++)
_channel_volume_eff[i] = (_channel_volume[i] + 1) * vol >> 7;
update_volumes();
return 0;
}
int IMuseInternal::set_master_volume(uint vol)
{
// recalibrate from 0-256 range
vol = vol * 127 / 256;
return set_master_volume_intern(vol);
}
int IMuseInternal::get_master_volume()
{
// recalibrate to 0-256 range
return _master_volume * 256 / 127;
}
int IMuseInternal::terminate()
{
if (_driver) {
_driver->uninit();
delete _driver;
_driver = NULL;
}
return 0;
/* not implemented */
}
int IMuseInternal::enqueue_trigger(int sound, int marker)
{
uint16 *p;
uint pos;
pos = _queue_pos;
p = _cmd_queue[pos].array;
p[0] = TRIGGER_ID;
p[1] = sound;
p[2] = marker;
pos = (pos + 1) & (ARRAYSIZE(_cmd_queue) - 1);
if (_queue_end == pos) {
_queue_pos = (pos - 1) & (ARRAYSIZE(_cmd_queue) - 1);
return -1;
}
_queue_pos = pos;
_queue_adding = true;
_queue_sound = sound;
_queue_marker = marker;
return 0;
}
int32 IMuseInternal::do_command(int a, int b, int c, int d, int e, int f, int g, int h)
{
int i;
byte cmd = a & 0xFF;
byte param = a >> 8;
Player *player = NULL;
if (!_initialized && (cmd || param))
return -1;
if (param == 0) {
switch (cmd) {
case 6:
return set_master_volume_intern(b);
case 7:
return _master_volume;
case 8:
return start_sound(b) ? 0 : -1;
case 9:
return stop_sound(b);
case 10: // FIXME: Sam and Max - Not sure if this is correct
return stop_all_sounds();
case 11:
return stop_all_sounds();
case 12:
// Sam & Max: Player-scope commands
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)b)
break;
}
if (!i)
return -1;
switch (d) {
case 6:
// Set player volume.
// Jamieson360: Good, this is consistent with IMuseDigital.
return player->set_vol (e);
default:
warning("IMuseInternal::do_command (6) unsupported sub-command %d", d);
}
return -1;
case 13:
return get_sound_status(b);
case 14:
// Sam and Max: Volume Fader?
// Prevent instantaneous volume fades.
// Fixes a Ball of Twine issue, but might not be the right long-term solution.
if (f != 0) {
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)b) {
player->fade_vol(e, f);
return 0;
}
}
}
return -1;
case 15:
// Sam & Max: Unconditional Jump?
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)b) {
player->_hook._jump = d;
return 0;
}
}
return -1;
case 16:
return set_volchan(b, c);
case 17:
if (g_scumm->_gameId != GID_SAMNMAX) {
return set_channel_volume(b, c);
} else {
// Sam & Max: ImSetTrigger.
// Sets a trigger for a particular player and
// marker ID, along with do_command parameters
// to invoke at the marker. The marker is
// represented by MIDI SysEx block 00 xx (F7)
// where "xx" is the marker ID.
for (i = 0; i < 16; ++i) {
if (!_snm_triggers [i].id) {
_snm_triggers [i].id = d;
_snm_triggers [i].sound = b;
_snm_triggers [i].command [0] = e;
_snm_triggers [i].command [1] = f;
_snm_triggers [i].command [2] = g;
_snm_triggers [i].command [3] = h;
return 0;
}
}
return -1;
}
case 18:
if (g_scumm->_gameId != GID_SAMNMAX) {
return set_volchan_entry(b, c);
} else {
// Sam & Max: ImCheckTrigger.
// According to Mike's notes to Ender,
// this function returns the number of triggers
// associated with a particular player ID and
// trigger ID.
a = 0;
for (i = 0; i < 16; ++i) {
if (_snm_triggers [i].sound == b && _snm_triggers [i].id &&
(d == -1 || _snm_triggers [i].id == d))
{
++a;
}
}
return a;
}
case 19:
// Sam & Max: ImClearTrigger
// This should clear a trigger that's been set up
// with ImSetTrigger (cmd == 17). Seems to work....
a = 0;
for (i = 0; i < 16; ++i) {
if (_snm_triggers [i].sound == b && _snm_triggers [i].id &&
(d == -1 || _snm_triggers [i].id == d))
{
_snm_triggers [i].sound = _snm_triggers [i].id = 0;
++a;
}
}
return (a > 0) ? 0 : -1;
case 20: // FIXME: Deferred command system? - Sam and Max
return 0;
case 2:
case 3:
return 0;
default:
warning("IMuseInternal::do_command invalid command %d", cmd);
}
} else if (param == 1) {
if ((1 << cmd) & (0x783FFF)) {
player = get_player_byid(b);
if (!player)
return -1;
if ((1 << cmd) & (1 << 11 | 1 << 22)) {
assert(c >= 0 && c <= 15);
player = (Player *)player->get_part(c);
if (!player)
return -1;
}
}
switch (cmd) {
case 0:
if (g_scumm->_gameId == GID_SAMNMAX)
return player->_marker;
else
return player->get_param(c, d);
case 1:
if (g_scumm->_gameId == GID_SAMNMAX)
player->jump (d - 1, (e - 1) * 4 + f, ((g * player->_ticks_per_beat) >> 2) + h);
else
player->set_priority(c);
return 0;
case 2:
return player->set_vol(c);
case 3:
player->set_pan(c);
return 0;
case 4:
return player->set_transpose(c, d);
case 5:
player->set_detune(c);
return 0;
case 6:
player->set_speed(c);
return 0;
case 7:
return player->jump(c, d, e) ? 0 : -1;
case 8:
return player->scan(c, d, e);
case 9:
return player->set_loop(c, d, e, f, g) ? 0 : -1;
case 10:
player->clear_loop();
return 0;
case 11:
((Part *)player)->set_onoff(d != 0);
return 0;
case 12:
return player->_hook.set(c, d, e);
case 13:
return player->fade_vol(c, d);
case 14:
return enqueue_trigger(b, c);
case 15:
return enqueue_command(b, c, d, e, f, g, h);
case 16:
return clear_queue();
case 19:
return player->get_param(c, d);
case 20:
return player->_hook.set(c, d, e);
case 21:
return -1;
case 22:
((Part *)player)->set_vol(d);
return 0;
case 23:
return query_queue(b);
case 24:
return 0;
default:
warning("IMuseInternal::do_command default midi command %d", cmd);
return -1;
}
}
return -1;
}
int IMuseInternal::set_channel_volume(uint chan, uint vol)
{
if (chan >= 8 || vol > 127)
return -1;
_channel_volume[chan] = vol;
_channel_volume_eff[chan] = _master_volume * (vol + 1) >> 7;
update_volumes();
return 0;
}
void IMuseInternal::update_volumes()
{
Player *player;
int i;
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active)
player->set_vol(player->_volume);
}
}
int IMuseInternal::set_volchan_entry(uint a, uint b)
{
if (a >= 8)
return -1;
_volchan_table[a] = b;
return 0;
}
int HookDatas::query_param(int param, byte chan)
{
switch (param) {
case 18:
return _jump;
case 19:
return _transpose;
case 20:
return _part_onoff[chan];
case 21:
return _part_volume[chan];
case 22:
return _part_program[chan];
case 23:
return _part_transpose[chan];
default:
return -1;
}
}
int HookDatas::set(byte cls, byte value, byte chan)
{
switch (cls) {
case 0:
_jump = value;
break;
case 1:
_transpose = value;
break;
case 2:
if (chan < 16)
_part_onoff[chan] = value;
else if (chan == 16)
memset(_part_onoff, value, 16);
break;
case 3:
if (chan < 16)
_part_volume[chan] = value;
else if (chan == 16)
memset(_part_volume, value, 16);
break;
case 4:
if (chan < 16)
_part_program[chan] = value;
else if (chan == 16)
memset(_part_program, value, 16);
break;
case 5:
if (chan < 16)
_part_transpose[chan] = value;
else if (chan == 16)
memset(_part_transpose, value, 16);
break;
default:
return -1;
}
return 0;
}
VolumeFader *IMuseInternal::allocate_volume_fader()
{
VolumeFader *vf;
int i;
vf = _volume_fader;
for (i = ARRAYSIZE(_volume_fader); vf->active;) {
vf++;
if (!--i)
return NULL;
}
vf->active = true;
_active_volume_faders = true;
return vf;
}
Player *IMuseInternal::get_player_byid(int id)
{
int i;
Player *player, *found = NULL;
for (i = ARRAYSIZE(_players), player = _players; i != 0; i--, player++) {
if (player->_active && player->_id == (uint16)id) {
if (found)
return NULL;
found = player;
}
}
return found;
}
int IMuseInternal::get_volchan_entry(uint a)
{
if (a < 8)
return _volchan_table[a];
return -1;
}
uint32 IMuseInternal::property(int prop, uint32 value)
{
switch (prop) {
case IMuse::PROP_TEMPO_BASE:
_game_tempo = value;
break;
}
return 0;
}
void IMuseInternal::setBase(byte **base)
{
_base_sounds = base;
}
IMuseInternal *IMuseInternal::create(OSystem *syst, MidiDriver *midi, SoundMixer *mixer)
{
IMuseInternal *i = new IMuseInternal;
i->initialize(syst, midi, mixer);
return i;
}
int IMuseInternal::initialize(OSystem *syst, MidiDriver *midi, SoundMixer *mixer)
{
int i;
IMuseDriver *driv;
if (midi == NULL) {
driv = new IMuseAdlib(mixer);
} else {
driv = new IMuseGM(midi);
}
_driver = driv;
_hardware_type = driv->get_hardware_type();
_game_tempo = driv->get_base_tempo();
driv->init(this, syst);
_master_volume = 127;
if (_music_volume < 1)
_music_volume = kDefaultMusicVolume;
for (i = 0; i != 8; i++)
_channel_volume[i] = _channel_volume_eff[i] = _volchan_table[i] = 127;
init_players();
init_sustaining_notes();
init_volume_fader();
init_queue();
init_parts();
_initialized = true;
return 0;
}
void IMuseInternal::init_queue()
{
_queue_adding = false;
_queue_pos = 0;
_queue_end = 0;
_trigger_count = 0;
}
void IMuseInternal::pause(bool paused)
{
lock();
int i;
Part *part;
for (i = ARRAYSIZE(_parts), part = _parts; i != 0; i--, part++) {
if (part->_player) {
if (paused) {
part->_vol_eff = 0;
} else {
part->set_vol(part->_vol);
}
part->changed(IMuseDriver::pcVolume);
}
}
_paused = paused;
unlock();
}
/*************************************************************************/
int Player::fade_vol(byte vol, int time)
{
VolumeFader *vf;
int i;
cancel_volume_fade();
if (time == 0) {
set_vol(vol);
return 0;
}
vf = _se->allocate_volume_fader();
if (vf == NULL)
return -1;
vf->player = this;
vf->num_steps = vf->speed_lo_max = time;
vf->curvol = _volume;
i = (vol - vf->curvol);
vf->speed_hi = i / time;
if (i < 0) {
i = -i;
vf->direction = -1;
} else {
vf->direction = 1;
}
vf->speed_lo = i % time;
vf->speed_lo_counter = 0;
return 0;
}
bool Player::is_fading_out()
{
VolumeFader *vf = _se->_volume_fader;
int i;
for (i = 0; i < 8; i++, vf++) {
if (vf->active && vf->direction < 0 && vf->player == this && vf->fading_to() == 0)
return true;
}
return false;
}
void Player::clear()
{
uninit_seq();
cancel_volume_fade();
uninit_parts();
_active = false;
_ticks_per_beat = TICKS_PER_BEAT;
}
bool Player::start_sound(int sound)
{
void *mdhd;
mdhd = _se->findTag(sound, MDHD_TAG, 0);
if (mdhd == NULL) {
mdhd = _se->findTag(sound, MDPG_TAG, 0);
if (mdhd == NULL) {
warning("P::start_sound failed: Couldn't find %s", MDHD_TAG);
return false;
}
}
_mt32emulate = _se->isMT32(sound);
_isGM = _se->isGM(sound);
_parts = NULL;
_active = true;
_id = sound;
_priority = 0x80;
_volume = 0x7F;
_vol_chan = 0xFFFF;
_vol_eff = (_se->get_channel_volume(0xFFFF) << 7) >> 7;
_pan = 0;
_transpose = 0;
_detune = 0;
hook_clear();
if (start_seq_sound(sound) != 0) {
_active = false;
return false;
}
return true;
}
void Player::hook_clear()
{
memset(&_hook, 0, sizeof(_hook));
}
int Player::start_seq_sound(int sound)
{
byte *ptr, *track_ptr;
_song_index = sound;
_timer_counter = 0;
_loop_to_beat = 1;
_loop_from_beat = 1;
_track_index = 0;
_loop_counter = 0;
_loop_to_tick = 0;
_loop_from_tick = 0;
set_tempo(500000);
set_speed(128);
ptr = _se->findTag(sound, "MTrk", _track_index);
if (ptr == NULL)
return -1;
track_ptr = ptr;
_cur_pos = _next_pos = get_delta_time(&track_ptr);
_song_offset = track_ptr - ptr;
_tick_index = _cur_pos;
_beat_index = 1;
if (_tick_index >= _ticks_per_beat) {
_beat_index += _tick_index / _ticks_per_beat;
_tick_index %= _ticks_per_beat;
}
return 0;
}
void Player::set_tempo(uint32 b)
{
uint32 i, j;
i = _se->_game_tempo;
j = _tempo = b;
while (i & 0xFFFF0000 || j & 0xFFFF0000) {
i >>= 1;
j >>= 1;
}
_tempo_eff = (i << 16) / j;
set_speed(_speed);
}
void Player::cancel_volume_fade()
{
VolumeFader *vf = _se->_volume_fader;
int i;
for (i = 0; i < 8; i++, vf++) {
if (vf->active && vf->player == this)
vf->active = false;
}
}
void Player::uninit_parts()
{
if (_parts && _parts->_player != this)
error("asd");
while (_parts)
_parts->uninit();
}
void Player::uninit_seq()
{
_abort = true;
}
void Player::set_speed(byte speed)
{
_speed = speed;
_timer_speed = (_tempo_eff * speed >> 7);
}
byte *Player::parse_midi(byte *s)
{
byte cmd, chan, note, velocity, control;
uint value;
Part *part;
cmd = *s++;
chan = cmd & 0xF;
switch (cmd >> 4) {
case 0x8: /* key off */
note = *s++;
if (!_scanning) {
key_off(chan, note);
} else {
clear_active_note(chan, note);
}
s++; /* skip velocity */
break;
case 0x9: /* key on */
note = *s++;
velocity = *s++;
if (velocity) {
if (!_scanning)
key_on(chan, note, velocity);
else
set_active_note(chan, note);
} else {
if (!_scanning)
key_off(chan, note);
else
clear_active_note(chan, note);
}
break;
case 0xA: /* aftertouch */
s += 2;
break;
case 0xB: /* control change */
control = *s++;
value = *s++;
part = get_part(chan);
if (!part)
break;
switch (control) {
case 1: /* modulation wheel */
part->set_modwheel(value);
break;
case 7: /* volume */
part->set_vol(value);
break;
case 10: /* pan position */
part->set_pan(value - 0x40);
break;
case 16: /* pitchbend factor */
part->set_pitchbend_factor(value);
break;
case 17: /* gp slider 2 */
part->set_detune(value - 0x40);
break;
case 18: /* gp slider 3 */
part->set_pri(value - 0x40);
_se->_driver->update_pris();
break;
case 64: /* hold pedal */
part->set_pedal(value != 0);
break;
case 91: /* effects level */
part->set_effect_level(value);
break;
case 93: /* chorus */
part->set_chorus(value);
break;
default:
warning("parse_midi: invalid control %d", control);
}
break;
case 0xC: /* program change */
value = *s++;
debug (2, "Player::parse_midi - Setting channel %2d to program %d", chan, value); // Jamieson630: Helps to build the GM-to-FM mapping
part = get_part(chan);
if (part)
part->set_program(value);
break;
case 0xD: /* channel pressure */
s++;
break;
case 0xE: /* pitch bend */
part = get_part(chan);
if (part)
// part->set_pitchbend(((s[1] - 0x40) << 7) | s[0]);
part->set_pitchbend(((s[1] << 7) | s[0]) - 0x2000);
s += 2;
break;
case 0xF:
if (chan == 0) {
uint size = get_delta_time(&s);
parse_sysex(s, size);
s += size;
} else if (chan == 0xF) {
cmd = *s++;
if (cmd == 47)
goto Error; /* end of song */
if (cmd == 81) {
set_tempo((s[1] << 16) | (s[2] << 8) | s[3]);
s += 4;
break;
}
s += get_delta_time(&s);
} else if (chan == 0x7) {
s += get_delta_time(&s);
} else {
goto Error;
}
break;
default:
Error:;
if (!_scanning)
clear();
return NULL;
}
return s;
}
struct {
char *name;
byte program;
}
roland_to_gm_map [] = {
// TODO: Construct this database.
{ " ", 0 }
};
void Player::parse_sysex(byte *p, uint len)
{
byte code;
byte a;
uint b;
byte buf[128];
Part *part;
// Check SysEx manufacturer.
// Roland is 0x41
a = *p++;
--len;
if (a != IMUSE_SYSEX_ID) {
if (a == ROLAND_SYSEX_ID) {
// Roland custom instrument definition.
part = get_part (p[0] & 0x0F);
if (part) {
memcpy (buf, p + 6, 10);
buf[10] = '\0';
// debug (0, "[%02d] Roland (model 0x%02X, cmd 0x%02X) Instrument: \"%8s\"", p[0], p[1], p[2], buf);
for (b = 0; b < ARRAYSIZE(roland_to_gm_map); ++b) {
if (!memcmp (roland_to_gm_map[b].name, buf, 10)) {
a = roland_to_gm_map[b].program;
for (b = 0; b < ARRAYSIZE(mt32_to_gmidi); ++b) {
if (mt32_to_gmidi [b] == a) {
part->set_program (b);
return;
}
}
warning ("Could not find appropriate MT-32 program for GM program %d", (int) a);
return;
}
}
warning ("Could not find appropriate GM program for MT-32 custom instrument \"%s\"", buf);
}
} else {
warning ("Unknown SysEx manufacturer 0x%02X", (int) a);
return;
}
}
--len;
// Too big?
if (len >= sizeof(buf) * 2)
return;
switch (code = *p++) {
case 0:
if (g_scumm->_gameId != GID_SAMNMAX) {
// There are 17 bytes of useful information beyond
// what we've read so far. All we know about them is
// as follows:
// BYTE 00: Channel #
// BYTE 02: BIT 01 (0x01): Part on? (1 = yes)
// BYTE 05: Volume (upper 4 bits) [guessing]
// BYTE 06: Volume (lower 4 bits) [guessing]
// BYTE 09: BIT 04 (0x08): Percussion? (1 = yes)
part = get_part (p[0] & 0x0F);
if (part) {
part->set_onoff (p[2] & 0x01);
part->set_vol ((p[5] & 0x0F) << 4 | (p[6] & 0x0F));
part->_percussion = ((p[9] & 0x08) > 0);
if (part->_percussion) {
if (part->_mc)
part->off();
} else {
part->changed (IMuseDriver::pcAll);
}
}
} else {
// Sam & Max seems to use this as a marker for
// ImSetTrigger. When a marker is encountered whose sound
// ID and (presumably) marker ID match what was set by
// ImSetTrigger, something magical is supposed to happen....
_marker = *p;
for (a = 0; a < 16; ++a) {
if (_se->_snm_triggers [a].sound == _id &&
_se->_snm_triggers [a].id == *p)
{
_se->_snm_triggers [a].sound = _se->_snm_triggers [a].id = 0;
_se->do_command (_se->_snm_triggers [a].command [0],
_se->_snm_triggers [a].command [1],
_se->_snm_triggers [a].command [2],
_se->_snm_triggers [a].command [3],
0, 0, 0, 0);
break;
}
}
} // end if
break;
case 1:
// This SysEx is used in Sam & Max for maybe_jump.
if (_scanning)
break;
maybe_jump (p[0], p[1] - 1, (read_word (p + 2) - 1) * 4 + p[4], ((p[5] * _ticks_per_beat) >> 2) + p[6]);
break;
case 16: /* set instrument in part */
a = *p++ & 0x0F;
if (_se->_hardware_type != *p++)
break;
decode_sysex_bytes(p, buf, len - 3);
part = get_part(a);
if (part)
part->set_instrument((Instrument *) buf);
break;
case 17: /* set global instrument */
p++;
if (_se->_hardware_type != *p++)
break;
a = *p++;
decode_sysex_bytes(p, buf, len - 4);
_se->_driver->set_instrument(a, buf);
break;
case 33: /* param adjust */
a = *p++ & 0x0F;
if (_se->_hardware_type != *p++)
break;
decode_sysex_bytes(p, buf, len - 3);
part = get_part(a);
if (part)
part->set_param(read_word(buf), read_word(buf + 2));
break;
case 48: /* hook - jump */
if (_scanning)
break;
decode_sysex_bytes(p + 1, buf, len - 2);
maybe_jump (buf[0], read_word (buf + 1), read_word (buf + 3), read_word (buf + 5));
break;
case 49: /* hook - global transpose */
decode_sysex_bytes(p + 1, buf, len - 2);
maybe_set_transpose(buf);
break;
case 50: /* hook - part on/off */
buf[0] = *p++ & 0x0F;
decode_sysex_bytes(p, buf + 1, len - 2);
maybe_part_onoff(buf);
break;
case 51: /* hook - set volume */
buf[0] = *p++ & 0x0F;
decode_sysex_bytes(p, buf + 1, len - 2);
maybe_set_volume(buf);
break;
case 52: /* hook - set program */
buf[0] = *p++ & 0x0F;
decode_sysex_bytes(p, buf + 1, len - 2);
maybe_set_program(buf);
break;
case 53: /* hook - set transpose */
buf[0] = *p++ & 0x0F;
decode_sysex_bytes(p, buf + 1, len - 2);
maybe_set_transpose_part(buf);
break;
case 64: /* marker */
p++;
len -= 2;
while (len--) {
_se->handle_marker(_id, *p++);
}
break;
case 80: /* loop */
decode_sysex_bytes(p + 1, buf, len - 2);
set_loop(read_word(buf),
read_word(buf + 2), read_word(buf + 4), read_word(buf + 6), read_word(buf + 8)
);
break;
case 81: /* end loop */
clear_loop();
break;
case 96: /* set instrument */
part = get_part(p[0] & 0x0F);
b = (p[1] & 0x0F) << 12 | (p[2] & 0x0F) << 8 | (p[4] & 0x0F) << 4 | (p[4] & 0x0F);
if (part)
part->set_instrument(b);
break;
default:
debug(6, "unknown sysex %d", code);
}
}
void Player::decode_sysex_bytes(byte *src, byte *dst, int len)
{
while (len >= 0) {
*dst++ = (src[0] << 4) | (src[1] & 0xF);
src += 2;
len -= 2;
}
}
void Player::maybe_jump (byte cmd, uint track, uint beat, uint tick)
{
/* is this the hook i'm waiting for? */
if (cmd && _hook._jump != cmd)
return;
/* reset hook? */
if (cmd != 0 && cmd < 0x80)
_hook._jump = 0;
jump (track, beat, tick);
}
void Player::maybe_set_transpose(byte *data)
{
byte cmd;
cmd = data[0];
/* is this the hook i'm waiting for? */
if (cmd && _hook._transpose != cmd)
return;
/* reset hook? */
if (cmd != 0 && cmd < 0x80)
_hook._transpose = 0;
set_transpose(data[1], (int8)data[2]);
}
void Player::maybe_part_onoff(byte *data)
{
byte cmd, *p;
uint chan;
Part *part;
cmd = data[1];
chan = data[0];
p = &_hook._part_onoff[chan];
/* is this the hook i'm waiting for? */
if (cmd && *p != cmd)
return;
if (cmd != 0 && cmd < 0x80)
*p = 0;
part = get_part(chan);
if (part)
part->set_onoff(data[2] != 0);
}
void Player::maybe_set_volume(byte *data)
{
byte cmd;
byte *p;
uint chan;
Part *part;
cmd = data[1];
chan = data[0];
p = &_hook._part_volume[chan];
/* is this the hook i'm waiting for? */
if (cmd && *p != cmd)
return;
/* reset hook? */
if (cmd != 0 && cmd < 0x80)
*p = 0;
part = get_part(chan);
if (part)
part->set_vol(data[2]);
}
void Player::maybe_set_program(byte *data)
{
byte cmd;
byte *p;
uint chan;
Part *part;
cmd = data[1];
chan = data[0];
/* is this the hook i'm waiting for? */
p = &_hook._part_program[chan];
if (cmd && *p != cmd)
return;
if (cmd != 0 && cmd < 0x80)
*p = 0;
part = get_part(chan);
if (part)
part->set_program(data[2]);
}
void Player::maybe_set_transpose_part(byte *data)
{
byte cmd;
byte *p;
uint chan;
cmd = data[1];
chan = data[0];
/* is this the hook i'm waiting for? */
p = &_hook._part_transpose[chan];
if (cmd && *p != cmd)
return;
/* reset hook? */
if (cmd != 0 && cmd < 0x80)
*p = 0;
part_set_transpose(chan, data[2], (int8)data[3]);
}
int Player::set_transpose(byte relative, int b)
{
Part *part;
if (b > 24 || b < -24 || relative > 1)
return -1;
if (relative)
b = transpose_clamp(_transpose + b, -7, 7);
_transpose = b;
for (part = _parts; part; part = part->_next) {
part->set_transpose(part->_transpose);
}
return 0;
}
void Player::clear_active_notes()
{
memset(_se->_active_notes, 0, sizeof(_se->_active_notes));
}
void Player::clear_active_note(int chan, byte note)
{
_se->_active_notes[note] &= ~(1 << chan);
}
void Player::set_active_note(int chan, byte note)
{
_se->_active_notes[note] |= (1 << chan);
}
void Player::part_set_transpose(uint8 chan, byte relative, int8 b)
{
Part *part;
if (b > 24 || b < -24)
return;
part = get_part(chan);
if (!part)
return;
if (relative)
b = transpose_clamp(b + part->_transpose, -7, 7);
part->set_transpose(b);
}
void Player::key_on(uint8 chan, uint8 note, uint8 velocity)
{
Part *part;
part = get_part(chan);
if (!part || !part->_on)
return;
part->key_on(note, velocity);
}
void Player::key_off(uint8 chan, uint8 note)
{
Part *part;
for (part = _parts; part; part = part->_next) {
if (part->_chan == (byte)chan && part->_on)
part->key_off(note);
}
}
bool Player::jump(uint track, uint beat, uint tick)
{
byte *mtrk, *cur_mtrk, *scanpos;
uint32 topos, curpos, track_offs;
if (!_active)
return false;
mtrk = _se->findTag(_song_index, "MTrk", track);
if (!mtrk)
return false;
cur_mtrk = _se->findTag(_song_index, "MTrk", _track_index);
if (!cur_mtrk)
return false;
_se->lock();
if (beat == 0)
beat = 1;
topos = (beat - 1) * _ticks_per_beat + tick;
if (track == _track_index && topos >= _cur_pos) {
scanpos = _song_offset + mtrk;
curpos = _next_pos;
} else {
scanpos = mtrk;
curpos = get_delta_time(&scanpos);
}
while (curpos < topos) {
skip_midi_cmd(&scanpos);
if (!scanpos) {
_se->unlock();
return false;
}
curpos += get_delta_time(&scanpos);
}
track_offs = scanpos - mtrk;
turn_off_pedals();
find_sustaining_notes(cur_mtrk + _song_offset, mtrk + track_offs, curpos - topos);
_beat_index = beat;
_tick_index = tick;
_cur_pos = topos;
_next_pos = curpos;
_timer_counter = 0;
_song_offset = track_offs;
if (track != _track_index) {
_track_index = track;
_loop_counter = 0;
}
_abort = true;
_se->unlock();
return true;
}
bool Player::set_loop(uint count, uint tobeat, uint totick, uint frombeat, uint fromtick)
{
if (tobeat + 1 >= frombeat)
return false;
if (tobeat == 0)
tobeat = 1;
_loop_counter = 0; /* because of possible interrupts */
_loop_to_beat = tobeat;
_loop_to_tick = totick;
_loop_from_beat = frombeat;
_loop_from_tick = fromtick;
_loop_counter = count;
return true;
}
void Player::clear_loop()
{
_loop_counter = 0;
}
void Player::turn_off_pedals()
{
Part *part;
for (part = _parts; part; part = part->_next) {
if (part->_pedal)
part->set_pedal(false);
}
}
void Player::find_sustaining_notes(byte *a, byte *b, uint32 l)
{
uint32 pos;
uint16 mask;
uint16 *bitlist_ptr;
SustainingNotes *sn, *next;
IsNoteCmdData isnote;
int j;
uint num_active;
uint max_off_pos;
num_active = update_actives();
/* pos contains number of ticks since current position */
pos = _next_pos - _cur_pos;
if ((int32)pos < 0)
pos = 0;
/* locate the positions where the notes are turned off.
* remember each note that was turned off
*/
while (num_active != 0) {
/* is note off? */
j = is_note_cmd(&a, &isnote);
if (j == -1)
break;
if (j == 1) {
mask = 1 << isnote.chan;
bitlist_ptr = _se->_active_notes + isnote.note;
if (*bitlist_ptr & mask) {
*bitlist_ptr &= ~mask;
num_active--;
/* Get a node from the free list */
if ((sn = _se->_sustain_notes_free) == NULL)
return;
_se->_sustain_notes_free = sn->next;
/* Insert it in the beginning of the used list */
sn->next = _se->_sustain_notes_used;
_se->_sustain_notes_used = sn;
sn->prev = NULL;
if (sn->next)
sn->next->prev = sn;
sn->note = isnote.note;
sn->chan = isnote.chan;
sn->player = this;
sn->off_pos = pos;
sn->pos = 0;
sn->counter = 0;
}
}
pos += get_delta_time(&a);
}
/* find the maximum position where a note was turned off */
max_off_pos = 0;
for (sn = _se->_sustain_notes_used; sn; sn = sn->next) {
_se->_active_notes[sn->note] |= (1 << sn->chan);
if (sn->off_pos > max_off_pos) {
max_off_pos = sn->off_pos;
}
}
/* locate positions where notes are turned on */
pos = l;
while (pos < max_off_pos) {
j = is_note_cmd(&b, &isnote);
if (j == -1)
break;
if (j == 2) {
mask = 1 << isnote.chan;
bitlist_ptr = _se->_active_notes + isnote.note;
if (*bitlist_ptr & mask) {
sn = _se->_sustain_notes_used;
while (sn) {
next = sn->next;
if (sn->note == isnote.note && sn->chan == isnote.chan && pos < sn->off_pos) {
*bitlist_ptr &= ~mask;
/* Unlink from the sustain list */
if (next)
next->prev = sn->prev;
if (sn->prev)
sn->prev->next = next;
else
_se->_sustain_notes_used = next;
/* Insert into the free list */
sn->next = _se->_sustain_notes_free;
_se->_sustain_notes_free = sn;
}
sn = next;
}
}
}
pos += get_delta_time(&b);
}
/* Concatenate head and used list */
if (!_se->_sustain_notes_head) {
_se->_sustain_notes_head = _se->_sustain_notes_used;
_se->_sustain_notes_used = NULL;
return;
}
sn = _se->_sustain_notes_head;
while (sn->next)
sn = sn->next;
sn->next = _se->_sustain_notes_used;
_se->_sustain_notes_used = NULL;
if (sn->next)
sn->next->prev = sn;
}
Part *Player::get_part(uint8 chan)
{
Part *part;
part = _parts;
while (part) {
if (part->_chan == chan)
return part;
part = part->_next;
}
part = _se->allocate_part(_priority);
if (!part) {
warning("no parts available");
return NULL;
}
part->_chan = chan;
part->setup(this);
return part;
}
uint Player::update_actives()
{
Part *part;
uint16 *active;
int count = 0;
clear_active_notes();
active = _se->_active_notes;
for (part = _parts; part; part = part->_next) {
if (part->_mc)
count += part->update_actives(active);
}
return count;
}
void Player::set_priority(int pri)
{
Part *part;
_priority = pri;
for (part = _parts; part; part = part->_next) {
part->set_pri(part->_pri);
}
_se->_driver->update_pris();
}
void Player::set_pan(int pan)
{
Part *part;
_pan = pan;
for (part = _parts; part; part = part->_next) {
part->set_pan(part->_pan);
}
}
void Player::set_detune(int detune)
{
Part *part;
_detune = detune;
for (part = _parts; part; part = part->_next) {
part->set_detune(part->_detune);
}
}
int Player::scan(uint totrack, uint tobeat, uint totick)
{
byte *mtrk, *scanptr;
uint32 curpos, topos;
uint32 pos;
assert(totrack >= 0 && tobeat >= 0 && totick >= 0);
if (!_active)
return -1;
mtrk = _se->findTag(_song_index, "MTrk", totrack);
if (!mtrk)
return -1;
_se->lock();
if (tobeat == 0)
tobeat++;
turn_off_parts();
clear_active_notes();
scanptr = mtrk;
curpos = get_delta_time(&scanptr);
_scanning = true;
topos = (tobeat - 1) * _ticks_per_beat + totick;
while (curpos < topos) {
scanptr = parse_midi(scanptr);
if (!scanptr) {
_scanning = false;
_se->unlock();
return -1;
}
curpos += get_delta_time(&scanptr);
}
pos = scanptr - mtrk;
_scanning = false;
_se->driver()->update_pris();
play_active_notes();
_beat_index = tobeat;
_tick_index = totick;
_cur_pos = topos;
_next_pos = curpos;
_timer_counter = 0;
_song_offset = pos;
if (_track_index != totrack) {
_track_index = totrack;
_loop_counter = 0;
}
_se->unlock();
return 0;
}
void Player::turn_off_parts()
{
Part *part;
for (part = _parts; part; part = part->_next)
part->off();
}
void Player::play_active_notes()
{
int i, j;
uint mask;
for (i = 0; i != 128; i++) {
mask = _se->_active_notes[i];
for (j = 0; j != 16; j++, mask >>= 1) {
if (mask & 1) {
key_on(j, i, 80);
}
}
}
}
int Player::set_vol(byte vol)
{
Part *part;
if (vol > 127)
return -1;
_volume = vol;
_vol_eff = _se->get_channel_volume(_vol_chan) * (vol + 1) >> 7;
for (part = _parts; part; part = part->_next) {
part->set_vol(part->_vol);
}
return 0;
}
int Player::get_param(int param, byte chan)
{
switch (param) {
case 0:
return (byte)_priority;
case 1:
return (byte)_volume;
case 2:
return (byte)_pan;
case 3:
return (byte)_transpose;
case 4:
return (byte)_detune;
case 5:
return _speed;
case 6:
return _track_index;
case 7:
return _beat_index;
case 8:
return _tick_index;
case 9:
return _loop_counter;
case 10:
return _loop_to_beat;
case 11:
return _loop_to_tick;
case 12:
return _loop_from_beat;
case 13:
return _loop_from_tick;
case 14:
case 15:
case 16:
case 17:
return query_part_param(param, chan);
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
return _hook.query_param(param, chan);
default:
return -1;
}
}
int Player::query_part_param(int param, byte chan)
{
Part *part;
part = _parts;
while (part) {
if (part->_chan == chan) {
switch (param) {
case 14:
return part->_on;
case 15:
return part->_vol;
case 16:
return part->_program;
case 17:
return part->_transpose;
default:
return -1;
}
}
part = part->_next;
}
return 129;
}
void Player::sequencer_timer()
{
byte *mtrk;
uint32 counter;
byte *song_ptr;
counter = _timer_counter + _timer_speed;
_timer_counter = counter & 0xFFFF;
_cur_pos += counter >> 16;
_tick_index += counter >> 16;
if (_tick_index >= _ticks_per_beat) {
_beat_index += _tick_index / _ticks_per_beat;
_tick_index %= _ticks_per_beat;
}
if (_loop_counter && _beat_index >= _loop_from_beat && _tick_index >= _loop_from_tick) {
_loop_counter--;
jump(_track_index, _loop_to_beat, _loop_to_tick);
}
if (_next_pos <= _cur_pos) {
mtrk = _se->findTag(_song_index, "MTrk", _track_index);
if (!mtrk) {
warning("Sound %d was unloaded while active", _song_index);
clear();
} else {
song_ptr = mtrk + _song_offset;
_abort = false;
while (_next_pos <= _cur_pos) {
song_ptr = parse_midi(song_ptr);
if (!song_ptr || _abort)
return;
_next_pos += get_delta_time(&song_ptr);
}
_song_offset = song_ptr - mtrk;
}
}
}
/*******************************************************************/
enum {
TYPE_PART = 1,
TYPE_PLAYER = 2,
};
int IMuseInternal::saveReference(IMuseInternal *me, byte type, void *ref)
{
switch (type) {
case TYPE_PART:
return (Part *)ref - me->_parts;
case TYPE_PLAYER:
return (Player *)ref - me->_players;
default:
error("saveReference: invalid type");
}
}
void *IMuseInternal::loadReference(IMuseInternal *me, byte type, int ref)
{
switch (type) {
case TYPE_PART:
return &me->_parts[ref];
case TYPE_PLAYER:
return &me->_players[ref];
default:
error("loadReference: invalid type");
}
}
int IMuseInternal::save_or_load(Serializer *ser, Scumm *scumm)
{
const SaveLoadEntry mainEntries[] = {
MKLINE(IMuseInternal, _queue_end, sleUint8),
MKLINE(IMuseInternal, _queue_pos, sleUint8),
MKLINE(IMuseInternal, _queue_sound, sleUint16),
MKLINE(IMuseInternal, _queue_adding, sleByte),
MKLINE(IMuseInternal, _queue_marker, sleByte),
MKLINE(IMuseInternal, _queue_cleared, sleByte),
MKLINE(IMuseInternal, _master_volume, sleByte),
MKLINE(IMuseInternal, _trigger_count, sleUint16),
MKARRAY(IMuseInternal, _channel_volume[0], sleUint16, 8),
MKARRAY(IMuseInternal, _volchan_table[0], sleUint16, 8),
MKEND()
};
const SaveLoadEntry playerEntries[] = {
MKREF(Player, _parts, TYPE_PART),
MKLINE(Player, _active, sleByte),
MKLINE(Player, _id, sleUint16),
MKLINE(Player, _priority, sleByte),
MKLINE(Player, _volume, sleByte),
MKLINE(Player, _pan, sleInt8),
MKLINE(Player, _transpose, sleByte),
MKLINE(Player, _detune, sleInt8),
MKLINE(Player, _vol_chan, sleUint16),
MKLINE(Player, _vol_eff, sleByte),
MKLINE(Player, _speed, sleByte),
MKLINE(Player, _song_index, sleUint16),
MKLINE(Player, _track_index, sleUint16),
MKLINE(Player, _timer_counter, sleUint16),
MKLINE(Player, _loop_to_beat, sleUint16),
MKLINE(Player, _loop_from_beat, sleUint16),
MKLINE(Player, _loop_counter, sleUint16),
MKLINE(Player, _loop_to_tick, sleUint16),
MKLINE(Player, _loop_from_tick, sleUint16),
MKLINE(Player, _tempo, sleUint32),
MKLINE(Player, _cur_pos, sleUint32),
MKLINE(Player, _next_pos, sleUint32),
MKLINE(Player, _song_offset, sleUint32),
MKLINE(Player, _tick_index, sleUint16),
MKLINE(Player, _beat_index, sleUint16),
MKLINE(Player, _ticks_per_beat, sleUint16),
MKLINE(Player, _hook._jump, sleByte),
MKLINE(Player, _hook._transpose, sleByte),
MKARRAY(Player, _hook._part_onoff[0], sleByte, 16),
MKARRAY(Player, _hook._part_volume[0], sleByte, 16),
MKARRAY(Player, _hook._part_program[0], sleByte, 16),
MKARRAY(Player, _hook._part_transpose[0], sleByte, 16),
MKEND()
};
const SaveLoadEntry volumeFaderEntries[] = {
MKREF(VolumeFader, player, TYPE_PLAYER),
MKLINE(VolumeFader, active, sleUint8),
MKLINE(VolumeFader, curvol, sleUint8),
MKLINE(VolumeFader, speed_lo_max, sleUint16),
MKLINE(VolumeFader, num_steps, sleUint16),
MKLINE(VolumeFader, speed_hi, sleInt8),
MKLINE(VolumeFader, direction, sleInt8),
MKLINE(VolumeFader, speed_lo, sleInt8),
MKLINE(VolumeFader, speed_lo_counter, sleUint16),
MKEND()
};
const SaveLoadEntry partEntries[] = {
MKREF(Part, _next, TYPE_PART),
MKREF(Part, _prev, TYPE_PART),
MKREF(Part, _player, TYPE_PLAYER),
MKLINE(Part, _pitchbend, sleInt16),
MKLINE(Part, _pitchbend_factor, sleUint8),
MKLINE(Part, _transpose, sleInt8),
MKLINE(Part, _vol, sleUint8),
MKLINE(Part, _detune, sleInt8),
MKLINE(Part, _pan, sleInt8),
MKLINE(Part, _on, sleUint8),
MKLINE(Part, _modwheel, sleUint8),
MKLINE(Part, _pedal, sleUint8),
MKLINE(Part, _program, sleUint8),
MKLINE(Part, _pri, sleUint8),
MKLINE(Part, _chan, sleUint8),
MKLINE(Part, _effect_level, sleUint8),
MKLINE(Part, _chorus, sleUint8),
MKLINE(Part, _percussion, sleUint8),
MKLINE(Part, _bank, sleUint8),
MKEND()
};
if (!ser->isSaving()) {
stop_all_sounds();
}
#ifdef _WIN32_WCE // Don't break savegames made with andys' build
if (!ser->isSaving() && ser->checkEOFLoadStream())
return 0;
#endif
ser->_ref_me = this;
ser->_saveload_ref = ser->isSaving()? ((void *)&saveReference) : ((void *)&loadReference);
ser->saveLoadEntries(this, mainEntries);
ser->saveLoadArrayOf(_players, ARRAYSIZE(_players), sizeof(_players[0]), playerEntries);
ser->saveLoadArrayOf(_parts, ARRAYSIZE(_parts), sizeof(_parts[0]), partEntries);
ser->saveLoadArrayOf(_volume_fader, ARRAYSIZE(_volume_fader),
sizeof(_volume_fader[0]), volumeFaderEntries);
if (!ser->isSaving()) {
/* Load all sounds that we need */
fix_players_after_load(scumm);
init_sustaining_notes();
_active_volume_faders = true;
fix_parts_after_load();
_driver->update_pris();
}
return 0;
}
#undef MKLINE
#undef MKEND
void IMuseInternal::fix_parts_after_load()
{
Part *part;
int i;
for (i = ARRAYSIZE(_parts), part = _parts; i != 0; i--, part++) {
if (part->_player)
part->fix_after_load();
}
}
/* Only call this routine from the main thread,
* since it uses getResourceAddress */
void IMuseInternal::fix_players_after_load(Scumm *scumm)
{
Player *player = _players;
int i;
for (i = ARRAYSIZE(_players); i != 0; i--, player++) {
if (player->_active) {
player->set_tempo(player->_tempo);
scumm->getResourceAddress(rtSound, player->_id);
player->_mt32emulate = isMT32(player->_id);
player->_isGM = isGM(player->_id);
if (scumm->_use_adlib) {
// FIXME - This should make sure the right
// instruments are loaded, but it does not
// even try to move to the right position in
// the track. Using scan() gives a marginally
// better result, but not good enough.
//
// The correct fix is probably to store the
// Adlib instruments, or information on where
// to find them, in the savegame.
player->jump(player->_track_index, 0, 0);
}
}
}
}
void Part::set_detune(int8 detune)
{
_detune_eff = clamp((_detune = detune) + _player->_detune, -128, 127);
changed(IMuseDriver::pcMod);
}
void Part::set_pitchbend(int value)
{
_pitchbend = value;
changed(IMuseDriver::pcMod);
}
void Part::set_vol(uint8 vol)
{
_vol_eff = ((_vol = vol) + 1) * _player->_vol_eff >> 7;
changed(IMuseDriver::pcVolume);
}
void Part::set_pri(int8 pri)
{
_pri_eff = clamp((_pri = pri) + _player->_priority, 0, 255);
}
void Part::set_pan(int8 pan)
{
_pan_eff = clamp((_pan = pan) + _player->_pan, -64, 63);
changed(IMuseDriver::pcPan);
}
void Part::set_transpose(int8 transpose)
{
_transpose_eff = transpose_clamp((_transpose = transpose) + _player->_transpose, -12, 12);
changed(IMuseDriver::pcMod);
}
void Part::set_pedal(bool value)
{
_pedal = value;
changed(IMuseDriver::pcPedal);
}
void Part::set_modwheel(uint value)
{
_modwheel = value;
changed(IMuseDriver::pcModwheel);
}
void Part::set_chorus(uint chorus)
{
_chorus = chorus;
changed(IMuseDriver::pcChorus);
}
void Part::set_effect_level(uint level)
{
_effect_level = level;
changed(IMuseDriver::pcEffectLevel);
}
void Part::fix_after_load()
{
set_transpose(_transpose);
set_vol(_vol);
set_detune(_detune);
set_pri(_pri);
set_pan(_pan);
}
void Part::set_pitchbend_factor(uint8 value)
{
if (value > 12)
return;
set_pitchbend(0);
_pitchbend_factor = value;
changed (IMuseDriver::pcPitchBendFactor);
}
void Part::set_onoff(bool on)
{
if (_on != on) {
_on = on;
if (!on)
off();
if (!_percussion)
update_pris();
}
}
void Part::set_instrument(Instrument * data)
{
_drv->part_set_instrument(this, data);
}
void Part::key_on(byte note, byte velocity)
{
_drv->part_key_on(this, note, velocity);
}
void Part::key_off(byte note)
{
_drv->part_key_off(this, note);
}
void Part::init(IMuseDriver * driver)
{
_drv = driver;
_player = NULL;
_next = NULL;
_prev = NULL;
_mc = NULL;
}
void Part::setup(Player *player)
{
_player = player;
/* Insert first into player's list */
_prev = NULL;
_next = player->_parts;
if (player->_parts)
player->_parts->_prev = this;
player->_parts = this;
_percussion = (player->_isGM && _chan == 9); // true;
_on = true;
_pri_eff = player->_priority;
_pri = 0;
_vol = 127;
_vol_eff = player->_vol_eff;
_pan = clamp(player->_pan, -64, 63);
_transpose_eff = player->_transpose;
_transpose = 0;
_detune = 0;
_detune_eff = player->_detune;
_pitchbend_factor = 2;
_pitchbend = 0;
_effect_level = 64;
_program = player->_se->get_channel_program (_chan);
_chorus = 0;
_modwheel = 0;
_bank = 0;
_pedal = false;
_mc = NULL;
if (_program < 128)
changed (IMuseDriver::pcAll);
}
void Part::uninit()
{
if (!_player)
return;
off();
/* unlink */
if (_next)
_next->_prev = _prev;
if (_prev)
_prev->_next = _next;
else
_player->_parts = _next;
_player = NULL;
_next = NULL;
_prev = NULL;
update_pris(); // In case another player couldn't allocate all its parts
}
void Part::off()
{
_drv->part_off(this);
}
void Part::changed(uint16 what)
{
_drv->part_changed(this, what);
}
void Part::set_param(byte param, int value)
{
_drv->part_set_param(this, param, value);
}
void Part::update_pris()
{
_drv->update_pris();
}
int Part::update_actives(uint16 *active)
{
return _drv->part_update_active(this, active);
}
void Part::set_program(byte program)
{
if (_program != program || _bank != 0) {
_program = program;
_bank = 0;
changed(IMuseDriver::pcProgram);
}
}
void Part::set_instrument(uint b)
{
_bank = (byte)(b >> 8);
_program = (byte)b;
changed(IMuseDriver::pcProgram);
}
//********************************************
//***** ADLIB PART OF IMUSE STARTS HERE ******
//********************************************
static byte lookup_table[64][32];
const byte volume_table[] = {
0, 4, 7, 11,
13, 16, 18, 20,
22, 24, 26, 27,
29, 30, 31, 33,
34, 35, 36, 37,
38, 39, 40, 41,
42, 43, 44, 44,
45, 46, 47, 47,
48, 49, 49, 50,
51, 51, 52, 53,
53, 54, 54, 55,
55, 56, 56, 57,
57, 58, 58, 59,
59, 60, 60, 60,
61, 61, 62, 62,
62, 63, 63, 63
};
int lookup_volume(int a, int b)
{
if (b == 0)
return 0;
if (b == 31)
return a;
if (a < -63 || a > 63) {
return b * (a + 1) >> 5;
}
if (b < 0) {
if (a < 0) {
return lookup_table[-a][-b];
} else {
return -lookup_table[a][-b];
}
} else {
if (a < 0) {
return -lookup_table[-a][b];
} else {
return lookup_table[a][b];
}
}
}
void create_lookup_table()
{
int i, j;
int sum;
for (i = 0; i < 64; i++) {
sum = i;
for (j = 0; j < 32; j++) {
lookup_table[i][j] = sum >> 5;
sum += i;
}
}
for (i = 0; i < 64; i++)
lookup_table[i][0] = 0;
}
MidiChannelAdl *IMuseAdlib::allocate_midichan(byte pri)
{
MidiChannelAdl *ac, *best = NULL;
int i;
for (i = 0; i < 9; i++) {
if (++_midichan_index >= 9)
_midichan_index = 0;
ac = &_midi_channels[_midichan_index];
if (!ac->_part)
return ac;
if (!ac->_next) {
if (ac->_part->_pri_eff <= pri) {
pri = ac->_part->_pri_eff;
best = ac;
}
}
}
if (best)
mc_off(best);
else; //debug(1, "Denying adlib channel request");
return best;
}
void IMuseAdlib::premix_proc(void *param, int16 *buf, uint len)
{
((IMuseAdlib *) param)->generate_samples(buf, len);
}
void IMuseAdlib::init(IMuseInternal *eng, OSystem *syst)
{
int i;
MidiChannelAdl *mc;
_se = eng;
for (i = 0, mc = _midi_channels; i != ARRAYSIZE(_midi_channels); i++, mc++) {
mc->_channel = i;
mc->_s11a.s10 = &mc->_s10b;
mc->_s11b.s10 = &mc->_s10a;
}
_adlib_reg_cache = (byte *)calloc(256, 1);
_opl = OPLCreate(OPL_TYPE_YM3812, 3579545, syst->property(OSystem::PROP_GET_SAMPLE_RATE, 0));
adlib_write(1, 0x20);
adlib_write(8, 0x40);
adlib_write(0xBD, 0x00);
create_lookup_table();
_mixer->setupPremix(this, premix_proc);
}
void IMuseAdlib::adlib_write(byte port, byte value)
{
if (_adlib_reg_cache[port] == value)
return;
_adlib_reg_cache[port] = value;
OPLWriteReg(_opl, port, value);
}
void IMuseAdlib::adlib_key_off(int chan)
{
byte port = chan + 0xB0;
adlib_write(port, adlib_read(port) & ~0x20);
}
struct AdlibSetParams {
byte a, b, c, d;
};
static const byte channel_mappings[9] = {
0, 1, 2, 8,
9, 10, 16, 17,
18
};
static const byte channel_mappings_2[9] = {
3, 4, 5, 11,
12, 13, 19, 20,
21
};
static const AdlibSetParams adlib_setparam_table[] = {
{0x40, 0, 63, 63}, /* level */
{0xE0, 2, 0, 0}, /* unused */
{0x40, 6, 192, 0}, /* level key scaling */
{0x20, 0, 15, 0}, /* modulator frequency multiple */
{0x60, 4, 240, 15}, /* attack rate */
{0x60, 0, 15, 15}, /* decay rate */
{0x80, 4, 240, 15}, /* sustain level */
{0x80, 0, 15, 15}, /* release rate */
{0xE0, 0, 3, 0}, /* waveform select */
{0x20, 7, 128, 0}, /* amp mod */
{0x20, 6, 64, 0}, /* vib */
{0x20, 5, 32, 0}, /* eg typ */
{0x20, 4, 16, 0}, /* ksr */
{0xC0, 0, 1, 0}, /* decay alg */
{0xC0, 1, 14, 0} /* feedback */
};
void IMuseAdlib::adlib_set_param(int channel, byte param, int value)
{
const AdlibSetParams *as;
byte port;
assert(channel >= 0 && channel < 9);
if (param <= 12) {
port = channel_mappings_2[channel];
} else if (param <= 25) {
param -= 13;
port = channel_mappings[channel];
} else if (param <= 27) {
param -= 13;
port = channel;
} else if (param == 28 || param == 29) {
if (param == 28)
value -= 15;
else
value -= 383;
value <<= 4;
channel_table_2[channel] = value;
adlib_playnote(channel, curnote_table[channel] + value);
return;
} else {
return;
}
as = &adlib_setparam_table[param];
if (as->d)
value = as->d - value;
port += as->a;
adlib_write(port, (adlib_read(port) & ~as->c) | (((byte)value) << as->b));
}
static const byte octave_numbers[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7
};
static const byte note_numbers[] = {
3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
3, 4, 5, 6, 7, 8, 9, 10
};
static const byte note_to_f_num[] = {
90, 91, 92, 92, 93, 94, 94, 95,
96, 96, 97, 98, 98, 99, 100, 101,
101, 102, 103, 104, 104, 105, 106, 107,
107, 108, 109, 110, 111, 111, 112, 113,
114, 115, 115, 116, 117, 118, 119, 120,
121, 121, 122, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142,
143, 145, 146, 147, 148, 149, 150, 151,
152, 153, 154, 155, 157, 158, 159, 160,
161, 162, 163, 165, 166, 167, 168, 169,
171, 172, 173, 174, 176, 177, 178, 180,
181, 182, 184, 185, 186, 188, 189, 190,
192, 193, 194, 196, 197, 199, 200, 202,
203, 205, 206, 208, 209, 211, 212, 214,
215, 217, 218, 220, 222, 223, 225, 226,
228, 230, 231, 233, 235, 236, 238, 240,
242, 243, 245, 247, 249, 251, 252, 254,
};
void IMuseAdlib::adlib_playnote(int channel, int note)
{
byte old, oct, notex;
int note2;
int i;
note2 = (note >> 7) - 4;
oct = octave_numbers[note2] << 2;
notex = note_numbers[note2];
old = adlib_read(channel + 0xB0);
if (old & 0x20) {
old &= ~0x20;
if (oct > old) {
if (notex < 6) {
notex += 12;
oct -= 4;
}
} else if (oct < old) {
if (notex > 11) {
notex -= 12;
oct += 4;
}
}
}
i = (notex << 3) + ((note >> 4) & 0x7);
adlib_write(channel + 0xA0, note_to_f_num[i]);
adlib_write(channel + 0xB0, oct | 0x20);
}
void IMuseAdlib::adlib_note_on(int chan, byte note, int mod)
{
int code;
assert(chan >= 0 && chan < 9);
code = (note << 7) + mod;
curnote_table[chan] = code;
adlib_playnote(chan, channel_table_2[chan] + code);
}
void IMuseAdlib::adlib_note_on_ex(int chan, byte note, int mod)
{
int code;
assert(chan >= 0 && chan < 9);
code = (note << 7) + mod;
curnote_table[chan] = code;
channel_table_2[chan] = 0;
adlib_playnote(chan, code);
}
void IMuseAdlib::adlib_key_onoff(int channel)
{
byte val;
byte port = channel + 0xB0;
assert(channel >= 0 && channel < 9);
val = adlib_read(port);
adlib_write(port, val & ~0x20);
adlib_write(port, val | 0x20);
}
void IMuseAdlib::adlib_setup_channel(int chan, Instrument * instr, byte vol_1, byte vol_2)
{
byte port;
assert(chan >= 0 && chan < 9);
port = channel_mappings[chan];
adlib_write(port + 0x20, instr->flags_1);
if (!(g_scumm->_features & GF_SMALL_HEADER) || (instr->feedback & 1))
adlib_write(port + 0x40, (instr->oplvl_1 | 0x3F) - vol_1 );
else
adlib_write(port + 0x40, instr->oplvl_1);
adlib_write(port + 0x60, 0xff & (~instr->atdec_1));
adlib_write(port + 0x80, 0xff & (~instr->sustrel_1));
adlib_write(port + 0xE0, instr->waveform_1);
port = channel_mappings_2[chan];
adlib_write(port + 0x20, instr->flags_2);
adlib_write(port + 0x40, (instr->oplvl_2 | 0x3F) - vol_2 );
adlib_write(port + 0x60, 0xff & (~instr->atdec_2));
adlib_write(port + 0x80, 0xff & (~instr->sustrel_2));
adlib_write(port + 0xE0, instr->waveform_2);
adlib_write((byte)chan + 0xC0, instr->feedback);
}
int IMuseAdlib::adlib_read_param(int chan, byte param)
{
const AdlibSetParams *as;
byte val;
byte port;
assert(chan >= 0 && chan < 9);
if (param <= 12) {
port = channel_mappings_2[chan];
} else if (param <= 25) {
param -= 13;
port = channel_mappings[chan];
} else if (param <= 27) {
param -= 13;
port = chan;
} else if (param == 28) {
return 0xF;
} else if (param == 29) {
return 0x17F;
} else {
return 0;
}
as = &adlib_setparam_table[param];
val = adlib_read(port + as->a);
val &= as->c;
val >>= as->b;
if (as->d)
val = as->d - val;
return val;
}
void IMuseAdlib::generate_samples(int16 *data, int len)
{
int step;
if (!_opl) {
memset(data, 0, len * sizeof(int16));
return;
}
do {
step = len;
if (step > _next_tick)
step = _next_tick;
YM3812UpdateOne(_opl, data, step);
if (!(_next_tick -= step)) {
_se->on_timer();
reset_tick();
}
data += step;
} while (len -= step);
}
void IMuseAdlib::reset_tick()
{
_next_tick = 88;
}
void IMuseAdlib::on_timer()
{
MidiChannelAdl *mc;
int i;
_adlib_timer_counter += 0xD69;
while (_adlib_timer_counter >= 0x411B) {
_adlib_timer_counter -= 0x411B;
mc = _midi_channels;
for (i = 0; i != ARRAYSIZE(_midi_channels); i++, mc++) {
if (!mc->_part)
continue;
if (mc->_duration && (mc->_duration -= 0x11) <= 0) {
mc_off(mc);
return;
}
if (mc->_s10a.active) {
mc_inc_stuff(mc, &mc->_s10a, &mc->_s11a);
}
if (mc->_s10b.active) {
mc_inc_stuff(mc, &mc->_s10b, &mc->_s11b);
}
}
}
}
const byte param_table_1[16] = {
29, 28, 27, 0,
3, 4, 7, 8,
13, 16, 17, 20,
21, 30, 31, 0
};
const uint16 param_table_2[16] = {
0x2FF, 0x1F, 0x7, 0x3F,
0x0F, 0x0F, 0x0F, 0x3,
0x3F, 0x0F, 0x0F, 0x0F,
0x3, 0x3E, 0x1F, 0
};
static const uint16 num_steps_table[] = {
1, 2, 4, 5,
6, 7, 8, 9,
10, 12, 14, 16,
18, 21, 24, 30,
36, 50, 64, 82,
100, 136, 160, 192,
240, 276, 340, 460,
600, 860, 1200, 1600
};
int IMuseAdlib::random_nr(int a)
{
static byte _rand_seed = 1;
if (_rand_seed & 1) {
_rand_seed >>= 1;
_rand_seed ^= 0xB8;
} else {
_rand_seed >>= 1;
}
return _rand_seed * a >> 8;
}
void IMuseAdlib::struct10_setup(Struct10 * s10)
{
int b, c, d, e, f, g, h;
byte t;
b = s10->unk3;
f = s10->active - 1;
t = s10->table_a[f];
e = num_steps_table[lookup_table[t & 0x7F][b]];
if (t & 0x80) {
e = random_nr(e);
}
if (e == 0)
e++;
s10->num_steps = s10->speed_lo_max = e;
if (f != 2) {
c = s10->param;
g = s10->start_value;
t = s10->table_b[f];
d = lookup_volume(c, (t & 0x7F) - 31);
if (t & 0x80) {
d = random_nr(d);
}
if (d + g > c) {
h = c - g;
} else {
h = d;
if (d + g < 0)
h = -g;
}
h -= s10->cur_val;
} else {
h = 0;
}
s10->speed_hi = h / e;
if (h < 0) {
h = -h;
s10->direction = -1;
} else {
s10->direction = 1;
}
s10->speed_lo = h % e;
s10->speed_lo_counter = 0;
}
byte IMuseAdlib::struct10_ontimer(Struct10 * s10, Struct11 * s11)
{
byte result = 0;
int i;
if (s10->count && (s10->count -= 17) <= 0) {
s10->active = 0;
return 0;
}
i = s10->cur_val + s10->speed_hi;
s10->speed_lo_counter += s10->speed_lo;
if (s10->speed_lo_counter >= s10->speed_lo_max) {
s10->speed_lo_counter -= s10->speed_lo_max;
i += s10->direction;
}
if (s10->cur_val != i || s10->modwheel != s10->modwheel_last) {
s10->cur_val = i;
s10->modwheel_last = s10->modwheel;
i = lookup_volume(i, s10->modwheel_last);
if (i != s11->modify_val) {
s11->modify_val = i;
result = 1;
}
}
if (!--s10->num_steps) {
s10->active++;
if (s10->active > 4) {
if (s10->loop) {
s10->active = 1;
result |= 2;
struct10_setup(s10);
} else {
s10->active = 0;
}
} else {
struct10_setup(s10);
}
}
return result;
}
void IMuseAdlib::struct10_init(Struct10 * s10, InstrumentExtra * ie)
{
s10->active = 1;
s10->cur_val = 0;
s10->modwheel_last = 31;
s10->count = ie->a;
if (s10->count)
s10->count *= 63;
s10->table_a[0] = ie->b;
s10->table_a[1] = ie->d;
s10->table_a[2] = ie->f;
s10->table_a[3] = ie->g;
s10->table_b[0] = ie->c;
s10->table_b[1] = ie->e;
s10->table_b[2] = 0;
s10->table_b[3] = ie->h;
struct10_setup(s10);
}
void IMuseAdlib::mc_init_stuff(MidiChannelAdl *mc, Struct10 * s10,
Struct11 * s11, byte flags, InstrumentExtra * ie)
{
Part *part = mc->_part;
s11->modify_val = 0;
s11->flag0x40 = flags & 0x40;
s10->loop = flags & 0x20;
s11->flag0x10 = flags & 0x10;
s11->param = param_table_1[flags & 0xF];
s10->param = param_table_2[flags & 0xF];
s10->unk3 = 31;
if (s11->flag0x40) {
s10->modwheel = part->_modwheel >> 2;
} else {
s10->modwheel = 31;
}
switch (s11->param) {
case 0:
s10->start_value = mc->_vol_2;
break;
case 13:
s10->start_value = mc->_vol_1;
break;
case 30:
s10->start_value = 31;
s11->s10->modwheel = 0;
break;
case 31:
s10->start_value = 0;
s11->s10->unk3 = 0;
break;
default:
s10->start_value = ((IMuseAdlib *) part->_drv)->adlib_read_param(mc->_channel, s11->param);
}
struct10_init(s10, ie);
}
void IMuseAdlib::mc_inc_stuff(MidiChannelAdl *mc, Struct10 * s10, Struct11 * s11)
{
byte code;
Part *part = mc->_part;
code = struct10_ontimer(s10, s11);
if (code & 1) {
switch (s11->param) {
case 0:
mc->_vol_2 = s10->start_value + s11->modify_val;
((IMuseAdlib *) part->_drv)->adlib_set_param(mc->_channel, 0,
volume_table[lookup_table[mc->_vol_2]
[part->_vol_eff >> 2]]);
break;
case 13:
mc->_vol_1 = s10->start_value + s11->modify_val;
if (mc->_twochan) {
((IMuseAdlib *) part->_drv)->adlib_set_param(mc->_channel, 13,
volume_table[lookup_table[mc->_vol_1]
[part->_vol_eff >> 2]]);
} else {
((IMuseAdlib *) part->_drv)->adlib_set_param(mc->_channel, 13, mc->_vol_1);
}
break;
case 30:
s11->s10->modwheel = (char)s11->modify_val;
break;
case 31:
s11->s10->unk3 = (char)s11->modify_val;
break;
default:
((IMuseAdlib *) part->_drv)->adlib_set_param(mc->_channel, s11->param,
s10->start_value + s11->modify_val);
break;
}
}
if (code & 2 && s11->flag0x10)
((IMuseAdlib *) part->_drv)->adlib_key_onoff(mc->_channel);
}
static byte map_gm_to_fm [128][30] = {
// DERIVED FROM DAY OF THE TENTACLE
{ 0x2F, 0x0B, 0x08, 0x78, 0x16, 0x24, 0x22, 0x0B, 0x9A, 0x34, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x2F, 0x0B, 0x08, 0x78, 0x16, 0x24, 0x22, 0x0B, 0x9A, 0x34, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x84, 0x40, 0x3B, 0x5A, 0x63, 0x81, 0x00, 0x3B, 0x5A, 0x7F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x2F, 0x0B, 0x08, 0x78, 0x16, 0x24, 0x22, 0x0B, 0x9A, 0x34, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x84, 0x40, 0x3B, 0x5A, 0x63, 0x81, 0x00, 0x3B, 0x5A, 0x7F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x85, 0x80, 0x05, 0xEA, 0x3D, 0x84, 0x18, 0x3C, 0xAA, 0x7C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE2, 0x00, 0x6C, 0x77, 0x7D, 0xE4, 0x40, 0x7D, 0xA7, 0x66, 0x07, 0xA1, 0x00, 0x02, 0x1E, 0x01, 0x20, 0x01, 0x01, 0x1F, 0xAC, 0x00, 0x04, 0x24, 0x02, 0x21, 0x02, 0x02, 0x21, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC9, 0x40, 0x3A, 0x78, 0x5E, 0xC2, 0x00, 0x4D, 0x9A, 0x7C, 0x00, 0xA1, 0xA0, 0x08, 0x1F, 0x05, 0x1E, 0x02, 0x05, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x28, 0x1D, 0x19, 0x68, 0x02, 0x22, 0x35, 0x09, 0x08, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x28, 0x1D, 0x19, 0x68, 0x02, 0x22, 0x35, 0x09, 0x08, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xC6, 0x40, 0x3B, 0x78, 0x5E, 0xC2, 0x00, 0x4D, 0x9A, 0x7C, 0x00, 0xA1, 0xA0, 0x08, 0x1F, 0x05, 0x1E, 0x02, 0x05, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE2, 0x00, 0x6C, 0x77, 0x7D, 0xE4, 0x40, 0x7D, 0xA7, 0x66, 0x07, 0xA1, 0x00, 0x02, 0x1E, 0x01, 0x20, 0x01, 0x01, 0x1F, 0xAC, 0x00, 0x04, 0x24, 0x02, 0x21, 0x02, 0x02, 0x21, 0x00 },
{ 0xE2, 0x28, 0x38, 0xE8, 0x02, 0xE6, 0x33, 0x0B, 0xF9, 0x00, 0x08, 0xA1, 0x00, 0x02, 0x1E, 0x02, 0x20, 0x00, 0x02, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0xE2, 0x28, 0x38, 0xE8, 0x02, 0xE6, 0x33, 0x0B, 0xF9, 0x00, 0x08, 0xA1, 0x00, 0x02, 0x1E, 0x02, 0x20, 0x00, 0x02, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE2, 0x00, 0x6C, 0x77, 0x7D, 0xE4, 0x40, 0x7D, 0xA7, 0x66, 0x07, 0xA1, 0x00, 0x02, 0x1E, 0x01, 0x20, 0x01, 0x01, 0x1F, 0xAC, 0x00, 0x04, 0x24, 0x02, 0x21, 0x02, 0x02, 0x21, 0x00 },
{ 0x85, 0x80, 0x05, 0xEA, 0x3D, 0x84, 0x18, 0x3C, 0xAA, 0x7C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x85, 0x80, 0x05, 0xEA, 0x3D, 0x84, 0x18, 0x3C, 0xAA, 0x7C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x85, 0x80, 0x05, 0xEA, 0x3D, 0x84, 0x18, 0x3C, 0xAA, 0x7C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x84, 0x40, 0x3B, 0x5A, 0x63, 0x81, 0x00, 0x3B, 0x5A, 0x7F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x31, 0x0C, 0x2D, 0xD7, 0x40, 0x62, 0x18, 0x2E, 0xB8, 0x7C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x01, 0x1C, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00 },
{ 0x22, 0x19, 0x79, 0x67, 0x00, 0x22, 0x3F, 0x2A, 0xC6, 0x02, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x31, 0x0C, 0x2D, 0xD7, 0x40, 0x62, 0x18, 0x2E, 0xB8, 0x7C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x01, 0x1C, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00 },
{ 0x31, 0x0C, 0x2D, 0xD7, 0x40, 0x62, 0x18, 0x2E, 0xB8, 0x7C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x01, 0x1C, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x31, 0x0C, 0x2D, 0xD7, 0x40, 0x62, 0x18, 0x2E, 0xB8, 0x7C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x01, 0x1C, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE2, 0x15, 0x7B, 0xB5, 0x02, 0xE1, 0x33, 0xAF, 0xF4, 0x36, 0x08, 0xA1, 0x00, 0x04, 0x1E, 0x04, 0x1F, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xF4, 0x40, 0x9F, 0xFA, 0x61, 0xE2, 0x13, 0x7F, 0xFA, 0x7D, 0x02, 0x21, 0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x35, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x02, 0x00, 0x67, 0xAA, 0x65, 0x02, 0x64, 0x28, 0xF9, 0x7C, 0x08, 0x81, 0x00, 0x04, 0x1D, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x02, 0x40, 0x04, 0x9A, 0x55, 0xC2, 0x4B, 0x2B, 0xCB, 0x7C, 0x06, 0x41, 0x00, 0x00, 0x20, 0x06, 0x1C, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x02, 0x0C, 0x03, 0x6A, 0x7D, 0x02, 0x00, 0x23, 0xEA, 0x7C, 0x02, 0x81, 0x00, 0x02, 0x20, 0x01, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xF4, 0x40, 0x9F, 0xFA, 0x61, 0xE2, 0x13, 0x7F, 0xFA, 0x7D, 0x02, 0x21, 0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x35, 0x00 },
{ 0xF4, 0x40, 0x9F, 0xFA, 0x61, 0xE2, 0x13, 0x7F, 0xFA, 0x7D, 0x02, 0x21, 0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x35, 0x00 },
{ 0xF4, 0x40, 0x9F, 0xFA, 0x61, 0xE2, 0x13, 0x7F, 0xFA, 0x7D, 0x02, 0x21, 0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x35, 0x00 },
{ 0x02, 0x00, 0x67, 0xAA, 0x65, 0x02, 0x64, 0x28, 0xF9, 0x7C, 0x08, 0x81, 0x00, 0x04, 0x1D, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE1, 0x00, 0xCE, 0xD9, 0x4E, 0xE2, 0x00, 0x8F, 0x99, 0x65, 0x0E, 0x01, 0x00, 0x01, 0x1F, 0x00, 0x1E, 0x01, 0x01, 0x20, 0x01, 0x00, 0x00, 0x1F, 0x06, 0x1E, 0x00, 0x06, 0x1F, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE2, 0x00, 0xCE, 0xD9, 0x4C, 0xE2, 0x00, 0x8F, 0x99, 0x64, 0x0E, 0x81, 0x10, 0x00, 0x1E, 0x05, 0x1F, 0x12, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x1F, 0x06, 0x1E, 0x00, 0x06, 0x1F, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xB2, 0x25, 0xAD, 0xE9, 0x00, 0x62, 0x00, 0x8F, 0xC8, 0x7C, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xF2, 0x00, 0xAF, 0xFA, 0x5C, 0xF2, 0x56, 0x9F, 0xEA, 0x7C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xF2, 0x00, 0xAF, 0xFA, 0x5C, 0xF2, 0x56, 0x9F, 0xEA, 0x7C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xB2, 0x25, 0xAD, 0xE9, 0x00, 0x62, 0x00, 0x8F, 0xC8, 0x7C, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE2, 0x02, 0x9F, 0xB8, 0x48, 0x22, 0x89, 0x9F, 0xE8, 0x7C, 0x00, 0x81, 0x00, 0x01, 0x1E, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE4, 0x28, 0x7E, 0xF8, 0x01, 0xE2, 0x23, 0x8E, 0xE8, 0x7D, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE4, 0x28, 0x7E, 0xF8, 0x01, 0xE2, 0x23, 0x8E, 0xE8, 0x7D, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x31, 0x0C, 0x2D, 0xD7, 0x40, 0x62, 0x18, 0x2E, 0xB8, 0x7C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x01, 0x1C, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00 },
{ 0x22, 0x10, 0x7E, 0xD8, 0x35, 0x2A, 0x2E, 0x8E, 0xD8, 0x7C, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE4, 0x28, 0x7E, 0xF8, 0x01, 0xE2, 0x23, 0x8E, 0xE8, 0x7D, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x22, 0x10, 0x7E, 0xD8, 0x35, 0x2A, 0x2E, 0x8E, 0xD8, 0x7C, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE4, 0x08, 0x7E, 0x99, 0x28, 0xE6, 0x16, 0x80, 0xF8, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE4, 0x23, 0x8F, 0xF9, 0x7C, 0xE2, 0x18, 0x9F, 0x88, 0x7C, 0x01, 0x01, 0x04, 0x00, 0x1E, 0x00, 0x1F, 0x06, 0x05, 0x1F, 0x03, 0x29, 0x01, 0x1F, 0x02, 0x21, 0x01, 0x02, 0x1F, 0x00 },
{ 0xE4, 0x23, 0x8F, 0xF9, 0x7C, 0xE2, 0x18, 0x9F, 0x88, 0x7C, 0x01, 0x01, 0x04, 0x00, 0x1E, 0x00, 0x1F, 0x06, 0x05, 0x1F, 0x03, 0x29, 0x01, 0x1F, 0x02, 0x21, 0x01, 0x02, 0x1F, 0x00 },
{ 0xE4, 0x23, 0x8F, 0xF9, 0x7C, 0xE2, 0x18, 0x9F, 0x88, 0x7C, 0x01, 0x01, 0x04, 0x00, 0x1E, 0x00, 0x1F, 0x06, 0x05, 0x1F, 0x03, 0x29, 0x01, 0x1F, 0x02, 0x21, 0x01, 0x02, 0x1F, 0x00 },
{ 0x2A, 0x1E, 0x98, 0xA9, 0x00, 0x62, 0x00, 0x9F, 0xB9, 0x7C, 0x00, 0x01, 0x00, 0x01, 0x1E, 0x00, 0x1F, 0x01, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x2A, 0x1E, 0x98, 0xA9, 0x00, 0x62, 0x00, 0x9F, 0xB9, 0x7C, 0x00, 0x01, 0x00, 0x01, 0x1E, 0x00, 0x1F, 0x01, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x62, 0xA8, 0x9D, 0x84, 0x44, 0x62, 0x23, 0x7F, 0xD5, 0x4C, 0x03, 0xA1, 0x00, 0x01, 0x1E, 0x02, 0x21, 0x01, 0x01, 0x20, 0xA8, 0x00, 0x01, 0x24, 0x06, 0x20, 0x04, 0x03, 0x24, 0x00 },
{ 0xE4, 0x28, 0x7E, 0xF8, 0x01, 0xE2, 0x23, 0x8E, 0xE8, 0x7D, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE4, 0x08, 0x7E, 0x99, 0x28, 0xE6, 0x16, 0x80, 0xF8, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE4, 0x28, 0x7E, 0xF8, 0x01, 0xE2, 0x23, 0x8E, 0xE8, 0x7D, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x22, 0x10, 0x7E, 0xD8, 0x35, 0x2A, 0x2E, 0x8E, 0xD8, 0x7C, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE4, 0x07, 0x05, 0xAA, 0x7C, 0xE2, 0x50, 0xBE, 0xC8, 0x7D, 0x07, 0x01, 0x00, 0x03, 0x1E, 0x01, 0x1E, 0x00, 0x00, 0x1E, 0xA8, 0x00, 0x01, 0x20, 0x06, 0x23, 0x04, 0x03, 0x20, 0x00 },
{ 0x85, 0x80, 0x05, 0xEA, 0x3D, 0x84, 0x18, 0x3C, 0xAA, 0x7C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xB2, 0x25, 0xAD, 0xE9, 0x00, 0x62, 0x00, 0x8F, 0xC8, 0x7C, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xF2, 0x00, 0xAF, 0xFA, 0x5C, 0xF2, 0x56, 0x9F, 0xEA, 0x7C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xE2, 0x02, 0x9F, 0xB8, 0x48, 0x22, 0x89, 0x9F, 0xE8, 0x7C, 0x00, 0x81, 0x00, 0x01, 0x1E, 0x00, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xB2, 0x25, 0xAD, 0xE9, 0x00, 0x62, 0x00, 0x8F, 0xC8, 0x7C, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xE2, 0x00, 0xCE, 0xD9, 0x4C, 0xE2, 0x00, 0x8F, 0x99, 0x64, 0x0E, 0x81, 0x10, 0x00, 0x1E, 0x05, 0x1F, 0x12, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x1F, 0x06, 0x1E, 0x00, 0x06, 0x1F, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC9, 0x40, 0x3A, 0x38, 0x5E, 0xC2, 0x00, 0x4C, 0xAA, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xC9, 0x40, 0x3A, 0x78, 0x5E, 0xC2, 0x00, 0x4D, 0x9A, 0x7C, 0x00, 0xA1, 0xA0, 0x08, 0x1F, 0x05, 0x1E, 0x02, 0x05, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC9, 0x40, 0x3A, 0x38, 0x5E, 0xC2, 0x00, 0x4C, 0xAA, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0xC9, 0x40, 0x3A, 0x78, 0x5E, 0xC2, 0x00, 0x4D, 0x9A, 0x7C, 0x00, 0xA1, 0xA0, 0x08, 0x1F, 0x05, 0x1E, 0x02, 0x05, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Extrapolated
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xC6, 0x00, 0x2E, 0xC7, 0x59, 0xC2, 0x06, 0x0E, 0xA7, 0x7D, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x02, 0x0C, 0x03, 0x6A, 0x7D, 0x02, 0x00, 0x23, 0xEA, 0x7C, 0x02, 0x81, 0x00, 0x02, 0x20, 0x01, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xCF, 0x3B, 0x2A, 0xFE, 0x7E, 0xC0, 0xC0, 0x0C, 0xEB, 0x63, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 }, // Extrapolated
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x02, 0x0C, 0x03, 0x6A, 0x7D, 0x02, 0x00, 0x23, 0xEA, 0x7C, 0x02, 0x81, 0x00, 0x02, 0x20, 0x01, 0x1F, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x0F, 0x10, 0x10, 0x09, 0x49, 0x02, 0x12, 0x07, 0x9A, 0x7C, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0xCF, 0x3B, 0x2A, 0xFE, 0x7E, 0xC0, 0xC0, 0x0C, 0xEB, 0x63, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0xCF, 0x40, 0x0A, 0x30, 0x5C, 0xCF, 0x00, 0x0D, 0x80, 0x7C, 0x00, 0xA0, 0x00, 0x0F, 0x1E, 0x0F, 0x20, 0x00, 0x0B, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Unknown
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } // Unknown
};
void IMuseAdlib::part_changed(Part *part, uint16 what)
{
MidiChannelAdl *mc;
if (what & pcProgram) {
if (part->_player ? part->_player->_isGM : false)
part_set_instrument(part, (Instrument *) &map_gm_to_fm[part->_program]);
else if (part->_program < 32)
part_set_instrument(part, &_glob_instr[part->_program]);
}
if (what & pcMod) {
for (mc = part->_mc->adl(); mc; mc = mc->_next) {
adlib_note_on(mc->_channel, mc->_note + part->_transpose_eff,
(part->_pitchbend * part->_pitchbend_factor >> 6) + part->_detune_eff);
}
}
if (what & pcVolume) {
for (mc = part->_mc->adl(); mc; mc = mc->_next) {
adlib_set_param(mc->_channel, 0, volume_table[lookup_table[mc->_vol_2]
[part->_vol_eff >> 2]]);
if (mc->_twochan) {
adlib_set_param(mc->_channel, 13, volume_table[lookup_table[mc->_vol_1]
[part->_vol_eff >> 2]]);
}
}
}
if (what & pcPedal) {
if (!part->_pedal) {
for (mc = (MidiChannelAdl *)part->_mc; mc; mc = mc->_next) {
if (mc->_waitforpedal)
mc_off(mc);
}
}
}
if (what & pcModwheel) {
for (mc = (MidiChannelAdl *)part->_mc; mc; mc = mc->_next) {
if (mc->_s10a.active && mc->_s11a.flag0x40)
mc->_s10a.modwheel = part->_modwheel >> 2;
if (mc->_s10b.active && mc->_s11b.flag0x40)
mc->_s10b.modwheel = part->_modwheel >> 2;
}
}
}
void IMuseAdlib::mc_key_on(MidiChannel * mc2, byte note, byte velocity)
{
MidiChannelAdl *mc = (MidiChannelAdl *)mc2;
Part *part = mc->_part;
Instrument *instr = &_part_instr[part->_slot];
int c;
byte vol_1, vol_2;
mc->_twochan = instr->feedback & 1;
mc->_note = note;
mc->_waitforpedal = false;
mc->_duration = instr->duration;
if (mc->_duration != 0)
mc->_duration *= 63;
vol_1 = (instr->oplvl_1 & 0x3F) + lookup_table[velocity >> 1][instr->waveform_1 >> 2];
if (vol_1 > 0x3F)
vol_1 = 0x3F;
mc->_vol_1 = vol_1;
vol_2 = (instr->oplvl_2 & 0x3F) + lookup_table[velocity >> 1][instr->waveform_2 >> 2];
if (vol_2 > 0x3F)
vol_2 = 0x3F;
mc->_vol_2 = vol_2;
c = part->_vol_eff >> 2;
vol_2 = volume_table[lookup_table[vol_2][c]];
if (mc->_twochan)
vol_1 = volume_table[lookup_table[vol_1][c]];
adlib_setup_channel(mc->_channel, instr, vol_1, vol_2);
adlib_note_on_ex(mc->_channel, part->_transpose_eff + note, part->_detune_eff + (part->_pitchbend * part->_pitchbend_factor >> 6));
if (instr->flags_a & 0x80) {
mc_init_stuff(mc, &mc->_s10a, &mc->_s11a, instr->flags_a, &instr->extra_a);
} else {
mc->_s10a.active = 0;
}
if (instr->flags_b & 0x80) {
mc_init_stuff(mc, &mc->_s10b, &mc->_s11b, instr->flags_b, &instr->extra_b);
} else {
mc->_s10b.active = 0;
}
}
void IMuseAdlib::set_instrument(uint slot, byte *data)
{
if (slot < 32)
memcpy(&_glob_instr[slot], data, sizeof(Instrument));
}
void IMuseAdlib::link_mc(Part *part, MidiChannelAdl *mc)
{
mc->_part = part;
mc->_next = (MidiChannelAdl *)part->_mc;
part->_mc = mc;
mc->_prev = NULL;
if (mc->_next)
mc->_next->_prev = mc;
}
void IMuseAdlib::part_key_on(Part *part, byte note, byte velocity)
{
MidiChannelAdl *mc;
if ((part->_player ? part->_player->_isGM : false) && part->_chan == 9)
return; // Jamieson630: GM percussion, not implemented for FM
mc = allocate_midichan(part->_pri_eff);
if (!mc)
return;
link_mc(part, mc);
mc_key_on(mc, note, velocity);
}
void IMuseAdlib::part_key_off(Part *part, byte note)
{
MidiChannelAdl *mc;
if ((part->_player ? part->_player->_isGM : false) && part->_chan == 9)
return; // Jamieson630: GM percussion, not implemented for FM
for (mc = (MidiChannelAdl *)part->_mc; mc; mc = mc->_next) {
if (mc->_note == note) {
if (part->_pedal)
mc->_waitforpedal = true;
else
mc_off(mc);
}
}
}
struct AdlibInstrSetParams {
byte param;
byte shl;
byte mask;
};
#define MKLINE(_a_,_b_,_c_) { (int)&((Instrument*)0)->_a_, _b_, ((1<<(_c_))-1)<<(_b_) }
static const AdlibInstrSetParams adlib_instr_params[69] = {
MKLINE(oplvl_2, 0, 6),
MKLINE(waveform_2, 2, 5),
MKLINE(oplvl_2, 6, 2),
MKLINE(flags_2, 0, 4),
MKLINE(atdec_2, 4, 4),
MKLINE(atdec_2, 0, 4),
MKLINE(sustrel_2, 4, 4),
MKLINE(sustrel_2, 0, 4),
MKLINE(waveform_2, 0, 2),
MKLINE(flags_2, 7, 1),
MKLINE(flags_2, 6, 1),
MKLINE(flags_2, 5, 1),
MKLINE(flags_2, 4, 1),
MKLINE(oplvl_1, 0, 6),
MKLINE(waveform_1, 2, 5),
MKLINE(oplvl_1, 6, 2),
MKLINE(flags_1, 0, 4),
MKLINE(atdec_1, 4, 4),
MKLINE(atdec_1, 0, 4),
MKLINE(sustrel_1, 4, 4),
MKLINE(sustrel_1, 0, 4),
MKLINE(waveform_1, 0, 2),
MKLINE(flags_1, 7, 1),
MKLINE(flags_1, 6, 1),
MKLINE(flags_1, 5, 1),
MKLINE(flags_1, 4, 1),
MKLINE(feedback, 0, 1),
MKLINE(feedback, 1, 3),
MKLINE(flags_a, 7, 1),
MKLINE(flags_a, 6, 1),
MKLINE(flags_a, 5, 1),
MKLINE(flags_a, 4, 1),
MKLINE(flags_a, 0, 4),
MKLINE(extra_a.a, 0, 8),
MKLINE(extra_a.b, 0, 7),
MKLINE(extra_a.c, 0, 7),
MKLINE(extra_a.d, 0, 7),
MKLINE(extra_a.e, 0, 7),
MKLINE(extra_a.f, 0, 7),
MKLINE(extra_a.g, 0, 7),
MKLINE(extra_a.h, 0, 7),
MKLINE(extra_a.b, 7, 1),
MKLINE(extra_a.c, 7, 1),
MKLINE(extra_a.d, 7, 1),
MKLINE(extra_a.e, 7, 1),
MKLINE(extra_a.f, 7, 1),
MKLINE(extra_a.g, 7, 1),
MKLINE(extra_a.h, 7, 1),
MKLINE(flags_b, 7, 1),
MKLINE(flags_b, 6, 1),
MKLINE(flags_b, 5, 1),
MKLINE(flags_b, 4, 1),
MKLINE(flags_b, 0, 4),
MKLINE(extra_b.a, 0, 8),
MKLINE(extra_b.b, 0, 7),
MKLINE(extra_b.c, 0, 7),
MKLINE(extra_b.d, 0, 7),
MKLINE(extra_b.e, 0, 7),
MKLINE(extra_b.f, 0, 7),
MKLINE(extra_b.g, 0, 7),
MKLINE(extra_b.h, 0, 7),
MKLINE(extra_b.b, 7, 1),
MKLINE(extra_b.c, 7, 1),
MKLINE(extra_b.d, 7, 1),
MKLINE(extra_b.e, 7, 1),
MKLINE(extra_b.f, 7, 1),
MKLINE(extra_b.g, 7, 1),
MKLINE(extra_b.h, 7, 1),
MKLINE(duration, 0, 8),
};
#undef MKLINE
void IMuseAdlib::part_set_param(Part *part, byte param, int value)
{
const AdlibInstrSetParams *sp = &adlib_instr_params[param];
byte *p = (byte *)&_part_instr[part->_slot] + sp->param;
*p = (*p & ~sp->mask) | (value << sp->shl);
if (param < 28) {
MidiChannelAdl *mc;
for (mc = (MidiChannelAdl *)part->_mc; mc; mc = mc->_next) {
adlib_set_param(mc->_channel, param, value);
}
}
}
void IMuseAdlib::part_off(Part *part)
{
MidiChannelAdl *mc = (MidiChannelAdl *)part->_mc;
part->_mc = NULL;
for (; mc; mc = mc->_next) {
mc_off(mc);
}
}
void IMuseAdlib::mc_off(MidiChannel * mc2)
{
MidiChannelAdl *mc = (MidiChannelAdl *)mc2, *tmp;
adlib_key_off(mc->_channel);
tmp = mc->_prev;
if (mc->_next)
mc->_next->_prev = tmp;
if (tmp)
tmp->_next = mc->_next;
else
mc->_part->_mc = mc->_next;
mc->_part = NULL;
}
void IMuseAdlib::part_set_instrument(Part *part, Instrument * instr)
{
Instrument *i = &_part_instr[part->_slot];
memcpy(i, instr, sizeof(Instrument));
}
int IMuseAdlib::part_update_active(Part *part, uint16 *active)
{
uint16 bits;
int count = 0;
MidiChannelAdl *mc;
bits = 1 << part->_chan;
for (mc = part->_mc->adl(); mc; mc = mc->_next) {
if (!(active[mc->_note] & bits)) {
active[mc->_note] |= bits;
count++;
}
}
return count;
}
//********************************************
//** GENERAL MIDI PART OF IMUSE STARTS HERE **
//********************************************
void IMuseGM::midiPitchBend(byte chan, int16 pitchbend)
{
uint16 tmp;
if (_midi_pitchbend_last[chan] != pitchbend) {
_midi_pitchbend_last[chan] = pitchbend;
tmp = pitchbend + 0x2000;
_md->send(((tmp >> 7) & 0x7F) << 16 | (tmp & 0x7F) << 8 | 0xE0 | chan);
}
}
void IMuseGM::midiPitchBendFactor (byte chan, byte factor) {
if (_midi_pitchbend_factor_last[chan] != factor) {
_midi_pitchbend_factor_last[chan] = factor;
_md->setPitchBendRange (chan, factor);
}
}
void IMuseGM::midiVolume(byte chan, byte volume)
{
if (_midi_volume_last[chan] != volume) {
_midi_volume_last[chan] = volume;
_md->send(volume << 16 | 7 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiPedal(byte chan, bool pedal)
{
if (_midi_pedal_last[chan] != pedal) {
_midi_pedal_last[chan] = pedal;
_md->send(pedal << 16 | 64 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiModWheel(byte chan, byte modwheel)
{
if (_midi_modwheel_last[chan] != modwheel) {
_midi_modwheel_last[chan] = modwheel;
_md->send(modwheel << 16 | 1 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiEffectLevel(byte chan, byte level)
{
if (_midi_effectlevel_last[chan] != level) {
_midi_effectlevel_last[chan] = level;
_md->send(level << 16 | 91 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiChorus(byte chan, byte chorus)
{
if (_midi_chorus_last[chan] != chorus) {
_midi_chorus_last[chan] = chorus;
_md->send(chorus << 16 | 93 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiControl0(byte chan, byte value)
{
_md->send(value << 16 | 0 << 8 | 0xB0 | chan);
}
void IMuseGM::midiProgram(byte chan, byte program, bool mt32emulate)
{
if (mt32emulate) { /* Don't convert the percussion channel, it is the same in GM and MT32 */
if (chan != PERCUSSION_CHANNEL)
program = mt32_to_gmidi[program];
}
_md->send(program << 8 | 0xC0 | chan);
}
void IMuseGM::midiPan(byte chan, int8 pan)
{
if (_midi_pan_last[chan] != pan) {
_midi_pan_last[chan] = pan;
_md->send(((pan - 64) & 0x7F) << 16 | 10 << 8 | 0xB0 | chan);
}
}
void IMuseGM::midiNoteOn(byte chan, byte note, byte velocity)
{
_md->send(velocity << 16 | note << 8 | 0x90 | chan);
}
void IMuseGM::midiNoteOff(byte chan, byte note)
{
_md->send(note << 8 | 0x80 | chan);
}
void IMuseGM::midiSilence(byte chan)
{
_md->send((64 << 8) | 0xB0 | chan);
_md->send((123 << 8) | 0xB0 | chan);
}
void IMuseGM::part_key_on(Part *part, byte note, byte velocity)
{
MidiChannelGM *mc = part->_mc->gm();
if (mc) {
mc->_actives[note >> 4] |= (1 << (note & 0xF));
midiNoteOn(mc->_chan, note, velocity);
} else if (part->_percussion) {
midiVolume(PERCUSSION_CHANNEL, part->_vol_eff);
midiProgram(PERCUSSION_CHANNEL, part->_bank, part->_player->_mt32emulate);
midiNoteOn(PERCUSSION_CHANNEL, note, velocity);
}
}
void IMuseGM::part_key_off(Part *part, byte note)
{
MidiChannelGM *mc = part->_mc->gm();
if (mc) {
mc->_actives[note >> 4] &= ~(1 << (note & 0xF));
midiNoteOff(mc->_chan, note);
} else if (part->_percussion) {
midiNoteOff(PERCUSSION_CHANNEL, note);
}
}
#if !defined(__MORPHOS__)
int IMuseGM::midi_driver_thread(void *param)
{
IMuseGM *mid = (IMuseGM *) param;
int old_time, cur_time;
old_time = mid->_system->get_msecs();
for (;;) {
mid->_system->delay_msecs(10);
cur_time = mid->_system->get_msecs();
while (old_time < cur_time) {
old_time += 10;
mid->_se->on_timer();
}
}
return 0;
}
#else
#include <proto/exec.h>
#include <proto/dos.h>
#include "morphos.h"
#include "morphos_sound.h"
int IMuseGM::midi_driver_thread(void *param)
{
IMuseGM *mid = (IMuseGM *) param;
int old_time, cur_time;
MsgPort *music_timer_port = NULL;
timerequest *music_timer_request = NULL;
ObtainSemaphore(&ScummMusicThreadRunning);
if (!OSystem_MorphOS::OpenATimer(&music_timer_port, (IORequest **) &music_timer_request, UNIT_MICROHZ, false)) {
warning("Could not open a timer - music will not play");
Wait(SIGBREAKF_CTRL_C);
}
else {
old_time = mid->_system->get_msecs();
for (;;) {
music_timer_request->tr_node.io_Command = TR_ADDREQUEST;
music_timer_request->tr_time.tv_secs = 0;
music_timer_request->tr_time.tv_micro = 10000;
DoIO((struct IORequest *)music_timer_request);
if (CheckSignal(SIGBREAKF_CTRL_C))
break;
cur_time = mid->_system->get_msecs();
while (old_time < cur_time) {
old_time += 10;
mid->_se->on_timer();
}
}
}
ReleaseSemaphore(&ScummMusicThreadRunning);
RemTask(NULL);
return 0;
}
#endif
void IMuseGM::init(IMuseInternal *eng, OSystem *syst)
{
int i;
MidiChannelGM *mc;
_system = syst;
/* open midi driver */
int result = _md->open(MidiDriver::MO_SIMPLE);
if (result)
error("IMuseGM::error = %s", MidiDriver::get_error_name(result));
/* Install the on_timer thread */
_se = eng;
syst->create_thread(midi_driver_thread, this);
for (i = 0, mc = _midi_channels; i != ARRAYSIZE(_midi_channels); i++, mc++) {
mc->_chan = i;
_midi_program_last [i] = 255;
}
}
void IMuseGM::uninit()
{
_md->close();
}
void IMuseGM::update_pris()
{
Part *part, *hipart;
int i;
byte hipri, lopri;
MidiChannelGM *mc, *lomc;
while (true) {
hipri = 0;
hipart = NULL;
for (i = 32, part = _se->parts_ptr(); i; i--, part++) {
if (part->_player && !part->_percussion && part->_on && !part->_mc && part->_pri_eff >= hipri) {
hipri = part->_pri_eff;
hipart = part;
}
}
if (!hipart)
return;
lopri = 255;
lomc = NULL;
for (i = ARRAYSIZE(_midi_channels), mc = _midi_channels;; mc++) {
if (mc->_chan != 9) {
if (!mc->_part) {
lomc = mc;
break;
}
if (mc->_part->_pri_eff <= lopri) {
lopri = mc->_part->_pri_eff;
lomc = mc;
}
}
if (!--i) {
if (lopri >= hipri)
return;
lomc->_part->off();
break;
}
}
hipart->_mc = lomc;
lomc->_part = hipart;
hipart->changed(pcAll);
}
}
int IMuseGM::part_update_active(Part *part, uint16 *active)
{
int i, j;
uint16 *act, mask, bits;
int count = 0;
bits = 1 << part->_chan;
act = part->_mc->gm()->_actives;
for (i = 8; i; i--) {
mask = *act++;
if (mask) {
for (j = 16; j; j--, mask >>= 1, active++) {
if (mask & 1 && !(*active & bits)) {
*active |= bits;
count++;
}
}
} else {
active += 16;
}
}
return count;
}
void IMuseGM::part_changed(Part *part, uint16 what)
{
MidiChannelGM *mc;
/* Mark for re-schedule if program changed when in pre-state */
if (what & pcProgram && !part->_percussion && !part->_mc) {
update_pris();
}
if (!(mc = part->_mc->gm()))
return;
if (part->_player == NULL) { /* No player, so dump phantom channel */
part->_mc = NULL;
mc->_part = NULL;
memset(mc->_actives, 0, sizeof(mc->_actives));
return;
}
if (what & pcPitchBendFactor)
midiPitchBendFactor (mc->_chan, part->_pitchbend_factor);
if (what & pcMod)
midiPitchBend(mc->_chan,
clamp(part->_pitchbend +
(part->_detune_eff * 64 / 12) +
(part->_transpose_eff * 8192 / 12), -8192, 8191));
if (what & pcVolume)
midiVolume(mc->_chan, part->_vol_eff);
if (what & pcPedal)
midiPedal(mc->_chan, part->_pedal);
if (what & pcModwheel)
midiModWheel(mc->_chan, part->_modwheel);
if (what & pcPan)
midiPan(mc->_chan, part->_pan_eff);
if (what & pcEffectLevel)
midiEffectLevel(mc->_chan, part->_effect_level);
if (what & pcProgram) {
_midi_program_last [part->_chan] = part->_program;
if (part->_bank) {
midiControl0(mc->_chan, part->_bank);
midiProgram(mc->_chan, part->_program, part->_player->_mt32emulate);
midiControl0(mc->_chan, 0);
} else {
midiProgram(mc->_chan, part->_program, part->_player->_mt32emulate);
}
}
if (what & pcChorus)
midiChorus(mc->_chan, part->_effect_level);
}
void IMuseGM::part_off(Part *part)
{
MidiChannelGM *mc = part->_mc->gm();
if (mc) {
part->_mc = NULL;
mc->_part = NULL;
memset(mc->_actives, 0, sizeof(mc->_actives));
midiSilence(mc->_chan);
}
}
// The IMuse::create method provides a front-end factory
// for creating IMuseInternal without exposing that class
// to the client.
IMuse *IMuse::create(OSystem *syst, MidiDriver *midi, SoundMixer *mixer)
{
return (IMuse *) IMuseInternal::create (syst, midi, mixer);
}
/*
* IMUSE Digital Implementation, for SCUMM v7 and higher.
*/
static void imus_digital_handler(void * engine) {
g_scumm->_imuseDigital->handler();
}
IMuseDigital::IMuseDigital(Scumm *scumm) {
memset(_channel, 0, sizeof(channel) * MAX_DIGITAL_CHANNELS);
_scumm = scumm;
for (int32 l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
_channel[l]._initialized = false;
}
_scumm->_mixer->beginSlots(MAX_DIGITAL_CHANNELS + 1);
_scumm->_timer->installProcedure(imus_digital_handler, 200);
_pause = false;
}
IMuseDigital::~IMuseDigital() {
for (int32 l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
_scumm->_mixer->stop(l);
}
_scumm->_timer->releaseProcedure(imus_digital_handler);
}
struct imuse_music_table {
int16 index;
char name[30];
char title[30];
char filename[15];
int8 unk1;
};
struct imuse_music_map {
int16 room;
int16 table_index;
int16 unk1;
int16 unk2;
int16 unk3;
int16 unk4;
};
static const imuse_music_map _digStateMusicMap[] = {
{0, 0, 0, 0, 0, 0 },
{1, 0, 0, 0, 0, 0 },
{2, 2, 0, 0, 0, 0 },
{3, 47, 0, 0, 0, 0 },
{4, 3, 0, 0, 0, 0 },
{5, 3, 0, 0, 0, 0 },
{6, 3, 0, 0, 0, 0 },
{7, 3, 0, 0, 0, 0 },
{8, 4, 0, 0, 0, 0 },
{9, 5, 0, 0, 0, 0 },
{10, 4, 0, 0, 0, 0 },
{11, 44, 0, 0, 0, 0 },
{12, 5, 0, 0, 0, 0 },
{13, 1, 0, 0, 0, 0 },
{14, 5, 0, 0, 0, 0 },
{15, 6, 29, 7, 0, 0 },
{16, 8, 0, 0, 0, 0 },
{17, 1, 0, 0, 0, 0 },
{18, 9, 0, 0, 0, 0 },
{19, 9, 0, 0, 0, 0 },
{20, 6, 0, 0, 0, 0 },
{21, 6, 0, 0, 0, 0 },
{22, 44, 0, 0, 0, 0 },
{23, 10, 7, 0, 0, 0 },
{24, 26, 0, 0, 0, 0 },
{25, 17, 0, 0, 0, 0 },
{26, 17, 0, 0, 0, 0 },
{27, 18, 0, 0, 0, 0 },
{28, 1, 0, 0, 0, 0 },
{29, 20, 0, 0, 0, 0 },
{30, 22, 0, 0, 0, 0 },
{31, 23, 0, 0, 0, 0 },
{32, 22, 0, 0, 0, 0 },
{33, 26, 0, 0, 0, 0 },
{34, 24, 0, 0, 0, 0 },
{35, 1, 0, 0, 0, 0 },
{36, 1, 0, 0, 0, 0 },
{37, 42, 0, 0, 0, 0 },
{38, 43, 0, 0, 0, 0 },
{39, 44, 0, 0, 0, 0 },
{40, 1, 0, 0, 0, 0 },
{41, 43, 0, 0, 0, 0 },
{42, 44, 0, 0, 0, 0 },
{43, 43, 0, 0, 0, 0 },
{44, 45, 117,45, 114,26},
{45, 1, 0, 0, 0, 0 },
{46, 33, 6, 35, 5, 34},
{47, 1, 0, 0, 0, 0 },
{48, 43, 0, 0, 0, 0 },
{49, 44, 0, 0, 0, 0 },
{50, 1, 0, 0, 0, 0 },
{51, 1, 0, 0, 0, 0 },
{52, 0, 0, 0, 0, 0 },
{53, 28, 0, 0, 0, 0 },
{54, 28, 0, 0, 0, 0 },
{55, 29, 0, 0, 0, 0 },
{56, 29, 0, 0, 0, 0 },
{57, 29, 0, 0, 0, 0 },
{58, 31, 0, 0, 0, 0 },
{59, 1, 0, 0, 0, 0 },
{60, 37, 0, 0, 0, 0 },
{61, 39, 0, 0, 0, 0 },
{62, 38, 0, 0, 0, 0 },
{63, 39, 0, 0, 0, 0 },
{64, 39, 0, 0, 0, 0 },
{65, 40, 0, 0, 0, 0 },
{66, 1, 0, 0, 0, 0 },
{67, 40, 0, 0, 0, 0 },
{68, 39, 0, 0, 0, 0 },
{69, 1, 0, 0, 0, 0 },
{70, 49, 0, 0, 0, 0 },
{71, 1, 0, 0, 0, 0 },
{72, 1, 0, 0, 0, 0 },
{73, 50, 0, 0, 0, 0 },
{74, 1, 0, 0, 0, 0 },
{75, 51, 0, 0, 0, 0 },
{76, 1, 0, 0, 0, 0 },
{77, 52, 7, 0, 0, 0 },
{78, 63, 0, 0, 0, 0 },
{79, 1, 0, 0, 0, 0 },
{80, 41, 0, 0, 0, 0 },
{81, 48, 0, 0, 0, 0 },
{82, 21, 0, 0, 0, 0 },
{83, 27, 0, 0, 0, 0 },
{84, 1, 0, 0, 0, 0 },
{85, 1, 0, 0, 0, 0 },
{86, 0, 0, 0, 0, 0 },
{87, 1, 0, 0, 0, 0 },
{88, 32, 0, 0, 0, 0 },
{89, 33, 6, 35, 5, 34},
{90, 16, 0, 0, 0, 0 },
{91, 57, 0, 0, 0, 0 },
{92, 25, 0, 0, 0, 0 },
{93, 0, 0, 0, 0, 0 },
{94, 36, 0, 0, 0, 0 },
{95, 19, 0, 0, 0, 0 },
{96, 13, 0, 0, 0, 0 },
{97, 14, 0, 0, 0, 0 },
{98, 11, 0, 0, 0, 0 },
{99, 15, 0, 0, 0, 0 },
{100, 17, 0, 0, 0, 0 },
{101, 38, 0, 0, 0, 0 },
{102, 1, 0, 0, 0, 0 },
{103, 0, 0, 0, 0, 0 },
{104, 0, 0, 0, 0, 0 },
{105, 30, 128,29, 0, 0 },
{106, 0, 0, 0, 0, 0 },
{107, 1, 0, 0, 0, 0 },
{108, 1, 0, 0, 0, 0 },
{109, 1, 0, 0, 0, 0 },
{110, 2, 0, 0, 0, 0 },
{111, 1, 0, 0, 0, 0 },
{-1, 1, 0, 0, 0, 0 },
};
static const imuse_music_table _digStateMusicTable[] = {
{0, "STATE_NULL", "", "", 0},
{1, "stateNoChange", "", "", 0},
{2, "stateAstShip", "Asteroid (amb-ship)", "ASTERO~1.IMU", 3},
{3, "stateAstClose", "Asteroid (amb-close)", "ASTERO~2.IMU", 3},
{4, "stateAstInside", "Asteroid (inside)", "ASTERO~3.IMU", 3},
{5, "stateAstCore", "Asteroid (core)", "ASTERO~4.IMU", 3},
{6, "stateCanyonClose", "Canyon (close)", "CANYON~1.IMU", 3},
{7, "stateCanyonClose_m", "Canyon (close-m)", "CANYON~2.IMU", 3},
{8, "stateCanyonOver", "Canyon (over)", "CANYON~3.IMU", 3},
{9, "stateCanyonWreck", "Canyon (wreck)", "CANYON~4.IMU", 3},
{10, "stateNexusCanyon", "Nexus (plan)", "NEXUS(~1.IMU", 3},
{11, "stateNexusPlan", "Nexus (plan)", "NEXUS(~1.IMU", 3},
{12, "stateNexusRamp", "Nexus (ramp)", "NEXUS(~2.IMU", 3},
{13, "stateNexusMuseum", "Nexus (museum)", "NEXUS(~3.IMU", 3},
{14, "stateNexusMap", "Nexus (map)", "NEXUS(~4.IMU", 3},
{15, "stateNexusTomb", "Nexus (tomb)", "NE3706~5.IMU", 3},
{16, "stateNexusCath", "Nexus (cath)", "NE3305~5.IMU", 3},
{17, "stateNexusAirlock", "Nexus (airlock)", "NE2D3A~5.IMU", 3},
{18, "stateNexusPowerOff", "Nexus (power)", "NE8522~5.IMU", 3},
{19, "stateMuseumTramNear", "Tram (mu-near)", "TRAM(M~1.IMU", 3},
{20, "stateMuseumTramFar", "Tram (mu-far)", "TRAM(M~2.IMU", 3},
{21, "stateMuseumLockup", "Museum (lockup)", "MUSEUM~1.IMU", 3},
{22, "stateMuseumPool", "Museum (amb-pool)", "MUSEUM~2.IMU", 3},
{23, "stateMuseumSpire", "Museum (amb-spire)", "MUSEUM~3.IMU", 3},
{24, "stateMuseumMuseum", "Museum (amb-mu)", "MUSEUM~4.IMU", 3},
{25, "stateMuseumLibrary", "Museum (library)", "MUB575~5.IMU", 3},
{26, "stateMuseumCavern", "Museum (cavern)", "MUF9BE~5.IMU", 3},
{27, "stateTombTramNear", "Tram (tomb-near)", "TRAM(T~1.IMU", 3},
{28, "stateTombBase", "Tomb (amb-base)", "TOMB(A~1.IMU", 3},
{29, "stateTombSpire", "Tomb (amb-spire)", "TOMB(A~2.IMU", 3},
{30, "stateTombCave", "Tomb (amb-cave)", "TOMB(A~3.IMU", 3},
{31, "stateTombCrypt", "Tomb (crypt)", "TOMB(C~1.IMU", 3},
{32, "stateTombGuards", "Tomb (crypt-guards)", "TOMB(C~2.IMU", 3},
{33, "stateTombInner", "Tomb (inner)", "TOMB(I~1.IMU", 3},
{34, "stateTombCreator1", "Tomb (creator 1)", "TOMB(C~3.IMU", 3},
{35, "stateTombCreator2", "Tomb (creator 2)", "TOMB(C~4.IMU", 3},
{36, "statePlanTramNear", "Tram (plan-near)", "TRAM(P~1.IMU", 3},
{37, "statePlanTramFar", "Tram (plan-far)", "TRAM(P~2.IMU", 3},
{38, "statePlanBase", "Plan (amb-base)", "PLAN(A~1.IMU", 3},
{39, "statePlanSpire", "Plan (amb-spire)", "PLAN(A~2.IMU", 3},
{40, "statePlanDome", "Plan (dome)", "PLAN(D~1.IMU", 3},
{41, "stateMapTramNear", "Tram (map-near)", "TRAM(M~3.IMU", 3},
{42, "stateMapTramFar", "Tram (map-far)", "TRAM(M~4.IMU", 3},
{43, "stateMapCanyon", "Map (amb-canyon)", "MAP(AM~1.IMU", 3},
{44, "stateMapExposed", "Map (amb-exposed)", "MAP(AM~2.IMU", 3},
{45, "stateMapNestEmpty", "Map (amb-nest)", "MAP(AM~4.IMU", 3},
{46, "stateMapNestMonster", "Map (monster)", "MAP(MO~1.IMU", 3},
{47, "stateMapKlein", "Map (klein)", "MAP(KL~1.IMU", 3},
{48, "stateCathTramNear", "Tram (cath-near)", "TRAM(C~1.IMU", 3},
{49, "stateCathTramFar", "Tram (cath-far)", "TRAM(C~2.IMU", 3},
{50, "stateCathLab", "Cath (amb-inside)", "CATH(A~1.IMU", 3},
{51, "stateCathOutside", "Cath (amb-outside)", "CATH(A~2.IMU", 3},
{52, "stateWorldMuseum", "World (museum)", "WORLD(~1.IMU", 3},
{53, "stateWorldPlan", "World (plan)", "WORLD(~2.IMU", 3},
{54, "stateWorldTomb", "World (tomb)", "WORLD(~3.IMU", 3},
{55, "stateWorldMap", "World (map)", "WORLD(~4.IMU", 3},
{56, "stateWorldCath", "World (cath)", "WO3227~5.IMU", 3},
{57, "stateEye1", "Eye 1", "EYE1~1.IMU", 3},
{58, "stateEye2", "Eye 2", "EYE2~1.IMU", 3},
{59, "stateEye3", "Eye 3", "EYE3~1.IMU", 3},
{60, "stateEye4", "Eye 4", "EYE4~1.IMU", 3},
{61, "stateEye5", "Eye 5", "EYE5~1.IMU", 3},
{62, "stateEye6", "Eye 6", "EYE6~1.IMU", 3},
{63, "stateEye7", "Eye 7", "EYE7~1.IMU", 3},
{-1, "", "", "", 0},
};
static const imuse_music_table _digSeqMusicTable[] = {
{2000, "SEQ_NULL", "", "", 0},
{2005, "seqLogo", "", "", 0},
{2010, "seqIntro", "", "", 0},
{2020, "seqExplosion1b", "", "", 6},
{2030, "seqAstTunnel1a", "Seq (ast tunnel 1a)", "SEQ(AS~1.IMU", 3},
{2031, "seqAstTunnel2b", "", "", 6},
{2032, "seqAstTunnel3a", "Seq (ast tunnel 3a)", "SEQ(AS~2.IMU", 4},
{2040, "seqToPlanet1b", "", "", 5},
{2045, "seqArgBegin", "Seq (arg begin)", "SEQ(AR~1.IMU", 4},
{2046, "seqArgEnd", "Seq (arg end)", "SEQ(AR~2.IMU", 4},
{2050, "seqWreckGhost", "Seq (ghost-wreck)", "SEQ(GH~1.IMU", 4},
{2060, "seqCanyonGhost", "Seq (ghost-canyon)", "SEQ(GH~2.IMU", 4},
{2070, "seqBrinkFall", "", "", 0},
{2080, "seqPanUpCanyon", "Seq (pan up canyon)", "SEQ(PA~1.IMU", 4},
{2091, "seqAirlockTunnel1b", "", "", 6},
{2100, "seqTramToMu", "", "", 6},
{2101, "seqTramFromMu", "", "", 6},
{2102, "seqTramToTomb", "", "", 6},
{2103, "seqTramFromTomb", "", "", 6},
{2104, "seqTramToPlan", "", "", 6},
{2105, "seqTramFromPlan", "", "", 6},
{2106, "seqTramToMap", "", "", 6},
{2107, "seqTramFromMap", "", "", 6},
{2108, "seqTramToCath", "", "", 6},
{2109, "seqTramFromCath", "", "", 6},
{2110, "seqMuseumGhost", "", "", 0},
{2120, "seqSerpentAppears", "", "", 0},
{2130, "seqSerpentEats", "", "", 0},
{2140, "seqBrinkRes1b", "", "", 6},
{2141, "seqBrinkRes2a", "Seq (brink's madness)", "SEQ(BR~1.IMU", 4},
{2150, "seqLockupEntry", "Seq (brink's madness)", "SEQ(BR~1.IMU", 3},
{2160, "seqSerpentExplodes", "", "", 0},
{2170, "seqSwimUnderwater", "Seq (descent)", "SEQ(DE~1.IMU", 4},
{2175, "seqWavesPlunge", "Seq (plunge)", "SEQ(PL~1.IMU", 4},
{2180, "seqCryptOpens", "", "", 0},
{2190, "seqGuardsFight", "", "", 0},
{2200, "seqCreatorRes1.1a", "Seq (creator res 1.1a)", "SEQ(CR~1.IMU", 3},
{2201, "seqCreatorRes1.2b", "", "", 6},
{2210, "seqMaggieCapture1b", "", "", 6},
{2220, "seqStealCrystals", "Seq (brink's madness)", "SEQ(BR~1.IMU", 3},
{2230, "seqGetByMonster", "", "", 0},
{2240, "seqKillMonster1b", "", "", 6},
{2250, "seqCreatorRes2.1a", "Seq (creator res 2.1a)", "SEQ(CR~2.IMU", 3},
{2251, "seqCreatorRes2.2b", "", "", 6},
{2252, "seqCreatorRes2.3a", "Seq (creator res 2.3a)", "SEQ(CR~3.IMU", 4},
{2260, "seqMaggieInsists", "", "", 0},
{2270, "seqBrinkHelpCall", "", "", 0},
{2280, "seqBrinkCrevice1a", "Seq (brink crevice 1a)", "SEQ(BR~2.IMU", 3},
{2281, "seqBrinkCrevice2a", "Seq (brink crevice 2a)", "SEQ(BR~3.IMU", 3},
{2290, "seqCathAccess1b", "", "", 6},
{2291, "seqCathAccess2a", "Seq (cath access 2a)", "SEQ(CA~1.IMU", 4},
{2300, "seqBrinkAtGenerator", "Seq (brink's madness)", "SEQ(BR~1.IMU", 3},
{2320, "seqFightBrink1b", "", "", 6},
{2340, "seqMaggieDies1b", "", "", 6},
{2346, "seqMaggieRes1b", "", "", 6},
{2347, "seqMaggieRes2a", "Seq (maggie res 2a)", "SEQ(MA~1.IMU", 4},
{2350, "seqCreatureFalls", "", "", 0},
{2360, "seqFinale1b", "", "", 5},
{2370, "seqFinale2a", "Seq (finale 2a)", "SEQ(FI~1.IMU", 3},
{2380, "seqFinale3b1", "", "", 6},
{2390, "seqFinale3b2", "", "", 6},
{2400, "seqFinale4a", "Seq (finale 4a)", "SEQ(FI~2.IMU", 3},
{2410, "seqFinale5a", "Seq (finale 5a)", "SEQ(FI~3.IMU", 3},
{2420, "seqFinale6a", "Seq (finale 6a)", "SEQ(FI~4.IMU", 3},
{2430, "seqFinale7a", "Seq (finale 7a)", "SE3D2B~5.IMU", 3},
{2440, "seqFinale8b", "", "", 6},
{2450, "seqFinale9a", "Seq (finale 9a)", "SE313B~5.IMU", 4},
{-1, "", "", "", 0},
};
struct imuse_ft_music_table {
int16 index;
char audioname[15];
int8 unk1;
int8 volume;
char name[30];
};
static const imuse_ft_music_table _ftStateMusicTable[] = {
{0, "", 0, 0, "STATE_NULL" },
{1, "", 4, 127, "stateKstandOutside" },
{2, "kinside", 2, 127, "stateKstandInside" },
{3, "moshop", 3, 64, "stateMoesInside" },
{4, "melcut", 2, 127, "stateMoesOutside" },
{5, "mellover", 2, 127, "stateMellonAbove" },
{6, "radloop", 3, 28, "stateTrailerOutside" },
{7, "radloop", 3, 58, "stateTrailerInside" },
{8, "radloop", 3, 127, "stateTodShop" },
{9, "junkgate", 2, 127, "stateJunkGate" },
{10, "junkover", 3, 127, "stateJunkAbove" },
{11, "gastower", 2, 127, "stateGasTower" },
{12, "", 4, 0, "stateTowerAlarm" },
{13, "melcut", 2, 127, "stateCopsOnGround" },
{14, "melcut", 2, 127, "stateCopsAround" },
{15, "melcut", 2, 127, "stateMoesRuins" },
{16, "melcut", 2, 127, "stateKstandNight" },
{17, "trukblu2", 2, 127, "stateTruckerTalk" },
{18, "stretch", 2, 127, "stateMumblyPeg" },
{19, "kstand", 2, 127, "stateRanchOutside" },
{20, "kinside", 2, 127, "stateRanchInside" },
{21, "desert", 2, 127, "stateWreckedTruck" },
{22, "opening", 2, 127, "stateGorgeVista" },
{23, "caveopen", 2, 127, "stateCaveOpen" },
{24, "cavecut1", 2, 127, "stateCaveOuter" },
{25, "cavecut1", 1, 127, "stateCaveMiddle" },
{26, "cave", 2, 127, "stateCaveInner" },
{27, "corville", 2, 127, "stateCorvilleFront" },
{28, "mines", 2, 127, "stateMineField" },
{29, "bunyman3", 2, 127, "stateBunnyStore" },
{30, "stretch", 2, 127, "stateStretchBen" },
{31, "saveme", 2, 127, "stateBenPleas" },
{32, "", 4, 0, "stateBenConvinces" },
{33, "derby", 3, 127, "stateDemoDerby" },
{34, "fire", 3, 127, "stateLightMyFire" },
{35, "derby", 3, 127, "stateDerbyChase" },
{36, "carparts", 2, 127, "stateVultureCarParts"},
{37, "cavecut1", 2, 127, "stateVulturesInside" },
{38, "mines", 2, 127, "stateFactoryRear" },
{39, "croffice", 2, 127, "stateCorleyOffice" },
{40, "melcut", 2, 127, "stateCorleyHall" },
{41, "", 4, 0, "stateProjRoom" },
{42, "", 4, 0, "stateMMRoom" },
{43, "bumper", 2, 127, "stateBenOnBumper" },
{44, "benump", 2, 127, "stateBenOnBack" },
{45, "plane", 2, 127, "stateInCargoPlane" },
{46, "saveme", 2, 127, "statePlaneControls" },
{47, "", 4, 0, "stateCliffHanger1" },
{48, "", 4, 0, "stateCliffHanger2" },
{-1, "", 0, 0, "" },
};
static const imuse_ft_music_table _ftSeqMusicTable[] = {
{0, "", 2, 127, "SEQ_NULL" },
{1, "", 0, 0, "seqLogo" },
{2, "", 0, 0, "seqOpenFlick" },
{3, "", 0, 0, "seqBartender" },
{4, "opening", 2, 127, "seqBenWakes" },
{5, "", 0, 0, "seqPhotoScram" },
{6, "", 0, 0, "seqClimbChain" },
{7, "", 0, 0, "seqDogChase" },
{8, "barbeat", 2, 127, "seqDogSquish" },
{9, "barwarn", 2, 127, "seqDogHoist" },
{10, "", 0, 0, "seqCopsArrive" },
{11, "", 0, 0, "seqCopsLand" },
{12, "benwakes", 2, 127, "seqCopsLeave" },
{13, "", 0, 0, "seqCopterFlyby" },
{14, "", 0, 0, "seqCopterCrash" },
{15, "", 0, 0, "seqMoGetsParts" },
{16, "barwarn", 2, 127, "seqMoFixesBike" },
{17, "", 0, 0, "seqFirstGoodbye" },
{18, "", 0, 0, "seqCopRoadblock" },
{19, "", 0, 0, "seqDivertCops" },
{20, "swatben", 2, 127, "seqMurder" },
{21, "", 0, 0, "seqCorleyDies" },
{22, "", 0, 0, "seqTooLateAtMoes" },
{23, "", 0, 0, "seqPicture" },
{24, "dogattak", 2, 127, "seqNewsReel" },
{25, "", 0, 0, "seqCopsInspect" },
{26, "", 0, 0, "seqHijack" },
{27, "", 0, 0, "seqNestolusAtRanch" },
{28, "", 4, 0, "seqRipLimo" },
{29, "", 0, 0, "seqGorgeTurn" },
{30, "", 0, 0, "seqStealRamp" },
{31, "", 0, 0, "seqCavefishTalk" },
{32, "", 4, 0, "seqArriveCorville" },
{33, "", 0, 0, "seqSingleBunny" },
{34, "", 0, 0, "seqBunnyArmy" },
{35, "", 0, 0, "seqArriveAtMines" },
{36, "cops2", 2, 127, "seqArriveAtVultures" },
{37, "", 0, 0, "seqMakePlan" },
{38, "", 0, 0, "seqShowPlan" },
{39, "", 0, 0, "seqDerbyStart" },
{40, "cops2", 2, 127, "seqLightBales" },
{41, "", 0, 0, "seqNestolusBBQ" },
{42, "", 0, 0, "seqCallSecurity" },
{43, "", 0, 0, "seqFilmFail" },
{44, "cops2", 2, 127, "seqFilmBurn" },
{45, "", 0, 0, "seqRipSpeech" },
{46, "", 0, 0, "seqExposeRip" },
{47, "", 0, 0, "seqRipEscape" },
{48, "", 0, 0, "seqRareMoment" },
{49, "", 0, 0, "seqFanBunnies" },
{50, "", 0, 0, "seqRipDead" },
{51, "bunymrch", 2, 127, "seqFuneral" },
{52, "", 0, 0, "seqCredits" },
{-1, "", 0, 0, "" },
};
void IMuseDigital::handler() {
uint32 l = 0, i = 0;
if (_pause == true)
return;
for (l = 0; l < MAX_DIGITAL_CHANNELS;l ++) {
if (_channel[l]._used) {
if (_channel[l]._toBeRemoved == true) {
_scumm->_mixer->stop(l);
if (_scumm->_mixer->_channels[l] == NULL) {
free(_channel[l]._data);
_channel[l]._used = false;
_channel[l]._initialized = false;
}
continue;
}
if (_channel[l]._delay > 0) {
_channel[l]._delay--;
continue;
}
if (_channel[l]._volumeFade != -1) {
if (_channel[l]._volumeFadeStep < 0) {
if (_channel[l]._volume > _channel[l]._volumeFade) {
_channel[l]._volume += _channel[l]._volumeFadeStep;
_channel[l]._volumeRight += _channel[l]._volumeFadeStep;
if (_channel[l]._volume < _channel[l]._volumeFade) {
_channel[l]._volume = _channel[l]._volumeFade;
}
if (_channel[l]._volumeRight < _channel[l]._volumeFade) {
_channel[l]._volumeRight = _channel[l]._volumeFade;
}
if ((_channel[l]._volume == 0) && (_channel[l]._volumeRight == 0)) {
_channel[l]._toBeRemoved = true;
}
}
} else if (_channel[l]._volumeFadeStep > 0) {
if (_channel[l]._volume < _channel[l]._volumeFade) {
_channel[l]._volume += _channel[l]._volumeFadeStep;
_channel[l]._volumeRight += _channel[l]._volumeFadeStep;
if (_channel[l]._volume > _channel[l]._volumeFade) {
_channel[l]._volume = _channel[l]._volumeFade;
}
if (_channel[l]._volumeRight > _channel[l]._volumeFade) {
_channel[l]._volumeRight = _channel[l]._volumeFade;
}
}
}
}
if ((_channel[l]._jump[0]._numLoops == 0) && (_channel[l]._isJump == true)) {
_channel[l]._isJump = false;
}
uint32 new_size = _channel[l]._mixerSize;
uint32 mixer_size = new_size;
if (_channel[l]._initialized == false) {
mixer_size *= 2;
new_size *= 2;
}
if (_channel[l]._isJump == false) {
if (_channel[l]._offset + mixer_size > _channel[l]._size) {
new_size = _channel[l]._size - _channel[l]._offset;
if (_channel[l]._numLoops == 0) {
_channel[l]._toBeRemoved = true;
mixer_size = new_size;
}
}
} else if (_channel[l]._isJump == true) {
if (_channel[l]._jump[0]._numLoops != 500) {
_channel[l]._jump[0]._numLoops--;
}
if (_channel[l]._offset + mixer_size >= _channel[l]._jump[0]._offset) {
new_size = _channel[l]._jump[0]._offset - _channel[l]._offset;
}
}
byte *buf = (byte*)malloc(mixer_size);
memcpy(buf, _channel[l]._data + _channel[l]._offset, new_size);
if ((new_size != mixer_size) && (_channel[l]._isJump == true)) {
memcpy(buf + new_size, _channel[l]._data + _channel[l]._jump[0]._dest, mixer_size - new_size);
_channel[l]._offset = _channel[l]._jump[0]._dest + (mixer_size - new_size);
} else if ((_channel[l]._numLoops > 0) && (new_size != mixer_size)) {
memcpy(buf + new_size, _channel[l]._data, mixer_size - new_size);
_channel[l]._offset = mixer_size - new_size;
} else {
_channel[l]._offset += mixer_size;
}
if (_channel[l]._bits == 12) {
for(i = 0; i < (mixer_size / 4); i++) {
byte sample1 = buf[i * 4 + 0];
byte sample2 = buf[i * 4 + 1];
byte sample3 = buf[i * 4 + 2];
byte sample4 = buf[i * 4 + 3];
uint16 sample_a = (uint16)(((int16)((sample1 << 8) | sample2) * _channel[l]._volumeRight) >> 8);
uint16 sample_b = (uint16)(((int16)((sample3 << 8) | sample4) * _channel[l]._volume) >> 8);
buf[i * 4 + 0] = (byte)(sample_a >> 8);
buf[i * 4 + 1] = (byte)(sample_a & 0xff);
buf[i * 4 + 2] = (byte)(sample_b >> 8);
buf[i * 4 + 3] = (byte)(sample_b & 0xff);
}
} else if (_channel[l]._bits == 8) {
for(i = 0; i < (mixer_size / 2); i++) {
buf[i * 2 + 0] = (byte)(((int8)(buf[i * 2 + 0] ^ 0x80) * _channel[l]._volumeRight) >> 8) ^ 0x80;
buf[i * 2 + 1] = (byte)(((int8)(buf[i * 2 + 1] ^ 0x80) * _channel[l]._volume) >> 8) ^ 0x80;
}
}
if (_scumm->_silentDigitalImuse == false) {
if (_channel[l]._initialized == false) {
_scumm->_mixer->playStream(NULL, l, buf, mixer_size,
_channel[l]._freq, _channel[l]._mixerFlags, 3, 2000000);
_channel[l]._initialized = true;
} else {
_scumm->_mixer->append(l, buf, mixer_size, _channel[l]._freq, _channel[l]._mixerFlags);
}
}
}
}
}
void IMuseDigital::startSound(int sound) {
debug(2, "IMuseDigital::startSound(%d)", sound);
int32 l;
for(l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if(_channel[l]._used == false) {
byte *ptr = _scumm->getResourceAddress(rtSound, sound);
byte *s_ptr = ptr;
if(ptr == NULL) {
warning("IMuseDigital::startSound(%d) NULL resource pointer", sound);
return;
}
_channel[l]._idSound = sound;
_channel[l]._offset = 0;
_channel[l]._numRegions = 0;
_channel[l]._numJumps = 0;
_channel[l]._volumeRight = 127;
_channel[l]._volume = 127;
_channel[l]._volumeFade = -1;
_channel[l]._volumeFadeParam = 0;
_channel[l]._delay = 1;
uint32 tag, size = 0, r, t;
if (READ_UINT32_UNALIGNED(ptr) == MKID('Crea')) {
_channel[l]._bits = 8;
_channel[l]._channels = 2;
_channel[l]._mixerSize = (22050 / 5) * 2;
_channel[l]._mixerFlags = SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_STEREO | SoundMixer::FLAG_REVERSE_STEREO | SoundMixer::FLAG_UNSIGNED;
byte * t_ptr= _scumm->_sound->readCreativeVocFile(ptr, size, _channel[l]._freq, _channel[l]._numLoops);
if (_channel[l]._freq == 22222) {
_channel[l]._freq = 22050;
} else if (_channel[l]._freq == 10989) {
_channel[l]._freq = 11025;
}
if (_channel[l]._freq == 11025) {
_channel[l]._mixerSize /= 2;
}
size *= 2;
_channel[l]._data = (byte *)malloc(size);
for (t = 0; t < size / 2; t++) {
*(_channel[l]._data + t * 2 + 0) = *(t_ptr + t);
*(_channel[l]._data + t * 2 + 1) = *(t_ptr + t);
}
free(t_ptr);
_channel[l]._size = size;
} else if (READ_UINT32_UNALIGNED(ptr) == MKID('iMUS')) {
ptr += 16;
for (;;) {
tag = READ_BE_UINT32(ptr); ptr += 4;
switch(tag) {
case MKID_BE('FRMT'):
ptr += 12;
_channel[l]._bits = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._freq = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._channels = READ_BE_UINT32(ptr); ptr += 4;
break;
case MKID_BE('TEXT'):
size = READ_BE_UINT32(ptr); ptr += size + 4;
break;
case MKID_BE('REGN'):
ptr += 4;
if (_channel[l]._numRegions >= MAX_IMUSE_REGIONS) {
warning("IMuseDigital::startSound(%d) Not enough space for Region");
ptr += 8;
break;
}
_channel[l]._region[_channel[l]._numRegions]._offset = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._region[_channel[l]._numRegions]._length = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._numRegions++;
break;
case MKID_BE('STOP'):
ptr += 4;
_channel[l]._offsetStop = READ_BE_UINT32(ptr); ptr += 4;
break;
case MKID_BE('JUMP'):
ptr += 4;
if (_channel[l]._numJumps >= MAX_IMUSE_JUMPS) {
warning("IMuseDigital::startSound(%d) Not enough space for Jump");
ptr += 16;
break;
}
_channel[l]._jump[_channel[l]._numJumps]._offset = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._jump[_channel[l]._numJumps]._dest = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._jump[_channel[l]._numJumps]._id = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._jump[_channel[l]._numJumps]._numLoops = READ_BE_UINT32(ptr); ptr += 4;
_channel[l]._isJump = true;
_channel[l]._numJumps++;
break;
case MKID_BE('DATA'):
size = READ_BE_UINT32(ptr); ptr += 4;
break;
default:
error("IMuseDigital::startSound(%d) Unknown sfx header %c%c%c%c", tag>>24, tag>>16, tag>>8, tag);
}
if (tag == MKID_BE('DATA')) break;
}
// if ((sound == 131) || (sound == 123) || (sound == 122)) {
_channel[l]._isJump = false;
_channel[l]._numJumps = 0;
// }
uint32 header_size = ptr - s_ptr;
_channel[l]._offsetStop -= header_size;
if (_channel[l]._bits == 12) {
_channel[l]._offsetStop = (_channel[l]._offsetStop / 3) * 4;
}
for (r = 0; r < _channel[l]._numRegions; r++) {
_channel[l]._region[r]._offset -= header_size;
if (_channel[l]._bits == 12) {
_channel[l]._region[r]._offset = (_channel[l]._region[r]._offset / 3) * 4;
_channel[l]._region[r]._length = (_channel[l]._region[r]._length / 3) * 4;
}
}
if (_channel[l]._numJumps > 0) {
for (r = 0; r < _channel[l]._numJumps; r++) {
_channel[l]._jump[r]._offset -= header_size;
_channel[l]._jump[r]._dest -= header_size;
if (_channel[l]._bits == 12) {
_channel[l]._jump[r]._offset = (_channel[l]._jump[r]._offset / 3) * 4;
_channel[l]._jump[r]._dest = (_channel[l]._jump[r]._dest / 3) * 4;
}
}
}
_channel[l]._mixerSize = (22050 / 5) * 2;
_channel[l]._mixerFlags = SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_STEREO | SoundMixer::FLAG_REVERSE_STEREO;
if (_channel[l]._bits == 12) {
_channel[l]._mixerSize *= 2;
_channel[l]._mixerFlags |= SoundMixer::FLAG_16BITS;
_channel[l]._size = _scumm->_sound->decode12BitsSample(ptr, &_channel[l]._data, size, (_channel[l]._channels == 2) ? false : true);
}
if (_channel[l]._bits == 8) {
_channel[l]._mixerFlags |= SoundMixer::FLAG_UNSIGNED;
if (_channel[l]._channels == 1) {
size *= 2;
_channel[l]._channels = 2;
_channel[l]._data = (byte *)malloc(size);
for (t = 0; t < size / 2; t++) {
*(_channel[l]._data + t * 2 + 0) = *(ptr + t);
*(_channel[l]._data + t * 2 + 1) = *(ptr + t);
}
} else {
_channel[l]._data = (byte *)malloc(size);
memcpy(_channel[l]._data, ptr, size);
}
_channel[l]._size = size;
}
if (_channel[l]._freq == 11025) {
_channel[l]._mixerSize /= 2;
}
}
_channel[l]._toBeRemoved = false;
_channel[l]._used = true;
break;
}
}
}
void IMuseDigital::stopSound(int sound) {
debug(2, "IMuseDigital::stopSound(%d)", sound);
for (int32 l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if ((_channel[l]._idSound == sound) && (_channel[l]._used == true)) {
_channel[l]._toBeRemoved = true;
}
}
}
void IMuseDigital::stopAll() {
for (int32 l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if (_channel[l]._used == true) {
_channel[l]._toBeRemoved = true;
}
}
}
void IMuseDigital::pause(bool p) {
_pause = p;
}
int32 IMuseDigital::doCommand(int a, int b, int c, int d, int e, int f, int g, int h) {
byte cmd = a & 0xFF;
byte param = a >> 8;
int32 sample = b, r;
byte sub_cmd = c >> 8;
int8 chan = -1, l;
int8 tmp;
if (!(cmd || param))
return 1;
if (param == 0) {
switch (cmd) {
case 12:
switch (sub_cmd) {
case 5:
debug(2, "IMuseDigital::doCommand 12,5 sample(%d), param(%d)", sample, d);
return 0;
case 6: // volume control (0-127)
debug(2, "IMuseDigital::doCommand setting volume sample(%d), volume(%d)", sample, d);
for (l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if ((_channel[l]._idSound == sample) && (_channel[l]._used == true)) {
chan = l;
break;
}
}
if (chan == -1) {
debug(2, "IMuseDigital::doCommand 12,6 sample(%d) not exist in channels", sample);
return 1;
}
_channel[chan]._volume = d;
_channel[chan]._volumeRight = d;
if (_channel[chan]._volumeFade != -1) {
tmp = ((_channel[chan]._volumeFade - _channel[chan]._volume) * 2) / _channel[chan]._volumeFadeParam;
if ((tmp < 0) && (tmp > -2)) {
tmp = -1;
} else if ((tmp > 0) && (tmp < 2)) {
tmp = 1;
} else {
tmp /= 2;
}
_channel[chan]._volumeFadeStep = tmp;
}
return 0;
case 7: // right volume control (0-127)
debug(2, "IMuseDigital::doCommand setting right volume sample(%d),volume(%d)", sample, d);
for (l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if ((_channel[l]._idSound == sample) && (_channel[l]._used == true)) {
chan = l;
break;
}
}
if (chan == -1) {
debug(2, "IMuseDigital::doCommand 12,7 sample(%d) not exist in channels", sample);
return 1;
}
_channel[chan]._volumeRight = d;
return 0;
default:
warning("IMuseDigital::doCommand 12 DEFAULT sub command %d", sub_cmd);
return 1;
}
case 14:
switch (sub_cmd) {
case 6: // fade volume control
debug(2, "IMuseDigital::doCommand fading volume sample(%d),fade(%d, %d)", sample, d, e);
for (l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if ((_channel[l]._idSound == sample) && (_channel[l]._used == true)) {
chan = l;
break;
}
}
if (chan == -1) {
debug(2, "IMuseDigital::doCommand 14,6 sample %d not exist in channels", sample);
return 1;
}
_channel[chan]._volumeFade = d;
_channel[chan]._volumeFadeParam = e;
tmp = ((_channel[chan]._volumeFade - _channel[chan]._volume) * 2) / _channel[chan]._volumeFadeParam;
if ((tmp < 0) && (tmp > -2)) {
tmp = -1;
} else if ((tmp > 0) && (tmp < 2)) {
tmp = 1;
} else {
tmp /= 2;
}
_channel[chan]._volumeFadeStep = tmp;
return 0;
default:
warning("IMuseDigital::doCommand 14 DEFAULT sub command %d", sub_cmd);
return 1;
}
default:
warning("IMuseDigital::doCommand DEFAULT command %d", cmd);
return 1;
}
} else if (param == 16) {
switch (cmd) {
case 0: // play music (state)
debug(2, "IMuseDigital::doCommand 0x1000 (%d)", b);
if (_scumm->_gameId == GID_DIG) {
for(l = 0;; l++) {
if (_digStateMusicMap[l].room == -1) {
return 1;
}
if (_digStateMusicMap[l].room == b) {
int16 music = _digStateMusicMap[l].table_index;
debug(2, "Play imuse music: %s, %s, %s", _digStateMusicTable[music].name, _digStateMusicTable[music].title, _digStateMusicTable[music].filename);
if (_digStateMusicTable[music].filename[0] != 0) {
_scumm->_sound->playBundleMusic((char*)&_digStateMusicTable[music].filename);
}
return 0;
}
}
} else if (_scumm->_gameId == GID_FT) {
for(l = 0;; l++) {
if (_ftStateMusicTable[l].index == -1) {
return 1;
}
if (_ftStateMusicTable[l].index == b) {
debug(2, "Play imuse music: %s, %s", _ftStateMusicTable[l].name, _ftStateMusicTable[l].audioname);
if (_ftStateMusicTable[l].audioname[0] != 0) {
for(r = 0; r < _scumm->_numAudioNames; r++) {
if (strcmp(_ftStateMusicTable[l].audioname, &_scumm->_audioNames[r * 9]) == 0) {
startSound(r);
doCommand(12, r, 1536, _ftStateMusicTable[l].volume, 0, 0, 0, 0);
}
}
}
}
}
}
return 0;
case 1: // play music (seq)
debug(2, "IMuseDigital::doCommand 0x1001 (%d)", b);
if (_scumm->_gameId == GID_DIG) {
for(l = 0;; l++) {
if (_digSeqMusicTable[l].index == -1) {
return 1;
}
if ((_digSeqMusicTable[l].index == b)) {
debug(2, "Play imuse music: %s, %s, %s", _digSeqMusicTable[l].name, _digSeqMusicTable[l].title, _digSeqMusicTable[l].filename);
if (_digSeqMusicTable[l].filename[0] != 0) {
_scumm->_sound->playBundleMusic((char*)&_digSeqMusicTable[l].filename);
}
return 0;
}
}
} else if (_scumm->_gameId == GID_FT) {
for(l = 0;; l++) {
if (_ftSeqMusicTable[l].index == -1) {
return 1;
}
if (_ftSeqMusicTable[l].index == b) {
debug(2, "Play imuse music: %s, %s", _ftSeqMusicTable[l].name, _ftSeqMusicTable[l].audioname);
if (_ftSeqMusicTable[l].audioname[0] != 0) {
for(r = 0; r < _scumm->_numAudioNames; r++) {
if (strcmp(_ftSeqMusicTable[l].audioname, &_scumm->_audioNames[r * 9]) == 0) {
startSound(r);
doCommand(12, r, 1536, _ftSeqMusicTable[l].volume, 0, 0, 0, 0);
}
}
}
}
}
}
return 0;
case 2: // dummy in DIG and CMI
debug(2, "IMuseDigital::doCommand 0x1002 (%d)", b);
return 0;
case 3: // ??? (stream related)
debug(2, "IMuseDigital::doCommand 0x1003 (%d,%d)", b, c);
return 0;
default:
warning("IMuseDigital::doCommand (0x1xxx) DEFAULT command %d", cmd);
return 1;
}
}
return 1;
}
int IMuseDigital::getSoundStatus(int sound) {
debug(2, "IMuseDigital::getSoundStatus(%d)", sound);
for (int32 l = 0; l < MAX_DIGITAL_CHANNELS; l++) {
if ((_channel[l]._idSound == sound) && (_channel[l]._used == true)) {
return 1;
}
}
return 0;
}
|