summaryrefslogblamecommitdiffstats
path: root/private/mvdm/wow32/wmmedia2.c
blob: df5e1004326e95cc1f5d01c4032c79d04f28850a (plain) (tree)
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





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                 
/***********************************************************************\

*
*  WOW v1.0
*
*  Copyright (c) 1991, Microsoft Corporation
*
*  WMMEDIA2.C
*  WOW32 16-bit MultiMedia API support
*
*  Contains:
*       Midi IN apis
*       Midi OUT apis
*       Wave IN apis
*       Wave OUT apis
*
*
*  History:
*  Created 21-Jan-1992 by Mike Tricker (MikeTri), after jeffpar
*  Changed 15-Jul-1992 by Mike Tricker (MikeTri), fixing GetDevCaps calls
*          26-Jul-1992 by Stephen Estrop (StephenE) thunks for mciSendCommand
*          30-Jul-1992 by Mike Tricker (MikeTri), fixing Wave/Midi/MMIO
*          03-Aug-1992 by Mike Tricker (MikeTri), added proper error handling
*          08-Oct-1992 by StephenE spawned from the original wmmedia.c
*
\***********************************************************************/


//
// We define NO_STRICT so that the compiler doesn't moan and groan when
// I use the FARPROC type for the Multi-Media api loading.
//
#define NO_STRICT
#define OEMRESOURCE

#include "precomp.h"
#pragma hdrstop

#if 0


MODNAME(wmmedia2.c);

// A 32 bit ptr to the 16 bit callback data
extern PCALLBACK_DATA       pCallBackData;
extern CRITICAL_SECTION     mmCriticalSection;

#if DBG
/*
** AllocCount maintains a count of the number XXM_DONE messages that
** we expect to receive before the device is closed.  When the device is
** closed this count should be zero.
**
*/
int AllocWaveCount = 0;
int AllocMidiCount = 0;
int mmTraceWave    = 0;
int mmTraceMidi    = 0;
#endif


/* ---------------------------------------------------------------------
** MIDI Output API's
** ---------------------------------------------------------------------
*/

/**********************************************************************\
*
* WMM32midiOutGetNumDevs
*
* This function retrieves the number of MIDI output devices present
* in the system.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutGetNumDevs(PVDMFRAME pFrame)
{
    static  FARPROC mmAPI = NULL;
            ULONG   ul;

    UNREFERENCED_PARAMETER(pFrame);

    GET_MULTIMEDIA_API( "midiOutGetNumDevs", mmAPI, MMSYSERR_NODRIVER );

    trace_midi(( "midiOutGetNumDevs()" ));
    ul = GETWORD16((*mmAPI)() );
    trace_midi(( "-> %ld\n", ul ));

    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutGetDevCaps
*
* This function queries a specified MIDI output device to determine its
* capabilities.
*
*
*
*  We will now change things...
*
* Step 1: get the ENTIRE Caps structure, irrespective of the number of bytes
*         requested
*         (previously I was getting the requested number of bytes via
*         parg16->f3 (plus 2 'cos of the WORD -> UINT change for version -
*         which was wrong anyway...) )
*
* Step 2: thunk the ENTIRE structure in to a 16 bit local variable
*
* Step 3: RtlCopyMemory the REQUESTED number of bytes from the local copy
*         to the "real" structure within the app
*
* Thanks to RCBS for sorting me out once again !
*
*
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutGetDevCaps(PVDMFRAME pFrame)

{
    register PMIDIOUTGETDEVCAPS16   parg16;
    static   FARPROC                mmAPI = NULL;
             ULONG                  ul;
             MIDIOUTCAPS            midioutcaps;

    GET_MULTIMEDIA_API( "midiOutGetDevCapsA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTGETDEVCAPS16), parg16);

    trace_midi(( "midiOutGetDevCaps( %x, %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));


    /*
    ** If the size parameter was zero return straight away.  Note that this
    ** is not an error.
    */
    if ( UINT32( parg16->f3 ) == 0 ) {
        ul = MMSYSERR_NOERROR;
    }
    else {

        ul = GETWORD16((*mmAPI)( INT32(parg16->f1), &midioutcaps,
                                 sizeof(MIDIOUTCAPS) ));
        /*
        ** This must now thunk the ENTIRE structure, then copy parg16->f3
        ** bytes onto the "real" structure in the app, but only if the call
        ** returned success.
        */
        if ( ul == MMSYSERR_NOERROR ) {
            ul = PUTMIDIOUTCAPS16(parg16->f2, &midioutcaps, UINT32(parg16->f3));
        }
    }
    trace_midi(( "-> %ld\n", ul ));


    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutGetErrorText
*
* This function retrieves a textual description of the error
* identified by the specified error number.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutGetErrorText(PVDMFRAME pFrame)
{
    register PMIDIOUTGETERRORTEXT16 parg16;
    static   FARPROC                mmAPI = NULL;
             ULONG                  ul = MMSYSERR_NOERROR;
             PSZ                    pszText;

    GET_MULTIMEDIA_API( "midiOutGetErrorTextA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTGETERRORTEXT16), parg16);

    trace_midi(( "midiOutGetErrorText( %x, %x, %x )", UINT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** Test against a zero length string and a NULL pointer.  If 0 is passed
    ** as the buffer length then the manual says we should return
    ** MMSYSERR_NOERR.  MMGETOPTPTR only returns a pointer if parg16->f2 is
    ** not NULL.
    */
    MMGETOPTPTR( parg16->f2, UINT32(parg16->f3), pszText );
    if ( pszText != NULL ) {

        ul = GETWORD16((*mmAPI)( UINT32(parg16->f1), pszText,
                                 UINT32(parg16->f3) ));

        FLUSHVDMPTR(DWORD32(parg16->f2), UINT32(parg16->f3), pszText);
        FREEVDMPTR(pszText);
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/***********************************************************************\
*
* WMM32midiOutOpen
*
* This function opens a specified MIDI output device for playback.
*
\***********************************************************************/
ULONG FASTCALL WMM32midiOutOpen(PVDMFRAME pFrame)
{
    register PMIDIOUTOPEN16 parg16;
    static   FARPROC        mmAPI = NULL;

    ULONG         ul = MMSYSERR_NOERROR;
    UINT          uDevID;
    PINSTANCEDATA pInstanceData;
    LPHMIDIOUT    lpHMidiOut;        // pointer to handle in 16 bit app space
    HMIDIOUT      Hand32;            // 32bit handle

    GET_MULTIMEDIA_API( "midiOutOpen", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTOPEN16), parg16);

    trace_midi(( "midiOutOpen( %x, %x, %x, %x, %x )",
                 DWORD32( parg16->f1 ), INT32  ( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ),
                 DWORD32( parg16->f5 ) ));

    /*
    ** Get the device ID. We use INT32 here not UINT32 to make sure that
    ** negative values (such as MIDI_MAPPER (-1)) get thunked correctly.
    */
    uDevID = (UINT)INT32(parg16->f2);

    /*
    ** Map the 16 bit pointer is one was specified.
    */
    MMGETOPTPTR( parg16->f1, sizeof(HMIDIOUT16), lpHMidiOut );
    if ( lpHMidiOut ) {

        /*
        ** Create InstanceData block to be used by our callback routine.
        **
        ** NOTE: Although we malloc it here we don't free it.
        ** This is not a mistake - it must not be freed before the
        ** callback routine has used it - so it does the freeing.
        **
        ** If the malloc fails we bomb down to the bottom,
        ** set ul to MMSYSERR_NOMEM and exit gracefully.
        **
        ** We always have a callback functions.  This is to ensure that
        ** the MIDIHDR structure keeps getting copied back from
        ** 32 bit space to 16 bit, as it contains flags which
        ** applications are liable to keep checking.
        */
        if ( pInstanceData = malloc_w(sizeof(INSTANCEDATA)) ) {

            dprintf2(( "WM32midiOutOpen: Allocated instance buffer at %8X",
                       pInstanceData ));
            pInstanceData->dwCallback         = DWORD32(parg16->f3);
            pInstanceData->dwCallbackInstance = DWORD32(parg16->f4);
            pInstanceData->dwFlags            = DWORD32(parg16->f5);

            ul = GETWORD16((*mmAPI)( &Hand32, uDevID,
                                     (DWORD)W32CommonDeviceOpen,
                                     (DWORD)pInstanceData,
                                     CALLBACK_FUNCTION ));

        }
        else {
            ul = MMSYSERR_NOMEM;
        }

        /*
        ** If the call returns success update the 16 bit handle,
        ** otherwise don't, and free the memory we malloc'd earlier, as
        ** the callback that would have freed it will never get callled.
        */
        if ( ul == MMSYSERR_NOERROR ) {

                HMIDIOUT16 Hand16 = GETHMIDIOUT16(Hand32);

                trace_midi(( "Handle -> %x", Hand16 ));

                STOREWORD  ( *lpHMidiOut, Hand16 );
                FLUSHVDMPTR( DWORD32(parg16->f1), sizeof(HMIDIOUT16),
                             lpHMidiOut );
        }

        /*
        ** We only free the memory if we actually allocated any
        */
        else if ( pInstanceData ) {

            free_w(pInstanceData);
        }

        /*
        ** Regardless of sucess or failure we need to free the pointer
        ** to the 16 bit MidiIn handle.
        */
        FREEVDMPTR ( lpHMidiOut );

    }
    else {

        ul = MMSYSERR_INVALPARAM;
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32midiOutClose
*
* This function closes the specified MIDI output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutClose(PVDMFRAME pFrame)
{
    register PMIDIOUTCLOSE16 parg16;
    static   FARPROC         mmAPI = NULL;
             ULONG           ul;

    GET_MULTIMEDIA_API( "midiOutClose", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTCLOSE16), parg16);

    trace_midi(( "midiOutClose( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16( (*mmAPI)(HMIDIOUT32(parg16->f1) ));
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32midiOutPrepareHeader
*
* This function prepares the specified midiform header.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutPrepareHeader(PVDMFRAME pFrame)
{
    register PMIDIOUTPREPAREHEADER3216 parg16;
    static   FARPROC                   mmAPI = NULL;
             ULONG                     ul;
             MIDIHDR                   midihdr;


    GET_MULTIMEDIA_API( "midiOutPrepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(MIDIOUTPREPAREHEADER3216), parg16);
    trace_midi(( "midiOutPrepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETMIDIHDR16(parg16->f2, &midihdr);

    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1),
                             &midihdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    **
    */
    if ( !ul ) {
        PUTMIDIHDR16(parg16->f2, &midihdr);
    }

    trace_midi(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutUnprepareHeader
*
* This function prepares the specified midiform header.
* This function cleans up the preparation performed by midiOutPrepareHeader.
* The function must be called after the device driver has finished with a
* data block. You must call this function before freeing the data buffer.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutUnprepareHeader(PVDMFRAME pFrame)
{
    register PMIDIOUTUNPREPAREHEADER3216  parg16;
    static   FARPROC                      mmAPI = NULL;
             ULONG                        ul;
             MIDIHDR                      midihdr;

    GET_MULTIMEDIA_API( "midiOutUnprepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(MIDIOUTUNPREPAREHEADER3216), parg16);
    trace_midi(( "midiOutUnprepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETMIDIHDR16(parg16->f2, &midihdr);

    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1),
                             &midihdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    */
    if (!ul) {
        PUTMIDIHDR16(parg16->f2, &midihdr);
    }

    trace_midi(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32midiOutShortMsg
*
* This function sends a short MIDI message to the specified MIDI output device.
* Use this function to send any MIDI message except for system exclusive
* messages.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutShortMsg(PVDMFRAME pFrame)
{
    register PMIDIOUTSHORTMSG16 parg16;
    static   FARPROC            mmAPI = NULL;
             ULONG              ul;

    GET_MULTIMEDIA_API( "midiOutShortMsg", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTSHORTMSG16), parg16);

    trace_midi(( "midiOutShortMsg( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1), DWORD32(parg16->f2) ));
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutLongMsg
*
* This function sends a long MIDI message to the specified MIDI output
* device.  Use this function to send system exclusive messages or to
* send a buffer filled with short messages.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutLongMsg(PVDMFRAME pFrame)
{
    register PMIDIOUTLONGMSG16  parg16;
    static   FARPROC            mmAPI = NULL;
             ULONG              ul;
             PMIDIHDR32         pMidihdr32;

    GET_MULTIMEDIA_API( "midiOutLongMsg", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTLONGMSG16), parg16);

    trace_midi(( "midiOutLongMsg( %x, %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the MIDIHDR structure is too small
    ** or the lphdr is invalid return an error
    **
    */
    if ( UINT32(parg16->f3) < sizeof(MIDIHDR16)
      || HIWORD( DWORD32(parg16->f2) ) == 0 ) {

        ul = MMSYSERR_INVALPARAM;
    }
    else {
        if ( pMidihdr32 = malloc_w(sizeof(MIDIHDR32)) ) {

            PMIDIHDR   lpwhdr;
#if DBG
            AllocMidiCount++;
            dprintf2(( "M>> %8X (%d)", pMidihdr32, AllocMidiCount ));
#endif
            /* Copy across the midi header stuff.  Note that lpwhdr (a
            ** 32 bit ptr to a 32 bit midi header) is used to make the
            ** pointer stuff a bit less hairy.
            **
            ** pMidihdr32->pMidihdr32 is a 32 bit ptr to a 16 bit midi header
            ** pMidihdr32->pMidihdr16 is a 16 bit ptr to a 16 bit midi header
            ** pMidihdr32->Midihdr    is a 32 bit midi header
            */
            lpwhdr = &(pMidihdr32->Midihdr);
            pMidihdr32->pMidihdr16 = (PMIDIHDR16)DWORD32(parg16->f2);
            pMidihdr32->pMidihdr32 = GETMIDIHDR16(DWORD32(parg16->f2), lpwhdr);

            /*
            ** GETMIDIHDR16 can return NULL, in which case we should set
            ** lpwhdr to NULL too and call midiOutLongMessage only to get the
            ** correct error code.
            */
            if ( pMidihdr32->pMidihdr32 == NULL ) {
                lpwhdr = NULL;
            }

            ul = GETWORD16( (*mmAPI)( HMIDIOUT32(parg16->f1), lpwhdr,
                                      UINT32(parg16->f3) ) );
            /*
            ** If the call fails we need to free the memory we malloc'd
            ** above, as the callback that would have freed it will never
            ** get called.
            **
            */
            if ( ul == MMSYSERR_NOERROR ) {

                /*
                ** Make sure we reflect any changes that midiOutLongMessage did
                ** to the MIDIHDR back to 16 bit land.
                **
                ** This is important because some apps poll the
                ** MHDR_DONE bit !!
                */
                COPY_MIDIOUTHDR16_FLAGS( pMidihdr32->pMidihdr32,
                                         pMidihdr32->Midihdr );
            }
            else {
#if DBG
                AllocMidiCount--;
                dprintf2(( "M<< \t%8X (%d)", pMidihdr32,
                            AllocMidiCount ));
#endif
                free_w( pMidihdr32 );
            }
        }
        else {
            ul = MMSYSERR_NOMEM;
        }
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutReset
*
* This function turns off all notes on al MIDI channels for the specified
* MIDI output deice.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutReset(PVDMFRAME pFrame)
{
    register PMIDIOUTRESET16   parg16;
    static   FARPROC           mmAPI = NULL;
             ULONG             ul;

    GET_MULTIMEDIA_API( "midiOutReset", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTRESET16), parg16);

    trace_midi(( "midiOutReset( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1) ));
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutGetVolume
*
* This function returns the current volume setting of a MIDI output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutGetVolume(PVDMFRAME pFrame)
{
    register PMIDIOUTGETVOLUME16 parg16;
    static   FARPROC             mmAPI = NULL;
             ULONG               ul;
             LPDWORD             lpdwVolume;
             DWORD               dwVolume;

    GET_MULTIMEDIA_API( "midiOutGetVolume", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTGETVOLUME16), parg16);

    trace_midi(( "midiOutGetVolume( %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( INT32(parg16->f1), &dwVolume ));
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(DWORD), lpdwVolume);

        if ( lpdwVolume ) {
            STOREDWORD ( *lpdwVolume, dwVolume );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(DWORD), lpdwVolume );
            FREEVDMPTR ( lpdwVolume );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutSetVolume
*
* This function sets the volume of a MIDI output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutSetVolume(PVDMFRAME pFrame)
{
    register PMIDIOUTSETVOLUME16 parg16;
    static   FARPROC             mmAPI = NULL;
             ULONG               ul;

    GET_MULTIMEDIA_API( "midiOutSetVolume", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTSETVOLUME16), parg16);

    trace_midi(( "midiOutSetVolume( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));
    ul = GETWORD16((*mmAPI)( INT32(parg16->f1), DWORD32(parg16->f2) ));
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutCachePatches
*
* This function requests that an internal MIDI synthesizer device preload a
* specified set of patches. Some synthesizers are not capable of keeping all
* patches loaded simultaneously and must load data from disk when they receive
* MIDI program change messages. Caching patches ensures specified patches are
* immediately available.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutCachePatches(PVDMFRAME pFrame)
{
    register PMIDIOUTCACHEPATCHES16 parg16;
    static   FARPROC                mmAPI = NULL;
             ULONG                  ul = MMSYSERR_INVALPARAM;
             LPPATCHARRAY           lppa1;

    GET_MULTIMEDIA_API( "midiOutCachePatches", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTCACHEPATCHES16), parg16);

    trace_midi(( "midiOutCachePatches( %x, %x, %x, %x )", WORD32( parg16->f1 ),
                 UINT32( parg16->f2 ), DWORD32( parg16->f3 ),
                 UINT32( parg16->f4 ) ));

    /*
    ** GETMISCPTR checks that parg16->f3 is not zero so we need not bother.
    */
    GETMISCPTR( DWORD32( parg16->f3 ), lppa1 );

    if ( lppa1 ) {

        ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1), UINT32(parg16->f2),
                                 lppa1, UINT32(parg16->f4) ));
        FREEMISCPTR( lppa1 );

    }
    else  {
        dprintf1(( "WMM32midiOutCachePatches passed a NULL pointer" ));
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutCacheDrumPatches
*
* This function requests that an internal MIDI synthesizer device preload a
* specified set of key-based percussion patches. Some synthesizers are not
* capable of keeping all percussion patches loaded simultaneously. Caching
* patches ensures specified patches are immediately available.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutCacheDrumPatches(PVDMFRAME pFrame)
{
    register PMIDIOUTCACHEDRUMPATCHES16 parg16;
    static   FARPROC                    mmAPI = NULL;
             ULONG                      ul = MMSYSERR_INVALPARAM;
             LPKEYARRAY                 lpka1;

    GET_MULTIMEDIA_API( "midiOutCacheDrumPatches", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTCACHEDRUMPATCHES16), parg16);
    trace_midi(( "midiOutCacheDrumPatches( %x, %x, %x, %x )",
                 WORD32( parg16->f1 ), UINT32( parg16->f2 ),
                 DWORD32( parg16->f3 ), UINT32( parg16->f4 ) ));

    GETMISCPTR( DWORD32( parg16->f3 ), lpka1 );
    if ( lpka1 ) {

        ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1), UINT32(parg16->f2),
                                 lpka1, UINT32(parg16->f4) ) );
        FREEMISCPTR(lpka1);
    }
    else  {
        dprintf1(( "WMM32midiOutCacheDrumPatches passed a NULL pointer" ));
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutGetID
*
* This function gets the device ID for a MIDI output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutGetID(PVDMFRAME pFrame)
{
    register PMIDIOUTGETID16    parg16;
    static   FARPROC            mmAPI = NULL;
             ULONG              ul;
             UINT               dwDeviceID32;
             LPWORD             lpwDeviceID16;

    GET_MULTIMEDIA_API( "midiOutGetID", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTGETID16), parg16);

    trace_midi(( "midiOutGetID( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( (HMIDIOUT)HMIDIOUT32(parg16->f1), &dwDeviceID32 ));

    /*
    ** Only copy the ID back to 16 bit space if the call was sucessful
    **
    */
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(WORD), lpwDeviceID16 );

        if ( lpwDeviceID16 ) {
            STOREWORD  ( *lpwDeviceID16, dwDeviceID32 );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(WORD), lpwDeviceID16 );
            FREEVDMPTR ( lpwDeviceID16 );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiOutMessage
*
* This function sends a message to the specified MIDI output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiOutMessage(PVDMFRAME pFrame)
{
    register PMIDIOUTMESSAGE3216 parg16;
    static   FARPROC             mmAPI = NULL;
             ULONG               ul;

    GET_MULTIMEDIA_API( "midiOutMessage", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIOUTMESSAGE16), parg16);

    trace_midi(( "midiOutMessage( %x, %x, %x, %x )",
                 WORD32( parg16->f1 ),   UINT32( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ) ));

    if ( (UINT32(parg16->f2) >= DRV_BUFFER_LOW)
      && (UINT32(parg16->f2) <= DRV_BUFFER_HIGH) ) {

        LPDWORD     lpdwParam1;
        GETMISCPTR(parg16->f3, lpdwParam1);

        ul = GETDWORD16((*mmAPI)( HMIDIOUT32(parg16->f1), UINT32(parg16->f2),
                                  (DWORD)lpdwParam1, DWORD32(parg16->f4)));
        FREEMISCPTR(lpdwParam1);

    } else {
        ul = GETDWORD16((*mmAPI)( HMIDIOUT32(parg16->f1),
                                  MAKELONG( WORD32(parg16->f2), 0xFFFF ),
                                  DWORD32(parg16->f3), DWORD32(parg16->f4) ));
    }

    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/* ---------------------------------------------------------------------
** MIDI Input API's
** ---------------------------------------------------------------------
*/


/**********************************************************************\
*
* WMM32midiInGetNumDevs
*
* This function retrieves the number of MIDI input devices in the system.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInGetNumDevs(PVDMFRAME pFrame)
{
    static  FARPROC mmAPI = NULL;
            ULONG   ul;

    GET_MULTIMEDIA_API( "midiInGetNumDevs", mmAPI, MMSYSERR_NODRIVER );

    UNREFERENCED_PARAMETER(pFrame);

    trace_midi(( "midiInGetNumDevs()" ));
    ul = GETWORD16((*mmAPI)() );
    trace_midi(( "-> %ld\n", ul ));

    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInGetDevCaps
*
* This function queries a specified MIDI input device to determine its
* capabilities.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInGetDevCaps(PVDMFRAME pFrame)
{
    ULONG ul;
    MIDIINCAPS midiincaps1;
    register PMIDIINGETDEVCAPS16 parg16;
    static   FARPROC             mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInGetDevCapsA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINGETDEVCAPS16), parg16);

    trace_midi(( "midiInGetDevCaps( %x, %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));
    if ( UINT32( parg16->f3 ) == 0 ) {
        ul = MMSYSERR_NOERROR;
    }
    else {

        ul = GETWORD16((*mmAPI)(INT32(parg16->f1), &midiincaps1,
                                sizeof(MIDIINCAPS) ) );
        /*
        ** This must now thunk the ENTIRE structure, then copy parg16->f3
        ** bytes onto the "real" structure in the app, but only if the
        ** call returned success.
        */
        if ( ul == MMSYSERR_NOERROR ) {
            ul = PUTMIDIINCAPS16( parg16->f2, &midiincaps1, UINT32(parg16->f3) );
        }
    }
    trace_midi(( "-> %ld\n", ul ));


    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInGetErrorText
*
* This function retrieves a textual description of the error identified by the
* specified error number.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInGetErrorText(PVDMFRAME pFrame)
{
    register PMIDIINGETERRORTEXT16  parg16;
             ULONG                  ul = MMSYSERR_NOERROR;
             PSZ                    pszText;
    static   FARPROC                mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInGetErrorTextA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR( pFrame, sizeof(MIDIINGETERRORTEXT16), parg16 );

    trace_midi(( "midiInGetErrorText( %x, %x, %x )", UINT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));
    /*
    ** Test against a zero length string and a NULL pointer.  If 0 is passed
    ** as the buffer length then the manual says we should return
    ** MMSYSERR_NOERR.
    */
    MMGETOPTPTR( parg16->f2, UINT32(parg16->f3), pszText );
    if ( pszText != NULL ) {

        ul = GETWORD16( (*mmAPI)( UINT32(parg16->f1), pszText,
                                  UINT32(parg16->f3) ) );

        FLUSHVDMPTR( DWORD32(parg16->f2), UINT32(parg16->f3), pszText);
        FREEVDMPTR( pszText );
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}



/**********************************************************************\
*
* WMM32midiInOpen
*
* This function opens a specified MIDI input device.
*
\***********************************************************************/
ULONG FASTCALL WMM32midiInOpen(PVDMFRAME pFrame)
{
    ULONG           ul=0;
    UINT            uDevID;
    PINSTANCEDATA   pInstanceData;
    LPHMIDIIN       lpHMidiIn;      // pointer to handle in 16 bit app space
    HMIDIIN         Hand32;         // 32bit handle
    register PMIDIINOPEN16 parg16;
    static   FARPROC       mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInOpen", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINOPEN16), parg16);

    trace_midi(( "midiInOpen( %x, %x, %x, %x, %x )",
                 DWORD32( parg16->f1 ), INT32  ( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ),
                 DWORD32( parg16->f5 ) ));

    /*
    ** Get the device ID. We use INT32 here not UINT32 to make sure that
    ** negative values (such as MIDI_MAPPER (-1)) get thunked correctly.
    */
    uDevID = (UINT)INT32(parg16->f2);

    /*
    ** Map the 16 bit pointer is one was specified.
    */
    MMGETOPTPTR( parg16->f1, sizeof(HMIDIIN), lpHMidiIn );
    if ( lpHMidiIn ) {

        /*
        ** Create InstanceData block to be used by our callback routine.
        **
        ** NOTE: Although we malloc it here we don't free it.
        **       This is not a mistake - it must not be freed before the
        **       callback routine has used it - so it does the freeing.
        **
        ** If the malloc fails we bomb down to the bottom,
        ** set ul to MMSYSERR_NOMEM and exit gracefully.
        **
        ** We always have a callback functions.  This is to ensure that
        ** the MIDIHDR structure keeps getting copied back from
        ** 32 bit space to 16 bit, as it contains flags which
        ** applications are liable to keep checking.
        */
        if ( pInstanceData = malloc_w(sizeof(INSTANCEDATA) ) ) {

            dprintf2(( "WM32midiInOpen: Allocated instance buffer at %8X",
                       pInstanceData ));
            pInstanceData->dwCallback         = DWORD32(parg16->f3);
            pInstanceData->dwCallbackInstance = DWORD32(parg16->f4);
            pInstanceData->dwFlags            = DWORD32(parg16->f5);

            ul = GETWORD16((*mmAPI)( &Hand32, uDevID,
                                     (DWORD)W32CommonDeviceOpen,
                                     (DWORD)pInstanceData,
                                     CALLBACK_FUNCTION ));

        }
        else {

            ul = (ULONG)MMSYSERR_NOMEM;
        }

        /*
        ** If the call returns success update the 16 bit handle,
        ** otherwise don't, and free the memory we malloc'd earlier, as
        ** the callback that would have freed it will never get callled.
        */
        if ( ul == MMSYSERR_NOERROR ) {

            HMIDIIN16 Hand16 = GETHMIDIIN16(Hand32);

            trace_midi(( "Handle -> %x", Hand16 ));
            STOREWORD  ( *lpHMidiIn, Hand16 );
            FLUSHVDMPTR( DWORD32(parg16->f1), sizeof(HMIDIIN), lpHMidiIn );
        }

        /*
        ** We only free the memory if we actually allocated any and the
        ** 32 bit call failed.
        */
        else if ( pInstanceData ) {

            free_w(pInstanceData);
        }

        /*
        ** Regardless of sucess or failure we need to free the pointer
        ** to the 16 bit MidiIn handle.
        */
        FREEVDMPTR ( lpHMidiIn );

    }
    else {
        ul = MMSYSERR_INVALPARAM;
    }

    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInClose
*
* This function closes the specified MIDI input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInClose(PVDMFRAME pFrame)
{
    ULONG ul;
    register PMIDIINCLOSE16 parg16;
    static   FARPROC        mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInClose", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINCLOSE16), parg16);

    trace_midi(( "midiInClose( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)(HMIDIIN32(parg16->f1) ) );
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32midiInPrepareHeader
*
* This function prepares the specified midiform header.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInPrepareHeader(PVDMFRAME pFrame)
{
    register PMIDIOUTPREPAREHEADER3216 parg16;
    static   FARPROC                   mmAPI = NULL;
             ULONG                     ul;
             MIDIHDR                   midihdr;


    GET_MULTIMEDIA_API( "midiInPrepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(MIDIOUTPREPAREHEADER3216), parg16);
    trace_midi(( "midiInPrepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETMIDIHDR16(parg16->f2, &midihdr);

    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1),
                             &midihdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    **
    */
    if ( !ul ) {
        PUTMIDIHDR16(parg16->f2, &midihdr);
    }

    trace_midi(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInUnprepareHeader
*
* This function prepares the specified midiform header.
* This function cleans up the preparation performed by midiInPrepareHeader.
* The function must be called after the device driver has finished with a
* data block. You must call this function before freeing the data buffer.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInUnprepareHeader(PVDMFRAME pFrame)
{
    register PMIDIOUTUNPREPAREHEADER3216  parg16;
    static   FARPROC                      mmAPI = NULL;
             ULONG                        ul;
             MIDIHDR                      midihdr;

    GET_MULTIMEDIA_API( "midiInUnprepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(MIDIOUTUNPREPAREHEADER3216), parg16);
    trace_midi(( "midiInUnprepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETMIDIHDR16(parg16->f2, &midihdr);

    ul = GETWORD16((*mmAPI)( HMIDIOUT32(parg16->f1),
                             &midihdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    */
    if (!ul) {
        PUTMIDIHDR16(parg16->f2, &midihdr);
    }

    trace_midi(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInAddBuffer
*
* This function sends an input buffer to a specified opened MIDI input device.
* When the buffer is filled, it is sent back to the application. Input buffers
* are used only for system exclusive messages.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInAddBuffer(PVDMFRAME pFrame)
{
    ULONG ul;
    PMIDIHDR32 pMidihdr32;
    register PMIDIINADDBUFFER16 parg16;
    static   FARPROC            mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInAddBuffer", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINADDBUFFER16), parg16);

    trace_midi(( "midiInAddBuffer( %x, %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the MIDIHDR structure is too small
    ** or the lphdr is invalid return an error
    **
    */
    if ( UINT32(parg16->f3) < sizeof(MIDIHDR16)
      || HIWORD( DWORD32(parg16->f2) ) == 0 ) {

        ul = (ULONG)MMSYSERR_INVALPARAM;
    }
    else  {
        if (pMidihdr32 = malloc_w(sizeof(MIDIHDR32) ) ) {

            PMIDIHDR   lpwhdr;
#if DBG
            AllocMidiCount++;
            dprintf2(( "M>> %8X (%d)", pMidihdr32, AllocMidiCount ));
#endif
            /* Copy across the midi header stuff.  Note that lpwhdr (a
            ** 32 bit ptr to a 32 bit midi header) is used to make the
            ** pointer stuff a bit less hairy.
            **
            ** pMidihdr32->Midihdr    is a 32 bit midi header
            ** pMidihdr32->pMidihdr16 is a 16 bit ptr to a 16 bit midi header
            ** pMidihdr32->pMidihdr32 is a 32 bit ptr to a 16 bit midi header
            */
            lpwhdr = &(pMidihdr32->Midihdr);
            pMidihdr32->pMidihdr16 = (PMIDIHDR16)DWORD32(parg16->f2);
            pMidihdr32->pMidihdr32 = GETMIDIHDR16(DWORD32(parg16->f2), lpwhdr);

            /*
            ** GETMIDIHDR16 can return NULL, in which case we should set
            ** lpwhdr to NULL too and call midiInAddBuffer only to get the
            ** correct error code.
            */
            if ( pMidihdr32->pMidihdr32 == NULL ) {
                lpwhdr = NULL;
            }

            ul = GETWORD16((*mmAPI)( HMIDIIN32(parg16->f1), lpwhdr,
                                     UINT32(parg16->f3) ));
            /*
            ** If the call fails we need to free the memory we malloc'd
            ** above, as the callback that would have freed it will never
            ** get called.
            **
            */
            if ( ul == MMSYSERR_NOERROR ) {

                /*
                ** Make sure we reflect any changes that midiInAddBuffer did
                ** to the MIDIHDR back to 16 bit land.
                **
                ** This is important because some apps poll the
                ** MHDR_DONE bit !!
                */
                COPY_MIDIINHDR16_FLAGS( pMidihdr32->pMidihdr32,
                                        pMidihdr32->Midihdr );
            }
            else {
#if DBG
                AllocMidiCount--;
                dprintf2(( "M<< \t%8X (%d)", pMidihdr32,
                            AllocMidiCount ));
#endif
                free_w( pMidihdr32 );
            }
        }
        else {
            ul = (ULONG)MMSYSERR_NOMEM;
        }
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInStart
*
* This function starts MIDI input on the specified MIDI input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInStart(PVDMFRAME pFrame)
{
    ULONG ul;
    register PMIDIINSTART16 parg16;
    static   FARPROC        mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInStart", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINSTART16), parg16);

    trace_midi(( "midiInStart( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)(HMIDIIN32(parg16->f1) ) );
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInStop
*
* This function terminates MIDI input on the specified MIDI input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInStop(PVDMFRAME pFrame)
{
    ULONG ul;
    register PMIDIINSTOP16 parg16;
    static   FARPROC        mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInStop", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINSTOP16), parg16);

    trace_midi(( "midiInStop( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)(HMIDIIN32(parg16->f1) ) );
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInReset
*
* This function stops input on a given MIDI input device and marks all pending
* input buffers as done.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInReset(PVDMFRAME pFrame)
{
    ULONG ul;
    register PMIDIINRESET16 parg16;
    static   FARPROC        mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInReset", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINRESET16), parg16);

    trace_midi(( "midiInReset( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)(HMIDIIN32(parg16->f1) ) );
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInGetID
*
* This function gets the device ID for a MIDI input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInGetID(PVDMFRAME pFrame)
{
    register PMIDIINGETID16 parg16;
    static   FARPROC        mmAPI = NULL;
             ULONG          ul;
             UINT           dwDeviceID32;
             LPWORD         lpwDeviceID16;

    GET_MULTIMEDIA_API( "midiInGetID", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINGETID16), parg16);

    trace_midi(( "midiInGetID( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HMIDIIN32(parg16->f1), &dwDeviceID32 ));

    /*
    ** Only copy the ID back to 16 bit space if the call was sucessful
    **
    */
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(WORD), lpwDeviceID16 );

        if ( lpwDeviceID16 ) {
            STOREWORD  ( *lpwDeviceID16, dwDeviceID32 );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(WORD), lpwDeviceID16 );
            FREEVDMPTR ( lpwDeviceID16 );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }
    trace_midi(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32midiInMessage
*
* This function sends a message to the specified MIDI input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32midiInMessage(PVDMFRAME pFrame)
{
    ULONG ul;
    register PMIDIINMESSAGE3216 parg16;
    static   FARPROC          mmAPI = NULL;

    GET_MULTIMEDIA_API( "midiInMessage", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(MIDIINMESSAGE16), parg16);

    trace_midi(( "midiInMessage( %x, %x, %x, %x )",
                 WORD32( parg16->f1 ),   UINT32( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ) ));

    if ( (UINT32(parg16->f2) >= DRV_BUFFER_LOW)
      && (UINT32(parg16->f2) <= DRV_BUFFER_HIGH) ) {

        LPDWORD     lpdwParam1;
        GETMISCPTR(parg16->f3, lpdwParam1);

        ul = GETDWORD16((*mmAPI)( HMIDIIN32(parg16->f1), UINT32(parg16->f2),
                                  (DWORD)lpdwParam1, DWORD32(parg16->f4)));
        FREEMISCPTR(lpdwParam1);

    } else {
        ul = GETDWORD16((*mmAPI)( HMIDIIN32(parg16->f1),
                                  MAKELONG( WORD32(parg16->f2), 0xFFFF ),
                                  DWORD32(parg16->f3), DWORD32(parg16->f4) ));
    }

    trace_midi(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}


/* ---------------------------------------------------------------------
** WAVE Output API's
** ---------------------------------------------------------------------
*/

/**********************************************************************\
*
* WMM32waveOutGetNumDevs
*
* This function retrieves the number of waveform output devices present in the
* system.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetNumDevs(PVDMFRAME pFrame)
{
    static  FARPROC mmAPI = NULL;
            ULONG   ul;

    GET_MULTIMEDIA_API( "waveOutGetNumDevs", mmAPI, MMSYSERR_NODRIVER );

    trace_wave(( "waveOutGetNumDevs()" ));
    ul = GETWORD16((*mmAPI)() );
    trace_wave(( "-> %ld\n", ul ));

    RETURN(ul);

    UNREFERENCED_PARAMETER(pFrame);
}

/**********************************************************************\
*
* WMM32waveOutGetDevCaps
*
* This function queries a specified waveform device to determine its
* capabilities.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetDevCaps(PVDMFRAME pFrame)
{
    register PWAVEOUTGETDEVCAPS16 parg16;
    static   FARPROC              mmAPI = NULL;
             ULONG                ul;
             WAVEOUTCAPS          waveoutcaps;

    GET_MULTIMEDIA_API( "waveOutGetDevCapsA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETDEVCAPS16), parg16);

    trace_wave(( "waveOutGetDevCaps( %x, %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the size parameter was zero return straight away.  Note that this
    ** is not an error.
    */
    if ( UINT32( parg16->f3 ) == 0 ) {
        ul = MMSYSERR_NOERROR;
    }
    else {

        ul = GETWORD16((*mmAPI)( INT32(parg16->f1), &waveoutcaps,
                                 sizeof(WAVEOUTCAPS) ));
        /*
        ** Don't update the 16 bit structure if the call failed
        **
        */
        if ( ul == MMSYSERR_NOERROR ) {
            ul = PUTWAVEOUTCAPS16(parg16->f2, &waveoutcaps, UINT32(parg16->f3));
        }
    }

    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);

    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutGetErrorText
*
* This function retrieves a textual description of the error identified by the
* specified error number.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetErrorText(PVDMFRAME pFrame)
{
    register    PWAVEOUTGETERRORTEXT16  parg16;
    static      FARPROC                 mmAPI = NULL;
                ULONG                   ul = MMSYSERR_NOERROR;
                PSZ                     pszText;

    GET_MULTIMEDIA_API( "waveOutGetErrorTextA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETERRORTEXT16), parg16);

    trace_wave(( "waveOutGetErrorText( %x, %x, %x )", UINT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** Test against a zero length string and a NULL pointer.  If 0 is passed
    ** as the buffer length then the manual says we should return
    ** MMSYSERR_NOERR.  MMGETOPTPTR only returns a pointer if parg16->f2 is
    ** not NULL.
    */
    MMGETOPTPTR( parg16->f2, UINT32(parg16->f3), pszText );
    if ( pszText != NULL ) {

        ul = GETWORD16( (*mmAPI)( UINT32(parg16->f1), pszText,
                                  UINT32(parg16->f3) ) );

        FLUSHVDMPTR( DWORD32(parg16->f2), UINT32(parg16->f3), pszText);
        FREEVDMPTR(pszText);
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutOpen
*
* This function opens a specified waveform output device for playback.
*
* As of November 1992 we map the 16 bit Wave Format data directly to the
* the 32 bit side, no thunking of the parameters is performed.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutOpen(PVDMFRAME pFrame)
{
    ULONG           ul = MMSYSERR_NOERROR;
    UINT            uDevID;
    PINSTANCEDATA   pInstanceData = NULL;
    DWORD           dwFlags;
    PWAVEFORMAT16   lpWaveformData;
    LPHWAVEOUT      lphWaveOut = NULL; // pointer to handle in 16 bit app space
    HWAVEOUT        Hand32;            // 32bit handle
    register PWAVEOUTOPEN16 parg16;
    static   FARPROC        mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutOpen", mmAPI, MMSYSERR_NODRIVER );


    GETARGPTR(pFrame, sizeof(WAVEOUTOPEN16), parg16);

    trace_wave(( "waveOutOpen( %x, %x, %x, %x, %x, %x )",
                 DWORD32( parg16->f1 ), INT32  ( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ),
                 DWORD32( parg16->f5 ), DWORD32( parg16->f6 ) ));

    /*
    ** Get the device ID. We use INT32 here not UINT32 to make sure that
    ** negative values (such as WAVE_MAPPER (-1)) get thunked correctly.
    ** Also, get the flags to be used.
    */
    uDevID = (UINT)INT32(parg16->f2);
    dwFlags = DWORD32(parg16->f6);


    /*
    ** Get a pointer to the WAVEFORMAT structure.  Because the format of this
    ** structure is exactly the same in 32 and 16 bit land I will use
    ** GETMISCPTR to get a generic pointer to the data.  The stuff being
    ** pointed to could be full of unaligned WORDs, but the 32 bit code
    ** would have to cope with this anyway.
    */
    GETMISCPTR( parg16->f3, lpWaveformData );
    if ( lpWaveformData == (PWAVEFORMAT16)NULL ) {
        ul = (ULONG)MMSYSERR_INVALPARAM;
        goto exit_function;
    }


    /*
    ** We don't need a callback routine when the WAVE_FORMAT_QUERY flag
    ** is specified.
    */
    if ( !(dwFlags & WAVE_FORMAT_QUERY) ) {

        /*
        ** Map the 16 bit pointer is one was specified.
        */
        MMGETOPTPTR( parg16->f1, sizeof(HWAVEOUT), lphWaveOut );
        if ( lphWaveOut == NULL ) {

            ul = MMSYSERR_INVALPARAM;
        }

        /*
        ** Create InstanceData block to be used by our callback routine.
        **
        ** NOTE: Although we malloc it here we don't free it.
        ** This is not a mistake - it must not be freed before the
        ** callback routine has used it - so it does the freeing.
        **
        ** If the malloc fails we bomb down to the bottom,
        ** set ul to MMSYSERR_NOMEM and exit gracefully.
        **
        ** We always have a callback functions.  This is to ensure that
        ** the WAVEHDR structure keeps getting copied back from
        ** 32 bit space to 16 bit, as it contains flags which
        ** applications are liable to keep checking.
        */
        else if ( pInstanceData = malloc_w(sizeof(INSTANCEDATA) ) ) {

            DWORD dwNewFlags = CALLBACK_FUNCTION;

            dprintf2(( "WM32waveOutOpen: Allocated instance buffer at %8X",
                       pInstanceData ));
            pInstanceData->dwCallback         = DWORD32(parg16->f4);;
            pInstanceData->dwCallbackInstance = DWORD32(parg16->f5);
            pInstanceData->dwFlags            = dwFlags;

            dwNewFlags |= (dwFlags & WAVE_ALLOWSYNC);

            ul = GETWORD16((*mmAPI)( &Hand32, uDevID,
                                     (LPWAVEFORMAT)lpWaveformData,
                                     (DWORD)W32CommonDeviceOpen,
                                     (DWORD)pInstanceData, dwNewFlags ));
            /*
            ** If the call returns success update the 16 bit handle,
            ** otherwise don't, and free the memory we malloc'd earlier, as
            ** the callback that would have freed it will never get callled.
            */
            if ( ul == MMSYSERR_NOERROR ) {

                HWAVEOUT16 Hand16 = GETHWAVEOUT16(Hand32);

                trace_wave(( "Handle -> %x", Hand16 ));

                STOREWORD  ( *lphWaveOut, Hand16);
                FLUSHVDMPTR( DWORD32(parg16->f1), sizeof(HWAVEOUT),
                             lphWaveOut );
            }
            else {

                dprintf2(( "WM32waveOutOpen: Freeing instance buffer at %8X",
                           pInstanceData ));
                free_w(pInstanceData);
            }

            /*
            ** Regardless of sucess or failure we need to free the pointer
            ** to the 16 bit WaveOut handle.
            */
            FREEVDMPTR ( lphWaveOut );
        }
        else {
            ul = (ULONG)MMSYSERR_NOMEM;
        }
    }
    else {
        ul = GETWORD16((*mmAPI)( NULL, uDevID, (LPWAVEFORMAT)lpWaveformData,
                                 DWORD32(parg16->f4), DWORD32(parg16->f5),
                                 dwFlags ));
    }

    /*
    ** Regardless of sucess or failure we need to free the pointer to the
    ** 16 bit WaveFormatData.
    */
    FREEMISCPTR( lpWaveformData );


exit_function:
    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutClose
*
* This function closes the specified waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutClose(PVDMFRAME pFrame)
{
    register PWAVEOUTCLOSE16 parg16;
    static   FARPROC         mmAPI = NULL;
             ULONG           ul;

    GET_MULTIMEDIA_API( "waveOutClose", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTCLOSE16), parg16);

    trace_wave(( "waveOutClose( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutPrepareHeader
*
* This function prepares the specified waveform header.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutPrepareHeader(PVDMFRAME pFrame)
{
    register PWAVEOUTPREPAREHEADER3216 parg16;
    static   FARPROC                   mmAPI = NULL;
             ULONG                     ul;
             WAVEHDR                   wavehdr;


    GET_MULTIMEDIA_API( "waveOutPrepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(WAVEOUTPREPAREHEADER3216), parg16);
    trace_wave(( "waveOutPrepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETWAVEHDR16(parg16->f2, &wavehdr);

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1),
                             &wavehdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    **
    */
    if ( !ul ) {
        PUTWAVEHDR16(parg16->f2, &wavehdr);
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutUnprepareHeader
*
* This function prepares the specified waveform header.
* This function cleans up the preparation performed by waveOutPrepareHeader.
* The function must be called after the device driver has finished with a
* data block. You must call this function before freeing the data buffer.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutUnprepareHeader(PVDMFRAME pFrame)
{
    register PWAVEOUTUNPREPAREHEADER3216  parg16;
    static   FARPROC                      mmAPI = NULL;
             ULONG                        ul;
             WAVEHDR                      wavehdr;

    GET_MULTIMEDIA_API( "waveOutUnprepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(WAVEOUTUNPREPAREHEADER3216), parg16);
    trace_wave(( "waveOutUnprepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETWAVEHDR16(parg16->f2, &wavehdr);

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1),
                             &wavehdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    */
    if (!ul) {
        PUTWAVEHDR16(parg16->f2, &wavehdr);
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutWrite
*
* This function sends a data block to the specified waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutWrite(PVDMFRAME pFrame)
{
    register    PWAVEOUTWRITE16 parg16;
    static      FARPROC         mmAPI = NULL;
                ULONG           ul;
                PWAVEHDR32      pWavehdr32;

    GET_MULTIMEDIA_API( "waveOutWrite", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTWRITE16), parg16);

    trace_wave(( "waveOutWrite( %x, %x, %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the WAVEHDR structure is too small
    ** or the lphdr is invalid return an error
    **
    */
    if ( UINT32(parg16->f3) < sizeof(WAVEHDR16)
      || HIWORD( DWORD32(parg16->f2) ) == 0 ) {

        ul = (ULONG)MMSYSERR_INVALPARAM;
    }
    else {

        if ( pWavehdr32 = malloc_w( sizeof(WAVEHDR32) ) ) {

            PWAVEHDR   lpwhdr;  /* used to simplify ptr stuff later on */
#if DBG
            AllocWaveCount++;
            dprintf2(( "W>> %8X (%d)", pWavehdr32, AllocWaveCount ));
#endif

            /* Copy across the wave header stuff.  Note that lpwhdr (a
            ** 32 bit ptr to a 32 bit wave header) is used to make the
            ** pointer stuff a bit less hairy.
            **
            ** pWavehdr32->Wavehdr    is a 32 bit wave header
            ** pWavehdr32->pWavehdr16 is a 16 bit ptr to a 16 bit wave header
            ** pWavehdr32->pWavehdr32 is a 32 bit ptr to a 16 bit wave header
            */
            lpwhdr = &(pWavehdr32->Wavehdr);
            pWavehdr32->pWavehdr16 = (PWAVEHDR16)DWORD32(parg16->f2);
            pWavehdr32->pWavehdr32 = GETWAVEHDR16(DWORD32(parg16->f2), lpwhdr);

            /*
            ** GETWAVEHDR16 can return NULL, in which case we should set
            ** lpwhdr to NULL too and call waveOutWrite only to get the
            ** correct error code.
            */
            if ( pWavehdr32->pWavehdr32 == NULL ) {
                lpwhdr = NULL;
            }

            ul = GETWORD16( (*mmAPI)( HWAVEOUT32(parg16->f1),
                                      lpwhdr, UINT32(parg16->f3) ) );

            /* If the call fails we need to free the memory we malloc'd
            ** above, as the callback that would have freed it will never
            ** get called.
            */
            if ( ul == MMSYSERR_NOERROR ) {

                /* Make sure we reflect any changes that waveOutWrite did
                ** to the WAVEHDR back to 16 bit land.
                **
                ** This is important because some apps (waveedit) poll the
                ** WHDR_DONE bit !!
                */
                COPY_WAVEOUTHDR16_FLAGS( pWavehdr32->pWavehdr32,
                                         pWavehdr32->Wavehdr );
            }
            else {
#if DBG
                AllocWaveCount--;
                dprintf2(( "W<< \t%8X (%d)", pWavehdr32,
                            AllocWaveCount ));
#endif
                free_w( pWavehdr32 );
            }
        }
        else {
            ul = (ULONG)MMSYSERR_NOMEM;
        }
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutPause
*
* This function pauses playback on a specified waveform output device. The
* current playback position is saved. Use wavOutRestart to resume playback from
* the current playback position.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutPause(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTPAUSE16 parg16;
    static   FARPROC         mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutPause", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTPAUSE16), parg16);

    trace_wave(( "waveOutGetPause( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutRestart
*
* This function restarts a paused waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutRestart(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTRESTART16 parg16;
    static   FARPROC           mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutRestart", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTRESTART16), parg16);

    trace_wave(( "waveOutRestart( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutReset
*
* This function stops playback on a given waveform output device and resets the
* current position to 0. All pending playback buffers are marked as done and
* returned to the application.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutReset(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTRESET16 parg16;
    static   FARPROC         mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutReset", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTRESET16), parg16);

    trace_wave(( "waveOutReset( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16( (*mmAPI)( HWAVEOUT32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutGetPosition
*
* This function retrieves the current palyback position of the specified
* waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetPosition(PVDMFRAME pFrame)
{
    register    PWAVEOUTGETPOSITION16   parg16;
    static      FARPROC                 mmAPI = NULL;
                MMTIME                  mmtime;
                ULONG                   ul = MMSYSERR_INVALPARAM;

    GET_MULTIMEDIA_API( "waveOutGetPosition", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETPOSITION16), parg16);

    trace_wave(( "waveOutGetPosition( %x, %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the MMTIME structure is too small return an error
    **
    ** There is a problem here on MIPS.  For some reason the MIPS
    ** compiler thinks a MMTIME16 structure is 10 bytes big.  We
    ** have a pragma in wowmmed.h to align this structure on byte
    ** boundaries therefore I guess this is a compiler bug!
    **
    ** If the input structure is not large enough we return immediately
    */

#ifdef MIPS_COMPILER_PACKING_BUG
    if ( UINT32(parg16->f3) >= 8 ) {
#else
    if ( UINT32(parg16->f3) >= sizeof(MMTIME16) ) {
#endif

        ul = GETMMTIME16( parg16->f2, &mmtime);
        if ( ul == MMSYSERR_NOERROR ) {

            ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1),
                                     &mmtime, sizeof(MMTIME) ));
            /*
            ** Only update the 16 bit structure if the call returns success
            **
            */
            if ( ul == MMSYSERR_NOERROR ) {
                ul = PUTMMTIME16( parg16->f2, &mmtime);
            }
        }
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32waveOutGetPitch
*
* This function queries the current pitch setting of a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetPitch(PVDMFRAME pFrame)
{
    register PWAVEOUTGETPITCH16 parg16;
    static   FARPROC            mmAPI = NULL;
             ULONG              ul = MMSYSERR_INVALPARAM;
             LPDWORD            lpdwPitch;
             DWORD              dwPitch;

    GET_MULTIMEDIA_API( "waveOutGetPitch", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETPITCH16), parg16);
    trace_wave(( "waveOutGetPitch( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), &dwPitch ));

    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(DWORD), lpdwPitch);

        if ( lpdwPitch ) {
            STOREDWORD ( *lpdwPitch, dwPitch );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(DWORD), lpdwPitch );
            FREEVDMPTR ( lpdwPitch );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutSetPitch
*
* This function sets the pitch of a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutSetPitch(PVDMFRAME pFrame)
{
    register PWAVEOUTSETPITCH16 parg16;
    static   FARPROC            mmAPI = NULL;
             ULONG              ul;

    GET_MULTIMEDIA_API( "waveOutSetPitch", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTSETPITCH16), parg16);
    trace_wave(( "waveOutSetPitch( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), DWORD32(parg16->f2) ));

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutGetVolume
*
* This function queries the current volume setting of a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetVolume(PVDMFRAME pFrame)
{
    register PWAVEOUTGETVOLUME16    parg16;
    static   FARPROC                mmAPI = NULL;
             ULONG                  ul;
             LPDWORD                lpdwVolume;
             DWORD                  dwVolume;

    GET_MULTIMEDIA_API( "waveOutGetVolume", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETVOLUME16), parg16);

    trace_wave(( "waveOutGetVolume( %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( INT32(parg16->f1), &dwVolume ));
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(DWORD), lpdwVolume);

        if ( lpdwVolume ) {
            STOREDWORD ( *lpdwVolume, dwVolume );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(DWORD), lpdwVolume );
            FREEVDMPTR ( lpdwVolume );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutSetVolume
*
* This function sets the volume of a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutSetVolume(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTSETVOLUME16 parg16;
    static   FARPROC             mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutSetVolume", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTSETVOLUME16), parg16);

    trace_wave(( "waveOutSetVolume( %x, %x )", INT32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));
    ul = GETWORD16((*mmAPI)( INT32(parg16->f1), DWORD32(parg16->f2) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutGetPlaybackRate
*
* This function queries the current playback rate setting of a waveform output
* device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetPlaybackRate(PVDMFRAME pFrame)
{
    register PWAVEOUTGETPLAYBACKRATE16  parg16;
    static   FARPROC                    mmAPI = NULL;
             ULONG                      ul;
             LPDWORD                    lpdwRate;
             DWORD                      dwRate;

    GET_MULTIMEDIA_API( "waveOutGetPlaybackRate", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETPLAYBACKRATE16), parg16);
    trace_wave(( "waveOutGetPlaybackRate( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), &dwRate ));
    if ( ul == MMSYSERR_NOERROR ) {

       MMGETOPTPTR( parg16->f2, sizeof(DWORD), lpdwRate );

       if ( lpdwRate ) {

           STOREDWORD ( *lpdwRate, dwRate );
           FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(DWORD), lpdwRate );
           FREEVDMPTR ( lpdwRate );
       }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutSetPlaybackRate
*
* This function sets the playback rate of a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutSetPlaybackRate(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTSETPLAYBACKRATE16 parg16;
    static   FARPROC                   mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutSetPlaybackRate", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTSETPLAYBACKRATE16), parg16);

    trace_wave(( "waveOutSetPlaybackRate( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), DWORD32(parg16->f2) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}



/**********************************************************************\
*
* WMM32waveOutBreakLoop
*
* This function breaks a loop on a given waveform output device and allows
* playback to continue with the next block in the driver list.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutBreakLoop(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTBREAKLOOP16 parg16;
    static   FARPROC             mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutBreakLoop", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTBREAKLOOP16), parg16);

    trace_wave(( "waveOutBreakLoop( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutGetID
*
* This function gets the device ID for a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutGetID(PVDMFRAME pFrame)
{
    register    PWAVEOUTGETID16 parg16;
    static      FARPROC         mmAPI = NULL;
                ULONG           ul;
                UINT            dwDeviceID32;
                LPWORD          lpwDeviceID16;

    GET_MULTIMEDIA_API( "waveOutGetID", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTGETID16), parg16);

    trace_wave(( "waveOutGetID( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), &dwDeviceID32 ));

    /*
    ** Only copy the ID back to 16 bit space if the call was sucessful
    **
    */
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(WORD), lpwDeviceID16 );

        if ( lpwDeviceID16 ) {
            STOREWORD  ( *lpwDeviceID16, dwDeviceID32 );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(WORD), lpwDeviceID16 );
            FREEVDMPTR ( lpwDeviceID16 );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveOutMessage
*
* This function send a message to a waveform output device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveOutMessage(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEOUTMESSAGE3216 parg16;
    static   FARPROC             mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveOutMessage", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEOUTMESSAGE16), parg16);

    trace_wave(( "waveOutMessage( %x, %x, %x, %x )",
                 WORD32( parg16->f1 ),   UINT32( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ) ));

    if ( (UINT32(parg16->f2) >= DRV_BUFFER_LOW)
      && (UINT32(parg16->f2) <= DRV_BUFFER_HIGH) ) {

        LPDWORD     lpdwParam1;
        GETMISCPTR(parg16->f3, lpdwParam1);

        ul = GETDWORD16((*mmAPI)( HWAVEOUT32(parg16->f1), UINT32(parg16->f2),
                                  (DWORD)lpdwParam1, DWORD32(parg16->f4) ));
        FREEMISCPTR(lpdwParam1);

    } else {
        ul = GETDWORD16((*mmAPI)( HWAVEOUT32(parg16->f1),
                                  MAKELONG( WORD32(parg16->f2), 0xFFFF ),
                                  DWORD32(parg16->f3), DWORD32(parg16->f4) ));
    }

    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/* ---------------------------------------------------------------------
** WAVE Input API's
** ---------------------------------------------------------------------
*/

/**********************************************************************\
*
* WMM32waveInGetNumDevs
*
* This function returns the number of waveform input devices.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInGetNumDevs(PVDMFRAME pFrame)
{
            ULONG   ul;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInGetNumDevs", mmAPI, MMSYSERR_NODRIVER );

    UNREFERENCED_PARAMETER(pFrame);

    trace_wave(( "waveInGetNumDevs()" ));
    ul = GETWORD16((*mmAPI)() );
    trace_wave(( "-> %ld\n", ul ));

    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInGetDevCaps
*
* This function queries a specified waveform input device to determine its
* capabilities.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInGetDevCaps(PVDMFRAME pFrame)
{
    ULONG ul;
    WAVEINCAPS waveincaps1;
    register PWAVEINGETDEVCAPS16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInGetDevCapsA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINGETDEVCAPS16), parg16);

    trace_wave(( "waveInGetDevCaps( %x, %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the size parameter was zero return straight away.  Note that this
    ** is not an error.
    */
    if ( UINT32( parg16->f3 ) == 0 ) {
        ul = MMSYSERR_NOERROR;
    }
    else {

        ul = GETWORD16((*mmAPI)(INT32(parg16->f1), &waveincaps1,
                                sizeof(WAVEINCAPS) ) );
        /*
        ** Don't update the 16 bit structure if the call failed
        **
        */
        if ( ul == MMSYSERR_NOERROR ) {
            ul = PUTWAVEINCAPS16(parg16->f2, &waveincaps1, UINT32(parg16->f3));
        }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);

    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInGetErrorText
*
* This function retrieves a textual description of the error identified by the
* specified error number.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInGetErrorText(PVDMFRAME pFrame)
{
    ULONG ul = MMSYSERR_NOERROR;
    PSZ pszText;
    register PWAVEINGETERRORTEXT16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInGetErrorTextA", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINGETERRORTEXT16), parg16);

    trace_wave(( "waveInGetErrorText( %x, %x, %x )", UINT32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** Test against a zero length string and a NULL pointer.  If 0 is passed
    ** as the buffer length then the manual says we should return
    ** MMSYSERR_NOERR.  MMGETOPTPTR only returns a pointer if parg16->f2 is
    ** not NULL.
    */
    MMGETOPTPTR( parg16->f2, UINT32(parg16->f3), pszText );
    if ( pszText != NULL ) {

        ul = GETWORD16((*mmAPI)( UINT32(parg16->f1), pszText,
                                 UINT32(parg16->f3) ));

        FLUSHVDMPTR( DWORD32(parg16->f2), UINT32(parg16->f3), pszText);
        FREEVDMPTR(pszText);
    }

    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInOpen
*
* This function opens a specified waveform input device for recording.
*
* As of November 1992 we map the 16 bit Wave Format data directly to the
* the 32 bit side, no thunking of the parameters is performed.
*
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInOpen(PVDMFRAME pFrame)
{
    ULONG         ul=0;
    UINT          uDevID;
    PINSTANCEDATA pInstanceData = NULL;
    DWORD         dwFlags;
    PWAVEFORMAT16 lpWaveformData;
    LPHWAVEIN     lphWaveIn;         // pointer to handle in 16 bit app space
    HWAVEIN       Hand32;            // 32bit handle
    register PWAVEINOPEN16 parg16;
    static   FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInOpen", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINOPEN16), parg16);

    trace_wave(( "waveInOpen( %x, %x, %x, %x, %x, %x )",
                 DWORD32( parg16->f1 ), INT32  ( parg16->f2 ),
                 DWORD32( parg16->f3 ), DWORD32( parg16->f4 ),
                 DWORD32( parg16->f5 ), DWORD32( parg16->f6 ) ));

    /*
    ** Get the device ID. We use INT32 here not UINT32 to make sure that
    ** negative values (such as WAVE_MAPPER (-1)) get thunked correctly.
    */
    uDevID = (UINT)INT32(parg16->f2);

    /*
    ** Get the flags to be used.
    */
    dwFlags = DWORD32(parg16->f6);

    /*
    ** Get a pointer to the WAVEFORMAT structure.  Because the format of this
    ** structure is exactly the same in 32 and 16 bit land I will use
    ** GETMISCPTR to get a generic pointer to the data.  The stuff being
    ** pointed to could be full of unaligned WORDs, but the 32 bit code
    ** would have to cope with this anyway.
    */
    GETMISCPTR( DWORD32(parg16->f3), lpWaveformData );
    if ( lpWaveformData == (PWAVEFORMAT16)NULL ) {
        ul = (ULONG)MMSYSERR_INVALPARAM;
        goto exit_function;
    }

    /*
    ** We don't need a callback routine when the WAVE_FORMAT_QUERY flag
    ** is specified.
    */
    if ( !( dwFlags & WAVE_FORMAT_QUERY ) ) {

        /*
        ** Map the 16 bit pointer is one was specified.
        */
        MMGETOPTPTR( parg16->f1, sizeof(HWAVEIN), lphWaveIn );
        if ( lphWaveIn == NULL ) {

            ul = MMSYSERR_INVALPARAM;
        }

        /*
        ** Create InstanceData block to be used by our callback routine.
        **
        ** NOTE: Although we malloc it here we don't free it.
        ** This is not a mistake - it must not be freed before the
        ** callback routine has used it - so it does the freeing.
        **
        ** If the malloc fails we bomb down to the bottom,
        ** set ul to MMSYSERR_NOMEM and exit gracefully.
        **
        ** We always have a callback functions.  This is to ensure that
        ** the WAVEHDR structure keeps getting copied back from
        ** 32 bit space to 16 bit, as it contains flags which
        ** applications are liable to keep checking.
        */
        else if ( pInstanceData = malloc_w(sizeof(INSTANCEDATA) ) ) {

            DWORD   dwNewFlags = CALLBACK_FUNCTION;

            dprintf2(( "WM32waveInOpen: Allocated instance buffer at %8X",
                       pInstanceData ));
            pInstanceData->dwCallback         = DWORD32(parg16->f4);;
            pInstanceData->dwCallbackInstance = DWORD32(parg16->f5);
            pInstanceData->dwFlags            = dwFlags;

            dwNewFlags |= (dwFlags & WAVE_ALLOWSYNC);

            ul = GETWORD16( (*mmAPI)( &Hand32, uDevID,
                                      (LPWAVEFORMAT)lpWaveformData,
                                      (DWORD)W32CommonDeviceOpen,
                                      (DWORD)pInstanceData, dwNewFlags ) );
            /*
            ** If the call returns success update the 16 bit handle,
            ** otherwise don't, and free the memory we malloc'd earlier, as
            ** the callback that would have freed it will never get callled.
            */
            if ( ul == MMSYSERR_NOERROR ) {

                HWAVEIN16 Hand16 = GETHWAVEIN16(Hand32);

                trace_wave(( "Handle -> %x", Hand16 ));
                STOREWORD  ( *lphWaveIn, Hand16 );
                FLUSHVDMPTR( DWORD32(parg16->f1), sizeof(HWAVEIN), lphWaveIn );
            }
            else {

                free_w(pInstanceData);
            }

            /*
            ** Regardless of sucess or failure we need to free the pointer
            ** to the 16 bit WaveIn handle.
            */
            FREEVDMPTR ( lphWaveIn );
        }
        else {
            ul = (ULONG)MMSYSERR_NOMEM;
        }
    }
    else {

        ul = GETWORD16( (*mmAPI)( NULL, uDevID, (LPWAVEFORMAT)lpWaveformData,
                                  DWORD32(parg16->f4),
                                  DWORD32(parg16->f5), dwFlags) );
    }

    /*
    ** Regardless of sucess or failure we need to free the pointer to the
    ** 16 bit WaveFormatData.
    */
    FREEMISCPTR( lpWaveformData );


exit_function:
    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInClose
*
* This function closes the specified waveform input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInClose(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEINCLOSE16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInClose", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINCLOSE16), parg16);

    trace_wave(( "waveInClose( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}


/**********************************************************************\
*
* WMM32waveInPrepareHeader
*
* This function prepares the specified waveform header.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInPrepareHeader(PVDMFRAME pFrame)
{
    register PWAVEINPREPAREHEADER3216 parg16;
    static   FARPROC                  mmAPI = NULL;
             ULONG                    ul;
             WAVEHDR                  wavehdr;


    GET_MULTIMEDIA_API( "waveInPrepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(WAVEINPREPAREHEADER3216), parg16);
    trace_wave(( "waveInPrepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETWAVEHDR16(parg16->f2, &wavehdr);

    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1),
                             &wavehdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    **
    */
    if ( !ul ) {
        PUTWAVEHDR16(parg16->f2, &wavehdr);
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInUnprepareHeader
*
* This function prepares the specified waveform header.
* This function cleans up the preparation performed by waveInPrepareHeader.
* The function must be called after the device driver has finished with a
* data block. You must call this function before freeing the data buffer.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInUnprepareHeader(PVDMFRAME pFrame)
{
    register PWAVEINUNPREPAREHEADER3216  parg16;
    static   FARPROC                     mmAPI = NULL;
             ULONG                       ul;
             WAVEHDR                     wavehdr;

    GET_MULTIMEDIA_API( "waveInUnprepareHeader", mmAPI, MMSYSERR_NODRIVER );
    GETARGPTR(pFrame, sizeof(WAVEINUNPREPAREHEADER3216), parg16);
    trace_wave(( "waveInUnprepareHeader( %x %x %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), WORD32( parg16->f3 ) ));

    GETWAVEHDR16(parg16->f2, &wavehdr);

    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1),
                             &wavehdr, WORD32(parg16->f3) ) );
    /*
    ** Only update the 16 bit structure if the call returns success
    */
    if (!ul) {
        PUTWAVEHDR16(parg16->f2, &wavehdr);
    }

    trace_wave(( "-> %ld\n", ul ));
    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInAddBuffer
*
* This function sends an input buffer to a waveform input device.
* When the buffer is filled it is sent back to the application.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInAddBuffer(PVDMFRAME pFrame)
{
    ULONG ul;
    PWAVEHDR32 pWavehdr32;
    register PWAVEINADDBUFFER16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInAddBuffer", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINADDBUFFER16), parg16);

    trace_wave(( "waveInAddBuffer( %x, %x, %x)", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the WAVEHDR structure is too small
    ** or the lphdr is invalid return an error
    **
    */
    if ( UINT32(parg16->f3) < sizeof(WAVEHDR16)
      || HIWORD( DWORD32(parg16->f2) ) == 0 ) {

        ul = (ULONG)MMSYSERR_INVALPARAM;
    }
    else {
        if (pWavehdr32 = malloc_w(sizeof(WAVEHDR32) ) ) {

            PWAVEHDR   lpwhdr;  /* used to simplify ptr stuff later on */
#if DBG
            AllocWaveCount++;
            dprintf2(( "W>> %8X (%d)", pWavehdr32, AllocWaveCount ));
#endif

            /* Copy across the wave header stuff.  Note that lpwhdr (a
            ** 32 bit ptr to a 32 bit wave header) is used to make the
            ** pointer stuff a bit less hairy.
            **
            ** pWavehdr32->Wavehdr    is a 32 bit wave header
            ** pWavehdr32->pWavehdr16 is a 16 bit ptr to a 16 bit wave header
            ** pWavehdr32->pWavehdr32 is a 32 bit ptr to a 16 bit wave header
            */
            lpwhdr = &(pWavehdr32->Wavehdr);
            pWavehdr32->pWavehdr16 = (PWAVEHDR16)DWORD32(parg16->f2);
            pWavehdr32->pWavehdr32 = GETWAVEHDR16(DWORD32(parg16->f2), lpwhdr);

            /*
            ** GETWAVEHDR16 can return NULL, in which case we should set
            ** lpwhdr to NULL too and call waveInAddBuffer only to get the
            ** correct error code.
            */
            if ( pWavehdr32->pWavehdr32 == NULL ) {
                lpwhdr = NULL;
            }

            ul = GETWORD16( (*mmAPI)( HWAVEIN32(parg16->f1),
                                      lpwhdr, UINT32(parg16->f3) ) );
            /*
            ** If the call fails we need to free the memory we malloc'd
            ** above, as the callback that would have freed it will never
            ** get called.
            **
            */
            if ( ul == MMSYSERR_NOERROR ) {

                /*
                ** Make sure we reflect any changes that waveInAddBuffer did
                ** to the WAVEHDR back to 16 bit land.
                **
                ** This is important because some apps (waveedit) poll the
                ** WHDR_DONE bit !!
                */
                COPY_WAVEINHDR16_FLAGS( pWavehdr32->pWavehdr32,
                                        pWavehdr32->Wavehdr );
            }
            else {
#if DBG
                AllocWaveCount--;
                dprintf2(( "W<< \t%8X (%d)", pWavehdr32,
                            AllocWaveCount ));
#endif
                free_w( pWavehdr32 );
            }
        }
        else {
            ul = (ULONG)MMSYSERR_NOMEM;
        }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInStart
*
* This function starts input on the specified waveform input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInStart(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEINSTART16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInStart", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINSTART16), parg16);

    trace_wave(( "waveInStart( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInStop
*
* This function stops waveform input.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInStop(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEINSTOP16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInStop", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINSTOP16), parg16);

    trace_wave(( "waveInStop( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInReset
*
* This function stops input on a given waveform input device and resets the
* current position to 0. All pending buffers are marked as done and returned
* to the application.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInReset(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEINRESET16 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInReset", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINRESET16), parg16);

    trace_wave(( "waveInReset( %x )", WORD32( parg16->f1 ) ));
    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1) ));
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInGetPosition
*
* This function retrieves the current input position of the specified waveform
* input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInGetPosition(PVDMFRAME pFrame)
{
    register PWAVEINGETPOSITION16   parg16;
             MMTIME                 mmtime;
             ULONG                  ul = MMSYSERR_INVALPARAM;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInGetPosition", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINGETPOSITION16), parg16);

    trace_wave(( "waveInGetPosition( %x, %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ), UINT32( parg16->f3 ) ));

    /*
    ** If the given size of the MMTIME structure is too small return an error
    **
    ** There is a problem here on MIPS.  For some reason the MIPS
    ** compiler thinks a MMTIME16 structure is 10 bytes big.  We
    ** have a pragma in wowmmed.h to align this structure on byte
    ** boundaries therefore I guess this is a compiler bug!
    **
    ** If the input structure is not large enough we return immediately
    */
#ifdef MIPS_COMPILER_PACKING_BUG
    if ( UINT32(parg16->f3) >= 8 ) {
#else
    if ( UINT32(parg16->f3) >= sizeof(MMTIME16) ) {
#endif

        ul = GETMMTIME16( parg16->f2, &mmtime );
        if ( ul == MMSYSERR_NOERROR ) {

            ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1),
                                     &mmtime, sizeof(MMTIME) ));
            /*
            ** Only update the 16 bit structure if the call returns success
            **
            */
            if ( ul == MMSYSERR_NOERROR ) {
                ul = PUTMMTIME16( parg16->f2, &mmtime );
            }
        }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInGetID
*
* This function gets the device ID for a waveform input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInGetID(PVDMFRAME pFrame)
{
    register PWAVEINGETID16 parg16;
             ULONG          ul;
             UINT           dwDeviceID32;
             LPWORD         lpwDeviceID16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInGetID", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINGETID16), parg16);
    trace_wave(( "waveInGetID( %x, %x )", WORD32( parg16->f1 ),
                 DWORD32( parg16->f2 ) ));

    ul = GETWORD16((*mmAPI)( HWAVEIN32(parg16->f1), &dwDeviceID32 ));

    /*
    ** Only copy the ID back to 16 bit space if the call was sucessful
    **
    */
    if ( ul == MMSYSERR_NOERROR ) {

        MMGETOPTPTR( parg16->f2, sizeof(WORD), lpwDeviceID16 );

        if ( lpwDeviceID16 ) {
            STOREWORD  ( *lpwDeviceID16, dwDeviceID32 );
            FLUSHVDMPTR( DWORD32(parg16->f2), sizeof(WORD), lpwDeviceID16 );
            FREEVDMPTR ( lpwDeviceID16 );
        }
        else {
            ul = MMSYSERR_INVALPARAM;
        }
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* WMM32waveInMessage
*
* This function sends a message to a waveform input device.
*
\**********************************************************************/
ULONG FASTCALL WMM32waveInMessage(PVDMFRAME pFrame)
{
    ULONG ul;
    register PWAVEINMESSAGE3216 parg16;
    static  FARPROC mmAPI = NULL;

    GET_MULTIMEDIA_API( "waveInMessage", mmAPI, MMSYSERR_NODRIVER );

    GETARGPTR(pFrame, sizeof(WAVEINMESSAGE16), parg16);


    trace_wave(( "waveInMessage( %x, %x, %x, %x )", WORD32( parg16->f1 ),
                 UINT32( parg16->f2 ), DWORD32( parg16->f3 ),
                 DWORD32( parg16->f4 ) ));

    if ( (UINT32(parg16->f2) >= DRV_BUFFER_LOW)
      && (UINT32(parg16->f2) <= DRV_BUFFER_HIGH) ) {

        LPDWORD     lpdwParam1;
        GETMISCPTR(parg16->f3, lpdwParam1);

        ul = GETDWORD16((*mmAPI)( HWAVEIN32(parg16->f1), UINT32(parg16->f2),
                                  (DWORD)lpdwParam1, DWORD32(parg16->f4) ));
        FREEMISCPTR(lpdwParam1);

    } else {
        ul = GETDWORD16((*mmAPI)( HWAVEIN32(parg16->f1),
                                  MAKELONG( WORD32(parg16->f2), 0xFFFF ),
                                  DWORD32(parg16->f3), DWORD32(parg16->f4) ));
    }
    trace_wave(( "-> %ld\n", ul ));

    FREEARGPTR(parg16);
    RETURN(ul);
}

/**********************************************************************\
*
* W32CommonDeviceOpen
*
* This routine is the callback which is ALWAYS called by wave and midi
* functions.  This is done to ensure that the XXXXHDR structure keeps
* getting copied back from 32 bit space to 16 bit, as it contains flags
* which the application is liable to keep checking.
*
* The way this whole business works is that the wave/midi data stays in 16
* bit space, but the XXXXHDR is copied to the 32 bit side, with the
* address of the data thunked accordingly so that Robin's device driver
* can still get at the data but we don't have the performance penalty of
* copying it back and forth all the time, not least because it is liable
* to be rather large...
*
* It also handles the tidying up of memory which is reserved to store
* the XXXXHDR, and the instance data (HWND/Callback address; instance
* data; flags) which the xxxxOpen calls pass to this routine, enabling
* it to forward messages or call callback as required.
*
* This routine handles all the messages that get sent from Robin's
* driver, and in fact thunks them back to the correct 16 bit form.  In
* theory there should be no MM_ format messages from the 16 bit side, so
* I can zap 'em out of WMSG16.  However the 32 bit side should thunk the
* mesages correctly and forward them to the 16 bit side and thence to
* the app.
*
* However...  I discover that somewhere in the system the wParam Msg
* parameter (which is 32 bits in Win32 and 16 bits in Win16) is having
* the top 16 bits trashed (zeroed actually).  As I pass a 32 bit handle
* in it, to be thunked in WMDISP32 back to a 16 bit handle, the loss of
* the top 16 bits is not conducive to correct thunking.  Soooo, no more
* thunking in WMDISP32 - I do it all here and Post the correct 16 bit
* message.
*
* Hence WMTBL32 (the 32 bit message switch table) contains the
* NoThunking entry for all the MM_ messages - I'll do 'em myself thank
* you.
*
*
* For the MM_WIM_DATA and MM_WOM_DONE message dwParam1 points to the
* following data struture.
*
*    P32HDR  is a 32 bit pointer to the original 16 bit header
*    P16HDR  is a 16 bit far pointer to the original 16 bit header
*
*    If we need to refernece the original header we must do via the
*    P32HDR pointer.
*
*                   +---------+
*                   | P32HDR  +----->+---------+
*                   +---------+      | 16 bit  |
*                   | P16HDR  +----->|         |    This is the original
*    dwParam1 ----->+---------+      |  Wave   |    wave header passed to
*                   | 32 bit  |      | Header  |    us by the Win 16 app.
*    This is the 32 |         |      |         |
*    bit wave       |  Wave   |      +---------+
*    header that we | Header  |
*    thunked at     |         |
*    earlier.       +---------+
*
*
* We must ensure that the 32 bit structure is completely hidden from the
* 16 bit application, ie. the 16 bit app only see's the wave header that it
* passed to us earlier.
*
*
* NOTE: dwParam2 is junk
*
*
\**********************************************************************/
VOID W32CommonDeviceOpen( HANDLE handle, UINT uMsg, DWORD dwInstance,
                        DWORD dwParam1, DWORD dwParam2 )
{
    PWAVEHDR32      pWavehdr32;
    PMIDIHDR32      pMidihdr32;
    PINSTANCEDATA   pInstanceData;
    WORD            Handle;


    switch (uMsg) {

        /* ------------------------------------------------------------
        ** MIDI INPUT MESSAGES
        ** ------------------------------------------------------------
        */

    case MM_MIM_LONGDATA:
        /*
        ** This message is sent to a window when an input buffer has been
        ** filled with MIDI system-exclusive data and is being returned to
        ** the application.
        */

    case MM_MIM_LONGERROR:
        /*
        ** This message is sent to a window when an invalid MIDI
        ** system-exclusive message is received.
        */
        pMidihdr32 = (PMIDIHDR32)( (PBYTE)dwParam1 - (sizeof(PMIDIHDR16) * 2) );
        WOW32ASSERT( pMidihdr32 );
        COPY_MIDIINHDR16_FLAGS( pMidihdr32->pMidihdr32, pMidihdr32->Midihdr );
        dwParam1 = (DWORD)pMidihdr32->pMidihdr16;


    case MM_MIM_DATA:
        /*
        ** This message is sent to a window when a MIDI message is
        ** received by a MIDI input device.
        */

    case MM_MIM_ERROR:
        /*
        ** This message is sent to a window when an invalid MIDI message
        ** is received.
        */

    case MM_MIM_OPEN:
        /*
        ** This message is sent to a window when a MIDI input device is opened.
        ** We process this message the same way as MM_MIM_CLOSE (see below)
        */

    case MM_MIM_CLOSE:
        /*
        ** This message is sent to a window when a MIDI input device is
        ** closed. The device handle is no longer valid once this message
        ** has been sent.
        */
        Handle = GETHMIDIIN16(handle);
        break;



        /* ------------------------------------------------------------
        ** MIDI OUTPUT MESSAGES
        ** ------------------------------------------------------------
        */

    case MM_MOM_DONE:
        /*
        ** This message is sent to a window when the specified
        ** system-exclusive buffer has been played and is being returned to
        ** the application.
        */
        pMidihdr32 = (PMIDIHDR32)( (PBYTE)dwParam1 - (sizeof(PMIDIHDR16) * 2) );
        WOW32ASSERT( pMidihdr32 );
        COPY_MIDIOUTHDR16_FLAGS( pMidihdr32->pMidihdr32, pMidihdr32->Midihdr );
        dwParam1 = (DWORD)pMidihdr32->pMidihdr16;


    case MM_MOM_OPEN:
        /*
        ** This message is sent to a window when a MIDI output device is opened.
        ** We process this message the same way as MM_MOM_CLOSE (see below)
        */

    case MM_MOM_CLOSE:
        /*
        ** This message is sent to a window when a MIDI output device is
        ** closed. The device handle is no longer valid once this message
        ** has been sent.
        */
        Handle = GETHMIDIOUT16(handle);
        break;



        /* ------------------------------------------------------------
        ** WAVE INPUT MESSAGES
        ** ------------------------------------------------------------
        */

    case MM_WIM_DATA:
        /*
        ** This message is sent to a window when waveform data is present
        ** in the input buffer and the buffer is being returned to the
        ** application.  The message can be sent either when the buffer
        ** is full, or after the waveInReset function is called.
        */
        pWavehdr32 = (PWAVEHDR32)( (PBYTE)dwParam1 - (sizeof(PWAVEHDR16) * 2));
        WOW32ASSERT( pWavehdr32 );
        COPY_WAVEINHDR16_FLAGS( pWavehdr32->pWavehdr32, pWavehdr32->Wavehdr );
        dwParam1 = (DWORD)pWavehdr32->pWavehdr16;

    case MM_WIM_OPEN:
        /*
        ** This message is sent to a window when a waveform input
        ** device is opened.
        **
        ** We process this message the same way as MM_WIM_CLOSE (see below)
        */

    case MM_WIM_CLOSE:
        /*
        ** This message is sent to a window when a waveform input device is
        ** closed.  The device handle is no longer valid once the message has
        ** been sent.
        */
        Handle = GETHWAVEIN16(handle);
        break;



        /* ------------------------------------------------------------
        ** WAVE OUTPUT MESSAGES
        ** ------------------------------------------------------------
        */

    case MM_WOM_DONE:
        /*
        ** This message is sent to a window when the specified output
        ** buffer is being returned to the application. Buffers are returned
        ** to the application when they have been played, or as the result of
        ** a call to waveOutReset.
        */
        pWavehdr32 = (PWAVEHDR32)( (PBYTE)dwParam1 - (sizeof(PWAVEHDR16) * 2));
        WOW32ASSERT( pWavehdr32 );
        COPY_WAVEOUTHDR16_FLAGS( pWavehdr32->pWavehdr32, pWavehdr32->Wavehdr );
        dwParam1 = (DWORD)pWavehdr32->pWavehdr16;

    case MM_WOM_OPEN:
        /*
        ** This message is sent to a window when a waveform output device
        ** is opened.
        **
        ** We process this message the same way as MM_WOM_CLOSE (see below)
        */

    case MM_WOM_CLOSE:
        /*
        ** This message is sent to a window when a waveform output device
        ** is closed.  The device handle is no longer valid once the
        ** message has been sent.
        */
        Handle = GETHWAVEOUT16(handle);
        break;

#if DBG
    default:
        dprintf(( "Unknown message received in CallBack function " ));
        dprintf(( "best call StephenE or MikeTri" ));
        return;
#endif

    }


    /*
    ** Now make the CallBack, or PostMessage call depending
    ** on the flags passed to original (wave|midi)(In|Out)Open call.
    */
    pInstanceData = (PINSTANCEDATA)dwInstance;
    WOW32ASSERT( pInstanceData );

    switch (pInstanceData->dwFlags & CALLBACK_TYPEMASK)  {

    case CALLBACK_WINDOW:
        dprintf2(( "WINDOW callback identified" ));
        PostMessage( HWND32( LOWORD(pInstanceData->dwCallback) ),
                     uMsg, Handle, dwParam1 );
        break;


    case CALLBACK_TASK:
    case CALLBACK_FUNCTION: {

        DWORD   dwFlags;

        if ( (pInstanceData->dwFlags & CALLBACK_TYPEMASK) == CALLBACK_TASK ) {
            dprintf2(( "TASK callback identified" ));
            dwFlags = DCB_TASK;
        }
        else {
            dprintf2(( "FUNCTION callback identified" ));
            dwFlags = DCB_FUNCTION;
        }

        WOW32DriverCallback( pInstanceData->dwCallback,
                             dwFlags,
                             Handle,
                             LOWORD( uMsg ),
                             pInstanceData->dwCallbackInstance,
                             dwParam1,
                             dwParam2 );

        }
        break;
    }

    /*
    ** Now, free up any storage that was allocated during the waveOutOpen
    ** and waveInOpen.  This should only be freed during the MM_WOM_CLOSE or
    ** MM_WIM_CLOSE message.
    **
    ** Also, free up any storage that was allocated during the waveOutWrite
    ** and waveInAddBuffer call.  This should only be freed during the
    ** MM_WOM_DONE or MM_WIM_DATA message.
    */
    switch (uMsg) {

    case MM_MIM_CLOSE:
    case MM_MOM_CLOSE:
    case MM_WIM_CLOSE:
    case MM_WOM_CLOSE:
        dprintf2(( "W32CommonDeviceOpen: Freeing device open buffer at %X",
                    pInstanceData ));
        dprintf2(( "Alloc Midi count = %d", AllocMidiCount ));
        dprintf2(( "Alloc Wave count = %d", AllocWaveCount ));
        free_w( pInstanceData );
        FREEHWAVEIN16( Handle );
        break;

    case MM_WIM_DATA:
    case MM_WOM_DONE:
#       if DBG
            AllocWaveCount--;
            dprintf2(( "W<< \t%8X (%d)", pWavehdr32, AllocWaveCount ));
#       endif
        free_w( pWavehdr32 );
        break;

    case MM_MIM_LONGDATA:
    case MM_MIM_LONGERROR:
    case MM_MOM_DONE:
#       if DBG
            AllocMidiCount--;
            dprintf2(( "M<< \t%8X (%d)", pMidihdr32, AllocMidiCount ));
#       endif
        free_w( pMidihdr32 );
        break;
    }

}

#endif