File: midifns.c

package info (click to toggle)
audacity 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,076 kB
  • sloc: cpp: 192,859; ansic: 158,072; sh: 34,021; python: 24,248; lisp: 7,495; makefile: 3,667; xml: 573; perl: 31; sed: 16
file content (1886 lines) | stat: -rw-r--r-- 51,014 bytes parent folder | download | duplicates (7)
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
/*****************************************************************************
*                            midifns.c
* Copyright 1989 Carnegie Mellon University
*  Date | Change
*-----------+-----------------------------------------------------------------
* 29-Mar-88 | Created from IBM PC version of mpu.c
*           | Added settime()
* 02-May-88 | AMIGA  2000 version. portable version.
* 12-Oct-88 | JCD : Exclusive AMIGA Version.
* 13-Apr-89 | JCD : New portable version.
* 19-Apr-89 | JCD : Amiga CAMD Version added.
*  5-Apr-91 | JDW : Further modification
* 17-Feb-92 | GWL : incorporate JMN's new mpu.c
*  8-Jun-92 | JDZ : add support for ITC midi interface
* 16-Dec-92 | RBD : replace JMN's mpu.c with LMS's mpu.c
* 11-Mar-94 | PLu : port to  IRIX
* 25-Apr-97 | RBD : it looks like SGI changed their interface.  I
*	        |       made it compile again, but MIDI does not work, so
*	        |       took out calls to actually send/recv MIDI data
* 28-Apr-03 | DM  : Renamed random -> cmtrand, true->TRUE, false->FALSE
*           |       Use features rather than system names in #ifdef's
*****************************************************************************/

#include "switches.h"

#ifdef UNIX
#include <sys/resource.h>
#include <sys/param.h>
#ifndef OPEN_MAX
/* this is here for compiling the UNIX version under AIX. This is a BSDism */
#define OPEN_MAX 2000
#endif /* OPEN_MAX */
#endif /* UNIX */

#ifdef UNIX_MACH
#include "machmidi.h"
#endif


#ifdef AMIGA
#ifdef AZTEC
#include "functions.h"
#endif /* AZTEC */
#include "midi/camd.h"
#include "clib/camd_protos.h"
/* note: azt_camd_pragmas.h was produced by running MAPFD on the
 * lib/camd_lib.fd file included in the CAMD disk from Commodore.
 * The "CamdClient" calls are manually removed from 
 */
#ifdef AZTEC
#include "pragmas/azt_camd_pragmas.h"
#else /* !AZTEC */
#include "pragmas/lat_camd_pragmas.h"
#endif /* AZTEC */
#include "camdmidi.h"
#include "ctype.h"
#endif /* AMIGA */

#ifdef UNIX_IRIX
/* #define UNIX_IRIX_MIDIFNS -- this would enable actual midi I/O
 * if the actual midi I/O code worked
 */
/* IRIX changed the MIDI interface,
 * retain this for older systems:
 */
#ifdef UNIX_IRIX_MIDIFNS
#include <dmedia/midi.h>
#endif
#endif

#include "stdio.h"
#include "cext.h"
#include "midicode.h"
#include "cmdline.h"
#include "pitch.h"
#include "midifns.h"
#include "userio.h"
#include "string.h"
#ifdef MACINTOSH_OR_DOS
#ifndef WINDOWS
#include "midibuff.h"
#endif
#endif

#ifdef UNIX_ITC /* was ITC */
#include "sys/param.h"
/* since boolean is defined, block its definition in midistruct.h.
 * CMT defines boolean as ushort, but midistruct.h uses int.  
 * This is not a problem on RS/6000s, but beware!
 */
/* the following would be included if we had the BSD switch set.  I think
   we should try to avoid BSDisms; when fixed, the following should be
   removed
 */
#define NBBY 8
#include "sys/select.h" /* defines fd_set */
#define MIDI_HAS_BOOLEAN
#include "midistruct.h"
#include "cmtio.h"
#endif /* UNIX_ITC */

#ifdef  DOS
#ifndef WINDOWS
#include "timer.h"
#include "mpu.h"
#endif /* ifndef WINDOWS */
#endif /* ifdef DOS */

#ifndef BREAKTEST
#define BREAKTEST
#endif

#ifdef __APPLE__
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#else
#ifdef UNIX
#ifndef UNIX_IRIX
#include "sys/time.h"
#include "sys/timeb.h"
#include "cmtio.h"
#else
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>

#ifdef UNIX_IRIX_MIDIFNS
#include <midi.h>
#include <midiio.h>
#endif /* UNIX_IRIX_MIDIFNS */
#endif  /* UNIX_IRIX */
#endif /* UNIX */
#endif /* __APPLE__ */

#ifdef ITC
static int ignore_realtime = 0;
#endif /* ITC */

#ifdef MACINTOSH

/* added for ThinkC 7: */
#include <OSUtils.h>

/* port numbers are in the range 0..MAX_PORTS-1 */
#define CHANNELS_PER_PORT 16
#define MAX_PORTS ((MAX_CHANNELS + CHANNELS_PER_PORT - 1) / CHANNELS_PER_PORT)

/* here are some MIDIMGR specific definitions */
#ifdef MIDIMGR
#include "MIDI.h"
#include "midimgr.h"

#define TICKS_TO_MS(t) t
#define MS_TO_TICKS(t) t

#else
/* here are some non-MIDIMGR definitions for the Mac */
/****************************************************************************
*
*       DMH: constants from macmidi.c
*
****************************************************************************/

/* the modem port, also called port A */
#define portA 0

/* the printer port, also called port B */
#define portB 1

/* a tick is 1/60 of a second
 *
 * the following tables and routines are used to convert
 * between ticks and milliseconds
 */
#define TICKS_TO_MS(t)  (((t) * 50) / 3)
#define MS_TO_TICKS(t)  (((t) * 3) / 50)
#endif  /* def MIDIMGR */
#endif  /* def MACINTOSH */

#ifdef WINDOWS
#define huge
#endif

/****************************************************************************
*
* exported flags
*
****************************************************************************/

boolean miditrace = FALSE;      /* enables printed trace of MIDI output */
boolean musictrace = FALSE;     /* enables printed trace of commands */
#ifdef MACINTOSH_OR_DOS
boolean ctrlFilter = TRUE;    /* suppress continuous controller data */
boolean exclFilter = TRUE;    /* suppress exclusive messages */
boolean realFilter = TRUE;    /* suppress realtime messages */
#endif

/****************************************************************************
*
* exported variables
*
****************************************************************************/

public int keyloud;    /* set to velocity of last getkey event */
/* public long error; */
public short midi_error_flags = 0;

/* The following midifns_syntax lists command line switches and options.
   Since these are machine dependent, use conditional compilation.
   Conditional compilation within a string is a bit tricky: you want to
   write "\" for line continuation within the string, but "\" gets eaten
   by the macro preprocessor.
   That's why we define macros like AMIGAINPORT.
   Regretably it doesn't work for all compilers.
 */
 

/* Lattice and RT/Unix aren't happy expanding the embedded macros below, so
   I made a separate declaration of midifns_syntax for Unix
 */
#ifdef UNIX
public char *midifns_syntax = "block<s>Turn off midi THRU;\
    miditrace<s>Trace low-level midi functions;\
    noalloff<s>Do not send alloff message when done;\
    trace<s>Trace music operations;\
    tune<o>Load a tuning file";
#else
#ifdef MACINTOSH
#ifdef MIDIMGR
public char *midifns_syntax = "miditrace<s>Trace low-level midi functions;\
    noalloff<s>Do not send alloff message when done;\
    patch<s>Remember/reuse Midi Mgr patches;\
    trace<s>Trace music operations;\
    keep<s>Keep other processes running;\
    tune<o>Load a tuning file";
#else /* no MIDIMGR */
public char *midifns_syntax = "miditrace<s>Trace low-level midi functions;\
    noalloff<s>Do not send alloff message when done;\
    patch<s>Remember/reuse Midi Mgr patches;\
    trace<s>Trace music operations;\
    tune<o>Load a tuning file";		
#endif /* MIDIMGR */
#else 
#ifdef AMIGA
public char *midifns_syntax = "block<s>Turn off midi THRU;\
    inport<o>Inpur port number;\
    miditrace<s>Trace low-level midi functions;\
    noalloff<s>Do not send alloff message when done;\
    outport<o>Output port number;\
    trace<s>Trace music operations;\
    tune<o>Load a tuning file";
#else /* not UNIX or MACINTOSH or MIDIMGR or AMIGA */
#ifdef DOS
public char *midifns_syntax = "miditrace<s>Trace low-level midi functions;\
    noalloff<s>Do not send alloff message when done;\
    trace<s>Trace music operations;\
    tune<o>Load a tuning file";
#endif /* DOS */
#endif /* AMIGA */
#endif /* MACINTOSH */
#endif /* UNIX */

#ifdef MACINTOSH
boolean do_midi_thru = FALSE; /* exported: copy midi in to midi out */
#endif


/****************************************************************************
*
* local module variables
*
****************************************************************************/

private int initialized = FALSE;   /* set by musicinit, cleared by musicterm */
private boolean tune_flag = FALSE; /* set by musicinit, never cleared */
#ifdef DOS
private boolean metroflag = FALSE; /* flag to turn on metronome */
#endif
private int user_scale = FALSE;    /* TRUE if user-defined scale */
private int bend[MAX_CHANNELS];    /* current pitch bend on channel */
short cur_midi_prgm[MAX_CHANNELS];
private pitch_table pit_tab[128];  /* scale definition */

#ifdef DOS
private ulong timeoffset = 0;
public boolean exclerr = FALSE;
public byte xcodemask; /* mask (00 or FF) */
public byte xcode; /* mfr code */
#endif

#ifdef MACINTOSH_OR_DOS
boolean sysex_pending = FALSE;
#endif

#ifdef AMIGA

#define CONTCONT ((CMF_Ctrl & ~CMF_CtrlSwitch) | CMF_PitchBend | \
        CMF_ChanPress)
#endif  /* def AMIGA */

#ifdef UNIX
private ulong timeoffset = 0;
#endif

#ifdef UNIX_IRIX_MIDIFNS
static MIport *miport;
static int ignore_realtime = 0;

private byte *sysex_p;
private int sysex_n;
#endif 

#ifdef ITC
mi_id midiconn;
#endif

#ifdef MACINTOSH
private ulong ticksAtStart = 0L;
    /* clock ticks at time of last musicinit or timereset
     * ASSUME: tick clock never wraps.  this is a good assumption, since
     * the tick clock is set to zero when the power is turned on and the
     * tick counter is 32 bits.  the Macintosh would need to be on for
     * 828.5 days for the tick counter to wrap around! */

#endif  /* def MACINTOSH */

/****************************************************************************
*
* functions declared in this module
*
****************************************************************************/

private void fixup();
private void midi_init();
extern boolean check_ascii(); /*userio.c*/
private void musicterm();


/****************************************************************************
*                alloff
* Inputs:
*    none
* Effect: 
*    Sends MIDI all notes off command on every channel.
****************************************************************************/

#define ALL_NOTES_OFF 0x7B /*DMH: from macmidi.c*/

void alloff()
{
    int c;

    if (!initialized) fixup();
    if (musictrace)
    gprintf(TRANS,"alloff()\n");
    for (c = 1; c <= MAX_CHANNELS; c++) {
    midi_write(3, MIDI_PORT(c), (byte) (0xb0 | MIDI_CHANNEL(c)), ALL_NOTES_OFF, 0);
    }
}



/***************************************************************
*                           eventwait
*
* Input : wakeup time, -1 means forever
* Output : none
* Return: none
* Effect: waits until ascii or midi input or timeout
***************************************************************/

#ifdef UNIX_ITC
void eventwait(timeout)
  long timeout;
{
    struct timeval unix_timeout;
    struct timeval *waitspec = NULL;
    fd_set readfds;
    struct rlimit file_limit;

    FD_ZERO(&readfds);
    FD_SET(MI_CONNECTION(midiconn), &readfds);
    FD_SET(fileno(stdin), &readfds);
    if (timeout >= 0) {
    timeout -= gettime();   /* convert to millisecond delay */
    unix_timeout.tv_sec = timeout / 1000;
    /* remainder become microsecs: */
    unix_timeout.tv_usec = (timeout - (unix_timeout.tv_sec * 1000)) * 1000;
    waitspec = &unix_timeout;
    }
    getrlimit(RLIMIT_NOFILE, &file_limit);
    select(file_limit.rlim_max+1, &readfds, 0, 0, waitspec);
    return;
}
#else /* !UNIX_ITC */
#ifdef UNIX
/* see machmidi.c for UNIX_MACH implementation */
#ifndef UNIX_MACH
#ifdef UNIX_IRIX_MIDIFNS
void eventwait(timeout)
  long timeout;
{
    struct timeval unix_timeout;
    struct timeval *waitspec = NULL;
    fd_set readfds;

    FD_ZERO(&readfds);
    FD_SET(mdGetFd(miport), &readfds);
    FD_SET(fileno(stdin), &readfds);
    if (timeout >= 0) {
      
        timeout -= gettime();   /* convert to millisecond delay */
        unix_timeout.tv_sec = timeout / 1000;
        /* remainder become microsecs: */
        unix_timeout.tv_usec = (timeout - (unix_timeout.tv_sec * 1000)) * 1000;
        waitspec = &unix_timeout;
    }
     select(FD_SETSIZE, &readfds, 0, 0, waitspec);

    return;
}
#else
#ifdef BUFFERED_SYNCHRONOUS_INPUT
void eventwait(timeout)
  long timeout;
{
    struct timeval unix_timeout;
    struct timeval *waitspec = NULL;
    struct rlimit file_limit;

    if (timeout >= 0) {
    timeout -= gettime();   /* convert to millisecond delay */
    unix_timeout.tv_sec = timeout / 1000;
    /* remainder become microsecs: */
    unix_timeout.tv_usec = (timeout - (unix_timeout.tv_sec * 1000)) * 1000;
    waitspec = &unix_timeout;
    getrlimit(RLIMIT_NOFILE, &file_limit);
    select(file_limit.rlim_max+1, 0, 0, 0, waitspec);
    } else {
    int c = getc(stdin);
    ungetc(c, stdin);
    }
    return;
}
#else
void eventwait(timeout)
  long timeout;
{
    struct timeval unix_timeout;
    struct timeval *waitspec = NULL;
    int readfds = 1 << IOinputfd;
    struct rlimit file_limit;

    if (timeout >= 0) {
    timeout -= gettime();   /* convert to millisecond delay */
    unix_timeout.tv_sec = timeout / 1000;
    /* remainder become microsecs: */
    unix_timeout.tv_usec = (timeout - (unix_timeout.tv_sec * 1000)) * 1000;
    waitspec = &unix_timeout;
    }
    getrlimit(RLIMIT_NOFILE, &file_limit);
    select(file_limit.rlim_max+1, &readfds, 0, 0, waitspec);
    return;
}
#endif /* BUFFERED_SYNCHRONOUS_INPUT */
#endif /* UNIX_IRIX */
#endif /* UNIX_MACH */
#endif /* UNIX */ /* I wanted to put an else here, but this confused a Unix C compiler */
#endif /* UNIX_ITC */
#ifdef AMIGA
/* see camdmidi.c for Amiga implementation */
#else
#ifndef UNIX /* since I couldn't use an else above, have to check UNIX here */
#ifdef WINDOWS
void eventwait(timeout)
  long timeout;
{
    if (timeout >= 0) {
    gprintf(TRANS, "eventwait: not implemented\n");
    return;
    } else {
    int c = getc(stdin);
    ungetc(c, stdin);
    }
    return;
}
#else
void eventwait(timeout)
  long timeout;
{
    while (timeout > gettime() || timeout == -1) {
        if (check_ascii() || check_midi()) return;
    }
}
#endif /* WINDOWS */
#endif /* UNIX */
#endif /* AMIGA */


/****************************************************************************
*                exclusive
* Inputs:
*    boolean onflag -- set to TRUE to receive midi exclusive data
* Effect: 
*    Tells module to read exclusive messages into buffer
****************************************************************************/

void exclusive(boolean onflag)
{
    if  (!initialized) fixup();
    if (musictrace) gprintf(TRANS, "exclusive: %d\n", onflag);
#ifdef AMIGA
    if (onflag) SetMidiFilters(cmt_mi, 
    cmt_mi->PortFilter, cmt_mi->TypeFilter | CMF_SysEx, cmt_mi->ChanFilter);
    else SetMidiFilters(cmt_mi, 
    cmt_mi->PortFilter, cmt_mi->TypeFilter & ~CMF_SysEx, cmt_mi->ChanFilter);
#endif
#ifdef  MACINTOSH_OR_DOS
    exclFilter = !onflag;
#endif
}


/****************************************************************************
*                    fixup
* Effect: 
*    Print error message and call musicinit
****************************************************************************/

private void fixup()
{
    gprintf(ERROR, "You forgot to call musicinit.  I'll do it for you.\n");
    musicinit();
}

#ifdef UNIX_IRIX_MIDIFNS
private void flush_sysex(void);
#endif

long get_excl(byte *buffer, long len)
{
    long ret = 0;
#ifdef UNIX_IRIX_MIDIFNS
    byte *sxp = sysex_p;
    long l = len;
#endif
#ifdef UNIX_ITC  /* was ITC */
    ret = mi_getx(midiconn, FALSE, len, (char *) buffer);
#endif
#ifdef UNIX_MACH
    ret = mi_getx(midiconn, FALSE, len, (unsigned char *)buffer);
#endif
#ifdef UNIX_IRIX_MIDIFNS
    if (!sysex_p) return 0;
    if (len > sysex_n) len = sysex_n;
    while (l--)
      {
        *buffer = *(sxp++);
        if (*(buffer++) == CMT_MIDI_EOX)
          {
        flush_sysex();
        break;
          }
      }
    ret = len - l - 1;
#endif
#ifdef AMIGA
    ret = GetSysEx(cmt_mi, (UBYTE *) buffer, len);
    AMIGA_ERROR_CHECK;
#endif
#ifdef MACINTOSH_OR_DOS
#ifndef WINDOWS
    /* I'm not sure the following line is a good thing: it forces the
     * caller to wait until a full sysex message is received and the
     * 1st 4 bytes are fetched via getbuf() before a sysex message can
     * be read via get_excl().  Without this, both mm.c and exget.c
     * were fetching ahead and getting out of sync with getbuf().  I
     * fixed mm.c and exget.c to work (by checking for EOX), but I added
     * this line (which should never have any effect) just to make the
     * DOS interface behave more like the Amiga and Mac interfaces.  The
     * drawback is that you can't fetch bytes until the EOX is seen,
     * because nothing goes into the getbuf() buffer until then.
     */
    if (!sysex_pending) return 0;
    while (len-- && (xbufhead != xbuftail)) {
    *buffer = xbuff[xbufhead++];
    ret++;
    if (*buffer == MIDI_EOX) {
        sysex_pending = FALSE;
        break;
    }
    buffer++;
    xbufhead &= xbufmask;
    }
#endif
#endif
    return ret;
}

/****************************************************************************
*                   getbuf
* Inputs:
*    boolean waitflag: TRUE if routine should wait for data
*    byte * p: Pointer to data destination
* Result: boolean
*    TRUE if data was written to *p
*    FALSE if data not written to *p
* Effect: 
*    copies data from buffer to *p
*    will wait for buffer to become nonempty if waitflag is TRUE
*
* Modified 24 May 1988 for AMIGA (JCD)
****************************************************************************/

#ifdef UNIX_IRIX_MIDIFNS
    private void setup_sysex(MDevent *event, u_char *buffer);
#endif /* UNIX_IRIX */
boolean getbuf(boolean waitflag, unsigned char * p)
{
#ifdef UNIX_IRIX_MIDIFNS
    MDevent event;
    int ret;
#endif /* UNIX_IRIX */

    if (!initialized) fixup();
#ifdef UNIX
#ifdef UNIX_IRIX_MIDIFNS
/* current IRIX version ignores the waitflag (it never waits) */

  if (sysex_p) flush_sysex();
  if (ignore_realtime == 0) {
      ret = mdReceive(miport, &event, 1);
      if (ret) {
      if (event.msg[0] != 0xF0) {
          *((u_long*) p) = *((u_long*) event.msg);
      } else {
          setup_sysex(&event, p);
      }
      }
      return ret;
  } else {
      do /* skip realtime messages */
    {
       ret = mdReceive(miport, &event, 1);
       if (ret == -1) return ret;
     } while (event.msg[0] == 0xf8);
      if (event.msg[0] != 0xF0) {
      *((u_long*) p) = *((u_long*) event.msg);
      } else {
      setup_sysex(&event, p);
      }
      return ret;
  }
#endif /* UNIX_IRIX */
#ifdef UNIX_ITC
    if (ignore_realtime == 0) {
        return(mi_get(midiconn, waitflag, (char *) p));
    }
    else {
        boolean ret=false;
        /* filter out realtime msgs */
        do {
            ret = mi_get(midiconn, waitflag, (char *) p);
            if (ret == FALSE)
                return(ret);
        } while(p[0] == 0xf8);
        return(ret);
    }
#else /* UNIX_ITC */
#ifndef UNIX_IRIX
    if (waitflag) {
    gprintf(ERROR, "getbuf called with waitflag!");
    EXIT(1);
    }
    return FALSE;
#endif /* UNIX_IRIX */
#endif /* UNIX_ITC */
#endif /* UNIX */

#ifdef MACINTOSH_OR_DOS
#ifndef WINDOWS
    if (sysex_pending) { /* flush sysex to keep buffers in sync */
        while (xbuff[xbufhead++] != MIDI_EOX) {
        xbufhead &= xbufmask;
        if (xbufhead == xbuftail) break;
        }
        sysex_pending = FALSE;
    }
    if (waitflag) while (buffhead == bufftail) /* wait */ ;
    else if (buffhead == bufftail) return(false);
    *(long *)p = *(long *)(((char *)buff)+buffhead);
    buffhead = (buffhead + 4) & BUFF_MASK;
    if (*p == MIDI_SYSEX) { /* if sys-ex, remember to fetch from xbuff */
        sysex_pending = TRUE;
    }
    return(true);
#else
    return FALSE;
#endif /* WINDOWS */
#endif /* MACINTOSH_OR_DOS */

#ifdef AMIGA
    if (waitflag) {
        do {
        WaitMidi(cmt_mi, &cmt_msg);
        AMIGA_ERROR_CHECK;
        } while (amigaerrflags);
    } else {
        AMIGA_ERROR_CHECK;
        if (!GetMidi(cmt_mi, &cmt_msg)) return(false);
    }    
    *(long *)p = *(long *)&cmt_msg;
    clearmsg(cmt_msg);
    return(true);
#endif /* AMIGA */
}

#ifdef UNIX_IRIX_MIDIFNS

private void setup_sysex(MDevent *event, u_char *buffer)
/* N.B. do not leak memory remember to call free(sysex_p) */
{
   u_char *sxp = (u_char *) event->sysexmsg;
   int i;

   for (i=0;i<4;i++)
     *(buffer++) = *(sxp++);
   sysex_p = event->sysexmsg;
   sysex_n = event->msglen;
}

private void flush_sysex()
{
  mdFree(sysex_p);
  sysex_p = 0;
  sysex_n = 0;
}
#endif

#ifdef MACINTOSH_OR_DOS
#ifndef WINDOWS
public boolean check_midi()
{
    if (buffhead == bufftail) return FALSE;
    else return TRUE;
}
#endif
#endif


/****************************************************************************
*                   getkey
* Inputs:
*    boolean waitflag: TRUE if wait until key depression, FALSE if
*             return immediately
* Result: int
*    key number of key which has been depressed
*    It returns -1 if waitflag is FALSE and no key has been pressed
*    If waitflag is TRUE this routine will block until a key is pressed
* Effect: 
*    reads a key
****************************************************************************/

/*DMH: in previous version, macmidi.c subtracted 12 from msg to get key at each occurence...*/

short getkey(boolean waitflag)
{
    byte msg[4];
    short k;

    if (!initialized) fixup();

    while (TRUE) {    /* process data until you find a note */
    /* look for data and exit if none found */
    /* NOTE: waitflag will force waiting until data arrives */
    if (!getbuf(waitflag, msg)) { /* nothing there */
        k = -1;
        break;
    } else if ((msg[0] & MIDI_CODE_MASK) == MIDI_ON_NOTE) {
        if (msg[2] == 0) { /* velocity 0 -> note off */
        keyloud = 0;
        k = msg[1] + 128;
        } else {
        keyloud = msg[2];
        k = msg[1];
        }
        break;
    } else if ((msg[0] & MIDI_CODE_MASK) == MIDI_OFF_NOTE) {
        keyloud = 0;
        k = msg[1] + 128;
        break;
    }
    }
    if (musictrace) {
    if (k != -1) gprintf(TRANS,"getkey got %d\n", k);
    }
    return k;
}


/****************************************************************************
*                   gettime
* Result: ulong
*    current timestamp since the last call to
*    musicinit or timereset
* Effect: 
*    fakes it
****************************************************************************/

ulong gettime()         /*DMH: ulong is from mpu->midifns conversion, for Mac*/
{
#if HAS_GETTIMEOFDAY
    struct timeval timeval;
#endif
#if HAS_FTIME
    struct timeb ftime_res;
#endif
    register ulong ticks = 0L;

    BREAKTEST    /* abort if user typed Ctrl Break */
    if (!initialized) fixup();

#ifdef MACINTOSH
#ifdef MIDIMGR
    ticks = MIDIGetCurTime(OutputRefNum) - ticksAtStart;
#else
    ticks = TickCount() - ticksAtStart;
#endif
    if (initialized) abort_check();     /* give user a chance to abort */
    ticks = TICKS_TO_MS(ticks);
#endif

#ifdef AMIGA
    ticks = (*camdtime - timeoffset) << 1;      /* return milliseconds */
#endif

#ifdef  DOS
#ifndef WINDOWS       
    ticks = elapsedtime(timeoffset, readtimer()); /* return milliseconds */
    /* gprintf(TRANS, "currtime = %ld, timeoffset = %ld\n", currtime, timeoffset); */
#endif
#endif  /* ifdef DOS */

#if HAS_GETTIMEOFDAY
    gettimeofday(&timeval, 0);
    ticks = timeval.tv_sec * 1000 + timeval.tv_usec / 1000 - timeoffset;
#endif
#if HAS_FTIME
    ftime(&ftime_res);
    ticks = ((ftime_res.time - timeoffset) * 1000) + ftime_res.millitm;
#endif

    /* if (miditrace) gprintf(TRANS, "."); */
    return(ticks);
}


/****************************************************************************
*                   l_rest
* Inputs:
*    long time: Amount of time to rest
* Effect: 
*    Waits until the amount of time specified has lapsed
****************************************************************************/

void l_rest(time)
long time;
{
    if (!initialized) fixup();
    l_restuntil(time + gettime());    
}

/****************************************************************************
*                 l_restuntil
* Inputs:
*    long time: Event time to rest until
* Effect: 
*    Waits until the specified time has been reached (absolute time)
****************************************************************************/

void l_restuntil(time)
long time;
{
#ifdef MACINTOSH
    ulong now = gettime();  
    ulong junk; /* changed from ulong for ThinkC 7, back to ulong for CW5 */
#endif

#ifdef AMIGA
    while (time > gettime()) eventwait(time);
#else  
    for(; (time_type) time > gettime(););
#endif

#ifdef MACINTOSH
    now = gettime();        

    if (time > now)  Delay(MS_TO_TICKS(time - now), &junk);
    /* else time <= now, so return immediately */
#endif

}

/****************************************************************************
*               metronome
* Inputs:
*    boolean onflag: TRUE or FALSE
* Effect:
*    enables (true) or disables (false) MPU-401 metronome function.
*    must be called before musicinit
****************************************************************************/

void metronome(boolean onflag)
{
#ifdef DOS
metroflag = onflag;
#endif
}


/****************************************************************************
*                  midi_bend
* Inputs:
*    int channel: midi channel on which to send data
*    int value: pitch bend value
* Effect: 
*    Sends a midi pitch bend message
****************************************************************************/

void midi_bend(int channel, int value)
{
    if (!initialized) fixup();
    if (musictrace)
    gprintf(TRANS,"midi_bend: ch %d, val %d\n", channel, value - (1 << 13));
        bend[MIDI_CHANNEL(channel)] = value;
    
    midi_write(3, MIDI_PORT(channel), (byte) (MIDI_BEND | MIDI_CHANNEL(channel)),
        (byte) MIDI_DATA(value), (byte) MIDI_DATA(value >> 7));
}


/****************************************************************************
*               midi_buffer
* Inputs:
*    byte * buffer: the buffer address
*    int size: number of bytes in buffer
* Returns:
*    FALSE if size is less than 16 or buffer is NULL, otherwise TRUE
* Effect: DOS, MAC:
*    tells interrupt routine to store system exclusive messages in
*    buffer.     The largest power of 2 bytes less than size will be
*    used.  xbufhead and xbuftail will be initialized to zero,
*    and xbuftail will be one greater than the index of the last
*    system exclusive byte read.  Since there may already be a buffer
*    and therefore the normal midi message buffer may have the first
*    4 bytes of some sysex messages, clear the normal midi buffer too.
* AMIGA:
*    adds buffer to midi interface
*
****************************************************************************/

boolean midi_buffer(byte huge *buffer, ulong size)
{
    if (!buffer) return FALSE;
#ifdef AMIGA
    if (!SetSysExQueue(cmt_mi, (UBYTE *) buffer, (ULONG) size)) return(false);
    cu_register(remove_sysex_buffer, buffer);
#endif

#ifdef MACINTOSH_OR_DOS
#ifndef WINDOWS
    {
    int mask = 0x000F;

    if (size < 16) return(false);
    while (mask < size && mask > 0) mask = ((mask << 1) | 1);
    midi_flush();
    xbuff = NULL;    /* turn off buffering */
    xbufmask = mask >> 1;
    xbufhead = xbuftail = 0;
    xbuff = buffer;    /* set buffer, turn on buffering */
    }
#endif
#endif
#ifdef UNIX
    return FALSE;
#else
    exclusive(TRUE);
    return TRUE;
#endif
}


/* midi_clock -- send a midi time clock message */
/**/
void midi_clock()
{
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS, "+");
    midi_write(1, 0, MIDI_TIME_CLOCK, 0, 0);
}


/****************************************************************************
*               midi_cont
* Inputs:
*    boolean onflag: TRUE or FALSE
* Effect:
*    enables (true) or disables (false) continuous control
****************************************************************************/

void midi_cont(boolean onflag)
{
    if (!initialized) fixup();
    if (onflag) {
#ifdef AMIGA
    SetMidiFilters(cmt_mi, cmt_mi->PortFilter, 
        cmt_mi->TypeFilter | CONTCONT, cmt_mi->ChanFilter);
#endif
#ifdef  DOS
#ifndef WINDOWS
    mPutCmd(BENDERON);
#endif
#endif
    } else {
#ifdef AMIGA
    SetMidiFilters(cmt_mi, cmt_mi->PortFilter,
        cmt_mi->TypeFilter & ~CONTCONT, cmt_mi->ChanFilter);
#endif
    }
#ifdef MACINTOSH_OR_DOS
    ctrlFilter = !onflag;
#endif
    if (musictrace) gprintf(TRANS,"midi_cont: %d\n", onflag);
}


/****************************************************************************
*                  midi_ctrl
* Inputs:
*    int channel: midi channel on which to send data
*    int control: control number
*    int value: control value
* Effect: 
*    Sends a midi control change message
****************************************************************************/

void midi_ctrl(int channel, int control, int value)
{
    if (!initialized) fixup();
    if (musictrace)
    gprintf(TRANS,"midi_ctrl: ch %d, ctrl %d, val %d\n",
        channel, control, value);

    midi_write(3, MIDI_PORT(channel), (byte) (MIDI_CTRL | MIDI_CHANNEL(channel)), 
          (byte) MIDI_DATA(control), (byte) MIDI_DATA(value));
}


/****************************************************************************
*                midi_exclusive
* Inputs:
*    byte *msg: pointer to a midi exclusive message, terminated by 0xF7
* Effect: 
*    Sends a midi exclusive message
* Bugs:
*   18-mar-94 PLu : This function does not know which port to send to in
*                    case of multiple midi-ports (MAC, IRIX)
****************************************************************************/

#ifdef MACINTOSH
#define INTERBYTE_DELAY 10
#endif

void midi_exclusive(msg)
unsigned char *msg; /* the data to be sent */
{
#ifdef ITC
    int count, done, tosend, willsend;
    unsigned char *m;
    mi_status ret;
#endif
#ifdef UNIX_IRIX_MIDIFNS
    unsigned char *m;
    MDevent mdevent;
#endif

#ifdef MACINTOSH
#ifndef NYQUIST
    int i;                      /* for DX7 delay loop */
    int count = 0;      /* counter for formatting midi byte trace */
    MIDIPacket TheMIDIPacket;
    unsigned char prev = 0;
    boolean first_packet = TRUE;
#endif
#endif

    /*
     *  if user mistakenly called midi_exclusive instead of exclusive,
     *  the argument will be TRUE or FALSE, both of which are highly    
     *  unlikely valid arguments for midi_exclusive:
     */

    if (msg == (byte *) FALSE || msg == (byte *) TRUE) {
    gprintf(ERROR,"midi_exclusive: invalid argument %u.\n", msg);
    EXIT(1);
    }
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS,"midi_exclusive\n");

#ifdef AMIGA
    PutSysEx(cmt_mi, msg);
#endif
#ifdef MACINTOSH
#ifndef NYQUIST /* if NYQUIST, do nothing */
#ifdef MIDIMGR
    while (prev != MIDI_EOX) {
    int len = 0;
    while (prev != MIDI_EOX && len < 249) {
        TheMIDIPacket.data[len++] = prev = *msg++;
    }
    TheMIDIPacket.len = 6 + len;
    TheMIDIPacket.tStamp = 0;
    if (first_packet && (prev != MIDI_EOX)) {
        TheMIDIPacket.flags = midiTimeStampCurrent + midiStartCont;
        first_packet = FALSE;
    } else if (first_packet) {
        TheMIDIPacket.flags = midiTimeStampCurrent + midiNoCont;
    } else if (prev == MIDI_EOX) {
        TheMIDIPacket.flags = midiTimeStampCurrent + midiEndCont;
    } else {
        TheMIDIPacket.flags = midiTimeStampCurrent + midiMidCont;
    }               
    MIDIWritePacket(OutputRefNum, &TheMIDIPacket);
    }
#else
    while (*msg != MIDI_EOX) {
    Xmit(0, *msg);
    msg++;
    count++;
    /* this is a delay loop, without which your DX7 will crash */
    for (i = INTERBYTE_DELAY; i > 0; i--)
        abort_check();
    }
    Xmit(0, MIDI_EOX);
#endif /* MIDIMGR */
#endif /* NYQUIST */
#endif /* MACINTOSH */

#ifdef DOS
#ifndef WINDOWS
    do {
    mPutData(*msg);
    } while (*msg++ != MIDI_EOX);
#endif
#endif
#ifdef ITC
    for (m = msg, tosend = 1; (*m) != MIDI_EOX; m++, tosend++);
    for (count = 0; count < tosend; count += done) {
    willsend = min(16384, tosend);
    ret = mi_exclusive(midiconn, 1, msg, (short) willsend);
    if (ret != MI_SUCCESS) {
        gprintf(GWARN, "Got %d from mi_exclusive\n", ret);
    }
    done = willsend;
    }
#endif
#ifdef UNIX_IRIX_MIDIFNS
/* we don't know which device to sent SYSEX messages to so port zero is
   assumed. */
    for (m = msg, mdevent.msglen = 1; (*m) != CMT_MIDI_EOX; m++, mdevent.msglen++);
    mdevent.sysexmsg = msg;
    if (mdSend(miport, &mdevent, 1) == -1) {
      gprintf(GWARN, "could not send SYSEX message\n");
    }
#endif

    if (miditrace) {
    do { gprintf(TRANS, "~%2x", *msg);
#ifdef UNIX_IRIX_MIDIFNS
        } while (*msg++ != CMT_MIDI_EOX);
#else
        } while (*msg++ != MIDI_EOX);
#endif
    }
}


/****************************************************************************
*                  midi_note
* Inputs:
*    int channel: midi channel on which to send data
*    int pitch: midi pitch code
*    int velocity: velocity with which to sound it (0=> release)
* Effect: 
*    Sends a midi note-play request out
****************************************************************************/

void midi_note(int channel, int pitch, int velocity)
{
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS,"midi_note: ch %d, key %d, vel %d\n",
    channel, pitch, velocity);
    if (user_scale) {
    /* check for correct pitch bend */
    if ((pit_tab[pitch].pbend != bend[MIDI_CHANNEL(channel)]) 
        && (velocity != 0)) {
        midi_bend(channel, pit_tab[pitch].pbend);
        bend[channel] = pit_tab[pitch].pbend;
    }
    pitch = pit_tab[pitch].ppitch;  
    }
    midi_write(3, MIDI_PORT(channel), (byte) (MIDI_ON_NOTE | MIDI_CHANNEL(channel)),
          (byte) MIDI_DATA(pitch), (byte) MIDI_DATA(velocity));
}


/****************************************************************************
*                midi_program
* Inputs:
*    int channel: Channel on which to send midi program change request
*    int program: Program number to send (decremented by 1 before
*           being sent as midi data)
* Effect: 
*    Sends a program change request out the channel
****************************************************************************/

void midi_program(int channel, int program)
{
#ifdef MACINTOSH
    int port, midi_chan;
#endif

    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS,"midi_program: ch %d, prog %d\n",
                channel, program);
    channel = MIDI_CHANNEL(channel);    
    if (cur_midi_prgm[channel] != program) {  
    midi_write(2, MIDI_PORT(channel), (byte) (MIDI_CH_PROGRAM | channel),
            (byte) (MIDI_PROGRAM(program)), 0);
    cur_midi_prgm[channel] = program;
    }
}


/****************************************************************************
*               midi_real
* Inputs:
*    boolean onflag: TRUE or FALSE
* Effect:
*    enables (true) or disables (false) midi realtime messages F8-FF
****************************************************************************/

void midi_real(boolean onflag)  
{
    if (!initialized) fixup();
#ifdef UNIX_ITC
    {
        mi_status ret;

        ret = mi_realtime(midiconn, onflag);
        if (ret != MI_SUCCESS) {
            gprintf(ERROR, "Warning: bad ret = %d in midi_real\n", ret);
        }
    }
#endif /* UNIX_ITC */
#ifdef ITC
    ignore_realtime = !onflag;
#endif /* ITC */
#ifdef AMIGA
    if (onflag) {
        SetMidiFilters(cmt_mi, cmt_mi->PortFilter,
        cmt_mi->TypeFilter | CMF_RealTime, cmt_mi->ChanFilter);
    } else {
        SetMidiFilters(cmt_mi, cmt_mi->PortFilter,
        cmt_mi->TypeFilter & ~CMF_RealTime, cmt_mi->ChanFilter);
    }
#endif
#ifdef MACINTOSH_OR_DOS
    realFilter = !onflag;
#endif

    if (musictrace) gprintf(TRANS,"midi_real: %d\n", onflag);
}


/* midi_start -- send a midi start message */
/**/
void midi_start()
{
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS, "`");
    midi_write(1, 0, MIDI_START, 0, 0);
}


/* midi_stop -- send a midi stop message */
/**/
void midi_stop()
{
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS, "'");
    midi_write(1, 0 /* ignored */, MIDI_STOP, 0, 0);
}


/****************************************************************************
*               midi_thru
* Inputs:
*    boolean onflag: TRUE or FALSE
* Effect:
* DOS:      enables (true) or disables (false) midi thru info from
*      MPU-401 to host.  (Default is set; reset with cmdline -block.)
* AMIGA:  enables (true) or disables (false) midi route from AMIGA
*        midi input to AMIGA midi output.
****************************************************************************/

void midi_thru(boolean onflag)  /* DMH: midi thru is not supported on the MAC or DOS */
{
    if (!initialized) fixup();
#ifndef MIDI_THRU
    gprintf(ERROR, "midi_thru called but not implemented\n");
#else
#ifdef AMIGA
    MidiThru(0L, (long) onflag);
#endif
#ifdef MACINTOSH
    /* this currently does not do anything - Mac driver doesn't
     * support THRU
     */
    do_midi_thru = onflag;
#endif
#endif

    if (musictrace) gprintf(TRANS,"midi_thru: %d\n", onflag);
}


/****************************************************************************
*                  midi_touch
* Inputs:
*    int channel: midi channel on which to send data
*    int value: control value
* Effect: 
*    Sends a midi after touch message
****************************************************************************/

void midi_touch(int channel, int value)
{
    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS,"midi_touch: ch %d, val %d\n",channel,value);
    midi_write(2, MIDI_PORT(channel), (byte) (MIDI_TOUCH | MIDI_CHANNEL(channel)),
        (byte) MIDI_DATA(value), 0);
}


/****************************************************************************
*                  midi_write
* Inputs:
*       UBYTE n: number of characters to send (1, 2 or 3);
    int port: the port number (usually 0), on MAC, this may be 1
*    char c1,c2,c3: Character(s) to write to MIDI data port
* Effect: 
*    Writes the data to the serial interface designated by port
****************************************************************************
* Change log
*  Date     | Change
*-----------+----------------------------------------------------------------
* 15-Mar-94 | PLu : Added IRIX version
****************************************************************************/
  
#ifdef UNIX
#ifdef UNIX_IRIX_MIDIFNS
void midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3)
{
  MDevent event;
  
  if (port < 0) return;

  * ((u_long *) event.msg) = 0xe0000000 | ((port & 0x1f) << 24) | (c1 << 16) | 
           (c2 << 8) | c3;
  if (mdSend(miport, &event, 1) == -1)
    gprintf(ERROR, "Can not send midi message in midi_write");

  midi_write_trace(n, port, c1, c2, c3);
}
#else

#ifdef ITC
void midi_write(int n, int port,
        unsigned char c1, unsigned char c2, unsigned char c3)
{
    unsigned char outb[3];
    mi_channel mch;
    mi_status ret;

    if (port < 0) return;
    outb[0] = c1;
    outb[1] = c2;
    outb[2] = c3;
    mch = (16*port)+((int)MI_CHANNEL(c1));
    ret = mi_put(midiconn, mch, outb);
    if (ret != MI_SUCCESS)
    gprintf(ERROR, "Warning: bad ret = %d in midi_write\n", (int)ret);
    midi_write_trace(n, port, c1, c2, c3);
}
#else
void midi_write(int n, int port, 
        unsigned char c1, unsigned char c2, unsigned char c3)
{
    /* no output */
    midi_write_trace(n, port, c1, c2, c3);
}
#endif /* ITC */
#endif /*  UNIX_IRIX */
#endif /* UNIX */

#ifdef DOS
#ifndef WINDOWS
void midi_write(int n, int port,
        unsigned char c1, unsigned char c2, unsigned char c3)
{
    if (n >= 1) mPutData(c1);
    if (n >= 2) mPutData(c2);
    if (n >= 3) mPutData(c3);
    midi_write_trace(n, port, c1, c2, c3);
}
#else
void midi_write(int n, int port, 
        unsigned char c1, unsigned char c2, unsigned char c3)
{
    midi_write_trace(n, port, c1, c2, c3);
}
#endif
#endif

#ifdef MACINTOSH
#ifdef MIDIMGR
void midi_write(int n, int port,
        unsigned char c1, unsigned char c2, unsigned char c3)
{
    MIDIPacket TheMIDIPacket;
    
    TheMIDIPacket.flags = midiTimeStampCurrent;
    TheMIDIPacket.len = 6 + n;
    TheMIDIPacket.tStamp = 0;
    TheMIDIPacket.data[0] = c1;
    TheMIDIPacket.data[1] = c2;
    TheMIDIPacket.data[2] = c3;
    MIDIWritePacket(OutputRefNum, &TheMIDIPacket);
    midi_write_trace(n, port, c1, c2, c3);
}
#else
void midi_write(int n, int port, unsigned char c1, unsigned char c2, unsigned char c3)
{
#ifndef NYQUIST
    Xmit(port, c1);
    if (n >= 2) Xmit(port, c2);
    if (n >= 3) Xmit(port, c3);
#endif
    midi_write_trace(n, port, c1, c2, c3);
}
#endif
#endif

void midi_write_trace(int n, int port,
              unsigned char c1, unsigned char c2, unsigned char c3)
{
    if (miditrace) {
    /* to indicate bytes going out on port 1, put message in brackets
     * with the port number, e.g. [1:~90~3c~64]
     */
    if (port > 0) gprintf(TRANS, "[%d:", port);
    if (n >= 1) gprintf(TRANS, "~%2x", c1);
    if (n >= 2) gprintf(TRANS, "~%2x", c2);
    if (n >= 3) gprintf(TRANS, "~%2x", c3);
    if (port > 0) gprintf(TRANS, "]", port);
    }
}



/*****************************************************************
*           set_pitch_default
*****************************************************************/

private void set_pitch_default()
{
    int i;

    for (i = 0; i < 128; i++) {
    pit_tab[i].pbend = 8192;
    pit_tab[i].ppitch = i;
    }
}

/*****************************************************************
*           read_tuning
*****************************************************************/

void read_tuning(filename)
char *filename;
{
    int index, pit, lineno = 0;
    float bend;
    FILE *fpp;

    user_scale = TRUE;
    set_pitch_default();

    fpp = fileopen(filename, "tun", "r", "Tuning definition file");
    while ((fscanf(fpp, "%d %d %f\n", &index, &pit, &bend) > 2) &&
    (lineno < 128)) {
    lineno++;
    if (index >= 0 && index <= 127) {
        pit_tab[index].pbend = (int)(8192 * bend/100 + 8192);
        pit_tab[index].ppitch = pit;
    }
    }
}


/****************************************************************************
*                  musicinit
* Effect: 
****************************************************************************/

void musicinit()
{
    int i;
    char *filename;

    if (!tune_flag) {    /* do this code only once */
    miditrace = cl_switch("miditrace");
    musictrace = cl_switch("trace");
    }
    
    if (!initialized) {
    cu_register((cu_fn_type) musicterm, NULL);
    midi_init();
    }
    initialized = TRUE;
    /* this does some random cleanup activity */

#ifndef APPLICATION
    if (!tune_flag) {    /* do this code only once */
#ifdef DOS
#ifndef WINDOWS
#if 0
    version = mPutGetCmd(GETMPUVER);
    revision = mPutGetCmd(GETMPUREV);
    gprintf(TRANS, "MPU version %d.%d%c\n", version >> 4, version & 0x0f,
        revision + 'A' - 1);
#endif
    mPutCmd(UARTMODE);
    mPutCmd(NOREALTIME);    /* initially prevent Real Time MIDI info */
    mPutCmd(EXCLUSIVOFF);   /* initially prevent Sys-Ex data */
#endif
#endif
    tune_flag = TRUE;
    filename = cl_option("tune");
    if (filename != NULL) read_tuning(filename);
    }
    /* now that flags are set, print the trace message */
    if (musictrace) gprintf(TRANS, "musicinit()\n");

    if (user_scale) {
    for (i = 0; i < MAX_CHANNELS; i++) {
        midi_bend(i, 8192);
        bend[i] = 8192;
    }
    }
#endif /* ifndef APPLICATION */

    for (i = 0; i < MAX_CHANNELS; i++) {
    /* initialize to impossible values so that the
     * next call to midi_bend or midi_program will
     * not match and therefore send an output:
     */
    bend[i] = -1;
    cur_midi_prgm[i] = -1;
    }
#ifdef MIDI_THRU
    midi_thru(!(cl_switch("block")));    /* set MIDI thru */
#endif
    timereset();            /* Reset clock */
#ifdef AMIGA
    event_mask |= (1L << ascii_signal()) | (1L << cmt_mi->AlarmSigBit) |
          (1L << cmt_mi->RecvSigBit);
#endif
}


/****************************************************************************
*                  musicterm
* Effect: 
*     Miscellaneous cleanup of things done by musicinit.
****************************************************************************/

private void musicterm()
{
    if (musictrace) gprintf(TRANS, "musicterm()\n");
    initialized = FALSE;
}


/****************************************************************************
*                   cmtrand
* Inputs:
*    int lo: Lower limit of value
*    int hi: Upper limit of value
* Result: int
*    random number (lo <= result <= hi)
****************************************************************************/

long randseed = 1534781L;

short cmtrand(short lo, short hi)
{
    randseed *= 13L;
    randseed += 1874351L;
    return((short)(lo + (((hi + 1 - lo) * ((0x00ffff00 & randseed) >> 8)) >> 16)));
}


#ifdef AMIGA
/* remove_sysex_buffer -- a cleanup procedure for the Amiga */
/**/
void remove_sysex_buffer(void *obj)
{
    ClearSysExQueue(cmt_mi);
}
#endif /* AMIGA */


/****************************************************************************
*                  settime
* Inputs: new time
* Effect: 
*    Sets the current time to the new time.
*        DMH: for MAC, sets the clock to absTime
*                 implemented by adjusting ticksATStart
****************************************************************************/

void settime(newtime)
  time_type newtime;
{
    if (musictrace) gprintf(TRANS, "settime(%lu)\n", newtime);
#ifdef AMIGA
    timeoffset = *camdtime - (newtime >> 1);
#endif

#ifdef MACINTOSH
#ifdef MIDIMGR
    ticksAtStart = MIDIGetCurTime(OutputRefNum);
#else
    ticksAtStart = TickCount() - MS_TO_TICKS(newtime);
#endif
#endif  
}

/****************************************************************************
*                  timereset
* Effect: 
*    Resets the time.
*       DMH: for MAC, implemented by setting ticksAtStart to
*            current value of system tick counter
*       JMN: for DOS, resets the time on the MPU-401. Ticks is reset to 0
****************************************************************************/

void timereset()
{
#if HAS_GETTIMEOFDAY
    struct timeval timeval;
#endif
#if HAS_FTIME
    struct timeb ftime_res;
#endif

    if (!initialized) fixup();
    if (musictrace) gprintf(TRANS,"timereset()\n");

#ifdef AMIGA
    timeoffset = *camdtime;
#endif

#ifdef DOS
#ifndef WINDOWS
    timeoffset = (ulong) readtimer();
#endif
#endif

#ifdef MACINTOSH
#ifdef MIDIMGR
    ticksAtStart = MIDIGetCurTime(OutputRefNum);
#else
    ticksAtStart = TickCount();
#endif
#endif
    
#if HAS_GETTIMEOFDAY
    gettimeofday(&timeval, 0);
    timeoffset = timeval.tv_sec * 1000 + timeval.tv_usec / 1000 - timeoffset;
#endif
#if HAS_FTIME
    ftime(&ftime_res);
    timeoffset = ftime_res.time;
#endif
}


/****************************************************************************
*                  trace
* Inputs:
*    boolean flag: TRUE for trace on
* Effect: 
*    turns tracing on (flag == TRUE) or off (flag == FALSE)
****************************************************************************/

void trace(boolean flag)
{
    musictrace = flag;
}

/****************************************************************************
*                  tracemidi
* Inputs:
*    boolean flag: TRUE for trace on
* Effect: 
*    turns midi tracing on (flag == TRUE) or off (flag == FALSE)
****************************************************************************/

void tracemidi(boolean flag)
{
    miditrace = flag;
}



/***********************************************************************
*
* midi and timer initialization
*
***********************************************************************/

#ifdef  DOS

/* binary value of hex char */

private int xval(int c)
{
    int i;
    static char t[]="0123456789abcdef";

    for (i=0; i<16; i++)
        if(tolower(c)==t[i]) return(i);
    return (-1);
}

/* binary value of hex string */

private int atox(char *t)
{
    int             i=0;
    int             x;
    while(*t)
    {
        if ((x=xval(*t++))<0)return (0);
        i=(i<<4)+x;
    }
    return (i);
}
#endif  /* def DOS */


private void midi_init()
{
#ifdef UNIX_IRIX_MIDIFNS
#define PBUFLEN 4
  MIconfig *config;
  static u_int pbuf[] = { MI_STAMPING, MINOSTAMP, MI_BLOCKING, MINONBLOCKING};
#endif

#ifdef UNIX_MACH
    mach_midi_init();
#else
#ifdef ITC
    midiconn = mi_open(NULL);
    if (midiconn == NULL) {
    gprintf(FATAL, "could not open a MIDI device\n");
    EXIT(1);
    }
    cu_register((cu_fn_type) mi_close, (void *) midiconn);
#endif
#endif
#ifdef AMIGA
    amiga_midi_init();
#endif /* def AMIGA */
#ifdef DOS
#ifndef WINDOWS
    int err;
    int irq=SEARCHIRQ;
    int base=MPUBASEADDR;
    char *t;

    if (t=getenv("MPUIRQ")) {
    if (musictrace)
        gprintf(TRANS,"MPUIRQ %s\n",t);
    irq=atoi(t);
    }
    if (t=getenv("MPUBASE")) {
    if (musictrace)
        gprintf(TRANS,"MPUBASE %s\n",t);
    base=atox(t);
    }
    if(err = mOpen(base, irq)) {
    mClose(err);
    EXIT(1);
    }
    cu_register((cu_fn_type) mClose, 0);
    cu_register((cu_fn_type) mPutCmd, (cu_parm_type) MPURESET);
    initializetimer();
    cu_register((cu_fn_type) restoretimer, NULL);
#endif
#endif

#ifdef MACINTOSH
#ifndef NYQUIST /* if NYQUIST, do nothing */
#ifdef MIDIMGR
    setup_midimgr(); /* this registers itself for cleanup */
#else
    init_abort_handler();
    cu_register(cleanup_abort_handler, NULL);
    setupMIDI(portA, 0x80);
    cu_register(restoreMIDI, (long) portA);
    /* only initialize portB if necessary */
    if (MAX_CHANNELS > CHANNELS_PER_PORT) {
    setupMIDI(portB, 0x80);
    cu_register(restoreMIDI, (long) portB);
    }
#endif
#endif /* NYQUIST */
#ifdef MIDIMGR
    ticksAtStart = MIDIGetCurTime(OutputRefNum);
#else
    ticksAtStart = TickCount(); /* reset the clock */
#endif
#endif /* def MACINTOSH */

    if (!(cl_switch("noalloff")))
    cu_register((cu_fn_type) alloff, NULL);
}

#ifdef  DOS
/****************************************************************************
*                                  set_x_mfr
* Inputs:
*       unsigned char mfr: Manufacturer ID for MIDI
* Result: void
*       
* Effect: 
*       Sets the xcode and xcodemask to allow only these sysex messages
****************************************************************************/

void set_x_mfr(mfr)
unsigned char mfr;
{
    xcode = mfr;
    xcodemask = 0xFF;
}

/****************************************************************************
*                                 clear_x_mfr
* Result: void
*       
* Effect: 
*       Clears sysex manufacturer code filter; accepts all sysex messages
****************************************************************************/

void clear_x_mfr()
{
    xcode = 0;
    xcodemask = 0;
}
#endif /* DOS */