annotate abc2xml/abc2xml.py @ 824:49a3173287f8 build-default-308

Replace ABC slur with intended tie. Same visual output, better MIDI.
author Jim Hague <jim.hague@acm.org>
date Thu, 14 Jun 2018 22:52:16 +0100
parents 4fab69a1027d
children b1dbb76f4eb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1 # coding=latin-1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2 '''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3 Copyright (C) 2012: Willem G. Vree
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
4 Contributions: Nils Liberg, Nicolas Froment, Norman Schmidt, Reinier Maliepaard, Martin Tarenskeen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
5
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify it under the terms of the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
7 GNU General Public License as published by the Free Software Foundation; either version 2 of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
8 the License, or (at your option) any later version.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
9
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
11 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
12 See the GNU General Public License for more details. <http://www.gnu.org/licenses/gpl.html>.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
13 '''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
14
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
15 from pyparsing import Word, OneOrMore, Optional, Literal, NotAny, MatchFirst
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
16 from pyparsing import Group, oneOf, Suppress, ZeroOrMore, Combine, FollowedBy
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
17 from pyparsing import srange, CharsNotIn, StringEnd, LineEnd, White, Regex
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
18 from pyparsing import nums, alphas, alphanums, ParseException, Forward
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
19 try: import xml.etree.cElementTree as E
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
20 except: import xml.etree.ElementTree as E
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
21 import types, sys, os, re, datetime
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
22
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
23 VERSION = 58
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
24
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
25 def info (s, warn=1):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
26 x = (warn and '-- ' or '') + s
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
27 try: sys.stderr.write (x + '\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
28 except: sys.stderr.write (repr (x) + '\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
29
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
30 def abc_grammar (): # header, voice and lyrics grammar for ABC
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
31 b1 = Word (u"-,'<>\u2019#", exact=1) # catch misplaced chars in chords
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
32
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
33 #-----------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34 # ABC header (fld_text elements are matched later with reg. epr's)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
35 #-----------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
37 number = Word (nums).setParseAction (lambda t: int (t[0]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
38 field_str = Regex (r'(?:\\.|[^]\\])*') # match anything until end of field, skip escaped \]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
39 field_str.setParseAction (lambda t: t[0].strip ()) # and strip spacing
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
40
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
41 userdef_symbol = Word (srange ('[H-Wh-w~]'), exact=1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
42 fieldId = oneOf ('K L M Q P I T C O A Z N G H R B D F S E r') # info fields
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
43 X_field = Literal ('X') + Suppress (':') + number + field_str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
44 U_field = Literal ('U') + Suppress (':') + userdef_symbol + Suppress ('=') + field_str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
45 V_field = Literal ('V') + Suppress (':') + Word (alphanums + '_') + field_str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
46 inf_fld = fieldId + Suppress (':') + field_str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
47 ifield = Suppress ('[') + (X_field | U_field | V_field | inf_fld) + Suppress (']')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
48 abc_header = OneOrMore (ifield) + StringEnd ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
49
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
50 #---------------------------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
51 # I:score with recursive part groups and {* grand staff marker
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
52 #---------------------------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
53
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
54 voiceId = Suppress (Optional ('*')) + Word (alphanums + '_')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
55 voice_gr = Suppress ('(') + OneOrMore (voiceId | Suppress ('|')) + Suppress (')')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
56 simple_part = voiceId | voice_gr | Suppress ('|')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
57 grand_staff = oneOf ('{* {') + OneOrMore (simple_part) + Suppress ('}')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
58 part = Forward ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
59 part_seq = OneOrMore (part | Suppress ('|'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
60 brace_gr = Suppress ('{') + part_seq + Suppress ('}')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
61 bracket_gr = Suppress ('[') + part_seq + Suppress ('\]') # closing brackets are escaped by splitHeaderVoices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62 part << MatchFirst (simple_part | grand_staff | brace_gr | bracket_gr | Suppress ('|'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
63 abc_scoredef = Suppress (oneOf ('staves score')) + OneOrMore (part)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
65 #---------------------------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
66 # ABC voice (not white space sensitive, beams detected in note/rest parse actions)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
67 #---------------------------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
68
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
69 inline_field = Suppress ('[') + (inf_fld | U_field | V_field) + Suppress (']')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
70
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
71 note_length = Optional (number, 1) + Group (ZeroOrMore ('/')) + Optional (number, 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
72 octaveHigh = OneOrMore ("'").setParseAction (lambda t: len(t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
73 octaveLow = OneOrMore (',').setParseAction (lambda t: -len(t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
74 octave = octaveHigh | octaveLow
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
75
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
76 basenote = oneOf ('C D E F G A B c d e f g a b y') # includes spacer for parse efficiency
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
77 accidental = oneOf ('^^ __ ^ _ =')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
78 rest_sym = oneOf ('x X z Z')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
79 slur_beg = oneOf ('( .(') + ~Word (nums) # no tuplet_start
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
80 slur_ends = OneOrMore (oneOf (') .)'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
81
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
82 long_decoration = Combine (oneOf ('! +') + CharsNotIn ('!+ \n') + oneOf ('! +'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
83 staccato = Literal ('.') + ~Literal ('|') # avoid dotted barline
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
84 decoration = staccato | userdef_symbol | long_decoration | slur_beg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
85 decorations = OneOrMore (decoration)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
86 staff_decos = decorations + ~oneOf (': | [|] []')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
87
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
88 tie = oneOf ('.- -')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
89 rest = Optional (accidental) + rest_sym + note_length
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
90 pitch = Optional (accidental) + basenote + Optional (octave, 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
91 note = pitch + note_length + Optional (tie) + Optional (slur_ends)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
92 chord_note = Optional (decorations) + pitch + note_length + Optional (tie) + Optional (slur_ends)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
93 chord_notes = OneOrMore (chord_note | rest | b1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
94 grace_notes = Forward ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
95 chord = Suppress ('[') + OneOrMore (chord_notes | grace_notes) + Suppress (']') + note_length + Optional (tie) + Optional (slur_ends)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
96 stem = note | chord | rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
97
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
98 broken = Combine (OneOrMore ('<') | OneOrMore ('>'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
99
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
100 tuplet_num = Suppress ('(') + number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
101 tuplet_into = Suppress (':') + Optional (number, 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
102 tuplet_notes = Suppress (':') + Optional (number, 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
103 tuplet_start = tuplet_num + Optional (tuplet_into + Optional (tuplet_notes))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
104
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
105 acciaccatura = Literal ('/')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
106 grace_stem = Optional (decorations) + stem
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
107 grace_notes << Group (Suppress ('{') + Optional (acciaccatura) + OneOrMore (grace_stem) + Suppress ('}'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
108
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
109 text_expression = Optional (oneOf ('^ _ < > @'), '^') + Optional (CharsNotIn ('"'), "")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
110 chord_accidental = oneOf ('# b =')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
111 triad = oneOf ('ma Maj maj M mi min m aug dim o + -')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
112 seventh = oneOf ('7 ma7 Maj7 M7 maj7 mi7 m7 dim7 o7 -7 aug7 +7 m7b5 mi7b5')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
113 sixth = oneOf ('6 ma6 M6 m6 mi6')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
114 ninth = oneOf ('9 ma9 M9 maj9 Maj9 mi9 m9')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
115 elevn = oneOf ('11 ma11 M11 maj11 Maj11 mi m11')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
116 suspended = oneOf ('sus sus2 sus4')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
117 chord_degree = Combine (Optional (chord_accidental) + oneOf ('2 4 5 6 7 9 11 13'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
118 chord_kind = Optional (seventh | sixth | ninth | elevn | triad, '_') + Optional (suspended)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
119 chord_root = oneOf ('C D E F G A B') + Optional (chord_accidental)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
120 chord_bass = oneOf ('C D E F G A B') + Optional (chord_accidental) # needs a different parse action
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
121 chordsym = chord_root + chord_kind + ZeroOrMore (chord_degree) + Optional (Suppress ('/') + chord_bass)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
122 chord_sym = chordsym + Optional (Literal ('(') + CharsNotIn (')') + Literal (')')).suppress ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
123 chord_or_text = Suppress ('"') + (chord_sym ^ text_expression) + Suppress ('"')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
124
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
125 volta_nums = Optional ('[').suppress () + Combine (Word (nums) + ZeroOrMore (oneOf (', -') + Word (nums)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
126 volta_text = Literal ('[').suppress () + Regex (r'"[^"]+"')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
127 volta = volta_nums | volta_text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
128 invisible_barline = oneOf ('[|] []')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
129 dashed_barline = oneOf (': .|')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
130 double_rep = Literal (':') + FollowedBy (':') # otherwise ambiguity with dashed barline
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
131 voice_overlay = Combine (OneOrMore ('&'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
132 bare_volta = FollowedBy (Literal ('[') + Word (nums)) # no barline, but volta follows (volta is parsed in next measure)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
133 bar_left = (oneOf ('[|: |: [: :') + Optional (volta)) | Optional ('|').suppress () + volta | oneOf ('| [|')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
134 bars = ZeroOrMore (':') + ZeroOrMore ('[') + OneOrMore (oneOf ('| ]'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
135 bar_right = Optional (decorations) + (invisible_barline | double_rep | Combine (bars) | dashed_barline | voice_overlay | bare_volta)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
136
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
137 errors = ~bar_right + Optional (Word (' \n')) + CharsNotIn (':&|', exact=1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
138 linebreak = Literal ('$') | ~decorations + Literal ('!') # no need for I:linebreak !!!
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
139 element = inline_field | broken | staff_decos | stem | chord_or_text | grace_notes | tuplet_start | linebreak | errors
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
140 measure = Group (ZeroOrMore (inline_field) + Optional (bar_left) + ZeroOrMore (element) + bar_right + Optional (linebreak))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
141 noBarMeasure = Group (ZeroOrMore (inline_field) + Optional (bar_left) + OneOrMore (element) + Optional (linebreak))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
142 abc_voice = ZeroOrMore (measure) + Optional (noBarMeasure | Group (bar_left)) + ZeroOrMore (inline_field).suppress () + StringEnd ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
143
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
144 #----------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
145 # ABC lyric lines (white space sensitive)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
146 #----------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
147
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
148 skip_note = oneOf ('* - ~')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
149 extend_note = Literal ('_')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
150 measure_end = Literal ('|')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
151 syl_chars = CharsNotIn ('*~-_| \t\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
152 white = Word (' \t')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
153 syllable = Combine (Optional ('~') + syl_chars + ZeroOrMore (Literal ('~') + syl_chars)) + Optional ('-')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
154 lyr_elem = (syllable | skip_note | extend_note | measure_end) + Optional (white).suppress ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
155 lyr_head = (Literal ('w:') + Optional (white)).suppress ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
156 lyr_line = Group (lyr_head + ZeroOrMore (lyr_elem) + LineEnd ().suppress ())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
157
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
158 #----------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
159 # Parse actions to convert all relevant results into an abstract
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
160 # syntax tree where all tree nodes are instances of pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
161 #----------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
162
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
163 ifield.setParseAction (lambda t: pObj ('field', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
164 grand_staff.setParseAction (lambda t: pObj ('grand', t, 1)) # 1 = keep ordered list of results
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
165 brace_gr.setParseAction (lambda t: pObj ('bracegr', t, 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
166 bracket_gr.setParseAction (lambda t: pObj ('bracketgr', t, 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
167 voice_gr.setParseAction (lambda t: pObj ('voicegr', t, 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
168 voiceId.setParseAction (lambda t: pObj ('vid', t, 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
169 abc_scoredef.setParseAction (lambda t: pObj ('score', t, 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
170 note_length.setParseAction (lambda t: pObj ('dur', (t[0], (t[2] << len (t[1])) >> 1)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
171 chordsym.setParseAction (lambda t: pObj ('chordsym', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
172 chord_root.setParseAction (lambda t: pObj ('root', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
173 chord_kind.setParseAction (lambda t: pObj ('kind', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
174 chord_degree.setParseAction (lambda t: pObj ('degree', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
175 chord_bass.setParseAction (lambda t: pObj ('bass', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
176 text_expression.setParseAction (lambda t: pObj ('text', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
177 inline_field.setParseAction (lambda t: pObj ('inline', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
178 grace_notes.setParseAction (doGrace) # (lambda t: pObj ('grace', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
179 acciaccatura.setParseAction (lambda t: pObj ('accia', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
180 note.setParseAction (noteActn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
181 chord_note.setParseAction (noteActn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
182 rest.setParseAction (restActn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
183 decorations.setParseAction (lambda t: pObj ('deco', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
184 slur_ends.setParseAction (lambda t: pObj ('slurs', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
185 chord.setParseAction (lambda t: pObj ('chord', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
186 tie.setParseAction (lambda t: pObj ('tie', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
187 pitch.setParseAction (lambda t: pObj ('pitch', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
188 bare_volta.setParseAction (lambda t: ['|']) # return barline that user forgot
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
189 dashed_barline.setParseAction (lambda t: ['.|'])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
190 bar_right.setParseAction (lambda t: pObj ('rbar', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
191 bar_left.setParseAction (lambda t: pObj ('lbar', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
192 broken.setParseAction (lambda t: pObj ('broken', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
193 tuplet_start.setParseAction (lambda t: pObj ('tup', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
194 linebreak.setParseAction (lambda t: pObj ('linebrk', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
195 measure.setParseAction (doMaat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
196 noBarMeasure.setParseAction (doMaat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
197 syllable.setParseAction (lambda t: pObj ('syl', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
198 skip_note.setParseAction (lambda t: pObj ('skip', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
199 extend_note.setParseAction (lambda t: pObj ('ext', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
200 measure_end.setParseAction (lambda t: pObj ('sbar', t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
201 b1.setParseAction (errorWarn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
202 errors.setParseAction (errorWarn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
203 lyr_block = OneOrMore (lyr_line).leaveWhitespace () # after leaveWhiteSpace no more parse actions can be set!!
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
204
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
205 return abc_header, abc_voice, lyr_block, abc_scoredef
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
206
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
207 class pObj (object): # every relevant parse result is converted into a pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
208 def __init__ (s, name, t, seq=0): # t = list of nested parse results
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
209 s.name = name # name uniqueliy identifies this pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
210 rest = [] # collect parse results that are not a pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
211 attrs = {} # new attributes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
212 for x in t: # nested pObj's become attributes of this pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
213 if type (x) == pObj:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
214 attrs [x.name] = attrs.get (x.name, []) + [x]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
215 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
216 rest.append (x) # collect non-pObj's (mostly literals)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
217 for name, xs in attrs.items ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
218 if len (xs) == 1: xs = xs[0] # only list if more then one pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
219 setattr (s, name, xs) # create the new attributes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
220 s.t = rest # all nested non-pObj's (mostly literals)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
221 s.objs = seq and t or [] # for nested ordered (lyric) pObj's
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
222
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
223 def __repr__ (s): # make a nice string representation of a pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
224 r = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
225 for nm in dir (s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
226 if nm.startswith ('_'): continue # skip build in attributes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
227 elif nm == 'name': continue # redundant
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
228 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
229 x = getattr (s, nm)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
230 if not x: continue # s.t may be empty (list of non-pObj's)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
231 if type (x) == types.ListType: r.extend (x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
232 else: r.append (x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
233 xs = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
234 for x in r: # recursively call __repr__ and convert all strings to latin-1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
235 if isinstance (x, types.StringTypes):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
236 try: xs.append (x.encode ('latin-1'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
237 except: xs.append (repr (x)) # string -> no recursion
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
238 else: xs.append (repr (x)) # pObj -> recursive call
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
239 return '(' + s.name + ' ' +','.join (xs) + ')'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
240
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
241 global prevloc # global to remember previous match position of a note/rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
242 prevloc = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
243 def detectBeamBreak (line, loc, t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
244 global prevloc # location in string 'line' of previous note match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
245 xs = line[prevloc:loc+1] # string between previous and current note match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
246 xs = xs.lstrip () # first note match starts on a space!
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
247 prevloc = loc # location in string 'line' of current note match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
248 b = pObj ('bbrk', [' ' in xs]) # space somewhere between two notes -> beambreak
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
249 t.insert (0, b) # insert beambreak as a nested parse result
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
250
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
251 def noteActn (line, loc, t): # detect beambreak between previous and current note/rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
252 if 'y' in t[0].t: return [] # discard spacer
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
253 detectBeamBreak (line, loc, t) # adds beambreak to parse result t as side effect
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
254 return pObj ('note', t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
255
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
256 def restActn (line, loc, t): # detect beambreak between previous and current note/rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
257 detectBeamBreak (line, loc, t) # adds beambreak to parse result t as side effect
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
258 return pObj ('rest', t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
259
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
260 def errorWarn (line, loc, t): # warning for misplaced symbols and skip them
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
261 info ('**misplaced symbol: %s' % t[0], warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
262 lineCopy = line [:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
263 if loc > 40:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
264 lineCopy = line [loc - 40: loc + 40]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
265 loc = 40
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
266 info (lineCopy.replace ('\n', ' '), warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
267 info (loc * '-' + '^', warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
268 return []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
269
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
270 #-------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
271 # transformations of a measure (called by parse action doMaat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
272 #-------------------------------------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
273
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
274 def simplify (a, b): # divide a and b by their greatest common divisor
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
275 x, y = a, b
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
276 while b: a, b = b, a % b
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
277 return x / a, y / a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
278
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
279 def doBroken (prev, brk, x):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
280 if not prev: info ('error in broken rhythm: %s' % x); return # no changes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
281 nom1, den1 = prev.dur.t # duration of first note/chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
282 nom2, den2 = x.dur.t # duration of second note/chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
283 if brk == '>':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
284 nom1, den1 = simplify (3 * nom1, 2 * den1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
285 nom2, den2 = simplify (1 * nom2, 2 * den2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
286 elif brk == '<':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
287 nom1, den1 = simplify (1 * nom1, 2 * den1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
288 nom2, den2 = simplify (3 * nom2, 2 * den2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
289 elif brk == '>>':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
290 nom1, den1 = simplify (7 * nom1, 4 * den1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
291 nom2, den2 = simplify (1 * nom2, 4 * den2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
292 elif brk == '<<':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
293 nom1, den1 = simplify (1 * nom1, 4 * den1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
294 nom2, den2 = simplify (7 * nom2, 4 * den2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
295 else: return # give up
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
296 prev.dur.t = nom1, den1 # change duration of previous note/chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
297 x.dur.t = nom2, den2 # and current note/chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
298
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
299 def convertBroken (t): # convert broken rhythms to normal note durations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
300 prev = None # the last note/chord before the broken symbol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
301 brk = '' # the broken symbol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
302 remove = [] # indexes to broken symbols (to be deleted) in measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
303 for i, x in enumerate (t): # scan all elements in measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
304 if x.name == 'note' or x.name == 'chord' or x.name == 'rest':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
305 if brk: # a broken symbol was encountered before
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
306 doBroken (prev, brk, x) # change duration previous note/chord/rest and current one
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
307 brk = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
308 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
309 prev = x # remember the last note/chord/rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
310 elif x.name == 'broken':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
311 brk = x.t[0] # remember the broken symbol (=string)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
312 remove.insert (0, i) # and its index, highest index first
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
313 for i in remove: del t[i] # delete broken symbols from high to low
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
314
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
315 def convertChord (t): # convert chord to sequence of notes in musicXml-style
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
316 ins = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
317 for i, x in enumerate (t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
318 if x.name == 'chord':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
319 if hasattr (x, 'rest') and not hasattr (x, 'note'): # chords containing only rests
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
320 if type (x.rest) == types.ListType: x.rest = x.rest[0] # more rests == one rest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
321 ins.insert (0, (i, [x.rest])) # just output a single rest, no chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
322 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
323 num1, den1 = x.dur.t # chord duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
324 tie = getattr (x, 'tie', None) # chord tie
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
325 slurs = getattr (x, 'slurs', []) # slur endings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
326 deco = getattr (x, 'deco', []) # chord decorations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
327 if type (x.note) != types.ListType: x.note = [x.note] # when chord has only one note ...
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
328 for j, nt in enumerate (x.note): # all notes of the chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
329 num2, den2 = nt.dur.t # note duration * chord duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
330 nt.dur.t = simplify (num1 * num2, den1 * den2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
331 if tie: nt.tie = tie # tie on all chord notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
332 if j == 0 and deco: nt.deco = deco # decorations only on first chord note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
333 if j == 0 and slurs: nt.slurs = slurs # slur endings only on first chord note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
334 if j > 0: nt.chord = pObj ('chord', [1]) # label all but first as chord notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
335 else: # remember all pitches of the chord in the first note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
336 pitches = [n.pitch for n in x.note] # to implement conversion of erroneous ties to slurs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
337 nt.pitches = pObj ('pitches', pitches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
338 ins.insert (0, (i, x.note)) # high index first
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
339 for i, notes in ins: # insert from high to low
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
340 for nt in reversed (notes):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
341 t.insert (i+1, nt) # insert chord notes after chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
342 del t[i] # remove chord itself
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
343
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
344 def doMaat (t): # t is a Group() result -> the measure is in t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
345 convertBroken (t[0]) # remove all broken rhythms and convert to normal durations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
346 convertChord (t[0]) # replace chords by note sequences in musicXML style
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
347
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
348 def doGrace (t): # t is a Group() result -> the grace sequence is in t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
349 convertChord (t[0]) # a grace sequence may have chords
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
350 for nt in t[0]: # flag all notes within the grace sequence
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
351 if nt.name == 'note': nt.grace = 1 # set grace attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
352 return t[0] # ungroup the parse result
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
353 #--------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
354 # musicXML generation
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
355 #----------------------------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
356
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
357 def compChordTab (): # avoid some typing work: returns mapping constant {ABC chordsyms -> musicXML kind}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
358 maj, min, aug, dim, dom, ch7, ch6, ch9, ch11, hd = 'major minor augmented diminished dominant -seventh -sixth -ninth -11th half-diminished'.split ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
359 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])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
360 seventh = zip ('7 ma7 Maj7 M7 maj7 mi7 m7 dim7 o7 -7 aug7 +7 m7b5 mi7b5'.split (),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
361 [dom, maj+ch7, maj+ch7, maj+ch7, maj+ch7, min+ch7, min+ch7, dim+ch7, dim+ch7, min+ch7, aug+ch7, aug+ch7, hd, hd])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
362 sixth = zip ('6 ma6 M6 mi6 m6'.split (), [maj+ch6, maj+ch6, maj+ch6, min+ch6, min+ch6])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
363 ninth = zip ('9 ma9 M9 maj9 Maj9 mi9 m9'.split (), [dom+ch9, maj+ch9, maj+ch9, maj+ch9, maj+ch9, min+ch9, min+ch9])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
364 elevn = zip ('11 ma11 M11 maj11 Maj11 mi11 m11'.split (), [dom+ch11, maj+ch11, maj+ch11, maj+ch11, maj+ch11, min+ch11, min+ch11])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
365 return dict (triad + seventh + sixth + ninth + elevn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
366
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
367 def addElem (parent, child, level):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
368 indent = 2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
369 chldrn = parent.getchildren ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
370 if chldrn:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
371 chldrn[-1].tail += indent * ' '
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
372 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
373 parent.text = '\n' + level * indent * ' '
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
374 parent.append (child)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
375 child.tail = '\n' + (level-1) * indent * ' '
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
376
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
377 def addElemT (parent, tag, text, level):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
378 e = E.Element (tag)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
379 e.text = text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
380 addElem (parent, e, level)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
381
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
382 def mkTmod (tmnum, tmden, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
383 tmod = E.Element ('time-modification')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
384 addElemT (tmod, 'actual-notes', str (tmnum), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
385 addElemT (tmod, 'normal-notes', str (tmden), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
386 return tmod
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
387
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
388 def addDirection (parent, elem, lev, gstaff, subelms=[], placement='below', cue_on=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
389 dir = E.Element ('direction', placement=placement)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
390 addElem (parent, dir, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
391 typ = E.Element ('direction-type')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
392 addElem (dir, typ, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
393 addElem (typ, elem, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
394 for subel in subelms: addElem (elem, subel, lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
395 if cue_on: addElem (dir, E.Element ('level', size='cue'), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
396 if gstaff: addElemT (dir, 'staff', str (gstaff), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
397 return dir
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
398
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
399 def removeElems (root_elem, parent_str, elem_str):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
400 for p in root_elem.findall (parent_str):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
401 e = p.find (elem_str)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
402 if e != None: p.remove (e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
403
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
404 def alignLyr (vce, lyrs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
405 empty_el = pObj ('leeg', '*')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
406 for k, lyr in enumerate (lyrs): # lyr = one full line of lyrics
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
407 i = 0 # syl counter
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
408 for msre in vce: # reiterate the voice block for each lyrics line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
409 for elem in msre:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
410 if elem.name == 'note' and not (hasattr (elem, 'chord') or hasattr (elem, 'grace')):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
411 if i >= len (lyr): lr = empty_el
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
412 else: lr = lyr [i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
413 elem.objs.append (lr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
414 if lr.name != 'sbar': i += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
415 if i < len (lyr) and lyr[i].name == 'sbar': i += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
416 return vce
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
417
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
418 slur_move = re.compile (r'(?<![!+])([}><][<>]?)(\)+)') # (?<!...) means: not preceeded by ...
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
419 mm_rest = re.compile (r'([XZ])(\d+)')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
420 bar_space = re.compile (r'([:|][ |\[\]]+[:|])') # barlines with spaces
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
421 def fixSlurs (x): # repair slurs when after broken sign or grace-close
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
422 def f (mo): # replace a multi-measure rest by single measure rests
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
423 n = int (mo.group (2))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
424 return (n * (mo.group (1) + '|')) [:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
425 def g (mo): # squash spaces in barline expressions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
426 return mo.group (1).replace (' ','')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
427 x = mm_rest.sub (f, x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
428 x = bar_space.sub (g, x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
429 return slur_move.sub (r'\2\1', x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
430
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
431 def splitHeaderVoices (abctext):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
432 r1 = re.compile (r'%.*$') # comments
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
433 r2 = re.compile (r'^[A-Z]:.*$') # information field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
434 r3 = re.compile (r'^%%(?=[^%])') # directive: ^%% folowed by not a %
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
435 xs, nx = [], 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
436 for x in abctext.splitlines ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
437 x = x.strip ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
438 if not x and nx == 1: break # end of tune
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
439 x = r3.sub ('I:', x) # replace %% -> I:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
440 x2 = r1.sub ('', x) # remove comment
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
441 while x2.endswith ('*'): x2 = x2[:-1] # remove old syntax for right adjusting
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
442 if not x2: continue # empty line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
443 if x2[:2] == 'W:': continue # skip W: lyrics
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
444 if x2[:2] == 'w:' and xs[-1][-1] == '\\':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
445 xs[-1] = xs[-1][:-1] # ignore line continuation before lyrics line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
446 ro = r2.match (x2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
447 if ro: # field -> inline_field, escape all ']'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
448 if x2[-1] == '\\': x2 = x2[:-1] # ignore continuation after field line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
449 x2 = '[' + x2.replace (']',r'\]') + ']'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
450 if x2[:2] == '+:': # new style continuation
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
451 xs[-1] += x2[2:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
452 elif xs and xs[-1][-1] == '\\': # old style continuation
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
453 xs[-1] = xs[-1][:-1] + x2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
454 else: # skip lines (except I:) until first X:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
455 if x.startswith ('X:'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
456 if nx == 1: break # second tune starts without an empty line !!
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
457 nx = 1 # start of first tune
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
458 if nx == 1 or x.startswith ('I:'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
459 xs.append (x2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
460 if xs and xs[-1][-1] == '\\': # nothing left to continue with, remove last continuation
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
461 xs[-1] = xs[-1][:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
462
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
463 r1 = re.compile (r'\[[A-Z]:(\\.|[^]\\])*\]') # inline field with escaped ']'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
464 r2 = re.compile (r'\[K:') # start of K: field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
465 r3 = re.compile (r'\[V:|\[I:MIDI') # start of V: field or midi field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
466 fields, voices, b = [], [], 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
467 for i, x in enumerate (xs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
468 n = len (r1.sub ('', x)) # remove all inline fields
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
469 if n > 0: b = 1; break # real abc present -> end of header
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
470 if r2.search (x): # start of K: field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
471 fields.append (x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
472 i += 1; b = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
473 break # first K: field -> end of header
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
474 if r3.search (x): # start of V: field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
475 voices.append (x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
476 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
477 fields.append (x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
478 if b: voices += xs[i:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
479 else: voices += [] # tune has only header fields
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
480 header = '\n'.join (fields)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
481 abctext = '\n'.join (voices)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
482
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
483 xs = abctext.split ('[V:')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
484 if len (xs) == 1: abctext = '[V:1]' + abctext # abc has no voice defs at all
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
485 elif r1.sub ('', xs[0]).strip (): # remove inline fields from starting text, if any
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
486 abctext = '[V:1]' + abctext # abc with voices has no V: at start
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
487
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
488 r1 = re.compile (r'\[V:\s*(\S*)[ \]]') # get voice id from V: field (skip spaces betwee V: and ID)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
489 vmap = {} # {voice id -> [voice abc string]}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
490 vorder = {} # mark document order of voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
491 xs = re.split (r'(\[V:[^]]*\])', abctext) # split on every V-field (V-fields included in split result list)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
492 if len (xs) == 1: raise (Exception ('bugs ...'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
493 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
494 header += xs[0] # xs[0] = text between K: and first V:, normally empty, but we put it in the header
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
495 i = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
496 while i < len (xs): # xs = ['', V-field, voice abc, V-field, voice abc, ...]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
497 vce, abc = xs[i:i+2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
498 id = r1.search (vce).group (1) # get voice ID from V-field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
499 vmap[id] = vmap.get (id, []) + [vce, abc] # collect abc-text for each voice id (include V-fields)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
500 if id not in vorder: vorder [id] = i # store document order of first occurrence of voice id
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
501 i += 2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
502 voices = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
503 ixs = sorted ([(i, id) for id, i in vorder.items ()]) # restore document order of voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
504 for i, id in ixs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
505 voice = ''.join (vmap [id]) # all abc of one voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
506 xs = re.split (r'((?:\nw:[^\n]*)+)', voice) # split voice into voice-lyrics blocks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
507 if len (xs) == 1: # no lyrics
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
508 voice = fixSlurs (xs[0]) # put slurs right after the notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
509 vce_lyr = [[voice, '']]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
510 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
511 if xs[-1].strip () != '': xs.append ('w:') # last block had no lyrics
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
512 vce_lyr = [] # [[voice, lyrics],[],...] list of voice-lyrics blocks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
513 for k in range (0, len (xs) - 1, 2):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
514 voice, lyrics = xs [k:k+2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
515 voice = fixSlurs (voice) # put slurs right after the notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
516 vce_lyr.append ((voice, lyrics))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
517 voices.append ((id, vce_lyr))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
518 return header, voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
519
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
520 def mergeMeasure (m1, m2, slur_offset, voice_offset, is_grand=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
521 slurs = m2.findall ('note/notations/slur')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
522 for slr in slurs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
523 slrnum = int (slr.get ('number')) + slur_offset
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
524 slr.set ('number', str (slrnum)) # make unique slurnums in m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
525 vs = m2.findall ('note/voice') # set all voice number elements in m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
526 for v in vs: v.text = str (voice_offset + int (v.text))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
527 ls = m1.findall ('note/lyric') # all lyric elements in m1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
528 lnum_max = max ([int (l.get ('number')) for l in ls] + [0]) # highest lyric number in m1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
529 ls = m2.findall ('note/lyric') # update lyric elements in m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
530 for el in ls:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
531 n = int (el.get ('number'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
532 el.set ('number', str (n + lnum_max))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
533 ns = m1.findall ('note') # determine the total duration of m1, subtract all backups
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
534 dur1 = sum (int (n.find ('duration').text) for n in ns
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
535 if n.find ('grace') == None and n.find ('chord') == None)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
536 dur1 -= sum (int (b.text) for b in m1.findall ('backup/duration'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
537 nns, es = 0, [] # nns = number of real notes in m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
538 for e in m2.getchildren (): # scan all elements of m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
539 if e.tag == 'attributes':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
540 if not is_grand: continue # no attribute merging for normal voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
541 else: nns += 1 # but we do merge (clef) attributes for a grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
542 if e.tag == 'print': continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
543 if e.tag == 'note' and (mxm.gmwr or e.find ('rest') == None): nns += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
544 es.append (e) # buffer elements to be merged
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
545 if nns > 0: # only merge if m2 contains any real notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
546 if dur1 > 0: # only insert backup if duration of m1 > 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
547 b = E.Element ('backup')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
548 addElem (m1, b, level=3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
549 addElemT (b, 'duration', str (dur1), level=4)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
550 for e in es: addElem (m1, e, level=3) # merge buffered elements of m2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
551
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
552 def mergePartList (parts, is_grand=0): # merge parts, make grand staff when is_grand true
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
553
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
554 def delAttrs (part): # for the time being we only keep clef attributes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
555 xs = [(m, e) for m in part.findall ('measure') for e in m.findall ('attributes')]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
556 for m, e in xs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
557 for c in e.getchildren ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
558 if c.tag == 'clef': continue # keep clef attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
559 e.remove (c) # delete all other attrinutes for higher staff numbers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
560 if len (e.getchildren ()) == 0: m.remove (e) # remove empty attributes element
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
561
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
562 p1 = parts[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
563 for p2 in parts[1:]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
564 if is_grand: delAttrs (p2) # delete all attributes except clef
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
565 for i in range (len (p1) + 1, len (p2) + 1): # second part longer than first one
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
566 maat = E.Element ('measure', number = str(i)) # append empty measures
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
567 addElem (p1, maat, 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
568 slurs = p1.findall ('measure/note/notations/slur') # find highest slur num in first part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
569 slur_max = max ([int (slr.get ('number')) for slr in slurs] + [0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
570 vs = p1.findall ('measure/note/voice') # all voice number elements in first part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
571 vnum_max = max ([int (v.text) for v in vs] + [0]) # highest voice number in first part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
572 for im, m2 in enumerate (p2.findall ('measure')): # merge all measures of p2 into p1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
573 mergeMeasure (p1[im], m2, slur_max, vnum_max, is_grand) # may change slur numbers in p1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
574 return p1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
575
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
576 def mergeParts (parts, vids, staves, is_grand=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
577 if not staves: return parts, vids # no voice mapping
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
578 partsnew, vidsnew = [], []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
579 for voice_ids in staves:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
580 pixs = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
581 for vid in voice_ids:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
582 if vid in vids: pixs.append (vids.index (vid))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
583 else: info ('score partname %s does not exist' % vid)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
584 if pixs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
585 xparts = [parts[pix] for pix in pixs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
586 if len (xparts) > 1: mergedpart = mergePartList (xparts, is_grand)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
587 else: mergedpart = xparts [0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
588 partsnew.append (mergedpart)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
589 vidsnew.append (vids [pixs[0]])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
590 return partsnew, vidsnew
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
591
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
592 def mergePartMeasure (part, msre, ovrlaynum): # merge msre into last measure of part, only for overlays
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
593 slurs = part.findall ('measure/note/notations/slur') # find highest slur num in part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
594 slur_max = max ([int (slr.get ('number')) for slr in slurs] + [0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
595 last_msre = part.getchildren ()[-1] # last measure in part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
596 mergeMeasure (last_msre, msre, slur_max, ovrlaynum) # voice offset = s.overlayVNum
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
597
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
598 def setFristVoiceNameFromGroup (vids, vdefs): # vids = [vid], vdef = {vid -> (name, subname, voicedef)}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
599 vids = [v for v in vids if v in vdefs] # only consider defined voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
600 if not vids: return vdefs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
601 vid0 = vids [0] # first vid of the group
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
602 _, _, vdef0 = vdefs [vid0] # keep de voice definition (vdef0) when renaming vid0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
603 for vid in vids:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
604 nm, snm, vdef = vdefs [vid]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
605 if nm: # first non empty name encountered will become
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
606 vdefs [vid0] = nm, snm, vdef0 # name of merged group == name of first voice in group (vid0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
607 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
608 return vdefs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
609
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
610 def mkGrand (p, vdefs): # transform parse subtree into list needed for s.grands
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
611 xs = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
612 for i, x in enumerate (p.objs): # changing p.objs [i] alters the tree. changing x has no effect on the tree.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
613 if type (x) == pObj:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
614 us = mkGrand (x, vdefs) # first get transformation results of current pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
615 if x.name == 'grand': # x.objs contains ordered list of nested parse results within x
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
616 vids = [y.objs[0] for y in x.objs[1:]] # the voice ids in the grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
617 nms = [vdefs [u][0] for u in vids if u in vdefs] # the names of those voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
618 accept = sum ([1 for nm in nms if nm]) == 1 # accept as grand staff when only one of the voices has a name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
619 if accept or us[0] == '{*':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
620 xs.append (us[1:]) # append voice ids as a list (discard first item '{' or '{*')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
621 vdefs = setFristVoiceNameFromGroup (vids, vdefs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
622 p.objs [i] = x.objs[1] # replace voices by first one in the grand group (this modifies the parse tree)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
623 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
624 xs.extend (us[1:]) # extend current result with all voice ids of rejected grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
625 else: xs.extend (us) # extend current result with transformed pObj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
626 else: xs.append (p.t[0]) # append the non pObj (== voice id string)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
627 return xs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
628
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
629 def mkStaves (p, vdefs): # transform parse tree into list needed for s.staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
630 xs = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
631 for i, x in enumerate (p.objs): # structure and comments identical to mkGrand
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
632 if type (x) == pObj:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
633 us = mkStaves (x, vdefs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
634 if x.name == 'voicegr':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
635 xs.append (us)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
636 vids = [y.objs[0] for y in x.objs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
637 vdefs = setFristVoiceNameFromGroup (vids, vdefs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
638 p.objs [i] = x.objs[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
639 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
640 xs.extend (us)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
641 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
642 if p.t[0] not in '{*': xs.append (p.t[0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
643 return xs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
644
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
645 def mkGroups (p): # transform parse tree into list needed for s.groups
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
646 xs = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
647 for x in p.objs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
648 if type (x) == pObj:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
649 if x.name == 'vid': xs.extend (mkGroups (x))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
650 elif x.name == 'bracketgr': xs.extend (['['] + mkGroups (x) + [']'])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
651 elif x.name == 'bracegr': xs.extend (['{'] + mkGroups (x) + ['}'])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
652 else: xs.extend (mkGroups (x) + ['}']) # x.name == 'grand' == rejected grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
653 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
654 xs.append (p.t[0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
655 return xs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
656
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
657 class MusicXml:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
658 typeMap = {1:'long', 2:'breve', 4:'whole', 8:'half', 16:'quarter', 32:'eighth', 64:'16th', 128:'32nd', 256:'64th'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
659 dynaMap = {'p':1,'pp':1,'ppp':1,'f':1,'ff':1,'fff':1,'mp':1,'mf':1,'sfz':1}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
660 wedgeMap = {'>(':1, '>)':1, '<(':1,'<)':1,'crescendo(':1,'crescendo)':1,'diminuendo(':1,'diminuendo)':1}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
661 artMap = {'.':'staccato','>':'accent','accent':'accent','wedge':'staccatissimo','tenuto':'tenuto'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
662 ornMap = {'trill':'trill-mark','T':'trill-mark','turn':'turn','uppermordent':'inverted-mordent','lowermordent':'mordent',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
663 'pralltriller':'inverted-mordent','mordent':'mordent','turn':'turn','invertedturn':'inverted-turn'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
664 tecMap = {'upbow':'up-bow', 'downbow':'down-bow'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
665 capoMap = {'fine':('Fine','fine','yes'), 'D.S.':('D.S.','dalsegno','segno'), 'D.C.':('D.C.','dacapo','yes'),'dacapo':('D.C.','dacapo','yes'),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
666 'dacoda':('To Coda','tocoda','coda'), 'coda':('coda','coda','coda'), 'segno':('segno','segno','segno')}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
667 sharpness = ['Fb', 'Cb','Gb','Db','Ab','Eb','Bb','F','C','G','D','A', 'E', 'B', 'F#','C#','G#','D#','A#','E#','B#']
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
668 offTab = {'maj':8, 'm':11, 'min':11, 'mix':9, 'dor':10, 'phr':12, 'lyd':7, 'loc':13}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
669 modTab = {'maj':'major', 'm':'minor', 'min':'minor', 'mix':'mixolydian', 'dor':'dorian', 'phr':'phrygian', 'lyd':'lydian', 'loc':'locrian'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
670 clefMap = { 'alto1':('C','1'), 'alto2':('C','2'), 'alto':('C','3'), 'alto4':('C','4'), 'tenor':('C','4'),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
671 'bass3':('F','3'), 'bass':('F','4'), 'treble':('G','2'), 'perc':('percussion',''), 'none':('','')}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
672 clefLineMap = {'B':'treble', 'G':'alto1', 'E':'alto2', 'C':'alto', 'A':'tenor', 'F':'bass3', 'D':'bass'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
673 alterTab = {'=':'0', '_':'-1', '__':'-2', '^':'1', '^^':'2'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
674 accTab = {'=':'natural', '_':'flat', '__':'flat-flat', '^':'sharp', '^^':'sharp-sharp'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
675 chordTab = compChordTab ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
676 uSyms = {'~':'roll', 'H':'fermata','L':'>','M':'lowermordent','O':'coda',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
677 'P':'uppermordent','S':'segno','T':'trill','u':'upbow','v':'downbow'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
678 pageFmtDef = [1.764,297,210,10,10,10,10] # the MuseScore page formatting defaults for A4
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
679 creditTab = {'O':'origin', 'A':'area', 'Z':'transcription', 'N':'notes', 'G':'group', 'H':'history', 'R':'rhythm',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
680 'B':'book', 'D':'discography', 'F':'fileurl', 'S':'source'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
681
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
682 def __init__ (s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
683 s.pageFmtCmd = [] # set by command line option -p
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
684 s.gmwr = 0 # set by command line option -r
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
685 s.reset ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
686 def reset (s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
687 s.divisions = 120 # xml duration of 1/4 note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
688 s.ties = {} # {abc pitch tuple -> alteration} for all open ties
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
689 s.slurstack = [] # stack of open slur numbers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
690 s.slurbeg = 0 # number of slurs to start (when slurs are detected at element-level)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
691 s.tmnum = 0 # time modification, numerator
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
692 s.tmden = 0 # time modification, denominator
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
693 s.ntup = 0 # number of tuplet notes remaining
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
694 s.tupnts = [] # all tuplet modifiers with corresp. durations: [(duration, modifier), ...]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
695 s.irrtup = 0 # 1 if an irregular tuplet
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
696 s.ntype = '' # the normal-type of a tuplet (== duration type of a normal tuplet note)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
697 s.unitL = (1, 8) # default unit length
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
698 s.unitLcur = (1, 8) # unit length of current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
699 s.keyAlts = {} # alterations implied by key
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
700 s.msreAlts = {} # temporarily alterations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
701 s.curVolta = '' # open volta bracket
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
702 s.slurstack = [] # stack of open slur numbers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
703 s.title = '' # title of music
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
704 s.creator = {} # {creator-type -> creator string}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
705 s.credits = {} # {credit-type -> string}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
706 s.lyrdash = {} # {lyric number -> 1 if dash between syllables}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
707 s.usrSyms = s.uSyms # user defined symbols
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
708 s.prevNote = None # xml element of previous beamed note to correct beams (start, continue)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
709 s.grcbbrk = False # remember any bbrk in a grace sequence
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
710 s.linebrk = 0 # 1 if next measure should start with a line break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
711 s.bardecos = [] # barline decorations (coda, segno) that go into the next measure (MuseScore deficiency!)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
712 s.nextdecos = [] # decorations for the next note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
713 s.prevmsre = None # the previous measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
714 s.supports_tag = 0 # issue supports-tag in xml file when abc uses explicit linebreaks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
715 s.staveDefs = [] # collected %%staves or %%score instructions from score
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
716 s.staves = [] # staves = [[voice names to be merged into one stave]]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
717 s.groups = [] # list of merged part names with interspersed {[ and }]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
718 s.grands = [] # [[vid1, vid2, ..], ...] voiceIds to be merged in a grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
719 s.gStaffNums = {} # map each voice id in a grand staff to a staff number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
720 s.gNstaves = {} # map each voice id in a grand staff to total number of staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
721 s.pageFmtAbc = [] # formatting from abc directives
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
722 s.mdur = (4,4) # duration of one measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
723 s.gtrans = 0 # octave transposition (by clef)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
724 s.midprg = ['', ''] # MIDI channel nr, program nr for the current part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
725 s.vid = '' # abc voice id for the current part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
726 s.gcue_on = 0 # insert <cue/> tag in each note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
727
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
728 def mkPitch (s, acc, note, oct, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
729 nUp = note.upper ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
730 octnum = (4 if nUp == note else 5) + int (oct) + s.gtrans
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
731 pitch = E.Element ('pitch')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
732 addElemT (pitch, 'step', nUp, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
733 alter = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
734 if (note, oct) in s.ties:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
735 tied_alter, _, vnum = s.ties [(note,oct)] # vnum = overlay voice number when tie started
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
736 if vnum == s.overlayVnum: alter = tied_alter # tied note in the same overlay -> same alteration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
737 elif acc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
738 s.msreAlts [(nUp, octnum)] = s.alterTab [acc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
739 alter = s.alterTab [acc] # explicit notated alteration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
740 elif (nUp, octnum) in s.msreAlts: alter = s.msreAlts [(nUp, octnum)] # temporary alteration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
741 elif nUp in s.keyAlts: alter = s.keyAlts [nUp] # alteration implied by the key
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
742 if alter: addElemT (pitch, 'alter', alter, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
743 addElemT (pitch, 'octave', str (octnum), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
744 return pitch, alter
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
745
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
746 def mkNote (s, n, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
747 nnum, nden = n.dur.t # abc dutation of note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
748 if nden == 0: nden = 1 # occurs with illegal ABC like: "A2 1". Now interpreted as A2/1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
749 num, den = simplify (nnum * s.unitLcur[0], nden * s.unitLcur[1]) # normalised with unit length
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
750 if den > 64: # limit denominator to 64
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
751 num = int (round (64 * float (num) / den)) # scale note to num/64
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
752 num, den = simplify (max ([num, 1]), 64) # smallest num == 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
753 info ('duration too small: rounded to %d/%d' % (num, den))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
754 if n.name == 'rest' and ('Z' in n.t or 'X' in n.t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
755 num, den = s.mdur # duration of one measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
756 dvs = (4 * s.divisions * num) / den # divisions is xml-duration of 1/4
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
757 rdvs = dvs # real duration (will be 0 for chord/grace)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
758 num, den = simplify (num, den * 4) # scale by 1/4 for s.typeMap
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
759 ndot = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
760 if num == 3: ndot = 1; den = den / 2 # look for dotted notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
761 if num == 7: ndot = 2; den = den / 4
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
762 nt = E.Element ('note')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
763 if getattr (n, 'grace', ''): # a grace note (and possibly a chord note)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
764 grace = E.Element ('grace')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
765 if s.acciatura: grace.set ('slash', 'yes'); s.acciatura = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
766 addElem (nt, grace, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
767 dvs = rdvs = 0 # no (real) duration for a grace note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
768 if den <= 16: den = 32 # not longer than 1/8 for a grace note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
769 if s.gcue_on: # insert cue tag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
770 cue = E.Element ('cue')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
771 addElem (nt, cue, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
772 if getattr (n, 'chord', ''): # a chord note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
773 chord = E.Element ('chord')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
774 addElem (nt, chord, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
775 rdvs = 0 # chord notes no real duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
776 if rdvs and s.ntup >= 0: s.ntup -= 1 # count tuplet notes only on non-chord, non grace notes (rdvs > 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
777 if den not in s.typeMap: # take the nearest smaller legal duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
778 info ('illegal duration %d/%d' % (nnum, nden))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
779 den = min (x for x in s.typeMap.keys () if x > den)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
780 xmltype = str (s.typeMap [den]) # xml needs the note type in addition to duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
781 acc, step, oct = '', 'C', '0' # abc-notated pitch elements (accidental, pitch step, octave)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
782 alter = '' # xml alteration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
783 if n.name == 'rest':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
784 if 'x' in n.t or 'X' in n.t: nt.set ('print-object', 'no')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
785 rest = E.Element ('rest')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
786 addElem (nt, rest, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
787 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
788 p = n.pitch.t # get pitch elements from parsed tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
789 if len (p) == 3: acc, step, oct = p
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
790 else: step, oct = p
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
791 pitch, alter = s.mkPitch (acc, step, oct, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
792 addElem (nt, pitch, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
793 if s.ntup >= 0: # modify duration for tuplet notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
794 dvs = dvs * s.tmden / s.tmnum
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
795 if dvs: addElemT (nt, 'duration', str (dvs), lev + 1) # skip when dvs == 0, requirement of musicXML
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
796 inst = E.Element ('instrument', id='I-'+s.vid) # instrument id for midi
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
797 if s.midprg != ['', '']: addElem (nt, inst, lev + 1) # only add when %%midi was present
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
798 addElemT (nt, 'voice', '1', lev + 1) # default voice, for merging later
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
799 addElemT (nt, 'type', xmltype, lev + 1) # add note type
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
800 for i in range (ndot): # add dots
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
801 dot = E.Element ('dot')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
802 addElem (nt, dot, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
803 ptup = (step, oct) # pitch tuple without alteration to check for ties
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
804 tstop = ptup in s.ties and s.ties[ptup][2] == s.overlayVnum # open tie on this pitch tuple in this overlay
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
805 if acc and not tstop: addElemT (nt, 'accidental', s.accTab [acc], lev + 1) # only add accidental if note not tied
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
806 tupnotation = '' # start/stop notation element for tuplets
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
807 if s.ntup >= 0: # add time modification element for tuplet notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
808 tmod = mkTmod (s.tmnum, s.tmden, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
809 addElem (nt, tmod, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
810 if s.ntup > 0 and not s.tupnts: tupnotation = 'start'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
811 s.tupnts.append ((rdvs, tmod)) # remember all tuplet modifiers with corresp. durations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
812 if s.ntup == 0: # last tuplet note (and possible chord notes there after)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
813 if rdvs: tupnotation = 'stop' # only insert notation in the real note (rdvs > 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
814 s.cmpNormType (rdvs, lev + 1) # compute and/or add normal-type elements (-> s.ntype)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
815 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
816 if gstaff: addElemT (nt, 'staff', str (gstaff), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
817 s.doBeams (n, nt, den, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
818 s.doNotations (n, ptup, alter, tupnotation, tstop, nt, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
819 if n.objs: s.doLyr (n, nt, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
820 return nt
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
821
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
822 def cmpNormType (s, rdvs, lev): # compute the normal-type of a tuplet (only needed for Finale)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
823 if rdvs: # the last real tuplet note (chord notes can still follow afterwards with rdvs == 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
824 durs = [dur for dur, tmod in s.tupnts if dur > 0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
825 ndur = sum (durs) / s.tmnum # duration of the normal type
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
826 s.irrtup = any ((dur != ndur) for dur in durs) # irregular tuplet
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
827 tix = 16 * s.divisions / ndur # index in typeMap of normal-type duration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
828 if tix in s.typeMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
829 s.ntype = str (s.typeMap [tix]) # the normal-type
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
830 else: s.irrtup = 0 # give up, no normal type possible
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
831 if s.irrtup: # only add normal-type for irregular tuplets
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
832 for dur, tmod in s.tupnts: # add normal-type to all modifiers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
833 addElemT (tmod, 'normal-type', s.ntype, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
834 s.tupnts = [] # reset the tuplet buffer
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
835
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
836 def doNotations (s, n, ptup, alter, tupnotation, tstop, nt, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
837 slurs = getattr (n, 'slurs', 0) # slur ends
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
838 pts = getattr (n, 'pitches', []) # all chord notes available in the first note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
839 if pts: # make list of pitches in chord: [(pitch, octave), ..]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
840 if type (pts.pitch) == pObj: pts = [pts.pitch] # chord with one note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
841 else: pts = [tuple (p.t[-2:]) for p in pts.pitch] # normal chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
842 for pt, (tie_alter, nts, vnum) in s.ties.items (): # scan all open ties and delete illegal ones
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
843 if vnum != s.overlayVnum: continue # tie belongs to different overlay
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
844 if pts and pt in pts: continue # pitch tuple of tie exists in chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
845 if getattr (n, 'chord', 0): continue # skip chord notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
846 if pt == ptup: continue # skip correct single note tie
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
847 if getattr (n, 'grace', 0): continue # skip grace notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
848 info ('tie between different pitches: %s%s converted to slur' % pt)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
849 del s.ties [pt] # remove the note from pending ties
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
850 e = [t for t in nts.findall ('tied') if t.get ('type') == 'start'][0] # get the tie start element
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
851 e.tag = 'slur' # convert tie into slur
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
852 slurnum = len (s.slurstack) + 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
853 s.slurstack.append (slurnum)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
854 e.set ('number', str (slurnum))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
855 if slurs: slurs.t.append (')') # close slur on this note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
856 else: slurs = pObj ('slurs', [')'])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
857 tstart = getattr (n, 'tie', 0) # start a new tie
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
858 decos = s.nextdecos # decorations encountered so far
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
859 ndeco = getattr (n, 'deco', 0) # possible decorations of notes of a chord
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
860 if ndeco: # add decorations, translate used defined symbols
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
861 decos += [s.usrSyms.get (d, d).strip ('!+') for d in ndeco.t]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
862 s.nextdecos = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
863 if not (tstop or tstart or decos or slurs or s.slurbeg or tupnotation): return nt
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
864 nots = E.Element ('notations') # notation element needed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
865 if tupnotation: # add tuplet type
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
866 tup = E.Element ('tuplet', type=tupnotation)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
867 if tupnotation == 'start': tup.set ('bracket', 'yes')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
868 addElem (nots, tup, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
869 if tstop: # stop tie
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
870 del s.ties[ptup] # remove flag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
871 tie = E.Element ('tied', type='stop')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
872 addElem (nots, tie, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
873 if tstart: # start a tie
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
874 s.ties[ptup] = (alter, nots, s.overlayVnum) # remember pitch tuple to stop tie and apply same alteration
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
875 tie = E.Element ('tied', type='start')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
876 addElem (nots, tie, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
877 if decos: # look for slurs and decorations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
878 arts = [] # collect articulations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
879 for d in decos: # do all slurs and decos
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
880 if d == '(': s.slurbeg += 1; continue # slurs made in while loop at the end
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
881 elif d == 'fermata' or d == 'H':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
882 ntn = E.Element ('fermata', type='upright')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
883 elif d == 'arpeggio':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
884 ntn = E.Element ('arpeggiate', number='1')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
885 else: arts.append (d); continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
886 addElem (nots, ntn, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
887 if arts: # do only note articulations and collect staff annotations in xmldecos
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
888 rest = s.doArticulations (nots, arts, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
889 if rest: info ('unhandled note decorations: %s' % rest)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
890 while s.slurbeg > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
891 s.slurbeg -= 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
892 slurnum = len (s.slurstack) + 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
893 s.slurstack.append (slurnum)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
894 ntn = E.Element ('slur', number='%d' % slurnum, type='start')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
895 addElem (nots, ntn, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
896 if slurs: # these are only slur endings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
897 for d in slurs.t:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
898 if not s.slurstack: break # no more open slurs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
899 slurnum = s.slurstack.pop ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
900 slur = E.Element ('slur', number='%d' % slurnum, type='stop')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
901 addElem (nots, slur, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
902 if nots.getchildren() != []: # only add notations if not empty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
903 addElem (nt, nots, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
904
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
905 def doArticulations (s, nots, arts, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
906 decos = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
907 for a in arts:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
908 if a in s.artMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
909 art = E.Element ('articulations')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
910 addElem (nots, art, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
911 addElem (art, E.Element (s.artMap[a]), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
912 elif a in s.ornMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
913 orn = E.Element ('ornaments')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
914 addElem (nots, orn, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
915 addElem (orn, E.Element (s.ornMap[a]), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
916 elif a in s.tecMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
917 tec = E.Element ('technical')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
918 addElem (nots, tec, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
919 addElem (tec, E.Element (s.tecMap[a]), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
920 else: decos.append (a) # return staff annotations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
921 return decos
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
922
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
923 def doLyr (s, n, nt, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
924 for i, lyrobj in enumerate (n.objs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
925 if lyrobj.name != 'syl': continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
926 dash = len (lyrobj.t) == 2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
927 if dash:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
928 if i in s.lyrdash: type = 'middle'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
929 else: type = 'begin'; s.lyrdash [i] = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
930 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
931 if i in s.lyrdash: type = 'end'; del s.lyrdash [i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
932 else: type = 'single'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
933 lyrel = E.Element ('lyric', number = str (i + 1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
934 addElem (nt, lyrel, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
935 addElemT (lyrel, 'syllabic', type, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
936 addElemT (lyrel, 'text', lyrobj.t[0].replace ('~',' '), lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
937
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
938 def doBeams (s, n, nt, den, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
939 if hasattr (n, 'chord') or hasattr (n, 'grace'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
940 s.grcbbrk = s.grcbbrk or n.bbrk.t[0] # remember if there was any bbrk in or before a grace sequence
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
941 return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
942 bbrk = s.grcbbrk or n.bbrk.t[0] or den < 32
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
943 s.grcbbrk = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
944 if not s.prevNote: pbm = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
945 else: pbm = s.prevNote.find ('beam')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
946 bm = E.Element ('beam', number='1')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
947 bm.text = 'begin'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
948 if pbm != None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
949 if bbrk:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
950 if pbm.text == 'begin':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
951 s.prevNote.remove (pbm)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
952 elif pbm.text == 'continue':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
953 pbm.text = 'end'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
954 s.prevNote = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
955 else: bm.text = 'continue'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
956 if den >= 32 and n.name != 'rest':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
957 addElem (nt, bm, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
958 s.prevNote = nt
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
959
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
960 def stopBeams (s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
961 if not s.prevNote: return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
962 pbm = s.prevNote.find ('beam')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
963 if pbm != None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
964 if pbm.text == 'begin':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
965 s.prevNote.remove (pbm)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
966 elif pbm.text == 'continue':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
967 pbm.text = 'end'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
968 s.prevNote = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
969
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
970 def staffDecos (s, decos, maat, lev, bardecos=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
971 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
972 for d in decos:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
973 d = s.usrSyms.get (d, d).strip ('!+') # try to replace user defined symbol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
974 if d in s.dynaMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
975 dynel = E.Element ('dynamics')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
976 addDirection (maat, dynel, lev, gstaff, [E.Element (d)], 'below', s.gcue_on)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
977 elif d in s.wedgeMap: # wedge
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
978 if ')' in d: type = 'stop'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
979 else: type = 'crescendo' if '<' in d or 'crescendo' in d else 'diminuendo'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
980 addDirection (maat, E.Element ('wedge', type=type), lev, gstaff)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
981 elif d in ['coda', 'segno']:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
982 if bardecos: s.bardecos.append (d) # postpone to begin next measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
983 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
984 text, attr, val = s.capoMap [d]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
985 dir = addDirection (maat, E.Element (text), lev, gstaff, placement='above')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
986 sound = E.Element ('sound'); sound.set (attr, val)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
987 addElem (dir, sound, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
988 elif d in s.capoMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
989 text, attr, val = s.capoMap [d]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
990 words = E.Element ('words'); words.text = text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
991 dir = addDirection (maat, words, lev, gstaff, placement='above')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
992 sound = E.Element ('sound'); sound.set (attr, val)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
993 addElem (dir, sound, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
994 elif d == '(': s.slurbeg += 1 # start slur on next note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
995 else: s.nextdecos.append (d) # keep annotation for the next note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
996
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
997 def doFields (s, maat, fieldmap, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
998 def doClef ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
999 clef, gtrans = 0, 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1000 clefn = re.search (r'alto1|alto2|alto4|alto|tenor|bass3|bass|treble|perc|none', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1001 clefm = re.search (r"(?:^m=| m=|middle=)([A-Ga-g])([,']*)", field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1002 trans_oct2 = re.search (r'octave=([-+]\d)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1003 trans = re.search (r'(?:^t=| t=|transpose=)(-?[\d]+)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1004 trans_oct = re.search (r'([+-^_])(8|15)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1005 cue_onoff = re.search (r'cue=(on|off)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1006 if clefn:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1007 clef = clefn.group ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1008 if clefm:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1009 note, octstr = clefm.groups ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1010 nUp = note.upper ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1011 octnum = (4 if nUp == note else 5) + (len (octstr) if "'" in octstr else -len (octstr))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1012 gtrans = (3 if nUp in 'AFD' else 4) - octnum
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1013 if clef not in ['perc', 'none']: clef = s.clefLineMap [nUp]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1014 if clef:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1015 s.gtrans = gtrans # only change global tranposition when a clef is really defined
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1016 sign, line = s.clefMap [clef]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1017 if not sign: return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1018 c = E.Element ('clef')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1019 gstaff = s.gStaffNums.get (s.vid, 0) # the current staff number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1020 if gstaff: c.set ('number', str (gstaff)) # only add staff number when defined
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1021 addElemT (c, 'sign', sign, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1022 if line: addElemT (c, 'line', line, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1023 if trans_oct:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1024 n = trans_oct.group (1) in '-_' and -1 or 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1025 if trans_oct.group (2) == '15': n *= 2 # 8 => 1 octave, 15 => 2 octaves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1026 addElemT (c, 'clef-octave-change', str (n), lev + 2) # transpose print out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1027 if trans_oct.group (1) in '+-': s.gtrans += n # also transpose all pitches with one octave
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1028 if trans_oct2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1029 n = int (trans_oct2.group (1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1030 s.gtrans += n
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1031 atts.append ((7, c))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1032 if trans != None: # add transposition in semitones
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1033 e = E.Element ('transpose')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1034 addElemT (e, 'chromatic', str (trans.group (1)), lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1035 atts.append ((9, e))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1036 if cue_onoff: s.gcue_on = cue_onoff.group (1) == 'on'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1037 atts = [] # collect xml attribute elements [(order-number, xml-element), ..]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1038 for ftype, field in fieldmap.items ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1039 if not field: # skip empty fields
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1040 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1041 if ftype == 'Div': # not an abc field, but handled as if
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1042 d = E.Element ('divisions')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1043 d.text = field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1044 atts.append ((1, d))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1045 elif ftype == 'gstaff': # make grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1046 e = E.Element ('staves')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1047 e.text = str (field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1048 atts.append ((4, e))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1049 elif ftype == 'M':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1050 if field == 'none': continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1051 if field == 'C': field = '4/4'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1052 elif field == 'C|': field = '2/2'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1053 t = E.Element ('time')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1054 if '/' not in field:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1055 info ('M:%s not recognized, 4/4 assumed' % field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1056 field = '4/4'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1057 beats, btype = field.split ('/')[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1058 try: s.mdur = simplify (eval (beats), int (btype)) # measure duration for Z and X rests (eval allows M:2+3/4)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1059 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1060 info ('error in M:%s, 4/4 assumed' % field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1061 s.mdur = (4,4)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1062 beats, btype = '4','4'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1063 addElemT (t, 'beats', beats, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1064 addElemT (t, 'beat-type', btype, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1065 atts.append ((3, t))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1066 elif ftype == 'K':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1067 accs = ['F','C','G','D','A','E','B'] # == s.sharpness [7:14]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1068 mode = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1069 key = re.match (r'\s*([A-G][#b]?)\s*([a-zA-Z]*)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1070 alts = re.search (r'\s((\s?[=^_][A-Ga-g])+)', ' ' + field) # avoid matching middle=G and m=G
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1071 if key:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1072 key, mode = key.groups ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1073 mode = mode.lower ()[:3] # only first three chars, no case
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1074 if mode not in s.offTab: mode = 'maj'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1075 fifths = s.sharpness.index (key) - s.offTab [mode]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1076 if fifths >= 0: s.keyAlts = dict (zip (accs[:fifths], fifths * ['1']))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1077 else: s.keyAlts = dict (zip (accs[fifths:], -fifths * ['-1']))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1078 elif field.startswith ('none') or field == '': # the default key
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1079 fifths = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1080 mode = 'maj'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1081 if alts:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1082 alts = re.findall (r'[=^_][A-Ga-g]', alts.group(1)) # list of explicit alterations
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1083 alts = [(x[1], s.alterTab [x[0]]) for x in alts] # [step, alter]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1084 for step, alter in alts: # correct permanent alterations for this key
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1085 s.keyAlts [step.upper ()] = alter
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1086 k = E.Element ('key')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1087 koctave = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1088 lowerCaseSteps = [step.upper () for step, alter in alts if step.islower ()]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1089 for step, alter in s.keyAlts.items ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1090 if alter == '0': # skip neutrals
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1091 del s.keyAlts [step.upper ()] # otherwise you get neutral signs on normal notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1092 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1093 addElemT (k, 'key-step', step.upper (), lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1094 addElemT (k, 'key-alter', alter, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1095 koctave.append ('5' if step in lowerCaseSteps else '4')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1096 if koctave: # only key signature if not empty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1097 for oct in koctave:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1098 e = E.Element ('key-octave', number=oct)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1099 addElem (k, e, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1100 atts.append ((2, k))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1101 elif mode:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1102 k = E.Element ('key')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1103 addElemT (k, 'fifths', str (fifths), lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1104 addElemT (k, 'mode', s.modTab [mode], lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1105 atts.append ((2, k))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1106 doClef ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1107 elif ftype == 'L':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1108 s.unitLcur = map (int, field.split ('/'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1109 if len (s.unitLcur) == 1 or s.unitLcur[1] not in s.typeMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1110 info ('L:%s is not allowed, 1/8 assumed' % field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1111 s.unitLcur = 1,8
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1112 elif ftype == 'V':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1113 doClef ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1114 elif ftype == 'I':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1115 xs = s.doField_I (ftype, field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1116 if xs and len (xs) == 1: # when "%%MIDI transpose" matched insert 'attribute/transpose/chromatic'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1117 e = E.Element ('transpose')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1118 addElemT (e, 'chromatic', xs[0], lev + 2) # xs[0] == signed number string given after transpose
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1119 atts.append ((9, e))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1120 if xs and len (xs) == 2: # repeated occurrence of [I:MIDI] -> instrument change
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1121 midchan, midprog = xs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1122 snd = E.Element ('sound')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1123 mi = E.Element ('midi-instrument', id='I-' + s.vid)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1124 addElem (maat, snd, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1125 addElem (snd, mi, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1126 if midchan: addElemT (mi, 'midi-channel', midchan, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1127 if midprog: addElemT (mi, 'midi-program', str (int (midprog) + 1), lev + 2) # compatible with abc2midi
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1128
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1129 elif ftype == 'Q':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1130 s.doTempo (maat, field, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1131 elif ftype in 'TCOAZNGHRBDFSU':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1132 info ('**illegal header field in body: %s, content: %s' % (ftype, field))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1133 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1134 info ('unhandled field: %s, content: %s' % (ftype, field))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1135
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1136 if atts:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1137 att = E.Element ('attributes') # insert sub elements in the order required by musicXML
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1138 addElem (maat, att, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1139 for _, att_elem in sorted (atts): # ordering !
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1140 addElem (att, att_elem, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1141
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1142 def doTempo (s, maat, field, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1143 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1144 t = re.search (r'(\d)/(\d\d?)\s*=\s*([.\d]+)|([.\d]+)', field)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1145 if not t: return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1146 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1147 if t.group (4):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1148 num, den, upm = 1, s.unitLcur[1] , float (t.group (4))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1149 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1150 num, den, upm = int (t.group (1)), int (t.group (2)), float (t.group (3))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1151 except: return # float or int conversion failure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1152 if num != 1: info ('in Q: numerator > 1 in %d/%d not supported' % (num, den))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1153 qpm = 4. * num * upm / den
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1154 metro = E.Element ('metronome')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1155 u = E.Element ('beat-unit'); u.text = s.typeMap [4 * den]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1156 pm = E.Element ('per-minute'); pm.text = '%.2f' % upm
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1157 dir = addDirection (maat, metro, lev, gstaff, [u, pm], placement='above')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1158 sound = E.Element ('sound'); sound.set ('tempo', '%.2f' % qpm)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1159 addElem (dir, sound, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1160
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1161 def mkBarline (s, maat, loc, lev, style='', dir='', ending=''):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1162 b = E.Element ('barline', location=loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1163 if style:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1164 addElemT (b, 'bar-style', style, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1165 if s.curVolta: # first stop a current volta
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1166 end = E.Element ('ending', number=s.curVolta, type='stop')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1167 s.curVolta = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1168 if loc == 'left': # stop should always go to a right barline
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1169 bp = E.Element ('barline', location='right')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1170 addElem (bp, end, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1171 addElem (s.prevmsre, bp, lev) # prevmsre has no right barline! (ending would have stopped there)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1172 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1173 addElem (b, end, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1174 if ending:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1175 ending = ending.replace ('-',',') # MusicXML only accepts comma's
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1176 endtxt = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1177 if ending.startswith ('"'): # ending is a quoted string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1178 endtxt = ending.strip ('"')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1179 ending = '33' # any number that is not likely to occur elsewhere
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1180 end = E.Element ('ending', number=ending, type='start')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1181 if endtxt: end.text = endtxt # text appears in score in stead of number attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1182 addElem (b, end, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1183 s.curVolta = ending
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1184 if dir:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1185 r = E.Element ('repeat', direction=dir)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1186 addElem (b, r, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1187 addElem (maat, b, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1188
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1189 def doChordSym (s, maat, sym, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1190 alterMap = {'#':'1','=':'0','b':'-1'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1191 rnt = sym.root.t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1192 chord = E.Element ('harmony')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1193 addElem (maat, chord, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1194 root = E.Element ('root')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1195 addElem (chord, root, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1196 addElemT (root, 'root-step', rnt[0], lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1197 if len (rnt) == 2: addElemT (root, 'root-alter', alterMap [rnt[1]], lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1198 kind = s.chordTab.get (sym.kind.t[0], 'major')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1199 addElemT (chord, 'kind', kind, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1200 degs = getattr (sym, 'degree', '')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1201 if degs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1202 if type (degs) != types.ListType: degs = [degs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1203 for deg in degs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1204 deg = deg.t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1205 if deg[0] == '#': alter = '1'; deg = deg[1:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1206 elif deg[0] == 'b': alter = '-1'; deg = deg[1:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1207 else: alter = '0'; deg = deg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1208 degree = E.Element ('degree')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1209 addElem (chord, degree, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1210 addElemT (degree, 'degree-value', deg, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1211 addElemT (degree, 'degree-alter', alter, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1212 addElemT (degree, 'degree-type', 'add', lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1213
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1214 def mkMeasure (s, i, t, lev, fieldmap={}):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1215 s.msreAlts = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1216 s.ntup = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1217 s.acciatura = 0 # next grace element gets acciatura attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1218 overlay = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1219 maat = E.Element ('measure', number = str(i))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1220 if fieldmap: s.doFields (maat, fieldmap, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1221 if s.linebrk: # there was a line break in the previous measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1222 e = E.Element ('print')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1223 e.set ('new-system', 'yes')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1224 addElem (maat, e, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1225 s.linebrk = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1226 if s.bardecos: # output coda and segno attached to the previous right barline
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1227 s.staffDecos (s.bardecos, maat, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1228 s.bardecos = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1229 for it, x in enumerate (t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1230 if x.name == 'note' or x.name == 'rest':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1231 note = s.mkNote (x, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1232 addElem (maat, note, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1233 elif x.name == 'lbar':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1234 bar = x.t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1235 if bar == '|': pass # skip redundant bar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1236 elif ':' in bar: # forward repeat
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1237 volta = x.t[1] if len (x.t) == 2 else ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1238 s.mkBarline (maat, 'left', lev + 1, style='heavy-light', dir='forward', ending=volta)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1239 else: # bar must be a volta number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1240 s.mkBarline (maat, 'left', lev + 1, ending=bar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1241 elif x.name == 'rbar':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1242 if hasattr (x, 'deco'): # MuseScore does not support this -> emergency solution
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1243 s.staffDecos (x.deco.t, maat, lev + 1, bardecos=1) # coda, segno -> next measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1244 bar = x.t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1245 if bar == '.|':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1246 s.mkBarline (maat, 'right', lev + 1, style='dotted')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1247 elif ':' in bar: # backward repeat
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1248 s.mkBarline (maat, 'right', lev + 1, style='light-heavy', dir='backward')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1249 elif bar == '||':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1250 s.mkBarline (maat, 'right', lev + 1, style='light-light')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1251 elif bar == '[|]' or bar == '[]':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1252 s.mkBarline (maat, 'right', lev + 1, style='none')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1253 elif '[' in bar or ']' in bar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1254 s.mkBarline (maat, 'right', lev + 1, style='light-heavy')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1255 elif bar[0] == '&': overlay = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1256 elif x.name == 'tup':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1257 if len (x.t) == 3: n, into, nts = x.t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1258 else: n, into, nts = x.t[0], 0, 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1259 if into == 0: into = 3 if n in [2,4,8] else 2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1260 if nts == 0: nts = n
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1261 s.tmnum, s.tmden, s.ntup = n, into, nts
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1262 elif x.name == 'deco':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1263 s.staffDecos (x.t, maat, lev + 1) # output staff decos, postpone note decos to next note
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1264 elif x.name == 'text':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1265 pos, text = x.t[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1266 place = 'above' if pos == '^' else 'below'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1267 words = E.Element ('words')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1268 words.text = text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1269 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1270 addDirection (maat, words, lev + 1, gstaff, placement=place)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1271 elif x.name == 'inline':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1272 fieldtype, fieldval = x.t[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1273 s.doFields (maat, {fieldtype:fieldval}, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1274 elif x.name == 'accia': s.acciatura = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1275 elif x.name == 'linebrk':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1276 s.supports_tag = 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1277 if it > 0 and t[it -1].name == 'lbar': # we are at start of measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1278 e = E.Element ('print') # output linebreak now
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1279 e.set ('new-system', 'yes')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1280 addElem (maat, e, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1281 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1282 s.linebrk = 1 # output linebreak at start of next measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1283 elif x.name == 'chordsym':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1284 s.doChordSym (maat, x, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1285 s.stopBeams ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1286 s.prevmsre = maat
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1287 return maat, overlay
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1288
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1289 def mkPart (s, maten, id, lev, attrs, nstaves):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1290 s.slurstack = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1291 s.unitLcur = s.unitL # set the default unit length at begin of each voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1292 s.curVolta = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1293 s.lyrdash = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1294 s.linebrk = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1295 s.midprg = ['', ''] # MIDI channel nr, program nr for the current part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1296 s.gcue_on = 0 # reset cue note marker for each new voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1297 s.gtrans = 0 # reset octave transposition (by clef)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1298 part = E.Element ('part', id=id)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1299 s.overlayVnum = 0 # overlay voice number to relate ties that extend from one overlayed measure to the next
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1300 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1301 attrs_cpy = attrs.copy () # don't change attrs itself in next line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1302 if gstaff == 1: attrs_cpy ['gstaff'] = nstaves # make a grand staff
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1303 msre, overlay = s.mkMeasure (1, maten[0], lev + 1, attrs_cpy)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1304 addElem (part, msre, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1305 for i, maat in enumerate (maten[1:]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1306 s.overlayVnum = s.overlayVnum + 1 if overlay else 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1307 msre, next_overlay = s.mkMeasure (i+2, maat, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1308 if overlay: mergePartMeasure (part, msre, s.overlayVnum)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1309 else: addElem (part, msre, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1310 overlay = next_overlay
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1311 return part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1312
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1313 def mkScorePart (s, id, vids_p, partAttr, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1314 naam, subnm, midprg = partAttr [id]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1315 sp = E.Element ('score-part', id='P'+id)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1316 nm = E.Element ('part-name')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1317 nm.text = naam
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1318 addElem (sp, nm, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1319 snm = E.Element ('part-abbreviation')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1320 snm.text = subnm
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1321 if subnm: addElem (sp, snm, lev + 1) # only add if subname was given
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1322 if s.staves: instr_vids = [vids for vids in s.staves if vids[0] == id][0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1323 else: instr_vids = [id]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1324 inst = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1325 for id in instr_vids:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1326 if id not in partAttr: continue # error in %%score -> instr_vids may have non existing id's
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1327 naam, subnm, midprg = partAttr [id]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1328 midchan, midprog = midprg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1329 if not midchan and not midprog: continue # only add if program nr or channel was given
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1330 si = E.Element ('score-instrument', id='I-'+id)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1331 addElemT (si, 'instrument-name', naam, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1332 mi = E.Element ('midi-instrument', id='I-'+id)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1333 if midchan: addElemT (mi, 'midi-channel', midchan, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1334 if midprog: addElemT (mi, 'midi-program', str (int (midprog) + 1), lev + 2) # compatible with abc2midi
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1335 inst.append ((si, mi))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1336 for si, mi in inst: addElem (sp, si, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1337 for si, mi in inst: addElem (sp, mi, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1338 return sp, len (inst)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1339
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1340 def mkPartlist (s, vids, partAttr, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1341 def addPartGroup (sym, num):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1342 pg = E.Element ('part-group', number=str (num), type='start')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1343 addElem (partlist, pg, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1344 addElemT (pg, 'group-symbol', sym, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1345 addElemT (pg, 'group-barline', 'yes', lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1346 partlist = E.Element ('part-list')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1347 g_num = 0 # xml group number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1348 nInstrs = [] # number of instruments in each part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1349 for g in (s.groups or vids): # brace/bracket or abc_voice_id
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1350 if g == '[': g_num += 1; addPartGroup ('bracket', g_num)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1351 elif g == '{': g_num += 1; addPartGroup ('brace', g_num)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1352 elif g in '}]':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1353 pg = E.Element ('part-group', number=str (g_num), type='stop')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1354 addElem (partlist, pg, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1355 g_num -= 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1356 else: # g = abc_voice_id
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1357 if g not in vids: continue # error in %%score
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1358 sp, nInst = s.mkScorePart (g, vids, partAttr, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1359 addElem (partlist, sp, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1360 nInstrs.append (nInst)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1361 return partlist, nInstrs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1362
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1363 def doField_I (s, type, x):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1364 def readPfmt (x, n): # read ABC page formatting constant
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1365 if not s.pageFmtAbc: s.pageFmtAbc = s.pageFmtDef # set the default values on first change
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1366 ro = re.search (r'[^.\d]*([\d.]+)\s*(cm|in|pt)?', x) # float followed by unit
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1367 if ro:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1368 x, unit = ro.groups () # unit == None when not present
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1369 u = {'cm':10., 'in':25.4, 'pt':25.4/72} [unit] if unit else 1.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1370 s.pageFmtAbc [n] = float (x) * u # convert ABC values to millimeters
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1371 else: info ('error in page format: %s' % x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1372
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1373 if x.startswith ('score') or x.startswith ('staves'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1374 s.staveDefs += [x] # collect all voice mappings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1375 elif x.startswith ('staffwidth'): info ('skipped I-field: %s' % x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1376 elif x.startswith ('staff'): # set new staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1377 r1 = re.search (r'staff *([+-]?)(\d)', x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1378 if r1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1379 sign = r1.group (1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1380 num = int (r1.group (2))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1381 gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1382 if sign: # relative staff number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1383 num = (sign == '-') and gstaff - num or gstaff + num
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1384 else: # absolute abc staff number
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1385 try: vabc = s.staves [num - 1][0] # vid of (first voice of) abc-staff num
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1386 except: vabc = 0; info ('abc staff %s does not exist' % num)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1387 num = s.gStaffNumsOrg.get (vabc, 0) # xml staff number of abc-staff num
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1388 if gstaff and num > 0 and num <= s.gNstaves [s.vid]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1389 s.gStaffNums [s.vid] = num
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1390 else: info ('could not relocate to staff: %s' % r1.group ())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1391 else: info ('not a valid staff redirection: %s' % x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1392 elif x.startswith ('scale'): readPfmt (x, 0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1393 elif x.startswith ('pageheight'): readPfmt (x, 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1394 elif x.startswith ('pagewidth'): readPfmt (x, 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1395 elif x.startswith ('leftmargin'): readPfmt (x, 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1396 elif x.startswith ('rightmargin'): readPfmt (x, 4)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1397 elif x.startswith ('topmargin'): readPfmt (x, 5)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1398 elif x.startswith ('botmargin'): readPfmt (x, 6)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1399 elif x.startswith ('MIDI'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1400 r1 = re.search (r'program *(\d*) +(\d+)', x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1401 r2 = re.search (r'channel\D*(\d+)', x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1402 if r1: ch, prg = r1.groups () # channel nr or '', program nr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1403 if r2: ch, prg = r2.group (1), '' # channel nr only
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1404 if r1 or r2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1405 if s.midprg[1] == '': # no instrument defined yet
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1406 s.midprg[1] = prg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1407 if ch: s.midprg[0] = ch
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1408 elif ch and s.midprg[0] == '': # no channel defined yet
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1409 s.midprg[0] = ch
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1410 else: # repeated midi def -> insert instument change
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1411 return [ch, prg]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1412 r = re.search (r'transpose[^-\d]*(-?\d+)', x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1413 if r: return [r.group (1)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1414 else: info ('skipped I-field: %s' % x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1415
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1416 def parseStaveDef (s, vdefs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1417 if not s.staveDefs: return vdefs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1418 for x in s.staveDefs [1:]: info ('%%%%%s dropped, multiple stave mappings not supported' % x)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1419 x = s.staveDefs [0] # only the first %%score is honoured
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1420 score = abc_scoredef.parseString (x) [0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1421 f = lambda x: type (x) == types.UnicodeType and [x] or x
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1422 s.staves = map (f, mkStaves (score, vdefs))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1423 s.grands = map (f, mkGrand (score, vdefs))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1424 s.groups = mkGroups (score)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1425 vce_groups = [vids for vids in s.staves if len (vids) > 1] # all voice groups
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1426 d = {} # for each voice group: map first voice id -> all merged voice ids
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1427 for vgr in vce_groups: d [vgr[0]] = vgr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1428 for gstaff in s.grands: # for all grand staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1429 if len (gstaff) == 1: continue # skip single parts
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1430 for v, stf_num in zip (gstaff, range (1, len (gstaff) + 1)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1431 for vx in d.get (v, [v]): # allocate staff numbers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1432 s.gStaffNums [vx] = stf_num # to all constituant voices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1433 s.gNstaves [vx] = len (gstaff) # also remember total number of staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1434 s.gStaffNumsOrg = s.gStaffNums.copy () # keep original allocation for abc -> xml staff map
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1435 return vdefs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1436
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1437 def voiceNamesAndMaps (s, ps): # get voice names and mappings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1438 vdefs = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1439 for vid, vcedef, vce in ps: # vcedef == emtpy of first pObj == voice definition
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1440 pname, psubnm = '', '' # part name and abbreviation
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1441 if not vcedef: # simple abc without voice definitions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1442 vdefs [vid] = pname, psubnm, ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1443 else: # abc with voice definitions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1444 if vid != vcedef.t[1]: info ('voice ids unequal: %s (reg-ex) != %s (grammar)' % (vid, vcedef.t[1]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1445 rn = re.search (r'(?:name|nm)="([^"]*)"', vcedef.t[2])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1446 if rn: pname = rn.group (1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1447 rn = re.search (r'(?:subname|snm|sname)="([^"]*)"', vcedef.t[2])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1448 if rn: psubnm = rn.group (1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1449 vdefs [vid] = pname, psubnm, vcedef.t[2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1450 xs = [pObj.t[1] for maat in vce for pObj in maat if pObj.name == 'inline'] # all inline statements in vce
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1451 s.staveDefs += [x for x in xs if x.startswith ('score') or x.startswith ('staves')] # filter %%score and %%staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1452 return vdefs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1453
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1454 def doHeaderField (s, fld, attrmap):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1455 type, value = fld.t[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1456 if not value: # skip empty field
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1457 return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1458 if type == 'M':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1459 attrmap [type] = value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1460 elif type == 'L':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1461 try: s.unitL = map (int, fld.t[1].split ('/'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1462 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1463 info ('illegal unit length:%s, 1/8 assumed' % fld.t[1])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1464 s.unitL = 1,8
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1465 if len (s.unitL) == 1 or s.unitL[1] not in s.typeMap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1466 info ('L:%s is not allowed, 1/8 assumed' % fld.t[1])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1467 s.unitL = 1,8
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1468 elif type == 'K':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1469 attrmap[type] = value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1470 elif type == 'T':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1471 if s.title: s.title = s.title + '\n' + value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1472 else: s.title = value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1473 elif type == 'C':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1474 s.creator ['composer'] = s.creator.get ('composer', '') + value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1475 elif type == 'Z':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1476 s.creator ['lyricist'] = s.creator.get ('lyricist', '') + value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1477 elif type == 'U':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1478 sym = fld.t[2].strip ('!+')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1479 s.usrSyms [value] = sym
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1480 elif type == 'I':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1481 s.doField_I (type, value)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1482 elif type == 'Q':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1483 attrmap[type] = value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1484 elif type in s.creditTab: s.credits [s.creditTab [type]] = value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1485 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1486 info ('skipped header: %s' % fld)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1487
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1488 def mkIdentification (s, score, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1489 if s.title:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1490 addElemT (score, 'movement-title', s.title, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1491 ident = E.Element ('identification')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1492 addElem (score, ident, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1493 if s.creator:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1494 for ctype, cname in s.creator.items ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1495 c = E.Element ('creator', type=ctype)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1496 c.text = cname
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1497 addElem (ident, c, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1498 encoding = E.Element ('encoding')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1499 addElem (ident, encoding, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1500 encoder = E.Element ('encoder')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1501 encoder.text = 'abc2xml version %d' % VERSION
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1502 addElem (encoding, encoder, lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1503 if s.supports_tag: # avoids interference of auto-flowing and explicit linebreaks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1504 suports = E.Element ('supports', attribute="new-system", element="print", type="yes", value="yes")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1505 addElem (encoding, suports, lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1506 encodingDate = E.Element ('encoding-date')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1507 encodingDate.text = str (datetime.date.today ())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1508 addElem (encoding, encodingDate, lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1509
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1510 def mkDefaults (s, score, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1511 if s.pageFmtCmd: s.pageFmtAbc = s.pageFmtCmd
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1512 if not s.pageFmtAbc: return # do not output the defaults if none is desired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1513 space, h, w, l, r, t, b = s.pageFmtAbc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1514 mils = 4 * space # staff height in millimeters
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1515 scale = 40. / mils # tenth's per millimeter
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1516 dflts = E.Element ('defaults')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1517 addElem (score, dflts, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1518 scaling = E.Element ('scaling')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1519 addElem (dflts, scaling, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1520 addElemT (scaling, 'millimeters', '%g' % mils, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1521 addElemT (scaling, 'tenths', '40', lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1522 layout = E.Element ('page-layout')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1523 addElem (dflts, layout, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1524 addElemT (layout, 'page-height', '%g' % (h * scale), lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1525 addElemT (layout, 'page-width', '%g' % (w * scale), lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1526 margins = E.Element ('page-margins', type='both')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1527 addElem (layout, margins, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1528 addElemT (margins, 'left-margin', '%g' % (l * scale), lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1529 addElemT (margins, 'right-margin', '%g' % (r * scale), lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1530 addElemT (margins, 'top-margin', '%g' % (t * scale), lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1531 addElemT (margins, 'bottom-margin', '%g' % (b * scale), lev + 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1532
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1533 def mkCredits (s, score, lev):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1534 if not s.credits: return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1535 for ctype, ctext in s.credits.items ():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1536 credit = E.Element ('credit', page='1')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1537 addElemT (credit, 'credit-type', ctype, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1538 addElemT (credit, 'credit-words', ctext, lev + 2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1539 addElem (score, credit, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1540
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1541 def parse (s, abc_string):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1542 abctext = abc_string if type (abc_string) == types.UnicodeType else decodeInput (abc_string)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1543 abctext = abctext.replace ('[I:staff ','[I:staff') # avoid false beam breaks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1544 s.reset ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1545 header, voices = splitHeaderVoices (abctext)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1546 ps = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1547 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1548 hs = abc_header.parseString (header) if header else ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1549 for id, vce_lyr in voices: # vce_lyr = [voice-block] where voice-block = (measures, corresponding lyric lines)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1550 vcelyr = [] # list of measures where measure = list of elements (see syntax)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1551 prevLeftBar = None # previous voice ended with a left-bar symbol (double repeat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1552 for voice, lyr in vce_lyr:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1553 vce = abc_voice.parseString (voice).asList ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1554 if not vce: # empty voice, insert an inline field that will be rejected
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1555 vce = [[pObj ('inline', ['I', 'empty voice'])]]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1556 if prevLeftBar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1557 vce[0].insert (0, prevLeftBar) # insert at begin of first measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1558 prevLeftBar = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1559 if vce[-1] and vce[-1][-1].name == 'lbar': # last measure ends with an lbar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1560 prevLeftBar = vce[-1][-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1561 if len (vce) > 1: # vce should not become empty (-> exception when taking vcelyr [0][0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1562 del vce[-1] # lbar was the only element in measure vce[-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1563 lyr = lyr.strip () # strip leading \n (because we split on '\nw:...')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1564 if lyr: # no lyrics for this measures-lyrics block
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1565 lyr = lyr_block.parseString (lyr).asList ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1566 xs = alignLyr (vce, lyr) # put all syllables into corresponding notes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1567 else: xs = vce
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1568 vcelyr += xs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1569 elem1 = vcelyr [0][0] # the first element of the first measure
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1570 if elem1.name == 'inline'and elem1.t[0] == 'V': # is a voice definition
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1571 voicedef = elem1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1572 del vcelyr [0][0] # do not read voicedef twice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1573 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1574 voicedef = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1575 ps.append ((id, voicedef, vcelyr))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1576 except ParseException, err:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1577 if err.loc > 40: # limit length of error message, compatible with markInputline
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1578 err.pstr = err.pstr [err.loc - 40: err.loc + 40]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1579 err.loc = 40
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1580 xs = err.line[err.col-1:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1581 try: info (err.line.encode ('utf-8'), warn=0) # err.line is a unicode string!!
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1582 except: info (err.line.encode ('latin-1'), warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1583 info ((err.col-1) * '-' + '^', warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1584 if re.search (r'\[U:[XYZxyz]', xs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1585 info ('Error: illegal user defined symbol: %s' % xs[1:], warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1586 elif re.search (r'\[[OAPZNGHRBDFSXTCIU]:', xs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1587 info ('Error: header-only field %s appears after K:' % xs[1:], warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1588 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1589 info ('Syntax error at column %d' % err.col, warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1590 raise err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1591
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1592 s.unitL = (1, 8)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1593 s.title = ''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1594 s.creator = {} # {creator type -> name string}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1595 s.credits = {} # {credit type -> string}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1596 score = E.Element ('score-partwise')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1597 attrmap = {'Div': str (s.divisions), 'K':'C treble', 'M':'4/4'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1598 for res in hs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1599 if res.name == 'field':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1600 s.doHeaderField (res, attrmap)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1601 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1602 info ('unexpected header item: %s' % res)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1603
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1604 vdefs = s.voiceNamesAndMaps (ps)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1605 vdefs = s.parseStaveDef (vdefs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1606
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1607 lev = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1608 vids, parts, partAttr = [], [], {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1609 for vid, _, vce in ps: # voice id, voice parse tree
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1610 pname, psubnm, voicedef = vdefs [vid] # part name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1611 attrmap ['V'] = voicedef # abc text of first voice definition (after V:vid) or empty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1612 pid = 'P%s' % vid # let part id start with an alpha
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1613 s.vid = vid # avoid parameter passing, needed in mkNote for instrument id
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1614 part = s.mkPart (vce, pid, lev + 1, attrmap, s.gNstaves.get (vid, 0))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1615 if 'Q' in attrmap: del attrmap ['Q'] # header tempo only in first part
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1616 parts.append (part)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1617 vids.append (vid)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1618 partAttr [vid] = (pname, psubnm, s.midprg)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1619 parts, vidsnew = mergeParts (parts, vids, s.staves) # merge parts into staves as indicated by %%score
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1620 parts, _ = mergeParts (parts, vidsnew, s.grands, 1) # merge grand staves
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1621
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1622 s.mkIdentification (score, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1623 s.mkDefaults (score, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1624 s.mkCredits (score, lev)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1625
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1626 partlist, nInstrs = s.mkPartlist (vids, partAttr, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1627 addElem (score, partlist, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1628 for ip, part in enumerate (parts):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1629 if nInstrs [ip] < 2: # no instrument tag needed for one- or no-instrument parts
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1630 removeElems (part, 'measure/note', 'instrument')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1631 addElem (score, part, lev + 1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1632
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1633 return score
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1634
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1635 def decodeInput (data_string):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1636 try: enc = 'utf-8'; unicode_string = data_string.decode (enc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1637 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1638 try: enc = 'latin-1'; unicode_string = data_string.decode (enc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1639 except: raise Exception ('data not encoded in utf-8 nor in latin-1')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1640 info ('decoded from %s' % enc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1641 return unicode_string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1642
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1643 xmlVersion = "<?xml version='1.0' encoding='utf-8'?>"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1644 def fixDoctype (elem, enc):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1645 xs = E.tostring (elem, encoding=enc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1646 ys = xs.split ('\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1647 if enc == 'utf-8': ys.insert (0, xmlVersion) # crooked logic of ElementTree lib
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1648 ys.insert (1, '<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 3.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1649 return '\n'.join (ys)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1650
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1651 def xml2mxl (pad, fnm, data): # write xml data to compressed .mxl file
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1652 from zipfile import ZipFile, ZIP_DEFLATED
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1653 fnmext = fnm + '.xml' # file name with extension, relative to the root within the archive
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1654 outfile = os.path.join (pad, fnm + '.mxl')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1655 meta = '%s\n<container><rootfiles>\n' % xmlVersion
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1656 meta += '<rootfile full-path="%s" media-type="application/vnd.recordare.musicxml+xml"/>\n' % fnmext
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1657 meta += '</rootfiles></container>'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1658 f = ZipFile (outfile, 'w', ZIP_DEFLATED)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1659 f.writestr ('META-INF/container.xml', meta)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1660 f.writestr (fnmext, data)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1661 f.close ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1662 info ('%s written' % outfile, warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1663
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1664 def convert (pad, fnm, abc_string, mxl):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1665 # these globals should be initialised (as in the __main__ secion) before calling convert
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1666 global mxm # optimisation 1: keep instance of MusicXml
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1667 global abc_header, abc_voice, lyr_block, abc_scoredef # optimisation 2: keep computed grammars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1668 score = mxm.parse (abc_string)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1669 if pad:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1670 data = fixDoctype (score, 'utf-8')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1671 if not mxl or mxl in ['a', 'add']:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1672 outfnm = os.path.join (pad, fnm + '.xml')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1673 outfile = file (outfnm, 'wb')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1674 outfile.write (data)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1675 outfile.close ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1676 info ('%s written' % outfnm, warn=0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1677 if mxl: xml2mxl (pad, fnm, data) # also write a compressed version
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1678 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1679 outfile = sys.stdout
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1680 outfile.write (fixDoctype (score, 'utf-8'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1681 outfile.write ('\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1682
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1683 #----------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1684 # Main Program
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1685 #----------------
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1686 if __name__ == '__main__':
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1687 from optparse import OptionParser
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1688 from glob import glob
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1689 import time
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1690 global mxm # keep instance of MusicXml
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1691 global abc_header, abc_voice, lyr_block, abc_scoredef # keep computed grammars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1692 mxm = MusicXml ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1693
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1694 parser = OptionParser (usage='%prog [-h] [-r] [-m SKIP NUM] [-o DIR] [-p PFMT] [-z MODE] <file1> [<file2> ...]', version='version %d' % VERSION)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1695 parser.add_option ("-o", action="store", help="store xml files in DIR", default='', metavar='DIR')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1696 parser.add_option ("-m", action="store", help="skip SKIP tunes, then read at most NUM tunes", nargs=2, type='int', default=(0,1), metavar='SKIP NUM')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1697 parser.add_option ("-p", action="store", help="page formatting in PFMT", default='', metavar='PFMT')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1698 parser.add_option ("-z", "--mxl", dest="mxl", help="store as compressed mxl, MODE = a(dd) or r(eplace)", default='', metavar='MODE')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1699 parser.add_option ("-r", action="store_true", help="show whole measure rests in merged staffs", default=False)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1700 options, args = parser.parse_args ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1701 if len (args) == 0: parser.error ('no input file given')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1702 pad = options.o
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1703 if options.mxl and options.mxl not in ['a','add', 'r', 'replace']:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1704 parser.error ('MODE should be a(dd) or r(eplace), not: %s' % options.mxl)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1705 if pad:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1706 if not os.path.exists (pad): os.mkdir (pad)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1707 if not os.path.isdir (pad): parser.error ('%s is not a directory' % pad)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1708 if options.p: # set page formatting values
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1709 try: # space, page-height, -width, margin-left, -right, -top, -bottom
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1710 mxm.pageFmtCmd = map (float, options.p.split (','))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1711 if len (mxm.pageFmtCmd) != 7: raise Exception ('-p needs 7 values')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1712 except Exception, err: parser.error (err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1713 mxm.gmwr = options.r # ugly: needs to be globally accessable
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1714
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1715 abc_header, abc_voice, lyr_block, abc_scoredef = abc_grammar () # compute grammar only once per file set
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1716 fnmext_list = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1717 for i in args: fnmext_list += glob (i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1718 if not fnmext_list: parser.error ('none of the input files exist')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1719 t_start = time.time ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1720 for X, fnmext in enumerate (fnmext_list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1721 fnm, ext = os.path.splitext (fnmext)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1722 if ext.lower () not in ('.abc'):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1723 info ('skipped input file %s, it should have extension .abc' % fnmext)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1724 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1725 if os.path.isdir (fnmext):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1726 info ('skipped directory %s. Only files are accepted' % fnmext)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1727 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1728
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1729 fobj = open (fnmext, 'rb')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1730 encoded_data = fobj.read ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1731 fobj.close ()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1732 fragments = encoded_data.split ('X:')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1733 preamble = fragments [0] # tunes can be preceeded by formatting instructions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1734 tunes = fragments[1:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1735 if not tunes and preamble: tunes, preamble = ['1\n' + preamble], '' # tune without X:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1736 skip, num = options.m # skip tunes, then read at most num tunes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1737 numtunes = min ([len (tunes), num]) # number of tunes to be converted
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1738 for itune, tune in enumerate (tunes):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1739 if itune < skip: continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1740 if itune >= skip + num: break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1741 tune = preamble + 'X:' + tune # restore preamble before each tune
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1742 fnmNum = '%s%02d' % (fnm, itune + 1) if numtunes > 1 else fnm
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1743 try: # convert string abctext -> file pad/fnmNum.xml
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1744 convert (pad, fnmNum, tune, options.mxl)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1745 except ParseException, err: pass # output already printed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1746 except Exception, err: info ('an exception occurred.\n%s' % err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1747 info ('done in %.2f secs' % (time.time () - t_start))