File: capella.cpp

package info (click to toggle)
musescore 2.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 202,532 kB
  • ctags: 58,769
  • sloc: cpp: 257,595; xml: 172,226; ansic: 139,931; python: 6,565; sh: 6,383; perl: 423; makefile: 290; awk: 142; pascal: 67; sed: 3
file content (2673 lines) | stat: -rw-r--r-- 112,525 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
//=============================================================================
//  MuseScore
//  Linux Music Score Editor
//  $Id: capella.cpp 5637 2012-05-16 14:23:09Z wschweer $
//
//  Copyright (C) 2009-2013 Werner Schweer and others
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License version 2.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================

//
//    Capella 2000 import filter
//
#include <assert.h>
#include "libmscore/mscore.h"
#include "capella.h"
#include "libmscore/score.h"
#include "libmscore/part.h"
#include "libmscore/staff.h"
#include "libmscore/rest.h"
#include "libmscore/chord.h"
#include "libmscore/note.h"
#include "libmscore/utils.h"
#include "libmscore/lyrics.h"
#include "libmscore/timesig.h"
#include "libmscore/clef.h"
#include "libmscore/pitchspelling.h"
#include "libmscore/keysig.h"
#include "libmscore/slur.h"
#include "libmscore/tie.h"
#include "libmscore/box.h"
#include "libmscore/measure.h"
#include "libmscore/sig.h"
#include "libmscore/tuplet.h"
#include "libmscore/segment.h"
#include "libmscore/layoutbreak.h"
#include "libmscore/dynamic.h"
#include "libmscore/barline.h"
#include "libmscore/volta.h"
#include "libmscore/stafftext.h"
#include "libmscore/trill.h"
#include "libmscore/arpeggio.h"
#include "libmscore/breath.h"
#include "libmscore/hairpin.h"

extern QString rtf2html(const QString &);

namespace Ms {

//---------------------------------------------------------
//   errmsg
//---------------------------------------------------------

const char* Capella::errmsg[] = {
      "no error",
      "bad file signature, no Capella file or not from version 2000 (3.0) or later?",
      "unexpected end of file",
      "bad voice signature",
      "bad staff signature",
      "bad system signature",
      };

//---------------------------------------------------------
//   addDynamic
//---------------------------------------------------------

static void addDynamic(Score* score, Segment* s, int track, const char* name)
      {
      Dynamic* d = new Dynamic(score);
      d->setDynamicType(name);
      d->setTrack(track);
      s->add(d);
      }

//---------------------------------------------------------
//   addArticulationText
//---------------------------------------------------------

static void addArticulationText(Score* score, ChordRest* cr, int track, const QString& name)
      {
      Articulation* na = new Articulation(score);
      na->setTrack(track);
      na->setSubtype(name);
      cr->add(na);
      }

//---------------------------------------------------------
//   SetCapGraceDuration
//---------------------------------------------------------

static void SetCapGraceDuration(Chord* chord,ChordObj* o)
      {
      NoteType nt = NoteType::APPOGGIATURA;
      if (o->nTremoloBars > 0)
            nt = NoteType::ACCIACCATURA;
      ((Chord*)chord)->setNoteType(nt);
      if (o->t == TIMESTEP::D4) {
            ((Chord*)chord)->setNoteType(NoteType::GRACE4);
            chord->setDurationType(TDuration::DurationType::V_QUARTER);
            }
      else if (o->t == TIMESTEP::D_BREVE)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_BREVE);
      else if (o->t == TIMESTEP::D1)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_WHOLE);
      else if (o->t == TIMESTEP::D2)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_HALF);
      else if (o->t == TIMESTEP::D16) {
            ((Chord*)chord)->setNoteType(NoteType::GRACE16);
            chord->setDurationType(TDuration::DurationType::V_16TH);
            }
      else if (o->t == TIMESTEP::D32) {
            ((Chord*)chord)->setNoteType(NoteType::GRACE32);
            chord->setDurationType(TDuration::DurationType::V_32ND);
            }
      else if (o->t == TIMESTEP::D64)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_64TH);
      else if (o->t == TIMESTEP::D128)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_128TH);
      else if (o->t == TIMESTEP::D256)
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_256TH);
      else
            ((Chord*)chord)->setDurationType(TDuration::DurationType::V_EIGHTH);
      }

//---------------------------------------------------------
//   processBasicDrawObj
//---------------------------------------------------------

static void processBasicDrawObj(QList<BasicDrawObj*> objects, Segment* s, int track, ChordRest* cr)
      {
      Score* score = s->score();
      foreach(BasicDrawObj* oo, objects) {
            switch (oo->type) {
                  case CapellaType::SIMPLE_TEXT:
                        {
                        SimpleTextObj* st = static_cast<SimpleTextObj*>(oo);
                        if (st->font().family() == "capella3") {
                              QString text(st->text());
                              if (text.size() == 1) {
                                    QChar c(text[0]);
                                    ushort code = c.unicode();
                                    switch (code) {
                                          case 'p':
                                                addDynamic(score, s, track, "p");
                                                break;
                                          case 'q':
                                                addDynamic(score, s, track, "pp");
                                                break;
                                          case 'r':
                                                addDynamic(score, s, track, "ppp");
                                                break;
                                          case 's':
                                                addDynamic(score, s, track, "sf");
                                                break;
                                          case 'f':
                                                addDynamic(score, s, track, "f");
                                                break;
                                          case 'g':
                                                addDynamic(score, s, track, "ff");
                                                break;
                                          case 'h':
                                                addDynamic(score, s, track, "fff");
                                                break;
                                          case 'i':
                                                addDynamic(score, s, track, "mp");
                                                break;
                                          case 'j':
                                                addDynamic(score, s, track, "mf");
                                                break;
                                          case 'z':   // sfz
                                                addDynamic(score, s, track, "sfz");
                                                break;
                                          case '{':
                                                addDynamic(score, s, track, "fz");
                                                break;
                                          case '|':
                                                addDynamic(score, s, track, "fp");
                                                break;
                                          case 212:   // dynamic m
                                                addDynamic(score, s, track, "m");
                                                break;
                                          case 213:   // dynamic r
                                                addDynamic(score, s, track, "r");
                                                break;
                                          case 214:   // dynamic s
                                                addDynamic(score, s, track, "s");
                                                break;
                                          case 215:   // dynamic z
                                                addDynamic(score, s, track, "z");
                                                break;
                                          case 'k':   // fermata down
                                                addArticulationText(score, cr, track, QString("dfermata"));
                                                break;
                                          case 'u':   // fermata up
                                                addArticulationText(score, cr, track, QString("ufermata"));
                                                break;
                                          case 'd':   // da capo D.C.
                                          case 'e':   // dal segno D.S.
                                          case 'n':   // segno coda
                                          case 'o':   // segno coda (smaller)
                                          case 'y':   // segno
                                          case '$':   // segno variation
                                          case 'a':   // pedal Ped.
                                          case 'b':   // pedal asterisk *
                                          case 'v':   // 8va
                                          case 186:   // 15ma
                                                qDebug("Import of Capella text articulation %x(%c) not yet implemented", code, code);
                                                break;
                                          case 181:   // caesura
                                                {
                                                Breath* b = new Breath(score);
                                                b->setTrack(track);
                                                b->setBreathType(3);
                                                Segment* seg = s->measure()->getSegment(Segment::Type::Breath, s->tick() + (cr ? cr->actualTicks() : 0));
                                                seg->add(b);
                                                }
                                                break;
                                          default:
                                                break;
                                          }
                                    if (cr->type() == Element::Type::CHORD)
                                          switch (code) {
                                                case 't':   //  trill
                                                      addArticulationText(score, cr, track, QString("trill"));
                                                      break;
                                                case 'l':   // (upper) prall
                                                      addArticulationText(score, cr, track, QString("prall"));
                                                      break;
                                                case 'w':   // turn
                                                      addArticulationText(score, cr, track, QString("turn"));
                                                      break;
                                                case 'x':   // (lower) mordent
                                                      addArticulationText(score, cr, track, QString("mordent"));
                                                      break;
                                                case 'Y':   // down bow
                                                      addArticulationText(score, cr, track, QString("downbow"));
                                                      break;
                                                case 'Z':   // up bow
                                                      addArticulationText(score, cr, track, QString("upbow"));
                                                      break;
                                                case 182:   // plus sign
                                                      addArticulationText(score, cr, track, QString("plusstop"));
                                                      break;
                                                case 183:   // ouvert sign
                                                      addArticulationText(score, cr, track, QString("ouvert"));
                                                      break;
                                                case 184:   // snap pizzicato
                                                      addArticulationText(score, cr, track, QString("snappizzicato"));
                                                      break;
                                                case 189:   // schleifer
                                                      addArticulationText(score, cr, track, QString("schleifer"));
                                                      break;
                                                case 190:   // line prall
                                                      addArticulationText(score, cr, track, QString("lineprall"));
                                                      break;
                                                case 191:   // prall prall
                                                      addArticulationText(score, cr, track, QString("prallprall"));
                                                      break;
                                                case 192:   // down prall
                                                      addArticulationText(score, cr, track, QString("downprall"));
                                                      break;
                                                case 193:   // up prall
                                                      addArticulationText(score, cr, track, QString("upprall"));
                                                      break;
                                                case 194:   // prall mordent ?
                                                      addArticulationText(score, cr, track, QString("prallmordent"));
                                                      break;
                                                case 209:   // reverse turn
                                                case 211:   // alt. reverse turn
                                                      addArticulationText(score, cr, track, QString("reverseturn"));
                                                      break;
                                                case 172:   // arpeggio (short)
                                                case 173:   // arpeggio (long)
                                                      {
                                                      Arpeggio* a = new Arpeggio(score);
                                                      a->setArpeggioType(ArpeggioType::NORMAL);
                                                      if ((static_cast<Chord*>(cr))->arpeggio()) { // there can be only one
                                                            delete a;
                                                            a = 0;
                                                            }
                                                      else
                                                            cr->add(a);
                                                      }
                                                      break;
                                                case 187:   // arpeggio (wiggle line, arrow up)
                                                      {
                                                      Arpeggio* a = new Arpeggio(score);
                                                      a->setArpeggioType(ArpeggioType::UP);
                                                      if ((static_cast<Chord*>(cr))->arpeggio()) { // there can be only one
                                                            delete a;
                                                            a = 0;
                                                            }
                                                      else
                                                            cr->add(a);
                                                      }
                                                      break;
                                                case 188:   // arpeggio (wiggle line, arrow down)
                                                      {
                                                      Arpeggio* a = new Arpeggio(score);
                                                      a->setArpeggioType(ArpeggioType::DOWN);
                                                      if ((static_cast<Chord*>(cr))->arpeggio()) { // there can be only one
                                                            delete a;
                                                            a = 0;
                                                            }
                                                      else
                                                            cr->add(a);
                                                      }
                                                      break;
                                                default:
                                                      break;
                                                }
                                    break;
                                    }
                              }
                        Text* text = new StaffText(score);
                        QFont f(st->font());
                        text->textStyle().setFamily(f.family());
                        text->textStyle().setItalic(f.italic());
                        // text->setUnderline(f.underline());
                        text->textStyle().setBold(f.bold());
                        text->textStyle().setSize(f.pointSizeF());

                        text->setPlainText(st->text());
                        QPointF p(st->pos());
                        p = p / 32.0 * score->spatium();
                        // text->setUserOff(st->pos());
                        text->setUserOff(p);
                        // qDebug("setText %s (%f %f)(%f %f) <%s>",
                        //            qPrintable(st->font().family()),
                        //            st->pos().x(), st->pos().y(), p.x(), p.y(), qPrintable(st->text()));
                        Align textalign = AlignmentFlags::LEFT;
                        switch (st->textalign()) {
                              case 0:   textalign = AlignmentFlags::LEFT;    break;
                              case 1:   textalign = AlignmentFlags::HCENTER; break;
                              case 2:   textalign = AlignmentFlags::RIGHT;   break;
                              default:                                       break;
                              }
                        text->textStyle().setAlign(textalign | AlignmentFlags::BASELINE);
                        text->textStyle().setYoff(2.0);
                        text->setTrack(track);
                        s->add(text);
                        }
                        break;
                  case CapellaType::TEXT:
                        qDebug("======================Text:");
                        break;
                  default:
                        break;
                  }
            }
      }

//---------------------------------------------------------
//   TupletFractionCap
//---------------------------------------------------------

Fraction TupletFractionCap(int tupletCount, bool tuplettrp, bool tupletprol)
      {
      int dd = 0;
      int nn = 0;
      qreal exponent = 0;
      qreal count = tupletCount;
      Fraction f(3,2);
      if ((count > 0) && (count <= 15)) {
            if (tuplettrp)
                  exponent = qFloor(qLn(count/3.0)/qLn(2.0));
            else
                  exponent = qFloor(qLn(count)/qLn(2.0));
            }
      else {
            qDebug("Unknown tuplet, count = %d",tupletCount);
            return f;
            }
      if (tupletprol)
            exponent += 1.0;
      if (exponent < 0.0)
            exponent = 0.0;
      nn = tupletCount;
      dd =  static_cast<int>(qPow(2.0, exponent));
      if (tuplettrp)
            dd = dd * 3;
      qDebug("Tuplet Fraction: %d / %d",nn,dd);
      f.setNumerator(nn);
      f.setDenominator(dd);
      return f;
      }

//---------------------------------------------------------
//   findChordRests -- find begin and end ChordRest for BasicDrawObj o
//   return true on success (both begin and end found)
//---------------------------------------------------------

static bool findChordRests(BasicDrawObj const* const o, Score* score, const int track, const int tick,
                           ChordRest*& cr1, ChordRest*& cr2, NoteObj* no, QList<NoteObj*> objects)
      {
      cr1 = 0;                         // ChordRest where BasicDrawObj o begins
      cr2 = 0;                         // ChordRest where BasicDrawObj o ends

      // find the ChordRests where o begins and ends
      int n = o->nNotes + 1;                                // # notes in BasicDrawObj (nNotes is # notes following the first note)
      int graceNumber = 0;
      int graceNumber1 = 0;
      bool foundcr1 = false;
      int tick2 = tick;
      foreach(NoteObj* nobj, objects) {
            BasicDurationalObj* d = 0;
            if (nobj->type() == CapellaNoteObjectType::REST) {
                  d = static_cast<BasicDurationalObj*>(static_cast<RestObj*>(nobj));
                  graceNumber = 0;
                  }
            else if (nobj->type() == CapellaNoteObjectType::CHORD) {
                  ChordObj* cho = static_cast<ChordObj*>(nobj);
                  d = static_cast<BasicDurationalObj*>(cho);
                  if (!(cho->invisible) && (cho->ticks() == 0)) // grace note
                        ++graceNumber;
                  else
                        graceNumber = 0;
                  }
            if (!d)
                  continue;
            if (nobj == no) {
                  foundcr1 = true;
                  graceNumber1 = graceNumber;
                  }
            int ticks = 0;
            if (foundcr1) {
                  --n;   // found the object corresponding to cr1, count down to find the second one
                  ticks = d->ticks();
                  if (d->count) {
                        Fraction f = TupletFractionCap(d->count, d->tripartite, d->isProlonging);
                        ticks = ticks*f.denominator()/f.numerator();
                        }
                  if (nobj->type() == CapellaNoteObjectType::REST) {
                        RestObj* ro = static_cast<RestObj*>(nobj);
                        if (ro->fullMeasures) {
                              Measure* m = score->getCreateMeasure(tick2);
                              int ft     = m->ticks();
                              ticks = ft * ro->fullMeasures;
                              }
                        }
                  if (n == 0)
                        break;
                  tick2 += ticks;
                  }
            }
      // Now we have the tick (tick) and the level of grace note (graceNumber1, if "no" is a grace note) for the first ChordRest
      // and the tick (tick2) and the level of grace note (graceNumber, if the target is a grace note) for the 2nd ChordRest
      for (Segment* seg = score->tick2segment(tick); seg; seg = seg->next1()) {
            if (seg->segmentType() != Segment::Type::ChordRest)
                  continue;
            ChordRest* cr = static_cast<ChordRest*>(seg->element(track));
            if (cr) {
                  if (graceNumber1 > 0) { // the spanner is starting from a grace note
                        Chord* chord = static_cast<Chord*>(cr);
                        foreach(Chord* cc, chord->graceNotes()) {
                              --graceNumber1;
                              if ((graceNumber1 == 0) && (!cr1))
                                    cr1 = static_cast<ChordRest*>(cc); // found first ChordRest
                              }
                        }
                  if (!cr1) cr1 = cr; // found first ChordRest
                  break;
                  }
            }
      for (Segment* seg = score->tick2segment(tick2); seg; seg = seg->next1()) {
            if (seg->segmentType() != Segment::Type::ChordRest)
                  continue;
            ChordRest* cr = static_cast<ChordRest*>(seg->element(track));
            if (cr) {
                  if ((graceNumber > 0) && (cr->type() == Element::Type::CHORD)) { // the spanner is ending on a grace note
                        Chord* chord = static_cast<Chord*>(cr);
                        foreach(Chord* cc, chord->graceNotes()) {
                              --graceNumber;
                              if ((graceNumber == 0) && (!cr2))
                                    cr2 = static_cast<ChordRest*>(cc); // found 2nd ChordRest
                              }
                        }
                  if (!cr2) cr2 = cr; // found 2nd ChordRest
                  break;
                  }
            }
      qDebug("findChordRests o %p nNotes %d score %p track %d tick %d cr1 %p cr2 %p", o, o->nNotes, score, track, tick, cr1, cr2);

      if (!(cr1 && cr2)) {
            qDebug("first or second anchor for BasicDrawObj not found (tick %d type %d track %d first %p second %p)",
                   tick, int(o->type), track, cr1, cr2);
            return false;
            }
      return true;
      }

//---------------------------------------------------------
//   readCapVoice
//---------------------------------------------------------

static int readCapVoice(Score* score, CapVoice* cvoice, int staffIdx, int tick, bool capxMode)
      {
      int voice = cvoice->voiceNo;
      int track = staffIdx * VOICES + voice;

      qDebug("readCapVoice 1");
      //
      // pass I
      //
      int startTick = tick;

      Tuplet* tuplet = 0;
      int tupletCount = 0;
      bool tuplettrp = false;
      bool tupletprol = false;
      int nTuplet = 0;
      int tupletTick = 0;

      qDebug("    read voice: tick %d track: %d)", tick, track);
      QList<Chord*> graceNotes;
      foreach(NoteObj* no, cvoice->objects) {
            switch (no->type()) {
                  case CapellaNoteObjectType::REST:
                        {
                        qDebug("     <Rest>");
                        Measure* m = score->getCreateMeasure(tick);
                        RestObj* o = static_cast<RestObj*>(no);
                        int ticks  = o->ticks();
                        if (o->invisible && ticks == 0) {  // get rid of placeholders
                              break;
                              }
                        TDuration d;
                        d.setVal(ticks);
                        if (o->count) {
                              if (tuplet == 0) {
                                    tupletCount = o->count;
                                    tuplettrp   = o->tripartite;
                                    tupletprol  = o->isProlonging;
                                    nTuplet     = 0;
                                    tupletTick  = tick;
                                    tuplet      = new Tuplet(score);
                                    Fraction f  = TupletFractionCap(tupletCount,tuplettrp,tupletprol);
                                    tuplet->setRatio(f);
                                    tuplet->setBaseLen(d);
                                    tuplet->setTrack(track);
                                    tuplet->setTick(tick);
                                    tuplet->setParent(m);
                                    int nn = ((tupletCount * ticks) * f.denominator()) / f.numerator();
                                    tuplet->setDuration(Fraction::fromTicks(nn));
                                    }
                              }

                        int ft     = m->ticks();
                        if (o->fullMeasures) {
                              ticks = ft * o->fullMeasures;
                              if (!o->invisible) {
                                    for (unsigned i = 0; i < o->fullMeasures; ++i) {
                                          Measure* m = score->getCreateMeasure(tick + i * ft);
                                          Segment* s = m->getSegment(Segment::Type::ChordRest, tick + i * ft);
                                          Rest* rest = new Rest(score);
                                          rest->setDurationType(TDuration(TDuration::DurationType::V_MEASURE));
                                          rest->setDuration(m->len());
                                          rest->setTrack(staffIdx * VOICES + voice);
                                          s->add(rest);
                                          }
                                    }
                              }
                        if (!o->invisible || voice == 0) {
                              Segment* s = m->getSegment(Segment::Type::ChordRest, tick);
                              Rest* rest = new Rest(score);
                              if (tuplet) {
                                    rest->setTuplet(tuplet);
                                    tuplet->add(rest);
                                    }
                              TDuration d;
                              if (o->fullMeasures) {
                                    d.setType(TDuration::DurationType::V_MEASURE);
                                    rest->setDuration(m->len());
                                    }
                              else {
                                    d.setVal(ticks);
                                    rest->setDuration(d.fraction());
                                    }
                              rest->setDurationType(d);
                              rest->setTrack(track);
                              rest->setVisible(!o->invisible);
                              s->add(rest);
                              processBasicDrawObj(o->objects, s, track, rest);
                              }

                        if (tuplet) {
                              if (++nTuplet >= tupletCount) {
                                    tick = tupletTick + tuplet->actualTicks();
                                    tuplet = 0;
                                    }
                              else {
                                    tick += (ticks * tuplet->ratio().denominator()) / tuplet->ratio().numerator();
                                    }
                              }
                        else
                              tick += ticks;
                        }
                        break;
                  case CapellaNoteObjectType::CHORD:
                        {
                        qDebug("     <Chord>");
                        ChordObj* o = static_cast<ChordObj*>(no);
                        int ticks = o->ticks();
                        if (o->invisible && ticks == 0) {  // get rid of placeholders
                              break;
                              }
                        TDuration d;
                        d.setVal(ticks);
                        Measure* m = score->getCreateMeasure(tick);

                        bool isgracenote = (!(o->invisible) && (ticks==0));
                        if (o->count) {
                              if (tuplet == 0) {
                                    tupletCount = o->count;
                                    tuplettrp   = o->tripartite;
                                    tupletprol  = o->isProlonging;
                                    nTuplet     = 0;
                                    tupletTick  = tick;
                                    tuplet      = new Tuplet(score);
                                    Fraction f  = TupletFractionCap(tupletCount,tuplettrp,tupletprol);
                                    tuplet->setRatio(f);
                                    tuplet->setBaseLen(d);
                                    tuplet->setTrack(track);
                                    tuplet->setTick(tick);
                                    tuplet->setParent(m);
                                    int nn = ((tupletCount * ticks) * f.denominator()) / f.numerator();
                                    tuplet->setDuration(Fraction::fromTicks(nn));
                                    }
                              qDebug("Tuplet at %d: count: %d  tri: %d  prolonging: %d  ticks %d objects %d",
                                     tick, o->count, o->tripartite, o->isProlonging, ticks,
                                     o->objects.size());
                              }

                        Chord* chord = new Chord(score);
                        if (tuplet) {
                              chord->setTuplet(tuplet);
                              tuplet->add(chord);
                              }
                        if (isgracenote) { // grace notes
                              SetCapGraceDuration(chord,o);
                              chord->setDuration(chord->durationType().fraction());
                              }
                        else { // normal notes
                              chord->setDurationType(d);
                              chord->setDuration(d.fraction());
                              }
                        chord->setTrack(track);
                        switch (o->stemDir) {
                              case ChordObj::StemDir::DOWN:
                                    chord->setStemDirection(MScore::Direction::DOWN);
                                    break;
                              case ChordObj::StemDir::UP:
                                    chord->setStemDirection(MScore::Direction::UP);
                                    break;
                              case ChordObj::StemDir::NONE:
                                    chord->setNoStem(true);
                                    break;
                              case ChordObj::StemDir::AUTO:
                              default:
                                    break;
                              }
                        Segment* s = m->getSegment(Segment::Type::ChordRest, tick);
                        if (isgracenote)
                              graceNotes.push_back(chord);
                        else {
                              s->add(chord);
                              // append grace notes before
                              int ii = -1;
                              for (ii = graceNotes.size() - 1; ii >= 0; ii--) {
                                    Chord* gc = graceNotes[ii];
                                    if(gc->voice() == chord->voice()){
                                          chord->add(gc);
                                          }
                                    }
                              graceNotes.clear();
                              }
                        ClefType clef = score->staff(staffIdx)->clef(tick);
                        Key key  = score->staff(staffIdx)->key(tick);
                        int off;
                        switch (clef) {
                              case ClefType::G:      off = 0; break;
                              case ClefType::G1:     off = 7; break;
                              case ClefType::G2:     off = 14; break;
                              case ClefType::G3:     off = -7; break;
                              case ClefType::F:      off = -14; break;
                              case ClefType::F8:     off = -21; break;
                              case ClefType::F15:    off = -28; break;
                              case ClefType::F_B:    off = -14; break;
                              case ClefType::F_C:    off = -14; break;
                              case ClefType::C1:     off = -7; break;
                              case ClefType::C2:     off = -7; break;
                              case ClefType::C3:     off = -7; break;
                              case ClefType::C4:     off = -7; break;
                              case ClefType::C5:     off = -7; break;
                              case ClefType::G4:     off = 0; break;
                              case ClefType::F_8VA:  off = -7; break;
                              case ClefType::F_15MA: off = 0; break;
                              default:          off = 0; qDebug("clefType %d not implemented", int(clef));
                              }
                        // qDebug("clef %hhd off %d", clef, off);

                        static int keyOffsets[15] = {
                              /*   -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7 */
                              /* */ 7, 4, 1, 5, 2, 6, 3, 0, 4, 1, 5, 2, 6, 3, 0
                              };
                        off += keyOffsets[int(key) + 7];

                        foreach(CNote n, o->notes) {
                              Note* note = new Note(score);
                              int pitch = 0;
                              // .cap import: pitch contains the diatonic note number relative to clef and key
                              // .capx  import: pitch the MIDI note number instead
                              if (capxMode) {
                                    pitch = n.pitch;
                                    }
                              else {
                                    int l = n.pitch + off + 7 * 6;
                                    int octave = 0;
                                    while (l < 0) {
                                          l += 7;
                                          octave--;
                                          }
                                    octave += l / 7;
                                    l       = l % 7;

                                    pitch = pitchKeyAdjust(l, key) + octave * 12;
                                    }
                              pitch += n.alteration;
                              pitch += score->staff(staffIdx)->part()->instrument()->transpose().chromatic; // assume not in concert pitch
                              pitch = limit(pitch, 0, 127);

                              chord->add(note);
                              note->setPitch(pitch);
                              // TODO: compute tpc from pitch & line
                              note->setTpcFromPitch();
                              if (o->rightTie) {
                                    Tie* tie = new Tie(score);
                                    tie->setStartNote(note);
                                    tie->setTrack(track);
                                    note->setTieFor(tie);
                                    }
                              }
                        foreach(Verse v, o->verse) {
                              Lyrics* l = new Lyrics(score);
                              l->setTrack(track);
                              l->setPlainText(v.text);
                              if (v.hyphen)
                                    l->setSyllabic(Lyrics::Syllabic::BEGIN);
                              l->setNo(v.num);
                              chord->add(l);
                              }

                        processBasicDrawObj(o->objects, s, track, chord);
                        switch (o->articulation) {
                              case 1:   addArticulationText(score, chord, track, QString("staccato")); break;
                              case 2:   addArticulationText(score, chord, track, QString("tenuto")); break;
                              case 3:   addArticulationText(score, chord, track, QString("portato")); break;
                              case 4:   addArticulationText(score, chord, track, QString("staccatissimo")); break;
                              case 5:   addArticulationText(score, chord, track, QString("sforzato")); break;
                              case 6:   addArticulationText(score, chord, track, QString("marcato")); break;
                              case 7:   // "weak beat"
                              case 8:   // "strong beat"
                              default:  if(o->articulation) qDebug("Articulation # %d not implemented", o->articulation); break;
                              }

                        if (tuplet) {
                              if (++nTuplet >= tupletCount) {
                                    tick = tupletTick + tuplet->actualTicks();
                                    tuplet = 0;
                                    }
                              else {
                                    tick += (ticks * tuplet->ratio().denominator()) / tuplet->ratio().numerator();
                                    }
                              }
                        else
                              tick += ticks;
                        }
                        break;
                  case CapellaNoteObjectType::CLEF:
                        {
                        qDebug("     <Clef>");
                        CapClef* o = static_cast<CapClef*>(no);
                        ClefType nclef = o->clef();
                        qDebug("%d:%d <Clef> %s line %d oct %d clef %d", tick, staffIdx, o->name(), int(o->line), int(o->oct), int(o->clef()));
                        if (nclef == ClefType::INVALID)
                              break;
                        // staff(staffIdx)->setClef(tick, nclef);
                        Clef* clef = new Clef(score);
                        clef->setClefType(nclef);
                        clef->setTrack(staffIdx * VOICES);
                        Measure* m = score->getCreateMeasure(tick);
                        Segment* s = m->getSegment(Segment::Type::Clef, tick);
                        s->add(clef);
                        }
                        break;
                  case CapellaNoteObjectType::KEY:
                        {
                        qDebug("   <Key>");
                        CapKey* o = static_cast<CapKey*>(no);
                        KeySigEvent key = score->staff(staffIdx)->keySigEvent(tick);
                        KeySigEvent okey;
                        okey.setKey(Key(o->signature));
                        if (!(key == okey)) {
                              score->staff(staffIdx)->setKey(tick, okey);
                              KeySig* ks = new KeySig(score);
                              ks->setTrack(staffIdx * VOICES);
                              Measure* m = score->getCreateMeasure(tick);
                              Segment* s = m->getSegment(Segment::Type::KeySig, tick);
                              ks->setKeySigEvent(okey);
                              s->add(ks);
                              }
                        }
                        break;
                  case CapellaNoteObjectType::METER:
                        {
                        CapMeter* o = static_cast<CapMeter*>(no);
                        qDebug("     <Meter> tick %d %d/%d", tick, o->numerator, 1 << o->log2Denom);
                        if (o->log2Denom > 7 || o->log2Denom < 0)
                              qFatal("illegal fraction");
                        SigEvent se = score->sigmap()->timesig(tick);
                        Fraction f(o->numerator, 1 << o->log2Denom);
                        SigEvent ne(f);
                        if (!(se == ne))
                              score->sigmap()->add(tick, ne);
                        TimeSig* ts = new TimeSig(score);
                        ts->setSig(f);
                        ts->setTrack(track);
                        Measure* m = score->getCreateMeasure(tick);
                        Segment* s = m->getSegment(Segment::Type::TimeSig, tick);
                        s->add(ts);
                        m->setLen(f);
                        }
                        break;
                  case CapellaNoteObjectType::EXPL_BARLINE:
                  case CapellaNoteObjectType::IMPL_BARLINE:    // does not exist?
                        {
                        CapExplicitBarline* o = static_cast<CapExplicitBarline*>(no);
                        qDebug("     <Barline>");
                        Measure* pm = 0; // the previous measure (the one terminated by this barline)
                        if (tick > 0)
                              pm = score->getCreateMeasure(tick-1);
                        if (pm) {
                              int ticks = tick - pm->tick();
                              if (ticks > 0 && ticks != pm->ticks()) {
                                    // this is a measure with different actual duration
                                    Fraction f = Fraction::fromTicks(ticks);
                                    pm->setLen(f);
#if 0
                                    AL::SigEvent ne(f);
                                    ne.setNominal(m->timesig());
                                    score->sigmap()->add(m->tick(), ne);
                                    AL::SigEvent ne2(m->timesig());
                                    score->sigmap()->add(m->tick() + m->ticks(), ne2);
#endif
                                    }
                              }
                        // qDebug("pm %p", pm);

                        BarLineType st = o->type();
                        if (st == BarLineType::NORMAL)
                              break;

                        if (pm && (st == BarLineType::DOUBLE || st == BarLineType::END || st == BarLineType::BROKEN))
                              pm->setEndBarLineType(st, false, true);

                        if (st == BarLineType::START_REPEAT || st == BarLineType::END_START_REPEAT) {
                              Measure* nm = 0; // the next measure (the one started by this barline)
                              nm = score->getCreateMeasure(tick);
                              // qDebug("nm %p", nm);
                              if (nm)
                                    nm->setRepeatFlags(nm->repeatFlags() | Repeat::START);
                              }

                        if (st == BarLineType::END_REPEAT || st == BarLineType::END_START_REPEAT) {
                              if (pm)
                                    pm->setRepeatFlags(pm->repeatFlags() | Repeat::END);
                              }
                        }
                        break;
                  case CapellaNoteObjectType::PAGE_BKGR:
                        qDebug("     <PageBreak>");
                        break;
                  }
            }
      int endTick = tick;

      qDebug("readCapVoice 2");
      //
      // pass II
      //
      tick = startTick;
      foreach(NoteObj* no, cvoice->objects) {
            BasicDurationalObj* d = 0;
            if (no->type() == CapellaNoteObjectType::REST)
                  d = static_cast<BasicDurationalObj*>(static_cast<RestObj*>(no));
            else if (no->type() == CapellaNoteObjectType::CHORD)
                  d = static_cast<BasicDurationalObj*>(static_cast<ChordObj*>(no));
            if (!d)
                  continue;
            foreach(BasicDrawObj* o, d->objects) {
                  switch (o->type) {
                        case CapellaType::SIMPLE_TEXT:
                              // qDebug("simple text at %d", tick);
                              break;
                        case CapellaType::WAVY_LINE:
                              break;
                        case CapellaType::SLUR:
                              {
                              // SlurObj* so = static_cast<SlurObj*>(o);
                              // qDebug("slur tick %d  %d-%d-%d-%d   %d-%d", tick, so->nEnd, so->nMid,
                              //        so->nDotDist, so->nDotWidth, so->nRefNote, so->nNotes);
                              ChordRest* cr1 = 0; // ChordRest where slur begins
                              ChordRest* cr2 = 0; // ChordRest where slur ends
                              bool res = findChordRests(o, score, track, tick, cr1, cr2, no, cvoice->objects);

                              if (res) {
                                    if (cr1 == cr2)
                                          qDebug("first and second anchor for slur identical (tick %d track %d first %p second %p)",
                                                 tick, track, cr1, cr2);
                                    else {
                                          Slur* slur = new Slur(score);
                                          qDebug("tick %d track %d cr1 %p cr2 %p -> slur %p", tick, track, cr1, cr2, slur);
                                          slur->setTick(cr1->tick());
                                          slur->setTick2(cr2->tick());
                                          slur->setStartElement(cr1);
                                          slur->setEndElement(cr2);
                                          slur->setTrack(cr1->track());
                                          slur->setTrack2(cr2->track());
                                          score->addElement(slur);
                                          }
                                    }
                              }
                              break;
                        case CapellaType::TEXT: {

                              TextObj* to = static_cast<TextObj*>(o);
                              Text* s = new Text(score);
                              QString ss = ::rtf2html(QString(to->text));

                              // qDebug("string %f:%f w %d ratio %d <%s>",
                              //    to->relPos.x(), to->relPos.y(), to->width, to->yxRatio, qPrintable(ss));
                              s->setXmlText(ss);
                              MeasureBase* measure = score->measures()->first();
                              if (measure->type() != Element::Type::VBOX) {
                                    MeasureBase* mb = new VBox(score);
                                    mb->setTick(0);
                                    score->addMeasure(mb, measure);
                                    measure = mb;
                                    }
                              s->setParent(measure);
                              s->setTextStyleType(TextStyleType::TITLE);
                              measure->add(s);
                              }
                              break;
                        case CapellaType::VOLTA:
                              {
                              VoltaObj* vo = static_cast<VoltaObj*>(o);
                              ChordRest* cr1 = 0; // ChordRest where volta begins
                              ChordRest* cr2 = 0; // ChordRest where volta ends
                              bool res = findChordRests(o, score, track, tick, cr1, cr2, no, cvoice->objects);

                              if (res) {
                                    Volta* volta = new Volta(score);
                                    volta->setTrack(track);
                                    volta->setTrack2(track);
                                    // TODO also support endings such as "1 - 3"
                                    volta->setText(QString("%1.").arg(vo->to));
                                    volta->endings().append(vo->to);
                                    if (vo->bRight)
                                          volta->setVoltaType(Volta::Type::CLOSED);
                                    else
                                          volta->setVoltaType(Volta::Type::OPEN);
                                    volta->setTick(cr1->measure()->tick());
                                    volta->setTick2(cr2->measure()->tick() + cr2->measure()->ticks());
                                    score->addElement(volta);
                                    }
                              }
                              break;
                        case CapellaType::TRILL:
                              {
                              TrillObj* tro = static_cast<TrillObj*>(o);
                              ChordRest* cr1 = 0; // ChordRest where trill line begins
                              ChordRest* cr2 = 0; // ChordRest where trill line ends
                              bool res = findChordRests(o, score, track, tick, cr1, cr2, no, cvoice->objects);
                              if (res) {
                                    if (cr1 == cr2)
                                          qDebug("first and second anchor for trill line identical (tick %d track %d first %p second %p)",
                                                 tick, track, cr1, cr2);
                                    else {
                                          Trill* trill = new Trill(score);
                                          trill->setTrack(track);
                                          trill->setTrack2(track);
                                          trill->setTick(cr1->tick());
                                          trill->setTick2(cr2->tick());
                                          if (!(tro->trillSign))
                                                trill->setTrillType("prallprall");
                                          score->addElement(trill);
                                          }
                                    }
                              }
                              break;
                        case CapellaType::WEDGE:
                              {
                              WedgeObj* wdgo = static_cast<WedgeObj*>(o);
                              ChordRest* cr1 = 0; // ChordRest where hairpin begins
                              ChordRest* cr2 = 0; // ChordRest where hairpin ends
                              bool res = findChordRests(o, score, track, tick, cr1, cr2, no, cvoice->objects);
                              if (res) {
                                    if (cr1 == cr2)
                                          qDebug("first and second anchor for hairpin identical (tick %d track %d first %p second %p)",
                                                 tick, track, cr1, cr2);
                                    else {
                                          Hairpin* hp = new Hairpin(score);
                                          if (wdgo->decresc)
                                                hp->setHairpinType(Hairpin::Type::DECRESCENDO);
                                          else
                                                hp->setHairpinType(Hairpin::Type::CRESCENDO);
                                          hp->setTick(cr1->tick());
                                          hp->setTick2(cr2->tick());
                                          hp->setTrack(track);
                                          hp->setTrack2(track);
                                          hp->setAnchor(Spanner::Anchor::SEGMENT);
                                          score->addSpanner(hp);
                                          score->updateHairpin(hp);
                                          }
                                    }
                              }
                              break;
                        default:
                              break;
                        }
                  }
            int ticks = d->ticks();
            if (d->count) {
                  Fraction f = TupletFractionCap(d->count, d->tripartite, d->isProlonging);
                  ticks = ticks*f.denominator()/f.numerator();
                  }
            if (no->type() == CapellaNoteObjectType::REST) {
                  RestObj* o = static_cast<RestObj*>(no);
                  if (o->fullMeasures) {
                        Measure* m = score->getCreateMeasure(tick);
                        int ft     = m->ticks();
                        ticks = ft * o->fullMeasures;
                        }
                  }
            tick += ticks;
            }
      qDebug("   readCapVoice");
      return endTick;
      }

//---------------------------------------------------------
//   needPart -- determine if a staff needs its own part
//---------------------------------------------------------

// As Capella does not define parts (it only knows about staves,
// MIDI instruments numbers, brackets and braces), the following
// algorithm is used:
// - every staff is a separate part
// - unless:
//   - it is in a brace
//   - it is not the first staff in the brace
//   - it has the same MIDI instrument as the previous staff
// Common cases:
// - Keyboards: two or three staves with the same MIDI instrument and a brace
//   -> create one part
// - SATB: two or four staves with the same MIDI instrument and a bracket
//   -> create two or four parts

static bool needPart(const int prevInst, const int currInst, const int staffIdx, QList<CapBracket> const& bracketList)
      {
      // qDebug("needPart(prevInst %d, currInst %d, staffIdx %d)", prevInst, currInst, staffIdx);
      foreach(CapBracket cb, bracketList) {
            // qDebug("needPart bracket %d-%d curly %d", cb.from, cb.to, cb.curly);
            if (prevInst == currInst && cb.from < staffIdx && staffIdx <= cb.to && cb.curly) {
                  // qDebug("needPart found brace, continue part");
                  return false;
                  }
            }
      return true;
      }

//---------------------------------------------------------
//   convertCapella
//---------------------------------------------------------

void convertCapella(Score* score, Capella* cap, bool capxMode)
      {
      if (cap->systems.isEmpty())
            return;

      score->style()->set(StyleIdx::measureSpacing, 1.0);
      score->setSpatium(cap->normalLineDist * DPMM);
      score->style()->set(StyleIdx::smallStaffMag, cap->smallLineDist / cap->normalLineDist);
      score->style()->set(StyleIdx::minSystemDistance, Spatium(8));
      score->style()->set(StyleIdx::maxSystemDistance, Spatium(12));
      // score->style()->set(StyleIdx::hideEmptyStaves, true);

#if 1
      foreach(CapSystem* csys, cap->systems) {
            qDebug("System:");
            foreach(CapStaff* cstaff, csys->staves) {
                  CapStaffLayout* cl = cap->staffLayout(cstaff->iLayout);
                  qDebug("  Staff layout <%s><%s><%s><%s><%s> %d  barline %d-%d mode %d",
                         qPrintable(cl->descr), qPrintable(cl->name), qPrintable(cl->abbrev),
                         qPrintable(cl->intermediateName), qPrintable(cl->intermediateAbbrev),
                         cstaff->iLayout, cl->barlineFrom, cl->barlineTo, cl->barlineMode);
                  }
            }
#endif

      //
      // find out the maximum number of staves
      //
      int staves = 0;
      foreach(CapSystem* csys, cap->systems) {
            staves = qMax(staves, csys->staves.size());
            }
      //
      // check the assumption that every stave should be
      // associated with a CapStaffLayout
      //
      if (staves != cap->staffLayouts().size()) {
            qDebug("Capella: max number of staves != number of staff layouts (%d, %d)",
                   staves, cap->staffLayouts().size());
            staves = qMax(staves, cap->staffLayouts().size());
            }

      // set the initial time signature
      CapStaff* cs = cap->systems[0]->staves[0];
      if (cs->log2Denom <= 7)
            score->sigmap()->add(0, Fraction(cs->numerator, 1 << cs->log2Denom));

      // create parts and staves
      Staff* bstaff = 0;
      int span = 1;
      int midiPatch = -1; // the previous MIDI patch (instrument)
      Part* part = 0;
      for (int staffIdx = 0; staffIdx < staves; ++staffIdx) {
            CapStaffLayout* cl = cap->staffLayout(staffIdx);
            // qDebug("Midi staff %d program %d", staffIdx, cl->sound);

            // create a new part if necessary
            if (needPart(midiPatch, cl->sound, staffIdx, cap->brackets)) {
                  part = new Part(score);
                  score->appendPart(part);
                  }
            midiPatch = cl->sound;

            Staff* s = new Staff(score);
            s->setPart(part);
            if (cl->bPercussion)
                  part->setMidiProgram(0, 128);
            else
                  part->setMidiProgram(cl->sound, 0);
            part->setPartName(cl->descr);
            part->setPlainLongName(cl->name);
            part->setPlainShortName(cl->abbrev);

            // ClefType clefType = CapClef::clefType(cl->form, cl->line, cl->oct);
            // s->setClef(0, clefType);
            s->setBarLineSpan(0);
            if (bstaff == 0) {
                  bstaff = s;
                  span = 0;
                  }
            ++span;
            if (cl->barlineMode == 1) {
                  bstaff->setBarLineSpan(span);
                  bstaff = 0;
                  }
            s->setSmall(cl->bSmall);
            part->insertStaff(s, -1);
            Interval interval;
            // guess diatonic transposition from chromatic transposition for the instrument
            int values[23] = {-6,-6,-5,-5,-4,-3,-3,-2,-2,-1,-1,0,1,1,2,2,3,4,4,5,5,6,6};
            interval.diatonic = values[(cl->transp % 12) + 11] + (cl->transp / 12) * 7;
            interval.chromatic = cl->transp;
            s->part()->instrument()->setTranspose(interval);
            score->staves().push_back(s);
            // _parts.push_back(part);
            }
      if (bstaff)
            bstaff->setBarLineSpan(span);

      foreach(CapBracket cb, cap->brackets) {
            qDebug("Bracket %d-%d curly %d", cb.from, cb.to, cb.curly);
            Staff* staff = score->staves().value(cb.from);
            if (staff == 0) {
                  qDebug("bad bracket 'from' value");
                  continue;
                  }
            staff->setBracket(0, cb.curly ? BracketType::BRACE : BracketType::NORMAL);
            staff->setBracketSpan(0, cb.to - cb.from + 1);
            }
      MeasureBase* measure = nullptr;
      foreach(BasicDrawObj* o, cap->backgroundChord->objects) {
            switch (o->type) {
                  case CapellaType::SIMPLE_TEXT:
                        {
                        SimpleTextObj* to = static_cast<SimpleTextObj*>(o);
                        Text* s = new Text(score);
                        switch (to->textalign()) {
                              case 0:   s->setTextStyleType(TextStyleType::POET);    break;
                              case 1:   s->setTextStyleType(TextStyleType::TITLE); break;
                              case 2:   s->setTextStyleType(TextStyleType::COMPOSER);   break;
                              default:                                       break;
                              }
                        QFont f(to->font());
                        s->textStyle().setItalic(f.italic());
                        // s->setUnderline(f.underline());
                        s->textStyle().setBold(f.bold());
                        s->textStyle().setSize(f.pointSizeF());

                        QString ss = to->text();
                        s->setPlainText(ss);
                        if (!measure) {
                              measure = new VBox(score);
                              measure->setTick(0);
                              score->addMeasure(measure, score->measures()->first());
                              }
                        measure->add(s);
                        // qDebug("page background object type %d (CapellaType::SIMPLE_TEXT) text %s", o->type, qPrintable(ss));
                        }
                        break;
                  default:
                        qDebug("page background object type %d", int(o->type));
                        break;
                  }
            }

      if (cap->topDist) {
            VBox* mb = 0;
            MeasureBaseList* mbl = score->measures();
            if (mbl->size() && mbl->first()->type() == Element::Type::VBOX)
                  mb = static_cast<VBox*>(mbl->first());
            else {
                  VBox* vb = new VBox(score);
                  vb->setTick(0);
                  score->addMeasure(vb, mb);
                  mb = vb;
                  }
            mb->setBoxHeight(Spatium(cap->topDist));
            }

      int systemTick = 0;
      foreach(CapSystem* csys, cap->systems) {
            qDebug("readCapSystem");
            /*
            if (csys->explLeftIndent > 0) {
                  HBox* mb = new HBox(score);
                  mb->setTick(systemTick);
                  mb->setBoxWidth(Spatium(csys->explLeftIndent));
                  score->addMeasure(mb);
                  }
            */
            int mtick = 0;
            foreach(CapStaff* cstaff, csys->staves) {
                  //
                  // assumption: layout index is mscore staffIdx
                  //    which means that there is a 1:1 relation between layout/staff
                  //

                  qDebug("  ReadCapStaff %d/%d", cstaff->numerator, 1 << cstaff->log2Denom);
                  int staffIdx = cstaff->iLayout;
                  int voice = 0;
                  foreach(CapVoice* cvoice, cstaff->voices) {
                        int tick = readCapVoice(score, cvoice, staffIdx, systemTick, capxMode);
                        ++voice;
                        if (tick > mtick)
                              mtick = tick;
                        }
                  }
            Measure* m = score->tick2measure(mtick-1);
            if (m && !m->lineBreak()) {
                  LayoutBreak* lb = new LayoutBreak(score);
                  lb->setLayoutBreakType(LayoutBreak::Type::LINE);
                  lb->setTrack(-1);       // this are system elements
                  m->add(lb);
                  }
            systemTick = mtick;
            }

      //
      // fill empty measures with rests
      //
      Segment::Type st = Segment::Type::ChordRest;
      for (Measure* m = score->firstMeasure(); m; m = m->nextMeasure()) {
            for (int staffIdx = 0; staffIdx < score->staves().size(); ++staffIdx) {
                  bool empty = true;
                  for (Segment* s = m->first(st); s; s = s->next(st)) {
                        if (s->element(staffIdx * VOICES)) {
                              empty = false;
                              break;
                              }
                        }
                  if (empty) {
                        Segment* s = m->getSegment(Segment::Type::ChordRest, m->tick());
                        Rest* rest = new Rest(score);
                        TDuration d(m->len());
                        if ((m->len() == m->timesig()) || !d.isValid())
                              rest->setDurationType(TDuration::DurationType::V_MEASURE);
                        else
                              rest->setDurationType(d.type());
                        rest->setDuration(m->len());
                        rest->setTrack(staffIdx * VOICES);
                        s->add(rest);
                        }
                  }
            }
      // score->connectSlurs();
      score->connectTies();
      score->fixTicks();
      score->setPlaylistDirty();
      score->setLayoutAll(true);
      score->addLayoutFlags(LayoutFlag::FIX_TICKS | LayoutFlag::FIX_PITCH_VELO);
      }

//---------------------------------------------------------
//   Capella
//---------------------------------------------------------

Capella::Capella()
      {
      author   = 0;
      keywords = 0;
      comment  = 0;
      }

Capella::~Capella()
      {
      delete[] author;
      delete[] keywords;
      delete[] comment;
      }

//---------------------------------------------------------
//   SlurObj::read
//---------------------------------------------------------

void SlurObj::read()
      {
      BasicDrawObj::read();
      for (int i = 0; i < 4; ++i) {
            bezierPoint[i].setX(cap->readInt());
            bezierPoint[i].setY(cap->readInt());
            }
      color     = cap->readColor();
      nEnd      = cap->readByte();
      nMid      = cap->readByte();
      nDotDist  = cap->readByte();
      nDotWidth = cap->readByte();
      // qDebug("SlurObj nEnd %d nMid %d nDotDist %d nDotWidth %d",
      //        nEnd, nMid, nDotDist, nDotWidth);
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void TextObj::read()
      {
      BasicRectObj::read();
      unsigned size = cap->readUnsigned();
      char txt[size+1];
      cap->read(txt, size);
      txt[size] = 0;
      text = QString(txt);
      // qDebug("read textObj len %d <%s>", size, txt);
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void SimpleTextObj::read()
      {
      BasicDrawObj::read();
      relPos = cap->readPoint();
      align  = cap->readByte();
      _font  = cap->readFont();
      _text  = cap->readQString();
      // qDebug("read SimpletextObj(%f,%f) len %zd <%s>",
      //        relPos.x(), relPos.y(), _text.length(), qPrintable(_text));
      }

//---------------------------------------------------------
//   LineObj::read
//---------------------------------------------------------

void LineObj::read()
      {
      BasicDrawObj::read();
      pt1       = cap->readPoint();
      pt2       = cap->readPoint();
      color     = cap->readColor();
      lineWidth = cap->readByte();
      // qDebug("LineObj: %f:%f  %f:%f  width %d", pt1.x(), pt1.y(), pt2.x(), pt2.y(), lineWidth);
      }

//---------------------------------------------------------
//   BracketObj::read
//---------------------------------------------------------

void BracketObj::read()
      {
      LineObj::read();
      orientation = cap->readByte();
      number      = cap->readByte();
      }

//---------------------------------------------------------
//   GroupObj::read
//---------------------------------------------------------

void GroupObj::read()
      {
      BasicDrawObj::read();
      relPos = cap->readPoint();
      objects = cap->readDrawObjectArray();
      }

//---------------------------------------------------------
//   TransposableObj::read
//---------------------------------------------------------

void TransposableObj::read()
      {
      BasicDrawObj::read();
      relPos = cap->readPoint();
      b = cap->readByte();
      if (b != 12 && b != 21)
            qDebug("TransposableObj::read: warning: unknown drawObjectArray size of %d", b);
      variants = cap->readDrawObjectArray();
      if (variants.size() != b)
            qDebug("variants.size %d, expected %d", variants.size(), b);
      Q_ASSERT(variants.size() == b);
      /*int nRefNote =*/ cap->readInt();
      }

//---------------------------------------------------------
//   MetafileObj::read
//---------------------------------------------------------

void MetafileObj::read()
      {
      BasicRectObj::read();
      unsigned size = cap->readUnsigned();
      char enhMetaFileBits[size];
      cap->read(enhMetaFileBits, size);
      // qDebug("MetaFileObj::read %d bytes", size);
      }

//---------------------------------------------------------
//   RectEllipseObj::read
//---------------------------------------------------------

void RectEllipseObj::read()
      {
      LineObj::read();
      radius = cap->readInt();
      bFilled = cap->readByte();
      clrFill = cap->readColor();
      }

//---------------------------------------------------------
//   PolygonObj::read
//---------------------------------------------------------

void PolygonObj::read()
      {
      BasicDrawObj::read();

      unsigned nPoints = cap->readUnsigned();
      for (unsigned i = 0; i < nPoints; ++i)
            cap->readPoint();

      bFilled = cap->readByte();
      lineWidth = cap->readByte();
      clrFill = cap->readColor();
      clrLine = cap->readColor();
      }

//---------------------------------------------------------
//   WavyLineObj::read
//---------------------------------------------------------

void WavyLineObj::read()
      {
      LineObj::read();
      waveLen = cap->readByte();
      adapt = cap->readByte();
      }

//---------------------------------------------------------
//   NotelinesObj::read
//---------------------------------------------------------

void NotelinesObj::read()
      {
      BasicDrawObj::read();

      x0 = cap->readInt();
      x1 = cap->readInt();
      y  = cap->readInt();
      color = cap->readColor();

      unsigned char b = cap->readByte();
      switch (b) {
            case 1: break; // Einlinienzeile
            case 2: break; // Standard (5 Linien)
            default: {
                  Q_ASSERT(b == 0);
                  char lines[11];
                  cap->read(lines, 11);
                  break;
                  }
            }
      }

//---------------------------------------------------------
//   VoltaObj::read
//---------------------------------------------------------

void VoltaObj::read()
      {
      BasicDrawObj::read();

      x0 = cap->readInt();
      x1 = cap->readInt();
      y  = cap->readInt();
      color = cap->readColor();

      unsigned char flags = cap->readByte();
      bLeft      = (flags & 1) != 0; // links abgeknickt
      bRight     = (flags & 2) != 0; // rechts abgeknickt
      bDotted    = (flags & 4) != 0;
      allNumbers = (flags & 8) != 0;

      unsigned char numbers = cap->readByte();
      from = numbers & 0x0F;
      to = (numbers >> 4) & 0x0F;
      qDebug("VoltaObj::read x0 %d x1 %d y %d bLeft %d bRight %d bDotted %d allNumbers %d from %d to %d",
             x0, x1, y, bLeft, bRight, bDotted, allNumbers, from, to);
      }

//---------------------------------------------------------
//   GuitarObj::read
//---------------------------------------------------------

void GuitarObj::read()
      {
      BasicDrawObj::read();
      relPos  = cap->readPoint();
      color   = cap->readColor();
      flags   = cap->readWord();
      strings = cap->readDWord();   // 8 Saiten in 8 Halbbytes
      }

//---------------------------------------------------------
//   TrillObj::read
//---------------------------------------------------------

void TrillObj::read()
      {
      BasicDrawObj::read();
      x0 = cap->readInt();
      x1 = cap->readInt();
      y  = cap->readInt();
      color = cap->readColor();
      trillSign = cap->readByte();
      }

//---------------------------------------------------------
//   readDrawObjectArray
//---------------------------------------------------------

QList<BasicDrawObj*> Capella::readDrawObjectArray()
      {
      QList<BasicDrawObj*> ol;
      int n = readUnsigned();       // draw obj array

      // qDebug("readDrawObjectArray %d elements", n);
      for (int i = 0; i < n; ++i) {
            CapellaType type = CapellaType(readByte());

            // qDebug("   readDrawObject %d of %d, type %d", i, n, type);
            switch (type) {
                  case  CapellaType::GROUP: {
                        GroupObj* o = new GroupObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::TRANSPOSABLE: {
                        TransposableObj* o = new TransposableObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::METAFILE: {
                        MetafileObj* o = new MetafileObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::SIMPLE_TEXT: {
                        SimpleTextObj* o = new SimpleTextObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::TEXT: {
                        TextObj* o = new TextObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::RECT_ELLIPSE: {
                        RectEllipseObj* o = new RectEllipseObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case CapellaType::LINE: {
                        LineObj* o = new LineObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::POLYGON: {
                        PolygonObj* o = new PolygonObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::WAVY_LINE: {
                        WavyLineObj* o = new WavyLineObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case CapellaType::SLUR: {
                        SlurObj* o = new SlurObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::NOTE_LINES: {
                        NotelinesObj* o = new NotelinesObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case CapellaType::WEDGE: {
                        WedgeObj* o = new WedgeObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::VOLTA: {
                        VoltaObj* o = new VoltaObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case CapellaType::BRACKET: {
                        BracketObj* o = new BracketObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::GUITAR: {
                        GuitarObj* o = new GuitarObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  case  CapellaType::TRILL: {
                        TrillObj* o = new TrillObj(this);
                        o->read();
                        ol.append(o);
                        }
                        break;
                  default:
                        qFatal("readDrawObjectArray unsupported type %d", int(type));
                        break;
                  }
            }
      return ol;
      }

//---------------------------------------------------------
//   BasicDrawObj
//---------------------------------------------------------

void BasicDrawObj::read()
      {
      modeX       = cap->readByte();      // anchor mode
      modeY       = cap->readByte();
      distY       = cap->readByte();
      flags       = cap->readByte();
      nRefNote    = cap->readInt();
      short range = cap->readWord();
      nNotes      = range & 0x0fff;
      background  = range & 0x1000;
      pageRange   = (range >> 13) & 0x7;
      qDebug("BasicDrawObj::read modeX %d modeY %d distY %d flags %d nRefNote %d nNotes %d background %d pageRange %d",
             modeX, modeY, distY, flags, nRefNote, nNotes, background, pageRange);
      }

//---------------------------------------------------------
//   BasicRectObj
//---------------------------------------------------------

void BasicRectObj::read()
      {
      BasicDrawObj::read();
      relPos  = cap->readPoint();
      width   = cap->readInt();
      yxRatio = cap->readInt();
      height  = (width * yxRatio) / 0x10000;
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void BasicDurationalObj::read()
      {
      unsigned char b = cap->readByte();
      nDots      = b & 0x03;
      noDuration = b & 0x04;
      postGrace  = b & 0x08;
      bSmall     = b & 0x10;
      invisible  = b & 0x20;
      notBlack   = b & 0x40;
      Q_ASSERT(!(b & 0x80));

      color = notBlack ? cap->readColor() : Qt::black;

      unsigned char c = cap->readByte();
      t = TIMESTEP(c & 0x0f);
      horizontalShift = (c & 0x10) ? cap->readInt() : 0;
      count = 0;
      tripartite = 0;
      isProlonging = 0;
      if (c & 0x20) {
            unsigned char tuplet = cap->readByte();
            count        = tuplet & 0x0f;
            tripartite   = (tuplet & 0x10) != 0;
            isProlonging = (tuplet & 0x20) != 0;
            if (tuplet & 0xc0)
                  qDebug("bad tuplet value 0x%02x", tuplet);
            }
      if (c & 0x40) {
            objects = cap->readDrawObjectArray();
            }
      Q_ASSERT(!(c & 0x80));
      qDebug("DurationObj ndots %d nodur %d postgr %d bsm %d inv %d notbl %d t %d hsh %d cnt %d trp %d ispro %d",
             nDots, noDuration, postGrace, bSmall, invisible, notBlack, int(t), horizontalShift, count, tripartite, isProlonging
             );
      }

//---------------------------------------------------------
//   RestObj
//---------------------------------------------------------

RestObj::RestObj(Capella* c)
      : BasicDurationalObj(c), NoteObj(CapellaNoteObjectType::REST)
      {
      cap          = c;
      fullMeasures = 0;
      vertShift    = 0;
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void RestObj::read()
      {
      BasicDurationalObj::read();
      unsigned char b        = cap->readByte();
      bool bMultiMeasures    = b & 1;
      bVerticalCentered      = b & 2;
      bool bAddVerticalShift = b & 4;
      if (b & 0xf8)
            qFatal("RestObj: res. bits 0x%02x", b);
      fullMeasures = bMultiMeasures ? cap->readUnsigned() : 0;
      vertShift    = bAddVerticalShift ? cap->readInt() : 0;
      }

//---------------------------------------------------------
//   ChordObj
//---------------------------------------------------------

ChordObj::ChordObj(Capella* c)
      : BasicDurationalObj(c), NoteObj(CapellaNoteObjectType::CHORD)
      {
      cap      = c;
      beamMode = BeamMode::AUTO;
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void ChordObj::read()
      {
      stemDir      = StemDir::AUTO;
      dStemLength  = 0;
      nTremoloBars = 0;
      articulation = 0;
      leftTie      = false;
      rightTie     = false;
      beamShift    = 0;
      beamSlope    = 0;

      BasicDurationalObj::read();

      unsigned char flags = cap->readByte();
      beamMode      = (flags & 0x01) ? BeamMode(cap->readByte()) : BeamMode::AUTO;
      notationStave = (flags & 0x02) ? cap->readChar() : 0;
      Q_ASSERT(notationStave >= -1 && notationStave <= 1);

      if (flags & 0x04) {
            stemDir     = StemDir(cap->readChar());
            dStemLength = cap->readChar();
            }
      if (flags & 0x08) {
            nTremoloBars = cap->readByte();
            articulation = cap->readByte();
            }
      if (flags & 0x10) {
            unsigned char b = cap->readByte();
            leftTie  = b & 1;
            rightTie = b & 2;
            }
      if (flags & 0x20) {
            beamShift = cap->readChar();
            beamSlope = cap->readChar();
            }
      if (flags & 0x40) {
            unsigned nVerses = cap->readUnsigned();
            for (unsigned int i = 0; i < nVerses; ++i) {
                  bool bVerse = cap->readByte();
                  if (bVerse) {
                        Verse v;
                        unsigned char b = cap->readByte();
                        v.leftAlign = b & 1;
                        v.extender  = b & 2;
                        v.hyphen    = b & 4;
                        v.num       = i;
                        if (b & 8)
                              v.verseNumber = cap->readQString();
                        if (b & 16)
                              v.text = cap->readQString();
                        verse.append(v);
                        }
                  }
            }
      unsigned nNotes = cap->readUnsigned();
      for (unsigned int i = 0; i < nNotes; ++i) {
            CNote n;
            n.explAlteration = 0;
            char c           = cap->readChar();
            bool bit7        = c & 0x80;
            bool bit6        = c & 0x40;
            n.pitch          = (signed char) c;
            if (bit7 != bit6) {
                  n.explAlteration = 2;
                  n.pitch ^= 0x80;
                  }
            unsigned char b = cap->readByte();
            n.headType      = b & 7;
            n.alteration    = ((b >> 3) & 7) - 2;  // -2 -- +2
            if (b & 0x40)
                  n.explAlteration = 1;
            n.silent = b & 0x80;
            qDebug("ChordObj::read() note pitch %d explAlt %d head %d alt %d silent %d",
                   n.pitch, n.explAlteration, n.headType, n.alteration, n.silent);
            notes.append(n);
            }
      }

//---------------------------------------------------------
//    read
//    return false on error
//---------------------------------------------------------

void Capella::read(void* p, qint64 len)
      {
      if (len == 0)
            return;
      qint64 rv = f->read((char*)p, len);
      if (rv != len)
            throw Capella::Error::CAP_EOF;
      curPos += len;
      }

//---------------------------------------------------------
//   readByte
//---------------------------------------------------------

unsigned char Capella::readByte()
      {
      unsigned char c;
      read(&c, 1);
      return c;
      }

//---------------------------------------------------------
//   readChar
//---------------------------------------------------------

char Capella::readChar()
      {
      char c;
      read(&c, 1);
      return c;
      }

//---------------------------------------------------------
//   readWord
//---------------------------------------------------------

short Capella::readWord()
      {
      short c;
      read(&c, 2);
      return c;
      }

//---------------------------------------------------------
//   readDWord
//---------------------------------------------------------

int Capella::readDWord()
      {
      int c;
      read(&c, 4);
      return c;
      }

//---------------------------------------------------------
//   readLong
//---------------------------------------------------------

int Capella::readLong()
      {
      int c;
      read(&c, 4);
      return c;
      }

//---------------------------------------------------------
//   readUnsigned
//---------------------------------------------------------

unsigned Capella::readUnsigned()
      {
      unsigned char c;
      read(&c, 1);
      if (c == 254) {
            unsigned short s;
            read(&s, 2);
            return s;
            }
      else if (c == 255) {
            unsigned s;
            read(&s, 4);
            return s;
            }
      else
            return c;
      }

//---------------------------------------------------------
//   readInt
//---------------------------------------------------------

int Capella::readInt()
      {
      signed char c;
      read(&c, 1);
      if (c == -128) {
            short s;
            read(&s, 2);
            return s;
            }
      else if (c == 127) {
            int s;
            read(&s, 4);
            return s;
            }
      else
            return c;
      }

//---------------------------------------------------------
//   readString -- read Capella string into newly allocated char buffer
//   note that no carriage return / newline interpretation is done
//---------------------------------------------------------

char* Capella::readString()
      {
      unsigned len = readUnsigned();
      char* buffer = new char[len + 1];
      read(buffer, len);
      buffer[len] = 0;
      return buffer;
      }

//---------------------------------------------------------
//   readQString -- read Capella string into QString
//   strings in Capella files may contain \r\n, must remove the \r
//---------------------------------------------------------

QString Capella::readQString()
      {
      char* buffer = readString();               // read Capella string
      QString res = QString::fromLatin1(buffer); // and copy into QString
      res = res.remove(QChar('\r'));             // remove the \r
      delete [] buffer;                          // delete memory allocated by readString
      return res;
      }

//---------------------------------------------------------
//   readColor
//---------------------------------------------------------

QColor Capella::readColor()
      {
      static const int colors[] = {
            0x000000, // schwarz
            0x000080, // dunkelrot
            0x008000, // dunkelgrün
            0x008080, // ocker
            0x800000, // dunkelblau
            0x800080, // purpurrot
            0x808000, // blaugün
            0x808080, // grau
            0xC0C0C0, // hellgrau
            0x0000FF, // rot
            0x00FF00, // grün
            0x00FFFF, // gelb
            0xFF0000, // blau
            0xFF00FF, // lila
            0xFFFF00, // aquamarin
            0xFFFFFF  // weiß
            };

      QColor c;
      unsigned char b = readByte();
      if (b >= 16) {
            Q_ASSERT(b == 255);
            int r = readByte();
            int g = readByte();
            int b = readByte();
            c = QColor(r, g, b);
            }
      else {
            c = QColor(colors[b]);
            }
      return c;
      }

//---------------------------------------------------------
//   readFont
//---------------------------------------------------------

QFont Capella::readFont()
      {
      int index = readUnsigned();
      if (index == 0) {
            int lfHeight           = readLong();
            /*int lfWidth            =*/ readLong();
            /*int lfEscapement       =*/ readLong();
            /*int lfOrientation      =*/ readLong();
            int lfWeight           = readLong();
            uchar lfItalic         = readByte();
            uchar lfUnderline      = readByte();
            uchar lfStrikeOut      = readByte();
            /*uchar lfCharSet        =*/ readByte();
            /*uchar lfOutPrecision   =*/ readByte();
            /*uchar lfClipPrecision  =*/ readByte();
            /*uchar lfQuality        =*/ readByte();
            /*uchar lfPitchAndFamily =*/ readByte();
            /*QColor color           =*/ readColor();
            QString face             = readQString();

            qDebug("Font <%s> size %d, weight %d", qPrintable(face), lfHeight, lfWeight);
            QFont font(face);
            font.setPointSizeF(lfHeight / 1000.0);
            font.setItalic(lfItalic);
            font.setStrikeOut(lfStrikeOut);
            font.setUnderline(lfUnderline);

            switch (lfWeight) {
                  case 700:  font.setWeight(QFont::Bold); break;
                  case 400:  font.setWeight(QFont::Normal); break;
                  case 0:    font.setWeight(QFont::Light); break;
                  }
            fonts.append(font);
            return font;
            }
      index -= 1;
      if (index >= fonts.size()) {
            qDebug("illegal font index %d (max %d)", index, fonts.size()-1);
            }
      return fonts[index];
      }

//---------------------------------------------------------
//   readStaveLayout
//---------------------------------------------------------

void Capella::readStaveLayout(CapStaffLayout* sl, int idx)
      {
      sl->barlineMode = readByte();
      sl->noteLines   = readByte();
      switch (sl->noteLines) {
            case 1:     break;      // one line
            case 2:     break;      // five lines
            default:
                  {
                  char lines[11];
                  f->read(lines, 11);
                  curPos += 11;
                  }
                  break;
            }
      qDebug("StaffLayout %d: barlineMode %d noteLines %d", idx, sl->barlineMode, sl->noteLines);

      sl->bSmall      = readByte();
      qDebug("staff size small %d", sl->bSmall);

      sl->topDist      = readInt();
      sl->btmDist      = readInt();
      sl->groupDist    = readInt();
      sl->barlineFrom = readByte();
      sl->barlineTo   = readByte();
      // qDebug("topDist %d btmDist %d groupDist %d barlineFrom %d barlineTo %d",
      //        sl->topDist, sl->btmDist, sl->groupDist, sl->barlineFrom, sl->barlineTo);

      unsigned char clef = readByte();
      sl->form = Form(clef & 7);
      sl->line = ClefLine((clef >> 3) & 7);
      sl->oct  = Oct((clef >> 6));
      qDebug("   clef %x  form %d, line %d, oct %d", clef, int(sl->form), int(sl->line), int(sl->oct));

      // Schlagzeuginformation
      unsigned char b   = readByte();
      sl->bPercussion  = b & 1;    // Schlagzeugkanal verwenden
      sl->bSoundMapIn  = b & 2;
      sl->bSoundMapOut = b & 4;
      if (sl->bSoundMapIn) {      // Umleitungstabelle für Eingabe vom Keyboard
            uchar iMin = readByte();
            Q_UNUSED(iMin);
            uchar n    = readByte();
            Q_ASSERT (n > 0 and iMin + n <= 128);
            f->read(sl->soundMapIn, n);
            curPos += n;
            }
      if (sl->bSoundMapOut) {     // Umleitungstabelle für das Vorspielen
            unsigned char iMin = readByte();
            Q_UNUSED(iMin);
            unsigned char n    = readByte();
            Q_ASSERT (n > 0 and iMin + n <= 128);
            f->read(sl->soundMapOut, n);
            curPos += n;
            }
      sl->sound  = readInt();
      sl->volume = readInt();
      sl->transp = readInt();
      qDebug("   sound %d vol %d transp %d", sl->sound, sl->volume, sl->transp);

      sl->descr              = readQString();
      sl->name               = readQString();
      sl->abbrev             = readQString();
      sl->intermediateName   = readQString();
      sl->intermediateAbbrev = readQString();
      qDebug("   descr <%s> name <%s>  abbrev <%s> iname <%s> iabrev <%s>",
             qPrintable(sl->descr), qPrintable(sl->name), qPrintable(sl->abbrev),
             qPrintable(sl->intermediateName), qPrintable(sl->intermediateAbbrev));
      }

//---------------------------------------------------------
//   readLayout
//---------------------------------------------------------

void Capella::readLayout()
      {
      smallLineDist  = double(readInt()) / 100;
      normalLineDist = double(readInt()) / 100;
      qDebug("Capella::readLayout(): smallLineDist %g normalLineDist %g", smallLineDist, normalLineDist);

      topDist        = readInt();
      interDist      = readInt();
      qDebug("Capella::readLayout(): topDist %d", topDist);

      txtAlign   = readByte();    // Stimmenbezeichnungen 0=links, 1=zentriert, 2=rechts
      adjustVert = readByte();    // 0=nein, 1=au�er letzte Seite, 3=alle Seiten

      unsigned char b          = readByte();
      redundantKeys    = b & 1;
      modernDoubleNote = b & 2;
      Q_ASSERT((b & 0xFC) == 0); // bits 2...7 reserviert

      bSystemSeparators = readByte();
      nUnnamed           = readInt();

      namesFont = readFont();

      // Musterzeilen
      unsigned nStaveLayouts = readUnsigned();

      // qDebug("%d staves", nStaveLayouts);

      for (unsigned iStave = 0; iStave < nStaveLayouts; iStave++) {
            CapStaffLayout* sl = new CapStaffLayout;
            readStaveLayout(sl, iStave);
            _staffLayouts.append(sl);
            }

      // system brackets:
      unsigned n = readUnsigned();  // number of brackets
      for (unsigned int i = 0; i < n; i++) {
            CapBracket b;
            b.from   = readInt();
            b.to     = readInt();
            b.curly = readByte();
            // qDebug("Bracket%d %d-%d curly %d", i, b.from, b.to, b.curly);
            brackets.append(b);
            }
      // qDebug("Capella::readLayout(): done");
      }

//---------------------------------------------------------
//   readExtra
//---------------------------------------------------------

void Capella::readExtra()
      {
      uchar n = readByte();
      if (n) {
            qDebug("Capella::readExtra(%d)", n);
            for (int i = 0; i < n; ++i)
                  readByte();
            }
      }

//---------------------------------------------------------
//   CapClef::read
//---------------------------------------------------------

void CapClef::read()
      {
      unsigned char b = cap->readByte();
      form            = Form(b & 7);
      line            = ClefLine((b >> 3) & 7);
      oct             = Oct(b >> 6);
      qDebug("Clef::read form %d line %d oct %d", int(form), int(line), int(oct));
      }

//---------------------------------------------------------
//   clef
//---------------------------------------------------------

ClefType CapClef::clef() const
      {
      return clefType(form, line, oct);
      }

ClefType CapClef::clefType(Form form, ClefLine line, Oct oct)
      {
      int idx = int(form) + (int(line) << 3) + (int(oct) << 5);
      switch (idx) {
            case int(Form::G) + (int(ClefLine::L2) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::G;
            case int(Form::G) + (int(ClefLine::L2) << 3) + (int(Oct::OCT_ALTA) << 5):  return ClefType::G1;
            case int(Form::G) + (int(ClefLine::L2) << 3) + (int(Oct::OCT_BASSA) << 5): return ClefType::G3;

            case int(Form::C) + (int(ClefLine::L1) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::C1;
            case int(Form::C) + (int(ClefLine::L2) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::C2;
            case int(Form::C) + (int(ClefLine::L3) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::C3;
            case int(Form::C) + (int(ClefLine::L4) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::C4;
            case int(Form::C) + (int(ClefLine::L5) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::C5;

            case int(Form::F) + (int(ClefLine::L4) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::F;
            case int(Form::F) + (int(ClefLine::L4) << 3) + (int(Oct::OCT_BASSA) << 5): return ClefType::F8;
            case int(Form::F) + (int(ClefLine::L3) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::F_B;
            case int(Form::F) + (int(ClefLine::L5) << 3) + (int(Oct::OCT_NULL) << 5):  return ClefType::F_C;

            default:
                  if (form == Form::FORM_NULL)
                        return ClefType::INVALID;
                  qDebug("unknown clef %d %d %d", int(form), int(line), int(oct));
                  break;
            }
      return ClefType::INVALID;
      }

//---------------------------------------------------------
//   CapKey::read
//---------------------------------------------------------

void CapKey::read()
      {
      unsigned char b = cap->readByte();
      signature = int(b) - 7;
      // qDebug("         Key %d", signature);
      }

//---------------------------------------------------------
//   CapMeter::read
//---------------------------------------------------------

void CapMeter::read()
      {
      numerator = cap->readByte();
      uchar d   = cap->readByte();
      log2Denom = (d & 0x7f) - 1;
      allaBreve = d & 0x80;
      qDebug("   Meter %d/%d allaBreve %d", numerator, log2Denom, allaBreve);
      if (log2Denom > 7 || log2Denom < 0) {
            qDebug("   illegal fraction");
            // abort();
            log2Denom = 2;
            numerator = 4;
            }
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void WedgeObj::read()
      {
      LineObj::read();
      char b = cap->readByte();
      height = b & 0x7f;
      decresc = b & 0x80;
      }

//---------------------------------------------------------
//   CapExplicitBarline::read
//---------------------------------------------------------

void CapExplicitBarline::read()
      {
      unsigned char b = cap->readByte();
      int type = b & 0x0f;
      if (type == 0) _type = BarLineType::NORMAL;
      else if (type == 1) _type = BarLineType::DOUBLE;
      else if (type == 2) _type = BarLineType::END;
      else if (type == 3) _type = BarLineType::END_REPEAT;
      else if (type == 4) _type = BarLineType::START_REPEAT;
      else if (type == 5) _type = BarLineType::END_START_REPEAT;
      else if (type == 6) _type = BarLineType::BROKEN;
      else _type = BarLineType::NORMAL; // default
      _barMode = b >> 4;         // 0 = auto, 1 = nur Zeilen, 2 = durchgezogen
      Q_ASSERT(_barMode <= 2);

      qDebug("         Expl.Barline type %d mode %d", int(_type), _barMode);
      }

//---------------------------------------------------------
//   readVoice
//---------------------------------------------------------

void Capella::readVoice(CapStaff* cs, int idx)
      {
      qDebug("      readVoice %d", idx);

      if (readChar() != 'C')
            throw Capella::Error::BAD_VOICE_SIG;

      CapVoice* v   = new CapVoice;
      v->voiceNo    = idx;
      v->y0Lyrics   = readByte();
      v->dyLyrics   = readByte();
      v->lyricsFont = readFont();
      v->stemDir    = readByte();
      readExtra();

      unsigned nNoteObjs = readUnsigned();          // Notenobjekte
      for (unsigned i = 0; i < nNoteObjs; i++) {
            QColor color       = Qt::black;
            uchar type = readByte();
            // qDebug("         Voice %d read object idx %d(%d) type %d", idx,  i, nNoteObjs, type);
            readExtra();
            if ((type != uchar(CapellaNoteObjectType::REST)) && (type != uchar(CapellaNoteObjectType::CHORD)) && (type != uchar(CapellaNoteObjectType::PAGE_BKGR)))
                  color = readColor();

            // Die anderen Objekttypen haben eine komprimierte Farbcodierung
            switch (CapellaNoteObjectType(type)) {
                  case CapellaNoteObjectType::REST:
                        {
                        RestObj* rest = new RestObj(this);
                        rest->read();
                        v->objects.append(rest);
                        }
                        break;
                  case CapellaNoteObjectType::CHORD:
                  case CapellaNoteObjectType::PAGE_BKGR:
                        {
                        ChordObj* chord = new ChordObj(this);
                        chord->read();
                        v->objects.append(chord);
                        }
                        break;
                  case CapellaNoteObjectType::CLEF:
                        {
                        CapClef* clef = new CapClef(this);
                        clef->read();
                        v->objects.append(clef);
                        }
                        break;
                  case CapellaNoteObjectType::KEY:
                        {
                        CapKey* key = new CapKey(this);
                        key->read();
                        v->objects.append(key);
                        }
                        break;
                  case CapellaNoteObjectType::METER:
                        {
                        CapMeter* meter = new CapMeter(this);
                        meter->read();
                        v->objects.append(meter);
                        }
                        break;
                  case CapellaNoteObjectType::EXPL_BARLINE:
                        {
                        CapExplicitBarline* bl = new CapExplicitBarline(this);
                        bl->read();
                        qDebug("append Expl Barline==========");
                        v->objects.append(bl);
                        }
                        break;
                  default:
                        qFatal("bad voice type %d", type);
                  }
            }
      cs->voices.append(v);
      }

//---------------------------------------------------------
//   readStaff
//---------------------------------------------------------

void Capella::readStaff(CapSystem* system)
      {
      if (readChar() != 'V')
            throw Capella::Error::BAD_STAFF_SIG;

      CapStaff* staff = new CapStaff;
      //Meter
      staff->numerator = readByte();
      uchar d          = readByte();
      staff->log2Denom = (d & 0x7f) - 1;
      staff->allaBreve = d & 0x80;

      staff->iLayout   = readByte();
      staff->topDistX  = readInt();
      staff->btmDistX  = readInt();
      staff->color     = readColor();
      readExtra();

      qDebug("      Staff iLayout %d", staff->iLayout);
      // Stimmen
      unsigned nVoices = readUnsigned();
      for (unsigned i = 0; i < nVoices; i++)
            readVoice(staff, i);
      system->staves.append(staff);
      }

//---------------------------------------------------------
//   readSystem
//---------------------------------------------------------

void Capella::readSystem()
      {
      if (readChar() != 'S')
            throw Capella::Error::BAD_SYSTEM_SIG;

      CapSystem* s = new CapSystem;
      s->nAddBarCount   = readInt();
      s->bBarCountReset = readByte();
      s->explLeftIndent = readByte();

      s->beamMode = BeamMode(readByte());
      s->tempo    = readUnsigned();
      s->color    = readColor();
      readExtra();

      unsigned char b  = readByte();
      s->bJustified    = b & 2;
      s->bPageBreak    = (b & 4) != 0;
      s->instrNotation = (b >> 3) & 7;

      unsigned nStaves = readUnsigned();
      for (unsigned i = 0; i < nStaves; i++)
            readStaff(s);
      systems.append(s);
      }

//---------------------------------------------------------
//   toTicks
//---------------------------------------------------------

int BasicDurationalObj::ticks() const
      {
      if (noDuration)
            return 0;
      int len = 0;
      switch (t) {
            case TIMESTEP::D1:          len = 4 * MScore::division; break;
            case TIMESTEP::D2:          len = 2 * MScore::division; break;
            case TIMESTEP::D4:          len = MScore::division; break;
            case TIMESTEP::D8:          len = MScore::division >> 1; break;
            case TIMESTEP::D16:         len = MScore::division >> 2; break;
            case TIMESTEP::D32:         len = MScore::division >> 3; break;
            case TIMESTEP::D64:         len = MScore::division >> 4; break;
            case TIMESTEP::D128:        len = MScore::division >> 5; break;
            case TIMESTEP::D256:        len = MScore::division >> 6; break;
            case TIMESTEP::D_BREVE:     len = MScore::division * 8; break;
            default:
                  qDebug("BasicDurationalObj::ticks: illegal duration value %d", int(t));
                  break;
            }
      int slen = len;
      int dots = nDots;
      while (dots--) {
            slen >>= 1;
            len += slen;
            }
      return len;
      }

//---------------------------------------------------------
//   readPoint
//---------------------------------------------------------

QPointF Capella::readPoint()
      {
      int x = readInt();
      int y = readInt();
      return QPointF(double(x), double(y));
      }

//---------------------------------------------------------
//   read
//---------------------------------------------------------

void Capella::read(QFile* fp)
      {
      f      = fp;
      curPos = 0;

      char signature[9];
      read(signature, 8);
      signature[8] = 0;
      if (memcmp(signature, "cap3-v:", 7) != 0)
            throw Capella::Error::BAD_SIG;

      // qDebug("read Capella file signature <%s>", signature);

      // TODO: test for signature[7] = a-z

      author   = readString();
      keywords = readString();
      comment  = readString();

      // qDebug("author <%s> keywords <%s> comment <%s>", author, keywords, comment);

      nRel   = readUnsigned();            // 75
      nAbs   = readUnsigned();            // 16
      unsigned char b   = readByte();
      bUseRealSize      = b & 1;
      bAllowCompression = b & 2;
      bPrintLandscape   = b & 16;

      // qDebug("  nRel %d  nAbs %d useRealSize %d compresseion %d", nRel, nAbs, bUseRealSize, bAllowCompression);

      readLayout();

      beamRelMin0 = readByte();        // basic setup for beam slope
      beamRelMin1 = readByte();
      beamRelMax0 = readByte();
      beamRelMax1 = readByte();

      readExtra();

      readDrawObjectArray();                // Galerie (gesammelte Grafikobjekte)

      unsigned n = readUnsigned();
      if (n) {
            qDebug("Gallery objects");
            }
      for (unsigned int i = 0; i < n; ++i) {
            /*char* s =*/ readString();     // Namen der Galerie-Objekte
            // qDebug("Galerie: <%s>", s);
            }

      // qDebug("read backgroundChord");
      backgroundChord = new ChordObj(this);
      backgroundChord->read();              // contains graphic objects on the page background
      // qDebug("read backgroundChord done");
      bShowBarCount    = readByte();        // Taktnumerierung zeigen
      barNumberFrame   = readByte();        // 0=kein, 1=Rechteck, 2=Ellipse
      nBarDistX        = readByte();
      nBarDistY        = readByte();
      QFont barNumFont = readFont();
      nFirstPage       = readUnsigned();    // Versatz fuer Seitenzaehlung
      leftPageMargins  = readUnsigned();    // Seitenraender
      topPageMargins   = readUnsigned();
      rightPageMargins = readUnsigned();
      btmPageMargins   = readUnsigned();

      unsigned nSystems  = readUnsigned();
      for (unsigned i = 0; i < nSystems; i++)
            readSystem();
      char esig[4];
      read(esig, 4);
      if (memcmp (esig, "\0\0\0\0", 4) != 0)
            throw Capella::Error::BAD_SIG;
      }

//---------------------------------------------------------
//   importCapella
//---------------------------------------------------------

Score::FileError importCapella(Score* score, const QString& name)
      {
      QFile fp(name);
      if(!fp.exists())
            return Score::FileError::FILE_NOT_FOUND;
      if (!fp.open(QIODevice::ReadOnly))
            return Score::FileError::FILE_OPEN_ERROR;

      Capella cf;
      try {
            cf.read(&fp);
            }
      catch (Capella::Error errNo) {
            if (!MScore::noGui) {
                  QMessageBox::warning(0,
                     QWidget::tr("MuseScore: Import Capella"),
                     QWidget::tr("Load failed: ") + cf.error(errNo),
                     QString::null, QWidget::tr("Quit"), QString::null, 0, 1);
                  }
            fp.close();
            // avoid another error message box
            return Score::FileError::FILE_NO_ERROR;
            }
      fp.close();
      convertCapella(score, &cf, false);
      return Score::FileError::FILE_NO_ERROR;
      }
}