A
download MKConductor.m
Language: ObjectiveC
Copyright: (c) 1988-1992, NeXT Computer, Inc. (c) 1999-2004 The MusicKit Project. (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT (c) 1994 Stanford University
LOC: 1377
Project Info
The MusicKit(musickit)
Server: SourceForge
Type: cvs
...sicKit\Frameworks\MusicKit\
   _ArielQP.h
   _DSPMK.h
   _error.h
   _midi.h
   _MKAppProxy.h
   _MKAppProxy.m
   _MKNameTable.h
   _MKNameTable.m
   _MKParameter.h
   _MTCHelper.h
   _MTCHelper.m
   _musickit.h
   _noteRecorder.h
   _OrchloopbeginUG.h
   _OrchloopbeginUG.m
   _ParName.h
   _ParName.m
   _scorefile.h
   _ScorefileVar.h
   _ScorefileVar.m
   _SharedSynthInfo.h
   _SharedSynthInfo.m
   _synthElementMethods.m
   _time.h
   ArielQP.h
   ArielQP.m
   classFuncs.h
   ConductorPrivate.h
   dsp_types.h
   DSPSerialPortDevice.h
   dspwrap.h
   EnvelopePrivate.h
   equalTempered.m
   errors.h
   fastFFT.c
   fastFFT.h
   GNUmakefile.postamble
   GNUmakefile.preamble.in
   InstrumentPrivate.h
   keynums.h
   libMusicKit.def
   Localized.strings
   make.sh
   Makefile.postamble
   Makefile.preamble
   midi_spec.h
   midifile.h
   midifile.m
   MidiPrivate.h
   midiTranslation.h
   MKConductor.h
   MKConductor.m
   MKConductorDelegate.h
   MKDeviceStatus.h
   MKEnvelope.h
   MKEnvelope.m
   MKFilePerformer.h
   MKFilePerformer.m
   MKFileWriter.h
   MKFileWriter.m
   MKInstrument.h
   MKInstrument.m
   MKMidi.h
   MKMidi.m
   MKMixerInstrument.h
   MKMixerInstrument.m
   MKMTCPerformer.h
   MKMTCPerformer.m
   MKNote.h
   MKNoteFilter.h
   MKNoteFilter.m
   MKNoteReceiver.h
   MKNoteReceiver.m
   MKNoteSender.h
   MKNoteSender.m
   MKOrchestra.h
   MKPart.m
   MKPartials.h
   MKPartials.m
   ...tialsWaveshapingTable.m
   MKPartPerformer.h
   MKPartPerformer.m
   MKPartRecorder.h
   MKPartRecorder.m
   MKPatch.h
   MKPatch.m
   MKPatchConnection.h
   MKPatchConnection.m
   MKPatchEntry.h
   MKPatchEntry.m
   MKPatchTemplate.h
   MKPatchTemplate.m
   MKPerformer.h
   MKPerformer.m
   MKPerformerDelegate.h
   MKPlugin.h
   MKSamplerInstrument.h
   MKSamplerInstrument.m
   MKSamples.h
   MKScore.h
   MKScore.m
   MKScorefileObject.h
   MKScorefilePerformer.h
   MKScorefilePerformer.m
   MKScorefileWriter.m
   MKScorePerformer.h
   MKScorePerformer.m
   MKScoreRecorder.h
   MKScoreRecorder.m
   MKSynthData.h
   MKSynthData.m
   MKSynthInstrument.h
   MKSynthPatch.h
   MKTimbre.h
   MKTimbre.m
   MKTuningSystem.h
   MKTuningSystem.m
   MKUnitGenerator.h
   MKUnitGenerator.m
   MKWaveTable.h
   MKWaveTable.m
   mtcMidi.m
   mtcMidiPrivate.m
   MusicKit-Info.plist
   MusicKit.h
   MusicKitConfig.h.in
   names.h
   noDVal.h
   noteDispatcherMethods.m
   NotePrivate.h
   noteRecorderCFuncs.m
   noteRecorderMethods.m
   orch.h
   OrchestraPrivate.h
   OrchloopbeginUG.h
   OrchloopbeginUG.m
   orchloopbeginUGInclude.m
   params.h
   parNames.m
   partialsDBInclude.m
   PartialsPrivate.h
   PartPerformerPrivate.h
   PartPrivate.h
   PartRecorderPrivate.h
   PatchTemplatePrivate.h
   PB.project
   PerformerPrivate.h
   pitches.h
   platform.make
   ...efilePerformerPrivate.h
   ScorePerformerPrivate.h
   ScorePrivate.h
   ScoreRecorderPrivate.h
   SynthDataPrivate.h
   synthElementCFuncs.m
   synthElementMethods.m
   SynthInstrumentPrivate.h
   SynthPatchList.h
   SynthPatchList.m
   SynthPatchPrivate.h
   timetagInclude.m
   timeunits.h
   tokenNames.m
   tokens.h
   trigonometry.c
   trigonometry.h
   TuningSystemPrivate.h
   UnitGeneratorPrivate.h
   win32-def.top
   writeBinary.m
   writeMidi.m
   writeScore.m

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
/*
  $Id: MKConductor.m,v 1.35 2005/04/15 04:18:24 leighsmith Exp $
  Defined In: The MusicKit

  Description:
    This is the MusicKit scheduler. See documentation for details. 
    Note that, in clocked mode, all timing is done with a single NSTimer "timed entry." 

    In this version, you must use the FoundationKit NSRunLoop if you are in clocked mode. You may,
    however, use the unClocked mode, although its purpose is now deprecated.

  Original Author: David Jaffe

  Copyright (c) 1988-1992, NeXT Computer, Inc.
  Portions Copyright (c) 1994 NeXT Computer, Inc. and reproduced under license from NeXT
  Portions Copyright (c) 1994 Stanford University
  Portions Copyright (c) 1999-2004 The MusicKit Project.
*/
/*
  Original pre MusicKit.org project modification history. For current history, 
  check the CVS log on musickit.org.

  09/15/89/daj - Added caching of inverse of beatSize. 
  09/19/89/daj - Unraveled inefficient MIN(MAX construct.
  09/25/89/daj - Added check for common case in addConductor.
  01/03/90/daj - Added check for performanceIsPaused in adjustTime and
                 _adjustTimeNoTE:. Made jmp_buf be static.
		 Optimized insertMsgQueue() a bit.
  01/07/90/daj - Changed to use faster time stamps.		 
		 Changed comments. addConductor() name changed to 
		 repositionCond()
  01/31/90/daj - Moved _jmpSet = YES; in unclocked loop to fix bug:
                 Memory exception when empty unclocked conducted performance 
		 following an unclocked performance that wasn't empty. 
		 (bug 4451). Added extraInstanceVars mechanism so that
		 we can forever remain backward compatable.
  02/01/90/daj - Added comments. Added check for inPerformance in
                 MKCancelMsgRequest() to fix bug whereby if finishPerformance
		 is triggered by the repositionCond(), condQueue was nil and
		 you got a memory fault.
  03/13/90/daj - Fixed bugs involving pause/resume and timeOffset 
                 (much thanks to lbj). 
                 Fixed bug in MKRescheduleMsgRequest() whereby 
		 the end of performance was erroneously being triggered.
		 Also added MKRepositionMsgRequest().
  	         Moved private methods to a catagory.
  03/19/90/daj - Added pauseFor:.
  03/23/90 lbj - Fixed pauseFor: -- 
  		Put in a return-if-negative-arg predicate and changed the 
		recipient of the resume msgReq to clockConductor (it was self).
  03/23/90/daj - Added check for isPaused in insertMsgQueue() and -emptyQueue
  03/27/90/daj - Added delegate, against my better judgement, at the request
                 of lbj.
  04/21/90/daj - Small mods to get rid of -W compiler warnings.
  04/27/90/daj - Commented out MKSetTime(0) (see changes in time.m) 
  06/10/90/daj - Added _MKAdjustTimeIfNotBehind() for Orchestra's synchTime.
  07/27/90/daj - Added seperate thread mechanism.  See lock.m.
  07/27/90/daj - Moved all checks of the form if (!allConductors) condInit()
                 that were in factory methods into +initialize.
  08/20/90/daj - Added delegate methods for crossing high/low deltaT thresholds.
  08/30/90/daj - Fixed bug that caused empty performance to crash if tempo
                 isn't 60! (Changes to initializeConductorList). Also changed float compares
		 of MK_ENDOFTIME and ENDOFLIST to be safer.
  09/02/90/daj - Changed MAXDOUBLE references to noDVal.h way of doing things
  09/29/90/daj - Changed to coincide with new way of doing separate thread 
                 loop. Also fixed bug in forking of thread!
  12/12/90/daj - Fixed big memory leak in conductor body.
  04/22/91/daj - Changed separate-threaded finishPerformance to do longjmp
  02/06/92/daj - Added bulletproofing against messages scheduled for negative
                 times.  (See masterConductorBody).
  02/25/92/daj - Changed adjustBeat to allow for floating point round off error in 
                 timeOffset calc.
  11/02/92/daj - Changes for MTC
  11/09/92/daj - Changed beatToClock and adjustBeat.  Changes to pause/resume, etc.
  11/16/92/daj - Bumped archiving version to 3 and added archiving of delegate.
                 We don't archive activePerformers because MKPerformer doesn't archive
		 it's status--unarchived performers always come up inactive.
		 We don't archive MTCSynch because MKMidi (still!) doesn't support 
		 archiving. (Sigh.)
*/
#import <AppKit/NSApplication.h>
#import "_musickit.h"
#import "_time.h"
#import "MidiPrivate.h"
#import "_MTCHelper.h"
#import "_MKAppProxy.h"
#import "ConductorPrivate.h"

#define MK_INLINE 1
#define COUNT_MSG_QUEUE_LENGTH 0

static BOOL separateThread = NO;
static NSTimer *timedEntry = NOTIMEDENTRY; /* Only used for DPS client mode */
					   /* sb: now NSTimer instead of void*. Not DPS. */
static BOOL inPerformance = NO;   /* YES if we're currently in performance. */
static BOOL dontHang = YES;       /* NO if we are expecting asynchronous input, e.g. MIDI or mouse */
static BOOL isClocked = YES;      /* YES if we should stay synced up to the clock.
				     NO if we can run as fast as possible. */
static NSDate * startTime = nil;  /* Start of performance time. */
static NSDate * pauseTime = nil;  /* Time last paused. */
static double clockTime = 0.0;    /* Clock time. */
static double oldClockTime = 0;

static MKMsgStruct *afterPerformanceQueue = NULL; /* end-of-time messages. */
static MKMsgStruct *_afterPerformanceQueue = NULL; /* same but private */
static MKMsgStruct *beforePerformanceQueue = NULL;/* start-of-time messages.*/
static MKMsgStruct *afterPerformanceQueueEnd = NULL; /* end-of-time messages.*/
static MKMsgStruct *_afterPerformanceQueueEnd = NULL; /* same but private */
static MKMsgStruct *beforePerformanceQueueEnd = NULL;/* start-of-time msgs.*/
static BOOL performanceIsPaused = NO; /* YES if the entire performance is
					 paused. */
static id classDelegate = nil;  /* Delegate for the whole class. */

@implementation MKConductor

/* METHOD TYPES
 * Creating and freeing Conductors
 * Querying the object
 * Modifying the object
 * Controlling a performance
 * Tempo and timeOffset
 * Requesting messages
 */

static MKConductor *curRunningCond = nil; /* Or nil if no running conductor. */
static MKConductor *condQueue = nil;   /* Head of conductor queue. */
static MKConductor *defaultCond = nil; /* default Conductor. */
// TODO this should be static but has to be extern for categories to be able to access it.
MKConductor *clockCond = nil;   /* clock time Conductor. */

#define NORMALCOND (unsigned char)0
#define CLOCKCOND (unsigned char)1
#define DEFAULTCOND (unsigned char)2

#define VERSION2 2
#define VERSION3 3

static NSMutableArray *allConductors = nil; /* An array of all conductors. */
static void condInit();    /* Forward decl */

+ (void) initialize
{
    if (self != [MKConductor class])
	return;
    [MKConductor setVersion: VERSION3];//sb: suggested by Stone conversion guide (replaced self)
    if (!allConductors)
        condInit();
    // we assume we are being called first from the main thread - if not, we are in trouble.
    [self detachDelegateMessageThread];
    return;
}

#import "separateThread.m"

static MKMsgStruct *evalSpecialQueue();

/*
 LMS these are SB's notes, perhaps redundant.
 startTime is absolute date (NSDate *)
 nextmsgtime: relative time to start of performance
 clockTime: relative to start of performance
 pauseTime is absolute
*/

/* The following implements the delta T high water/low water notification */
static double deltaTThresholdLowPC = .25; /* User sets this */
static double deltaTThresholdHighPC = .75;/* User sets this */

static double deltaTThresholdLow = 0;     
static double deltaTThresholdHigh = 0;    
static BOOL delegateRespondsToThresholdMsgs = NO; 
static BOOL delegateRespondsToLowThresholdMsg = NO;
static BOOL delegateRespondsToHighThresholdMsg = NO;
static BOOL lowThresholdCrossed = NO;

void MKSetLowDeltaTThreshold(double percentage)
{
    deltaTThresholdLowPC = percentage;
    [MKConductor _adjustDeltaTThresholds];
}

void MKSetHighDeltaTThreshold(double percentage)
{
    deltaTThresholdHighPC = percentage;
    [MKConductor _adjustDeltaTThresholds];
}

double _MKTheTimeToWait(double nextMsgTime)
{
    double t;
//    t = getTime();  /* Current time */
//    t -= startTime; /* current time relative to the start of performance. */
    t = [[NSDate date] timeIntervalSinceDate:startTime]; //sb: replaced previous 2 lines.
    t = nextMsgTime - t; /* relative time */
    t = MIN(t, MK_ENDOFTIME); /* clip */
    if (delegateRespondsToThresholdMsgs) {
        if (t < deltaTThresholdLow && !lowThresholdCrossed) {
            if (delegateRespondsToLowThresholdMsg)
                [classDelegate conductorCrossedLowDeltaTThreshold];
            lowThresholdCrossed = YES;
        }
        else if (t > deltaTThresholdHigh && lowThresholdCrossed) {
            if (delegateRespondsToHighThresholdMsg)
                [classDelegate conductorCrossedHighDeltaTThreshold];
            lowThresholdCrossed = NO;
        }
    }
    t = MAX(t,0);
    return t; /*sb: returns relative time... */
}

static void adjustTimedEntry(double nextMsgTime)
    /* The idea here is that we always calibrate by clock time. Therefore
       we can't accumulate errors. We subtract the difference between 
       where we are and where we should be. It is assumed that time is
       already updated. */
{
    // NSLog(@"Adjusting timed entry %lf %d, %d, %d, %d\n", nextMsgTime, !inPerformance, performanceIsPaused, musicKitHasLock(), !isClocked);
    if ((!inPerformance) || (performanceIsPaused) || (musicKitHasLock()) || (!isClocked)) 
        return;  /* No timed entry, s.v.p. */
    if (separateThread)
        sendMessageToWakeUpMKThread();
    else {
        if (timedEntry != NOTIMEDENTRY) {
            [timedEntry invalidate];
            [timedEntry release];
        }
        timedEntry = [[NSTimer timerWithTimeInterval: _MKTheTimeToWait(nextMsgTime) 
			       target: [MKConductor class]
			       selector: @selector(masterConductorBody:)
			       userInfo: nil
			       repeats: NO] retain];
        [[NSRunLoop currentRunLoop] addTimer: timedEntry forMode: _MK_DPSPRIORITY];
    }
}

BOOL checkForEndOfTime()
{
    if ((dontHang || (!isClocked)) && ISENDOFTIME(condQueue->nextMsgTime) && mtcEndOfTime()) {
        [MKConductor finishPerformance];
        return YES;
    } 
    return NO;
}

void repositionCond(MKConductor *cond, double nextMsgTime)
/*
 Enqueue a MKConductor (this happens every time a new message is 
scheduled.)

cond is the conductor to be enqueued.  nextMsgTime is the next
post-mapped time that the conductor wants to run.  If we're not in
performance, just sets cond->nextMsgTime.  Otherwise, enqueues cond at
the appropriate place, ordered by time. If, after adding the conductor, the head of the
queue is MK_ENDOFTIME and if we're not hanging, sends
+finishPerformance. If the newly enqueued conductor is added at the head of the list, calls adjustTimedEntry().
Question is: where do we retain cond? I presume it is assumed cond is already retained.
 */
{
    MKConductor *tmp;
    register double t;
    
    t = MIN(nextMsgTime, MK_ENDOFTIME);
    t = MAX(t, clockTime);
    cond->nextMsgTime = t;
    if (!inPerformance)
	return;
    /* remove conductor from doubly-linked list. */
    if (cond == condQueue) { /* It's first */
	if ((!cond->_condNext) || (t < ((MKConductor *)cond->_condNext)->nextMsgTime)) { 
	    /* It's us again. */
	    /* We use < to avoid doing an adjustTimedEntry if possible. */
	    /* No need to reposition. */
	    if (!curRunningCond)  /* See comment below */
		adjustTimedEntry(t);
	    if (!cond->_condNext)
		checkForEndOfTime();
	    /* We only need to check for ENDOFTIME if !cond->_condNext.
		* If cond->_condNext != nil, then we used the second part of
		* the conditional above--i.e. we know that nextMsgTime < 
		* ENDOFTIME.
		*/
	    return;
	}
	/* Remove us from queue. No need to set pointers in cond because
	they're going to be set below. */
	condQueue = cond->_condNext;
	condQueue->_condLast = nil;
    }
    else { 
	/* Remove cond from queue. No need to set pointers in cond because
	they're going to be set below. */
	if (cond->_condLast) /* The first time, this can be nil */
	    ((MKConductor *)cond->_condLast)->_condNext = cond->_condNext;
	if (cond->_condNext)
	    ((MKConductor *)cond->_condNext)->_condLast = cond->_condLast;
    }
    /* Now add it. */
    if ( // (!condQueue) || 
	 (t < condQueue->nextMsgTime)) { /* Add at the start of queue? */
	 /* We use < to avoid doing an adjustTimedEntry if possible. */
	 /* This can only happen if curRunningCond == self or if
	 nobody's running. In the first case, the timed entry is 
	 added by masterConductorBody. In the second, we add it
	 below. */
	 tmp = condQueue;
	 condQueue = cond;
	 cond->_condNext = tmp;
	 cond->_condLast = nil;
	 //	if (tmp)
	 tmp->_condLast = cond;
	 if (!curRunningCond)
	     adjustTimedEntry(t); /* Nobody's running and we're not in setup. */
	 return;                /* No need to check for ENDOFTIME because otherwise, t wouldn't be < nextMsgTime */
    }
    else {
	for (tmp = condQueue; (tmp->_condNext && (((MKConductor *)tmp->_condNext)->nextMsgTime <= t)); tmp = tmp->_condNext)
	    ;
	/* tmp is now first one before us and tmp->_condNext is the first one
	       after us, if any. */
	cond->_condLast = tmp;
	if (tmp->_condNext)
	    ((MKConductor *)tmp->_condNext)->_condLast = cond;
	cond->_condNext = tmp->_condNext;
	tmp->_condNext = cond;
    }	
    checkForEndOfTime();
}

#if i386 && defined(__NeXT__)  /* There's a Pentium optimization bug that is tickled here */
#pragma CC_OPT_OFF
#endif

/* Conversion from beat time to clock time.
   This function assumes that self has been adjusted with adjustBeat. */
double beatToClock(MKConductor *self, double newBeat)
{
    double newClockTime;
    
    if (ISENDOFLIST(newBeat))
	return MK_ENDOFTIME;
    /* The formula is MAX(mapFun(newBeat) + pauseOffset, clockTime) */
    if (self == clockCond)
	return MAX(newBeat, clockTime);
    if (DELEGATE_RESPONDS_TO(self, BEAT_TO_CLOCK)) {
	newClockTime = [self->delegate beatToClock: newBeat from: self] + self->_pauseOffset;
    }
    else { /* Use built-in version */
	double beatDiff;
	double adjustedBaseTime;
	
	/* The point of this subtracting and then adding in again is to
	    * make sure that if pauseOffset > clockTime, the music waits
	    * appropriately.
	    */
	adjustedBaseTime = clockTime - self->_pauseOffset;
	if (adjustedBaseTime < 0) 
	    adjustedBaseTime = 0;
	beatDiff = newBeat - self->time;
	newClockTime = (MAX(beatDiff, 0.0) * self->beatSize + adjustedBaseTime + self->_pauseOffset);
    }
    return MAX(newClockTime, clockTime);
}

#if i386 && defined(__NeXT__)
#pragma CC_OPT_ON
#endif

static void adjustBeat(MKConductor *self)
    /* Given clock time, adjust internal state to reflect current time. */
{
    double adjustedClockTime,x;
    if (self == clockCond) 
      self->time += clockTime - oldClockTime;
    else if (self->isPaused) 
      self->_pauseOffset += (clockTime - oldClockTime);
    else {
	adjustedClockTime = clockTime - self->_pauseOffset;
	if (DELEGATE_RESPONDS_TO(self,CLOCK_TO_BEAT)) 
	  x = [self->delegate clockToBeat:adjustedClockTime from:self];
	else { /* Use built-in version */
          x = self->time + (adjustedClockTime - self->oldAdjustedClockTime) * self->inverseBeatSize;
          self->oldAdjustedClockTime = MAX(adjustedClockTime,0);
	}
	if (x > self->time)
	  self->time = x;
    }
}

static void setTime(double newTime)
/* Adjusts beats of all conductors and resets time. */
{
    register MKConductor *cond;
    
    oldClockTime = clockTime;
    clockTime = newTime;
    // NSLog(@"Setting clockTime to %lf\n", clockTime);
    if (curRunningCond) {
  	for (cond = curRunningCond->_condNext; cond != nil; cond = cond->_condNext)
	    adjustBeat(cond);
 	for (cond = curRunningCond->_condLast; cond != nil; cond = cond->_condLast)
	    adjustBeat(cond);
	/* Need to set oldAdjustedClockTime for the running conductor */
	if (curRunningCond != clockCond && !DELEGATE_RESPONDS_TO(curRunningCond, CLOCK_TO_BEAT)) {
	    double x = clockTime - curRunningCond->_pauseOffset;
	    
            curRunningCond->oldAdjustedClockTime = MAX(x, 0);
	}
    }
    else {
	for (cond = condQueue; cond != nil; cond = cond->_condNext)
	    adjustBeat(cond);
    }
}

void adjustTime()
/* Normally, the variable time jumps in discrete amounts. However,
   sometimes, as, for example, when an asynchronous event such as
   MIDI or a mouse-click is received, it is desirable to adjust time 
   to reflect the current time. 
   This function adjustTime() attempts to serve this need. It will set
   the conductors clockTime to either the current system time or the current clockTime.
   The current value of clockTime is set. */
{
    double time;
//    time = getTime() - startTime;
    time = [[NSDate date] timeIntervalSinceDate:startTime]; //sb: replaced previous line.
    /* Don't allow it to exceed next scheduled msg. This insures that 
       multiple adjustTimes (e.g. for flurry of MIDI events) don't push 
       scheduled events into the past. (The event loop may favor port action
       over timed entries. Even though it's not obvious, experiments have 
       shown that it's better to do this clip. Otherwise notes are completely
       omitted, whether or not preemption is used (because envelopes are
       "stepped on" out of existance). */
    // NSLog(@"adjusting Time %lf condQueue->nextMsgTime = %lf\n", time, condQueue->nextMsgTime);
    time = MIN(time, condQueue->nextMsgTime);
    // NSLog(@"after to minimum Time %lf\n", time);
    time = MAX(time, clockTime); /* Must be more than the previous time, otherwise we're moving time backwards */
    // NSLog(@"after gating maximum Time %lf clockTime = %lf, oldClockTime = %lf\n", time, clockTime, oldClockTime);
    setTime(time);
}

BOOL _MKAdjustTimeIfNotBehind(void)
/* Normally, the variable time jumps in discrete amounts. However,
   sometimes, as, for example, when an asynchronous event such as
   MIDI or a mouse-click is received, it is desirable to adjust time 
   to reflect the current time.  AdjustTime serves this function.
   Returns the current value of clockTime. */
{
    double time;
//    time = getTime() - startTime;
    time = [[NSDate date] timeIntervalSinceDate:startTime]; //sb: replaced previous line.
    /* Don't allow it to exceed next scheduled msg. This insures that 
       multiple adjustTimes (e.g. for flurry of MIDI events) don't push 
       scheduled events into the past. (The event loop may favor port action
       over timed entries. Even though it's not obvious, experiments have 
       shown that it's better to do this clip. Otherwise notes are completely
       omitted, whether or not preemption is used (because envelopes are
       "stepped on" out of existance). */
    if (time > condQueue->nextMsgTime)
      return NO;
    time = MAX(time,clockTime); /* Must be more than previous time. */
    setTime(time);
    return YES;
}

+ (double) timeInSeconds
    /* Returns the time in seconds as viewed by the clock conductor.
       Same as [[MKConductor clockConductor] time]. 
       Returns MK_NODVAL if not in
       performance. Use MKIsNoDVal() to check for this return value.  */
{
    return (inPerformance) ? clockTime : MK_NODVAL;
}

/* Convenience class methods to set the delta time using the MKConductor class */
+ (void) setDeltaT: (double) newDeltaT
{
    MKSetDeltaT(newDeltaT);
}

/* Convenience class methods to return the delta time using the MKConductor class */
+ (double) deltaT
{
    return MKGetDeltaT();
}

/* Run until done. */
/* FIXME Might want to check for events here. */
static void unclockedLoop(void)
{
    while(inPerformance) {
        [MKConductor masterConductorBody: nil];
    }
}

/* In addition to the regular message queues, there are several special 
   message queues. E.g. there's one for before-performance messages, one
   for after-performance messages, and so on. These are handled somewhat
   differently. E.g. we don't use back links
   and we cancel just by setting object field to nil. */
static MKMsgStruct *
insertSpecialQueue(sp,queue,queueEnd)
    register MKMsgStruct *sp;
    MKMsgStruct *queue;
    register MKMsgStruct **queueEnd;
    /* insert at end of special msgQueues used for start and end messages */
{
    if (!sp)
        return queue;
    sp->_onQueue = YES;
    if (*queueEnd) {
	(*queueEnd)->_next = sp;
	(*queueEnd) = sp;
	sp->_next = NULL;
    }
    else {
	sp->_next = NULL;
	*queueEnd = sp;
	queue = sp;
    }
    sp->_conductor = nil; /* nil signals special queue */
    return queue;
}

static id insertMsgQueue(register MKMsgStruct *sp, MKConductor *self)
/* inserts in msgQueue and changes timed entry if necessary. */
{
    register double t;
    register MKMsgStruct * tmp;
    if (!sp)
        return nil;
    t = MIN(sp->_timeOfMsg,MK_ENDOFTIME);
    t = MAX(t,self->time);
    sp->_onQueue = YES;
    if ((t < PEEKTIME(self->_msgQueue)) || (!self->_msgQueue->_next)) { 
	sp->_next = self->_msgQueue;
	sp->_prev = NULL;
	sp->_next->_prev = sp;
	self->_msgQueue = sp;
	if (!self->isPaused)
	  repositionCond(self,beatToClock(self,t)); 
	/* Only need to add yourself if this message is the next one. */
    }
    else {
#if 0
	/* Commented out because version below is faster */
	for (tmp = self->_msgQueue; (t >= tmp->_next->_timeOfMsg); 
	     tmp = tmp->_next)
	  ;
	/* insert after tmp */
	sp->_next = tmp->_next;
	sp->_next->_prev = sp;
	tmp->_next = sp;	
	sp->_prev = tmp;
#endif
	for (tmp = self->_msgQueue->_next; (t >= tmp->_timeOfMsg); 
	     tmp = tmp->_next)
	  ;
	/* insert before tmp */
	sp->_next = tmp;
	sp->_prev = tmp->_prev;
	tmp->_prev = sp;	
	sp->_prev->_next = sp;
    }	
    sp->_conductor = self;
#if COUNT_MSG_QUEUE_LENGTH
    {
	static int maxQueueLen = 0;
	int i;
	for (i = 0, tmp = self->_msgQueue; tmp != NULL; tmp = tmp->_next, i++)
	  if (i > maxQueueLen) {
	      NSLog(@"Message max queue length == %d\n",i);
	      maxQueueLen = i;
	  }
    }
#endif
    return self;
}

/* Why do I call MKMsgStructs "sp" you ask? I don't know!
   Perhaps it means "struct pointer"? */

#define SPCACHESIZE 64  
static MKMsgStruct *spCache[SPCACHESIZE];
static unsigned spCachePtr = 0;

#define USECACHE 1

#if USECACHE
static MKMsgStruct *allocSp()
    /* alloc a new sp. */
{
    MKMsgStruct *sp;
    if (spCachePtr) 
      sp = spCache[--spCachePtr]; 
    else _MK_MALLOC(sp,MKMsgStruct,1);
    return sp;
}

static void freeSp(sp)
    MKMsgStruct * sp;
    /* If cache isn't full, cache sp, else free it. 
       Be careful not to freeSp the same sp twice! */
{
    if (spCachePtr < SPCACHESIZE) 
      spCache[spCachePtr++] = sp;
    else free(sp);
}

#else

static MKMsgStruct *allocSp()
    /* alloc a new sp. */
{
    MKMsgStruct *sp;
    _MK_MALLOC(sp,MKMsgStruct,1);
    return sp;
}

static void freeSp(sp)
    MKMsgStruct * sp;
{
    free(sp);
}

#endif

static MKMsgStruct * 
popMsgQueue(MKMsgStruct **msgQueue)
    /* Pop and return first element in process queue. 
       msgQueue is a pointer to the process queue head. */
{
    register MKMsgStruct * sp;
    sp = *msgQueue;		/* Pop msgQueue. */
    if ((*msgQueue = (*msgQueue)->_next))
      (*msgQueue)->_prev = NULL;
    return(sp);
}

// forward declaration
static MKMsgStruct *newMsgRequest(BOOL,double,SEL,id,int,id,BOOL,id,BOOL);

static void condInit()
/*sb: changed from void to (id)callingObj because I need to tell initializeBackgroundThread()
 which object the main conductor is, so that the conductor can be sent objC messages from the background thread.
 */
{
    allConductors = [NSMutableArray new];
    defaultCond   = [MKConductor new];
    clockCond     = [MKConductor new];
    /* This actually works ok for +new. The first time +new is called,
       it creates defaultCond, clockCond and the new Cond. */
    initializeBackgroundThread();
    // NSLog(@"condInit(): allConductors = %@, defaultCond = %@, clockCond = %@\n", allConductors, defaultCond, clockCond);
}

- (void) dealloc
{
    /* LMS: In the past, freeing a conductor was not permitted. I'm keeping this error only so 
    long as there hasn't been research to allow MKConductor instances to dealloc themselves. */
    [self doesNotRecognizeSelector:_cmd];
    [super dealloc];
}

- _initialize
    /* Private method that initializes the Conductor when it is created
       and after it finishes performing. Sent by self only. Returns self.
       BeatSize is not reset. It retains its previous value. */
{	
    pauseFor = MKCancelMsgRequest(pauseFor);
    /* timeOffset is inititialized to 0 because it's an instance var. Nowdays so are the rest, but so what? */
    oldAdjustedClockTime = 0;
    time = 0;	
    nextMsgTime = 0;
    _pauseOffset = 0;
    isPaused = NO;
    /* If the end-of-list marker is ever sent, it will print an error. (It's a bug if this ever happens.) */
    if (!_msgQueue)
        _msgQueue = newMsgRequest(CONDUCTORFREES,ENDOFLIST, @selector(_error:), self,
                                1, @"MKConductor's end-of-list was erroneously evaluated (this shouldn't happen).\n", TRUE,nil,FALSE);
    // Remove links. We don't want to leave links around because they screw up repositionCond.
    // The links are added at the last minute in initializeConductorList.
    _condLast = _condNext = nil; 
    return self;
}

+ allocWithZone:(NSZone *)zone {
    if (inPerformance)
      return nil;
    self = [super allocWithZone:zone];
    return self;
}

+ alloc {
    if (inPerformance)
      return nil;
    self = [super alloc];
    return self;
}

- init
  /* TYPE: Creating; Creates a new Conductor.
   * Creates and returns a new Conductor object with a tempo of
   * 60 beats a minute.
   * If inPerformance is YES, does nothing
   * and returns nil.
   */
{
    self = [super init];
    if (self) {
	/* Initialize instance variables here that are only intiailized upon
	   creation. Initialize instance variables that are reinitialized for
	   each performance in the _initialize method. */
	MKConductor *oldLast = [allConductors lastObject];
	
	if (oldLast == self) /* Check for an attempt to init twice */
	    return nil;
	if (![allConductors containsObject: self])
	    [allConductors addObject: self];
	if ([allConductors lastObject] != self)
	    return nil; /* Attempt to init twice */
	activePerformers = [[NSMutableArray allocWithZone: [self zone]] init];
	beatSize = 1;
	pauseFor = NULL; 
	delegateFlags = 0;
	inverseBeatSize = 1;
	_msgQueue = NULL;
	[self _initialize];
    }
    return self;
}

- copyWithZone:(NSZone *)zone
  /* Same as [[self class] allocFromZone:zone] followed by [self init]. */
{
    id obj;
    obj = [[self class] allocWithZone:zone]; 
    [obj init];
    return obj;
}

+ adjustTime
  /* TYPE: Modifying; Updates the current time.
   * Updates the factory's notion of the current time.
   * This method should be invoked whenever 
   * an asychronous event (such as a mouse, keyboard, or MIDI
   * event) takes place. The MidiIn object sends adjustTime for you.
   */
{
    if (inPerformance)
	adjustTime();
    return self;
}

static void initializeConductorList()
  /* Very private function. Makes the conductor list with much hackery. */
{
    /* These hacks are to keep repositionCond() from triggering
       finishPerformance or adding timed entries while sorting list. */
    BOOL clk = isClocked;
    BOOL noHng = dontHang;
    
    dontHang = NO;
    isClocked = YES;
    curRunningCond = clockCond;
    condQueue = clockCond; 
    /* Set head of queue to an arbitrary conductor. Sorting is done by _runSetup. */
    [allConductors makeObjectsPerformSelector: @selector(_runSetup)];
    dontHang = noHng;
    isClocked = clk;
    curRunningCond = nil;
}

+ startPerformance
  /* TYPE: Managing; Starts a performance.
   * Starts a Music Kit performance.  All Conductor objects
   * begin at the same time.
   * In clocked mode, the Conductor assumes the use of the AppKit's 
   * default NSRunLoop is running.
   * If you have not yet sent the -run message to your application, or if 
   * NSApp has not been created, startPerformance does nothing and returns nil.
   */
{
    if (inPerformance)
        return self;

    _MKSetConductedPerformance(YES, self);
    inPerformance = YES;   /* Set this before doing initializeConductorList() so that repositionCond() works right. */
    [self _adjustDeltaTThresholds]; /* For automatic notification */
    setTime(clockTime = 0.0);       // this forces oldClockTime to 0.0 also.
    initializeConductorList();          // Was before setTime()
    setupMTC();
    if (MKIsTraced(MK_TRACECONDUCTOR))
        NSLog(@"Evaluating the beforePerformance queue,%s separate threaded,%s clocked.\n",
	      separateThread ? "" : " not", isClocked ? "" : " not");
    beforePerformanceQueue = evalSpecialQueue(beforePerformanceQueue, &beforePerformanceQueueEnd);
    if (!isClocked && !separateThread) {
	timedEntry = NOTIMEDENTRY;
	unclockedLoop();
	return self;
    }
    if (checkForEndOfTime()) {
	return self;
    }
    if (!separateThread) {
        setPriority();
        /*sb: I am assuming that self, in a class method, equals the class method. */
        timedEntry = [[NSTimer timerWithTimeInterval: condQueue->nextMsgTime
					      target: [self class]
					    selector: @selector(masterConductorBody:)
					    userInfo: nil
					     repeats: YES] retain];
        [[NSRunLoop currentRunLoop] addTimer: timedEntry forMode: _MK_DPSPRIORITY];
    }
    [startTime autorelease];
    startTime = [[NSDate date] retain];
    if (MKGetDeltaTMode() == MK_DELTAT_SCHEDULER_ADVANCE) {
        [startTime autorelease];
        startTime = [[startTime addTimeInterval: (0 - MKGetDeltaT())] retain];
    }
    if (separateThread) {
	launchThread();
    }
    return self;
}

+ (MKConductor *) defaultConductor
  /* TYPE: Querying; Returns the defaultConductor. 
   * Returns the defaultConductor.
   */
{ 	
    return defaultCond;
}

+ (BOOL) inPerformance
  /* TYPE: Querying; Returns YES if a performance is in session.
   * Returns YES if a performance is currently taking
   * place.
   */
{
    return inPerformance;
}

static void evalAfterQueues()
    /* Calls evalSpecialQueue for both the private and public after-performance
       queues. */
{
    if (MKIsTraced(MK_TRACECONDUCTOR))
        NSLog(@"Evaluating afterPerformance queue.\n");
   _afterPerformanceQueue = evalSpecialQueue(_afterPerformanceQueue, &_afterPerformanceQueueEnd);
   afterPerformanceQueue = evalSpecialQueue(afterPerformanceQueue, &afterPerformanceQueueEnd);
}

+ finishPerformance
  /* TYPE: Modifying; Ends the performance.
   * Stops the performance.  All enqueued messages are  
   * flushed and the afterPerformance message is sent
   * to the factory.  Returns nil if it was in performance, nil
   * otherwise.
   *
   * If the performance doesn't hang,
   * the factory automatically sends the finishPerformance
   * message to itself when the message queue is exhausted.
   * It can also be sent by the application to terminate the
   * performance prematurely.
   */
{	
    double lastTime;
    
    if (MKIsTraced(MK_TRACECONDUCTOR))
        NSLog(@"finishPerformance,%s separate threaded,%s in performance.\n", separateThread ? "" : " not", inPerformance ? "" : " not");
    if (!inPerformance) {
	evalAfterQueues(); /* This is needed for MKFinishPerformance() */
	return nil;
    }
    performanceIsPaused = NO;
    _MKSetConductedPerformance(NO,self);
    inPerformance = NO; /* Must be set before -emptyQueue is sent */
    [allConductors makeObjectsPerformSelector: @selector(emptyQueue)];
    if (separateThread)
        removeTimedEntry(exitThread);
    else if (timedEntry != NOTIMEDENTRY) {
        [timedEntry invalidate];
        [timedEntry release];
    }
    if (!separateThread)
        resetPriority();
    timedEntry = NOTIMEDENTRY;
    // condQueue is being set to nil, before the MusicKit thread has finished up.
    // however this is not that tragic as the inPerformance = NO will stop
    // masterConductorBody doing anything harmful.
    condQueue = nil;
    lastTime = clockTime;
    setTime(clockTime = 0.0);  // this forces oldClockTime to 0.0 also.
    resetMTCTime();
    //   MKSetTime(0.0); /* Handled by _MKSetConductedPerformance now */
    oldClockTime = lastTime;
    [allConductors makeObjectsPerformSelector: @selector(_initialize)];
    evalAfterQueues();

    return self;
}

+ pausePerformance
  /* TYPE: Controlling; Pauses a performance.
   * Pauses all Conductors.  The performance is resumed when
   * the factory receives the resume message.
   * The factory object's notion of the current time is suspended
   * until the performance is resumed.
   * It's illegal to pause an unclocked performance. Returns nil in this
   * case, otherwise returns the receiver.
   * Note that pausing a performance does not pause MidiIn, nor does it
   * pause any Instruments that have their own clocks. (e.g. MidiOut, and
   * the MKOrchestra).
   */
{	
   if ((!inPerformance)  || performanceIsPaused)
       return self;
   if (!isClocked || weGotMTC())
       return nil;
   [pauseTime autorelease];
   pauseTime = [[NSDate date] retain];
   if (separateThread)
       removeTimedEntry(pauseThread);
   else if (timedEntry != NOTIMEDENTRY) {
       [timedEntry invalidate];
       [timedEntry release];
   }
   timedEntry = NOTIMEDENTRY;
   performanceIsPaused = YES;
   return self;
}

+ (BOOL) isPaused
  /* TYPE: Querying; YES if performance is paused.
   * Returns YES if the performance is paused.
   */
{
    return performanceIsPaused;
}

+ resumePerformance
  /* TYPE: Controlling; Unpauses a paused performance.
   * Resumes a paused performance.
   * When the performance resumes, the notion of the
   * current time will be the same as when the factory
   * received the pause message --
   * time stops while the performance
   * is paused. It's illegal to resume a performance that's not clocked.
   * Returns nil in this case, otherwise returns the receiver.
   */
{	
    if ((!inPerformance) || (!performanceIsPaused))
	return self;
    if (!isClocked)
	return nil;
    performanceIsPaused = NO;
    [startTime autorelease];
//    startTime += (getTime() - pauseTime);
    startTime = [[startTime addTimeInterval:[[NSDate date] timeIntervalSinceDate:pauseTime]] retain];
    /* We use cur-start to get the time since the start of the performance
       with all pauses removed. Thus by increasing startTime by the
       paused time, we remove the effect of the pause. */
    adjustTimedEntry(condQueue->nextMsgTime); 
    return self;
}

+ currentConductor
  /* TYPE: Querying; Returns the Conductor that's sending a message, if any.
   * Returns the Conductor that's in the process
   * of sending a message, or nil if no message
   * is currently being sent.
   */
{
    return curRunningCond;
}

+ setFinishWhenEmpty: (BOOL) yesOrNo
  /* TYPE: Modifying; Sets BOOL for continuing with empty queues.
   * If yesOrNo is NO (the default), the performance
   * is terminated when all the Conductors' message queues are empty.
   * If YES, the performance continues until the finishPerformance
   * message is sent.
   */
{
    dontHang = yesOrNo;
    return self;
}


+ setClocked: (BOOL) yesOrNo 
  /* TYPE: Modifying; Establishes clocked or unclocked performance.  
   * If yesOrNo is YES, messages are dispatched
   * at specific times.  If NO, they are sent 
   * as quickly as possible.
   * It's illegal to invoke this method while the performance is in progress.
   * Returns nil in this case, else self.
   * Initialized to YES.  
   */
{	
    if (inPerformance && ((yesOrNo && (!isClocked)) || ((!yesOrNo) && isClocked)))
        return nil;
    isClocked = yesOrNo;
    return self;
}

+ (BOOL) isEmpty
  /* TYPE: Querying; YES if in performance and all queues are empty.
   * YES if the performance is active and all message queues are empty. 
   * (This can only happen if setFinishWhenEmpty:NO was sent.)
   */
{
    return ((!dontHang) && (inPerformance) && 
	    (!condQueue || ((ISENDOFTIME(condQueue->nextMsgTime) &&
			     mtcEndOfTime()))));
}

+ (BOOL) finishWhenEmpty
  /* TYPE: Querying; YES if performance continues despite empty queues.
   * Returns NO if the performance is terminated when the
   * queues are empty, NO if the performance continues.
   */
{
    return dontHang;
}

+ (BOOL) isClocked
  /* TYPE: Querying; YES if performance is clocked, NO if not.
   * Returns YES if messages are sent at specific times,
   * NO if they are sent as quickly as possible.
   */
{	
    return isClocked;
}

- activePerformers
{
    return activePerformers;
}

- (BOOL) isPaused 
  /* Returns YES if the receiver is paused. */
{
    return isPaused;
}


- pause
  /* TYPE: Controlling; Pauses the receiver.
   * Pauses the performance of the receiver.
   * The effect on the receiver is restricted to
   * the present performance;
   * paused Conductors are automatically resumed at the end of each
   * performance.
   * Returns the receiver.
   */
{
    if (self == clockCond || MTCSynch)
	return nil;
    if (isPaused)
	return self;
    isPaused = YES;
    repositionCond(self, PAUSETIME);
    if ([delegate respondsToSelector: @selector(conductorDidPause:)])
	[delegate conductorDidPause: self];
    return self;
}

- resume
  /* TYPE: Controlling; Resumes a paused receiver.
   * Resumes the receiver.  If the receiver isn't currently paused
   * (if it wasn't previously sent the pause message),
   * this has no effect.
   */
{
    if (MTCSynch)
	return nil;
    if (!isPaused)
	return self;
    [self _resume];
    if ([delegate respondsToSelector: @selector(conductorDidResume:)])
	[delegate conductorDidResume: self];
    return self;
}

- pauseFor: (double) seconds
{
    /* Pause it if it's not already paused. */
    if (seconds <= 0.0 || ![self pause]) 
      return nil;
    if (pauseFor)	/* Already doing a "pauseFor"? */
      MKRepositionMsgRequest(pauseFor,clockTime + seconds);
    else {             /* New "pauseFor". */
	pauseFor = MKNewMsgRequest(
	            clockTime + seconds, @selector(resume),self,0);
	MKScheduleMsgRequest(pauseFor, clockCond); 
    }
    return self;
}

+ clockConductor
  /* TYPE: Querying; Returns the clockConductor. 
   * Returns the clockConductor.
   */
{ 	
    return clockCond;
}

- (double) setBeatSize: (double) newBeatSize
  /* TYPE: Tempo; Sets the tempo by resizing the beat.
   * Sets the tempo by changing the size of a beat to newBeatSize,
   * measuredin seconds.  The default beat size is 1.0 (one beat per
   * second).
   * Attempts to set the beat size of the clockConductor are ignored. 
   * Returns the previous beat size.
   */
{
    register double oldBeatSize = beatSize;
    
    if (self == clockCond)
	return oldBeatSize;
    beatSize = newBeatSize;
    inverseBeatSize = 1.0 / beatSize;
    if (curRunningCond != self && !isPaused) 
	repositionCond(self, beatToClock(self, PEEKTIME(self->_msgQueue)));
    return oldBeatSize;
}

- (double) setTempo: (double) newTempo
  /* TYPE: Tempo; Sets the tempo in beats per minute.
   * Sets the tempo to newTempo, measured in beats per minute.
   * Implemented as [self\ setBeatSize:(60.0/newTempo)].
   * Attempts to set the tempo of the clockConductor are ignored. 
   * Returns the previous tempo.
   */
{
    return 60.0 / [self setBeatSize: (60.0 / newTempo)];
}

- (double) setTimeOffset: (double) newTimeOffset
  /* TYPE: Tempo; Sets the receiver's timeOffset value in seconds.
   * Sets the number of seconds into the performance
   * that the receives waits before it begins processing
   * its message queue.  Notice that newtimeOffset is measured
   * in seconds -- it's not affected by the receiver's
   * tempo.
   * Attempts to set the timeOffset of the clockConductor are ignored. 
   * Returns the previous timeOffset.
   */
{
    double oldTimeOffset = timeOffset;
    if (self == clockCond)
      return timeOffset;
    else if (MTCSynch) {
	[self->MTCSynch _setMTCOffset:newTimeOffset];
	timeOffset = newTimeOffset;
	return oldTimeOffset;
    }
    if (inPerformance) 
      _pauseOffset = newTimeOffset - oldTimeOffset;
    timeOffset = newTimeOffset;
    if (curRunningCond != self && !isPaused) 
      repositionCond(self,beatToClock(self,PEEKTIME(self->_msgQueue)));
    return oldTimeOffset;
}

- (double) beatSize
  /* TYPE: Tempo; Returns the receiver's beat size in seconds.
   * Returns the size of the receiver's beat, in seconds.
   */
{
    return beatSize;
}

- (double) tempo
  /* TYPE: Tempo; Returns the receiver's tempo in beats per minute.
   * Returns the receiver's tempo in beats per minute.
   */
{
    return 60.0 * inverseBeatSize;
}

- (double) timeOffset
  /* TYPE: Tempo; Returns the receiver's timeOffset.
   * Returns the receiver's timeOffset value.
   */
{
    return timeOffset;
}

// sendSelector:
- sel: (SEL) aSelector
   to: (id) toObject 
withDelay: (double) deltaT 
 argCount: (int) argCount, ...;
/* TYPE: Requesting; Requests aSelector to be sent to toObject.
 * Schedules a request for the receiver
 * to send the message aSelector to the
 * object toObject at time deltaT beats from now.
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * separated by commas (up to two arguments are allowed).
 * Returns the receiver unless argCount is too high, in which case returns nil.
 */
{
    id arg1,arg2;
    va_list ap;
    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    va_end(ap);
    return insertMsgQueue(
        newMsgRequest(CONDUCTORFREES, self->time + deltaT, aSelector, toObject, argCount, arg1, FALSE, arg2, FALSE), self);
}

-sel:(SEL)aSelector to:(id)toObject withDelay:(double)deltaT 
 argCount:(int)argCount
 arg1:(id)arg1 retain:(BOOL)retainArg1
 arg2:(id)arg2 retain:(BOOL)retainArg2;

/* TYPE: Requesting; Requests aSelector to be sent to toObject.
 * Schedules a request for the receiver
 * to send the message aSelector to the
 * object toObject at time deltaT beats from now.
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * separated by commas (up to two arguments are allowed).
 * Returns the receiver unless argCount is too high, in which case returns nil.
 */
{
    return
      insertMsgQueue(
        newMsgRequest(CONDUCTORFREES, self->time + deltaT,
		aSelector, toObject, argCount, arg1, retainArg1, arg2, retainArg2), self);
}

-sel:(SEL)aSelector to:(id)toObject atTime:(double)t
 argCount:(int)argCount, ...;
/* TYPE: Requesting; Requests aSelector to be sent to toObject.
 * Schedules a request for the receiver to send 
 * the message aSelector to the object toObject at
 * time t beats into the performance (offset by the
 * receiver's timeOffset).
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * seperated by commas (up to two arguments are allowed).
 * Returns the receiver unless argCount is too high, in which case returns 
 * nil. 
 */
{
    id arg1,arg2;
    va_list ap;
    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    va_end(ap);	
    return insertMsgQueue(
        newMsgRequest(CONDUCTORFREES, t, aSelector, toObject, argCount, arg1, FALSE, arg2, FALSE), self);
}

-sel:(SEL)aSelector to:(id)toObject atTime:(double)t
 argCount:(int)argCount
 arg1:(id)arg1 retain:(BOOL)retainArg1
 arg2:(id)arg2 retain:(BOOL)retainArg2;
/* TYPE: Requesting; Requests aSelector to be sent to toObject.
 * Schedules a request for the receiver to send 
 * the message aSelector to the object toObject at
 * time t beats into the performance (offset by the
 * receiver's timeOffset).
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * seperated by commas (up to two arguments are allowed).
 * Returns the receiver unless argCount is too high, in which case returns 
 * nil. 
 */
{
    return insertMsgQueue(
       newMsgRequest(
         CONDUCTORFREES, t, aSelector, toObject, argCount, arg1, retainArg1, arg2, retainArg2), self);
}

- _runSetup
  /* Private method that inits a Conductor to run. */
{
    _pauseOffset = timeOffset;
    if (MTCSynch)
        return [self _pause];
    if (ISENDOFLIST(PEEKTIME(_msgQueue))) {
        nextMsgTime = MK_ENDOFTIME;
    }
    else {
//	nextMsgTime = PEEKTIME(_msgQueue) * beatSize + timeOffset;
//	nextMsgTime = MIN(nextMsgTime,MK_ENDOFTIME);    
        nextMsgTime = beatToClock(self, PEEKTIME(_msgQueue));
    }
    repositionCond(self, nextMsgTime);
    return self;
}

- (double) predictTime: (double) beatTime
  /* TYPE: Querying; Returns predicted time corresponding to beat.
   * Returns the time, in tempo-mapped time units,
   * corresponding to a specified beat time.  In our Conductor, this method
   * just assumes the tempo is a constant between now and beatTime.  That
   * is, it keeps no record of how tempo has changed, nor does it know the
   * future; it just computes based on the current time and tempo.  
   * More sophisticated tempo mappings an be added by subclassing
   * conductor. See "Ensemble Aspects of Computer Music", by David Jaffe,
   * CMJ (etc.) for details of tempo mappings. 
   */
{
    return beatToClock(self, beatTime);
}    

- (double) timeInBeats
  /* TYPE: Querying; Returns the receiver's notion of the current time.
   * Returns the receiver's notion of the current time
   * in beats.
   */
{	
    return self->time;
}

- emptyQueue
  /* TYPE: Requesting; Flushes the receiver's message queue.
   * Flushes the receiver's message queue and returns self.
   */
{
    register MKMsgStruct *curProc;
    while (!ISENDOFLIST(PEEKTIME(_msgQueue))) {
	curProc = popMsgQueue(&(_msgQueue));
        if (curProc) { /* This test shouldn't be needed */
            if (curProc->_conductorFrees)
                freeSp(curProc);
            else
                curProc->_onQueue = NO;
        }
    }
    if (!isPaused)
	repositionCond(self,MK_ENDOFTIME);
    return self;
}

- (BOOL) isCurrentConductor
  /* TYPE: Querying; YES if the receiver is sending a message.
   * Returns YES if the receiver
   * is currently sending a message.
   */
{
    return (curRunningCond == self);
}    

/* The following functions and equivalent methods
   give the application more control over 
   scheduling. In particular, they allow scheduling requests to be
   unqueued. They work as follows:

   MKNewMsgRequest() creates a new MKMsgStruct.
   MKScheduleMsgRequest() schedules up the request.
   MKCancelMsgRequest() cancels the request.

   Unless a MKCancelMsgRequest() is done, it is the application's 
   responsibility to NX_FREE the structure. On the other hand,
   if a  MKCancelMsgRequest() is done, the application must relinquish
   ownership of the MKMsgStruct and should not NX_FREE it.
   */

static void freeSp();

MKMsgStruct *MKCancelMsgRequest(MKMsgStruct *aMsgStructPtr)
    /* Cancels MKScheduleMsgRequest() request and frees the structure. 
       Returns NULL.
       */
{
    if (!aMsgStructPtr)
	return NULL;
    if (!aMsgStructPtr->_conductor) { /* Special queue */
	/* Special queue messages are cancelled differently. Here we just
	   set the _toObject field to nil and leave the struct in the list. */
	if (!aMsgStructPtr->_toObject) /* Already canceled? */
	    return NULL;
        [aMsgStructPtr->_toObject release];
        if (aMsgStructPtr->_retainArg1) 
	    [aMsgStructPtr->_arg1 release];
        if (aMsgStructPtr->_retainArg2)
	    [aMsgStructPtr->_arg2 release];
	aMsgStructPtr->_toObject = nil;
	aMsgStructPtr->_arg1 = nil;
	aMsgStructPtr->_arg2 = nil;
	if (aMsgStructPtr->_onQueue) 
	    aMsgStructPtr->_conductorFrees = YES;
	else
	    freeSp(aMsgStructPtr);
	return NULL;
    }
    if (aMsgStructPtr->_onQueue) { /* Remove from ordinary queue */
	aMsgStructPtr->_next->_prev = aMsgStructPtr->_prev;
	if (aMsgStructPtr->_prev)
	    aMsgStructPtr->_prev->_next = aMsgStructPtr->_next;
	else {
	    MKConductor *conductor = aMsgStructPtr->_conductor;
	    conductor->_msgQueue = aMsgStructPtr->_next;
	    if ((curRunningCond != conductor) && !conductor->isPaused) {
		/* If our conductor is the running conductor, then 
		   repositionCond will be called by him so there's no need 
		   to do it here. */
		BOOL wasHeadOfQueue;
		double nextTime = beatToClock(conductor,
					      PEEKTIME(conductor->_msgQueue));
		wasHeadOfQueue = (conductor == condQueue);
		/* If we're the head of the queue, then the message we've
		   just deleted is enqueued to us with a timed entry. We've
		   got to do an adjustTimedEntry. */
		repositionCond(conductor,nextTime); 
		/* Need to check for inPerformance below because
		   repositionCond() can call finishPerformance. */
		/* If curRunningCond is non-nil, adjustTimedEntry will be 
		   done for us by masterConductorBody. */
		if (inPerformance && !curRunningCond && wasHeadOfQueue)
		    adjustTimedEntry(condQueue->nextMsgTime);
	    }
	}
    }
    [aMsgStructPtr->_toObject release];
    if (aMsgStructPtr->_retainArg1)
	[aMsgStructPtr->_arg1 release];
    if (aMsgStructPtr->_retainArg2)
	[aMsgStructPtr->_arg2 release];
    freeSp(aMsgStructPtr);
    return NULL;
}

MKMsgStruct *
MKNewMsgRequest(double timeOfMsg,SEL whichSelector,id destinationObject,
		int argCount,...)
    /* Creates a new msgStruct to be used with MKScheduleMsgRequest. 
       args may be ids or ints. The struct returned by MKNewMsgRequest
       should not be altered in any way. Its only use is to pass to
       MKCancelMsgRequest() and MKScheduleMsgRequest(). */
{
    id arg1,arg2;
    va_list ap;
    va_start(ap,argCount);
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    va_end(ap);
    return 
        newMsgRequest(
	    TARGETFREES, timeOfMsg, whichSelector, destinationObject,
	    argCount, arg1, FALSE, arg2, FALSE);
}	

MKMsgStruct *
MKNewMsgRequestWithObjectArgs(double timeOfMsg,SEL whichSelector,id destinationObject,
		int argCount, id arg1, BOOL retainArg1, id arg2, BOOL retainArg2)
    /* Creates a new msgStruct to be used with MKScheduleMsgRequest. 
       args may be ids or ints. If objects, specify TRUE as the "isObj" field
       next to each object, so that the conductor will retain the arguments until
       after they have been used.
       The struct returned by MKNewMsgRequest
       should not be altered in any way. Its only use is to pass to
       MKCancelMsgRequest() and MKScheduleMsgRequest(). */
{
    return 
        newMsgRequest(
	    TARGETFREES, timeOfMsg, whichSelector, 
	    destinationObject, argCount, arg1, retainArg1, arg2, retainArg2);
}	


void MKScheduleMsgRequest(MKMsgStruct *aMsgStructPtr, id conductor)
    /* Reschedule the specified msg. */
{
    if (aMsgStructPtr && conductor && (!aMsgStructPtr->_onQueue))
      insertMsgQueue(aMsgStructPtr,conductor);
}

MKMsgStruct *MKRescheduleMsgRequest(MKMsgStruct *aMsgStructPtr,id conductor,
				    double timeOfNewMsg,SEL whichSelector,
				    id destinationObject,int argCount,...)
    /* Reschedules the MKMsgStruct pointed to by aMsgStructPtr as indicated.
       If aMsgStructPtr is non-NULL and points to a message request currently
       in the queue, first cancels that request. Returns a pointer to the
       new MKMsgStruct. This function is equivalent to the following
       three function calls:
       MKNewMsgRequest() // New one
       MKScheduleMsgRequest() // New one 
       MKCancelMsgRequest() // Old one

       Note that aMsgStructPtr may be rescheduled with a different conductor,
       destinationObject and arguments, 
       than those used when it was previously scheduled.
       */
{
    MKMsgStruct *newMsgStructPtr;
    id arg1,arg2;
    va_list ap;
    va_start(ap,argCount);
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    va_end(ap);
    newMsgStructPtr = 
        newMsgRequest(
	    TARGETFREES, timeOfNewMsg, whichSelector, destinationObject,
	    argCount, arg1, FALSE, arg2, FALSE);
    MKScheduleMsgRequest(newMsgStructPtr,conductor);
    MKCancelMsgRequest(aMsgStructPtr); /* A noop if already canceled. */
    return aMsgStructPtr = newMsgStructPtr;
}

MKMsgStruct *MKRescheduleMsgRequestWithObjectArgs(MKMsgStruct *aMsgStructPtr,id conductor,
				    double timeOfNewMsg,SEL whichSelector,
				    id destinationObject,int argCount,
				    id arg1, BOOL retainArg1,
				    id arg2, BOOL retainArg2)
    /* Reschedules the MKMsgStruct pointed to by aMsgStructPtr as indicated.
       If aMsgStructPtr is non-NULL and points to a message request currently
       in the queue, first cancels that request. Returns a pointer to the
       new MKMsgStruct. This function is equivalent to the following
       three function calls:
       MKNewMsgRequest() // New one
       MKScheduleMsgRequest() // New one 
       MKCancelMsgRequest() // Old one

       Note that aMsgStructPtr may be rescheduled with a different conductor,
       destinationObject and arguments, 
       than those used when it was previously scheduled.
       */
{
    MKMsgStruct *newMsgStructPtr;
    newMsgStructPtr = 
        newMsgRequest(
	    TARGETFREES, timeOfNewMsg, whichSelector, destinationObject,
	    argCount, arg1, retainArg1, arg2, retainArg2);
    MKScheduleMsgRequest(newMsgStructPtr,conductor);
    MKCancelMsgRequest(aMsgStructPtr); /* A noop if already canceled. */
    return aMsgStructPtr = newMsgStructPtr;
}

MKMsgStruct *MKRepositionMsgRequest(MKMsgStruct *aMsgStructPtr,
				    double timeOfNewMsg)
{
    MKMsgStruct *newMsgStructPtr = newMsgRequest(TARGETFREES, timeOfNewMsg,
						    aMsgStructPtr->_aSelector,
						    aMsgStructPtr->_toObject,
						    aMsgStructPtr->_argCount,
						    aMsgStructPtr->_arg1,
						    aMsgStructPtr->_retainArg1,
						    aMsgStructPtr->_arg2,
						    aMsgStructPtr->_retainArg2);
    MKScheduleMsgRequest(newMsgStructPtr, aMsgStructPtr->_conductor);
    MKCancelMsgRequest(aMsgStructPtr);
    return aMsgStructPtr = newMsgStructPtr;
}

static MKMsgStruct *newMsgRequest(
    BOOL doesConductorFree,
    double timeOfMsg,
    SEL whichSelector,
    id destinationObject,
    int argCount,
    id arg1,
    BOOL retainArg1,
    id arg2,
    BOOL retainArg2)
    /* Create a new msg struct */
{
    MKMsgStruct *sp = allocSp();
    
    if ((sp->_conductorFrees = doesConductorFree) == TARGETFREES)
	sp->_methodImp = [destinationObject methodForSelector: whichSelector];
    sp->_timeOfMsg = timeOfMsg;
    sp->_aSelector = whichSelector;
    sp->_toObject = [destinationObject retain];
    sp->_argCount = argCount;
    sp->_next = NULL;
    sp->_prev = NULL;
    sp->_conductor = nil;
    sp->_onQueue = NO;
    sp->_retainArg1 = retainArg1;
    sp->_retainArg2 = retainArg2;
    switch (argCount) {
    default: 
	freeSp(sp);
	return NULL;
    case 2:
        sp->_arg2 = arg2;
	if (retainArg2)
	    [arg2 retain];
    case 1:
        sp->_arg1 = arg1;
	if (retainArg1)
	    [arg1 retain];
    case 0:
        break;
    }
    return(sp);
}	

+(MKMsgStruct *)afterPerformanceSel:(SEL)aSelector 
 to:(id)toObject 
 argCount:(int)argCount, ...;
/* TYPE: Requesting; Sends aSelector to toObject after performance.
 * Schedules a request for the factory object
 * to send the message aSelector to the object
 * toObject immediately after the performance ends, regardless
 * of how it ends.
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * seperated by commas.  Up to two arguments are allowed.
 * AfterPerfomance messages are sent in the order they were enqueued.
 * Returns a pointer to an MKMsgStruct that can be
 * passed to cancelMsgRequest:.
 */
{
    MKMsgStruct *sp;
    id arg1,arg2;
    va_list ap;

    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    afterPerformanceQueue = 
      insertSpecialQueue(sp = newMsgRequest(CONDUCTORFREES, MK_ENDOFTIME,
					    aSelector, toObject, argCount,
					    arg1, FALSE, arg2, FALSE),
			 afterPerformanceQueue,&afterPerformanceQueueEnd);
    va_end(ap);

    return(sp);
}

+(MKMsgStruct *)afterPerformanceSel:(SEL)aSelector 
 to:(id)toObject 
 argCount:(int)argCount
 arg1:(id)arg1 retain:(BOOL)retainArg1
 arg2:(id)arg2 retain:(BOOL)retainArg2
/* TYPE: Requesting; Sends aSelector to toObject after performance.
 * Schedules a request for the factory object
 * to send the message aSelector to the object
 * toObject immediately after the performance ends, regardless
 * of how it ends.
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves, and
 * a flag to say whether or not they should be retained/released.
 * Up to two arguments are allowed.
 * AfterPerfomance messages are sent in the order they were enqueued.
 * Returns a pointer to an MKMsgStruct that can be
 * passed to cancelMsgRequest:.
 */
{
    MKMsgStruct *sp;
    afterPerformanceQueue = 
      insertSpecialQueue(sp = newMsgRequest(CONDUCTORFREES, MK_ENDOFTIME,
					    aSelector, toObject, argCount,
					    arg1, retainArg1,
					    arg2, retainArg2),
			 afterPerformanceQueue,&afterPerformanceQueueEnd);

    return(sp);
}

+(MKMsgStruct *)beforePerformanceSel:(SEL)aSelector 
 to:(id)toObject 
 argCount:(int)argCount, ...;
/* TYPE: Requesting; Sends aSelector to toObject before performance.
 * Schedules a request for the factory object
 * to send the message aSelector to the object
 * toObject immediately before performance begins.
 * argCount specifies the number of arguments to 
 * aSelector followed by the arguments themselves,
 * seperated by commas.  Up to two arguments are allowed.
 * Messages requested through this method will be sent
 * before any other messages. beforePerformance messages are sent in the
 * order they were enqueued.
 * Returns a pointer to an MKMsgStruct that can be
 * passed to cancelMsgRequest:.
 */
{
    MKMsgStruct *sp;
    id arg1,arg2;
    va_list ap;

    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    beforePerformanceQueue = 
      insertSpecialQueue(sp = newMsgRequest(CONDUCTORFREES,0.0,
					    aSelector,toObject,argCount,
					    arg1,FALSE, arg2, FALSE),
			 beforePerformanceQueue,&beforePerformanceQueueEnd);
    va_end(ap);
    return(sp);
}

+(MKMsgStruct *)beforePerformanceSel:(SEL)aSelector 
 to:(id)toObject 
 argCount:(int)argCount
 arg1:(id)arg1 retain:(BOOL)retainArg1
 arg2:(id)arg2 retain:(BOOL)retainArg2
/* TYPE: Requesting; Sends aSelector to toObject before performance.
 * Schedules a request for the factory object
 * to send the message aSelector to the object
 * toObject immediately before performance begins.
 * argCount specifies the number of arguments to 
 * aSelector. the "retain" arguments specify whether or not
 * the conductor will attempt to retain the (object) arguments
 * before use, and release them afterwards. This is highly recommended
 * for object arguments, but will CRASH is used for other numeric or
 * pointer types.
 * Messages requested through this method will be sent
 * before any other messages. beforePerformance messages are sent in the
 * order they were enqueued.
 * Returns a pointer to an MKMsgStruct that can be
 * passed to cancelMsgRequest:.
 */
{
    MKMsgStruct *sp;
    beforePerformanceQueue = 
      insertSpecialQueue(sp = newMsgRequest(CONDUCTORFREES, 0.0,
					    aSelector, toObject, argCount,
					    arg1, retainArg1,
					    arg2, retainArg2),
			 beforePerformanceQueue, &beforePerformanceQueueEnd);
    return(sp);
}

static MKMsgStruct *evalSpecialQueue(MKMsgStruct *queue, MKMsgStruct **queueEnd)
    /* Sends all messages in the special queue, e.g. afterPerformanceQueue.
     */
{
    register MKMsgStruct *curProc;

    while (queue) {
        curProc = popMsgQueue(&(queue));
        if (curProc == *queueEnd)
            *queueEnd = NULL;
        if(curProc->_toObject) {  // ensure we have a valid object to perform a selector
            switch (curProc->_argCount) {
            case 0:
                [curProc->_toObject performSelector:curProc->_aSelector];
                break;
            case 1:
                [curProc->_toObject performSelector:curProc->_aSelector withObject:curProc->_arg1];
		if (curProc->_retainArg1) [curProc->_arg1 release];
		curProc->_arg1 = nil;
                break;
            case 2:
                [curProc->_toObject performSelector:curProc->_aSelector withObject:curProc->_arg1 withObject:curProc->_arg2];
		if (curProc->_retainArg1) [curProc->_arg1 release];
		if (curProc->_retainArg2) [curProc->_arg2 release];
		curProc->_arg1 = nil;
		curProc->_arg2 = nil;
                break;
            default:
                break;
            }
        }
//	else {
//            NSLog(@"Warning, nil object on special queue\n");
//      }
        if (curProc->_conductorFrees)
            freeSp(curProc);
    }    
    return NULL;
}


- (void)encodeWithCoder:(NSCoder *)aCoder
  /* TYPE: Archiving; Writes object.
     You never send this message directly.  
     Archives beatSize and timeOffset. Also archives whether this
     was the clockConductor or defaultConductor.
     */
{
//    [super encodeWithCoder:aCoder];//sb: unnec?
    [aCoder encodeValuesOfObjCTypes: "ddc", &beatSize, &timeOffset, &(archivingFlags)];
    [aCoder encodeConditionalObject: delegate];
}

- (id)initWithCoder:(NSCoder *)aDecoder
  /* TYPE: Archiving; Reads object.
     You never send this message directly.  
     Should be invoked with NXReadObject(). 
     See write: and finishUnarchiving.
     */
{
    delegateFlags = 0;
    if ([aDecoder versionForClassName:@"MKConductor"] >= VERSION2) {
        [aDecoder decodeValuesOfObjCTypes: "ddc", &beatSize, &timeOffset, &(archivingFlags)];
        activePerformers = nil;
    }
    if ([aDecoder versionForClassName:@"MKConductor"] >= VERSION3) {
          activePerformers = [[aDecoder decodeObject] retain];
    }
    return self;
}

- awakeAfterUsingCoder:(NSCoder *)aDecoder
  /* If the unarchived object was the clockConductor, frees the new object
     and returns the clockConductor. Otherwise, if there is a performance
     going on, frees the new object and returns the defaultConductor. 
     Otherwise, if the unarchived object was the defaultConductor, sets the 
     defaultConductor's beatSize and timeOffset from the unarchived object,
     frees the new object and returns the defaultConductor.
     Otherwise, the new unarchived Conductor is added to the Conductor
     list and nil is returned. */
{
    if (archivingFlags == CLOCKCOND) {
        if (delegate)
            [clockCond setDelegate:delegate];
        [super release];
        return clockCond;
    } else if (inPerformance) {
        [super release];
        return defaultCond;
    } else if (archivingFlags == DEFAULTCOND) {
        [defaultCond setDelegate:delegate];
        [defaultCond setBeatSize:beatSize];
        [defaultCond setTimeOffset:timeOffset];
        [super release];
        return defaultCond;
    } 
    [allConductors addObject: self];
    inverseBeatSize = 1.0 / beatSize;
    [self _initialize];

    return self;
}

+ (void)setDelegate:object
{
    delegateRespondsToLowThresholdMsg = 
      [object respondsToSelector:@selector(conductorCrossedLowDeltaTThreshold)];
    delegateRespondsToHighThresholdMsg = 
      [object respondsToSelector:@selector(conductorCrossedHighDeltaTThreshold)];
    delegateRespondsToThresholdMsgs = (delegateRespondsToLowThresholdMsg ||
				       delegateRespondsToHighThresholdMsg);
    classDelegate = object;
    return;
}

+ delegate 
{
    return classDelegate;
}

- (void)setDelegate:(id)object
{
    unsigned char flags;
    delegate = object;
    if (!delegate) {
        delegateFlags = 0;
        return;
    }
    flags = 0;
    if ([self->delegate respondsToSelector:@selector(beatToClock:from:)]) 
      flags = BEAT_TO_CLOCK;
    if ([self->delegate respondsToSelector:@selector(clockToBeat:from:)]) 
      flags |= CLOCK_TO_BEAT;
    delegateFlags = flags;
}

-delegate
{
    return delegate;
}

#if 0
/* Needed to get around a compiler bug FIXME */
static double getNextMsgTime(MKConductor *aCond)
{
    return aCond->nextMsgTime;
}
#endif

// for debugging
- (NSString *) description
{
//    MKMsgStruct *p;
    NSString *queue = [NSString stringWithFormat:
        @"%@\n  Next conductor 0x%x, Last conductor 0x%x\n",
	[super description], _condNext, _condLast];
    NSString *timeStr = [NSString stringWithFormat: @"  time %lf beats, nextMsgTime %lf, timeOffset %lf\n",
       time, nextMsgTime, timeOffset];
    NSString *beatTime = [NSString stringWithFormat:
        @"  beatSize %lf Secs, inverseBeatSize %lf, oldAdjustedClockTime %lf\n",
        beatSize, inverseBeatSize, oldAdjustedClockTime];
    NSString *misc = [NSString stringWithFormat:
        @"  archivingFlags %x, delegateFlags %x, delegate %@, activePerformers %@, MTCSynch %@\n",
        archivingFlags, delegateFlags, delegate, activePerformers, MTCSynch];
    NSString *pausing = [NSString stringWithFormat:
        @"  %s, pauseOffset = %lf, pauseFor = 0x%x\n",
        isPaused ? "Paused" : "Not paused", _pauseOffset, pauseFor];
    NSString *msgs = [NSString stringWithFormat: @"  msgQueue = 0x%x: ", _msgQueue];

//    for(p = _msgQueue; p != NULL; p = p->_next) {
//        [msgs stringByAppendingFormat: @"t %lf [%@ arg1:%@ arg2:%@] conductor %s \n",
//            p->_timeOfMsg, p->_toObject, p->_arg1, p->_arg2, p->_conductorFrees ? "frees" : "doesn't free"];
//        [msgs stringByAppendingFormat: @"t %lf\n",
//           p->_timeOfMsg];
//    }

    return [queue stringByAppendingFormat: @"%@%@%@\n%@%@", timeStr, beatTime, msgs, pausing, misc];
}

@end

@implementation MKConductor(Private)

+ (void) masterConductorBody: (NSTimer *) unusedTimer
/*sb: created for the change from DPS timers to OS-style timers. The timer performs a method, not
 * a function. It's a class method because we want only one object to look after these messages.
 * When called from a separate thread, it will not actually be called from a NSTimer,
 * but after a timed condition lock. Therefore we should never do anything with unusedTimer.
 */
{
    MKMsgStruct  *curProc;

    _MKLock();  // ensure we can do our thang uninterrupted.
    // Since masterConductorBody can be called from a NSTimer in a separate thread NSRunLoop without being
    // able to check the performance status, it's possible for the performance to end while waiting, 
    // such that by the time we arrive here, we don't want to perform anything, so we split this crazy scene...
    if(!inPerformance) {
        if (MKIsTraced(MK_TRACECONDUCTOR))
            NSLog(@"Early escape from masterConductorBody as not in performance\n"); 
        _MKUnlock(); // drop the lock before this early out.
        return;
    }

    /* Preamble */
    curRunningCond = condQueue;
    // NSLog(@"Setting time via masterConductorBody\n");
    setTime(condQueue->nextMsgTime);

    /* Here is the meat of the conductor's performance. */
    do {
        // NSLog(@"curRunningCond %@\n", curRunningCond);
        curProc = popMsgQueue(&(curRunningCond->_msgQueue));
        if (curProc->_timeOfMsg > curRunningCond->time) // IMPORTANT--Performers can give us negative vals
            curRunningCond->time = curProc->_timeOfMsg;
        if (MKIsTraced(MK_TRACECONDUCTOR))
            NSLog(@"t %g\n", clockTime);
        if (!curProc->_conductorFrees) {
            // NSLog(@"I'm not supposed to free %d, %@\n", curProc->_argCount, curProc->_toObject);
            // NSLog(@"separateThreaded %d and in MusicKit thread %d\n", separateThread, separateThreadedAndInMusicKitThread());
            curProc->_onQueue = NO;  // LMS this is neccessary but why?
            switch (curProc->_argCount) {
            case 0:
                (*curProc->_methodImp)(curProc->_toObject, curProc->_aSelector);
                break;
            case 1:
                (*curProc->_methodImp)(curProc->_toObject, curProc->_aSelector, curProc->_arg1);
		if (curProc->_retainArg1)
		    [curProc->_arg1 release];
		curProc->_arg1 = nil;
                break;
            case 2:
                (*curProc->_methodImp)(curProc->_toObject, curProc->_aSelector, curProc->_arg1, curProc->_arg2);
		if (curProc->_retainArg1)
		    [curProc->_arg1 release];
		if (curProc->_retainArg2)
		    [curProc->_arg2 release];
		curProc->_arg1 = nil;
		curProc->_arg2 = nil;
                break;
            }
            // NSLog(@"Returned from method call (conductor doesnt free) %@ sepThreadMK %d\n", 
	    // curProc->_toObject, separateThreadedAndInMusicKitThread());
        }
        else {
            // NSLog(@"I'm supposed to free %@\n", curProc->_toObject);
            switch (curProc->_argCount) {
            case 0:
                [curProc->_toObject performSelector: curProc->_aSelector];
                break;
            case 1:
                [curProc->_toObject performSelector: curProc->_aSelector withObject: curProc->_arg1];
		if (curProc->_retainArg1)
		    [curProc->_arg1 release];
		curProc->_arg1 = nil;
                break;
            case 2:
                [curProc->_toObject performSelector: curProc->_aSelector
					 withObject: curProc->_arg1
					 withObject: curProc->_arg2];
		if (curProc->_retainArg1)
		    [curProc->_arg1 release];
		if (curProc->_retainArg2)
		    [curProc->_arg2 release];
		curProc->_arg1 = nil;
		curProc->_arg2 = nil;
                break;
            default:
                break;
            }
            // NSLog(@"Returned from method call (conductor frees)\n");
            freeSp(curProc);
        }
        // NSLog(@"curRunningCond at end of loop %@\n", curRunningCond);
    } while (PEEKTIME(curRunningCond->_msgQueue) <= curRunningCond->time);

    if (!curRunningCond->isPaused) {
        double theNextBeat = PEEKTIME(curRunningCond->_msgQueue);
	double theNextClockTime = beatToClock(curRunningCond, theNextBeat);
	
	// NSLog(@"theNextBeat %lf theNextClockTime %lg\n", theNextBeat, theNextClockTime);
        repositionCond(curRunningCond, theNextClockTime);
    }

    // If at the end, repositionCond triggers [MKConductor finishPerformance].
    // If this occurs, adjustTimedEntry is a noop (see masterConductorBody:).
    [_MKClassOrchestra() flushTimedMessages];

    /* Postamble */
    // NSLog(@"Setting curRunningCond nil\n");
    curRunningCond = nil;
    if (inPerformance) {        /* Performance can be ended by repositionCond(). */
        adjustTimedEntry(condQueue->nextMsgTime);
    }
    _MKUnlock();
}

+ (MKMsgStruct *) _afterPerformanceSel: (SEL) aSelector 
				    to: (id) toObject 
			      argCount: (int) argCount, ...;
/* 
  Same as afterPerformanceSel:to:argCount: but ensures that message will
  be sent before any of the messages enqueued with that method. Private
  to the musickit.
*/
{
    MKMsgStruct *sp;
    id arg1, arg2;
    va_list ap;
    
    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    sp = newMsgRequest(CONDUCTORFREES, MK_ENDOFTIME, aSelector, toObject, argCount, arg1, FALSE, arg2, FALSE);
    _afterPerformanceQueue = insertSpecialQueue(sp, _afterPerformanceQueue, &_afterPerformanceQueueEnd);
    va_end(ap);
    return(sp);
}

+ (MKMsgStruct *) _afterPerformanceSel: (SEL) aSelector 
				    to: (id) toObject 
			      argCount: (int) argCount
				  arg1: (id) arg1 retain: (BOOL) retainArg1
				  arg2: (id) arg2 retain: (BOOL) retainArg2
/* 
  Same as afterPerformanceSel:to:argCount: but ensures that message will
  be sent before any of the messages enqueued with that method. Private
  to the musickit.
*/
{
    MKMsgStruct *sp = newMsgRequest(CONDUCTORFREES, MK_ENDOFTIME, aSelector, toObject, argCount, arg1, retainArg1, arg2, retainArg2);
    
    _afterPerformanceQueue = insertSpecialQueue(sp, _afterPerformanceQueue, &_afterPerformanceQueueEnd);
    return(sp);
}

+ (MKMsgStruct *) _newMsgRequestAtTime: (double) timeOfMsg
				   sel: (SEL) whichSelector
				    to: (id) destinationObject
			      argCount: (int) argCount, ...;
/* TYPE: Requesting; Creates and returns a new message request.
 * Creates and returns message request but doesn't schedule it.
 * The return value can be passed as an argument to the
 * _scheduleMsgRequest: and _cancelMsgRequest: methods.
 *
 * You should only invoke this method if you need greater control
 * over scheduling (for instance if you need to cancel a message request)
 * than that afforded by the sel:to:atTime:argCount: and
 * sel:to:withDelay:argCount: methods.
 */
{
    id arg1, arg2;
    va_list ap;
    
    va_start(ap, argCount); 
    arg1 = va_arg(ap, id);
    arg2 = va_arg(ap, id);
    va_end(ap);
    return newMsgRequest(TARGETFREES, timeOfMsg, whichSelector, destinationObject, argCount, arg1, FALSE, arg2, FALSE);
}

+ (MKMsgStruct *) _newMsgRequestAtTime: (double) timeOfMsg
				   sel: (SEL) whichSelector 
				    to: (id) destinationObject
			      argCount: (int) argCount
				  arg1: (id) arg1 retain: (BOOL) retainArg1
				  arg2: (id) arg2 retain: (BOOL) retainArg2
/* TYPE: Requesting; Creates and returns a new message request.
 * Creates and returns message request but doesn't schedule it.
 * The return value can be passed as an argument to the
 * _scheduleMsgRequest: and _cancelMsgRequest: methods.
 *
 * You should only invoke this method if you need greater control
 * over scheduling (for instance if you need to cancel a message request)
 * than that afforded by the sel:to:atTime:argCount: and
 * sel:to:withDelay:argCount: methods.
 */
{
    return newMsgRequest(TARGETFREES, timeOfMsg, whichSelector, destinationObject, argCount, arg1, retainArg1, arg2, retainArg2);
}

- (void) _scheduleMsgRequest: (MKMsgStruct *) aMsgStructPtr
  /* TYPE: Requesting; Schedules a message request with the receiver.
   * Sorts the message request aMsgStructPtr
   * into the receiver's message queue.  aMsgStructPtr is 
   * a pointer to an MKMsgStruct, such as returned by
   * _newMsgRequestAtTime:sel:to:argCount:.
   */
{
    if (aMsgStructPtr && (!aMsgStructPtr->_onQueue))
        insertMsgQueue(aMsgStructPtr, self);
}

+ (void) _scheduleMsgRequest: (MKMsgStruct *) aMsgStructPtr
  /* Same as _scheduleMsgRequest: but uses clock conductor. 
   */
{
    if (aMsgStructPtr && (!aMsgStructPtr->_onQueue))
        insertMsgQueue(aMsgStructPtr, clockCond);
}

- (MKMsgStruct *) _rescheduleMsgRequest: (MKMsgStruct *) aMsgStructPtr
				 atTime: (double) timeOfNewMsg 
				    sel: (SEL) whichSelector
				     to: (id) destinationObject 
			       argCount: (int) argCount, ...;
  /* TYPE: Requesting; Reschedules a message request with the receiver.
   * Redefines the message request aMsgStructPtr according
   * to the following arguments and resorts
   * it into the receiver's message queue.
   * aMsgStructPtr is 
   * a pointer to an MKMsgStruct, such as returned by
   * _newMsgRequestAtTime:sel:to:argCount:.
   */

/* Same as MKReschedule */
{
    id arg1, arg2;
    va_list ap;
    
    va_start(ap,argCount); 
    arg1 = va_arg(ap,id);
    arg2 = va_arg(ap,id);
    va_end(ap);
    return MKRescheduleMsgRequest(aMsgStructPtr, self, timeOfNewMsg, whichSelector, destinationObject, argCount, arg1, FALSE, arg2, FALSE);
}

- (MKMsgStruct *) _rescheduleMsgRequestWithObjectArgs: (MKMsgStruct *) aMsgStructPtr
					       atTime: (double) timeOfNewMsg
						  sel: (SEL) whichSelector
						   to: (id) destinationObject
					     argCount: (int) argCount
						 arg1: (id) arg1 retain: (BOOL) retainArg1
						 arg2: (id) arg2 retain: (BOOL) retainArg2
/* Same as MKReschedule */
{
    return MKRescheduleMsgRequest(aMsgStructPtr, self, timeOfNewMsg, whichSelector, destinationObject, argCount, arg1, retainArg1, arg2, retainArg2);
}


+ (MKMsgStruct *) _cancelMsgRequest: (MKMsgStruct *) aMsgStructPtr
  /* TYPE: Requesting; Cancels the message request aMsgStructPtr.
   * Removes the message request pointed to by
   * aMsgStructPtr. 
   * Notice that this is a factory method -- you don't have to
   * know which queue the message request is on to cancel it.
   * aMsgStructPtr is 
   * a pointer to an MKMsgStruct, such as returned by
   * _newMsgRequestAtTime:sel:to:argCount:.
   */
{
    return MKCancelMsgRequest(aMsgStructPtr);
}

+ (double) _adjustTimeNoTE: (double) desiredTime     
  /* TYPE: Modifying; Sets the current time to desiredTime.
   * Sets the factory's notion of the current time to
   * desiredTime.  desiredTime is clipped
   * to a value not less than the time that the previous message
   * was sent and not greater than that of the
   * next scheduled message.
   * Returns the adjusted time.
   */
{
    double t;
    if (!inPerformance || performanceIsPaused)
	return clockTime;
    /* FIXME Should maybe do a gettime() and compare with the time
	we've been handed. If they're different, we need to 
	adjust startTime (subtract difference) and then adjust timed entry.
	*/
    t = MIN(desiredTime, condQueue->nextMsgTime);
    t = MAX(t, clockTime);
    setTime(t);
    return clockTime;
}

+ (double) _getLastTime
  /* See time.m: _MKLastTime(). */ 
{
    return (clockTime != 0.0) ? clockTime : oldClockTime;
}

+ _adjustDeltaTThresholds
{
    double dt = -MKGetDeltaT();
    deltaTThresholdLow = dt * (1 - deltaTThresholdLowPC);
    deltaTThresholdHigh = dt * (1 - deltaTThresholdHighPC);
    if (deltaTThresholdLow > deltaTThresholdHigh)
      deltaTThresholdLow = deltaTThresholdHigh;
    return self;
}

// private error message method signalling an internal error from sending the end-of-list marker
// an NSString is immutable and therefore thread-safe.
- _error: (NSString *) errorMsg
{
   MKError(errorMsg);
   return self;
}

- _resume
    /* This is factored out of resume to avoid circularity in MTC
    * implementation.
    */
{
    if (!isPaused)
	return self;
    isPaused = NO;
    oldAdjustedClockTime = clockTime - _pauseOffset;
    repositionCond(self, beatToClock(self, PEEKTIME(_msgQueue)));
    pauseFor = MKCancelMsgRequest(pauseFor);
    return self;
}

- _pause
    /* Used by MTC mechanism */
{
    if (isPaused)
	return self;
    isPaused = YES;
    repositionCond(self,PAUSETIME);
    if ([delegate respondsToSelector: @selector(conductorDidPause:)])
	[delegate conductorDidPause: self];
    return self;
}

@end

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us