]> git.donarmstrong.com Git - lilypond.git/blob - scripts/musicxml2ly.py
MusicXML: Implement metronome mark (tempo marks)
[lilypond.git] / scripts / musicxml2ly.py
1 #!@TARGET_PYTHON@
2
3 import optparse
4 import sys
5 import re
6 import os
7 import string
8 import codecs
9 import zipfile
10 import StringIO
11
12 """
13 @relocate-preamble@
14 """
15
16 import lilylib as ly
17 _ = ly._
18
19 import musicxml
20 import musicexp
21
22 from rational import Rational
23
24 # Store command-line options in a global variable, so we can access them everythwere
25 options = None
26
27 class Conversion_Settings:
28     def __init__(self):
29        self.ignore_beaming = False
30
31 conversion_settings = Conversion_Settings ()
32 # Use a global variable to store the setting needed inside a \layout block.
33 # whenever we need to change a setting or add/remove an engraver, we can access 
34 # this layout and add the corresponding settings
35 layout_information = musicexp.Layout ()
36
37 def progress (str):
38     ly.stderr_write (str + '\n')
39     sys.stderr.flush ()
40
41 def error_message (str):
42     ly.stderr_write (str + '\n')
43     sys.stderr.flush ()
44
45 needed_additional_definitions = []
46 additional_definitions = {
47   "snappizzicato": """#(define-markup-command (snappizzicato layout props) ()
48   (interpret-markup layout props
49     (markup #:stencil
50       (ly:stencil-translate-axis
51         (ly:stencil-add
52           (make-circle-stencil 0.7 0.1 #f)
53           (ly:make-stencil
54             (list 'draw-line 0.1 0 0.1 0 1)
55             '(-0.1 . 0.1) '(0.1 . 1)))
56         0.7 X))))""",
57   "eyeglasses": """eyeglassesps = #"0.15 setlinewidth
58 % 255 0 0 setrgbcolor
59 -0.9 0 translate
60 1.1 1.1 scale
61 1.2 0.7 moveto
62 0.7 0.7 0.5 0 361 arc
63 stroke
64 2.20 0.70 0.50 0 361 arc
65 stroke
66 1.45 0.85 0.30 0 180 arc
67 stroke
68 0.20 0.70 moveto
69 0.80 2.00 lineto
70 0.92 2.26 1.30 2.40 1.15 1.70 curveto
71 stroke
72 2.70 0.70 moveto
73 3.30 2.00 lineto
74 3.42 2.26 3.80 2.40 3.65 1.70 curveto
75 stroke"
76 eyeglasses =  \markup { \with-dimensions #'(0 . 4.4) #'(0 . 2.5) \postscript #eyeglassesps }"""
77 }
78
79 def round_to_two_digits (val):
80     return round (val * 100) / 100
81
82 def extract_paper_information (tree):
83     paper = musicexp.Paper ()
84     defaults = tree.get_maybe_exist_named_child ('defaults')
85     if not defaults:
86         return None
87     tenths = -1
88     scaling = defaults.get_maybe_exist_named_child ('scaling')
89     if scaling:
90         mm = scaling.get_named_child ('millimeters')
91         mm = string.atof (mm.get_text ())
92         tn = scaling.get_maybe_exist_named_child ('tenths')
93         tn = string.atof (tn.get_text ())
94         tenths = mm / tn
95         paper.global_staff_size = mm * 72.27 / 25.4
96     # We need the scaling (i.e. the size of staff tenths for everything!
97     if tenths < 0:
98         return None
99
100     def from_tenths (txt):
101         return round_to_two_digits (string.atof (txt) * tenths / 10)
102     def set_paper_variable (varname, parent, element_name):
103         el = parent.get_maybe_exist_named_child (element_name)
104         if el: # Convert to cm from tenths
105             setattr (paper, varname, from_tenths (el.get_text ()))
106
107     pagelayout = defaults.get_maybe_exist_named_child ('page-layout')
108     if pagelayout:
109         # TODO: How can one have different margins for even and odd pages???
110         set_paper_variable ("page_height", pagelayout, 'page-height')
111         set_paper_variable ("page_width", pagelayout, 'page-width')
112
113         pmargins = pagelayout.get_named_children ('page-margins')
114         for pm in pmargins:
115             set_paper_variable ("left_margin", pm, 'left-margin')
116             set_paper_variable ("right_margin", pm, 'right-margin')
117             set_paper_variable ("bottom_margin", pm, 'bottom-margin')
118             set_paper_variable ("top_margin", pm, 'top-margin')
119
120     systemlayout = defaults.get_maybe_exist_named_child ('system-layout')
121     if systemlayout:
122         sl = systemlayout.get_maybe_exist_named_child ('system-margins')
123         if sl:
124             set_paper_variable ("system_left_margin", sl, 'left-margin')
125             set_paper_variable ("system_right_margin", sl, 'right-margin')
126         set_paper_variable ("system_distance", systemlayout, 'system-distance')
127         set_paper_variable ("top_system_distance", systemlayout, 'top-system-distance')
128
129     stafflayout = defaults.get_named_children ('staff-layout')
130     for sl in stafflayout:
131         nr = getattr (sl, 'number', 1)
132         dist = sl.get_named_child ('staff-distance')
133         #TODO: the staff distance needs to be set in the Staff context!!!
134
135     # TODO: Finish appearance?, music-font?, word-font?, lyric-font*, lyric-language*
136     appearance = defaults.get_named_child ('appearance')
137     if appearance:
138         lws = appearance.get_named_children ('line-width')
139         for lw in lws:
140             # Possible types are: beam, bracket, dashes,
141             #    enclosure, ending, extend, heavy barline, leger,
142             #    light barline, octave shift, pedal, slur middle, slur tip,
143             #    staff, stem, tie middle, tie tip, tuplet bracket, and wedge
144             tp = lw.type
145             w = from_tenths (lw.get_text  ())
146             # TODO: Do something with these values!
147         nss = appearance.get_named_children ('note-size')
148         for ns in nss:
149             # Possible types are: cue, grace and large
150             tp = ns.type
151             sz = from_tenths (ns.get_text ())
152             # TODO: Do something with these values!
153         # <other-appearance> elements have no specified meaning
154
155     rawmusicfont = defaults.get_named_child ('music-font')
156     if rawmusicfont:
157         # TODO: Convert the font
158         pass
159     rawwordfont = defaults.get_named_child ('word-font')
160     if rawwordfont:
161         # TODO: Convert the font
162         pass
163     rawlyricsfonts = defaults.get_named_children ('lyric-font')
164     for lyricsfont in rawlyricsfonts:
165         # TODO: Convert the font
166         pass
167
168     return paper
169
170
171
172 # score information is contained in the <work>, <identification> or <movement-title> tags
173 # extract those into a hash, indexed by proper lilypond header attributes
174 def extract_score_information (tree):
175     header = musicexp.Header ()
176     def set_if_exists (field, value):
177         if value:
178             header.set_field (field, musicxml.escape_ly_output_string (value))
179
180     work = tree.get_maybe_exist_named_child ('work')
181     if work:
182         set_if_exists ('title', work.get_work_title ())
183         set_if_exists ('worknumber', work.get_work_number ())
184         set_if_exists ('opus', work.get_opus ())
185     else:
186         movement_title = tree.get_maybe_exist_named_child ('movement-title')
187         if movement_title:
188             set_if_exists ('title', movement_title.get_text ())
189     
190     identifications = tree.get_named_children ('identification')
191     for ids in identifications:
192         set_if_exists ('copyright', ids.get_rights ())
193         set_if_exists ('composer', ids.get_composer ())
194         set_if_exists ('arranger', ids.get_arranger ())
195         set_if_exists ('editor', ids.get_editor ())
196         set_if_exists ('poet', ids.get_poet ())
197             
198         set_if_exists ('tagline', ids.get_encoding_software ())
199         set_if_exists ('encodingsoftware', ids.get_encoding_software ())
200         set_if_exists ('encodingdate', ids.get_encoding_date ())
201         set_if_exists ('encoder', ids.get_encoding_person ())
202         set_if_exists ('encodingdescription', ids.get_encoding_description ())
203
204         # Finally, apply the required compatibility modes
205         # Some applications created wrong MusicXML files, so we need to 
206         # apply some compatibility mode, e.g. ignoring some features/tags
207         # in those files
208         software = ids.get_encoding_software_list ()
209
210         # Case 1: "Sibelius 5.1" with the "Dolet 3.4 for Sibelius" plugin
211         #         is missing all beam ends => ignore all beaming information
212         if "Dolet 3.4 for Sibelius" in software:
213             conversion_settings.ignore_beaming = True
214             progress (_ ("Encountered file created by Dolet 3.4 for Sibelius, containing wrong beaming information. All beaming information in the MusicXML file will be ignored"))
215         # TODO: Check for other unsupported features
216
217     return header
218
219 class PartGroupInfo:
220     def __init__ (self):
221         self.start = {}
222         self.end = {}
223     def is_empty (self):
224         return len (self.start) + len (self.end) == 0
225     def add_start (self, g):
226         self.start[getattr (g, 'number', "1")] = g
227     def add_end (self, g):
228         self.end[getattr (g, 'number', "1")] = g
229     def print_ly (self, printer):
230         error_message (_ ("Unprocessed PartGroupInfo %s encountered") % self)
231     def ly_expression (self):
232         error_message (_ ("Unprocessed PartGroupInfo %s encountered") % self)
233         return ''
234
235 def staff_attributes_to_string_tunings (mxl_attr):
236     details = mxl_attr.get_maybe_exist_named_child ('staff-details')
237     if not details:
238         return []
239     lines = 6
240     staff_lines = details.get_maybe_exist_named_child ('staff-lines')
241     if staff_lines:
242         lines = string.atoi (staff_lines.get_text ())
243
244     tunings = [0]*lines
245     staff_tunings = details.get_named_children ('staff-tuning')
246     for i in staff_tunings:
247         p = musicexp.Pitch()
248         line = 0
249         try:
250             line = string.atoi (i.line) - 1
251         except ValueError:
252             pass
253         tunings[line] = p
254
255         step = i.get_named_child (u'tuning-step')
256         step = step.get_text ().strip ()
257         p.step = musicxml_step_to_lily (step)
258
259         octave = i.get_named_child (u'tuning-octave')
260         octave = octave.get_text ().strip ()
261         p.octave = int (octave) - 4
262
263         alter = i.get_named_child (u'tuning-alter')
264         if alter:
265             p.alteration = int (alter.get_text ().strip ())
266     # lilypond seems to use the opposite ordering than MusicXML...
267     tunings.reverse ()
268
269     return tunings
270
271
272 def staff_attributes_to_lily_staff (mxl_attr):
273     if not mxl_attr:
274         return musicexp.Staff ()
275
276     (staff_id, attributes) = mxl_attr.items ()[0]
277
278     # distinguish by clef:
279     # percussion (percussion and rhythmic), tab, and everything else
280     clef_sign = None
281     clef = attributes.get_maybe_exist_named_child ('clef')
282     if clef:
283         sign = clef.get_maybe_exist_named_child ('sign')
284         if sign:
285             clef_sign = {"percussion": "percussion", "TAB": "tab"}.get (sign.get_text (), None)
286
287     lines = 5
288     details = attributes.get_named_children ('staff-details')
289     for d in details:
290         staff_lines = d.get_maybe_exist_named_child ('staff-lines')
291         if staff_lines:
292             lines = string.atoi (staff_lines.get_text ())
293
294     staff = None
295     if clef_sign == "percussion" and lines == 1:
296         staff = musicexp.RhythmicStaff ()
297     elif clef_sign == "percussion":
298         staff = musicexp.DrumStaff ()
299         # staff.drum_style_table = ???
300     elif clef_sign == "tab":
301         staff = musicexp.TabStaff ()
302         staff.string_tunings = staff_attributes_to_string_tunings (attributes)
303         # staff.tablature_format = ???
304     else:
305         # TODO: Handle case with lines <> 5!
306         staff = musicexp.Staff ()
307
308     return staff
309
310
311 def extract_score_structure (part_list, staffinfo):
312     structure = musicexp.StaffGroup (None)
313     if not part_list:
314         return structure
315
316     def read_score_part (el):
317         if not isinstance (el, musicxml.Score_part):
318             return
319         # Depending on the attributes of the first measure, we create different
320         # types of staves (Staff, RhythmicStaff, DrumStaff, TabStaff, etc.)
321         staff = staff_attributes_to_lily_staff (staffinfo.get (el.id, None))
322         if not staff:
323             return None
324         staff.id = el.id
325         partname = el.get_maybe_exist_named_child ('part-name')
326         # Finale gives unnamed parts the name "MusicXML Part" automatically!
327         if partname and partname.get_text() != "MusicXML Part":
328             staff.instrument_name = partname.get_text ()
329         if el.get_maybe_exist_named_child ('part-abbreviation'):
330             staff.short_instrument_name = el.get_maybe_exist_named_child ('part-abbreviation').get_text ()
331         # TODO: Read in the MIDI device / instrument
332         return staff
333
334     def read_score_group (el):
335         if not isinstance (el, musicxml.Part_group):
336             return
337         group = musicexp.StaffGroup ()
338         if hasattr (el, 'number'):
339             id = el.number
340             group.id = id
341             #currentgroups_dict[id] = group
342             #currentgroups.append (id)
343         if el.get_maybe_exist_named_child ('group-name'):
344             group.instrument_name = el.get_maybe_exist_named_child ('group-name').get_text ()
345         if el.get_maybe_exist_named_child ('group-abbreviation'):
346             group.short_instrument_name = el.get_maybe_exist_named_child ('group-abbreviation').get_text ()
347         if el.get_maybe_exist_named_child ('group-symbol'):
348             group.symbol = el.get_maybe_exist_named_child ('group-symbol').get_text ()
349         if el.get_maybe_exist_named_child ('group-barline'):
350             group.spanbar = el.get_maybe_exist_named_child ('group-barline').get_text ()
351         return group
352
353
354     parts_groups = part_list.get_all_children ()
355
356     # the start/end group tags are not necessarily ordered correctly and groups
357     # might even overlap, so we can't go through the children sequentially!
358
359     # 1) Replace all Score_part objects by their corresponding Staff objects,
360     #    also collect all group start/stop points into one PartGroupInfo object
361     staves = []
362     group_info = PartGroupInfo ()
363     for el in parts_groups:
364         if isinstance (el, musicxml.Score_part):
365             if not group_info.is_empty ():
366                 staves.append (group_info)
367                 group_info = PartGroupInfo ()
368             staff = read_score_part (el)
369             if staff:
370                 staves.append (staff)
371         elif isinstance (el, musicxml.Part_group):
372             if el.type == "start":
373                 group_info.add_start (el)
374             elif el.type == "stop":
375                 group_info.add_end (el)
376     if not group_info.is_empty ():
377         staves.append (group_info)
378
379     # 2) Now, detect the groups:
380     group_starts = []
381     pos = 0
382     while pos < len (staves):
383         el = staves[pos]
384         if isinstance (el, PartGroupInfo):
385             prev_start = 0
386             if len (group_starts) > 0:
387                 prev_start = group_starts[-1]
388             elif len (el.end) > 0: # no group to end here
389                 el.end = {}
390             if len (el.end) > 0: # closes an existing group
391                 ends = el.end.keys ()
392                 prev_started = staves[prev_start].start.keys ()
393                 grpid = None
394                 intersection = filter(lambda x:x in ends, prev_started)
395                 if len (intersection) > 0:
396                     grpid = intersection[0]
397                 else:
398                     # Close the last started group
399                     grpid = staves[prev_start].start.keys () [0]
400                     # Find the corresponding closing tag and remove it!
401                     j = pos + 1
402                     foundclosing = False
403                     while j < len (staves) and not foundclosing:
404                         if isinstance (staves[j], PartGroupInfo) and staves[j].end.has_key (grpid):
405                             foundclosing = True
406                             del staves[j].end[grpid]
407                             if staves[j].is_empty ():
408                                 del staves[j]
409                         j += 1
410                 grpobj = staves[prev_start].start[grpid]
411                 group = read_score_group (grpobj)
412                 # remove the id from both the start and end
413                 if el.end.has_key (grpid):
414                     del el.end[grpid]
415                 del staves[prev_start].start[grpid]
416                 if el.is_empty ():
417                     del staves[pos]
418                 # replace the staves with the whole group
419                 for j in staves[(prev_start + 1):pos]:
420                     if j.is_group:
421                         j.stafftype = "InnerStaffGroup"
422                     group.append_staff (j)
423                 del staves[(prev_start + 1):pos]
424                 staves.insert (prev_start + 1, group)
425                 # reset pos so that we continue at the correct position
426                 pos = prev_start
427                 # remove an empty start group
428                 if staves[prev_start].is_empty ():
429                     del staves[prev_start]
430                     group_starts.remove (prev_start)
431                     pos -= 1
432             elif len (el.start) > 0: # starts new part groups
433                 group_starts.append (pos)
434         pos += 1
435
436     if len (staves) == 1:
437         return staves[0]
438     for i in staves:
439         structure.append_staff (i)
440     return structure
441
442
443 def musicxml_duration_to_lily (mxl_note):
444     d = musicexp.Duration ()
445     # if the note has no Type child, then that method spits out a warning and 
446     # returns 0, i.e. a whole note
447     d.duration_log = mxl_note.get_duration_log ()
448
449     d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
450     # Grace notes by specification have duration 0, so no time modification 
451     # factor is possible. It even messes up the output with *0/1
452     if not mxl_note.get_maybe_exist_typed_child (musicxml.Grace):
453         d.factor = mxl_note._duration / d.get_length ()
454
455     return d
456
457 def rational_to_lily_duration (rational_len):
458     d = musicexp.Duration ()
459
460     rational_len.normalize_self ()
461     d_log = {1: 0, 2: 1, 4:2, 8:3, 16:4, 32:5, 64:6, 128:7, 256:8, 512:9}.get (rational_len.denominator (), -1)
462
463     # Duration of the form 1/2^n or 3/2^n can be converted to a simple lilypond duration
464     if (d_log >= 0 and rational_len.numerator() in (1,3,5,7) ):
465         # account for the dots!
466         d.dots = (rational_len.numerator()-1)/2
467         d.duration_log = d_log - d.dots
468     elif (d_log >= 0):
469         d.duration_log = d_log
470         d.factor = Rational (rational_len.numerator ())
471     else:
472         error_message (_ ("Encountered rational duration with denominator %s, "
473                        "unable to convert to lilypond duration") %
474                        rational_len.denominator ())
475         # TODO: Test the above error message
476         return None
477
478     return d
479
480 def musicxml_partial_to_lily (partial_len):
481     if partial_len > 0:
482         p = musicexp.Partial ()
483         p.partial = rational_to_lily_duration (partial_len)
484         return p
485     else:
486         return Null
487
488 # Detect repeats and alternative endings in the chord event list (music_list)
489 # and convert them to the corresponding musicexp objects, containing nested
490 # music
491 def group_repeats (music_list):
492     repeat_replaced = True
493     music_start = 0
494     i = 0
495     # Walk through the list of expressions, looking for repeat structure
496     # (repeat start/end, corresponding endings). If we find one, try to find the
497     # last event of the repeat, replace the whole structure and start over again.
498     # For nested repeats, as soon as we encounter another starting repeat bar,
499     # treat that one first, and start over for the outer repeat.
500     while repeat_replaced and i < 100:
501         i += 1
502         repeat_start = -1  # position of repeat start / end
503         repeat_end = -1 # position of repeat start / end
504         repeat_times = 0
505         ending_start = -1 # position of current ending start
506         endings = [] # list of already finished endings
507         pos = 0
508         last = len (music_list) - 1
509         repeat_replaced = False
510         final_marker = 0
511         while pos < len (music_list) and not repeat_replaced:
512             e = music_list[pos]
513             repeat_finished = False
514             if isinstance (e, RepeatMarker):
515                 if not repeat_times and e.times:
516                     repeat_times = e.times
517                 if e.direction == -1:
518                     if repeat_end >= 0:
519                         repeat_finished = True
520                     else:
521                         repeat_start = pos
522                         repeat_end = -1
523                         ending_start = -1
524                         endings = []
525                 elif e.direction == 1:
526                     if repeat_start < 0:
527                         repeat_start = 0
528                     if repeat_end < 0:
529                         repeat_end = pos
530                     final_marker = pos
531             elif isinstance (e, EndingMarker):
532                 if e.direction == -1:
533                     if repeat_start < 0:
534                         repeat_start = 0
535                     if repeat_end < 0:
536                         repeat_end = pos
537                     ending_start = pos
538                 elif e.direction == 1:
539                     if ending_start < 0:
540                         ending_start = 0
541                     endings.append ([ending_start, pos])
542                     ending_start = -1
543                     final_marker = pos
544             elif not isinstance (e, musicexp.BarLine):
545                 # As soon as we encounter an element when repeat start and end
546                 # is set and we are not inside an alternative ending,
547                 # this whole repeat structure is finished => replace it
548                 if repeat_start >= 0 and repeat_end > 0 and ending_start < 0:
549                     repeat_finished = True
550
551             # Finish off all repeats without explicit ending bar (e.g. when
552             # we convert only one page of a multi-page score with repeats)
553             if pos == last and repeat_start >= 0:
554                 repeat_finished = True
555                 final_marker = pos
556                 if repeat_end < 0:
557                     repeat_end = pos
558                 if ending_start >= 0:
559                     endings.append ([ending_start, pos])
560                     ending_start = -1
561
562             if repeat_finished:
563                 # We found the whole structure replace it!
564                 r = musicexp.RepeatedMusic ()
565                 if repeat_times <= 0:
566                     repeat_times = 2
567                 r.repeat_count = repeat_times
568                 # don't erase the first element for "implicit" repeats (i.e. no
569                 # starting repeat bars at the very beginning)
570                 start = repeat_start+1
571                 if repeat_start == music_start:
572                     start = music_start
573                 r.set_music (music_list[start:repeat_end])
574                 for (start, end) in endings:
575                     s = musicexp.SequentialMusic ()
576                     s.elements = music_list[start+1:end]
577                     r.add_ending (s)
578                 del music_list[repeat_start:final_marker+1]
579                 music_list.insert (repeat_start, r)
580                 repeat_replaced = True
581             pos += 1
582         # TODO: Implement repeats until the end without explicit ending bar
583     return music_list
584
585
586
587 def group_tuplets (music_list, events):
588
589
590     """Collect Musics from
591     MUSIC_LIST demarcated by EVENTS_LIST in TimeScaledMusic objects.
592     """
593
594     
595     indices = []
596
597     j = 0
598     for (ev_chord, tuplet_elt, fraction) in events:
599         while (j < len (music_list)):
600             if music_list[j] == ev_chord:
601                 break
602             j += 1
603         if tuplet_elt.type == 'start':
604             indices.append ((j, None, fraction))
605         elif tuplet_elt.type == 'stop':
606             indices[-1] = (indices[-1][0], j, indices[-1][2])
607
608     new_list = []
609     last = 0
610     for (i1, i2, frac) in indices:
611         if i1 >= i2:
612             continue
613
614         new_list.extend (music_list[last:i1])
615         seq = musicexp.SequentialMusic ()
616         last = i2 + 1
617         seq.elements = music_list[i1:last]
618
619         tsm = musicexp.TimeScaledMusic ()
620         tsm.element = seq
621
622         tsm.numerator = frac[0]
623         tsm.denominator  = frac[1]
624
625         new_list.append (tsm)
626
627     new_list.extend (music_list[last:])
628     return new_list
629
630
631 def musicxml_clef_to_lily (attributes):
632     change = musicexp.ClefChange ()
633     (change.type, change.position, change.octave) = attributes.get_clef_information ()
634     return change
635     
636 def musicxml_time_to_lily (attributes):
637     (beats, type) = attributes.get_time_signature ()
638
639     change = musicexp.TimeSignatureChange()
640     change.fraction = (beats, type)
641     
642     return change
643
644 def musicxml_key_to_lily (attributes):
645     start_pitch  = musicexp.Pitch ()
646     (fifths, mode) = attributes.get_key_signature () 
647     try:
648         (n,a) = {
649             'major' : (0,0),
650             'minor' : (5,0),
651             }[mode]
652         start_pitch.step = n
653         start_pitch.alteration = a
654     except  KeyError:
655         error_message (_ ("unknown mode %s, expecting 'major' or 'minor'") % mode)
656
657     fifth = musicexp.Pitch()
658     fifth.step = 4
659     if fifths < 0:
660         fifths *= -1
661         fifth.step *= -1
662         fifth.normalize ()
663     
664     for x in range (fifths):
665         start_pitch = start_pitch.transposed (fifth)
666
667     start_pitch.octave = 0
668
669     change = musicexp.KeySignatureChange()
670     change.mode = mode
671     change.tonic = start_pitch
672     return change
673     
674 def musicxml_attributes_to_lily (attrs):
675     elts = []
676     attr_dispatch =  {
677         'clef': musicxml_clef_to_lily,
678         'time': musicxml_time_to_lily,
679         'key': musicxml_key_to_lily
680     }
681     for (k, func) in attr_dispatch.items ():
682         children = attrs.get_named_children (k)
683         if children:
684             elts.append (func (attrs))
685     
686     return elts
687
688 class Marker (musicexp.Music):
689     def __init__ (self):
690         self.direction = 0
691         self.event = None
692     def print_ly (self, printer):
693         ly.stderr_write (_ ("Encountered unprocessed marker %s\n") % self)
694         pass
695     def ly_expression (self):
696         return ""
697 class RepeatMarker (Marker):
698     def __init__ (self):
699         Marker.__init__ (self)
700         self.times = 0
701 class EndingMarker (Marker):
702     pass
703
704 # Convert the <barline> element to musicxml.BarLine (for non-standard barlines)
705 # and to RepeatMarker and EndingMarker objects for repeat and
706 # alternatives start/stops
707 def musicxml_barline_to_lily (barline):
708     # retval contains all possible markers in the order:
709     # 0..bw_ending, 1..bw_repeat, 2..barline, 3..fw_repeat, 4..fw_ending
710     retval = {}
711     bartype_element = barline.get_maybe_exist_named_child ("bar-style")
712     repeat_element = barline.get_maybe_exist_named_child ("repeat")
713     ending_element = barline.get_maybe_exist_named_child ("ending")
714
715     bartype = None
716     if bartype_element:
717         bartype = bartype_element.get_text ()
718
719     if repeat_element and hasattr (repeat_element, 'direction'):
720         repeat = RepeatMarker ()
721         repeat.direction = {"forward": -1, "backward": 1}.get (repeat_element.direction, 0)
722
723         if ( (repeat_element.direction == "forward" and bartype == "heavy-light") or
724              (repeat_element.direction == "backward" and bartype == "light-heavy") ):
725             bartype = None
726         if hasattr (repeat_element, 'times'):
727             try:
728                 repeat.times = int (repeat_element.times)
729             except ValueError:
730                 repeat.times = 2
731         repeat.event = barline
732         if repeat.direction == -1:
733             retval[3] = repeat
734         else:
735             retval[1] = repeat
736
737     if ending_element and hasattr (ending_element, 'type'):
738         ending = EndingMarker ()
739         ending.direction = {"start": -1, "stop": 1, "discontinue": 1}.get (ending_element.type, 0)
740         ending.event = barline
741         if ending.direction == -1:
742             retval[4] = ending
743         else:
744             retval[0] = ending
745
746     if bartype:
747         b = musicexp.BarLine ()
748         b.type = bartype
749         retval[2] = b
750
751     return retval.values ()
752
753 spanner_event_dict = {
754     'beam' : musicexp.BeamEvent,
755     'dashes' : musicexp.TextSpannerEvent,
756     'bracket' : musicexp.BracketSpannerEvent,
757     'glissando' : musicexp.GlissandoEvent,
758     'octave-shift' : musicexp.OctaveShiftEvent,
759     'pedal' : musicexp.PedalEvent,
760     'slide' : musicexp.GlissandoEvent,
761     'slur' : musicexp.SlurEvent,
762     'wavy-line' : musicexp.TrillSpanEvent,
763     'wedge' : musicexp.HairpinEvent
764 }
765 spanner_type_dict = {
766     'start': -1,
767     'begin': -1,
768     'crescendo': -1,
769     'decreschendo': -1,
770     'diminuendo': -1,
771     'continue': 0,
772     'change': 0,
773     'up': -1,
774     'down': -1,
775     'stop': 1,
776     'end' : 1
777 }
778
779 def musicxml_spanner_to_lily_event (mxl_event):
780     ev = None
781     
782     name = mxl_event.get_name()
783     func = spanner_event_dict.get (name)
784     if func:
785         ev = func()
786     else:
787         error_message (_ ('unknown span event %s') % mxl_event)
788
789
790     type = mxl_event.get_type ()
791     span_direction = spanner_type_dict.get (type)
792     # really check for None, because some types will be translated to 0, which
793     # would otherwise also lead to the unknown span warning
794     if span_direction != None:
795         ev.span_direction = span_direction
796     else:
797         error_message (_ ('unknown span type %s for %s') % (type, name))
798
799     ev.set_span_type (type)
800     ev.line_type = getattr (mxl_event, 'line-type', 'solid')
801
802     # assign the size, which is used for octave-shift, etc.
803     ev.size = mxl_event.get_size ()
804
805     return ev
806
807 def musicxml_direction_to_indicator (direction):
808     return { "above": 1, "upright": 1, "up": 1, "below": -1, "downright": -1, "down": -1, "inverted": -1 }.get (direction, 0)
809
810 def musicxml_fermata_to_lily_event (mxl_event):
811     ev = musicexp.ArticulationEvent ()
812     txt = mxl_event.get_text ()
813     # The contents of the element defined the shape, possible are normal, angled and square
814     ev.type = { "angled": "shortfermata", "square": "longfermata" }.get (txt, "fermata")
815     if hasattr (mxl_event, 'type'):
816       dir = musicxml_direction_to_indicator (mxl_event.type)
817       if dir and options.convert_directions:
818         ev.force_direction = dir
819     return ev
820
821 def musicxml_arpeggiate_to_lily_event (mxl_event):
822     ev = musicexp.ArpeggioEvent ()
823     ev.direction = musicxml_direction_to_indicator (getattr (mxl_event, 'direction', None))
824     return ev
825
826 def musicxml_nonarpeggiate_to_lily_event (mxl_event):
827     ev = musicexp.ArpeggioEvent ()
828     ev.non_arpeggiate = True
829     ev.direction = musicxml_direction_to_indicator (getattr (mxl_event, 'direction', None))
830     return ev
831
832 def musicxml_tremolo_to_lily_event (mxl_event):
833     ev = musicexp.TremoloEvent ()
834     txt = mxl_event.get_text ()
835     if txt:
836       ev.bars = txt
837     else:
838       ev.bars = "3"
839     return ev
840
841 def musicxml_falloff_to_lily_event (mxl_event):
842     ev = musicexp.BendEvent ()
843     ev.alter = -4
844     return ev
845
846 def musicxml_doit_to_lily_event (mxl_event):
847     ev = musicexp.BendEvent ()
848     ev.alter = 4
849     return ev
850
851 def musicxml_bend_to_lily_event (mxl_event):
852     ev = musicexp.BendEvent ()
853     ev.alter = mxl_event.bend_alter ()
854     return ev
855
856 def musicxml_caesura_to_lily_event (mxl_event):
857     ev = musicexp.MarkupEvent ()
858     # FIXME: default to straight or curved caesura?
859     ev.contents = "\\musicglyph #\"scripts.caesura.straight\""
860     ev.force_direction = 1
861     return ev
862
863 def musicxml_fingering_event (mxl_event):
864     ev = musicexp.ShortArticulationEvent ()
865     ev.type = mxl_event.get_text ()
866     return ev
867
868 def musicxml_snappizzicato_event (mxl_event):
869     needed_additional_definitions.append ("snappizzicato")
870     ev = musicexp.MarkupEvent ()
871     ev.contents = "\\snappizzicato"
872     return ev
873
874 def musicxml_string_event (mxl_event):
875     ev = musicexp.NoDirectionArticulationEvent ()
876     ev.type = mxl_event.get_text ()
877     return ev
878
879 def musicxml_accidental_mark (mxl_event):
880     ev = musicexp.MarkupEvent ()
881     contents = { "sharp": "\\sharp",
882       "natural": "\\natural",
883       "flat": "\\flat",
884       "double-sharp": "\\doublesharp",
885       "sharp-sharp": "\\sharp\\sharp",
886       "flat-flat": "\\flat\\flat",
887       "flat-flat": "\\doubleflat",
888       "natural-sharp": "\\natural\\sharp",
889       "natural-flat": "\\natural\\flat",
890       "quarter-flat": "\\semiflat",
891       "quarter-sharp": "\\semisharp",
892       "three-quarters-flat": "\\sesquiflat",
893       "three-quarters-sharp": "\\sesquisharp",
894     }.get (mxl_event.get_text ())
895     if contents:
896         ev.contents = contents
897         return ev
898     else:
899         return None
900
901 # translate articulations, ornaments and other notations into ArticulationEvents
902 # possible values:
903 #   -) string  (ArticulationEvent with that name)
904 #   -) function (function(mxl_event) needs to return a full ArticulationEvent-derived object
905 #   -) (class, name)  (like string, only that a different class than ArticulationEvent is used)
906 # TODO: Some translations are missing!
907 articulations_dict = {
908     "accent": (musicexp.ShortArticulationEvent, ">"), # or "accent"
909     "accidental-mark": musicxml_accidental_mark,
910     "bend": musicxml_bend_to_lily_event,
911     "breath-mark": (musicexp.NoDirectionArticulationEvent, "breathe"),
912     "caesura": musicxml_caesura_to_lily_event,
913     #"delayed-turn": "?",
914     "detached-legato": (musicexp.ShortArticulationEvent, "_"), # or "portato"
915     "doit": musicxml_doit_to_lily_event,
916     #"double-tongue": "",
917     "down-bow": "downbow",
918     "falloff": musicxml_falloff_to_lily_event,
919     "fingering": musicxml_fingering_event,
920     #"fingernails": "",
921     #"fret": "",
922     #"hammer-on": "",
923     "harmonic": "flageolet",
924     #"heel": "",
925     "inverted-mordent": "prall",
926     "inverted-turn": "reverseturn",
927     "mordent": "mordent",
928     "open-string": "open",
929     #"plop": "",
930     #"pluck": "",
931     #"pull-off": "",
932     #"schleifer": "?",
933     #"scoop": "",
934     #"shake": "?",
935     "snap-pizzicato": musicxml_snappizzicato_event,
936     #"spiccato": "",
937     "staccatissimo": (musicexp.ShortArticulationEvent, "|"), # or "staccatissimo"
938     "staccato": (musicexp.ShortArticulationEvent, "."), # or "staccato"
939     "stopped": (musicexp.ShortArticulationEvent, "+"), # or "stopped"
940     #"stress": "",
941     "string": musicxml_string_event,
942     "strong-accent": (musicexp.ShortArticulationEvent, "^"), # or "marcato"
943     #"tap": "",
944     "tenuto": (musicexp.ShortArticulationEvent, "-"), # or "tenuto"
945     "thumb-position": "thumb",
946     #"toe": "",
947     "turn": "turn",
948     "tremolo": musicxml_tremolo_to_lily_event,
949     "trill-mark": "trill",
950     #"triple-tongue": "",
951     #"unstress": ""
952     "up-bow": "upbow",
953     #"wavy-line": "?",
954 }
955 articulation_spanners = [ "wavy-line" ]
956
957 def musicxml_articulation_to_lily_event (mxl_event):
958     # wavy-line elements are treated as trill spanners, not as articulation ornaments
959     if mxl_event.get_name () in articulation_spanners:
960         return musicxml_spanner_to_lily_event (mxl_event)
961
962     tmp_tp = articulations_dict.get (mxl_event.get_name ())
963     if not tmp_tp:
964         return
965
966     if isinstance (tmp_tp, str):
967         ev = musicexp.ArticulationEvent ()
968         ev.type = tmp_tp
969     elif isinstance (tmp_tp, tuple):
970         ev = tmp_tp[0] ()
971         ev.type = tmp_tp[1]
972     else:
973         ev = tmp_tp (mxl_event)
974
975     # Some articulations use the type attribute, other the placement...
976     dir = None
977     if hasattr (mxl_event, 'type') and options.convert_directions:
978         dir = musicxml_direction_to_indicator (mxl_event.type)
979     if hasattr (mxl_event, 'placement') and options.convert_directions:
980         dir = musicxml_direction_to_indicator (mxl_event.placement)
981     if dir:
982         ev.force_direction = dir
983     return ev
984
985
986
987 def musicxml_dynamics_to_lily_event (dynentry):
988     dynamics_available = (
989         "ppppp", "pppp", "ppp", "pp", "p", "mp", "mf", 
990         "f", "ff", "fff", "ffff", "fp", "sf", "sff", "sp", "spp", "sfz", "rfz" )
991     dynamicsname = dynentry.get_name ()
992     if dynamicsname == "other-dynamics":
993         dynamicsname = dynentry.get_text ()
994     if not dynamicsname or dynamicsname=="#text":
995         return
996
997     if not dynamicsname in dynamics_available:
998         # Get rid of - in tag names (illegal in ly tags!)
999         dynamicstext = dynamicsname
1000         dynamicsname = string.replace (dynamicsname, "-", "")
1001         additional_definitions[dynamicsname] = dynamicsname + \
1002               " = #(make-dynamic-script \"" + dynamicstext + "\")"
1003         needed_additional_definitions.append (dynamicsname)
1004     event = musicexp.DynamicsEvent ()
1005     event.type = dynamicsname
1006     return event
1007
1008 # Convert single-color two-byte strings to numbers 0.0 - 1.0
1009 def hexcolorval_to_nr (hex_val):
1010     try:
1011         v = int (hex_val, 16)
1012         if v == 255:
1013             v = 256
1014         return v / 256.
1015     except ValueError:
1016         return 0.
1017
1018 def hex_to_color (hex_val):
1019     res = re.match (r'#([0-9a-f][0-9a-f]|)([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$', hex_val, re.IGNORECASE)
1020     if res:
1021         return map (lambda x: hexcolorval_to_nr (x), res.group (2,3,4))
1022     else:
1023         return None
1024
1025 def musicxml_words_to_lily_event (words):
1026     event = musicexp.TextEvent ()
1027     text = words.get_text ()
1028     text = re.sub ('^ *\n? *', '', text)
1029     text = re.sub (' *\n? *$', '', text)
1030     event.text = text
1031
1032     if hasattr (words, 'default-y') and options.convert_directions:
1033         offset = getattr (words, 'default-y')
1034         try:
1035             off = string.atoi (offset)
1036             if off > 0:
1037                 event.force_direction = 1
1038             else:
1039                 event.force_direction = -1
1040         except ValueError:
1041             event.force_direction = 0
1042
1043     if hasattr (words, 'font-weight'):
1044         font_weight = { "normal": '', "bold": '\\bold' }.get (getattr (words, 'font-weight'), '')
1045         if font_weight:
1046             event.markup += font_weight
1047
1048     if hasattr (words, 'font-size'):
1049         size = getattr (words, 'font-size')
1050         font_size = {
1051             "xx-small": '\\teeny',
1052             "x-small": '\\tiny',
1053             "small": '\\small',
1054             "medium": '',
1055             "large": '\\large',
1056             "x-large": '\\huge',
1057             "xx-large": '\\bigger\\huge'
1058         }.get (size, '')
1059         if font_size:
1060             event.markup += font_size
1061
1062     if hasattr (words, 'color'):
1063         color = getattr (words, 'color')
1064         rgb = hex_to_color (color)
1065         if rgb:
1066             event.markup += "\\with-color #(rgb-color %s %s %s)" % (rgb[0], rgb[1], rgb[2])
1067
1068     if hasattr (words, 'font-style'):
1069         font_style = { "italic": '\\italic' }.get (getattr (words, 'font-style'), '')
1070         if font_style:
1071             event.markup += font_style
1072
1073     # TODO: How should I best convert the font-family attribute?
1074
1075     # TODO: How can I represent the underline, overline and line-through
1076     #       attributes in Lilypond? Values of these attributes indicate
1077     #       the number of lines
1078
1079     return event
1080
1081
1082 # convert accordion-registration to lilypond.
1083 # Since lilypond does not have any built-in commands, we need to create
1084 # the markup commands manually and define our own variables.
1085 # Idea was taken from: http://lsr.dsi.unimi.it/LSR/Item?id=194
1086 def musicxml_accordion_to_markup (mxl_event):
1087     commandname = "accReg"
1088     command = ""
1089
1090     high = mxl_event.get_maybe_exist_named_child ('accordion-high')
1091     if high:
1092         commandname += "H"
1093         command += """\\combine
1094           \\raise #2.5 \\musicglyph #\"accordion.accDot\"
1095           """
1096     middle = mxl_event.get_maybe_exist_named_child ('accordion-middle')
1097     if middle:
1098         # By default, use one dot (when no or invalid content is given). The 
1099         # MusicXML spec is quiet about this case...
1100         txt = 1
1101         try:
1102           txt = string.atoi (middle.get_text ())
1103         except ValueError:
1104             pass
1105         if txt == 3:
1106             commandname += "MMM"
1107             command += """\\combine
1108           \\raise #1.5 \\musicglyph #\"accordion.accDot\"
1109           \\combine
1110           \\raise #1.5 \\translate #(cons 1 0) \\musicglyph #\"accordion.accDot\"
1111           \\combine
1112           \\raise #1.5 \\translate #(cons -1 0) \\musicglyph #\"accordion.accDot\"
1113           """
1114         elif txt == 2:
1115             commandname += "MM"
1116             command += """\\combine
1117           \\raise #1.5 \\translate #(cons 0.5 0) \\musicglyph #\"accordion.accDot\"
1118           \\combine
1119           \\raise #1.5 \\translate #(cons -0.5 0) \\musicglyph #\"accordion.accDot\"
1120           """
1121         elif not txt <= 0:
1122             commandname += "M"
1123             command += """\\combine
1124           \\raise #1.5 \\musicglyph #\"accordion.accDot\"
1125           """
1126     low = mxl_event.get_maybe_exist_named_child ('accordion-low')
1127     if low:
1128         commandname += "L"
1129         command += """\\combine
1130           \\raise #0.5 \musicglyph #\"accordion.accDot\"
1131           """
1132
1133     command += "\musicglyph #\"accordion.accDiscant\""
1134     command = "\\markup { \\normalsize %s }" % command
1135     # Define the newly built command \accReg[H][MMM][L]
1136     additional_definitions[commandname] = "%s = %s" % (commandname, command)
1137     needed_additional_definitions.append (commandname)
1138     return "\\%s" % commandname
1139
1140 def musicxml_accordion_to_ly (mxl_event):
1141     txt = musicxml_accordion_to_markup (mxl_event)
1142     if txt:
1143         ev = musicexp.MarkEvent (txt)
1144         return ev
1145     return
1146
1147
1148 def musicxml_rehearsal_to_ly_mark (mxl_event):
1149     text = mxl_event.get_text ()
1150     if not text:
1151         return
1152     # default is boxed rehearsal marks!
1153     encl = "box"
1154     if hasattr (mxl_event, 'enclosure'):
1155         encl = {"none": None, "square": "box", "circle": "circle" }.get (mxl_event.enclosure, None)
1156     if encl:
1157         text = "\\%s { %s }" % (encl, text)
1158     ev = musicexp.MarkEvent ("\\markup { %s }" % text)
1159     return ev
1160
1161 def musicxml_eyeglasses_to_ly (mxl_event):
1162     needed_additional_definitions.append ("eyeglasses")
1163     return musicexp.MarkEvent ("\\eyeglasses")
1164
1165 def next_non_hash_index (lst, pos):
1166     pos += 1
1167     while pos < len (lst) and isinstance (lst[pos], musicxml.Hash_text):
1168         pos += 1
1169     return pos
1170
1171 def musicxml_metronome_to_ly (mxl_event):
1172     children = mxl_event.get_all_children ()
1173     if not children:
1174         return
1175
1176     index = -1
1177     index = next_non_hash_index (children, index)
1178     if isinstance (children[index], musicxml.BeatUnit): 
1179         # first form of metronome-mark, using unit and beats/min or other unit
1180         ev = musicexp.TempoMark ()
1181         if hasattr (mxl_event, 'parentheses'):
1182             ev.set_parentheses (mxl_event.parentheses == "yes")
1183
1184         d = musicexp.Duration ()
1185         d.duration_log = musicxml.musicxml_duration_to_log (children[index].get_text ())
1186         index = next_non_hash_index (children, index)
1187         if isinstance (children[index], musicxml.BeatUnitDot):
1188             d.dots = 1
1189             index = next_non_hash_index (children, index)
1190         ev.set_base_duration (d)
1191         if isinstance (children[index], musicxml.BeatUnit):
1192             # Form "note = newnote"
1193             newd = musicexp.Duration ()
1194             newd.duration_log = musicxml.musicxml_duration_to_log (children[index].get_text ())
1195             index = next_non_hash_index (children, index)
1196             if isinstance (children[index], musicxml.BeatUnitDot):
1197                 newd.dots = 1
1198                 index = next_non_hash_index (children, index)
1199             ev.set_new_duration (newd)
1200         elif isinstance (children[index], musicxml.PerMinute):
1201             # Form "note = bpm"
1202             try:
1203                 beats = int (children[index].get_text ())
1204                 ev.set_beats_per_minute (beats)
1205             except ValueError:
1206                 pass
1207         else:
1208             error_message (_ ("Unknown metronome mark, ignoring"))
1209             return
1210         return ev
1211     else:
1212         #TODO: Implement the other (more complex) way for tempo marks!
1213         error_message (_ ("Metronome marks with complex relations (<metronome-note> in MusicXML) are not yet implemented."))
1214         return
1215
1216 # translate directions into Events, possible values:
1217 #   -) string  (MarkEvent with that command)
1218 #   -) function (function(mxl_event) needs to return a full Event-derived object
1219 #   -) (class, name)  (like string, only that a different class than MarkEvent is used)
1220 directions_dict = {
1221     'accordion-registration' : musicxml_accordion_to_ly,
1222     'coda' : (musicexp.MusicGlyphMarkEvent, "coda"),
1223 #     'damp' : ???
1224 #     'damp-all' : ???
1225     'eyeglasses': musicxml_eyeglasses_to_ly,
1226 #     'harp-pedals' : ???
1227 #     'image' : ???
1228     'metronome' : musicxml_metronome_to_ly,
1229     'rehearsal' : musicxml_rehearsal_to_ly_mark,
1230 #     'scordatura' : ???
1231     'segno' : (musicexp.MusicGlyphMarkEvent, "segno"),
1232     'words' : musicxml_words_to_lily_event,
1233 }
1234 directions_spanners = [ 'octave-shift', 'pedal', 'wedge', 'dashes', 'bracket' ]
1235
1236 def musicxml_direction_to_lily (n):
1237     # TODO: Handle the <staff> element!
1238     res = []
1239     # placement applies to all children!
1240     dir = None
1241     if hasattr (n, 'placement') and options.convert_directions:
1242         dir = musicxml_direction_to_indicator (n.placement)
1243     dirtype_children = []
1244     # TODO: The direction-type is used for grouping (e.g. dynamics with text), 
1245     #       so we can't simply flatten them out!
1246     for dt in n.get_typed_children (musicxml.DirType):
1247         dirtype_children += dt.get_all_children ()
1248
1249     for entry in dirtype_children:
1250         # backets, dashes, octave shifts. pedal marks, hairpins etc. are spanners:
1251         if entry.get_name() in directions_spanners:
1252             event = musicxml_spanner_to_lily_event (entry)
1253             if event:
1254                 res.append (event)
1255             continue
1256
1257         # now treat all the "simple" ones, that can be translated using the dict
1258         ev = None
1259         tmp_tp = directions_dict.get (entry.get_name (), None)
1260         if isinstance (tmp_tp, str): # string means MarkEvent
1261             ev = musicexp.MarkEvent (tmp_tp)
1262         elif isinstance (tmp_tp, tuple): # tuple means (EventClass, "text")
1263             ev = tmp_tp[0] (tmp_tp[1])
1264         elif tmp_tp:
1265             ev = tmp_tp (entry)
1266         if ev:
1267             # TODO: set the correct direction! Unfortunately, \mark in ly does
1268             #       not seem to support directions!
1269             res.append (ev)
1270             continue
1271
1272         if entry.get_name () == "dynamics":
1273             for dynentry in entry.get_all_children ():
1274                 ev = musicxml_dynamics_to_lily_event (dynentry)
1275                 if ev:
1276                     res.append (ev)
1277
1278     return res
1279
1280 def musicxml_frame_to_lily_event (frame):
1281     ev = musicexp.FretEvent ()
1282     ev.strings = frame.get_strings ()
1283     ev.frets = frame.get_frets ()
1284     #offset = frame.get_first_fret () - 1
1285     barre = []
1286     for fn in frame.get_named_children ('frame-note'):
1287         fret = fn.get_fret ()
1288         if fret <= 0:
1289             fret = "o"
1290         el = [ fn.get_string (), fret ]
1291         fingering = fn.get_fingering ()
1292         if fingering >= 0:
1293             el.append (fingering)
1294         ev.elements.append (el)
1295         b = fn.get_barre ()
1296         if b == 'start':
1297             barre[0] = el[0] # start string
1298             barre[2] = el[1] # fret
1299         elif b == 'stop':
1300             barre[1] = el[0] # end string
1301     if barre:
1302         ev.barre = barre
1303     return ev
1304
1305 def musicxml_harmony_to_lily (n):
1306     res = []
1307     for f in n.get_named_children ('frame'):
1308         ev = musicxml_frame_to_lily_event (f)
1309         if ev:
1310             res.append (ev)
1311
1312     return res
1313
1314 def musicxml_figured_bass_note_to_lily (n):
1315     res = musicexp.FiguredBassNote ()
1316     suffix_dict = { 'sharp' : "+", 
1317                     'flat' : "-", 
1318                     'natural' : "!", 
1319                     'double-sharp' : "++", 
1320                     'flat-flat' : "--", 
1321                     'sharp-sharp' : "++", 
1322                     'slash' : "/" }
1323     prefix = n.get_maybe_exist_named_child ('prefix')
1324     if prefix:
1325         res.set_prefix (suffix_dict.get (prefix.get_text (), ""))
1326     fnumber = n.get_maybe_exist_named_child ('figure-number')
1327     if fnumber:
1328         res.set_number (fnumber.get_text ())
1329     suffix = n.get_maybe_exist_named_child ('suffix')
1330     if suffix:
1331         res.set_suffix (suffix_dict.get (suffix.get_text (), ""))
1332     if n.get_maybe_exist_named_child ('extend'):
1333         # TODO: Implement extender lines (unfortunately, in lilypond you have 
1334         #       to use \set useBassFigureExtenders = ##t, which turns them on
1335         #       globally, while MusicXML has a property for each note...
1336         #       I'm not sure there is a proper way to implement this cleanly
1337         #n.extend
1338         pass
1339     return res
1340
1341
1342
1343 def musicxml_figured_bass_to_lily (n):
1344     if not isinstance (n, musicxml.FiguredBass):
1345         return
1346     res = musicexp.FiguredBassEvent ()
1347     for i in n.get_named_children ('figure'):
1348         note = musicxml_figured_bass_note_to_lily (i)
1349         if note:
1350             res.append (note)
1351     dur = n.get_maybe_exist_named_child ('duration')
1352     if dur:
1353         # apply the duration to res
1354         length = Rational(int(dur.get_text()), n._divisions)*Rational(1,4)
1355         res.set_real_duration (length)
1356         duration = rational_to_lily_duration (length)
1357         if duration:
1358             res.set_duration (duration)
1359     if hasattr (n, 'parentheses') and n.parentheses == "yes":
1360         res.set_parentheses (True)
1361     return res
1362
1363 instrument_drumtype_dict = {
1364     'Acoustic Snare Drum': 'acousticsnare',
1365     'Side Stick': 'sidestick',
1366     'Open Triangle': 'opentriangle',
1367     'Mute Triangle': 'mutetriangle',
1368     'Tambourine': 'tambourine',
1369     'Bass Drum': 'bassdrum',
1370 }
1371
1372 def musicxml_note_to_lily_main_event (n):
1373     pitch  = None
1374     duration = None
1375     event = None
1376
1377     mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
1378     if mxl_pitch:
1379         pitch = musicxml_pitch_to_lily (mxl_pitch)
1380         event = musicexp.NoteEvent ()
1381         event.pitch = pitch
1382
1383         acc = n.get_maybe_exist_named_child ('accidental')
1384         if acc:
1385             # let's not force accs everywhere. 
1386             event.cautionary = acc.editorial
1387
1388     elif n.get_maybe_exist_typed_child (musicxml.Unpitched):
1389         # Unpitched elements have display-step and can also have
1390         # display-octave.
1391         unpitched = n.get_maybe_exist_typed_child (musicxml.Unpitched)
1392         event = musicexp.NoteEvent ()
1393         event.pitch = musicxml_unpitched_to_lily (unpitched)
1394         
1395     elif n.get_maybe_exist_typed_child (musicxml.Rest):
1396         # rests can have display-octave and display-step, which are
1397         # treated like an ordinary note pitch
1398         rest = n.get_maybe_exist_typed_child (musicxml.Rest)
1399         event = musicexp.RestEvent ()
1400         pitch = musicxml_restdisplay_to_lily (rest)
1401         event.pitch = pitch
1402
1403     elif n.instrument_name:
1404         event = musicexp.NoteEvent ()
1405         drum_type = instrument_drumtype_dict.get (n.instrument_name)
1406         if drum_type:
1407             event.drum_type = drum_type
1408         else:
1409             n.message (_ ("drum %s type unknown, please add to instrument_drumtype_dict") % n.instrument_name)
1410             event.drum_type = 'acousticsnare'
1411
1412     else:
1413         n.message (_ ("cannot find suitable event"))
1414
1415     if event:
1416         event.duration = musicxml_duration_to_lily (n)
1417
1418     return event
1419
1420
1421 ## TODO
1422 class NegativeSkip:
1423     def __init__ (self, here, dest):
1424         self.here = here
1425         self.dest = dest
1426
1427 class LilyPondVoiceBuilder:
1428     def __init__ (self):
1429         self.elements = []
1430         self.pending_dynamics = []
1431         self.end_moment = Rational (0)
1432         self.begin_moment = Rational (0)
1433         self.pending_multibar = Rational (0)
1434         self.ignore_skips = False
1435
1436     def _insert_multibar (self):
1437         r = musicexp.MultiMeasureRest ()
1438         r.duration = musicexp.Duration()
1439         r.duration.duration_log = 0
1440         r.duration.factor = self.pending_multibar
1441         self.elements.append (r)
1442         self.begin_moment = self.end_moment
1443         self.end_moment = self.begin_moment + self.pending_multibar
1444         self.pending_multibar = Rational (0)
1445         
1446     def add_multibar_rest (self, duration):
1447         self.pending_multibar += duration
1448
1449     def set_duration (self, duration):
1450         self.end_moment = self.begin_moment + duration
1451     def current_duration (self):
1452         return self.end_moment - self.begin_moment
1453         
1454     def add_music (self, music, duration):
1455         assert isinstance (music, musicexp.Music)
1456         if self.pending_multibar > Rational (0):
1457             self._insert_multibar ()
1458
1459         self.elements.append (music)
1460         self.begin_moment = self.end_moment
1461         self.set_duration (duration)
1462         
1463         # Insert all pending dynamics right after the note/rest:
1464         if isinstance (music, musicexp.ChordEvent) and self.pending_dynamics:
1465             for d in self.pending_dynamics:
1466                 music.append (d)
1467             self.pending_dynamics = []
1468
1469     # Insert some music command that does not affect the position in the measure
1470     def add_command (self, command):
1471         assert isinstance (command, musicexp.Music)
1472         if self.pending_multibar > Rational (0):
1473             self._insert_multibar ()
1474         self.elements.append (command)
1475     def add_barline (self, barline):
1476         # TODO: Implement merging of default barline and custom bar line
1477         self.add_music (barline, Rational (0))
1478     def add_partial (self, command):
1479         self.ignore_skips = True
1480         self.add_command (command)
1481
1482     def add_dynamics (self, dynamic):
1483         # store the dynamic item(s) until we encounter the next note/rest:
1484         self.pending_dynamics.append (dynamic)
1485
1486     def add_bar_check (self, number):
1487         b = musicexp.BarLine ()
1488         b.bar_number = number
1489         self.add_barline (b)
1490
1491     def jumpto (self, moment):
1492         current_end = self.end_moment + self.pending_multibar
1493         diff = moment - current_end
1494         
1495         if diff < Rational (0):
1496             error_message (_ ('Negative skip %s') % diff)
1497             diff = Rational (0)
1498
1499         if diff > Rational (0) and not (self.ignore_skips and moment == 0):
1500             skip = musicexp.SkipEvent()
1501             duration_factor = 1
1502             duration_log = {1: 0, 2: 1, 4:2, 8:3, 16:4, 32:5, 64:6, 128:7, 256:8, 512:9}.get (diff.denominator (), -1)
1503             duration_dots = 0
1504             if duration_log > 0: # denominator is a power of 2...
1505                 if diff.numerator () == 3:
1506                     duration_log -= 1
1507                     duration_dots = 1
1508                 else:
1509                     duration_factor = Rational (diff.numerator ())
1510             else:
1511                 # for skips of a whole or more, simply use s1*factor
1512                 duration_log = 0
1513                 duration_factor = diff
1514             skip.duration.duration_log = duration_log
1515             skip.duration.factor = duration_factor
1516             skip.duration.dots = duration_dots
1517
1518             evc = musicexp.ChordEvent ()
1519             evc.elements.append (skip)
1520             self.add_music (evc, diff)
1521
1522         if diff > Rational (0) and moment == 0:
1523             self.ignore_skips = False
1524
1525     def last_event_chord (self, starting_at):
1526
1527         value = None
1528
1529         # if the position matches, find the last ChordEvent, do not cross a bar line!
1530         at = len( self.elements ) - 1
1531         while (at >= 0 and
1532                not isinstance (self.elements[at], musicexp.ChordEvent) and
1533                not isinstance (self.elements[at], musicexp.BarLine)):
1534             at -= 1
1535
1536         if (self.elements
1537             and at >= 0
1538             and isinstance (self.elements[at], musicexp.ChordEvent)
1539             and self.begin_moment == starting_at):
1540             value = self.elements[at]
1541         else:
1542             self.jumpto (starting_at)
1543             value = None
1544         return value
1545         
1546     def correct_negative_skip (self, goto):
1547         self.end_moment = goto
1548         self.begin_moment = goto
1549         evc = musicexp.ChordEvent ()
1550         self.elements.append (evc)
1551
1552
1553 class VoiceData:
1554     def __init__ (self):
1555         self.voicename = None
1556         self.voicedata = None
1557         self.ly_voice = None
1558         self.figured_bass = None
1559         self.lyrics_dict = {}
1560         self.lyrics_order = []
1561
1562 def musicxml_step_to_lily (step):
1563     if step:
1564         return (ord (step) - ord ('A') + 7 - 2) % 7
1565     else:
1566         return None
1567
1568 def musicxml_voice_to_lily_voice (voice):
1569     tuplet_events = []
1570     modes_found = {}
1571     lyrics = {}
1572     return_value = VoiceData ()
1573     return_value.voicedata = voice
1574     
1575     # First pitch needed for relative mode (if selected in command-line options)
1576     first_pitch = None
1577
1578     # Needed for melismata detection (ignore lyrics on those notes!):
1579     inside_slur = False
1580     is_tied = False
1581     is_chord = False
1582     is_beamed = False
1583     ignore_lyrics = False
1584
1585     current_staff = None
1586     
1587     pending_figured_bass = []
1588
1589     # Make sure that the keys in the dict don't get reordered, since
1590     # we need the correct ordering of the lyrics stanzas! By default,
1591     # a dict will reorder its keys
1592     return_value.lyrics_order = voice.get_lyrics_numbers ()
1593     for k in return_value.lyrics_order:
1594         lyrics[k] = []
1595
1596     voice_builder = LilyPondVoiceBuilder ()
1597     figured_bass_builder = LilyPondVoiceBuilder ()
1598
1599     for n in voice._elements:
1600         if n.get_name () == 'forward':
1601             continue
1602         staff = n.get_maybe_exist_named_child ('staff')
1603         if staff:
1604             staff = staff.get_text ()
1605             if current_staff and staff <> current_staff and not n.get_maybe_exist_named_child ('chord'):
1606                 voice_builder.add_command (musicexp.StaffChange (staff))
1607             current_staff = staff
1608
1609         if isinstance (n, musicxml.Partial) and n.partial > 0:
1610             a = musicxml_partial_to_lily (n.partial)
1611             if a:
1612                 voice_builder.add_partial (a)
1613             continue
1614
1615         if isinstance (n, musicxml.Direction):
1616             for a in musicxml_direction_to_lily (n):
1617                 if a.wait_for_note ():
1618                     voice_builder.add_dynamics (a)
1619                 else:
1620                     voice_builder.add_command (a)
1621             continue
1622
1623         if isinstance (n, musicxml.Harmony):
1624             for a in musicxml_harmony_to_lily (n):
1625                 if a.wait_for_note ():
1626                     voice_builder.add_dynamics (a)
1627                 else:
1628                     voice_builder.add_command (a)
1629             continue
1630         
1631         if isinstance (n, musicxml.FiguredBass):
1632             a = musicxml_figured_bass_to_lily (n)
1633             if a:
1634                 pending_figured_bass.append (a)
1635             continue
1636
1637         is_chord = n.get_maybe_exist_named_child ('chord')
1638         if not is_chord:
1639             try:
1640                 voice_builder.jumpto (n._when)
1641             except NegativeSkip, neg:
1642                 voice_builder.correct_negative_skip (n._when)
1643                 n.message (_ ("Negative skip found: from %s to %s, difference is %s") % (neg.here, neg.dest, neg.dest - neg.here))
1644             
1645         if isinstance (n, musicxml.Attributes):
1646             if n.is_first () and n._measure_position == Rational (0):
1647                 try:
1648                     number = int (n.get_parent ().number)
1649                 except ValueError:
1650                     number = 0
1651                 if number > 0:
1652                     voice_builder.add_bar_check (number)
1653
1654             for a in musicxml_attributes_to_lily (n):
1655                 voice_builder.add_command (a)
1656             continue
1657
1658         if isinstance (n, musicxml.Barline):
1659             barlines = musicxml_barline_to_lily (n)
1660             for a in barlines:
1661                 if isinstance (a, musicexp.BarLine):
1662                     voice_builder.add_barline (a)
1663                 elif isinstance (a, RepeatMarker) or isinstance (a, EndingMarker):
1664                     voice_builder.add_command (a)
1665             continue
1666
1667         if not n.__class__.__name__ == 'Note':
1668             error_message (_ ('unexpected %s; expected %s or %s or %s') % (n, 'Note', 'Attributes', 'Barline'))
1669             continue
1670
1671         rest = n.get_maybe_exist_typed_child (musicxml.Rest)
1672         if (rest
1673             and rest.is_whole_measure ()):
1674
1675             voice_builder.add_multibar_rest (n._duration)
1676             continue
1677
1678         if n.is_first () and n._measure_position == Rational (0):
1679             try: 
1680                 num = int (n.get_parent ().number)
1681             except ValueError:
1682                 num = 0
1683             if num > 0:
1684                 voice_builder.add_bar_check (num)
1685
1686         main_event = musicxml_note_to_lily_main_event (n)
1687         if main_event and not first_pitch:
1688             first_pitch = main_event.pitch
1689         # ignore lyrics for notes inside a slur, tie, chord or beam
1690         ignore_lyrics = inside_slur or is_tied or is_chord or is_beamed
1691
1692         if main_event and hasattr (main_event, 'drum_type') and main_event.drum_type:
1693             modes_found['drummode'] = True
1694
1695         ev_chord = voice_builder.last_event_chord (n._when)
1696         if not ev_chord: 
1697             ev_chord = musicexp.ChordEvent()
1698             voice_builder.add_music (ev_chord, n._duration)
1699
1700         grace = n.get_maybe_exist_typed_child (musicxml.Grace)
1701         if grace:
1702             grace_chord = None
1703             if n.get_maybe_exist_typed_child (musicxml.Chord) and ev_chord.grace_elements:
1704                 grace_chord = ev_chord.grace_elements.get_last_event_chord ()
1705             if not grace_chord:
1706                 grace_chord = musicexp.ChordEvent ()
1707                 ev_chord.append_grace (grace_chord)
1708             if hasattr (grace, 'slash'):
1709                 # TODO: use grace_type = "appoggiatura" for slurred grace notes
1710                 if grace.slash == "yes":
1711                     ev_chord.grace_type = "acciaccatura"
1712             # now that we have inserted the chord into the grace music, insert
1713             # everything into that chord instead of the ev_chord
1714             ev_chord = grace_chord
1715             ev_chord.append (main_event)
1716             ignore_lyrics = True
1717         else:
1718             ev_chord.append (main_event)
1719             # When a note/chord has grace notes (duration==0), the duration of the
1720             # event chord is not yet known, but the event chord was already added
1721             # with duration 0. The following correct this when we hit the real note!
1722             if voice_builder.current_duration () == 0 and n._duration > 0:
1723                 voice_builder.set_duration (n._duration)
1724         
1725         # if we have a figured bass, set its voice builder to the correct position
1726         # and insert the pending figures
1727         if pending_figured_bass:
1728           try:
1729               figured_bass_builder.jumpto (n._when)
1730           except NegativeSkip, neg:
1731               pass
1732           for fb in pending_figured_bass:
1733               figured_bass_builder.add_music (fb, fb.real_duration)
1734           pending_figured_bass = []
1735
1736
1737         notations_children = n.get_typed_children (musicxml.Notations)
1738         tuplet_event = None
1739         span_events = []
1740
1741         # The <notation> element can have the following children (+ means implemented, ~ partially, - not):
1742         # +tied | +slur | +tuplet | glissando | slide | 
1743         #    ornaments | technical | articulations | dynamics |
1744         #    +fermata | arpeggiate | non-arpeggiate | 
1745         #    accidental-mark | other-notation
1746         for notations in notations_children:
1747             for tuplet_event in notations.get_tuplets():
1748                 mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
1749                 frac = (1,1)
1750                 if mod:
1751                     frac = mod.get_fraction ()
1752                 
1753                 tuplet_events.append ((ev_chord, tuplet_event, frac))
1754
1755             slurs = [s for s in notations.get_named_children ('slur')
1756                 if s.get_type () in ('start','stop')]
1757             if slurs:
1758                 if len (slurs) > 1:
1759                     error_message (_ ('cannot have two simultaneous slurs'))
1760                 # record the slur status for the next note in the loop
1761                 if not grace:
1762                     if slurs[0].get_type () == 'start':
1763                         inside_slur = True
1764                     elif slurs[0].get_type () == 'stop':
1765                         inside_slur = False
1766                 lily_ev = musicxml_spanner_to_lily_event (slurs[0])
1767                 ev_chord.append (lily_ev)
1768
1769             if not grace:
1770                 mxl_tie = notations.get_tie ()
1771                 if mxl_tie and mxl_tie.type == 'start':
1772                     ev_chord.append (musicexp.TieEvent ())
1773                     is_tied = True
1774                 else:
1775                     is_tied = False
1776
1777             fermatas = notations.get_named_children ('fermata')
1778             for a in fermatas:
1779                 ev = musicxml_fermata_to_lily_event (a)
1780                 if ev: 
1781                     ev_chord.append (ev)
1782
1783             arpeggiate = notations.get_named_children ('arpeggiate')
1784             for a in arpeggiate:
1785                 ev = musicxml_arpeggiate_to_lily_event (a)
1786                 if ev:
1787                     ev_chord.append (ev)
1788
1789             arpeggiate = notations.get_named_children ('non-arpeggiate')
1790             for a in arpeggiate:
1791                 ev = musicxml_nonarpeggiate_to_lily_event (a)
1792                 if ev:
1793                     ev_chord.append (ev)
1794
1795             glissandos = notations.get_named_children ('glissando')
1796             glissandos += notations.get_named_children ('slide')
1797             for a in glissandos:
1798                 ev = musicxml_spanner_to_lily_event (a)
1799                 if ev:
1800                     ev_chord.append (ev)
1801
1802             # accidental-marks are direct children of <notation>!
1803             for a in notations.get_named_children ('accidental-mark'):
1804                 ev = musicxml_articulation_to_lily_event (a)
1805                 if ev:
1806                     ev_chord.append (ev)
1807
1808             # Articulations can contain the following child elements:
1809             #         accent | strong-accent | staccato | tenuto |
1810             #         detached-legato | staccatissimo | spiccato |
1811             #         scoop | plop | doit | falloff | breath-mark | 
1812             #         caesura | stress | unstress
1813             # Technical can contain the following child elements:
1814             #         up-bow | down-bow | harmonic | open-string |
1815             #         thumb-position | fingering | pluck | double-tongue |
1816             #         triple-tongue | stopped | snap-pizzicato | fret |
1817             #         string | hammer-on | pull-off | bend | tap | heel |
1818             #         toe | fingernails | other-technical
1819             # Ornaments can contain the following child elements:
1820             #         trill-mark | turn | delayed-turn | inverted-turn |
1821             #         shake | wavy-line | mordent | inverted-mordent | 
1822             #         schleifer | tremolo | other-ornament, accidental-mark
1823             ornaments = notations.get_named_children ('ornaments')
1824             ornaments += notations.get_named_children ('articulations')
1825             ornaments += notations.get_named_children ('technical')
1826
1827             for a in ornaments:
1828                 for ch in a.get_all_children ():
1829                     ev = musicxml_articulation_to_lily_event (ch)
1830                     if ev: 
1831                         ev_chord.append (ev)
1832
1833             dynamics = notations.get_named_children ('dynamics')
1834             for a in dynamics:
1835                 for ch in a.get_all_children ():
1836                     ev = musicxml_dynamics_to_lily_event (ch)
1837                     if ev:
1838                         ev_chord.append (ev)
1839
1840
1841         mxl_beams = [b for b in n.get_named_children ('beam')
1842                      if (b.get_type () in ('begin', 'end')
1843                          and b.is_primary ())] 
1844         if mxl_beams and not conversion_settings.ignore_beaming:
1845             beam_ev = musicxml_spanner_to_lily_event (mxl_beams[0])
1846             if beam_ev:
1847                 ev_chord.append (beam_ev)
1848                 if beam_ev.span_direction == -1: # beam and thus melisma starts here
1849                     is_beamed = True
1850                 elif beam_ev.span_direction == 1: # beam and thus melisma ends here
1851                     is_beamed = False
1852             
1853         if tuplet_event:
1854             mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
1855             frac = (1,1)
1856             if mod:
1857                 frac = mod.get_fraction ()
1858                 
1859             tuplet_events.append ((ev_chord, tuplet_event, frac))
1860
1861         # Extract the lyrics
1862         if not rest and not ignore_lyrics:
1863             note_lyrics_processed = []
1864             note_lyrics_elements = n.get_typed_children (musicxml.Lyric)
1865             for l in note_lyrics_elements:
1866                 if l.get_number () < 0:
1867                     for k in lyrics.keys ():
1868                         lyrics[k].append (l.lyric_to_text ())
1869                         note_lyrics_processed.append (k)
1870                 else:
1871                     lyrics[l.number].append(l.lyric_to_text ())
1872                     note_lyrics_processed.append (l.number)
1873             for lnr in lyrics.keys ():
1874                 if not lnr in note_lyrics_processed:
1875                     lyrics[lnr].append ("\skip4")
1876
1877     ## force trailing mm rests to be written out.   
1878     voice_builder.add_music (musicexp.ChordEvent (), Rational (0))
1879     
1880     ly_voice = group_tuplets (voice_builder.elements, tuplet_events)
1881     ly_voice = group_repeats (ly_voice)
1882
1883     seq_music = musicexp.SequentialMusic ()
1884
1885     if 'drummode' in modes_found.keys ():
1886         ## \key <pitch> barfs in drummode.
1887         ly_voice = [e for e in ly_voice
1888                     if not isinstance(e, musicexp.KeySignatureChange)]
1889     
1890     seq_music.elements = ly_voice
1891     for k in lyrics.keys ():
1892         return_value.lyrics_dict[k] = musicexp.Lyrics ()
1893         return_value.lyrics_dict[k].lyrics_syllables = lyrics[k]
1894     
1895     
1896     if len (modes_found) > 1:
1897        error_message (_ ('cannot simultaneously have more than one mode: %s') % modes_found.keys ())
1898        
1899     if options.relative:
1900         v = musicexp.RelativeMusic ()
1901         v.element = seq_music
1902         v.basepitch = first_pitch
1903         seq_music = v
1904
1905     return_value.ly_voice = seq_music
1906     for mode in modes_found.keys ():
1907         v = musicexp.ModeChangingMusicWrapper()
1908         v.element = seq_music
1909         v.mode = mode
1910         return_value.ly_voice = v
1911     
1912     # create \figuremode { figured bass elements }
1913     if figured_bass_builder.elements:
1914         fbass_music = musicexp.SequentialMusic ()
1915         fbass_music.elements = figured_bass_builder.elements
1916         v = musicexp.ModeChangingMusicWrapper()
1917         v.mode = 'figuremode'
1918         v.element = fbass_music
1919         return_value.figured_bass = v
1920     
1921     return return_value
1922
1923 def musicxml_id_to_lily (id):
1924     digits = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five',
1925               'Six', 'Seven', 'Eight', 'Nine', 'Ten']
1926     
1927     for digit in digits:
1928         d = digits.index (digit)
1929         id = re.sub ('%d' % d, digit, id)
1930
1931     id = re.sub  ('[^a-zA-Z]', 'X', id)
1932     return id
1933
1934 def musicxml_pitch_to_lily (mxl_pitch):
1935     p = musicexp.Pitch ()
1936     p.alteration = mxl_pitch.get_alteration ()
1937     p.step = musicxml_step_to_lily (mxl_pitch.get_step ())
1938     p.octave = mxl_pitch.get_octave () - 4
1939     return p
1940
1941 def musicxml_unpitched_to_lily (mxl_unpitched):
1942     p = None
1943     step = mxl_unpitched.get_step ()
1944     if step:
1945         p = musicexp.Pitch ()
1946         p.step = musicxml_step_to_lily (step)
1947     octave = mxl_unpitched.get_octave ()
1948     if octave and p:
1949         p.octave = octave - 4
1950     return p
1951
1952 def musicxml_restdisplay_to_lily (mxl_rest):
1953     p = None
1954     step = mxl_rest.get_step ()
1955     if step:
1956         p = musicexp.Pitch ()
1957         p.step = musicxml_step_to_lily (step)
1958     octave = mxl_rest.get_octave ()
1959     if octave and p:
1960         p.octave = octave - 4
1961     return p
1962
1963 def voices_in_part (part):
1964     """Return a Name -> Voice dictionary for PART"""
1965     part.interpret ()
1966     part.extract_voices ()
1967     voices = part.get_voices ()
1968     part_info = part.get_staff_attributes ()
1969
1970     return (voices, part_info)
1971
1972 def voices_in_part_in_parts (parts):
1973     """return a Part -> Name -> Voice dictionary"""
1974     return dict([(p.id, voices_in_part (p)) for p in parts])
1975
1976
1977 def get_all_voices (parts):
1978     all_voices = voices_in_part_in_parts (parts)
1979
1980     all_ly_voices = {}
1981     all_ly_staffinfo = {}
1982     for p, (name_voice, staff_info) in all_voices.items ():
1983
1984         part_ly_voices = {}
1985         for n, v in name_voice.items ():
1986             progress (_ ("Converting to LilyPond expressions..."))
1987             # musicxml_voice_to_lily_voice returns (lily_voice, {nr->lyrics, nr->lyrics})
1988             part_ly_voices[n] = musicxml_voice_to_lily_voice (v)
1989
1990         all_ly_voices[p] = part_ly_voices
1991         all_ly_staffinfo[p] = staff_info
1992
1993     return (all_ly_voices, all_ly_staffinfo)
1994
1995
1996 def option_parser ():
1997     p = ly.get_option_parser (usage = _ ("musicxml2ly [options] FILE.xml"),
1998                              description = _ ("Convert MusicXML from FILE.xml to LilyPond input. If the given filename is -, musicxml2ly reads from the command line.") + "\n",
1999                              add_help_option=False)
2000
2001     p.add_option("-h", "--help",
2002                  action="help",
2003                  help=_ ("show this help and exit"))
2004
2005     p.version = ('''%prog (LilyPond) @TOPLEVEL_VERSION@\n\n'''
2006                                       +
2007 _ ("""This program is free software.  It is covered by the GNU General Public
2008 License and you are welcome to change it and/or distribute copies of it
2009 under certain conditions.  Invoke as `%s --warranty' for more
2010 information.""") % 'lilypond'
2011 + """
2012 Copyright (c) 2005--2008 by
2013     Han-Wen Nienhuys <hanwen@xs4all.nl>,
2014     Jan Nieuwenhuizen <janneke@gnu.org> and
2015     Reinhold Kainhofer <reinhold@kainhofer.com>
2016 """)
2017     p.add_option("--version",
2018                  action="version",
2019                  help=_ ("show version number and exit"))
2020
2021     p.add_option ('-v', '--verbose',
2022                   action = "store_true",
2023                   dest = 'verbose',
2024                   help = _ ("be verbose"))
2025
2026     p.add_option ('', '--lxml',
2027                   action = "store_true",
2028                   default = False,
2029                   dest = "use_lxml",
2030                   help = _ ("use lxml.etree; uses less memory and cpu time"))
2031
2032     p.add_option ('-z', '--compressed',
2033                   action = "store_true",
2034                   dest = 'compressed',
2035                   default = False,
2036                   help = _ ("input file is a zip-compressed MusicXML file"))
2037
2038     p.add_option ('-r', '--relative',
2039                   action = "store_true",
2040                   default = True,
2041                   dest = "relative",
2042                   help = _ ("convert pitches in relative mode (default)"))
2043
2044     p.add_option ('-a', '--absolute',
2045                   action = "store_false",
2046                   dest = "relative",
2047                   help = _ ("convert pitches in absolute mode"))
2048
2049     p.add_option ('-l', '--language',
2050                   metavar = _ ("LANG"),
2051                   action = "store",
2052                   help = _ ("use a different language file 'LANG.ly' and corresponding pitch names, e.g. 'deutsch' for deutsch.ly"))
2053
2054     p.add_option ('--nd', '--no-articulation-directions', 
2055                   action = "store_false",
2056                   default = True,
2057                   dest = "convert_directions",
2058                   help = _ ("do not convert directions (^, _ or -) for articulations, dynamics, etc."))
2059
2060     p.add_option ('--no-beaming', 
2061                   action = "store_false",
2062                   default = True,
2063                   dest = "convert_beaming",
2064                   help = _ ("do not convert beaming information, use lilypond's automatic beaming instead"))
2065
2066     p.add_option ('-o', '--output',
2067                   metavar = _ ("FILE"),
2068                   action = "store",
2069                   default = None,
2070                   type = 'string',
2071                   dest = 'output_name',
2072                   help = _ ("set output filename to FILE, stdout if -"))
2073     p.add_option_group (ly.display_encode (_ ('Bugs')),
2074                         description = (_ ("Report bugs via")
2075                                      + ''' http://post.gmane.org/post.php'''
2076                                      '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
2077     return p
2078
2079 def music_xml_voice_name_to_lily_name (part_id, name):
2080     str = "Part%sVoice%s" % (part_id, name)
2081     return musicxml_id_to_lily (str) 
2082
2083 def music_xml_lyrics_name_to_lily_name (part_id, name, lyricsnr):
2084     str = "Part%sVoice%sLyrics%s" % (part_id, name, lyricsnr)
2085     return musicxml_id_to_lily (str) 
2086
2087 def music_xml_figuredbass_name_to_lily_name (part_id, voicename):
2088     str = "Part%sVoice%sFiguredBass" % (part_id, voicename)
2089     return musicxml_id_to_lily (str) 
2090
2091 def print_voice_definitions (printer, part_list, voices):
2092     for part in part_list:
2093         part_id = part.id
2094         nv_dict = voices.get (part_id, {})
2095         for (name, voice) in nv_dict.items ():
2096             k = music_xml_voice_name_to_lily_name (part_id, name)
2097             printer.dump ('%s = ' % k)
2098             voice.ly_voice.print_ly (printer)
2099             printer.newline()
2100             for l in voice.lyrics_order:
2101                 lname = music_xml_lyrics_name_to_lily_name (part_id, name, l)
2102                 printer.dump ('%s = ' % lname )
2103                 voice.lyrics_dict[l].print_ly (printer)
2104                 printer.newline()
2105             if voice.figured_bass:
2106                 fbname = music_xml_figuredbass_name_to_lily_name (part_id, name)
2107                 printer.dump ('%s = ' % fbname )
2108                 voice.figured_bass.print_ly (printer)
2109                 printer.newline()
2110
2111
2112 def uniq_list (l):
2113     return dict ([(elt,1) for elt in l]).keys ()
2114
2115 # format the information about the staff in the form 
2116 #     [staffid,
2117 #         [
2118 #            [voiceid1, [lyricsid11, lyricsid12,...], figuredbassid1],
2119 #            [voiceid2, [lyricsid21, lyricsid22,...], figuredbassid2],
2120 #            ...
2121 #         ]
2122 #     ]
2123 # raw_voices is of the form [(voicename, lyricsids, havefiguredbass)*]
2124 def format_staff_info (part_id, staff_id, raw_voices):
2125     voices = []
2126     for (v, lyricsids, figured_bass) in raw_voices:
2127         voice_name = music_xml_voice_name_to_lily_name (part_id, v)
2128         voice_lyrics = [music_xml_lyrics_name_to_lily_name (part_id, v, l)
2129                    for l in lyricsids]
2130         figured_bass_name = ''
2131         if figured_bass:
2132             figured_bass_name = music_xml_figuredbass_name_to_lily_name (part_id, v)
2133         voices.append ([voice_name, voice_lyrics, figured_bass_name])
2134     return [staff_id, voices]
2135
2136 def update_score_setup (score_structure, part_list, voices):
2137
2138     for part_definition in part_list:
2139         part_id = part_definition.id
2140         nv_dict = voices.get (part_id)
2141         if not nv_dict:
2142             error_message (_ ('unknown part in part-list: %s') % part_id)
2143             continue
2144
2145         staves = reduce (lambda x,y: x+ y,
2146                 [voice.voicedata._staves.keys ()
2147                  for voice in nv_dict.values ()],
2148                 [])
2149         staves_info = []
2150         if len (staves) > 1:
2151             staves_info = []
2152             staves = uniq_list (staves)
2153             staves.sort ()
2154             for s in staves:
2155                 thisstaff_raw_voices = [(voice_name, voice.lyrics_order, voice.figured_bass) 
2156                     for (voice_name, voice) in nv_dict.items ()
2157                     if voice.voicedata._start_staff == s]
2158                 staves_info.append (format_staff_info (part_id, s, thisstaff_raw_voices))
2159         else:
2160             thisstaff_raw_voices = [(voice_name, voice.lyrics_order, voice.figured_bass) 
2161                 for (voice_name, voice) in nv_dict.items ()]
2162             staves_info.append (format_staff_info (part_id, None, thisstaff_raw_voices))
2163         score_structure.set_part_information (part_id, staves_info)
2164
2165 # Set global values in the \layout block, like auto-beaming etc.
2166 def update_layout_information ():
2167     if not conversion_settings.ignore_beaming and layout_information:
2168         layout_information.set_context_item ('Score', 'autoBeaming = ##f')
2169
2170 def print_ly_preamble (printer, filename):
2171     printer.dump_version ()
2172     printer.print_verbatim ('%% automatically converted from %s\n' % filename)
2173
2174 def print_ly_additional_definitions (printer, filename):
2175     if needed_additional_definitions:
2176         printer.newline ()
2177         printer.print_verbatim ('%% additional definitions required by the score:')
2178         printer.newline ()
2179     for a in set(needed_additional_definitions):
2180         printer.print_verbatim (additional_definitions.get (a, ''))
2181         printer.newline ()
2182     printer.newline ()
2183
2184 # Read in the tree from the given I/O object (either file or string) and 
2185 # demarshall it using the classes from the musicxml.py file
2186 def read_xml (io_object, use_lxml):
2187     if use_lxml:
2188         import lxml.etree
2189         tree = lxml.etree.parse (io_object)
2190         mxl_tree = musicxml.lxml_demarshal_node (tree.getroot ())
2191         return mxl_tree
2192     else:
2193         from xml.dom import minidom, Node
2194         doc = minidom.parse(io_object)
2195         node = doc.documentElement
2196         return musicxml.minidom_demarshal_node (node)
2197     return None
2198
2199
2200 def read_musicxml (filename, compressed, use_lxml):
2201     raw_string = None
2202     if compressed:
2203         if filename == "-":
2204              progress (_ ("Input is compressed, extracting raw MusicXML data from stdin") )
2205              z = zipfile.ZipFile (sys.stdin)
2206         else:
2207             progress (_ ("Input file %s is compressed, extracting raw MusicXML data") % filename)
2208             z = zipfile.ZipFile (filename, "r")
2209         container_xml = z.read ("META-INF/container.xml")
2210         if not container_xml:
2211             return None
2212         container = read_xml (StringIO.StringIO (container_xml), use_lxml)
2213         if not container:
2214             return None
2215         rootfiles = container.get_maybe_exist_named_child ('rootfiles')
2216         if not rootfiles:
2217             return None
2218         rootfile_list = rootfiles.get_named_children ('rootfile')
2219         mxml_file = None
2220         if len (rootfile_list) > 0:
2221             mxml_file = getattr (rootfile_list[0], 'full-path', None)
2222         if mxml_file:
2223             raw_string = z.read (mxml_file)
2224
2225     if raw_string:
2226         io_object = StringIO.StringIO (raw_string)
2227     elif filename == "-":
2228         io_object = sys.stdin
2229     else:
2230         io_object = filename
2231
2232     return read_xml (io_object, use_lxml)
2233
2234
2235 def convert (filename, options):
2236     if filename == "-":
2237         progress (_ ("Reading MusicXML from Standard input ...") )
2238     else:
2239         progress (_ ("Reading MusicXML from %s ...") % filename)
2240
2241     tree = read_musicxml (filename, options.compressed, options.use_lxml)
2242     score_information = extract_score_information (tree)
2243     paper_information = extract_paper_information (tree)
2244
2245     parts = tree.get_typed_children (musicxml.Part)
2246     (voices, staff_info) = get_all_voices (parts)
2247
2248     score_structure = None
2249     mxl_pl = tree.get_maybe_exist_typed_child (musicxml.Part_list)
2250     if mxl_pl:
2251         score_structure = extract_score_structure (mxl_pl, staff_info)
2252         part_list = mxl_pl.get_named_children ("score-part")
2253
2254     # score information is contained in the <work>, <identification> or <movement-title> tags
2255     update_score_setup (score_structure, part_list, voices)
2256     # After the conversion, update the list of settings for the \layout block
2257     update_layout_information ()
2258
2259     if not options.output_name:
2260         options.output_name = os.path.basename (filename) 
2261         options.output_name = os.path.splitext (options.output_name)[0]
2262     elif re.match (".*\.ly", options.output_name):
2263         options.output_name = os.path.splitext (options.output_name)[0]
2264
2265
2266     #defs_ly_name = options.output_name + '-defs.ly'
2267     if (options.output_name == "-"):
2268       output_ly_name = 'Standard output'
2269     else:
2270       output_ly_name = options.output_name + '.ly'
2271
2272     progress (_ ("Output to `%s'") % output_ly_name)
2273     printer = musicexp.Output_printer()
2274     #progress (_ ("Output to `%s'") % defs_ly_name)
2275     if (options.output_name == "-"):
2276       printer.set_file (codecs.getwriter ("utf-8")(sys.stdout))
2277     else:
2278       printer.set_file (codecs.open (output_ly_name, 'wb', encoding='utf-8'))
2279     print_ly_preamble (printer, filename)
2280     print_ly_additional_definitions (printer, filename)
2281     if score_information:
2282         score_information.print_ly (printer)
2283     if paper_information:
2284         paper_information.print_ly (printer)
2285     if layout_information:
2286         layout_information.print_ly (printer)
2287     print_voice_definitions (printer, part_list, voices)
2288     
2289     printer.newline ()
2290     printer.dump ("% The score definition")
2291     printer.newline ()
2292     score_structure.print_ly (printer)
2293     printer.newline ()
2294
2295     return voices
2296
2297 def get_existing_filename_with_extension (filename, ext):
2298     if os.path.exists (filename):
2299         return filename
2300     newfilename = filename + "." + ext
2301     if os.path.exists (newfilename):
2302         return newfilename;
2303     newfilename = filename + ext
2304     if os.path.exists (newfilename):
2305         return newfilename;
2306     return ''
2307
2308 def main ():
2309     opt_parser = option_parser()
2310
2311     global options
2312     (options, args) = opt_parser.parse_args ()
2313     if not args:
2314         opt_parser.print_usage()
2315         sys.exit (2)
2316
2317     if options.language:
2318         musicexp.set_pitch_language (options.language)
2319         needed_additional_definitions.append (options.language)
2320         additional_definitions[options.language] = "\\include \"%s.ly\"\n" % options.language
2321     conversion_settings.ignore_beaming = not options.convert_beaming
2322
2323     # Allow the user to leave out the .xml or xml on the filename
2324     if args[0]=="-": # Read from stdin
2325         filename="-"
2326     else:
2327         filename = get_existing_filename_with_extension (args[0], "xml")
2328         if not filename:
2329             filename = get_existing_filename_with_extension (args[0], "mxl")
2330             options.compressed = True
2331     if filename and (filename == "-" or os.path.exists (filename)):
2332         voices = convert (filename, options)
2333     else:
2334         progress (_ ("Unable to find input file %s") % args[0])
2335
2336 if __name__ == '__main__':
2337     main()