annotate abc2xml/abc2xml.py @ 1092:0539e3ec5b5f build-default-407

Merge.
author Jim Hague <jim.hague@acm.org>
date Sat, 19 Nov 2022 20:43:32 +0000
parents b1dbb76f4eb9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1 #!/usr/bin/env python
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2 # coding=latin-1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3 '''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
4 Copyright (C) 2012-2018: Willem G. Vree
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
5 Contributions: Nils Liberg, Nicolas Froment, Norman Schmidt, Reinier Maliepaard, Martin Tarenskeen,
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
6 Paul Villiger, Alexander Scheutzow, Herbert Schneider, David Randolph, Michael Strasser
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
7
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
8 This program is free software; you can redistribute it and/or modify it under the terms of the
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
9 Lesser GNU General Public License as published by the Free Software Foundation;
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
10
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
11 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
12 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
13 See the Lesser GNU General Public License for more details. <http://www.gnu.org/licenses/lgpl.html>.
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
14 '''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
15
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
16 from functools import reduce
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
17 from pyparsing import Word, OneOrMore, Optional, Literal, NotAny, MatchFirst
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
18 from pyparsing import Group, oneOf, Suppress, ZeroOrMore, Combine, FollowedBy
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
19 from pyparsing import srange, CharsNotIn, StringEnd, LineEnd, White, Regex
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
20 from pyparsing import nums, alphas, alphanums, ParseException, Forward
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
21 try: import xml.etree.cElementTree as E
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
22 except: import xml.etree.ElementTree as E
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
23 import types, sys, os, re, datetime
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
24
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
25 VERSION = 238
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
26
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
27 python3 = sys.version_info[0] > 2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
28 lmap = lambda f, xs: list (map (f, xs)) # eager map for python 3
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
29 if python3:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
30 int_type = int
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
31 list_type = list
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
32 str_type = str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
33 uni_type = str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
34 stdin = sys.stdin.buffer # read binary!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
35 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
36 int_type = types.IntType
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
37 list_type = types.ListType
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
38 str_type = types.StringTypes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
39 uni_type = types.UnicodeType
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
40 stdin = sys.stdin
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
41
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
42 def info (s, warn=1):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
43 x = (warn and '-- ' or '') + s
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
44 try: sys.stderr.write (x + '\n')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
45 except: sys.stderr.write (repr (x) + '\n')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
46
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
47 def abc_grammar (): # header, voice and lyrics grammar for ABC
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
48 #-----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
49 # expressions that catch and skip some syntax errors (see corresponding parse expressions)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
50 #-----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
51 b1 = Word (u"-,'<>\u2019#", exact=1) # catch misplaced chars in chords
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
52 b2 = Regex ('[^H-Wh-w~=]*') # same in user defined symbol definition
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
53 b3 = Regex ('[^=]*') # same, second part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
54
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
55 #-----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
56 # ABC header (field_str elements are matched later with reg. epr's)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
57 #-----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
58
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
59 number = Word (nums).setParseAction (lambda t: int (t[0]))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
60 field_str = Regex (r'[^]]*') # match anything until end of field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
61 field_str.setParseAction (lambda t: t[0].strip ()) # and strip spacing
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
62
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
63 userdef_symbol = Word (srange ('[H-Wh-w~]'), exact=1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
64 fieldId = oneOf ('K L M Q P I T C O A Z N G H R B D F S E r Y') # info fields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
65 X_field = Literal ('X') + Suppress (':') + field_str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
66 U_field = Literal ('U') + Suppress (':') + b2 + Optional (userdef_symbol, 'H') + b3 + Suppress ('=') + field_str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
67 V_field = Literal ('V') + Suppress (':') + Word (alphanums + '_') + field_str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
68 inf_fld = fieldId + Suppress (':') + field_str
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
69 ifield = Suppress ('[') + (X_field | U_field | V_field | inf_fld) + Suppress (']')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
70 abc_header = OneOrMore (ifield) + StringEnd ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
71
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
72 #---------------------------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
73 # I:score with recursive part groups and {* grand staff marker
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
74 #---------------------------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
75
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
76 voiceId = Suppress (Optional ('*')) + Word (alphanums + '_')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
77 voice_gr = Suppress ('(') + OneOrMore (voiceId | Suppress ('|')) + Suppress (')')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
78 simple_part = voiceId | voice_gr | Suppress ('|')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
79 grand_staff = oneOf ('{* {') + OneOrMore (simple_part) + Suppress ('}')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
80 part = Forward ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
81 part_seq = OneOrMore (part | Suppress ('|'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
82 brace_gr = Suppress ('{') + part_seq + Suppress ('}')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
83 bracket_gr = Suppress ('[') + part_seq + Suppress (']')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
84 part <<= MatchFirst (simple_part | grand_staff | brace_gr | bracket_gr | Suppress ('|'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
85 abc_scoredef = Suppress (oneOf ('staves score')) + OneOrMore (part)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
86
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
87 #----------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
88 # ABC lyric lines (white space sensitive)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
89 #----------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
90
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
91 skip_note = oneOf ('* - ~')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
92 extend_note = Literal ('_')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
93 measure_end = Literal ('|')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
94 syl_str = CharsNotIn ('*~-_| \t\n\\]')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
95 syl_chars = Combine (OneOrMore (syl_str | Regex (r'\\.')))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
96 white = Word (' \t')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
97 syllable = Combine (Optional ('~') + syl_chars + ZeroOrMore (Literal ('~') + syl_chars)) + Optional ('-')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
98 lyr_elem = (syllable | skip_note | extend_note | measure_end) + Optional (white).suppress ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
99 lyr_line = Optional (white).suppress () + ZeroOrMore (lyr_elem)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
100
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
101 syllable.setParseAction (lambda t: pObj ('syl', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
102 skip_note.setParseAction (lambda t: pObj ('skip', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
103 extend_note.setParseAction (lambda t: pObj ('ext', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
104 measure_end.setParseAction (lambda t: pObj ('sbar', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
105 lyr_line_wsp = lyr_line.leaveWhitespace () # parse actions must be set before calling leaveWhitespace
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
106
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
107 #---------------------------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
108 # ABC voice (not white space sensitive, beams detected in note/rest parse actions)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
109 #---------------------------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
110
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
111 inline_field = Suppress ('[') + (inf_fld | U_field | V_field) + Suppress (']')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
112 lyr_fld = Suppress ('[') + Suppress ('w') + Suppress (':') + lyr_line_wsp + Suppress (']') # lyric line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
113 lyr_blk = OneOrMore (lyr_fld) # verses
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
114 fld_or_lyr = inline_field | lyr_blk # inline field or block of lyric verses
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
115
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
116 note_length = Optional (number, 1) + Group (ZeroOrMore ('/')) + Optional (number, 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
117 octaveHigh = OneOrMore ("'").setParseAction (lambda t: len(t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
118 octaveLow = OneOrMore (',').setParseAction (lambda t: -len(t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
119 octave = octaveHigh | octaveLow
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
120
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
121 basenote = oneOf ('C D E F G A B c d e f g a b y') # includes spacer for parse efficiency
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
122 accidental = oneOf ('^^ __ ^ _ =')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
123 rest_sym = oneOf ('x X z Z')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
124 slur_beg = oneOf ("( (, (' .( .(, .('") + ~Word (nums) # no tuplet_start
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
125 slur_ends = OneOrMore (oneOf (') .)'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
126
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
127 long_decoration = Combine (oneOf ('! +') + CharsNotIn ('!+ \n') + oneOf ('! +'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
128 staccato = Literal ('.') + ~Literal ('|') # avoid dotted barline
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
129 pizzicato = Literal ('!+!') # special case: plus sign is old style deco marker
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
130 decoration = slur_beg | staccato | userdef_symbol | long_decoration | pizzicato
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
131 decorations = OneOrMore (decoration)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
132
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
133 tie = oneOf ('.- -')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
134 rest = Optional (accidental) + rest_sym + note_length
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
135 pitch = Optional (accidental) + basenote + Optional (octave, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
136 note = pitch + note_length + Optional (tie) + Optional (slur_ends)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
137 dec_note = Optional (decorations) + pitch + note_length + Optional (tie) + Optional (slur_ends)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
138 chord_note = dec_note | rest | b1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
139 grace_notes = Forward ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
140 chord = Suppress ('[') + OneOrMore (chord_note | grace_notes) + Suppress (']') + note_length + Optional (tie) + Optional (slur_ends)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
141 stem = note | chord | rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
142
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
143 broken = Combine (OneOrMore ('<') | OneOrMore ('>'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
144
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
145 tuplet_num = Suppress ('(') + number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
146 tuplet_into = Suppress (':') + Optional (number, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
147 tuplet_notes = Suppress (':') + Optional (number, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
148 tuplet_start = tuplet_num + Optional (tuplet_into + Optional (tuplet_notes))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
149
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
150 acciaccatura = Literal ('/')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
151 grace_stem = Optional (decorations) + stem
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
152 grace_notes <<= Group (Suppress ('{') + Optional (acciaccatura) + OneOrMore (grace_stem) + Suppress ('}'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
153
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
154 text_expression = Optional (oneOf ('^ _ < > @'), '^') + Optional (CharsNotIn ('"'), "")
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
155 chord_accidental = oneOf ('# b =')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
156 triad = oneOf ('ma Maj maj M mi min m aug dim o + -')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
157 seventh = oneOf ('7 ma7 Maj7 M7 maj7 mi7 min7 m7 dim7 o7 -7 aug7 +7 m7b5 mi7b5')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
158 sixth = oneOf ('6 ma6 M6 mi6 min6 m6')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
159 ninth = oneOf ('9 ma9 M9 maj9 Maj9 mi9 min9 m9')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
160 elevn = oneOf ('11 ma11 M11 maj11 Maj11 mi11 min11 m11')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
161 thirt = oneOf ('13 ma13 M13 maj13 Maj13 mi13 min13 m13')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
162 suspended = oneOf ('sus sus2 sus4')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
163 chord_degree = Combine (Optional (chord_accidental) + oneOf ('2 4 5 6 7 9 11 13'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
164 chord_kind = Optional (seventh | sixth | ninth | elevn | thirt | triad) + Optional (suspended)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
165 chord_root = oneOf ('C D E F G A B') + Optional (chord_accidental)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
166 chord_bass = oneOf ('C D E F G A B') + Optional (chord_accidental) # needs a different parse action
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
167 chordsym = chord_root + chord_kind + ZeroOrMore (chord_degree) + Optional (Suppress ('/') + chord_bass)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
168 chord_sym = chordsym + Optional (Literal ('(') + CharsNotIn (')') + Literal (')')).suppress ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
169 chord_or_text = Suppress ('"') + (chord_sym ^ text_expression) + Suppress ('"')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
170
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
171 volta_nums = Optional ('[').suppress () + Combine (Word (nums) + ZeroOrMore (oneOf (', -') + Word (nums)))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
172 volta_text = Literal ('[').suppress () + Regex (r'"[^"]+"')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
173 volta = volta_nums | volta_text
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
174 invisible_barline = oneOf ('[|] []')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
175 dashed_barline = oneOf (': .|')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
176 double_rep = Literal (':') + FollowedBy (':') # otherwise ambiguity with dashed barline
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
177 voice_overlay = Combine (OneOrMore ('&'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
178 bare_volta = FollowedBy (Literal ('[') + Word (nums)) # no barline, but volta follows (volta is parsed in next measure)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
179 bar_left = (oneOf ('[|: |: [: :') + Optional (volta)) | Optional ('|').suppress () + volta | oneOf ('| [|')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
180 bars = ZeroOrMore (':') + ZeroOrMore ('[') + OneOrMore (oneOf ('| ]'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
181 bar_right = invisible_barline | double_rep | Combine (bars) | dashed_barline | voice_overlay | bare_volta
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
182
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
183 errors = ~bar_right + Optional (Word (' \n')) + CharsNotIn (':&|', exact=1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
184 linebreak = Literal ('$') | ~decorations + Literal ('!') # no need for I:linebreak !!!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
185 element = fld_or_lyr | broken | decorations | stem | chord_or_text | grace_notes | tuplet_start | linebreak | errors
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
186 measure = Group (ZeroOrMore (inline_field) + Optional (bar_left) + ZeroOrMore (element) + bar_right + Optional (linebreak) + Optional (lyr_blk))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
187 noBarMeasure = Group (ZeroOrMore (inline_field) + Optional (bar_left) + OneOrMore (element) + Optional (linebreak) + Optional (lyr_blk))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
188 abc_voice = ZeroOrMore (measure) + Optional (noBarMeasure | Group (bar_left)) + ZeroOrMore (inline_field).suppress () + StringEnd ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
189
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
190 #----------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
191 # I:percmap note [step] [midi] [note-head]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
192 #----------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
193
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
194 white2 = (white | StringEnd ()).suppress ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
195 w3 = Optional (white2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
196 percid = Word (alphanums + '-')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
197 step = basenote + Optional (octave, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
198 pitchg = Group (Optional (accidental, '') + step + FollowedBy (white2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
199 stepg = Group (step + FollowedBy (white2)) | Literal ('*')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
200 midi = (Literal ('*') | number | pitchg | percid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
201 nhd = Optional (Combine (percid + Optional ('+')), '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
202 perc_wsp = Literal ('percmap') + w3 + pitchg + w3 + Optional (stepg, '*') + w3 + Optional (midi, '*') + w3 + nhd
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
203 abc_percmap = perc_wsp.leaveWhitespace ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
204
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
205 #----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
206 # Parse actions to convert all relevant results into an abstract
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
207 # syntax tree where all tree nodes are instances of pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
208 #----------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
209
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
210 ifield.setParseAction (lambda t: pObj ('field', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
211 grand_staff.setParseAction (lambda t: pObj ('grand', t, 1)) # 1 = keep ordered list of results
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
212 brace_gr.setParseAction (lambda t: pObj ('bracegr', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
213 bracket_gr.setParseAction (lambda t: pObj ('bracketgr', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
214 voice_gr.setParseAction (lambda t: pObj ('voicegr', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
215 voiceId.setParseAction (lambda t: pObj ('vid', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
216 abc_scoredef.setParseAction (lambda t: pObj ('score', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
217 note_length.setParseAction (lambda t: pObj ('dur', (t[0], (t[2] << len (t[1])) >> 1)))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
218 chordsym.setParseAction (lambda t: pObj ('chordsym', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
219 chord_root.setParseAction (lambda t: pObj ('root', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
220 chord_kind.setParseAction (lambda t: pObj ('kind', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
221 chord_degree.setParseAction (lambda t: pObj ('degree', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
222 chord_bass.setParseAction (lambda t: pObj ('bass', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
223 text_expression.setParseAction (lambda t: pObj ('text', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
224 inline_field.setParseAction (lambda t: pObj ('inline', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
225 lyr_fld.setParseAction (lambda t: pObj ('lyr_fld', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
226 lyr_blk.setParseAction (lambda t: pObj ('lyr_blk', t, 1)) # 1 = keep ordered list of lyric lines
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
227 grace_notes.setParseAction (doGrace)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
228 acciaccatura.setParseAction (lambda t: pObj ('accia', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
229 note.setParseAction (noteActn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
230 rest.setParseAction (restActn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
231 decorations.setParseAction (lambda t: pObj ('deco', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
232 pizzicato.setParseAction (lambda t: ['!plus!']) # translate !+!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
233 slur_ends.setParseAction (lambda t: pObj ('slurs', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
234 chord.setParseAction (lambda t: pObj ('chord', t, 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
235 dec_note.setParseAction (noteActn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
236 tie.setParseAction (lambda t: pObj ('tie', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
237 pitch.setParseAction (lambda t: pObj ('pitch', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
238 bare_volta.setParseAction (lambda t: ['|']) # return barline that user forgot
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
239 dashed_barline.setParseAction (lambda t: ['.|'])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
240 bar_right.setParseAction (lambda t: pObj ('rbar', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
241 bar_left.setParseAction (lambda t: pObj ('lbar', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
242 broken.setParseAction (lambda t: pObj ('broken', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
243 tuplet_start.setParseAction (lambda t: pObj ('tup', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
244 linebreak.setParseAction (lambda t: pObj ('linebrk', t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
245 measure.setParseAction (doMaat)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
246 noBarMeasure.setParseAction (doMaat)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
247 b1.setParseAction (errorWarn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
248 b2.setParseAction (errorWarn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
249 b3.setParseAction (errorWarn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
250 errors.setParseAction (errorWarn)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
251
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
252 return abc_header, abc_voice, abc_scoredef, abc_percmap
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
253
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
254 class pObj (object): # every relevant parse result is converted into a pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
255 def __init__ (s, name, t, seq=0): # t = list of nested parse results
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
256 s.name = name # name uniqueliy identifies this pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
257 rest = [] # collect parse results that are not a pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
258 attrs = {} # new attributes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
259 for x in t: # nested pObj's become attributes of this pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
260 if type (x) == pObj:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
261 attrs [x.name] = attrs.get (x.name, []) + [x]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
262 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
263 rest.append (x) # collect non-pObj's (mostly literals)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
264 for name, xs in attrs.items ():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
265 if len (xs) == 1: xs = xs[0] # only list if more then one pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
266 setattr (s, name, xs) # create the new attributes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
267 s.t = rest # all nested non-pObj's (mostly literals)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
268 s.objs = seq and t or [] # for nested ordered (lyric) pObj's
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
269
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
270 def __repr__ (s): # make a nice string representation of a pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
271 r = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
272 for nm in dir (s):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
273 if nm.startswith ('_'): continue # skip build in attributes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
274 elif nm == 'name': continue # redundant
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
275 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
276 x = getattr (s, nm)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
277 if not x: continue # s.t may be empty (list of non-pObj's)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
278 if type (x) == list_type: r.extend (x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
279 else: r.append (x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
280 xs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
281 for x in r: # recursively call __repr__ and convert all strings to latin-1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
282 if isinstance (x, str_type): xs.append (x) # string -> no recursion
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
283 else: xs.append (repr (x)) # pObj -> recursive call
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
284 return '(' + s.name + ' ' +','.join (xs) + ')'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
285
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
286 global prevloc # global to remember previous match position of a note/rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
287 prevloc = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
288 def detectBeamBreak (line, loc, t):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
289 global prevloc # location in string 'line' of previous note match
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
290 xs = line[prevloc:loc+1] # string between previous and current note match
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
291 xs = xs.lstrip () # first note match starts on a space!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
292 prevloc = loc # location in string 'line' of current note match
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
293 b = pObj ('bbrk', [' ' in xs]) # space somewhere between two notes -> beambreak
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
294 t.insert (0, b) # insert beambreak as a nested parse result
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
295
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
296 def noteActn (line, loc, t): # detect beambreak between previous and current note/rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
297 if 'y' in t[0].t: return [] # discard spacer
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
298 detectBeamBreak (line, loc, t) # adds beambreak to parse result t as side effect
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
299 return pObj ('note', t)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
300
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
301 def restActn (line, loc, t): # detect beambreak between previous and current note/rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
302 detectBeamBreak (line, loc, t) # adds beambreak to parse result t as side effect
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
303 return pObj ('rest', t)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
304
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
305 def errorWarn (line, loc, t): # warning for misplaced symbols and skip them
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
306 if not t[0]: return [] # only warn if catched string not empty
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
307 info ('**misplaced symbol: %s' % t[0], warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
308 lineCopy = line [:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
309 if loc > 40:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
310 lineCopy = line [loc - 40: loc + 40]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
311 loc = 40
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
312 info (lineCopy.replace ('\n', ' '), warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
313 info (loc * '-' + '^', warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
314 return []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
315
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
316 #-------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
317 # transformations of a measure (called by parse action doMaat)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
318 #-------------------------------------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
319
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
320 def simplify (a, b): # divide a and b by their greatest common divisor
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
321 x, y = a, b
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
322 while b: a, b = b, a % b
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
323 return x // a, y // a
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
324
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
325 def doBroken (prev, brk, x):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
326 if not prev: info ('error in broken rhythm: %s' % x); return # no changes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
327 nom1, den1 = prev.dur.t # duration of first note/chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
328 nom2, den2 = x.dur.t # duration of second note/chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
329 if brk == '>':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
330 nom1, den1 = simplify (3 * nom1, 2 * den1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
331 nom2, den2 = simplify (1 * nom2, 2 * den2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
332 elif brk == '<':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
333 nom1, den1 = simplify (1 * nom1, 2 * den1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
334 nom2, den2 = simplify (3 * nom2, 2 * den2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
335 elif brk == '>>':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
336 nom1, den1 = simplify (7 * nom1, 4 * den1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
337 nom2, den2 = simplify (1 * nom2, 4 * den2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
338 elif brk == '<<':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
339 nom1, den1 = simplify (1 * nom1, 4 * den1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
340 nom2, den2 = simplify (7 * nom2, 4 * den2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
341 else: return # give up
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
342 prev.dur.t = nom1, den1 # change duration of previous note/chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
343 x.dur.t = nom2, den2 # and current note/chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
344
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
345 def convertBroken (t): # convert broken rhythms to normal note durations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
346 prev = None # the last note/chord before the broken symbol
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
347 brk = '' # the broken symbol
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
348 remove = [] # indexes to broken symbols (to be deleted) in measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
349 for i, x in enumerate (t): # scan all elements in measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
350 if x.name == 'note' or x.name == 'chord' or x.name == 'rest':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
351 if brk: # a broken symbol was encountered before
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
352 doBroken (prev, brk, x) # change duration previous note/chord/rest and current one
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
353 brk = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
354 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
355 prev = x # remember the last note/chord/rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
356 elif x.name == 'broken':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
357 brk = x.t[0] # remember the broken symbol (=string)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
358 remove.insert (0, i) # and its index, highest index first
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
359 for i in remove: del t[i] # delete broken symbols from high to low
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
360
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
361 def ptc2midi (n): # convert parsed pitch attribute to a midi number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
362 pt = getattr (n, 'pitch', '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
363 if pt:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
364 p = pt.t
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
365 if len (p) == 3: acc, step, oct = p
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
366 else: acc = ''; step, oct = p
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
367 nUp = step.upper ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
368 oct = (4 if nUp == step else 5) + int (oct)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
369 midi = oct * 12 + [0,2,4,5,7,9,11]['CDEFGAB'.index (nUp)] + {'^':1,'_':-1}.get (acc, 0) + 12
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
370 else: midi = 130 # all non pitch objects first
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
371 return midi
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
372
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
373 def convertChord (t): # convert chord to sequence of notes in musicXml-style
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
374 ins = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
375 for i, x in enumerate (t):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
376 if x.name == 'chord':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
377 if hasattr (x, 'rest') and not hasattr (x, 'note'): # chords containing only rests
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
378 if type (x.rest) == list_type: x.rest = x.rest[0] # more rests == one rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
379 ins.insert (0, (i, [x.rest])) # just output a single rest, no chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
380 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
381 num1, den1 = x.dur.t # chord duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
382 tie = getattr (x, 'tie', None) # chord tie
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
383 slurs = getattr (x, 'slurs', []) # slur endings
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
384 if type (x.note) != list_type: x.note = [x.note] # when chord has only one note ...
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
385 elms = []; j = 0 # sort chord notes, highest first
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
386 nss = sorted (x.objs, key = ptc2midi, reverse=1) if mxm.orderChords else x.objs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
387 for nt in nss: # all chord elements (note | decorations | rest | grace note)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
388 if nt.name == 'note':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
389 num2, den2 = nt.dur.t # note duration * chord duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
390 nt.dur.t = simplify (num1 * num2, den1 * den2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
391 if tie: nt.tie = tie # tie on all chord notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
392 if j == 0 and slurs: nt.slurs = slurs # slur endings only on first chord note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
393 if j > 0: nt.chord = pObj ('chord', [1]) # label all but first as chord notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
394 else: # remember all pitches of the chord in the first note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
395 pitches = [n.pitch for n in x.note] # to implement conversion of erroneous ties to slurs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
396 nt.pitches = pObj ('pitches', pitches)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
397 j += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
398 if nt.name not in ['dur','tie','slurs','rest']: elms.append (nt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
399 ins.insert (0, (i, elms)) # chord position, [note|decotation|grace note]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
400 for i, notes in ins: # insert from high to low
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
401 for nt in reversed (notes):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
402 t.insert (i+1, nt) # insert chord notes after chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
403 del t[i] # remove chord itself
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
404
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
405 def doMaat (t): # t is a Group() result -> the measure is in t[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
406 convertBroken (t[0]) # remove all broken rhythms and convert to normal durations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
407 convertChord (t[0]) # replace chords by note sequences in musicXML style
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
408
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
409 def doGrace (t): # t is a Group() result -> the grace sequence is in t[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
410 convertChord (t[0]) # a grace sequence may have chords
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
411 for nt in t[0]: # flag all notes within the grace sequence
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
412 if nt.name == 'note': nt.grace = 1 # set grace attribute
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
413 return t[0] # ungroup the parse result
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
414 #--------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
415 # musicXML generation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
416 #----------------------------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
417
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
418 def compChordTab (): # avoid some typing work: returns mapping constant {ABC chordsyms -> musicXML kind}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
419 maj, min, aug, dim, dom, ch7, ch6, ch9, ch11, ch13, hd = 'major minor augmented diminished dominant -seventh -sixth -ninth -11th -13th half-diminished'.split ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
420 triad = zip ('ma Maj maj M mi min m aug dim o + -'.split (), [maj, maj, maj, maj, min, min, min, aug, dim, dim, aug, min])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
421 seventh = zip ('7 ma7 Maj7 M7 maj7 mi7 min7 m7 dim7 o7 -7 aug7 +7 m7b5 mi7b5'.split (),
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
422 [dom, maj+ch7, maj+ch7, maj+ch7, maj+ch7, min+ch7, min+ch7, min+ch7, dim+ch7, dim+ch7, min+ch7, aug+ch7, aug+ch7, hd, hd])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
423 sixth = zip ('6 ma6 M6 mi6 min6 m6'.split (), [maj+ch6, maj+ch6, maj+ch6, min+ch6, min+ch6, min+ch6])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
424 ninth = zip ('9 ma9 M9 maj9 Maj9 mi9 min9 m9'.split (), [dom+ch9, maj+ch9, maj+ch9, maj+ch9, maj+ch9, min+ch9, min+ch9, min+ch9])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
425 elevn = zip ('11 ma11 M11 maj11 Maj11 mi11 min11 m11'.split (), [dom+ch11, maj+ch11, maj+ch11, maj+ch11, maj+ch11, min+ch11, min+ch11, min+ch11])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
426 thirt = zip ('13 ma13 M13 maj13 Maj13 mi13 min13 m13'.split (), [dom+ch13, maj+ch13, maj+ch13, maj+ch13, maj+ch13, min+ch13, min+ch13, min+ch13])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
427 sus = zip ('sus sus4 sus2'.split (), ['suspended-fourth', 'suspended-fourth', 'suspended-second'])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
428 return dict (list (triad) + list (seventh) + list (sixth) + list (ninth) + list (elevn) + list (thirt) + list (sus))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
429
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
430 def addElem (parent, child, level):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
431 indent = 2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
432 chldrn = list (parent)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
433 if chldrn:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
434 chldrn[-1].tail += indent * ' '
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
435 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
436 parent.text = '\n' + level * indent * ' '
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
437 parent.append (child)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
438 child.tail = '\n' + (level-1) * indent * ' '
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
439
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
440 def addElemT (parent, tag, text, level):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
441 e = E.Element (tag)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
442 e.text = text
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
443 addElem (parent, e, level)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
444 return e
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
445
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
446 def mkTmod (tmnum, tmden, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
447 tmod = E.Element ('time-modification')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
448 addElemT (tmod, 'actual-notes', str (tmnum), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
449 addElemT (tmod, 'normal-notes', str (tmden), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
450 return tmod
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
451
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
452 def addDirection (parent, elems, lev, gstaff, subelms=[], placement='below', cue_on=0):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
453 dir = E.Element ('direction', placement=placement)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
454 addElem (parent, dir, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
455 if type (elems) != list_type: elems = [(elems, subelms)] # ugly hack to provide for multiple direction types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
456 for elem, subelms in elems: # add direction types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
457 typ = E.Element ('direction-type')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
458 addElem (dir, typ, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
459 addElem (typ, elem, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
460 for subel in subelms: addElem (elem, subel, lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
461 if cue_on: addElem (dir, E.Element ('level', size='cue'), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
462 if gstaff: addElemT (dir, 'staff', str (gstaff), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
463 return dir
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
464
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
465 def removeElems (root_elem, parent_str, elem_str):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
466 for p in root_elem.findall (parent_str):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
467 e = p.find (elem_str)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
468 if e != None: p.remove (e)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
469
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
470 def alignLyr (vce, lyrs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
471 empty_el = pObj ('leeg', '*')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
472 for k, lyr in enumerate (lyrs): # lyr = one full line of lyrics
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
473 i = 0 # syl counter
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
474 for elem in vce: # reiterate the voice block for each lyrics line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
475 if elem.name == 'note' and not (hasattr (elem, 'chord') or hasattr (elem, 'grace')):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
476 if i >= len (lyr): lr = empty_el
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
477 else: lr = lyr [i]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
478 lr.t[0] = lr.t[0].replace ('%5d',']')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
479 elem.objs.append (lr)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
480 if lr.name != 'sbar': i += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
481 if elem.name == 'rbar' and i < len (lyr) and lyr[i].name == 'sbar': i += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
482 return vce
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
483
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
484 slur_move = re.compile (r'(?<![!+])([}><][<>]?)(\)+)') # (?<!...) means: not preceeded by ...
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
485 mm_rest = re.compile (r'([XZ])(\d+)')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
486 bar_space = re.compile (r'([:|][ |\[\]]+[:|])') # barlines with spaces
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
487 def fixSlurs (x): # repair slurs when after broken sign or grace-close
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
488 def f (mo): # replace a multi-measure rest by single measure rests
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
489 n = int (mo.group (2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
490 return (n * (mo.group (1) + '|')) [:-1]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
491 def g (mo): # squash spaces in barline expressions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
492 return mo.group (1).replace (' ','')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
493 x = mm_rest.sub (f, x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
494 x = bar_space.sub (g, x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
495 return slur_move.sub (r'\2\1', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
496
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
497 def splitHeaderVoices (abctext):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
498 escField = lambda x: '[' + x.replace (']',r'%5d') + ']' # hope nobody uses %5d in a field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
499 r1 = re.compile (r'%.*$') # comments
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
500 r2 = re.compile (r'^([A-Zw]:.*$)|\[[A-Zw]:[^]]*]$') # information field, including lyrics
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
501 r3 = re.compile (r'^%%(?=[^%])') # directive: ^%% folowed by not a %
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
502 xs, nx, mcont, fcont = [], 0, 0, 0 # result lines, X-encountered, music continuation, field continuation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
503 mln = fln = '' # music line, field line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
504 for x in abctext.splitlines ():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
505 x = x.strip ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
506 if not x and nx == 1: break # end of tune (empty line)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
507 if x.startswith ('X:'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
508 if nx == 1: break # second tune starts without an empty line !!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
509 nx = 1 # start first tune
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
510 x = r3.sub ('I:', x) # replace %% -> I:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
511 x2 = r1.sub ('', x) # remove comment
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
512 while x2.endswith ('*') and not (x2.startswith ('w:') or x2.startswith ('+:') or 'percmap' in x2):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
513 x2 = x2[:-1] # remove old syntax for right adjusting
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
514 if not x2: continue # empty line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
515 if x2[:2] == 'W:':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
516 field = x2 [2:].strip ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
517 ftype = mxm.metaMap.get ('W', 'W') # respect the (user defined --meta) mapping of various ABC fields to XML meta data types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
518 c = mxm.metadata.get (ftype, '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
519 mxm.metadata [ftype] = c + '\n' + field if c else field # concatenate multiple info fields with new line as separator
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
520 continue # skip W: lyrics
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
521 if x2[:2] == '+:': # field continuation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
522 fln += x2[2:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
523 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
524 ro = r2.match (x2) # single field on a line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
525 if ro: # field -> inline_field, escape all ']'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
526 if fcont: # old style \-info-continuation active
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
527 fcont = x2 [-1] == '\\' # possible further \-info-continuation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
528 fln += re.sub (r'^.:(.*?)\\*$', r'\1', x2) # add continuation, remove .: and \
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
529 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
530 if fln: mln += escField (fln)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
531 if x2.startswith ('['): x2 = x2.strip ('[]')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
532 fcont = x2 [-1] == '\\' # first encounter of old style \-info-continuation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
533 fln = x2.rstrip ('\\') # remove continuation from field and inline brackets
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
534 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
535 if nx == 1: # x2 is a new music line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
536 fcont = 0 # stop \-continuations (-> only adjacent \-info-continuations are joined)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
537 if fln:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
538 mln += escField (fln)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
539 fln = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
540 if mcont:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
541 mcont = x2 [-1] == '\\'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
542 mln += x2.rstrip ('\\')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
543 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
544 if mln: xs.append (mln); mln = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
545 mcont = x2 [-1] == '\\'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
546 mln = x2.rstrip ('\\')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
547 if not mcont: xs.append (mln); mln = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
548 if fln: mln += escField (fln)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
549 if mln: xs.append (mln)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
550
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
551 hs = re.split (r'(\[K:[^]]*\])', xs [0]) # look for end of header K:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
552 if len (hs) == 1: header = hs[0]; xs [0] = '' # no K: present
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
553 else: header = hs [0] + hs [1]; xs [0] = ''.join (hs[2:]) # h[1] is the first K:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
554 abctext = '\n'.join (xs) # the rest is body text
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
555 hfs, vfs = [], []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
556 for x in header[1:-1].split (']['):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
557 if x[0] == 'V': vfs.append (x) # filter voice- and midi-definitions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
558 elif x[:6] == 'I:MIDI': vfs.append (x) # from the header to vfs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
559 elif x[:9] == 'I:percmap': vfs.append (x) # and also percmap
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
560 else: hfs.append (x) # all other fields stay in header
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
561 header = '[' + ']['.join (hfs) + ']' # restore the header
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
562 abctext = ('[' + ']['.join (vfs) + ']' if vfs else '') + abctext # prepend voice/midi from header before abctext
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
563
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
564 xs = abctext.split ('[V:')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
565 if len (xs) == 1: abctext = '[V:1]' + abctext # abc has no voice defs at all
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
566 elif re.sub (r'\[[A-Z]:[^]]*\]', '', xs[0]).strip (): # remove inline fields from starting text, if any
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
567 abctext = '[V:1]' + abctext # abc with voices has no V: at start
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
568
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
569 r1 = re.compile (r'\[V:\s*(\S*)[ \]]') # get voice id from V: field (skip spaces betwee V: and ID)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
570 vmap = {} # {voice id -> [voice abc string]}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
571 vorder = {} # mark document order of voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
572 xs = re.split (r'(\[V:[^]]*\])', abctext) # split on every V-field (V-fields included in split result list)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
573 if len (xs) == 1: raise ValueError ('bugs ...')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
574 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
575 pm = re.findall (r'\[P:.\]', xs[0]) # all P:-marks after K: but before first V:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
576 if pm: xs[2] = ''.join (pm) + xs[2] # prepend P:-marks to the text of the first voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
577 header += re.sub (r'\[P:.\]', '', xs[0]) # clear all P:-marks from text between K: and first V: and put text in the header
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
578 i = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
579 while i < len (xs): # xs = ['', V-field, voice abc, V-field, voice abc, ...]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
580 vce, abc = xs[i:i+2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
581 id = r1.search (vce).group (1) # get voice ID from V-field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
582 if not id: id, vce = '1', '[V:1]' # voice def has no ID
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
583 vmap[id] = vmap.get (id, []) + [vce, abc] # collect abc-text for each voice id (include V-fields)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
584 if id not in vorder: vorder [id] = i # store document order of first occurrence of voice id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
585 i += 2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
586 voices = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
587 ixs = sorted ([(i, id) for id, i in vorder.items ()]) # restore document order of voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
588 for i, id in ixs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
589 voice = ''.join (vmap [id]) # all abc of one voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
590 voice = fixSlurs (voice) # put slurs right after the notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
591 voices.append ((id, voice))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
592 return header, voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
593
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
594 def mergeMeasure (m1, m2, slur_offset, voice_offset, rOpt, is_grand=0, is_overlay=0):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
595 slurs = m2.findall ('note/notations/slur')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
596 for slr in slurs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
597 slrnum = int (slr.get ('number')) + slur_offset
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
598 slr.set ('number', str (slrnum)) # make unique slurnums in m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
599 vs = m2.findall ('note/voice') # set all voice number elements in m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
600 for v in vs: v.text = str (voice_offset + int (v.text))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
601 ls = m1.findall ('note/lyric') # all lyric elements in m1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
602 lnum_max = max ([int (l.get ('number')) for l in ls] + [0]) # highest lyric number in m1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
603 ls = m2.findall ('note/lyric') # update lyric elements in m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
604 for el in ls:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
605 n = int (el.get ('number'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
606 el.set ('number', str (n + lnum_max))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
607 ns = m1.findall ('note') # determine the total duration of m1, subtract all backups
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
608 dur1 = sum (int (n.find ('duration').text) for n in ns
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
609 if n.find ('grace') == None and n.find ('chord') == None)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
610 dur1 -= sum (int (b.text) for b in m1.findall ('backup/duration'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
611 repbar, nns, es = 0, 0, [] # nns = number of real notes in m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
612 for e in list (m2): # scan all elements of m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
613 if e.tag == 'attributes':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
614 if not is_grand: continue # no attribute merging for normal voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
615 else: nns += 1 # but we do merge (clef) attributes for a grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
616 if e.tag == 'print': continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
617 if e.tag == 'note' and (rOpt or e.find ('rest') == None): nns += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
618 if e.tag == 'barline' and e.find ('repeat') != None: repbar = e;
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
619 es.append (e) # buffer elements to be merged
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
620 if nns > 0: # only merge if m2 contains any real notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
621 if dur1 > 0: # only insert backup if duration of m1 > 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
622 b = E.Element ('backup')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
623 addElem (m1, b, level=3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
624 addElemT (b, 'duration', str (dur1), level=4)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
625 for e in es: addElem (m1, e, level=3) # merge buffered elements of m2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
626 elif is_overlay and repbar: addElem (m1, repbar, level=3) # merge repeat in empty overlay
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
627
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
628 def mergePartList (parts, rOpt, is_grand=0): # merge parts, make grand staff when is_grand true
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
629
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
630 def delAttrs (part): # for the time being we only keep clef attributes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
631 xs = [(m, e) for m in part.findall ('measure') for e in m.findall ('attributes')]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
632 for m, e in xs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
633 for c in list (e):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
634 if c.tag == 'clef': continue # keep clef attribute
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
635 if c.tag == 'staff-details': continue # keep staff-details attribute
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
636 e.remove (c) # delete all other attrinutes for higher staff numbers
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
637 if len (list (e)) == 0: m.remove (e) # remove empty attributes element
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
638
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
639 p1 = parts[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
640 for p2 in parts[1:]:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
641 if is_grand: delAttrs (p2) # delete all attributes except clef
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
642 for i in range (len (p1) + 1, len (p2) + 1): # second part longer than first one
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
643 maat = E.Element ('measure', number = str(i)) # append empty measures
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
644 addElem (p1, maat, 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
645 slurs = p1.findall ('measure/note/notations/slur') # find highest slur num in first part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
646 slur_max = max ([int (slr.get ('number')) for slr in slurs] + [0])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
647 vs = p1.findall ('measure/note/voice') # all voice number elements in first part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
648 vnum_max = max ([int (v.text) for v in vs] + [0]) # highest voice number in first part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
649 for im, m2 in enumerate (p2.findall ('measure')): # merge all measures of p2 into p1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
650 mergeMeasure (p1[im], m2, slur_max, vnum_max, rOpt, is_grand) # may change slur numbers in p1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
651 return p1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
652
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
653 def mergeParts (parts, vids, staves, rOpt, is_grand=0):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
654 if not staves: return parts, vids # no voice mapping
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
655 partsnew, vidsnew = [], []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
656 for voice_ids in staves:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
657 pixs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
658 for vid in voice_ids:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
659 if vid in vids: pixs.append (vids.index (vid))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
660 else: info ('score partname %s does not exist' % vid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
661 if pixs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
662 xparts = [parts[pix] for pix in pixs]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
663 if len (xparts) > 1: mergedpart = mergePartList (xparts, rOpt, is_grand)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
664 else: mergedpart = xparts [0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
665 partsnew.append (mergedpart)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
666 vidsnew.append (vids [pixs[0]])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
667 return partsnew, vidsnew
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
668
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
669 def mergePartMeasure (part, msre, ovrlaynum, rOpt): # merge msre into last measure of part, only for overlays
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
670 slur_offset = 0; # slur numbers determined by the slurstack size (as in a single voice)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
671 last_msre = list (part)[-1] # last measure in part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
672 mergeMeasure (last_msre, msre, slur_offset, ovrlaynum, rOpt, is_overlay=1) # voice offset = s.overlayVNum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
673
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
674 def pushSlur (boogStapel, stem):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
675 if stem not in boogStapel: boogStapel [stem] = [] # initialize slurstack for stem
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
676 boognum = sum (map (len, boogStapel.values ())) + 1 # number of open slurs in all (overlay) voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
677 boogStapel [stem].append (boognum)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
678 return boognum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
679
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
680 def setFristVoiceNameFromGroup (vids, vdefs): # vids = [vid], vdef = {vid -> (name, subname, voicedef)}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
681 vids = [v for v in vids if v in vdefs] # only consider defined voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
682 if not vids: return vdefs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
683 vid0 = vids [0] # first vid of the group
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
684 _, _, vdef0 = vdefs [vid0] # keep de voice definition (vdef0) when renaming vid0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
685 for vid in vids:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
686 nm, snm, vdef = vdefs [vid]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
687 if nm: # first non empty name encountered will become
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
688 vdefs [vid0] = nm, snm, vdef0 # name of merged group == name of first voice in group (vid0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
689 break
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
690 return vdefs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
691
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
692 def mkGrand (p, vdefs): # transform parse subtree into list needed for s.grands
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
693 xs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
694 for i, x in enumerate (p.objs): # changing p.objs [i] alters the tree. changing x has no effect on the tree.
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
695 if type (x) == pObj:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
696 us = mkGrand (x, vdefs) # first get transformation results of current pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
697 if x.name == 'grand': # x.objs contains ordered list of nested parse results within x
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
698 vids = [y.objs[0] for y in x.objs[1:]] # the voice ids in the grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
699 nms = [vdefs [u][0] for u in vids if u in vdefs] # the names of those voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
700 accept = sum ([1 for nm in nms if nm]) == 1 # accept as grand staff when only one of the voices has a name
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
701 if accept or us[0] == '{*':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
702 xs.append (us[1:]) # append voice ids as a list (discard first item '{' or '{*')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
703 vdefs = setFristVoiceNameFromGroup (vids, vdefs)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
704 p.objs [i] = x.objs[1] # replace voices by first one in the grand group (this modifies the parse tree)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
705 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
706 xs.extend (us[1:]) # extend current result with all voice ids of rejected grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
707 else: xs.extend (us) # extend current result with transformed pObj
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
708 else: xs.append (p.t[0]) # append the non pObj (== voice id string)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
709 return xs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
710
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
711 def mkStaves (p, vdefs): # transform parse tree into list needed for s.staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
712 xs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
713 for i, x in enumerate (p.objs): # structure and comments identical to mkGrand
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
714 if type (x) == pObj:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
715 us = mkStaves (x, vdefs)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
716 if x.name == 'voicegr':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
717 xs.append (us)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
718 vids = [y.objs[0] for y in x.objs]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
719 vdefs = setFristVoiceNameFromGroup (vids, vdefs)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
720 p.objs [i] = x.objs[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
721 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
722 xs.extend (us)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
723 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
724 if p.t[0] not in '{*': xs.append (p.t[0])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
725 return xs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
726
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
727 def mkGroups (p): # transform parse tree into list needed for s.groups
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
728 xs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
729 for x in p.objs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
730 if type (x) == pObj:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
731 if x.name == 'vid': xs.extend (mkGroups (x))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
732 elif x.name == 'bracketgr': xs.extend (['['] + mkGroups (x) + [']'])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
733 elif x.name == 'bracegr': xs.extend (['{'] + mkGroups (x) + ['}'])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
734 else: xs.extend (mkGroups (x) + ['}']) # x.name == 'grand' == rejected grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
735 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
736 xs.append (p.t[0])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
737 return xs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
738
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
739 def stepTrans (step, soct, clef): # [A-G] (1...8)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
740 if clef.startswith ('bass'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
741 nm7 = 'C,D,E,F,G,A,B'.split (',')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
742 n = 14 + nm7.index (step) - 12 # two octaves extra to avoid negative numbers
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
743 step, soct = nm7 [n % 7], soct + n // 7 - 2 # subtract two octaves again
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
744 return step, soct
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
745
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
746 def reduceMids (parts, vidsnew, midiInst): # remove redundant instruments from a part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
747 for pid, part in zip (vidsnew, parts):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
748 mids, repls, has_perc = {}, {}, 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
749 for ipid, ivid, ch, prg, vol, pan in sorted (list (midiInst.values ())):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
750 if ipid != pid: continue # only instruments from part pid
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
751 if ch == '10': has_perc = 1; continue # only consider non percussion instruments
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
752 instId, inst = 'I%s-%s' % (ipid, ivid), (ch, prg)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
753 if inst in mids: # midi instrument already defined in this part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
754 repls [instId] = mids [inst] # remember to replace instId by inst (see below)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
755 del midiInst [instId] # instId is redundant
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
756 else: mids [inst] = instId # collect unique instruments in this part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
757 if len (mids) < 2 and not has_perc: # only one instrument used -> no instrument tags needed in notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
758 removeElems (part, 'measure/note', 'instrument') # no instrument tag needed for one- or no-instrument parts
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
759 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
760 for e in part.findall ('measure/note/instrument'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
761 id = e.get ('id') # replace all redundant instrument Id's
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
762 if id in repls: e.set ('id', repls [id])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
763
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
764 class stringAlloc:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
765 def __init__ (s):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
766 s.snaarVrij = [] # [[(t1, t2) ...] for each string ]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
767 s.snaarIx = [] # index in snaarVrij for each string
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
768 s.curstaff = -1 # staff being allocated
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
769 def beginZoek (s): # reset snaarIx at start of each voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
770 s.snaarIx = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
771 for i in range (len (s.snaarVrij)): s.snaarIx.append (0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
772 def setlines (s, stflines, stfnum):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
773 if stfnum != s.curstaff: # initialize for new staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
774 s.curstaff = stfnum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
775 s.snaarVrij = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
776 for i in range (stflines): s.snaarVrij.append ([])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
777 s.beginZoek ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
778 def isVrij (s, snaar, t1, t2): # see if string snaar is free between t1 and t2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
779 xs = s.snaarVrij [snaar]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
780 for i in range (s.snaarIx [snaar], len (xs)):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
781 tb, te = xs [i]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
782 if t1 >= te: continue # te_prev < t1 <= te
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
783 if t1 >= tb: s.snaarIx [snaar] = i; return 0 # tb <= t1 < te
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
784 if t2 > tb: s.snaarIx [snaar] = i; return 0 # t1 < tb < t2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
785 s.snaarIx [snaar] = i; # remember position for next call
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
786 xs.insert (i, (t1,t2)) # te_prev < t1 < t2 < tb
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
787 return 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
788 xs.append ((t1,t2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
789 s.snaarIx [snaar] = len (xs) - 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
790 return 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
791 def bezet (s, snaar, t1, t2): # force allocation of note (t1,t2) on string snaar
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
792 xs = s.snaarVrij [snaar]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
793 for i, (tb, te) in enumerate (xs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
794 if t1 >= te: continue # te_prev < t1 <= te
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
795 xs.insert (i, (t1, t2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
796 return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
797 xs.append ((t1,t2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
798
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
799 class MusicXml:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
800 typeMap = {1:'long', 2:'breve', 4:'whole', 8:'half', 16:'quarter', 32:'eighth', 64:'16th', 128:'32nd', 256:'64th'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
801 dynaMap = {'p':1,'pp':1,'ppp':1,'pppp':1,'f':1,'ff':1,'fff':1,'ffff':1,'mp':1,'mf':1,'sfz':1}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
802 tempoMap = {'larghissimo':40, 'moderato':104, 'adagissimo':44, 'allegretto':112, 'lentissimo':48, 'allegro':120, 'largo':56,
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
803 'vivace':168, 'adagio':59, 'vivo':180, 'lento':62, 'presto':192, 'larghetto':66, 'allegrissimo':208, 'adagietto':76,
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
804 'vivacissimo':220, 'andante':88, 'prestissimo':240, 'andantino':96}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
805 wedgeMap = {'>(':1, '>)':1, '<(':1,'<)':1,'crescendo(':1,'crescendo)':1,'diminuendo(':1,'diminuendo)':1}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
806 artMap = {'.':'staccato','>':'accent','accent':'accent','wedge':'staccatissimo','tenuto':'tenuto',
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
807 'breath':'breath-mark','marcato':'strong-accent','^':'strong-accent','slide':'scoop'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
808 ornMap = {'trill':'trill-mark','T':'trill-mark','turn':'turn','uppermordent':'inverted-mordent','lowermordent':'mordent',
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
809 'pralltriller':'inverted-mordent','mordent':'mordent','turn':'turn','invertedturn':'inverted-turn'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
810 tecMap = {'upbow':'up-bow', 'downbow':'down-bow', 'plus':'stopped','open':'open-string','snap':'snap-pizzicato',
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
811 'thumb':'thumb-position'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
812 capoMap = {'fine':('Fine','fine','yes'), 'D.S.':('D.S.','dalsegno','segno'), 'D.C.':('D.C.','dacapo','yes'),'dacapo':('D.C.','dacapo','yes'),
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
813 'dacoda':('To Coda','tocoda','coda'), 'coda':('coda','coda','coda'), 'segno':('segno','segno','segno')}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
814 sharpness = ['Fb', 'Cb','Gb','Db','Ab','Eb','Bb','F','C','G','D','A', 'E', 'B', 'F#','C#','G#','D#','A#','E#','B#']
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
815 offTab = {'maj':8, 'm':11, 'min':11, 'mix':9, 'dor':10, 'phr':12, 'lyd':7, 'loc':13}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
816 modTab = {'maj':'major', 'm':'minor', 'min':'minor', 'mix':'mixolydian', 'dor':'dorian', 'phr':'phrygian', 'lyd':'lydian', 'loc':'locrian'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
817 clefMap = { 'alto1':('C','1'), 'alto2':('C','2'), 'alto':('C','3'), 'alto4':('C','4'), 'tenor':('C','4'),
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
818 'bass3':('F','3'), 'bass':('F','4'), 'treble':('G','2'), 'perc':('percussion',''), 'none':('',''), 'tab':('TAB','5')}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
819 clefLineMap = {'B':'treble', 'G':'alto1', 'E':'alto2', 'C':'alto', 'A':'tenor', 'F':'bass3', 'D':'bass'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
820 alterTab = {'=':'0', '_':'-1', '__':'-2', '^':'1', '^^':'2'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
821 accTab = {'=':'natural', '_':'flat', '__':'flat-flat', '^':'sharp', '^^':'sharp-sharp'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
822 chordTab = compChordTab ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
823 uSyms = {'~':'roll', 'H':'fermata','L':'>','M':'lowermordent','O':'coda',
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
824 'P':'uppermordent','S':'segno','T':'trill','u':'upbow','v':'downbow'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
825 pageFmtDef = [0.75,297,210,18,18,10,10] # the abcm2ps page formatting defaults for A4
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
826 metaTab = {'O':'origin', 'A':'area', 'Z':'transcription', 'N':'notes', 'G':'group', 'H':'history', 'R':'rhythm',
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
827 'B':'book', 'D':'discography', 'F':'fileurl', 'S':'source', 'P':'partmap', 'W':'lyrics'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
828 metaMap = {'C':'composer'} # mapping of composer is fixed
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
829 metaTypes = {'composer':1,'lyricist':1,'poet':1,'arranger':1,'translator':1, 'rights':1} # valid MusicXML meta data types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
830 tuningDef = 'E2,A2,D3,G3,B3,E4'.split (',') # default string tuning (guitar)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
831
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
832 def __init__ (s):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
833 s.pageFmtCmd = [] # set by command line option -p
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
834 s.reset ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
835 def reset (s, fOpt=False):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
836 s.divisions = 2520 # xml duration of 1/4 note, 2^3 * 3^2 * 5 * 7 => 5,7,9 tuplets
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
837 s.ties = {} # {abc pitch tuple -> alteration} for all open ties
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
838 s.slurstack = {} # stack of open slur numbers per (overlay) voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
839 s.slurbeg = [] # type of slurs to start (when slurs are detected at element-level)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
840 s.tmnum = 0 # time modification, numerator
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
841 s.tmden = 0 # time modification, denominator
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
842 s.ntup = 0 # number of tuplet notes remaining
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
843 s.trem = 0 # number of bars for tremolo
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
844 s.intrem = 0 # mark tremolo sequence (for duration doubling)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
845 s.tupnts = [] # all tuplet modifiers with corresp. durations: [(duration, modifier), ...]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
846 s.irrtup = 0 # 1 if an irregular tuplet
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
847 s.ntype = '' # the normal-type of a tuplet (== duration type of a normal tuplet note)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
848 s.unitL = (1, 8) # default unit length
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
849 s.unitLcur = (1, 8) # unit length of current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
850 s.keyAlts = {} # alterations implied by key
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
851 s.msreAlts = {} # temporarily alterations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
852 s.curVolta = '' # open volta bracket
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
853 s.title = '' # title of music
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
854 s.creator = {} # {creator-type -> creator string}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
855 s.metadata = {} # {metadata-type -> string}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
856 s.lyrdash = {} # {lyric number -> 1 if dash between syllables}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
857 s.usrSyms = s.uSyms # user defined symbols
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
858 s.prevNote = None # xml element of previous beamed note to correct beams (start, continue)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
859 s.prevLyric = {} # xml element of previous lyric to add/correct extend type (start, continue)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
860 s.grcbbrk = False # remember any bbrk in a grace sequence
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
861 s.linebrk = 0 # 1 if next measure should start with a line break
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
862 s.nextdecos = [] # decorations for the next note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
863 s.prevmsre = None # the previous measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
864 s.supports_tag = 0 # issue supports-tag in xml file when abc uses explicit linebreaks
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
865 s.staveDefs = [] # collected %%staves or %%score instructions from score
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
866 s.staves = [] # staves = [[voice names to be merged into one stave]]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
867 s.groups = [] # list of merged part names with interspersed {[ and }]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
868 s.grands = [] # [[vid1, vid2, ..], ...] voiceIds to be merged in a grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
869 s.gStaffNums = {} # map each voice id in a grand staff to a staff number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
870 s.gNstaves = {} # map each voice id in a grand staff to total number of staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
871 s.pageFmtAbc = [] # formatting from abc directives
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
872 s.mdur = (4,4) # duration of one measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
873 s.gtrans = 0 # octave transposition (by clef)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
874 s.midprg = ['', '', '', ''] # MIDI channel nr, program nr, volume, panning for the current part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
875 s.vid = '' # abc voice id for the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
876 s.pid = '' # xml part id for the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
877 s.gcue_on = 0 # insert <cue/> tag in each note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
878 s.percVoice = 0 # 1 if percussion enabled
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
879 s.percMap = {} # (part-id, abc_pitch, xml-octave) -> (abc staff step, midi note number, xml notehead)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
880 s.pMapFound = 0 # at least one I:percmap has been found
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
881 s.vcepid = {} # voice_id -> part_id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
882 s.midiInst = {} # inst_id -> (part_id, voice_id, channel, midi_number), remember instruments used
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
883 s.capo = 0 # fret position of the capodastro
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
884 s.tunmid = [] # midi numbers of strings
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
885 s.tunTup = [] # ordered midi numbers of strings [(midi_num, string_num), ...] (midi_num from high to low)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
886 s.fOpt = fOpt # force string/fret allocations for tab staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
887 s.orderChords = 0 # order notes in a chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
888 s.chordDecos = {} # decos that should be distributed to all chord notes for xml
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
889 ch10 = 'acoustic-bass-drum,35;bass-drum-1,36;side-stick,37;acoustic-snare,38;hand-clap,39;electric-snare,40;low-floor-tom,41;closed-hi-hat,42;high-floor-tom,43;pedal-hi-hat,44;low-tom,45;open-hi-hat,46;low-mid-tom,47;hi-mid-tom,48;crash-cymbal-1,49;high-tom,50;ride-cymbal-1,51;chinese-cymbal,52;ride-bell,53;tambourine,54;splash-cymbal,55;cowbell,56;crash-cymbal-2,57;vibraslap,58;ride-cymbal-2,59;hi-bongo,60;low-bongo,61;mute-hi-conga,62;open-hi-conga,63;low-conga,64;high-timbale,65;low-timbale,66;high-agogo,67;low-agogo,68;cabasa,69;maracas,70;short-whistle,71;long-whistle,72;short-guiro,73;long-guiro,74;claves,75;hi-wood-block,76;low-wood-block,77;mute-cuica,78;open-cuica,79;mute-triangle,80;open-triangle,81'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
890 s.percsnd = [x.split (',') for x in ch10.split (';')] # {name -> midi number} of standard channel 10 sound names
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
891 s.gTime = (0,0) # (XML begin time, XML end time) in divisions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
892 s.tabStaff = '' # == pid (part ID) for a tab staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
893
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
894 def mkPitch (s, acc, note, oct, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
895 if s.percVoice: # percussion map switched off by perc=off (see doClef)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
896 octq = int (oct) + s.gtrans # honour the octave= transposition when querying percmap
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
897 tup = s.percMap.get ((s.pid, acc+note, octq), s.percMap.get (('', acc+note, octq), 0))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
898 if tup: step, soct, midi, notehead = tup
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
899 else: step, soct = note, octq
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
900 octnum = (4 if step.upper() == step else 5) + int (soct)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
901 if not tup: # add percussion map for unmapped notes in this part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
902 midi = str (octnum * 12 + [0,2,4,5,7,9,11]['CDEFGAB'.index (step.upper())] + {'^':1,'_':-1}.get (acc, 0) + 12)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
903 notehead = {'^':'x', '_':'circle-x'}.get (acc, 'normal')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
904 if s.pMapFound: info ('no I:percmap for: %s%s in part %s, voice %s' % (acc+note, -oct*',' if oct<0 else oct*"'", s.pid, s.vid))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
905 s.percMap [(s.pid, acc+note, octq)] = (note, octq, midi, notehead)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
906 else: # correct step value for clef
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
907 step, octnum = stepTrans (step.upper (), octnum, s.curClef)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
908 pitch = E.Element ('unpitched')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
909 addElemT (pitch, 'display-step', step.upper (), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
910 addElemT (pitch, 'display-octave', str (octnum), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
911 return pitch, '', midi, notehead
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
912 nUp = note.upper ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
913 octnum = (4 if nUp == note else 5) + int (oct) + s.gtrans
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
914 pitch = E.Element ('pitch')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
915 addElemT (pitch, 'step', nUp, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
916 alter = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
917 if (note, oct) in s.ties:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
918 tied_alter, _, vnum, _ = s.ties [(note,oct)] # vnum = overlay voice number when tie started
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
919 if vnum == s.overlayVnum: alter = tied_alter # tied note in the same overlay -> same alteration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
920 elif acc:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
921 s.msreAlts [(nUp, octnum)] = s.alterTab [acc]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
922 alter = s.alterTab [acc] # explicit notated alteration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
923 elif (nUp, octnum) in s.msreAlts: alter = s.msreAlts [(nUp, octnum)] # temporary alteration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
924 elif nUp in s.keyAlts: alter = s.keyAlts [nUp] # alteration implied by the key
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
925 if alter: addElemT (pitch, 'alter', alter, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
926 addElemT (pitch, 'octave', str (octnum), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
927 return pitch, alter, '', ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
928
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
929 def getNoteDecos (s, n):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
930 decos = s.nextdecos # decorations encountered so far
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
931 ndeco = getattr (n, 'deco', 0) # possible decorations of notes of a chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
932 if ndeco: # add decorations, translate used defined symbols
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
933 decos += [s.usrSyms.get (d, d).strip ('!+') for d in ndeco.t]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
934 s.nextdecos = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
935 if s.tabStaff == s.pid and s.fOpt and n.name != 'rest': # force fret/string allocation if explicit string decoration is missing
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
936 if [d for d in decos if d in '0123456789'] == []: decos.append ('0')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
937 return decos
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
938
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
939 def mkNote (s, n, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
940 isgrace = getattr (n, 'grace', '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
941 ischord = getattr (n, 'chord', '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
942 if s.ntup >= 0 and not isgrace and not ischord:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
943 s.ntup -= 1 # count tuplet notes only on non-chord, non grace notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
944 if s.ntup == -1 and s.trem <= 0:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
945 s.intrem = 0 # tremolo pair ends at first note that is not a new tremolo pair (s.trem > 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
946 nnum, nden = n.dur.t # abc dutation of note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
947 if s.intrem: nnum += nnum # double duration of tremolo duplets
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
948 if nden == 0: nden = 1 # occurs with illegal ABC like: "A2 1". Now interpreted as A2/1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
949 num, den = simplify (nnum * s.unitLcur[0], nden * s.unitLcur[1]) # normalised with unit length
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
950 if den > 64: # limit denominator to 64
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
951 num = int (round (64 * float (num) / den)) # scale note to num/64
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
952 num, den = simplify (max ([num, 1]), 64) # smallest num == 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
953 info ('duration too small: rounded to %d/%d' % (num, den))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
954 if n.name == 'rest' and ('Z' in n.t or 'X' in n.t):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
955 num, den = s.mdur # duration of one measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
956 noMsrRest = not (n.name == 'rest' and (num, den) == s.mdur) # not a measure rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
957 dvs = (4 * s.divisions * num) // den # divisions is xml-duration of 1/4
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
958 rdvs = dvs # real duration (will be 0 for chord/grace)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
959 num, den = simplify (num, den * 4) # scale by 1/4 for s.typeMap
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
960 ndot = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
961 if num == 3 and noMsrRest: ndot = 1; den = den // 2 # look for dotted notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
962 if num == 7 and noMsrRest: ndot = 2; den = den // 4
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
963 nt = E.Element ('note')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
964 if isgrace: # a grace note (and possibly a chord note)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
965 grace = E.Element ('grace')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
966 if s.acciatura: grace.set ('slash', 'yes'); s.acciatura = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
967 addElem (nt, grace, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
968 dvs = rdvs = 0 # no (real) duration for a grace note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
969 if den <= 16: den = 32 # not longer than 1/8 for a grace note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
970 if s.gcue_on: # insert cue tag
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
971 cue = E.Element ('cue')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
972 addElem (nt, cue, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
973 if ischord: # a chord note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
974 chord = E.Element ('chord')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
975 addElem (nt, chord, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
976 rdvs = 0 # chord notes no real duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
977 if den not in s.typeMap: # take the nearest smaller legal duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
978 info ('illegal duration %d/%d' % (nnum, nden))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
979 den = min (x for x in s.typeMap.keys () if x > den)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
980 xmltype = str (s.typeMap [den]) # xml needs the note type in addition to duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
981 acc, step, oct = '', 'C', '0' # abc-notated pitch elements (accidental, pitch step, octave)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
982 alter, midi, notehead = '', '', '' # xml alteration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
983 if n.name == 'rest':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
984 if 'x' in n.t or 'X' in n.t: nt.set ('print-object', 'no')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
985 rest = E.Element ('rest')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
986 if not noMsrRest: rest.set ('measure', 'yes')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
987 addElem (nt, rest, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
988 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
989 p = n.pitch.t # get pitch elements from parsed tokens
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
990 if len (p) == 3: acc, step, oct = p
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
991 else: step, oct = p
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
992 pitch, alter, midi, notehead = s.mkPitch (acc, step, oct, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
993 if midi: acc = '' # erase accidental for percussion notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
994 addElem (nt, pitch, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
995 if s.ntup >= 0: # modify duration for tuplet notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
996 dvs = dvs * s.tmden // s.tmnum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
997 if dvs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
998 addElemT (nt, 'duration', str (dvs), lev + 1) # skip when dvs == 0, requirement of musicXML
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
999 if not ischord: s.gTime = s.gTime [1], s.gTime [1] + dvs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1000 ptup = (step, oct) # pitch tuple without alteration to check for ties
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1001 tstop = ptup in s.ties and s.ties[ptup][2] == s.overlayVnum # open tie on this pitch tuple in this overlay
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1002 if tstop:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1003 tie = E.Element ('tie', type='stop')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1004 addElem (nt, tie, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1005 if getattr (n, 'tie', 0):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1006 tie = E.Element ('tie', type='start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1007 addElem (nt, tie, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1008 if (s.midprg != ['', '', '', ''] or midi) and n.name != 'rest': # only add when %%midi was present or percussion
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1009 instId = 'I%s-%s' % (s.pid, 'X' + midi if midi else s.vid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1010 chan, midi = ('10', midi) if midi else s.midprg [:2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1011 inst = E.Element ('instrument', id=instId) # instrument id for midi
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1012 addElem (nt, inst, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1013 if instId not in s.midiInst: s.midiInst [instId] = (s.pid, s.vid, chan, midi, s.midprg [2], s.midprg [3]) # for instrument list in mkScorePart
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1014 addElemT (nt, 'voice', '1', lev + 1) # default voice, for merging later
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1015 if noMsrRest: addElemT (nt, 'type', xmltype, lev + 1) # add note type if not a measure rest
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1016 for i in range (ndot): # add dots
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1017 dot = E.Element ('dot')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1018 addElem (nt, dot, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1019 decos = s.getNoteDecos (n) # get decorations for this note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1020 if acc and not tstop: # only add accidental if note not tied
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1021 e = E.Element ('accidental')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1022 if 'courtesy' in decos:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1023 e.set ('parentheses', 'yes')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1024 decos.remove ('courtesy')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1025 e.text = s.accTab [acc]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1026 addElem (nt, e, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1027 tupnotation = '' # start/stop notation element for tuplets
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1028 if s.ntup >= 0: # add time modification element for tuplet notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1029 tmod = mkTmod (s.tmnum, s.tmden, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1030 addElem (nt, tmod, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1031 if s.ntup > 0 and not s.tupnts: tupnotation = 'start'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1032 s.tupnts.append ((rdvs, tmod)) # remember all tuplet modifiers with corresp. durations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1033 if s.ntup == 0: # last tuplet note (and possible chord notes there after)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1034 if rdvs: tupnotation = 'stop' # only insert notation in the real note (rdvs > 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1035 s.cmpNormType (rdvs, lev + 1) # compute and/or add normal-type elements (-> s.ntype)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1036 hasStem = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1037 if not ischord: s.chordDecos = {} # clear on non chord note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1038 if 'stemless' in decos or (s.nostems and n.name != 'rest') or 'stemless' in s.chordDecos:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1039 hasStem = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1040 addElemT (nt, 'stem', 'none', lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1041 if 'stemless' in decos: decos.remove ('stemless') # do not handle in doNotations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1042 if hasattr (n, 'pitches'): s.chordDecos ['stemless'] = 1 # set on first chord note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1043 if notehead:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1044 nh = addElemT (nt, 'notehead', re.sub (r'[+-]$', '', notehead), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1045 if notehead[-1] in '+-': nh.set ('filled', 'yes' if notehead[-1] == '+' else 'no')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1046 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1047 if gstaff: addElemT (nt, 'staff', str (gstaff), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1048 if hasStem: s.doBeams (n, nt, den, lev + 1) # no stems -> no beams in a tab staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1049 s.doNotations (n, decos, ptup, alter, tupnotation, tstop, nt, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1050 if n.objs: s.doLyr (n, nt, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1051 else: s.prevLyric = {} # clear on note without lyrics
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1052 return nt
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1053
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1054 def cmpNormType (s, rdvs, lev): # compute the normal-type of a tuplet (only needed for Finale)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1055 if rdvs: # the last real tuplet note (chord notes can still follow afterwards with rdvs == 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1056 durs = [dur for dur, tmod in s.tupnts if dur > 0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1057 ndur = sum (durs) // s.tmnum # duration of the normal type
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1058 s.irrtup = any ((dur != ndur) for dur in durs) # irregular tuplet
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1059 tix = 16 * s.divisions // ndur # index in typeMap of normal-type duration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1060 if tix in s.typeMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1061 s.ntype = str (s.typeMap [tix]) # the normal-type
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1062 else: s.irrtup = 0 # give up, no normal type possible
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1063 if s.irrtup: # only add normal-type for irregular tuplets
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1064 for dur, tmod in s.tupnts: # add normal-type to all modifiers
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1065 addElemT (tmod, 'normal-type', s.ntype, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1066 s.tupnts = [] # reset the tuplet buffer
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1067
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1068 def doNotations (s, n, decos, ptup, alter, tupnotation, tstop, nt, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1069 slurs = getattr (n, 'slurs', 0) # slur ends
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1070 pts = getattr (n, 'pitches', []) # all chord notes available in the first note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1071 ov = s.overlayVnum # current overlay voice number (0 for the main voice)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1072 if pts: # make list of pitches in chord: [(pitch, octave), ..]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1073 if type (pts.pitch) == pObj: pts = [pts.pitch] # chord with one note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1074 else: pts = [tuple (p.t[-2:]) for p in pts.pitch] # normal chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1075 for pt, (tie_alter, nts, vnum, ntelm) in sorted (list (s.ties.items ())): # scan all open ties and delete illegal ones
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1076 if vnum != s.overlayVnum: continue # tie belongs to different overlay
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1077 if pts and pt in pts: continue # pitch tuple of tie exists in chord
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1078 if getattr (n, 'chord', 0): continue # skip chord notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1079 if pt == ptup: continue # skip correct single note tie
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1080 if getattr (n, 'grace', 0): continue # skip grace notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1081 info ('tie between different pitches: %s%s converted to slur' % pt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1082 del s.ties [pt] # remove the note from pending ties
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1083 e = [t for t in ntelm.findall ('tie') if t.get ('type') == 'start'][0] # get the tie start element
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1084 ntelm.remove (e) # delete start tie element
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1085 e = [t for t in nts.findall ('tied') if t.get ('type') == 'start'][0] # get the tied start element
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1086 e.tag = 'slur' # convert tie into slur
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1087 slurnum = pushSlur (s.slurstack, ov)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1088 e.set ('number', str (slurnum))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1089 if slurs: slurs.t.append (')') # close slur on this note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1090 else: slurs = pObj ('slurs', [')'])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1091 tstart = getattr (n, 'tie', 0) # start a new tie
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1092 if not (tstop or tstart or decos or slurs or s.slurbeg or tupnotation or s.trem): return nt
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1093 nots = E.Element ('notations') # notation element needed
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1094 if s.trem: # +/- => tuple tremolo sequence / single note tremolo
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1095 if s.trem < 0: tupnotation = 'single'; s.trem = -s.trem
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1096 if not tupnotation: return # only add notation at first or last note of a tremolo sequence
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1097 orn = E.Element ('ornaments')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1098 trm = E.Element ('tremolo', type=tupnotation) # type = start, stop or single
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1099 trm.text = str (s.trem) # the number of bars in a tremolo note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1100 addElem (nots, orn, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1101 addElem (orn, trm, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1102 if tupnotation == 'stop' or tupnotation == 'single': s.trem = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1103 elif tupnotation: # add tuplet type
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1104 tup = E.Element ('tuplet', type=tupnotation)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1105 if tupnotation == 'start': tup.set ('bracket', 'yes')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1106 addElem (nots, tup, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1107 if tstop: # stop tie
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1108 del s.ties[ptup] # remove flag
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1109 tie = E.Element ('tied', type='stop')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1110 addElem (nots, tie, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1111 if tstart: # start a tie
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1112 s.ties[ptup] = (alter, nots, s.overlayVnum, nt) # remember pitch tuple to stop tie and apply same alteration
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1113 tie = E.Element ('tied', type='start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1114 if tstart.t[0] == '.-': tie.set ('line-type', 'dotted')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1115 addElem (nots, tie, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1116 if decos: # look for slurs and decorations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1117 slurMap = { '(':1, '.(':1, '(,':1, "('":1, '.(,':1, ".('":1 }
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1118 arts = [] # collect articulations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1119 for d in decos: # do all slurs and decos
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1120 if d in slurMap: s.slurbeg.append (d); continue # slurs made in while loop at the end
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1121 elif d == 'fermata' or d == 'H':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1122 ntn = E.Element ('fermata', type='upright')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1123 elif d == 'arpeggio':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1124 ntn = E.Element ('arpeggiate', number='1')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1125 elif d in ['~(', '~)']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1126 if d[1] == '(': tp = 'start'; s.glisnum += 1; gn = s.glisnum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1127 else: tp = 'stop'; gn = s.glisnum; s.glisnum -= 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1128 if s.glisnum < 0: s.glisnum = 0; continue # stop without previous start
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1129 ntn = E.Element ('glissando', {'line-type':'wavy', 'number':'%d' % gn, 'type':tp})
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1130 elif d in ['-(', '-)']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1131 if d[1] == '(': tp = 'start'; s.slidenum += 1; gn = s.slidenum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1132 else: tp = 'stop'; gn = s.slidenum; s.slidenum -= 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1133 if s.slidenum < 0: s.slidenum = 0; continue # stop without previous start
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1134 ntn = E.Element ('slide', {'line-type':'solid', 'number':'%d' % gn, 'type':tp})
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1135 else: arts.append (d); continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1136 addElem (nots, ntn, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1137 if arts: # do only note articulations and collect staff annotations in xmldecos
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1138 rest = s.doArticulations (nt, nots, arts, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1139 if rest: info ('unhandled note decorations: %s' % rest)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1140 if slurs: # these are only slur endings
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1141 for d in slurs.t: # slurs to be closed on this note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1142 if not s.slurstack.get (ov, 0): break # no more open old slurs for this (overlay) voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1143 slurnum = s.slurstack [ov].pop ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1144 slur = E.Element ('slur', number='%d' % slurnum, type='stop')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1145 addElem (nots, slur, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1146 while s.slurbeg: # create slurs beginning on this note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1147 stp = s.slurbeg.pop (0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1148 slurnum = pushSlur (s.slurstack, ov)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1149 ntn = E.Element ('slur', number='%d' % slurnum, type='start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1150 if '.' in stp: ntn.set ('line-type', 'dotted')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1151 if ',' in stp: ntn.set ('placement', 'below')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1152 if "'" in stp: ntn.set ('placement', 'above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1153 addElem (nots, ntn, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1154 if list (nots) != []: # only add notations if not empty
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1155 addElem (nt, nots, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1156
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1157 def doArticulations (s, nt, nots, arts, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1158 decos = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1159 for a in arts:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1160 if a in s.artMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1161 art = E.Element ('articulations')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1162 addElem (nots, art, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1163 addElem (art, E.Element (s.artMap[a]), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1164 elif a in s.ornMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1165 orn = E.Element ('ornaments')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1166 addElem (nots, orn, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1167 addElem (orn, E.Element (s.ornMap[a]), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1168 elif a in ['trill(','trill)']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1169 orn = E.Element ('ornaments')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1170 addElem (nots, orn, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1171 type = 'start' if a.endswith ('(') else 'stop'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1172 if type == 'start': addElem (orn, E.Element ('trill-mark'), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1173 addElem (orn, E.Element ('wavy-line', type=type), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1174 elif a in s.tecMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1175 tec = E.Element ('technical')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1176 addElem (nots, tec, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1177 addElem (tec, E.Element (s.tecMap[a]), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1178 elif a in '0123456':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1179 tec = E.Element ('technical')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1180 addElem (nots, tec, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1181 if s.tabStaff == s.pid: # current voice belongs to a tabStaff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1182 alt = int (nt.findtext ('pitch/alter') or 0) # find midi number of current note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1183 step = nt.findtext ('pitch/step')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1184 oct = int (nt.findtext ('pitch/octave'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1185 midi = oct * 12 + [0,2,4,5,7,9,11]['CDEFGAB'.index (step)] + alt + 12
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1186 if a == '0': # no string annotation: find one
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1187 firstFit = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1188 for smid, istr in s.tunTup: # midi numbers of open strings from high to low
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1189 if midi >= smid: # highest open string where this note can be played
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1190 isvrij = s.strAlloc.isVrij (istr - 1, s.gTime [0], s.gTime [1])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1191 a = str (istr) # string number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1192 if not firstFit: firstFit = a
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1193 if isvrij: break
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1194 if not isvrij: # no free string, take the first fit (lowest fret)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1195 a = firstFit
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1196 s.strAlloc.bezet (int (a) - 1, s.gTime [0], s.gTime [1])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1197 else: # force annotated string number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1198 s.strAlloc.bezet (int (a) - 1, s.gTime [0], s.gTime [1])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1199 bmidi = s.tunmid [int (a) - 1] # midi number of allocated string (with capodastro)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1200 fret = midi - bmidi # fret position (respecting capodastro)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1201 if fret < 25 and fret >= 0:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1202 addElemT (tec, 'fret', str (fret), lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1203 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1204 altp = 'b' if alt == -1 else '#' if alt == 1 else ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1205 info ('fret %d out of range, note %s%d on string %s' % (fret, step+altp, oct, a))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1206 addElemT (tec, 'string', a, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1207 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1208 addElemT (tec, 'fingering', a, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1209 else: decos.append (a) # return staff annotations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1210 return decos
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1211
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1212 def doLyr (s, n, nt, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1213 for i, lyrobj in enumerate (n.objs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1214 lyrel = E.Element ('lyric', number = str (i + 1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1215 if lyrobj.name == 'syl':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1216 dash = len (lyrobj.t) == 2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1217 if dash:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1218 if i in s.lyrdash: type = 'middle'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1219 else: type = 'begin'; s.lyrdash [i] = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1220 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1221 if i in s.lyrdash: type = 'end'; del s.lyrdash [i]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1222 else: type = 'single'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1223 addElemT (lyrel, 'syllabic', type, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1224 txt = lyrobj.t[0] # the syllabe
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1225 txt = re.sub (r'(?<!\\)~', ' ', txt) # replace ~ by space when not escaped (preceded by \)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1226 txt = re.sub (r'\\(.)', r'\1', txt) # replace all escaped characters by themselves (for the time being)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1227 addElemT (lyrel, 'text', txt, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1228 elif lyrobj.name == 'ext' and i in s.prevLyric:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1229 pext = s.prevLyric [i].find ('extend') # identify previous extend
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1230 if pext == None:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1231 ext = E.Element ('extend', type = 'start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1232 addElem (s.prevLyric [i], ext, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1233 elif pext.get('type') == 'stop': # subsequent extend: stop -> continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1234 pext.set ('type', 'continue')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1235 ext = E.Element ('extend', type = 'stop') # always stop on current extend
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1236 addElem (lyrel, ext, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1237 elif lyrobj.name == 'ext': info ('lyric extend error'); continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1238 else: continue # skip other lyric elements or errors
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1239 addElem (nt, lyrel, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1240 s.prevLyric [i] = lyrel # for extension (melisma) on the next note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1241
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1242 def doBeams (s, n, nt, den, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1243 if hasattr (n, 'chord') or hasattr (n, 'grace'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1244 s.grcbbrk = s.grcbbrk or n.bbrk.t[0] # remember if there was any bbrk in or before a grace sequence
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1245 return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1246 bbrk = s.grcbbrk or n.bbrk.t[0] or den < 32
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1247 s.grcbbrk = False
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1248 if not s.prevNote: pbm = None
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1249 else: pbm = s.prevNote.find ('beam')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1250 bm = E.Element ('beam', number='1')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1251 bm.text = 'begin'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1252 if pbm != None:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1253 if bbrk:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1254 if pbm.text == 'begin':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1255 s.prevNote.remove (pbm)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1256 elif pbm.text == 'continue':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1257 pbm.text = 'end'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1258 s.prevNote = None
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1259 else: bm.text = 'continue'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1260 if den >= 32 and n.name != 'rest':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1261 addElem (nt, bm, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1262 s.prevNote = nt
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1263
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1264 def stopBeams (s):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1265 if not s.prevNote: return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1266 pbm = s.prevNote.find ('beam')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1267 if pbm != None:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1268 if pbm.text == 'begin':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1269 s.prevNote.remove (pbm)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1270 elif pbm.text == 'continue':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1271 pbm.text = 'end'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1272 s.prevNote = None
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1273
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1274 def staffDecos (s, decos, maat, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1275 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1276 for d in decos:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1277 d = s.usrSyms.get (d, d).strip ('!+') # try to replace user defined symbol
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1278 if d in s.dynaMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1279 dynel = E.Element ('dynamics')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1280 addDirection (maat, dynel, lev, gstaff, [E.Element (d)], 'below', s.gcue_on)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1281 elif d in s.wedgeMap: # wedge
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1282 if ')' in d: type = 'stop'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1283 else: type = 'crescendo' if '<' in d or 'crescendo' in d else 'diminuendo'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1284 addDirection (maat, E.Element ('wedge', type=type), lev, gstaff)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1285 elif d.startswith ('8v'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1286 if 'a' in d: type, plce = 'down', 'above'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1287 else: type, plce = 'up', 'below'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1288 if ')' in d: type = 'stop'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1289 addDirection (maat, E.Element ('octave-shift', type=type, size='8'), lev, gstaff, placement=plce)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1290 elif d in (['ped','ped-up']):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1291 type = 'stop' if d.endswith ('up') else 'start'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1292 addDirection (maat, E.Element ('pedal', type=type), lev, gstaff)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1293 elif d in ['coda', 'segno']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1294 text, attr, val = s.capoMap [d]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1295 dir = addDirection (maat, E.Element (text), lev, gstaff, placement='above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1296 sound = E.Element ('sound'); sound.set (attr, val)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1297 addElem (dir, sound, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1298 elif d in s.capoMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1299 text, attr, val = s.capoMap [d]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1300 words = E.Element ('words'); words.text = text
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1301 dir = addDirection (maat, words, lev, gstaff, placement='above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1302 sound = E.Element ('sound'); sound.set (attr, val)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1303 addElem (dir, sound, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1304 elif d == '(' or d == '.(': s.slurbeg.append (d) # start slur on next note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1305 elif d in ['/-','//-','///-','////-']: # duplet tremolo sequence
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1306 s.tmnum, s.tmden, s.ntup, s.trem, s.intrem = 2, 1, 2, len (d) - 1, 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1307 elif d in ['/','//','///']: s.trem = - len (d) # single note tremolo
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1308 else: s.nextdecos.append (d) # keep annotation for the next note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1309
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1310 def doFields (s, maat, fieldmap, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1311 def instDir (midelm, midnum, dirtxt):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1312 instId = 'I%s-%s' % (s.pid, s.vid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1313 words = E.Element ('words'); words.text = dirtxt % midnum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1314 snd = E.Element ('sound')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1315 mi = E.Element ('midi-instrument', id=instId)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1316 dir = addDirection (maat, words, lev, gstaff, placement='above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1317 addElem (dir, snd, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1318 addElem (snd, mi, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1319 addElemT (mi, midelm, midnum, lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1320 def addTrans (n):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1321 e = E.Element ('transpose')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1322 addElemT (e, 'chromatic', n, lev + 2) # n == signed number string given after transpose
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1323 atts.append ((9, e))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1324 def doClef (field):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1325 if re.search (r'perc|map', field): # percussion clef or new style perc=on or map=perc
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1326 r = re.search (r'(perc|map)\s*=\s*(\S*)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1327 s.percVoice = 0 if r and r.group (2) not in ['on','true','perc'] else 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1328 field = re.sub (r'(perc|map)\s*=\s*(\S*)', '', field) # erase the perc= for proper clef matching
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1329 clef, gtrans = 0, 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1330 clefn = re.search (r'alto1|alto2|alto4|alto|tenor|bass3|bass|treble|perc|none|tab', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1331 clefm = re.search (r"(?:^m=| m=|middle=)([A-Ga-g])([,']*)", field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1332 trans_oct2 = re.search (r'octave=([-+]?\d)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1333 trans = re.search (r'(?:^t=| t=|transpose=)(-?[\d]+)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1334 trans_oct = re.search (r'([+-^_])(8|15)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1335 cue_onoff = re.search (r'cue=(on|off)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1336 strings = re.search (r"strings=(\S+)", field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1337 stafflines = re.search (r'stafflines=\s*(\d)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1338 capo = re.search (r'capo=(\d+)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1339 if clefn:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1340 clef = clefn.group ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1341 if clefm:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1342 note, octstr = clefm.groups ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1343 nUp = note.upper ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1344 octnum = (4 if nUp == note else 5) + (len (octstr) if "'" in octstr else -len (octstr))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1345 gtrans = (3 if nUp in 'AFD' else 4) - octnum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1346 if clef not in ['perc', 'none']: clef = s.clefLineMap [nUp]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1347 if clef:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1348 s.gtrans = gtrans # only change global tranposition when a clef is really defined
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1349 if clef != 'none': s.curClef = clef # keep track of current abc clef (for percmap)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1350 sign, line = s.clefMap [clef]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1351 if not sign: return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1352 c = E.Element ('clef')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1353 if gstaff: c.set ('number', str (gstaff)) # only add staff number when defined
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1354 addElemT (c, 'sign', sign, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1355 if line: addElemT (c, 'line', line, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1356 if trans_oct:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1357 n = trans_oct.group (1) in '-_' and -1 or 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1358 if trans_oct.group (2) == '15': n *= 2 # 8 => 1 octave, 15 => 2 octaves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1359 addElemT (c, 'clef-octave-change', str (n), lev + 2) # transpose print out
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1360 if trans_oct.group (1) in '+-': s.gtrans += n # also transpose all pitches with one octave
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1361 atts.append ((7, c))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1362 if trans_oct2: # octave= can also be in a K: field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1363 n = int (trans_oct2.group (1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1364 s.gtrans = gtrans + n
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1365 if trans != None: # add transposition in semitones
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1366 e = E.Element ('transpose')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1367 addElemT (e, 'chromatic', str (trans.group (1)), lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1368 atts.append ((9, e))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1369 if cue_onoff: s.gcue_on = cue_onoff.group (1) == 'on'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1370 nlines = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1371 if clef == 'tab':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1372 s.tabStaff = s.pid
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1373 if capo: s.capo = int (capo.group (1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1374 if strings: s.tuning = strings.group (1).split (',')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1375 s.tunmid = [int (boct) * 12 + [0,2,4,5,7,9,11]['CDEFGAB'.index (bstep)] + 12 + s.capo for bstep, boct in s.tuning]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1376 s.tunTup = sorted (zip (s.tunmid, range (len (s.tunmid), 0, -1)), reverse=1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1377 s.tunmid.reverse ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1378 nlines = str (len (s.tuning))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1379 s.strAlloc.setlines (len (s.tuning), s.pid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1380 s.nostems = 'nostems' in field # tab clef without stems
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1381 s.diafret = 'diafret' in field # tab with diatonic fretting
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1382 if stafflines or nlines:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1383 e = E.Element ('staff-details')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1384 if gstaff: e.set ('number', str (gstaff)) # only add staff number when defined
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1385 if not nlines: nlines = stafflines.group (1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1386 addElemT (e, 'staff-lines', nlines, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1387 if clef == 'tab':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1388 for line, t in enumerate (s.tuning):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1389 st = E.Element ('staff-tuning', line=str(line+1))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1390 addElemT (st, 'tuning-step', t[0], lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1391 addElemT (st, 'tuning-octave', t[1], lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1392 addElem (e, st, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1393 if s.capo: addElemT (e, 'capo', str (s.capo), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1394 atts.append ((8, e))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1395 s.diafret = 0 # chromatic fretting is default
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1396 atts = [] # collect xml attribute elements [(order-number, xml-element), ..]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1397 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1398 for ftype, field in fieldmap.items ():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1399 if not field: # skip empty fields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1400 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1401 if ftype == 'Div': # not an abc field, but handled as if
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1402 d = E.Element ('divisions')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1403 d.text = field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1404 atts.append ((1, d))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1405 elif ftype == 'gstaff': # make grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1406 e = E.Element ('staves')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1407 e.text = str (field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1408 atts.append ((4, e))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1409 elif ftype == 'M':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1410 if field == 'none': continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1411 if field == 'C': field = '4/4'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1412 elif field == 'C|': field = '2/2'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1413 t = E.Element ('time')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1414 if '/' not in field:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1415 info ('M:%s not recognized, 4/4 assumed' % field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1416 field = '4/4'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1417 beats, btype = field.split ('/')[:2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1418 try: s.mdur = simplify (eval (beats), int (btype)) # measure duration for Z and X rests (eval allows M:2+3/4)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1419 except:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1420 info ('error in M:%s, 4/4 assumed' % field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1421 s.mdur = (4,4)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1422 beats, btype = '4','4'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1423 addElemT (t, 'beats', beats, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1424 addElemT (t, 'beat-type', btype, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1425 atts.append ((3, t))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1426 elif ftype == 'K':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1427 accs = ['F','C','G','D','A','E','B'] # == s.sharpness [7:14]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1428 mode = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1429 key = re.match (r'\s*([A-G][#b]?)\s*([a-zA-Z]*)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1430 alts = re.search (r'\s((\s?[=^_][A-Ga-g])+)', ' ' + field) # avoid matching middle=G and m=G
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1431 if key:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1432 key, mode = key.groups ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1433 mode = mode.lower ()[:3] # only first three chars, no case
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1434 if mode not in s.offTab: mode = 'maj'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1435 fifths = s.sharpness.index (key) - s.offTab [mode]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1436 if fifths >= 0: s.keyAlts = dict (zip (accs[:fifths], fifths * ['1']))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1437 else: s.keyAlts = dict (zip (accs[fifths:], -fifths * ['-1']))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1438 elif field.startswith ('none') or field == '': # the default key
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1439 fifths = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1440 mode = 'maj'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1441 if alts:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1442 alts = re.findall (r'[=^_][A-Ga-g]', alts.group(1)) # list of explicit alterations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1443 alts = [(x[1], s.alterTab [x[0]]) for x in alts] # [step, alter]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1444 for step, alter in alts: # correct permanent alterations for this key
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1445 s.keyAlts [step.upper ()] = alter
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1446 k = E.Element ('key')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1447 koctave = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1448 lowerCaseSteps = [step.upper () for step, alter in alts if step.islower ()]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1449 for step, alter in sorted (list (s.keyAlts.items ())):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1450 if alter == '0': # skip neutrals
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1451 del s.keyAlts [step.upper ()] # otherwise you get neutral signs on normal notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1452 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1453 addElemT (k, 'key-step', step.upper (), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1454 addElemT (k, 'key-alter', alter, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1455 koctave.append ('5' if step in lowerCaseSteps else '4')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1456 if koctave: # only key signature if not empty
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1457 for oct in koctave:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1458 e = E.Element ('key-octave', number=oct)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1459 addElem (k, e, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1460 atts.append ((2, k))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1461 elif mode:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1462 k = E.Element ('key')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1463 addElemT (k, 'fifths', str (fifths), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1464 addElemT (k, 'mode', s.modTab [mode], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1465 atts.append ((2, k))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1466 doClef (field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1467 elif ftype == 'L':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1468 try: s.unitLcur = lmap (int, field.split ('/'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1469 except: s.unitLcur = (1,8)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1470 if len (s.unitLcur) == 1 or s.unitLcur[1] not in s.typeMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1471 info ('L:%s is not allowed, 1/8 assumed' % field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1472 s.unitLcur = 1,8
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1473 elif ftype == 'V':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1474 doClef (field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1475 elif ftype == 'I':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1476 s.doField_I (ftype, field, instDir, addTrans)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1477 elif ftype == 'Q':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1478 s.doTempo (maat, field, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1479 elif ftype == 'P': # ad hoc translation of P: into a staff text direction
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1480 words = E.Element ('rehearsal')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1481 words.set ('font-weight', 'bold')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1482 words.text = field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1483 addDirection (maat, words, lev, gstaff, placement='above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1484 elif ftype in 'TCOAZNGHRBDFSU':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1485 info ('**illegal header field in body: %s, content: %s' % (ftype, field))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1486 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1487 info ('unhandled field: %s, content: %s' % (ftype, field))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1488
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1489 if atts:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1490 att = E.Element ('attributes') # insert sub elements in the order required by musicXML
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1491 addElem (maat, att, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1492 for _, att_elem in sorted (atts, key=lambda x: x[0]): # ordering !
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1493 addElem (att, att_elem, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1494 if s.diafret:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1495 other = E.Element ('other-direction'); other.text = 'diatonic fretting'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1496 addDirection (maat, other, lev, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1497
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1498 def doTempo (s, maat, field, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1499 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1500 t = re.search (r'(\d)/(\d\d?)\s*=\s*(\d[.\d]*)|(\d[.\d]*)', field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1501 rtxt = re.search (r'"([^"]*)"', field) # look for text in Q: field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1502 if not t and not rtxt: return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1503 elems = [] # [(element, sub-elements)] will be added as direction-types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1504 if rtxt:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1505 num, den, upm = 1, 4, s.tempoMap.get (rtxt.group (1).lower ().strip (), 120)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1506 words = E.Element ('words'); words.text = rtxt.group (1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1507 elems.append ((words, []))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1508 if t:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1509 try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1510 if t.group (4): num, den, upm = 1, s.unitLcur[1] , float (t.group (4)) # old syntax Q:120
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1511 else: num, den, upm = int (t.group (1)), int (t.group (2)), float (t.group (3))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1512 except: info ('conversion error: %s' % field); return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1513 num, den = simplify (num, den);
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1514 dotted, den_not = (1, den // 2) if num == 3 else (0, den)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1515 metro = E.Element ('metronome')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1516 u = E.Element ('beat-unit'); u.text = s.typeMap.get (4 * den_not, 'quarter')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1517 pm = E.Element ('per-minute'); pm.text = ('%.2f' % upm).rstrip ('0').rstrip ('.')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1518 subelms = [u, E.Element ('beat-unit-dot'), pm] if dotted else [u, pm]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1519 elems.append ((metro, subelms))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1520 dir = addDirection (maat, elems, lev, gstaff, [], placement='above')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1521 if num != 1 and num != 3: info ('in Q: numerator in %d/%d not supported' % (num, den))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1522 qpm = 4. * num * upm / den
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1523 sound = E.Element ('sound'); sound.set ('tempo', '%.2f' % qpm)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1524 addElem (dir, sound, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1525
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1526 def mkBarline (s, maat, loc, lev, style='', dir='', ending=''):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1527 b = E.Element ('barline', location=loc)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1528 if style:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1529 addElemT (b, 'bar-style', style, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1530 if s.curVolta: # first stop a current volta
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1531 end = E.Element ('ending', number=s.curVolta, type='stop')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1532 s.curVolta = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1533 if loc == 'left': # stop should always go to a right barline
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1534 bp = E.Element ('barline', location='right')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1535 addElem (bp, end, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1536 addElem (s.prevmsre, bp, lev) # prevmsre has no right barline! (ending would have stopped there)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1537 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1538 addElem (b, end, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1539 if ending:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1540 ending = ending.replace ('-',',') # MusicXML only accepts comma's
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1541 endtxt = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1542 if ending.startswith ('"'): # ending is a quoted string
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1543 endtxt = ending.strip ('"')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1544 ending = '33' # any number that is not likely to occur elsewhere
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1545 end = E.Element ('ending', number=ending, type='start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1546 if endtxt: end.text = endtxt # text appears in score in stead of number attribute
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1547 addElem (b, end, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1548 s.curVolta = ending
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1549 if dir:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1550 r = E.Element ('repeat', direction=dir)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1551 addElem (b, r, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1552 addElem (maat, b, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1553
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1554 def doChordSym (s, maat, sym, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1555 alterMap = {'#':'1','=':'0','b':'-1'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1556 rnt = sym.root.t
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1557 chord = E.Element ('harmony')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1558 addElem (maat, chord, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1559 root = E.Element ('root')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1560 addElem (chord, root, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1561 addElemT (root, 'root-step', rnt[0], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1562 if len (rnt) == 2: addElemT (root, 'root-alter', alterMap [rnt[1]], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1563 kind = s.chordTab.get (sym.kind.t[0], 'major') if sym.kind.t else 'major'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1564 addElemT (chord, 'kind', kind, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1565 if hasattr (sym, 'bass'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1566 bnt = sym.bass.t
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1567 bass = E.Element ('bass')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1568 addElem (chord, bass, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1569 addElemT (bass, 'bass-step', bnt[0], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1570 if len (bnt) == 2: addElemT (bass, 'bass-alter', alterMap [bnt[1]], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1571 degs = getattr (sym, 'degree', '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1572 if degs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1573 if type (degs) != list_type: degs = [degs]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1574 for deg in degs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1575 deg = deg.t[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1576 if deg[0] == '#': alter = '1'; deg = deg[1:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1577 elif deg[0] == 'b': alter = '-1'; deg = deg[1:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1578 else: alter = '0'; deg = deg
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1579 degree = E.Element ('degree')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1580 addElem (chord, degree, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1581 addElemT (degree, 'degree-value', deg, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1582 addElemT (degree, 'degree-alter', alter, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1583 addElemT (degree, 'degree-type', 'add', lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1584
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1585 def mkMeasure (s, i, t, lev, fieldmap={}):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1586 s.msreAlts = {}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1587 s.ntup, s.trem, s.intrem = -1, 0, 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1588 s.acciatura = 0 # next grace element gets acciatura attribute
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1589 overlay = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1590 maat = E.Element ('measure', number = str(i))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1591 if fieldmap: s.doFields (maat, fieldmap, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1592 if s.linebrk: # there was a line break in the previous measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1593 e = E.Element ('print')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1594 e.set ('new-system', 'yes')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1595 addElem (maat, e, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1596 s.linebrk = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1597 for it, x in enumerate (t):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1598 if x.name == 'note' or x.name == 'rest':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1599 if x.dur.t[0] == 0: # a leading zero was used for stemmless in abcm2ps, we only support !stemless!
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1600 x.dur.t = tuple ([1, x.dur.t[1]])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1601 note = s.mkNote (x, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1602 addElem (maat, note, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1603 elif x.name == 'lbar':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1604 bar = x.t[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1605 if bar == '|' or bar == '[|': pass # skip redundant bar
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1606 elif ':' in bar: # forward repeat
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1607 volta = x.t[1] if len (x.t) == 2 else ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1608 s.mkBarline (maat, 'left', lev + 1, style='heavy-light', dir='forward', ending=volta)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1609 else: # bar must be a volta number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1610 s.mkBarline (maat, 'left', lev + 1, ending=bar)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1611 elif x.name == 'rbar':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1612 bar = x.t[0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1613 if bar == '.|':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1614 s.mkBarline (maat, 'right', lev + 1, style='dotted')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1615 elif ':' in bar: # backward repeat
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1616 s.mkBarline (maat, 'right', lev + 1, style='light-heavy', dir='backward')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1617 elif bar == '||':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1618 s.mkBarline (maat, 'right', lev + 1, style='light-light')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1619 elif bar == '[|]' or bar == '[]':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1620 s.mkBarline (maat, 'right', lev + 1, style='none')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1621 elif '[' in bar or ']' in bar:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1622 s.mkBarline (maat, 'right', lev + 1, style='light-heavy')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1623 elif bar[0] == '&': overlay = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1624 elif x.name == 'tup':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1625 if len (x.t) == 3: n, into, nts = x.t
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1626 elif len (x.t) == 2: n, into, nts = x.t + [0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1627 else: n, into, nts = x.t[0], 0, 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1628 if into == 0: into = 3 if n in [2,4,8] else 2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1629 if nts == 0: nts = n
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1630 s.tmnum, s.tmden, s.ntup = n, into, nts
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1631 elif x.name == 'deco':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1632 s.staffDecos (x.t, maat, lev + 1) # output staff decos, postpone note decos to next note
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1633 elif x.name == 'text':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1634 pos, text = x.t[:2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1635 place = 'above' if pos == '^' else 'below'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1636 words = E.Element ('words')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1637 words.text = text
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1638 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1639 addDirection (maat, words, lev + 1, gstaff, placement=place)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1640 elif x.name == 'inline':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1641 fieldtype, fieldval = x.t[0], ' '.join (x.t[1:])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1642 s.doFields (maat, {fieldtype:fieldval}, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1643 elif x.name == 'accia': s.acciatura = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1644 elif x.name == 'linebrk':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1645 s.supports_tag = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1646 if it > 0 and t[it -1].name == 'lbar': # we are at start of measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1647 e = E.Element ('print') # output linebreak now
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1648 e.set ('new-system', 'yes')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1649 addElem (maat, e, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1650 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1651 s.linebrk = 1 # output linebreak at start of next measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1652 elif x.name == 'chordsym':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1653 s.doChordSym (maat, x, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1654 s.stopBeams ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1655 s.prevmsre = maat
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1656 return maat, overlay
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1657
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1658 def mkPart (s, maten, id, lev, attrs, nstaves, rOpt):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1659 s.slurstack = {}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1660 s.glisnum = 0; # xml number attribute for glissandos
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1661 s.slidenum = 0; # xml number attribute for slides
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1662 s.unitLcur = s.unitL # set the default unit length at begin of each voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1663 s.curVolta = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1664 s.lyrdash = {}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1665 s.linebrk = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1666 s.midprg = ['', '', '', ''] # MIDI channel nr, program nr, volume, panning for the current part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1667 s.gcue_on = 0 # reset cue note marker for each new voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1668 s.gtrans = 0 # reset octave transposition (by clef)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1669 s.percVoice = 0 # 1 if percussion clef encountered
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1670 s.curClef = '' # current abc clef (for percmap)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1671 s.nostems = 0 # for the tab clef
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1672 s.tuning = s.tuningDef # reset string tuning to default
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1673 part = E.Element ('part', id=id)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1674 s.overlayVnum = 0 # overlay voice number to relate ties that extend from one overlayed measure to the next
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1675 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1676 attrs_cpy = attrs.copy () # don't change attrs itself in next line
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1677 if gstaff == 1: attrs_cpy ['gstaff'] = nstaves # make a grand staff
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1678 if 'perc' in attrs_cpy.get ('V', ''): del attrs_cpy ['K'] # remove key from percussion voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1679 msre, overlay = s.mkMeasure (1, maten[0], lev + 1, attrs_cpy)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1680 addElem (part, msre, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1681 for i, maat in enumerate (maten[1:]):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1682 s.overlayVnum = s.overlayVnum + 1 if overlay else 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1683 msre, next_overlay = s.mkMeasure (i+2, maat, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1684 if overlay: mergePartMeasure (part, msre, s.overlayVnum, rOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1685 else: addElem (part, msre, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1686 overlay = next_overlay
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1687 return part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1688
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1689 def mkScorePart (s, id, vids_p, partAttr, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1690 def mkInst (instId, vid, midchan, midprog, midnot, vol, pan, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1691 si = E.Element ('score-instrument', id=instId)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1692 pnm = partAttr.get (vid, [''])[0] # part name if present
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1693 addElemT (si, 'instrument-name', pnm or 'dummy', lev + 2) # MuseScore needs a name
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1694 mi = E.Element ('midi-instrument', id=instId)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1695 if midchan: addElemT (mi, 'midi-channel', midchan, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1696 if midprog: addElemT (mi, 'midi-program', str (int (midprog) + 1), lev + 2) # compatible with abc2midi
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1697 if midnot: addElemT (mi, 'midi-unpitched', str (int (midnot) + 1), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1698 if vol: addElemT (mi, 'volume', '%.2f' % (int (vol) / 1.27), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1699 if pan: addElemT (mi, 'pan', '%.2f' % (int (pan) / 127. * 180 - 90), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1700 return (si, mi)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1701 naam, subnm, midprg = partAttr [id]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1702 sp = E.Element ('score-part', id='P'+id)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1703 nm = E.Element ('part-name')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1704 nm.text = naam
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1705 addElem (sp, nm, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1706 snm = E.Element ('part-abbreviation')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1707 snm.text = subnm
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1708 if subnm: addElem (sp, snm, lev + 1) # only add if subname was given
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1709 inst = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1710 for instId, (pid, vid, chan, midprg, vol, pan) in sorted (s.midiInst.items ()):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1711 midprg, midnot = ('0', midprg) if chan == '10' else (midprg, '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1712 if pid == id: inst.append (mkInst (instId, vid, chan, midprg, midnot, vol, pan, lev))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1713 for si, mi in inst: addElem (sp, si, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1714 for si, mi in inst: addElem (sp, mi, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1715 return sp
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1716
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1717 def mkPartlist (s, vids, partAttr, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1718 def addPartGroup (sym, num):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1719 pg = E.Element ('part-group', number=str (num), type='start')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1720 addElem (partlist, pg, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1721 addElemT (pg, 'group-symbol', sym, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1722 addElemT (pg, 'group-barline', 'yes', lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1723 partlist = E.Element ('part-list')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1724 g_num = 0 # xml group number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1725 for g in (s.groups or vids): # brace/bracket or abc_voice_id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1726 if g == '[': g_num += 1; addPartGroup ('bracket', g_num)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1727 elif g == '{': g_num += 1; addPartGroup ('brace', g_num)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1728 elif g in '}]':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1729 pg = E.Element ('part-group', number=str (g_num), type='stop')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1730 addElem (partlist, pg, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1731 g_num -= 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1732 else: # g = abc_voice_id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1733 if g not in vids: continue # error in %%score
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1734 sp = s.mkScorePart (g, vids, partAttr, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1735 addElem (partlist, sp, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1736 return partlist
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1737
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1738 def doField_I (s, type, x, instDir, addTrans):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1739 def instChange (midchan, midprog): # instDir -> doFields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1740 if midchan and midchan != s.midprg [0]: instDir ('midi-channel', midchan, 'chan: %s')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1741 if midprog and midprog != s.midprg [1]: instDir ('midi-program', str (int (midprog) + 1), 'prog: %s')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1742 def readPfmt (x, n): # read ABC page formatting constant
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1743 if not s.pageFmtAbc: s.pageFmtAbc = s.pageFmtDef # set the default values on first change
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1744 ro = re.search (r'[^.\d]*([\d.]+)\s*(cm|in|pt)?', x) # float followed by unit
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1745 if ro:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1746 x, unit = ro.groups () # unit == None when not present
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1747 u = {'cm':10., 'in':25.4, 'pt':25.4/72} [unit] if unit else 1.
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1748 s.pageFmtAbc [n] = float (x) * u # convert ABC values to millimeters
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1749 else: info ('error in page format: %s' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1750 def readPercMap (x): # parse I:percmap <abc_note> <step> <MIDI> <notehead>
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1751 def getMidNum (sndnm): # find midi number of GM drum sound name
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1752 pnms = sndnm.split ('-') # sound name parts (from I:percmap)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1753 ps = s.percsnd [:] # copy of the instruments
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1754 _f = lambda ip, xs, pnm: ip < len (xs) and xs[ip].find (pnm) > -1 # part xs[ip] and pnm match
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1755 for ip, pnm in enumerate (pnms): # match all percmap sound name parts
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1756 ps = [(nm, mnum) for nm, mnum in ps if _f (ip, nm.split ('-'), pnm) ] # filter instruments
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1757 if len (ps) <= 1: break # no match or one instrument left
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1758 if len (ps) == 0: info ('drum sound: %s not found' % sndnm); return '38'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1759 return ps [0][1] # midi number of (first) instrument found
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1760 def midiVal (acc, step, oct): # abc note -> midi note number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1761 oct = (4 if step.upper() == step else 5) + int (oct)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1762 return oct * 12 + [0,2,4,5,7,9,11]['CDEFGAB'.index (step.upper())] + {'^':1,'_':-1,'=':0}.get (acc, 0) + 12
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1763 p0, p1, p2, p3, p4 = abc_percmap.parseString (x).asList () # percmap, abc-note, display-step, midi, note-head
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1764 acc, astep, aoct = p1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1765 nstep, noct = (astep, aoct) if p2 == '*' else p2
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1766 if p3 == '*': midi = str (midiVal (acc, astep, aoct))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1767 elif isinstance (p3, list_type): midi = str (midiVal (p3[0], p3[1], p3[2]))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1768 elif isinstance (p3, int_type): midi = str (p3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1769 else: midi = getMidNum (p3.lower ())
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1770 head = re.sub (r'(.)-([^x])', r'\1 \2', p4) # convert abc note head names to xml
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1771 s.percMap [(s.pid, acc + astep, aoct)] = (nstep, noct, midi, head)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1772 if x.startswith ('score') or x.startswith ('staves'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1773 s.staveDefs += [x] # collect all voice mappings
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1774 elif x.startswith ('staffwidth'): info ('skipped I-field: %s' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1775 elif x.startswith ('staff'): # set new staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1776 r1 = re.search (r'staff *([+-]?)(\d)', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1777 if r1:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1778 sign = r1.group (1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1779 num = int (r1.group (2))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1780 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1781 if sign: # relative staff number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1782 num = (sign == '-') and gstaff - num or gstaff + num
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1783 else: # absolute abc staff number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1784 try: vabc = s.staves [num - 1][0] # vid of (first voice of) abc-staff num
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1785 except: vabc = 0; info ('abc staff %s does not exist' % num)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1786 num = s.gStaffNumsOrg.get (vabc, 0) # xml staff number of abc-staff num
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1787 if gstaff and num > 0 and num <= s.gNstaves [s.vid]:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1788 s.gStaffNums [s.vid] = num
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1789 else: info ('could not relocate to staff: %s' % r1.group ())
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1790 else: info ('not a valid staff redirection: %s' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1791 elif x.startswith ('scale'): readPfmt (x, 0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1792 elif x.startswith ('pageheight'): readPfmt (x, 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1793 elif x.startswith ('pagewidth'): readPfmt (x, 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1794 elif x.startswith ('leftmargin'): readPfmt (x, 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1795 elif x.startswith ('rightmargin'): readPfmt (x, 4)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1796 elif x.startswith ('topmargin'): readPfmt (x, 5)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1797 elif x.startswith ('botmargin'): readPfmt (x, 6)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1798 elif x.startswith ('MIDI') or x.startswith ('midi'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1799 r1 = re.search (r'program *(\d*) +(\d+)', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1800 r2 = re.search (r'channel *(\d+)', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1801 r3 = re.search (r"drummap\s+([_=^]*)([A-Ga-g])([,']*)\s+(\d+)", x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1802 r4 = re.search (r'control *(\d+) +(\d+)', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1803 ch_nw, prg_nw, vol_nw, pan_nw = '', '', '', ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1804 if r1: ch_nw, prg_nw = r1.groups () # channel nr or '', program nr
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1805 if r2: ch_nw = r2.group (1) # channel nr only
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1806 if r4:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1807 cnum, cval = r4.groups () # controller number, controller value
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1808 if cnum == '7': vol_nw = cval
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1809 if cnum == '10': pan_nw = cval
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1810 if r1 or r2 or r4:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1811 ch = ch_nw or s.midprg [0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1812 prg = prg_nw or s.midprg [1]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1813 vol = vol_nw or s.midprg [2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1814 pan = pan_nw or s.midprg [3]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1815 instId = 'I%s-%s' % (s.pid, s.vid) # only look for real instruments, no percussion
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1816 if instId in s.midiInst: instChange (ch, prg) # instChance -> doFields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1817 s.midprg = [ch, prg, vol, pan] # mknote: new instrument -> s.midiInst
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1818 if r3: # translate drummap to percmap
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1819 acc, step, oct, midi = r3.groups ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1820 oct = -len (oct) if ',' in x else len (oct)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1821 notehead = 'x' if acc == '^' else 'circle-x' if acc == '_' else 'normal'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1822 s.percMap [(s.pid, acc + step, oct)] = (step, oct, midi, notehead)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1823 r = re.search (r'transpose[^-\d]*(-?\d+)', x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1824 if r: addTrans (r.group (1)) # addTrans -> doFields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1825 elif x.startswith ('percmap'): readPercMap (x); s.pMapFound = 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1826 else: info ('skipped I-field: %s' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1827
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1828 def parseStaveDef (s, vdefs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1829 for vid in vdefs: s.vcepid [vid] = vid # default: each voice becomes an xml part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1830 if not s.staveDefs: return vdefs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1831 for x in s.staveDefs [1:]: info ('%%%%%s dropped, multiple stave mappings not supported' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1832 x = s.staveDefs [0] # only the first %%score is honoured
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1833 score = abc_scoredef.parseString (x) [0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1834 f = lambda x: type (x) == uni_type and [x] or x
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1835 s.staves = lmap (f, mkStaves (score, vdefs)) # [[vid] for each staff]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1836 s.grands = lmap (f, mkGrand (score, vdefs)) # [staff-id], staff-id == [vid][0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1837 s.groups = mkGroups (score)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1838 vce_groups = [vids for vids in s.staves if len (vids) > 1] # all voice groups
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1839 d = {} # for each voice group: map first voice id -> all merged voice ids
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1840 for vgr in vce_groups: d [vgr[0]] = vgr
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1841 for gstaff in s.grands: # for all grand staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1842 if len (gstaff) == 1: continue # skip single parts
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1843 for v, stf_num in zip (gstaff, range (1, len (gstaff) + 1)):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1844 for vx in d.get (v, [v]): # allocate staff numbers
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1845 s.gStaffNums [vx] = stf_num # to all constituant voices
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1846 s.gNstaves [vx] = len (gstaff) # also remember total number of staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1847 s.gStaffNumsOrg = s.gStaffNums.copy () # keep original allocation for abc -> xml staff map
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1848 for xmlpart in s.grands:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1849 pid = xmlpart [0] # part id == first staff id == first voice id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1850 vces = [v for stf in xmlpart for v in d.get (stf, [stf])]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1851 for v in vces: s.vcepid [v] = pid
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1852 return vdefs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1853
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1854 def voiceNamesAndMaps (s, ps): # get voice names and mappings
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1855 vdefs = {}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1856 for vid, vcedef, vce in ps: # vcedef == emtpy or first pObj == voice definition
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1857 pname, psubnm = '', '' # part name and abbreviation
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1858 if not vcedef: # simple abc without voice definitions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1859 vdefs [vid] = pname, psubnm, ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1860 else: # abc with voice definitions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1861 if vid != vcedef.t[1]: info ('voice ids unequal: %s (reg-ex) != %s (grammar)' % (vid, vcedef.t[1]))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1862 rn = re.search (r'(?:name|nm)="([^"]*)"', vcedef.t[2])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1863 if rn: pname = rn.group (1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1864 rn = re.search (r'(?:subname|snm|sname)="([^"]*)"', vcedef.t[2])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1865 if rn: psubnm = rn.group (1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1866 vcedef.t[2] = vcedef.t[2].replace ('"%s"' % pname, '""').replace ('"%s"' % psubnm, '""') # clear voice name to avoid false clef matches later on
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1867 vdefs [vid] = pname, psubnm, vcedef.t[2]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1868 xs = [pObj.t[1] for maat in vce for pObj in maat if pObj.name == 'inline'] # all inline statements in vce
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1869 s.staveDefs += [x.replace ('%5d',']') for x in xs if x.startswith ('score') or x.startswith ('staves')] # filter %%score and %%staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1870 return vdefs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1871
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1872 def doHeaderField (s, fld, attrmap):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1873 type, value = fld.t[0], fld.t[1].replace ('%5d',']') # restore closing brackets (see splitHeaderVoices)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1874 if not value: # skip empty field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1875 return
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1876 if type == 'M':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1877 attrmap [type] = value
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1878 elif type == 'L':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1879 try: s.unitL = lmap (int, fld.t[1].split ('/'))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1880 except:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1881 info ('illegal unit length:%s, 1/8 assumed' % fld.t[1])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1882 s.unitL = 1,8
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1883 if len (s.unitL) == 1 or s.unitL[1] not in s.typeMap:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1884 info ('L:%s is not allowed, 1/8 assumed' % fld.t[1])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1885 s.unitL = 1,8
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1886 elif type == 'K':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1887 attrmap[type] = value
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1888 elif type == 'T':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1889 s.title = s.title + '\n' + value if s.title else value
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1890 elif type == 'U':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1891 sym = fld.t[2].strip ('!+')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1892 s.usrSyms [value] = sym
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1893 elif type == 'I':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1894 s.doField_I (type, value, lambda x,y,z:0, lambda x:0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1895 elif type == 'Q':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1896 attrmap[type] = value
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1897 elif type in 'CRZNOAGHBDFSP': # part maps are treated as meta data
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1898 type = s.metaMap.get (type, type) # respect the (user defined --meta) mapping of various ABC fields to XML meta data types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1899 c = s.metadata.get (type, '')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1900 s.metadata [type] = c + '\n' + value if c else value # concatenate multiple info fields with new line as separator
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1901 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1902 info ('skipped header: %s' % fld)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1903
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1904 def mkIdentification (s, score, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1905 if s.title:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1906 xs = s.title.split ('\n') # the first T: line goes to work-title
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1907 ys = '\n'.join (xs [1:]) # place subsequent T: lines into work-number
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1908 w = E.Element ('work')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1909 addElem (score, w, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1910 if ys: addElemT (w, 'work-number', ys, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1911 addElemT (w, 'work-title', xs[0], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1912 ident = E.Element ('identification')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1913 addElem (score, ident, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1914 for mtype, mval in s.metadata.items ():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1915 if mtype in s.metaTypes and mtype != 'rights': # all metaTypes are MusicXML creator types
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1916 c = E.Element ('creator', type=mtype)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1917 c.text = mval
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1918 addElem (ident, c, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1919 if 'rights' in s.metadata:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1920 c = addElemT (ident, 'rights', s.metadata ['rights'], lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1921 encoding = E.Element ('encoding')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1922 addElem (ident, encoding, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1923 encoder = E.Element ('encoder')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1924 encoder.text = 'abc2xml version %d' % VERSION
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1925 addElem (encoding, encoder, lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1926 if s.supports_tag: # avoids interference of auto-flowing and explicit linebreaks
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1927 suports = E.Element ('supports', attribute="new-system", element="print", type="yes", value="yes")
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1928 addElem (encoding, suports, lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1929 encodingDate = E.Element ('encoding-date')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1930 encodingDate.text = str (datetime.date.today ())
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1931 addElem (encoding, encodingDate, lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1932 s.addMeta (ident, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1933
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1934 def mkDefaults (s, score, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1935 if s.pageFmtCmd: s.pageFmtAbc = s.pageFmtCmd
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1936 if not s.pageFmtAbc: return # do not output the defaults if none is desired
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1937 abcScale, h, w, l, r, t, b = s.pageFmtAbc
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1938 space = abcScale * 2.117 # 2.117 = 6pt = space between staff lines for scale = 1.0 in abcm2ps
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1939 mils = 4 * space # staff height in millimeters
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1940 scale = 40. / mils # tenth's per millimeter
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1941 dflts = E.Element ('defaults')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1942 addElem (score, dflts, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1943 scaling = E.Element ('scaling')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1944 addElem (dflts, scaling, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1945 addElemT (scaling, 'millimeters', '%g' % mils, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1946 addElemT (scaling, 'tenths', '40', lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1947 layout = E.Element ('page-layout')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1948 addElem (dflts, layout, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1949 addElemT (layout, 'page-height', '%g' % (h * scale), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1950 addElemT (layout, 'page-width', '%g' % (w * scale), lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1951 margins = E.Element ('page-margins', type='both')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1952 addElem (layout, margins, lev + 2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1953 addElemT (margins, 'left-margin', '%g' % (l * scale), lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1954 addElemT (margins, 'right-margin', '%g' % (r * scale), lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1955 addElemT (margins, 'top-margin', '%g' % (t * scale), lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1956 addElemT (margins, 'bottom-margin', '%g' % (b * scale), lev + 3)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1957
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1958 def addMeta (s, parent, lev):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1959 misc = E.Element ('miscellaneous')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1960 mf = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1961 for mtype, mval in sorted (s.metadata.items ()):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1962 if mtype == 'S':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1963 addElemT (parent, 'source', mval, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1964 elif mtype in s.metaTypes: continue # mapped meta data has already been output (in creator elements)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1965 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1966 mf = E.Element ('miscellaneous-field', name=s.metaTab [mtype])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1967 mf.text = mval
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1968 addElem (misc, mf, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1969 if mf != 0: addElem (parent, misc, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1970
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1971 def parse (s, abc_string, rOpt=False, bOpt=False, fOpt=False):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1972 abctext = abc_string.replace ('[I:staff ','[I:staff') # avoid false beam breaks
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1973 s.reset (fOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1974 header, voices = splitHeaderVoices (abctext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1975 ps = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1976 try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1977 lbrk_insert = 0 if re.search (r'I:linebreak\s*([!$]|none)|I:continueall\s*(1|true)', header) else bOpt
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1978 hs = abc_header.parseString (header) if header else ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1979 for id, voice in voices:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1980 if lbrk_insert: # insert linebreak at EOL
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1981 r1 = re.compile (r'\[[wA-Z]:[^]]*\]') # inline field
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1982 has_abc = lambda x: r1.sub ('', x).strip () # empty if line only contains inline fields
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1983 voice = '\n'.join ([balk.rstrip ('$!') + '$' if has_abc (balk) else balk for balk in voice.splitlines ()])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1984 prevLeftBar = None # previous voice ended with a left-bar symbol (double repeat)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1985 s.orderChords = s.fOpt and ('tab' in voice [:200] or [x for x in hs if x.t[0] == 'K' and 'tab' in x.t[1]])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1986 vce = abc_voice.parseString (voice).asList ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1987 lyr_notes = [] # remember notes between lyric blocks
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1988 for m in vce: # all measures
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1989 for e in m: # all abc-elements
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1990 if e.name == 'lyr_blk': # -> e.objs is list of lyric lines
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1991 lyr = [line.objs for line in e.objs] # line.objs is listof syllables
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1992 alignLyr (lyr_notes, lyr) # put all syllables into corresponding notes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1993 lyr_notes = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1994 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1995 lyr_notes.append (e)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1996 if not vce: # empty voice, insert an inline field that will be rejected
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1997 vce = [[pObj ('inline', ['I', 'empty voice'])]]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1998 if prevLeftBar:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1999 vce[0].insert (0, prevLeftBar) # insert at begin of first measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2000 prevLeftBar = None
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2001 if vce[-1] and vce[-1][-1].name == 'lbar': # last measure ends with an lbar
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2002 prevLeftBar = vce[-1][-1]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2003 if len (vce) > 1: # vce should not become empty (-> exception when taking vcelyr [0][0])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2004 del vce[-1] # lbar was the only element in measure vce[-1]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2005 vcelyr = vce
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2006 elem1 = vcelyr [0][0] # the first element of the first measure
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2007 if elem1.name == 'inline'and elem1.t[0] == 'V': # is a voice definition
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2008 voicedef = elem1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2009 del vcelyr [0][0] # do not read voicedef twice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2010 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2011 voicedef = ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2012 ps.append ((id, voicedef, vcelyr))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2013 except ParseException as err:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2014 if err.loc > 40: # limit length of error message, compatible with markInputline
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2015 err.pstr = err.pstr [err.loc - 40: err.loc + 40]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2016 err.loc = 40
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2017 xs = err.line[err.col-1:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2018 info (err.line, warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2019 info ((err.col-1) * '-' + '^', warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2020 if re.search (r'\[U:', xs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2021 info ('Error: illegal user defined symbol: %s' % xs[1:], warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2022 elif re.search (r'\[[OAPZNGHRBDFSXTCIU]:', xs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2023 info ('Error: header-only field %s appears after K:' % xs[1:], warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2024 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2025 info ('Syntax error at column %d' % err.col, warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2026 raise
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2027
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2028 score = E.Element ('score-partwise')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2029 attrmap = {'Div': str (s.divisions), 'K':'C treble', 'M':'4/4'}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2030 for res in hs:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2031 if res.name == 'field':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2032 s.doHeaderField (res, attrmap)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2033 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2034 info ('unexpected header item: %s' % res)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2035
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2036 vdefs = s.voiceNamesAndMaps (ps)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2037 vdefs = s.parseStaveDef (vdefs)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2038
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2039 lev = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2040 vids, parts, partAttr = [], [], {}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2041 s.strAlloc = stringAlloc ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2042 for vid, _, vce in ps: # voice id, voice parse tree
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2043 pname, psubnm, voicedef = vdefs [vid] # part name
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2044 attrmap ['V'] = voicedef # abc text of first voice definition (after V:vid) or empty
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2045 pid = 'P%s' % vid # let part id start with an alpha
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2046 s.vid = vid # avoid parameter passing, needed in mkNote for instrument id
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2047 s.pid = s.vcepid [s.vid] # xml part-id for the current voice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2048 s.gTime = (0, 0) # reset time
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2049 s.strAlloc.beginZoek () # reset search index
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2050 part = s.mkPart (vce, pid, lev + 1, attrmap, s.gNstaves.get (vid, 0), rOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2051 if 'Q' in attrmap: del attrmap ['Q'] # header tempo only in first part
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2052 parts.append (part)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2053 vids.append (vid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2054 partAttr [vid] = (pname, psubnm, s.midprg)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2055 if s.midprg != ['', '', '', ''] and not s.percVoice: # when a part has only rests
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2056 instId = 'I%s-%s' % (s.pid, s.vid)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2057 if instId not in s.midiInst: s.midiInst [instId] = (s.pid, s.vid, s.midprg [0], s.midprg [1], s.midprg [2], s.midprg [3])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2058 parts, vidsnew = mergeParts (parts, vids, s.staves, rOpt) # merge parts into staves as indicated by %%score
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2059 parts, vidsnew = mergeParts (parts, vidsnew, s.grands, rOpt, 1) # merge grand staves
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2060 reduceMids (parts, vidsnew, s.midiInst)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2061
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2062 s.mkIdentification (score, lev)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2063 s.mkDefaults (score, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2064
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2065 partlist = s.mkPartlist (vids, partAttr, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2066 addElem (score, partlist, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2067 for ip, part in enumerate (parts): addElem (score, part, lev + 1)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2068
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2069 return score
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2070
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2071
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2072 def decodeInput (data_string):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2073 try: enc = 'utf-8'; unicode_string = data_string.decode (enc)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2074 except:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2075 try: enc = 'latin-1'; unicode_string = data_string.decode (enc)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2076 except: raise ValueError ('data not encoded in utf-8 nor in latin-1')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2077 info ('decoded from %s' % enc)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2078 return unicode_string
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2079
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2080 def ggd (a, b): # greatest common divisor
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2081 return a if b == 0 else ggd (b, a % b)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2082
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2083 xmlVersion = "<?xml version='1.0' encoding='utf-8'?>"
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2084 def fixDoctype (elem):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2085 if python3: xs = E.tostring (elem, encoding='unicode') # writing to file will auto-encode to utf-8
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2086 else: xs = E.tostring (elem, encoding='utf-8') # keep the string utf-8 encoded for writing to file
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2087 ys = xs.split ('\n')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2088 ys.insert (0, xmlVersion) # crooked logic of ElementTree lib
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2089 ys.insert (1, '<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2090 return '\n'.join (ys)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2091
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2092 def xml2mxl (pad, fnm, data): # write xml data to compressed .mxl file
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2093 from zipfile import ZipFile, ZIP_DEFLATED
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2094 fnmext = fnm + '.xml' # file name with extension, relative to the root within the archive
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2095 outfile = os.path.join (pad, fnm + '.mxl')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2096 meta = '%s\n<container><rootfiles>\n' % xmlVersion
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2097 meta += '<rootfile full-path="%s" media-type="application/vnd.recordare.musicxml+xml"/>\n' % fnmext
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2098 meta += '</rootfiles></container>'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2099 f = ZipFile (outfile, 'w', ZIP_DEFLATED)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2100 f.writestr ('META-INF/container.xml', meta)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2101 f.writestr (fnmext, data)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2102 f.close ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2103 info ('%s written' % outfile, warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2104
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2105 def convert (pad, fnm, abc_string, mxl, rOpt=False, tOpt=False, bOpt=False, fOpt=False): # not used, backwards compatibility
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2106 score = mxm.parse (abc_string, rOpt, bOpt, fOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2107 writefile (pad, fnm, '', score, mxl, tOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2108
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2109 def writefile (pad, fnm, fnmNum, xmldoc, mxlOpt, tOpt=False):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2110 ipad, ifnm = os.path.split (fnm) # base name of input path is
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2111 if tOpt:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2112 x = xmldoc.findtext ('work/work-title', 'no_title')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2113 ifnm = x.replace (',','_').replace ("'",'_').replace ('?','_')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2114 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2115 ifnm += fnmNum
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2116 xmlstr = fixDoctype (xmldoc)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2117 if pad:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2118 if not mxlOpt or mxlOpt in ['a', 'add']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2119 outfnm = os.path.join (pad, ifnm + '.xml') # joined with path from -o option
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2120 outfile = open (outfnm, 'w')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2121 outfile.write (xmlstr)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2122 outfile.close ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2123 info ('%s written' % outfnm, warn=0)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2124 if mxlOpt: xml2mxl (pad, ifnm, xmlstr) # also write a compressed version
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2125 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2126 outfile = sys.stdout
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2127 outfile.write (xmlstr)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2128 outfile.write ('\n')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2129
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2130 def readfile (fnmext, errmsg='read error: '):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2131 try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2132 if fnmext == '-.abc': fobj = stdin # see python2/3 differences
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2133 else: fobj = open (fnmext, 'rb')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2134 encoded_data = fobj.read ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2135 fobj.close ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2136 return encoded_data if type (encoded_data) == uni_type else decodeInput (encoded_data)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2137 except Exception as e:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2138 info (errmsg + repr (e) + ' ' + fnmext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2139 return None
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2140
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2141 def expand_abc_include (abctxt):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2142 ys = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2143 for x in abctxt.splitlines ():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2144 if x.startswith ('%%abc-include') or x.startswith ('I:abc-include'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2145 x = readfile (x[13:].strip (), 'include error: ')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2146 if x != None: ys.append (x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2147 return '\n'.join (ys)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2148
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2149 abc_header, abc_voice, abc_scoredef, abc_percmap = abc_grammar () # compute grammars only once
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2150 mxm = MusicXml () # same for instance of MusicXml
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2151
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2152 def getXmlScores (abc_string, skip=0, num=1, rOpt=False, bOpt=False, fOpt=False): # not used, backwards compatibility
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2153 return [fixDoctype (xml_doc) for xml_doc in
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2154 getXmlDocs (abc_string, skip=0, num=1, rOpt=False, bOpt=False, fOpt=False)]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2155
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2156 def getXmlDocs (abc_string, skip=0, num=1, rOpt=False, bOpt=False, fOpt=False): # added by David Randolph
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2157 xml_docs = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2158 abctext = expand_abc_include (abc_string)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2159 fragments = re.split ('^\s*X:', abctext, flags=re.M)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2160 preamble = fragments [0] # tunes can be preceeded by formatting instructions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2161 tunes = fragments[1:]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2162 if not tunes and preamble: tunes, preamble = ['1\n' + preamble], '' # tune without X:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2163 for itune, tune in enumerate (tunes):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2164 if itune < skip: continue # skip tunes, then read at most num tunes
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2165 if itune >= skip + num: break
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2166 tune = preamble + 'X:' + tune # restore preamble before each tune
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2167 try: # convert string abctext -> file pad/fnmNum.xml
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2168 score = mxm.parse (tune, rOpt, bOpt, fOpt)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2169 ds = list (score.iter ('duration')) # need to iterate twice
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2170 ss = [int (d.text) for d in ds]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2171 deler = reduce (ggd, ss + [21]) # greatest common divisor of all durations
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2172 for i, d in enumerate (ds): d.text = str (ss [i] // deler)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2173 for d in score.iter ('divisions'): d.text = str (int (d.text) // deler)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2174 xml_docs.append (score)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2175 except ParseException:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2176 pass # output already printed
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2177 except Exception as err:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2178 info ('an exception occurred.\n%s' % err)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2179 return xml_docs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2180
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2181 #----------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2182 # Main Program
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2183 #----------------
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2184 if __name__ == '__main__':
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2185 from optparse import OptionParser
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2186 from glob import glob
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2187 import time
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2188
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2189 parser = OptionParser (usage='%prog [-h] [-r] [-t] [-b] [-m SKIP NUM] [-o DIR] [-p PFMT] [-z MODE] [--meta MAP] <file1> [<file2> ...]', version='version %d' % VERSION)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2190 parser.add_option ("-o", action="store", help="store xml files in DIR", default='', metavar='DIR')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2191 parser.add_option ("-m", action="store", help="skip SKIP (0) tunes, then read at most NUM (1) tunes", nargs=2, type='int', default=(0,1), metavar='SKIP NUM')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2192 parser.add_option ("-p", action="store", help="pageformat PFMT (mm) = scale (0.75), pageheight (297), pagewidth (210), leftmargin (18), rightmargin (18), topmargin (10), botmargin (10)", default='', metavar='PFMT')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2193 parser.add_option ("-z", "--mxl", dest="mxl", help="store as compressed mxl, MODE = a(dd) or r(eplace)", default='', metavar='MODE')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2194 parser.add_option ("-r", action="store_true", help="show whole measure rests in merged staffs", default=False)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2195 parser.add_option ("-t", action="store_true", help="use tune title as file name", default=False)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2196 parser.add_option ("-b", action="store_true", help="line break at EOL", default=False)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2197 parser.add_option ("--meta", action="store", help="map infofields to XML metadata, MAP = R:poet,Z:lyricist,N:...", default='', metavar='MAP')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2198 parser.add_option ("-f", action="store_true", help="force string/fret allocations for tab staves", default=False)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2199 options, args = parser.parse_args ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2200 if len (args) == 0: parser.error ('no input file given')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2201 pad = options.o
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2202 if options.mxl and options.mxl not in ['a','add', 'r', 'replace']:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2203 parser.error ('MODE should be a(dd) or r(eplace), not: %s' % options.mxl)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2204 if pad:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2205 if not os.path.exists (pad): os.mkdir (pad)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2206 if not os.path.isdir (pad): parser.error ('%s is not a directory' % pad)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2207 if options.p: # set page formatting values
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2208 try: # space, page-height, -width, margin-left, -right, -top, -bottom
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2209 mxm.pageFmtCmd = lmap (float, options.p.split (','))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2210 if len (mxm.pageFmtCmd) != 7: raise ValueError ('-p needs 7 values')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2211 except Exception as err: parser.error (err)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2212 for x in options.meta.split (','):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2213 if not x: continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2214 try: field, tag = x.split (':')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2215 except: parser.error ('--meta: %s cannot be split on colon' % x)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2216 if field not in 'OAZNGHRBDFSPW': parser.error ('--meta: field %s is no valid ABC field' % field)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2217 if tag not in mxm.metaTypes: parser.error ('--meta: tag %s is no valid XML creator type' % tag)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2218 mxm.metaMap [field] = tag
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2219 fnmext_list = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2220 for i in args:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2221 if i == '-': fnmext_list.append ('-.abc') # represents standard input
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2222 else: fnmext_list += glob (i)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2223 if not fnmext_list: parser.error ('none of the input files exist')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2224 t_start = time.time ()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2225 for fnmext in fnmext_list:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2226 fnm, ext = os.path.splitext (fnmext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2227 if ext.lower () not in ('.abc'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2228 info ('skipped input file %s, it should have extension .abc' % fnmext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2229 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2230 if os.path.isdir (fnmext):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2231 info ('skipped directory %s. Only files are accepted' % fnmext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2232 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2233 abctext = readfile (fnmext)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2234 skip, num = options.m
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2235 xml_docs = getXmlDocs (abctext, skip, num, options.r, options.b, options.f)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2236 for itune, xmldoc in enumerate (xml_docs):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2237 fnmNum = '%02d' % (itune + 1) if len (xml_docs) > 1 else ''
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2238 writefile (pad, fnm, fnmNum, xmldoc, options.mxl, options.t)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2239 info ('done in %.2f secs' % (time.time () - t_start))