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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
""" Generate a score of Karlheinz Stockhausen´s “Plus Minus” """

import pm_functions as pmf
import sys, cPickle, random, time, operator, copy, math
import logging, os, shutil, codecs
from instruments import *
logging.basicConfig(level=logging.INFO, format='%(levelname)s :%(lineno)d: %(message)s')

class Score:
    """ this class contains the entire realisation """
    def __init__(self):
        self.timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime())
        self.pmplotfile = open('pmplot.dat', 'w')
        # get the symbol data
        symbolsfile = open('symbols.pkl', 'rb')
        self.symbols = cPickle.load(symbolsfile)
        symbolsfile.close()
        # map event types to chord numbers - instruction 7
        self.chordorder = range(7)
        random.shuffle(self.chordorder)
        # get the chord data
        chordsfile = open('chords.pkl', 'rb')
        self.chords = cPickle.load(chordsfile)
        chordsfile.close()
        # get the subsidiary data
        subsfile = open('subsidiaries.pkl', 'rb')
        self.subsidiaries = cPickle.load(subsfile)
        subsfile.close()

    def Build(self):
        print 'Loop 1: assign degree of change and plus-minus flags.'
        self.layers = []
        for l in range(pmf.number_of_layers):
            self.layers.append(Layer(self, l))
        for l in self.layers:
            print 'Layer %s has %s events' % (l.layernumber, len(l.events))
        print 'Loop 2: process plus-minus flags.'
        loop2(self)
        for l in self.layers:
            print 'Layer %s has %s events, pmvalues: %s' % (l.layernumber, len(l.events), l.pmvalues)
        print 'Loop 3: calculate event durations.'
        loop3(self)
        self.elapsed_time = self.elapsed()
        print 'Layer durations: %s' % self.elapsed_time
        print 'Loop 4: fill empty events.'
        loop4(self)
        self.elapsed_time = self.elapsed()
        print 'Layer durations: %s' % self.elapsed_time
        print 'Loop 5: quantize and align events.'
        self.sections = []
        loop5(self)
        print 'Loop 6: define bar structure.'
        self.bars = []
        loop6(self)
        lastbeat = self.bars[-1].start + self.bars[-1].length
        self.elapsed_time = pmf.beatelapsed(lastbeat, self.bars, self.sections)
        sectiondurations, proportions = [], ''
        for s in self.sections:
            sectiondurations.append(pmf.beatelapsed(s.start, self.bars, self.sections))
        sectiondurations.append(self.elapsed_time)
        for s in range(len(self.sections)):
            self.sections[s].duration = sectiondurations[s + 1] - sectiondurations[s]
            proportions += '%s ' % round(self.sections[s].duration)
        print 'Total duration: %s min, sections: %sseconds.' % \
                (round(self.elapsed_time / 60.0, 2), proportions)
        print 'Loop 7: convert durations to beats, merge rests, equalise layer lengths.'
        loop7(self)
        print 'Total duration: %s min.' % round(self.elapsed_time / 60.0, 2)
        print 'Loop 8: assign dynamics, register, activity to events.'
        loop8(self)
        print 'Loop 9: assign players to layers, choose central sound chords.'
        loop9(self)
        print 'Loop 10: score consonance of chords and order them.'
        loop10(self)
        print 'Loop 11: order components, choose accessory pitches, score consonance of subsidiary notes and order them.'
        loop11(self)
        print 'Loop 12: create event rhythms.'
        loop12(self)
        print 'Loop 13: assign pitches to notes.'
        loop13(self)
        print 'Loop 14: add dynamics and articulations.'
        loop14(self)
        self.savepickle(14)

    def savepickle(self, loop):
        pass
        output = open('score_loop%s.pkl' % loop, 'wb')
        cPickle.dump(self, output, 2)
        output.close()

    def elapsed(self):
        """ calculate duration """
        elapsed_time = [0] * pmf.number_of_layers
        for l in self.layers:
            for e in l.events:
                elapsed_time[l.layernumber] += sum(e.duration)
        return elapsed_time

class Layer:
    """ this class represents one [p]layer """
    def __init__(self, score, layernumber):
        self.layernumber = layernumber
        self.alignedevents = 0
        self.elapsedbeats = 0
        self.elapsedtime = 0
        self.elapsedtimeold = 0
        self.loopcounter = 0
        self.pmvalues = pmf.generate_pm_values(1, 1, 1, 1)
        print 'Layer %s initial plus-minus values: %s' % (layernumber, self.pmvalues)
        self.thirteens = [0] * 7
        # random order of symbol and notes pages - instruction 3
        self.symbol_pages = pmf.symbol_pages
        random.shuffle(self.symbol_pages)
        self.notes_pages = copy.copy(self.symbol_pages)
        random.shuffle(self.notes_pages)
        print 'Layer %s page order: %s %s' % (layernumber, self.symbol_pages, self.notes_pages)
        # sort symbols list (from score) by this page order (Schwartzian transform)
        symbolsdeco = [(self.symbol_pages.index(s['page']), s) for s in score.symbols]
        symbolsdeco.sort()
        symbolssorted = [s for _, s in symbolsdeco]
        # initialise symbols
        self.events = []
        for ss in symbolssorted:
            self.events.append(Event(ss))

class Event:
    """ this class represents one box on the symbols pages """
    def __init__(self, symbols):
        self.symbols = symbols
        self.thirteen = 0
        self.wasempty = False
        self.components = []
        self.prerest = 0
        self.repeatsbefore = False
        self.repetition = False
        self.cs = CentralSound()
        self.subsnotes = False
        self.negative = False
        self.firstnege = False
        self.lystart = ''
        self.debug = ''
        # is it an empty event?
        if symbols['empty'] == 'not empty':
            # allocate degree of change
            self.change = pmf.allocatechange(symbols)

class CentralSound:
    """ this class represents a Central Sound object for linked events """
    def __init__(self):
        pass

class Note:
    """ one note or rest """
    def __init__(self, ctype, start, length, value, tuplet=None, tied=False, pitch=None, articulation=''):
        self.ctype = ctype
        self.start = start
        self.length = length
        self.value = value
        self.tuplet = tuplet
        if ctype == 'rest' or ctype == 'o':
            self.tied = False
        else:
            self.tied = tied
        self.pitch = pitch
        self.articulation = articulation

class Tuplet:
    """ a tuplet container """
    def __init__(self, start, numerator, denominator):
        self.start = start
        self.numerator = numerator
        self.denominator = denominator

class Section:
    """ section of the piece """
    def __init__(self, id, start, tempo):
        self.id = id
        self.start = start
        self.tempo = tempo

class Bar:
    """ one bar of the piece """
    def __init__(self, id, start, length):
        self.id = id
        self.start = start
        self.length = length
        self.section = False
        self.gracespace = {}

def loadpickledscore(loop):
    """ reload a pickled score at point loop """
    picklefile = open('score_loop%s.pkl' % loop, 'rb')
    score = cPickle.load(picklefile)
    print score.timestamp
    picklefile.close()
    return score

def loop2(score):
    """ plusminus flags """
    for l in score.layers:
        # trim layer to target number of events
        maxstart = len(l.events) - pmf.events_per_layer
        startevent = random.choice(range(maxstart + 1))
        newevents = []
        virgintypes = range(7)
        eliminated = []
        layerevents = float(len(l.events))
        for i, e in enumerate(l.events):
            if e.symbols['empty'] == 'empty':
                # do nothing with empty events
                newevents.append(e)
                continue
            e_type_index = pmf.event_types[e.symbols['event_type']]
            e_location = i / layerevents
            e.flag = pmf.plusminusvalue(e_location, l.pmvalues[e_type_index],
                    (e.symbols['flag_plus'], e.symbols['flag_minus']))
            if e_type_index in virgintypes:
                # just append virgin events - "the flag does not apply until the
                # first repetition of the type"
                # record current transformation value
                e.thirteen = l.thirteens[e_type_index]
                # record current cumulative flag value
                e.pmvalue = l.pmvalues[e_type_index]
                newevents.append(e)
                # remove type from virgintypes
                del virgintypes[virgintypes.index(e_type_index)]
                # add the event's flag to the cumulative values for this type
                l.pmvalues[e_type_index] += e.flag
                logging.debug('Layer %s event type %s used' % (l.layernumber, e_type_index))
            else:
                # eliminate -13 events
                if l.pmvalues[e_type_index] <= -13 or l.pmvalues[e_type_index] + e.flag <= -13:
                    if e_type_index not in eliminated:
                        eliminated.append(e_type_index)
                        logging.info('Layer %s event type %s eliminated' % (l.layernumber, e_type_index))
                    continue
                # transform +13 events
                elif l.pmvalues[e_type_index] >= 13:
                    l.thirteens[e_type_index] += 1
                    # reset cumulative flag to 1
                    l.pmvalues[e_type_index] = 1
                    logging.info('Layer %s event type %s transformed' % (l.layernumber, e_type_index))
                # record current transformation value
                e.thirteen = l.thirteens[e_type_index]
                # record current cumulative flag value
                e.pmvalue = l.pmvalues[e_type_index]
                if e.pmvalue == 0 or abs(e.pmvalue) == 1:
                    # just append if pmvalue is 0, -1 or 1
                    newevents.append(e)
                else:
                    # choose whether to put repetitions before or after
                    before = random.choice([True, False])
                    if before:
                        e.repeatsbefore = True
                        orig_e = copy.deepcopy(e)
                    else:
                        newevents.append(e)
                    # get the event type
                    e_type_index = pmf.event_types[e.symbols['event_type']]
                    # pick a variety of repetition
                    e.repetition = random.choice(pmf.event_type_rep[e_type_index])
                    for i in range(abs(e.pmvalue) - 1):
                        newevents.append(copy.deepcopy(e))
                        # relink central sound object
                        newevents[-1].cs = e.cs
                    if before:
                        newevents.append(orig_e)
                        # relink central sound object
                        newevents[-1].cs = e.cs
                # add the event's flag to the cumulative values for this type
                l.pmvalues[e_type_index] += e.flag
        l.events = newevents

def loop3(score):
    """ event durations """
    for l in score.layers:
        for e in l.events:
            # first run: deal with degree of change and post-rests
            try:
                change, reldur, pmval = e.change[2], e.symbols['relative_duration'], e.pmvalue
            except (IndexError, KeyError, AttributeError):
                # catch empty events
                change, reldur, pmval = 0, 0, 0
            e.scoremultiplier, e.restmultiplier = pmf.eventscoremultiplier(change, reldur, pmval)
            # deal with repetition multiplier
            if e.repetition:
                repmultiplier = random.uniform(e.repetition[1][0], e.repetition[1][1])
                e.scoremultiplier *= repmultiplier
        logging.debug('Durations: score muliplier calculated')
        l_events = len(l.events)
        l_relduration = 0
        ev = 0
        for e in l.events:
            # second run: assign relative durations by event number
            e_relduration = pmf.eventduration_f( ev / float(l_events) ) \
                    * e.scoremultiplier * (1 + e.restmultiplier)
            l_relduration += e_relduration
            e.relduration = e_relduration
            ev += 1
        logging.debug('Durations: relative durations assigned: %s' % l_relduration)
        cursor = 0
        l_duration = 0
        for e in l.events:
            # third run: reassign relative duration by relative position
            e_relduration = pmf.eventduration_f( cursor / l_relduration ) \
                    * e.scoremultiplier * (1 + e.restmultiplier)
            cursor += e.relduration
            l_duration += e_relduration
            e.relduration = e_relduration
        logging.debug('Durations: relative durations reassigned: %s' % l_duration)
        cursor = 0
        l.elapsedtime = 0
        for e in l.events:
            # fourth run: calculate event durations in seconds
            # include empty events
            # returns tuple(event_Eduration, event_Rduration)
            e.duration = pmf.eventduration(cursor, l_duration, e.scoremultiplier, e.restmultiplier)
            cursor += e.relduration
            l.elapsedtime += sum(e.duration)
        logging.info('Durations: layer %s durations assigned: %s' % (l.layernumber, l.elapsedtime))

def loop4(score):
    """ fill empty events """
    for l in range(pmf.number_of_layers):
        events_to_delete = []
        for e in range(len(score.layers[l].events)):
            ev = score.layers[l].events[e]
            if ev.symbols['empty'] == 'empty':
                replacement = pmf.fillempty(e, ev.symbols['page'],
                        score.layers[l].events, score.layers[l].symbol_pages)
                if replacement is False:
                    events_to_delete.append(e)
                else:
                    # copy replacement event to empty event
                    score.layers[l].events[e] = copy.deepcopy(score.layers[l].events[replacement])
                    score.layers[l].events[e].wasempty = True
                    # relink central sound object
                    score.layers[l].events[e].cs = score.layers[l].events[replacement].cs
        # delete remaining empty events
        events_to_delete.sort(reverse=True)
        for e in events_to_delete:
            del score.layers[l].events[e]
        print '%s events deleted in layer %s.' % (len(events_to_delete), l)

def loop5(score):
    """ quantize and align events """
    max_loops = 4
    found_events = 0
    not_found_events = 0
    short_sections = 0
    current_tempo = 0
    total_alignedevents = 0
    added_beats = 0
    finished_layers = [False] * pmf.number_of_layers
    score.loop5loops = 0
    # count total events
    total_events = 0
    for l in score.layers:
        total_events += len(l.events)
    logging.info('Total events: %s' % total_events)
    # start with any layer
    l = random.choice(score.layers)
    # loop until all events processed
    while total_alignedevents != total_events:
        score.loop5loops += 1
        # get event
        try:
            e = l.events[l.alignedevents]
        except IndexError:
            # no more events in this layer - try another
            finished_layers[l.layernumber] = True
            ll = l.layernumber
            l = score.layers[pmf.randomlayer(ll)]
            logging.debug('Reached end of layer %s, switching to layer %s' % (ll, l.layernumber))
            continue
        e.index = l.alignedevents
        e_timing = e.symbols['timing']
        e_nextevent = False
        #e_nexteventstart = False
        e_nexteventlength = False
        e.start = False
        e_with = False
        # how many layers have started?
        other_layers_started = 0
        for ol in score.layers:
            if ol.alignedevents > 0 and ol != l:
                other_layers_started += 1
        if l.alignedevents == 0 and other_layers_started == 0:
            # this is the first event in any layer
            if (e_timing == 'between' or e_timing == 'immediately') and l.loopcounter < max_loops:
                # pick another layer to start
                l.loopcounter += 1
                ll = l.layernumber
                l = score.layers[pmf.randomlayer(ll)]
                logging.debug('Layer %s rejected as bad candidate to start, trying layer %s' % (ll, l.layernumber))
                continue
            else:
                # event starts at beginning
                e.start = 0
        else:
            # every other event (not the first)
            if e_timing != 'between' and e_timing != 'immediately' and l.alignedevents == 0:
                # if this is the first event in this layer and it's not a 'between' or an 'immediately'
                e.start = l.elapsedbeats
            elif e_timing == 'between' and other_layers_started >= 1:
                # pick an event in another layers and start during it
                e.start = True
                e_with = True
            elif e_timing == 'immediately' and other_layers_started >= 1:
                # pick an event in another layer and start just after it
                # why are these the same? this is dealt with later
                e.start = True
                e_with = True
            elif e_timing == 'with one' and other_layers_started >= 2:
                # event is 1/6th likely to start with another
                e.start = True
                e_with = random.choice([False] * 5 + [True])
            elif e_timing == 'with two' and other_layers_started >= 2:
                # event is 2/6ths likely to start with another
                e.start = True
                e_with = random.choice([False] * 4 + [True] * 2)
            elif e_timing == 'with 3 or 4' and other_layers_started >= 2:
                # event is 3/6ths or 4/6ths (7/12ths) likely to start with another
                e.start = True
                e_with = random.choice([False] * 5 + [True] * 7)
            elif e_timing == 'with 5 or 6' and other_layers_started >= 2:
                # event is 5/6ths or 6/6ths (11/12ths) likely to start with another
                e.start = True
                e_with = random.choice([False] + [True] * 11)
            elif e_timing == None:
                # event timing not specified, just go for it
                e.start = l.elapsedbeats
        # event timing should be coordinated - instruction 32
        if e.start is True and e_with is True:
            if l.loopcounter > max_loops:
                # we've tried this enough already
                e.start = l.elapsedbeats
                logging.debug('Avoiding loop - assigning start for event.')
            else:
                # refer to other layers
                e_nextevent = pmf.findnextevent(l.elapsedbeats, l.layernumber, score.layers)
                if e_nextevent:
                    # found another event to align to
                    e.start = l.elapsedbeats
                    e.prerest = e_nextevent[0] - l.elapsedbeats
                    e_nexteventlength = e_nextevent[1]
                    found_events += 1
                else:
                    # haven't found another event to align to
                    e.start = False # reset
                    not_found_events += 1
                    # increment loop
                    l.loopcounter += 1
        # deal with what we have now
        if e.start is not False and e_timing == 'immediately':
            # start minimum duration after 
            e.prerest += 1
            e.start = l.elapsedbeats
        elif e.start is not False and e_timing == 'between':
            # start at a random point during the found other event
            try:
                e.prerest += random.choice(range(e_nexteventlength))
            except IndexError:
                # e_nexteventlength is still False - i.e. if we skipped due to max loops
                e.prerest += random.choice(range(5))
            e.start = l.elapsedbeats
        elif e.start is True and e_with is False:
            # start straight away in this layer
            e.start = l.elapsedbeats
        elif e.start is False:
            # still haven't found anything
            if finished_layers.count(True) > 2 or l.loopcounter > max_loops:
                # if more than 2 layers are finished, or we've already looped
                # enough, then just start straight away
                e.start = l.elapsedbeats
            else:
                # try another layer
                l.loopcounter += 1
                ll = l.layernumber
                l = score.layers[pmf.randomlayer(ll)]
                logging.debug('Corresponding event not found - trying another layer')
                continue
        # check that a start beat has been defined
        if e.start is True:
            raise 'This shouldn\'t happen!'
        # record number of added beats
        added_beats += e.prerest
        # find out the current tempo
        if current_tempo != 0:
            for s in score.sections:
                if s.start <= e.start:
                    current_tempo = s.tempo
                else:
                    break
        # pick a tempo
        elapsed_proportion = e.index / float(len(l.events))
        e.tempo = pmf.eventtempo(current_tempo, elapsed_proportion)
        # event lengths in multiples of shortest duration
        e_duration = sum(e.duration)
        e.length = pmf.seconds2beats(e.tempo, e_duration) + e.prerest
        # if tempo has changed, may have to add a new section
        if e.tempo != current_tempo:
            # check each section
            for s in score.sections:
                if s.start > e.start:
                    # new section is already due to start during this event
                    # override recommended tempo change
                    e.tempo = current_tempo
                    e.length = pmf.seconds2beats(e.tempo, e_duration) + e.prerest
                    logging.debug('New section due to start soon - tempo change overridden')
                    break
        # tempo still changed?
        if e.tempo != current_tempo:
            # check if last section long enough
            try:
                if score.sections[-1].start + pmf.minimum_section_length > e.start:
                    short_sections += 1
                    # override recommended tempo change
                    e.tempo = current_tempo
                    e.length = pmf.seconds2beats(e.tempo, e_duration) + e.prerest
                    logging.debug('Section too short - tempo change overridden')
            except IndexError:
                # must be the first section
                pass
        # still changed?
        if e.tempo != current_tempo \
                or (e.start - score.sections[-1].start) > pmf.maximum_section_length:
            # sections
            section_number = len(score.sections)
            score.sections.append(Section(section_number, e.start, e.tempo))
            current_tempo = e.tempo
            logging.info('Section %s added: %s, %s' % (section_number, e.start, e.tempo))
        # if this is the first event in the layer and e_start is not zero
        if e.index == 0 and e.start != 0:
            raise 'Event 0 doesn\'t start at 0!'
        logging.debug('layer %s event %s start %s tempo %s length %s events %s beats %s' % \
                (l.layernumber, e.index, e.start, e.tempo, e.length,
                    l.alignedevents, l.elapsedbeats))
        # add duration to elapsed time
        l.elapsedtimeold += e_duration
        l.elapsedtime += pmf.beats2seconds(e.tempo, e.length)
        l.elapsedbeats = e.start + e.length
        if e.index > 1:
            # check that events are in order
            laste = l.events[e.index - 1]
            if laste.start >= e.start:
                logging.error('Out of order in layer %s: %s' % (l, e.start))
                raise 'Damn'
            elif laste.start + laste.length != e.start:
                logging.error('Event does not follow on correctly!')
                raise 'Damn'
        # increment counters
        l.alignedevents += 1
        total_alignedevents += 1
        # reset loop counter
        l.loopcounter = 0
    logging.info('Alignment: searched for %s events; found %s; %s short sections avoided.' % \
            ((found_events + not_found_events), found_events, short_sections))
    logging.info('Alignment: %s beats added.' % added_beats)
    logging.info('Alignment: %s loops required.' % score.loop5loops)

def loop6(score):
    """ define bar structure """
    # count total events
    total_events = [] # list of total events in each section
    for l in score.layers:
        total_events.append(len(l.events))
    barred_events = [0] * pmf.number_of_layers
    nextbar_start = [0] * pmf.number_of_layers
    possbar_length = [0] * pmf.number_of_layers
    finished_layers = [False] * pmf.number_of_layers
    currbar_start = 0
    currbar_length = 0
    currbar_number = 0
    while barred_events != total_events:
        # find first event on or after in all layers
        for l in range(pmf.number_of_layers):
            e = barred_events[l]
            currbar_events = 0
            try:
                while (currbar_start + pmf.minimum_bar_length) > score.layers[l].events[e + currbar_events].start:
                    currbar_events += 1
                nextbar_start[l] = score.layers[l].events[e + currbar_events].start
                possbar_length[l] = score.layers[l].events[e + currbar_events].start - currbar_start
            except IndexError:
                logging.debug('End of layer %s reached.' % l)
                nextbar_start[l] = False
                possbar_length[l] = False
            except:
                raise 'This shouldn\'t happen'
        try:
            # if list of possible bar_lengths includes current bar length, then
            # use it
            l = possbar_length.index(currbar_length)
            logging.debug('Current bar length available in layer %s' % l)
        except ValueError:
            # otherwise filter out invalid lengths
            fours = []; evens = []; odds = []
            for pbl in possbar_length:
                if pbl % 2 and pbl >= pmf.minimum_bar_length and pbl <= pmf.maximum_bar_length:
                    odds.append(pbl)
                elif pbl >= pmf.minimum_bar_length and pbl <= pmf.maximum_bar_length:
                    if pbl % 4:
                        evens.append(pbl)
                    else:
                        fours.append(pbl)
            if len(fours):
                # prefer values divisible by four
                currbar_length = random.choice(fours)
                l = possbar_length.index(currbar_length)
                logging.debug('Four bar length %s found in layer %s' % (currbar_length, l))
            elif len(evens):
                # prefer even values
                currbar_length = random.choice(evens)
                l = possbar_length.index(currbar_length)
                logging.debug('Even bar length %s found in layer %s' % (currbar_length, l))
            elif len(odds):
                # if no even values, pick an odd value
                currbar_length = random.choice(odds)
                l = possbar_length.index(currbar_length)
                logging.debug('Odd bar length %s found in layer %s' % (currbar_length, l))
            else:
                # no valid values available
                logging.debug('No new value available, bar length remains %s' % currbar_length)
            # test for zero length - e.g. first bar
            if currbar_length == 0:
                currbar_length = random.choice(range(pmf.minimum_bar_length,
                    pmf.maximum_bar_length + 1))
                logging.debug('Zero length bar altered to %s' % currbar_length)
        # tag all layers' concurrent events that start in this bar
        currbar_end = currbar_start + currbar_length
        for l in range(pmf.number_of_layers):
            if finished_layers[l]:
                continue
            try:
                while currbar_end > score.layers[l].events[barred_events[l]].start:
                    if score.layers[l].events[barred_events[l]].start >= currbar_start:
                        score.layers[l].events[barred_events[l]].barnumber = currbar_number
                        barred_events[l] += 1
            except IndexError:
                finished_layers[l] = True
            except:
                raise 'This shouldn\'t happen!'
        # add bar to bar list
        score.bars.append(Bar(currbar_number, currbar_start, currbar_length))
        currbar_number += 1
        currbar_start += currbar_length
    # move section starts to beginning of bar
    for s in score.sections:
        for i, b in enumerate(score.bars):
            if b.start > s.start:
                s.start = score.bars[i - 1].start
                s.startbar = i - 1
                break
    # tag bars with section number
    for s in score.sections:
        for b in score.bars:
            if b.id >= s.startbar:
                b.section = s.id

def loop7(score):
    """ equalize layer lengths, convert durations to beats and merge prerests """
    #totallength = score.bars[-1].start + score.bars[-1].length
    l_laststart = []
    for l in score.layers:
        ev = 0
        l_laststart.append(l.events[-1].start)
        for e in l.events: 
            # divide event length into event and rest
            try:
                debugtest = e.pmvalue
            except AttributeError:
                logging.error('Event %s in layer %s has no pmvalue attribute.'
                        % (e.index, l.layernumber))
            if e.pmvalue == 0:
                # pm flag is zero - event is silent
                e.elength = 0
                e.rlength = e.length - e.prerest
            else:
                e.elength, e.rlength = pmf.divideevent(e.length - e.prerest, e.duration)
            # merge prerest back to previous event
            if ev == 0:
                # stop here for the first event
                e.location = 0
                ev += 1
                continue
            # add prerest to previous rlength and length
            l.events[ev - 1].rlength += e.prerest
            l.events[ev - 1].length += e.prerest
            # move event start
            e.start += e.prerest
            # adjust event length
            e.length -= e.prerest
            # reset prerest to zero
            e.prerest = 0
            ev += 1
    # find first last event to start
    laststart = min(l_laststart)
    l_lastend = []
    for l in score.layers:
        lchop = len(l.events) - 1
        for e in l.events:
            if e.start > laststart:
                lchop = e.index
                break
        # cut events that start after first last event
        lostevents = len(l.events) - 1 - lchop
        l.events = l.events[:lchop]
        logging.info('%s events chopped from layer %s' % \
                (lostevents, l.layernumber))
        l_lastend.append(l.events[-1].start + l.events[-1].length)
    # find last beat of longest layer
    lastbeat = max(l_lastend)
    score.elapsed_time = pmf.beatelapsed(lastbeat, score.bars, score.sections)
    for l in score.layers:
        # stretch last event rest to total length
        layerlength = l.events[-1].start + l.events[-1].length
        diff = lastbeat - layerlength
        if diff < 0:
            raise 'Problem!'
        if diff == 0:
            logging.info('Layer %s is longest layer.' % l.layernumber)
        else:
            l.events[-1].rlength += diff
            l.events[-1].length += diff
            logging.info('%s beats added to layer %s.' % (diff, l.layernumber))
    # cut sections
    for s in score.sections:
        if s.start >= lastbeat:
            schop = s.id
            break
    try:
        score.sections = score.sections[:schop]
        logging.info('Chopping from %s, section %s' % (lastbeat, schop))
    except NameError:
        pass
    # cut bars to lastbeat
    for b in score.bars:
        if b.start >= lastbeat:
            bchop = b.id
            break
    score.bars = score.bars[:bchop]
    # adjust last bar length
    lastbarlength = lastbeat - score.bars[-1].start
    logging.info('Last bar adjusted from %s to %s.' % \
            (score.bars[-1].length, lastbarlength))
    score.bars[-1].length = lastbarlength
    for l in score.layers:
        for e in l.events:
            # record location of event start
            e.location = pmf.beatelapsed(e.start, score.bars, score.sections) / score.elapsed_time

def loop8(score):
    """ assign dynamics, consonance, register, activity to events; locate start of negative section """
    score.negelayers = []
    negestart = False
    for l in score.layers:
        for e in l.events:
            e.dynamic = pmf.eventdynamic(e.location, e.change[1])
            e.register = pmf.eventregister(e.location)
            e.activity = pmf.eventactivity(e.location)
            e.consinter = pmf.eventconsonance(e.location, 'inter')
            e.consintra = pmf.eventconsonance(e.location, 'intra')
        # find first negative event after start of negative section
        nege = False
        for i, e in enumerate(l.events):
            if nege:
                e.negative = True
                continue
            if ((e.location > pmf.negativesection and negestart is False) or \
                    (negestart is not False and e.start > (negestart + (pmf.negativesectiongap/2)))) \
                    and e.pmvalue < 0:
                nege, negestart = i, e.start
                score.negelayers.append((e.start, l.layernumber))
                l.negstart = e.start
                e.negative, e.firstnege, e.repetition = True, True, False
                e.debug += 'event starts negative section\n'
                while l.events[i-1].start >= negestart - pmf.negativesectiongap: 
                    # silence events in the changeover gap
                    i -= 1
                    #l.events[i].rlength += l.events[i].elength
                    #l.events[i].elength = 0
                    #l.events[i].repetition = False
                    #l.events[i].debug += 'silenced for gap in loop8\n'
                    pmf.silence(l.events[i], 'silenced for gap in loop8\n')
    score.negelayers.sort()
    logging.info('Negative section starts %s' % score.negelayers)
    # second time through, deal with harmony_dynamics from symbols
    for l in score.layers:
        for e in l.events:
            try:
                if e.symbols['harmony_dynamics'].split()[-1] != 'dynamic':
                    continue
            except AttributeError:
                continue
            # get list of concurrent events
            oes = pmf.concurrentevents(e.start + e.prerest, score)
            other_dynamics = []
            for i in range(len(oes)):
                if i == l.layernumber:
                    # don't include this layer
                    continue
                # filter out events which also may change dynamic
                try:
                    if score.layers[i].events[oes[i]].symbols['harmony_dynamics'].split()[-1] != 'dynamic':
                        other_dynamics.append(score.layers[i].events[oes[i]].dynamic)
                    else:
                        logging.debug('Rejected layer %s event %s for dynamics' % (i, oes[i]))
                except AttributeError:
                    pass
                except IndexError:
                    print 'Layer %s, event %s' % (i, oes)
                    raise 'This is bad!'
            if len(other_dynamics) == 0:
                logging.debug('No appropriate dynamics found.')
                continue
            if e.symbols['harmony_dynamics'].split()[1] == 'one':
                other_dynamic = random.choice(other_dynamics)
            elif e.symbols['harmony_dynamics'].split()[1] == 'average':
                other_dynamic = sum(other_dynamics) / float(len(other_dynamics))
            new_dynamic = ( e.dynamic + other_dynamic ) / 2.0
            logging.debug('Adjusted dynamic by %s' % (e.dynamic - new_dynamic))
            e.dynamic = new_dynamic

def loop9(score):
    """ assign layers to players, choose central sound pitches """
    # layer 0 is reserved for soprano
    # assign others by order in score.negelayers
    # percussion, clarinet, guitar, then random
    negelayers = [l for _, l in score.negelayers]
    if 0 in negelayers:
        del negelayers[negelayers.index(0)]
    poselayers = list(set(range(1, len(score.layers))).difference(set(negelayers)))
    random.shuffle(poselayers)
    negelayers.extend(poselayers)
    for p in instrumentation.players:
        if p.name == 'soprano':
            score.layers[0].player = p
        elif p.name == 'percussion':
            score.layers[negelayers[0]].player = p
        elif p.name == 'clarinet':
            score.layers[negelayers[1]].player = p
        elif p.name == 'guitar':
            score.layers[negelayers[2]].player = p
        elif p.name == 'piano':
            score.layers[negelayers[3]].player = p
        elif p.name == 'cello':
            score.layers[negelayers[4]].player = p
        elif p.name == 'electronics':
            score.layers[negelayers[5]].player = p
    for l in score.layers:
        e_chordnotes = False
        for e in l.events:
            # subsidiary notes
            if e.symbols['subsidiary_number'] is not None:
                e.subsnotes = []
                for s in score.subsidiaries:
                    if s['page'] == e.symbols['page'] and s['number'] == e.symbols['subsidiary_number']:
                        p1 = pmf.pitch2number(s['pitch_1'], s['octave_1'])
                        if s['pitch_2'] is not None:
                            p2 = pmf.pitch2number(s['pitch_2'], s['octave_2'])
                        else:
                            p2 = None
                        e.subsnotes.append((p1, p2, s['articulation']))
            # we need to choose central sound notes if
            #   a) event is not repetition (repetition is False)
            #       and does not have a repetition before it (repeatsbefore is False)
            #   b) it has been copied to fill an empty event (wasempty is True)
            #   c) there are no current notes (i.e. first event)
            #   d) it is the first event in the negative section
            if (e.repetition is False and e.repeatsbefore is False) or \
                    e.wasempty is True or e_chordnotes is False or e.firstnege is True:
                e.csnotesassigned = True
                # get central sound chord pitches
                e_type_index = pmf.event_types[e.symbols['event_type']]
                e_chordnumber = score.chordorder.index(e_type_index) + 1 # chord db is 1-indexed
                e_page_index = l.symbol_pages.index(e.symbols['page'])
                e.cs.npage = l.notes_pages[e_page_index]
                e_chordnotes = []
                clusterbottom = False
                clustertop = False
                for c in score.chords:
                    if c['page'] == e.cs.npage and c['number'] == e_chordnumber:
                        n = pmf.pitch2number(c['pitch'], c['octave'])
                        e_chordnotes.append((n, c['articulation']))
                        if c['articulation'] == 'cluster_top':
                            clustertop = pmf.pitch2number(c['pitch'], c['octave'])
                        elif c['articulation'] == 'cluster_bottom':
                            clusterbottom = pmf.pitch2number(c['pitch'], c['octave'])
                # fill clusters
                if clustertop and clusterbottom:
                    # we have top and bottom already
                    for n in range(clusterbottom + 1, clustertop):
                        e_chordnotes.append((n, None))
                e.cs.number = int(e.length ** (1/1.3))
                # need to get the right number of notes here so that repeated events have the same note set
                e_cschords = []
                # make sure we don't ask for too many notes
                if e.cs.number < len(e_chordnotes):
                    posschords = pmf.factorial(len(e_chordnotes)) / \
                            (pmf.factorial(e.cs.number) * pmf.factorial(len(e_chordnotes) - e.cs.number))
                    if posschords > 10: posschords = 10
                    for i in range(posschords):
                        e_cschords.append(random.sample(e_chordnotes, e.cs.number))
                        e_cschords[-1].sort()
                else:
                    e_chordnotes.sort()
                    e_cschords.append(e_chordnotes)
                e.cs.chords = e_cschords
                if len(e_cschords) == 0:
                    logging.error('Zero length chord list')
                # keep a copy of CentralSound object for next inherited event
                e_cs = e.cs
            else:
                e.csnotesassigned = 'Inherited'
                e.cs = e_cs
    """ copy events to percussion, piano staves """
    score.staves = []
    for l in score.layers:
        l.player.staves[0].events = l.events
        if l.player.name == 'percussion':
            # copy events to other percussion staves
            l.player.staves[1].events = copy.deepcopy(l.events)
            l.player.staves[2].events = copy.deepcopy(l.events)
        if l.player.name == 'piano':
            l.player.staves[1].events = copy.deepcopy(l.events)
        for s in l.player.staves:
            score.staves.append(s)
    """ assign event types to staves for percussion, piano """
    etypes = pmf.event_types.keys()
    for l in score.layers:
        if l.player.name == 'percussion':
            random.shuffle(etypes)
            perc_etypes = {'perccrotales': etypes[0:3] + [etypes[6]],
                    'marimbavibraphone': etypes[3:6], 'timpano': etypes[2:4]}
            for st in l.player.staves:
                for e in st.events:
                    if e.symbols['event_type'] not in perc_etypes[st.name]:
                        pmf.silence(e, 'unused event type in loop9\n')
        elif l.player.name == 'piano':
            random.shuffle(etypes)
            piano_etypes = {'kbtreble': etypes[0:5], 'kbbass': etypes[2:7]}
            for st in l.player.staves:
                for e in st.events:
                    if e.symbols['event_type'] not in piano_etypes[st.name]:
                        pmf.silence(e, 'unused event type in loop9\n')

def loop10(score):
    """ score consonance of chords and order appropriately """
    consmin, consmax = 2, -2
    for st in score.staves:
        previouschord = None
        for e in st.events:
            if e.csnotesassigned is not True:
                continue
            # get instrument range
            if e.negative is True:
                instrument = st.neg[0]
            else:
                instrument = st.pos[0]
            if instrument.name == 'speaker' or instrument.name == 'cymbals':
                # percussion pitches dealt with later
                e.debug += '%s event, no chords assigned here' % instrument.name
                continue
            lo, hi = pmf.instrumentrange(instrument, e.register)
            e.eventpitches = {}
            for i, chord in enumerate(e.cs.chords):
                if previouschord is None:
                    previouschord = copy.copy(chord)
                    steps = 0
                else:
                    steps = e.change[0]
                # transpose to fit in range
                e.cs.chords[i] = pmf.transposechord(chord, previouschord, steps, lo, hi)
                e.debug += 'freshly transposed chord %s %s\n\tpc %s\n\tsteps %s lo %s hi %s\n' % \
                        (i, e.cs.chords[i], previouschord, steps, lo, hi)
                try:
                    for p, _ in e.cs.chords[i]:
                        dontcare = e.eventpitches.setdefault(p, 0)
                        e.eventpitches[p] += 1
                except TypeError:
                    print st.name, e.index, previouschord, e.eventpitches
            previouschord = copy.copy(e.cs.chords[0])
            # record most common pitches for consonance scoring
            e.eventpitcheslist = [(nn, pp) for pp, nn in e.eventpitches.items()]
            e.eventpitcheslist.sort(reverse=True)
            # there should always be at least two
            e.cs.diad = [(pp, None) for nn, pp in e.eventpitcheslist[0:2]]
    for st in score.staves:
        for e in st.events:
            if e.csnotesassigned is not True:
                continue
            # get cs diad in all layers
            op = []
            for ol, oe in enumerate(pmf.concurrentevents(e.start + e.prerest, score)):
                try:
                    op.extend(score.layers[ol].events[oe].cs.diad)
                except AttributeError: # other event doesn't have assigned pitches
                    pass
            # score chords
            chords_deco = []
            for chord in e.cs.chords:
                chordscore = 0
                paircount = 0
                for pair in pmf.combinations(chord + op, 2):
                    chordscore += pmf.consonance(pair[0][0], pair[1][0])
                    paircount += 1
                chordscore = chordscore / float(paircount)
                chords_deco.append((chordscore, chord))
                # record min/max
                if consmin > chordscore:
                    consmin = chordscore
                if consmax < chordscore:
                    consmax = chordscore
            chords_deco.sort()
            e.cs.chords_deco = chords_deco
            # calculate target with reference to assigned values
            # y = (max - min).x + min
            target_consonance = (e.consinter * (consmax - consmin)) + consmin
            e.cs.target_consonance = target_consonance
            chords_closest = []
            # re-sort by closest match to target
            for chordscore, c in e.cs.chords_deco:
                prox = abs(target_consonance - chordscore)
                chords_closest.append((prox, chordscore, c))
            chords_closest.sort()
            e.cs.chords_closest = chords_closest
            # write back to chords list to be used in this order
            e.cs.chords = [chord for prox, chordscore, chord in chords_closest]
    logging.info('Chords consonance %s - %s' % (consmin, consmax))

def loop11(score):
    """ order components, get accessory pitches, score consonance of subsidiary notes and order appropriately """
    consmin, consmax = 2, -2 # for recording only
    for st in score.staves:
        for e in st.events:
            # order of components
            if e.elength == 0:
                e.csorder = ''
                e.subsnotes = False
                continue
            elif e.repetition:
                e.csorder = e.repetition[0]
            else:
                e.csorder = pmf.etorder[e.symbols['event_type']]
            # get instrument range
            if e.negative is True:
                instrument = st.neg[0]
            else:
                instrument = st.pos[0]
            e.csorder = pmf.subsidiaryposition(e.csorder, e.symbols['subsidiary_position'], instrument.name)
            if instrument.name == 'speaker' or instrument.name == 'cymbals':
                if e.csorder == '':
                    # nothing to do - convert to rest
                    pmf.silence(e, 'no csorder, silenced in loop 11\n')
                    #e.rlength += e.elength
                    #e.elength = 0
                    #e.subsnotes = False
                # speaker needs no pitches, percussion pitches dealt with later
                continue
            lo, hi = pmf.instrumentrange(instrument, e.register)
            # get possible accessory pitches
            e.csorder, e.accessorypitches = pmf.accessorypitches(e.csorder, e.register, lo, hi)
            if e.csorder == '':
                # nothing to do - convert to rest
                pmf.silence(e, 'no csorder, silenced in loop 11\n')
                #e.rlength += e.elength
                #e.elength = 0
                #e.subsnotes = False
            if e.subsnotes is False:
                continue
            # get first chord in events' central sound
            chordnotes = [p for p, _ in e.cs.chords[0]]
            # get first chord in previous events' central sound
            if e.index == 0:
                previouschord = chordnotes
                steps = 0
            else:
                previouschord = [p for p, _ in st.events[e.index - 1].cs.chords[0]]
                steps = e.change[0]
            subs_deco = []
            for p1, p2, articulation in e.subsnotes:
                try:
                    npage = e.cs.npage
                except TypeError:
                    logging.error('Central sound problem %s staff, event %s' % (st.name, e.index))
                    npage = 'A'
                # transpose by degree of change and to range of instrument
                p1, p2 = pmf.transposesubsidiaries(p1, p2, previouschord, \
                        npage, steps, lo, hi)
                subscore = 0
                paircount = 0
                if p2 is None:
                    noteslist = chordnotes + [p1]
                else:
                    noteslist = chordnotes + [p1, p2]
                for pair in pmf.combinations(noteslist, 2):
                    subscore += pmf.consonance(pair[0], pair[1])
                    paircount += 1
                subscore = subscore / float(paircount)
                subs_deco.append((subscore, p1, p2, articulation))
                # record min/max
                if consmin > subscore:
                    consmin = subscore
                if consmax < subscore:
                    consmax = subscore
            subs_deco.sort()
            e.subsnotes_deco = subs_deco
            # calculate target with reference to assigned values
            # y = (max - min).x + min
            target_consonance = (e.consintra * (consmax - consmin)) + consmin
            subs_closest = []
            for subscore, p1, p2, articulation in subs_deco:
                # re-sort by closest match to target
                prox = abs(target_consonance - subscore)
                subs_closest.append((prox, subscore, p1, p2, articulation))
            subs_closest.sort()
            e.subsnotes_closest = subs_closest
            # write back to subsnotes list to be used in this order
            e.subsnotes = [(p1, p2, aa) for prox, subscore, p1, p2, aa in subs_closest]
    logging.info('Subsidiaries consonance %s - %s' % (consmin, consmax))

def loop12(score):
    """ create rhythms """
    for st in score.staves:
        logging.info('Starting staff %s' % st.name)
        for e in st.events:
            # get instrument details
            if e.negative is True:
                instrument = st.neg[0]
            else:
                instrument = st.pos[0]
            # number of attacks
            if st.name == 'soprano':
                # invert activity for soprano staff
                e.activity = 1 - e.activity
            if e.csorder != '' and instrument.name not in ['speaker', 'electronics']:
                #e.attacknumber = int(round(e.activity * st.attackconstant * math.sqrt(e.elength))) + 1
                e.attacknumber = int(round(e.activity * st.attackconstant * (e.elength**(1/3.0)))) + 1
                # allocate attacks to components
                e.attackallocation = pmf.allocateattacks(e.attacknumber, e.csorder, e.symbols['subsidiary_number'])
            elif instrument.name in ['speaker', 'electronics']:
                e.attacknumber = 1
                e.attackallocation = [('C', 1)]
            else:
                e.attackallocation = []
            # divide duration between attacks
            e.attacklengths = []
            for comp, attacks in e.attackallocation:
                e.attacklengths.extend(pmf.attackproportions(e.location, comp, attacks, \
                        e.symbols['subsidiary_contour'], e.symbols['quality']))
            attacksum = sum([p for c, p in e.attacklengths])
            for i, ap in enumerate(e.attacklengths):
                bp = (ap[1] * e.elength) / float(attacksum)
                e.attacklengths[i] = (ap[0], bp)
            # initialize component list
            e.components = []
            if e.prerest > 0:
                prstart = e.start
                prvals = pmf.beats2noteval(prstart, e.prerest, score.bars)
                for dur, lyv in prvals:
                    e.components.append(Note('prerest', prstart, dur, lyv, pitch='r'))
                    prstart += dur
            # quantize component rhythm
            if len(e.attacklengths) == 0:
                logging.debug('No attacks in %s staff, event %s' % (st.name, e.index))
            else:
                e.components += pmf.quantizerhythm(e.start + e.prerest, \
                        copy.deepcopy(e.attacklengths), e.activity, score.bars, instrument.maxgrace)
            # add final rest component
            if e.rlength > 0:
                rstart = e.start + e.prerest + e.elength
                rvals = pmf.beats2noteval(rstart, e.rlength, score.bars)
                for dur, lyv in rvals:
                    e.components.append(Note('rest', rstart, dur, lyv, pitch='r'))
                    rstart += dur

def loop13(score):
    """ assign pitches to component notes """
    for st in score.staves:
        for e in st.events:
            # get instrument details
            if e.negative is True:
                instrument = st.neg[0]
            else:
                instrument = st.pos[0]
            # deal with special cases
            if instrument.name == 'speaker':
                if st.name == 'soprano':
                    spitch = "b'"
                elif st.name == 'timpano':
                    spitch = "c'"
                for note in e.components:
                    if note.ctype == 'rest':
                        note.pitch = 'r'
                    else:
                        note.pitch = spitch
                continue
            elif instrument.name == 'cymbals':
                # unpitched timbral assignment for percussion
                e.components = pmf.percussionpitches(e.components,
                        st.instrumenttimbre[e.symbols['timbre']], e.register)
                continue
            elif st.name == 'electronics':
                spitch = pmf.electronicspitches(e.symbols['event_type'])
                for note in e.components:
                    if note.ctype == 'rest':
                        note.pitch = 'r'
                    else:
                        note.pitch = spitch
                continue
            # chordsize depends on event type
            chordsize = instrument.stretch[pmf.event_types[e.symbols['event_type']]][0]
            if e.subsnotes is not False:
                subsnoteiter = iter(e.subsnotes)
            csnoteiter = pmf.flatten(e.cs.chords, list)
            componentsiter = iter(e.components)
            wastied, lastpitch = False, None
            for i, note in enumerate(componentsiter):
                if wastied:
                    note.pitch = lastpitch
                elif note.ctype == 'o':
                    # rest
                    note.pitch = 'r'
                elif note.ctype in 'bAa':
                    # accessory alone
                    n_pitch = random.choice(e.accessorypitches) + instrument.transposition
                    note.pitch = pmf.pitch2lily(n_pitch)
                elif note.ctype in 'srvTtu':
                    # subsidiary
                    try:
                        subspitch = subsnoteiter.next()
                    except StopIteration:
                        subsnoteiter = iter(e.subsnotes)
                        subspitch = subsnoteiter.next()
                    # choose pitches from clusters
                    if subspitch[2] in ['cluster', 'accent', 'accent_slur']:
                        if subspitch[0] > subspitch[1]:
                            s0, s1 = subspitch[1], subspitch[0]
                        else:
                            s0, s1 = subspitch[0], subspitch[1]
                        try:
                            subs = random.sample(xrange(s0, s1 + 1), 2)
                        except ValueError:
                            subs = [s0, s1]
                        subspitch = (subs[0], subs[1], None)
                    if subspitch[2] == 'appogiatura':
                        appogpitch = pmf.pitch2lily(subspitch[0] + instrument.transposition)
                        appog = Note(note.ctype, note.start, 0, 'g', pitch=appogpitch)
                        note.pitch = pmf.pitch2lily(subspitch[1] + instrument.transposition)
                        e.components.insert(i, appog)
                        note = componentsiter.next()
                    elif chordsize == 1 or subspitch[1] is None:
                        note.pitch = pmf.pitch2lily(subspitch[0] + instrument.transposition)
                        note.articulation = pmf.notearticulation(subspitch[2])
                    else:
                        n_chord = [sp + instrument.transposition for sp in subspitch[0:2]]
                        note.pitch = pmf.pitches2lilychord(n_chord)
                        note.articulation = pmf.notearticulation(subspitch[2])
                elif note.ctype in 'CB':
                    # centralsound
                    pitches = []
                    for n in range(chordsize):
                        try:
                            pitches.append(csnoteiter.next())
                        except StopIteration:
                            csnoteiter = pmf.flatten(e.cs.chords, list)
                            pitches.append(csnoteiter.next())
                    if chordsize == 1:
                        note.pitch = pmf.pitch2lily(pitches[0][0] + instrument.transposition)
                        note.articulation = pmf.notearticulation(pitches[0][0])
                    else:
                        n_chord = [p + instrument.transposition for p, _ in pitches]
                        note.pitch = pmf.pitches2lilychord(n_chord)
                        # take the second pitch's articulation for page A number 1
                        note.articulation = pmf.notearticulation(pitches[1][1])
                    for p in pitches:
                        if p[1] == 'appogiatura':
                            appogpitch = pmf.pitch2lily(p[0] + instrument.transposition)
                            appog = Note(note.ctype, note.start, 0, 'g', tied=True, pitch=appogpitch)
                            e.components.insert(i, appog)
                            note = componentsiter.next()
                        elif p[1] == 'accent_appogiatura':
                            appogpitch = pmf.pitch2lily(p[0] + instrument.transposition)
                            appog = Note(note.ctype, note.start, 0, 'g', tied=True, pitch=appogpitch, articulation='->')
                            e.components.insert(i, appog)
                            note = componentsiter.next()
                if note.tied:
                    wastied, lastpitch = True, note.pitch
                else:
                    wastied, lastpitch = False, None

def loop14(score):
    """ add dynamics and articulations """
    dd = len(pmf.dynamics)
    for st in score.staves:
        currentdynamicstr = None
        currentstickstr = ''
        currentgtrtone = ''
        for e in st.events:
            # skip if elength is zero
            if e.elength == 0:
                continue
            # get instrument details
            if e.negative is True:
                instrument = st.neg[0]
            else:
                instrument = st.pos[0]
            dynamicstr = '\\' + pmf.dynamics[int(e.dynamic * dd)]
            stickstr = ''
            # add stick choice if percussion
            if instrument.sticks:
                stickstr = pmf.percsticks(e.symbols['timbre'][0:4])
            gtrtone = ''
            # add tone choice if eguitar
            if instrument.tone:
                gtrtone = pmf.gtrtone(e.symbols['timbre'])
            # special cases
            if instrument.name == 'speaker' or instrument.name == 'electronics':
                e_timbre = ('', '', '', '', '')
                e_quality = ('', '', '', '', '')
            elif instrument.name == 'cymbals':
                # percussion timbre
                e.components = pmf.perctimbrequality(e.symbols['timbre'], 
                        e.symbols['quality'], e.components, e.location, e.negative, st.pos)
                e_timbre = ('', '', '', '', '')
                e_quality = ('', '', '', '', '')
            else:
                e_timbre = pmf.picktimbre(e.symbols['timbre'], instrument,
                        e.location, e.negative)
                e_quality = pmf.pickquality(e.symbols['quality'], instrument,
                        e.location, e.negative)
            e.lystart += e_timbre[0]
            e.lystart += e_quality[0]
            wastied = False
            for i, c in enumerate(e.components):
                if i == 0:
                    # articulations for the first note
                    c.articulation += e_timbre[1]
                    c.articulation += e_quality[1]
                if c.pitch != 'r' and wastied is False:
                    # articulations for non-rests - gracenotes don't take
                    # certain articulations
                    if c.value == 'g' and e_timbre[2] in ['-\\slaptongue', '\\flageolet', '^\\bartok']:
                        pass
                    else:
                        c.articulation += e_timbre[2]
                    if c.value == 'g' and e_quality[2] in ['\\trill', '^\\ricochet']:
                        pass
                    else:
                        c.articulation += e_quality[2]
                    if stickstr != currentstickstr:
                        c.articulation += stickstr
                        currentstickstr = stickstr
                    if gtrtone != currentgtrtone:
                        c.articulation += gtrtone
                        currentgtrtone = gtrtone
                    if dynamicstr != currentdynamicstr:
                        c.articulation += dynamicstr
                        currentdynamicstr = dynamicstr
                    # lyrics for soprano
                    if instrument.name == 'soprano':
                        c.lyric = pmf.lyrics(e.symbols['timbre'], c.length)
                wastied = c.tied
            # close articulations
            e.components[-1].articulation += e_timbre[3]
            e.components[-1].articulation += e_quality[3]

def gracespace(score):
    for st in score.staves:
        bariter = iter(score.bars)
        b = bariter.next()
        eventiter = iter(st.events)
        e = eventiter.next()
        componentiter = iter(e.components)
        c = componentiter.next()
        st.gracespace = {}
        while 1:
            if c.start < b.start:
                pass
            elif c.start == b.start and c.length == 0:
                b.gracespace.setdefault(st.name, 0)
                b.gracespace[st.name] += 1
            elif c.start > b.start:
                try:
                    b = bariter.next()
                except StopIteration:
                    #print b.start, e.start, c.start, st.name, 'out of bars'
                    break
                continue
            try:
                c = componentiter.next()
            except StopIteration:
                try:
                    e = eventiter.next()
                    componentiter = iter(e.components)
                    c = componentiter.next()
                except StopIteration:
                    #print b.start, e.start, c.start, st.name, 'out of events'
                    break
    for b in score.bars:
        try:
            maxgrace = max(b.gracespace.values())
        except ValueError:
            maxgrace = 0
        for st in score.staves:
            b.gracespace.setdefault(st.name, 0)
            st.gracespace[b.start] = maxgrace - b.gracespace[st.name]
        b.gracespace['max'] = maxgrace

def serialize(score):
    """ add every output to a stream dictionary """
    score.barstream = {}
    score.barspacestream = {}
    currentbarlength = 0
    currentsection = -1
    for b in score.bars:
        # barcheck
        b_output = '|\n'
        # section 
        if b.section != currentsection:
            # overwrite barcheck at start of section
            b_output = lysection(b.section, b.id, score.sections[b.section].tempo)
            currentsection = b.section
            currentbarlength = 0 # reset to force time signature
        # bar
        b_output += '%% bar %03d\n' % b.id
        # timesignature
        if b.length != currentbarlength:
            b_output += lytimesig(b.length)
            currentbarlength = b.length
        score.barstream[b.start] = b_output
        # barspace
        score.barspacestream[b.start] = lygracespace(b.gracespace['max'])
        score.barspacestream[b.start] += lyspace(b.length)
    score.outputkeys = list(set(score.barstream.keys()) | set(score.barspacestream.keys()))
    score.outputkeys.sort()
    for st in score.staves:
        logging.info('Serializing %s staff' % st.name)
        st.eventstream = {}
        st.notestream = {}
        st.instchangestartstream = {}
        st.instchangeendstream = {}
        st.clefstream = {}
        st.gracestream = {}
        st.tupletstartstream = {}
        st.tupletendstream = {}
        currentclef = ''
        currentinstchange = st.poschange
        instfinishchange = False
        for e in st.events:
            e_lyevent = lyevent(e.index, e.symbols['page'], e.symbols['number'], e.pmvalue)
            st.eventstream[e.start] = e_lyevent + e.lystart
            eventend = e.start + e.length
            currentgracestart, currentgracestring = None, ''
            currenttuplet = None
            # get instrument
            if e.negative is True:
                instrument = st.neg[0]
                instchange = st.negchange
            else:
                instrument = st.pos[0]
                instchange = st.poschange
            # instchangestream
            if instchange != currentinstchange:
                st.instchangestartstream[e.start] = instchange[0] + '\n'
                instfinishchange = True
                currentinstchange = instchange
            for c in e.components:
                # clefstream
                clef = lyclef(instrument.clefs, c.pitch, currentclef)
                if clef != currentclef:
                    st.clefstream[c.start] = clef
                    currentclef = clef
                # gracenotes
                gracespace = st.gracespace.get(c.start, 0)
                if c.value != 'g' and gracespace > 0 and currentgracestart is None:
                    # grace spacing without gracenotes
                    st.gracestream[c.start] = lygracespace(gracespace, True)
                elif c.value == 'g' and gracespace > 0 and currentgracestart is None:
                    # grace spacing with gracenotes
                    currentgracestring = lygracespace(gracespace, False) + lynote(c) + '[ '
                    currentgracestart = c.start
                elif c.value == 'g' and gracespace == 0 and currentgracestart is None:
                    # initialise new gracenote group
                    currentgracestring = '\n\\grace { ' + lynote(c) + '[ '
                    currentgracestart = c.start
                elif c.value == 'g' and currentgracestart is not None:
                    # add next grace note to string
                    currentgracestring += lynote(c)
                elif c.value != 'g' and currentgracestart is not None:
                    # close gracenote group and add to dict
                    currentgracestring += '] } % end grace\n'
                    st.gracestream[currentgracestart] = currentgracestring.replace('[ ]', '')
                    currentgracestart, currentgracestring = None, ''
                # notestream
                st.notestream[c.start] = lynote(c)
                # finish instchange
                if instfinishchange:
                    st.instchangeendstream[c.start] = instchange[1] + '\n'
                    instfinishchange = False
                # tupletstream
                tuplet = lytuplet(c.tuplet)
                if tuplet != currenttuplet:
                    if tuplet is not None:
                        st.tupletstartstream[c.start] = tuplet[0]
                    if currenttuplet is not None:
                        st.tupletendstream[c.start] = currenttuplet[1]
                    currenttuplet = tuplet
            # end current tuplet
            if currenttuplet is not None:
                st.tupletendstream[eventend] = currenttuplet[1]
                currenttuplet = None
        st.outputkeys = set(st.eventstream.keys())
        st.outputkeys |= set(st.notestream.keys())
        st.outputkeys |= set(st.instchangestartstream.keys())
        st.outputkeys |= set(st.instchangeendstream.keys())
        st.outputkeys |= set(st.clefstream.keys())
        st.outputkeys |= set(st.gracestream.keys())
        st.outputkeys |= set(st.tupletstartstream.keys())
        st.outputkeys |= set(st.tupletendstream.keys())

def lyoutput(score):
    """ create section output from stream dictionaries """
    # bar output
    score.baroutput = [''] * len(score.sections)
    sectionstarts = [s.start for s in score.sections[1:]]
    nextsectionstart = sectionstarts.pop(0)
    section = 0
    for cursor in score.outputkeys:
        # move to next section
        if cursor == nextsectionstart:
            score.baroutput[section] = pmf.prettify(score.baroutput[section])
            section += 1
            try:
                nextsectionstart = sectionstarts.pop(0)
            except IndexError:
                pass
        # bar stream
        score.baroutput[section] += score.barstream.get(cursor, '')
        # barspace stream
        score.baroutput[section] += score.barspacestream.get(cursor, '')
    score.baroutput[section] = pmf.prettify(score.baroutput[section])
    for st in score.staves:
        st.output = [''] * len(score.sections)
        # make key list
        st.outputkeys = list( set(score.outputkeys) | st.outputkeys )
        st.outputkeys.sort()
        sectionstarts = [s.start for s in score.sections[1:]]
        nextsectionstart = sectionstarts.pop(0)
        section = 0
        for cursor in st.outputkeys:
            # endtuplet stream
            st.output[section] += st.tupletendstream.get(cursor, '')
            # instrumentchange start
            st.output[section] += st.instchangestartstream.get(cursor, '')
            # move to next section
            if cursor == nextsectionstart:
                st.output[section] = pmf.prettify(st.output[section])
                section += 1
                try:
                    nextsectionstart = sectionstarts.pop(0)
                except IndexError:
                    pass
            # bar stream
            st.output[section] += score.barstream.get(cursor, '')
            # event stream
            st.output[section] += st.eventstream.get(cursor, '')
            st.output[section] += st.clefstream.get(cursor, '')
            # gracenotes
            st.output[section] += st.gracestream.get(cursor, '')
            # note streams
            st.output[section] += st.tupletstartstream.get(cursor, '')
            st.output[section] += st.notestream.get(cursor, '')
            # instrumentchange end
            st.output[section] += st.instchangeendstream.get(cursor, '')
        st.output[section] = pmf.prettify(st.output[section])

def sectionlyrics(score):
    """ make lyrics strings for each section """
    for st in score.staves:
        if st.name == 'soprano':
            break
    sectionstarts = [s.start for s in score.sections[1:]] + [999999]
    nextsectionstart = sectionstarts.pop(0)
    st.lyrics.append('')
    ss = 0
    for e in st.events:
        for i, c in enumerate(e.components):
            if c.start >= nextsectionstart:
                nextsectionstart = sectionstarts.pop(0)
                st.lyrics.append('')
                ss += 1
            if i == 0:
                st.lyrics[ss] += '\n%% bar %s' % e.barnumber
                st.lyrics[ss] += lyevent(e.index, e.symbols['page'], e.symbols['number'], e.pmvalue)
            try:
                st.lyrics[ss] += c.lyric
            except AttributeError:
                pass

def electronicsdict(score):
    """ create list of electronics event dictionaries for later use """
    for st in score.staves:
        if st.name == 'electronics':
            break
    score.electronics = []
    for e in st.events:
        edict = {
                'index': e.index,
                'event_type': pmf.event_types[e.symbols['event_type']],
                'timbre': e.symbols['timbre'],
                'quality': e.symbols['quality'],
                'page': e.symbols['page'],
                'number': e.symbols['number'],
                'pmvalue': e.pmvalue,
                'negative': e.negative,
                'tempo': e.tempo,
                'prerest': e.prerest,
                'elength': e.elength,
                'rlength': e.rlength,
                'barnumber': e.barnumber,
                'register': e.register,
                'activity': e.activity,
                'pitches': [p for p, _ in e.cs.diad]
                }
        score.electronics.append(edict)

def prepareoutput(score):
    """ run all the output functions """
    gracespace(score)
    serialize(score)
    lyoutput(score)
    sectionlyrics(score)
    electronicsdict(score)

def lynote(note):
    """ return lilypond code for this note """
    art = ''
    if note.articulation is not None:
        for a in note.articulation:
            art += a
    notepitch = note.pitch
    if note.pitch is None:
        logging.error('Notepitch is none! %s' % note.start)
        notepitch = 's'
    if note.value == 'g':
        # convert any grace rests to grace spaces
        if note.pitch == 'r':
            notepitch = 's'
        noteval = '16'
    else:
        noteval = note.value
    out = '%s%s%s ' % (notepitch, noteval, art)
    if note.tied and note.pitch != 'r':
        out += '~ '
    return out

def lyclef(clefs, pitch, currentclef):
    """ choose appropriate clef for instrument and pitch """
    if clefs is None or 'r' in pitch:
        return currentclef
    # parse pitchstring back to number
    pitches = pmf.lily2pitches(pitch)
    avpitch = sum(pitches) / len(pitches)
    # test if current clef is ok for this pitch
    if currentclef is None:
        print clefs, pitch, currentclef
    for clef in clefs:
        if clef[0] in currentclef:
            if clef[1] <= avpitch <= clef[2]:
                return currentclef
    # choose new clef
    for clef in clefs:
        if clef[1] <= avpitch <= clef[2]:
            return '\n\\clef "%s"\n' % clef[0]

def lygracespace(gg, closed=True):
    """ returns grace spacer notes for bar starts """
    if closed:
        closing = '} % grace spacers\n'
    else:
        closing = '% grace spacers\n'
    if gg == 0:
        return ''
    else:
        out = '\n\\grace { '
        out += 's16 ' * gg
        return out + closing

def lyspace(beats):
    """ returns spacer notes for bar file """
    return 's32*%s ' % beats

def lytimesig(beats):
    """ convert 32nd beats to lilypond time signature """
    # consider using "#(set-time-signature 9 8 '(2 2 2 3))" to more easily edit beating patterns
    if not beats % 4:
        timesig = str( beats / 4 ) + "/8"
    elif not beats % 2:
        timesig = str( beats / 2 ) + "/16"
    else:
        timesig = str( beats ) + "/32"
    return '\n\\time %s\n' % timesig

def lysection(id, barnumber, tempo):
    """ return lilypond code for this Section """
    out = '%% section %s\n' % id
    out = '\\set Score.currentBarNumber = #%s\n' % barnumber
    out += '\\mark \\markup { \\box %c } ' % (id + 65)
    out += '\\tempo 8=%s\n' % tempo
    return out

def lyevent(index, page, number, pmvalue):
    """ return lilypond code for this Event """
    out = '\n%% event %s (%s%s) PM%s\n' % (index, page, number, pmvalue)
    return out

def lytuplet(tuplet):
    """ return lilypond code for this Tuplet """
    if tuplet is None:
        return None
    else:
        num = tuplet.numerator
        den = tuplet.denominator
        for div in xrange(4, 1, -1):
            if num % div == 0 and den % div == 0:
                num /= div
                den /= div
        tstart = '\n\\times %s/%s { %% %sx32\n' % (num, den, tuplet.numerator)
        tend = '\n} % end tuplet\n'
        return tstart, tend

def writelilypond(score):
    """ write out lilypond files """
    # make output directory
    dirname = score.timestamp
    if os.path.exists(dirname):
        print 'Directory %s exists! Overwriting.' % dirname
    else:
        os.mkdir(dirname)
    # copy definitions
    shutil.copyfile('lilypond/definitions.ly', '%s/definitions.ly' % dirname)
    # get score template
    scorefile = codecs.open('lilypond/score.ly', 'rb', encoding='utf-8')
    lyscore = scorefile.read()
    lyscoresections = [lyscore] * len(score.sections)
    scorefile.close()
    # output bar files
    barreplace = ''
    for sid, out in enumerate(score.baroutput):
        outfilestr = 'bars_s%02d.ly' % sid
        outfile = open('%s/%s' % (dirname, outfilestr), 'w')
        outfile.write(unicode(out).encode('utf-8'))
        outfile.close()
        if sid == len(score.sections) - 1:
            barreplace += '\\include "%s"' % outfilestr
        else:
            barreplace += '\\include "%s"\n%s' % (outfilestr, ' ' * 4)
        lyscoresections[sid] = lyscoresections[sid].replace('__bar__', '\\include "%s"' % outfilestr)
    # replace text in score template
    lyscore = lyscore.replace('__bar__', barreplace)
    # output staff files
    for st in score.staves:
        voicereplace = ''
        for sid, out in enumerate(st.output):
            # write section file
            outfilestr = '%s_s%02d.ly' % (st.name, sid)
            outfile = open('%s/%s' % (dirname, outfilestr), 'w')
            outfile.write(unicode(out).encode('utf-8'))
            outfile.close()
            if sid == len(score.sections) - 1:
                voicereplace += '\\include "%s"' % outfilestr
            else:
                voicereplace += '\\include "%s"\n%s' % (outfilestr, ' ' * 16)
            lyscoresections[sid] = lyscoresections[sid].replace('__%s__' % st.name, '\\include "%s"' % outfilestr)
        # soprano lyrics
        if st.name == 'soprano':
            lyricsreplace = ''
            for sid, lyr in enumerate(st.lyrics):
                # write lyrics file
                outfilestr = 'sopranolyrics_s%02d.ly' % sid
                outfile = open('%s/%s' % (dirname, outfilestr), 'w')
                outfile.write(unicode(lyr).encode('utf-8'))
                outfile.close()
                if sid == len(score.sections) - 1:
                    lyricsreplace += '\\include "%s"' % outfilestr
                else:
                    lyricsreplace += '\\include "%s"\n%s' % (outfilestr, ' ' * 16)
                lyscoresections[sid] = lyscoresections[sid].replace('__sopranolyrics__', '\\include "%s"' % outfilestr)
        # replace text in score template
        lyscore = lyscore.replace('__%s__' % st.name, voicereplace)
    # write score file
    scorefile = open('%s/score.ly' % dirname, 'w')
    scorefile.write(unicode(lyscore).encode('utf-8'))
    scorefile.close()
    # write section score files
    for sid, ss in enumerate(lyscoresections):
        scorefile = open('%s/score_s%02d.ly' % (dirname, sid), 'w')
        scorefile.write(unicode(ss).encode('utf-8'))
        scorefile.close()
    # pickle score and electronics separately
    output = open('%s/score.pkl' % dirname, 'wb')
    cPickle.dump(score, output, 2)
    output = open('%s/electronics.pkl' % dirname, 'wb')
    cPickle.dump(score.electronics, output, 2)
    return 'Files written.'

if __name__ == '__main__':
    score = Score()
    score.Build()
    prepareoutput(score)
    writelilypond(score)