8 from gettext import gettext as _
19 from rational import Rational
23 sys.stderr.write (str + '\n')
26 def error_message (str):
27 sys.stderr.write (str + '\n')
30 # score information is contained in the <work>, <identification> or <movement-title> tags
31 # extract those into a hash, indexed by proper lilypond header attributes
32 def extract_score_information (tree):
33 header = musicexp.Header ()
34 def set_if_exists (field, value):
36 header.set_field (field, musicxml.escape_ly_output_string (value))
38 work = tree.get_maybe_exist_named_child ('work')
40 set_if_exists ('title', work.get_work_title ())
41 set_if_exists ('worknumber', work.get_work_number ())
42 set_if_exists ('opus', work.get_opus ())
44 movement_title = tree.get_maybe_exist_named_child ('movement-title')
46 set_if_exists ('title', movement_title.get_text ())
48 identifications = tree.get_named_children ('identification')
49 for ids in identifications:
50 set_if_exists ('copyright', ids.get_rights ())
51 set_if_exists ('composer', ids.get_composer ())
52 set_if_exists ('arranger', ids.get_arranger ())
53 set_if_exists ('editor', ids.get_editor ())
54 set_if_exists ('poet', ids.get_poet ())
56 set_if_exists ('tagline', ids.get_encoding_software ())
57 set_if_exists ('encodingsoftware', ids.get_encoding_software ())
58 set_if_exists ('encodingdate', ids.get_encoding_date ())
59 set_if_exists ('encoder', ids.get_encoding_person ())
60 set_if_exists ('encodingdescription', ids.get_encoding_description ())
66 def musicxml_duration_to_lily (mxl_note):
67 d = musicexp.Duration ()
68 # if the note has no Type child, then that method spits out a warning and
69 # returns 0, i.e. a whole note
70 d.duration_log = mxl_note.get_duration_log ()
72 d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
73 # Grace notes by specification have duration 0, so no time modification
74 # factor is possible. It even messes up the output with *0/1
75 if not mxl_note.get_maybe_exist_typed_child (musicxml.Grace):
76 d.factor = mxl_note._duration / d.get_length ()
80 def group_tuplets (music_list, events):
83 """Collect Musics from
84 MUSIC_LIST demarcated by EVENTS_LIST in TimeScaledMusic objects.
91 for (ev_chord, tuplet_elt, fraction) in events:
92 while (j < len (music_list)):
93 if music_list[j] == ev_chord:
96 if tuplet_elt.type == 'start':
97 indices.append ((j, None, fraction))
98 elif tuplet_elt.type == 'stop':
99 indices[-1] = (indices[-1][0], j, indices[-1][2])
103 for (i1, i2, frac) in indices:
107 new_list.extend (music_list[last:i1])
108 seq = musicexp.SequentialMusic ()
110 seq.elements = music_list[i1:last]
112 tsm = musicexp.TimeScaledMusic ()
115 tsm.numerator = frac[0]
116 tsm.denominator = frac[1]
118 new_list.append (tsm)
120 new_list.extend (music_list[last:])
124 def musicxml_clef_to_lily (attributes):
125 change = musicexp.ClefChange ()
126 (change.type, change.position, change.octave) = attributes.get_clef_information ()
129 def musicxml_time_to_lily (attributes):
130 (beats, type) = attributes.get_time_signature ()
132 change = musicexp.TimeSignatureChange()
133 change.fraction = (beats, type)
137 def musicxml_key_to_lily (attributes):
138 start_pitch = musicexp.Pitch ()
139 (fifths, mode) = attributes.get_key_signature ()
146 start_pitch.alteration = a
148 error_message ('unknown mode %s' % mode)
150 fifth = musicexp.Pitch()
157 for x in range (fifths):
158 start_pitch = start_pitch.transposed (fifth)
160 start_pitch.octave = 0
162 change = musicexp.KeySignatureChange()
164 change.tonic = start_pitch
167 def musicxml_attributes_to_lily (attrs):
170 'clef': musicxml_clef_to_lily,
171 'time': musicxml_time_to_lily,
172 'key': musicxml_key_to_lily
174 for (k, func) in attr_dispatch.items ():
175 children = attrs.get_named_children (k)
177 ## ugh: you get clefs spread over staves for piano
179 elts.append (func (attrs))
183 spanner_event_dict = {
184 'slur' : musicexp.SlurEvent,
185 'beam' : musicexp.BeamEvent,
186 'glissando' : musicexp.GlissandoEvent,
187 'pedal' : musicexp.PedalEvent,
188 'wavy-line' : musicexp.TrillSpanEvent,
189 'octave-shift' : musicexp.OctaveShiftEvent,
190 'wedge' : musicexp.HairpinEvent
192 spanner_type_dict = {
205 def musicxml_spanner_to_lily_event (mxl_event):
208 name = mxl_event.get_name()
209 func = spanner_event_dict.get (name)
213 error_message ('unknown span event %s' % mxl_event)
216 type = mxl_event.get_type ()
217 span_direction = spanner_type_dict.get (type)
218 # really check for None, because some types will be translated to 0, which
219 # would otherwise also lead to the unknown span warning
220 if span_direction != None:
221 ev.span_direction = span_direction
223 error_message ('unknown span type %s for %s' % (type, name))
225 ev.set_span_type (type)
226 ev.line_type = getattr (mxl_event, 'line-type', 'solid')
228 # assign the size, which is used for octave-shift, etc.
229 ev.size = mxl_event.get_size ()
233 def musicxml_direction_to_indicator (direction):
234 return { "above": 1, "upright": 1, "below": -1, "downright": -1 }.get (direction, '')
236 def musicxml_fermata_to_lily_event (mxl_event):
237 ev = musicexp.ArticulationEvent ()
239 if hasattr (mxl_event, 'type'):
240 dir = musicxml_direction_to_indicator (mxl_event.type)
242 ev.force_direction = dir
245 def musicxml_tremolo_to_lily_event (mxl_event):
246 if mxl_event.get_name () != "tremolo":
248 ev = musicexp.TremoloEvent ()
249 ev.bars = mxl_event.get_text ()
252 def musicxml_bend_to_lily_event (mxl_event):
253 if mxl_event.get_name () != "bend":
255 ev = musicexp.BendEvent ()
256 ev.alter = mxl_event.bend_alter ()
260 short_articulations_dict = {
264 "staccatissimo": "|",
266 "strong-accent": "^",
267 #"portato": "_", # does not exist in MusicXML
268 #"fingering": "", # fingering is special cased, as get_text() will be the event's name
270 # TODO: Some translations are missing!
271 articulations_dict = {
273 "trill-mark": "trill",
275 #"delayed-turn": "?",
276 "inverted-turn": "reverseturn",
279 "mordent": "mordent",
280 "inverted-mordent": "prall",
284 "down-bow": "downbow",
285 "harmonic": "flageolet",
287 #"thumb-position": "",
289 #"double-tongue": "",
290 #"triple-tongue": "",
291 #"snap-pizzicato": "",
296 #"bend": "bendAfter #%s", # bend is special-cased, as we need to process the bend-alter subelement!
302 #"detached-legato": "",
308 "breath-mark": "breathe",
309 #"caesura": "caesura",
313 articulation_spanners = [ "wavy-line" ]
315 def musicxml_articulation_to_lily_event (mxl_event):
316 # wavy-line elements are treated as trill spanners, not as articulation ornaments
317 if mxl_event.get_name () in articulation_spanners:
318 return musicxml_spanner_to_lily_event (mxl_event)
320 # special case, because of the bend-alter subelement
321 if mxl_event.get_name() == "bend":
322 return musicxml_bend_to_lily_event (mxl_event)
324 # If we can write a shorthand, use them!
325 if mxl_event.get_name() == "fingering":
326 ev = musicexp.ShortArticulationEvent ()
327 tp = mxl_event.get_text()
328 # In all other cases, use the dicts to translate the xml tag name to a proper lilypond command
329 elif short_articulations_dict.get (mxl_event.get_name ()):
330 ev = musicexp.ShortArticulationEvent ()
331 tp = short_articulations_dict.get (mxl_event.get_name ())
333 ev = musicexp.ArticulationEvent ()
334 tp = articulations_dict.get (mxl_event.get_name ())
341 # Some articulations use the type attribute, other the placement...
343 if hasattr (mxl_event, 'type'):
344 dir = musicxml_direction_to_indicator (mxl_event.type)
345 if hasattr (mxl_event, 'placement'):
346 dir = musicxml_direction_to_indicator (mxl_event.placement)
347 # \breathe cannot have any direction modifier (^, _, -)!
348 if dir and tp != "breathe":
349 ev.force_direction = dir
353 def musicxml_dynamics_to_lily_event (dynentry):
354 dynamics_available = ( "p", "pp", "ppp", "pppp", "ppppp", "pppppp",
355 "f", "ff", "fff", "ffff", "fffff", "ffffff",
356 "mp", "mf", "sf", "sfp", "sfpp", "fp",
357 "rf", "rfz", "sfz", "sffz", "fz" )
358 if not dynentry.get_name() in dynamics_available:
360 event = musicexp.DynamicsEvent ()
361 event.type = dynentry.get_name ()
365 direction_spanners = [ 'octave-shift', 'pedal', 'wedge' ]
367 def musicxml_direction_to_lily (n):
368 # TODO: Handle the <staff> element!
370 dirtype_children = []
371 for dt in n.get_typed_children (musicxml.DirType):
372 dirtype_children += dt.get_all_children ()
374 for entry in dirtype_children:
375 if entry.get_name () == "dynamics":
376 for dynentry in entry.get_all_children ():
377 ev = musicxml_dynamics_to_lily_event (dynentry)
381 # octave shifts. pedal marks, hairpins etc. are spanners:
382 if entry.get_name() in direction_spanners:
383 event = musicxml_spanner_to_lily_event (entry)
390 instrument_drumtype_dict = {
391 'Acoustic Snare Drum': 'acousticsnare',
392 'Side Stick': 'sidestick',
393 'Open Triangle': 'opentriangle',
394 'Mute Triangle': 'mutetriangle',
395 'Tambourine': 'tambourine'
398 def musicxml_note_to_lily_main_event (n):
402 mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
405 pitch = musicxml_pitch_to_lily (mxl_pitch)
406 event = musicexp.NoteEvent()
409 acc = n.get_maybe_exist_named_child ('accidental')
411 # let's not force accs everywhere.
412 event.cautionary = acc.editorial
414 elif n.get_maybe_exist_typed_child (musicxml.Rest):
415 # rests can have display-octave and display-step, which are
416 # treated like an ordinary note pitch
417 rest = n.get_maybe_exist_typed_child (musicxml.Rest)
418 event = musicexp.RestEvent()
419 pitch = musicxml_restdisplay_to_lily (rest)
421 elif n.instrument_name:
422 event = musicexp.NoteEvent ()
423 drum_type = instrument_drumtype_dict.get (n.instrument_name)
425 event.drum_type = drum_type
427 n.message ("drum %s type unknow, please add to instrument_drumtype_dict" % n.instrument_name)
428 event.drum_type = 'acousticsnare'
431 n.message ("cannot find suitable event")
433 event.duration = musicxml_duration_to_lily (n)
439 def __init__ (self, here, dest):
443 class LilyPondVoiceBuilder:
446 self.pending_dynamics = []
447 self.end_moment = Rational (0)
448 self.begin_moment = Rational (0)
449 self.pending_multibar = Rational (0)
451 def _insert_multibar (self):
452 r = musicexp.MultiMeasureRest ()
453 r.duration = musicexp.Duration()
454 r.duration.duration_log = 0
455 r.duration.factor = self.pending_multibar
456 self.elements.append (r)
457 self.begin_moment = self.end_moment
458 self.end_moment = self.begin_moment + self.pending_multibar
459 self.pending_multibar = Rational (0)
461 def add_multibar_rest (self, duration):
462 self.pending_multibar += duration
464 def set_duration (self, duration):
465 self.end_moment = self.begin_moment + duration
466 def current_duration (self):
467 return self.end_moment - self.begin_moment
469 def add_music (self, music, duration):
470 assert isinstance (music, musicexp.Music)
471 if self.pending_multibar > Rational (0):
472 self._insert_multibar ()
474 self.elements.append (music)
475 self.begin_moment = self.end_moment
476 self.set_duration (duration)
478 # Insert all pending dynamics right after the note/rest:
479 if duration > Rational (0):
480 for d in self.pending_dynamics:
481 self.elements.append (d)
482 self.pending_dynamics = []
484 # Insert some music command that does not affect the position in the measure
485 def add_command (self, command):
486 assert isinstance (command, musicexp.Music)
487 if self.pending_multibar > Rational (0):
488 self._insert_multibar ()
489 self.elements.append (command)
491 def add_dynamics (self, dynamic):
492 # store the dynamic item(s) until we encounter the next note/rest:
493 self.pending_dynamics.append (dynamic)
495 def add_bar_check (self, number):
496 b = musicexp.BarCheck ()
497 b.bar_number = number
498 self.add_music (b, Rational (0))
500 def jumpto (self, moment):
501 current_end = self.end_moment + self.pending_multibar
502 diff = moment - current_end
504 if diff < Rational (0):
505 error_message ('Negative skip %s' % diff)
508 if diff > Rational (0):
509 skip = musicexp.SkipEvent()
510 skip.duration.duration_log = 0
511 skip.duration.factor = diff
513 evc = musicexp.EventChord ()
514 evc.elements.append (skip)
515 self.add_music (evc, diff)
517 def last_event_chord (self, starting_at):
521 # if the position matches, find the last EventChord, do not cross a bar line!
522 at = len( self.elements ) - 1
524 not isinstance (self.elements[at], musicexp.EventChord) and
525 not isinstance (self.elements[at], musicexp.BarCheck)):
530 and isinstance (self.elements[at], musicexp.EventChord)
531 and self.begin_moment == starting_at):
532 value = self.elements[at]
534 self.jumpto (starting_at)
538 def correct_negative_skip (self, goto):
539 self.end_moment = goto
540 self.begin_moment = goto
541 evc = musicexp.EventChord ()
542 self.elements.append (evc)
544 def musicxml_voice_to_lily_voice (voice):
549 for k in voice.get_lyrics_numbers ():
552 voice_builder = LilyPondVoiceBuilder()
554 for n in voice._elements:
555 if n.get_name () == 'forward':
558 if isinstance (n, musicxml.Direction):
559 for a in musicxml_direction_to_lily (n):
560 if a.wait_for_note ():
561 voice_builder.add_dynamics (a)
563 voice_builder.add_command (a)
566 if not n.get_maybe_exist_named_child ('chord'):
568 voice_builder.jumpto (n._when)
569 except NegativeSkip, neg:
570 voice_builder.correct_negative_skip (n._when)
571 n.message ("Negative skip? from %s to %s, diff %s" % (neg.here, neg.dest, neg.dest - neg.here))
573 if isinstance (n, musicxml.Attributes):
574 if n.is_first () and n._measure_position == Rational (0):
576 number = int (n.get_parent ().number)
580 voice_builder.add_bar_check (number)
581 for a in musicxml_attributes_to_lily (n):
582 voice_builder.add_music (a, Rational (0))
585 if not n.__class__.__name__ == 'Note':
586 error_message ('not a Note or Attributes? %s' % n)
589 rest = n.get_maybe_exist_typed_child (musicxml.Rest)
591 and rest.is_whole_measure ()):
593 voice_builder.add_multibar_rest (n._duration)
596 if n.is_first () and n._measure_position == Rational (0):
598 num = int (n.get_parent ().number)
601 voice_builder.add_bar_check (num)
603 main_event = musicxml_note_to_lily_main_event (n)
605 if hasattr (main_event, 'drum_type') and main_event.drum_type:
606 modes_found['drummode'] = True
609 ev_chord = voice_builder.last_event_chord (n._when)
611 ev_chord = musicexp.EventChord()
612 voice_builder.add_music (ev_chord, n._duration)
613 # When a note/chord has grace notes (duration==0), the duration of the
614 # event chord is not yet known, but the event chord was already added
615 # with duration 0. The following correct this when we hit the real note!
616 if voice_builder.current_duration () == 0 and n._duration > 0:
617 voice_builder.set_duration (n._duration)
618 if n.get_maybe_exist_typed_child (musicxml.Grace):
619 ev_chord.append_grace (main_event)
621 ev_chord.append (main_event)
623 notations = n.get_maybe_exist_typed_child (musicxml.Notations)
627 # The <notation> element can have the following children (+ means implemented, ~ partially, - not):
628 # +tied | +slur | +tuplet | glissando | slide |
629 # ornaments | technical | articulations | dynamics |
630 # +fermata | arpeggiate | non-arpeggiate |
631 # accidental-mark | other-notation
633 if notations.get_tuplet():
634 tuplet_event = notations.get_tuplet()
635 mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
638 frac = mod.get_fraction ()
640 tuplet_events.append ((ev_chord, tuplet_event, frac))
642 slurs = [s for s in notations.get_named_children ('slur')
643 if s.get_type () in ('start','stop')]
646 error_message ('more than 1 slur?')
648 lily_ev = musicxml_spanner_to_lily_event (slurs[0])
649 ev_chord.append (lily_ev)
651 mxl_tie = notations.get_tie ()
652 if mxl_tie and mxl_tie.type == 'start':
653 ev_chord.append (musicexp.TieEvent ())
655 fermatas = notations.get_named_children ('fermata')
657 ev = musicxml_fermata_to_lily_event (a)
661 arpeggiate = notations.get_named_children ('arpeggiate')
663 ev_chord.append (musicexp.ArpeggioEvent ())
665 glissandos = notations.get_named_children ('glissando')
667 ev = musicxml_spanner_to_lily_event (a)
671 # Articulations can contain the following child elements:
672 # accent | strong-accent | staccato | tenuto |
673 # detached-legato | staccatissimo | spiccato |
674 # scoop | plop | doit | falloff | breath-mark |
675 # caesura | stress | unstress
676 # Technical can contain the following child elements:
677 # up-bow | down-bow | harmonic | open-string |
678 # thumb-position | fingering | pluck | double-tongue |
679 # triple-tongue | stopped | snap-pizzicato | fret |
680 # string | hammer-on | pull-off | bend | tap | heel |
681 # toe | fingernails | other-technical
682 # Ornaments can contain the following child elements:
683 # trill-mark | turn | delayed-turn | inverted-turn |
684 # shake | wavy-line | mordent | inverted-mordent |
685 # schleifer | tremolo | other-ornament, accidental-mark
686 ornaments = notations.get_named_children ('ornaments')
688 for ch in a.get_named_children ('tremolo'):
689 ev = musicxml_tremolo_to_lily_event (ch)
693 ornaments += notations.get_named_children ('articulations')
694 ornaments += notations.get_named_children ('technical')
697 for ch in a.get_all_children ():
698 ev = musicxml_articulation_to_lily_event (ch)
702 dynamics = notations.get_named_children ('dynamics')
704 for ch in a.get_all_children ():
705 ev = musicxml_dynamics_to_lily_event (ch)
710 note_lyrics_processed = []
711 note_lyrics_elements = n.get_typed_children (musicxml.Lyric)
712 for l in note_lyrics_elements:
713 if l.get_number () < 0:
714 for k in lyrics.keys ():
715 lyrics[k].append (l.lyric_to_text ())
716 note_lyrics_processed.append (k)
718 lyrics[l.number].append(l.lyric_to_text ())
719 note_lyrics_processed.append (l.number)
720 for lnr in lyrics.keys ():
721 if not lnr in note_lyrics_processed:
722 lyrics[lnr].append ("\skip4")
725 mxl_beams = [b for b in n.get_named_children ('beam')
726 if (b.get_type () in ('begin', 'end')
727 and b.is_primary ())]
729 beam_ev = musicxml_spanner_to_lily_event (mxl_beams[0])
731 ev_chord.append (beam_ev)
734 mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
737 frac = mod.get_fraction ()
739 tuplet_events.append ((ev_chord, tuplet_event, frac))
741 ## force trailing mm rests to be written out.
742 voice_builder.add_music (musicexp.EventChord (), Rational (0))
744 ly_voice = group_tuplets (voice_builder.elements, tuplet_events)
746 seq_music = musicexp.SequentialMusic ()
748 if 'drummode' in modes_found.keys ():
749 ## \key <pitch> barfs in drummode.
750 ly_voice = [e for e in ly_voice
751 if not isinstance(e, musicexp.KeySignatureChange)]
753 seq_music.elements = ly_voice
755 for k in lyrics.keys ():
756 lyrics_dict[k] = musicexp.Lyrics ()
757 lyrics_dict[k].lyrics_syllables = lyrics[k]
760 if len (modes_found) > 1:
761 error_message ('Too many modes found %s' % modes_found.keys ())
763 return_value = seq_music
764 for mode in modes_found.keys ():
765 v = musicexp.ModeChangingMusicWrapper()
766 v.element = return_value
770 return (return_value, lyrics_dict)
773 def musicxml_id_to_lily (id):
774 digits = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five',
775 'Six', 'Seven', 'Eight', 'Nine', 'Ten']
778 d = digits.index (digit)
779 id = re.sub ('%d' % d, digit, id)
781 id = re.sub ('[^a-zA-Z]', 'X', id)
785 def musicxml_pitch_to_lily (mxl_pitch):
787 p.alteration = mxl_pitch.get_alteration ()
788 p.step = (ord (mxl_pitch.get_step ()) - ord ('A') + 7 - 2) % 7
789 p.octave = mxl_pitch.get_octave () - 4
792 def musicxml_restdisplay_to_lily (mxl_rest):
794 step = mxl_rest.get_step ()
797 p.step = (ord (step) - ord ('A') + 7 - 2) % 7
798 octave = mxl_rest.get_octave ()
800 p.octave = octave - 4
803 def voices_in_part (part):
804 """Return a Name -> Voice dictionary for PART"""
806 part.extract_voices ()
807 voice_dict = part.get_voices ()
811 def voices_in_part_in_parts (parts):
812 """return a Part -> Name -> Voice dictionary"""
813 return dict([(p, voices_in_part (p)) for p in parts])
816 def get_all_voices (parts):
817 all_voices = voices_in_part_in_parts (parts)
820 for p, name_voice in all_voices.items ():
823 for n, v in name_voice.items ():
824 progress ("Converting to LilyPond expressions...")
825 # musicxml_voice_to_lily_voice returns (lily_voice, {nr->lyrics, nr->lyrics})
826 part_ly_voices[n] = (musicxml_voice_to_lily_voice (v), v)
828 all_ly_voices[p] = part_ly_voices
833 def option_parser ():
834 p = ly.get_option_parser(usage=_ ("musicxml2ly FILE.xml"),
835 version=('''%prog (LilyPond) @TOPLEVEL_VERSION@\n\n'''
837 _ ("""This program is free software. It is covered by the GNU General Public
838 License and you are welcome to change it and/or distribute copies of it
839 under certain conditions. Invoke as `%s --warranty' for more
840 information.""") % 'lilypond'
842 Copyright (c) 2005--2007 by
843 Han-Wen Nienhuys <hanwen@xs4all.nl> and
844 Jan Nieuwenhuizen <janneke@gnu.org>
846 description=_ ("Convert %s to LilyPond input.") % 'MusicXML' + "\n")
847 p.add_option ('-v', '--verbose',
850 help=_ ("be verbose"))
852 p.add_option ('', '--lxml',
856 help=_ ("Use lxml.etree; uses less memory and cpu time."))
858 p.add_option ('-o', '--output',
864 help=_ ("set output filename to FILE"))
865 p.add_option_group ('bugs',
866 description=(_ ("Report bugs via")
867 + ''' http://post.gmane.org/post.php'''
868 '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
871 def music_xml_voice_name_to_lily_name (part, name):
872 str = "Part%sVoice%s" % (part.id, name)
873 return musicxml_id_to_lily (str)
875 def music_xml_lyrics_name_to_lily_name (part, name, lyricsnr):
876 str = "Part%sVoice%sLyrics%s" % (part.id, name, lyricsnr)
877 return musicxml_id_to_lily (str)
879 def print_voice_definitions (printer, part_list, voices):
881 for (part, nv_dict) in voices.items():
882 part_dict[part.id] = (part, nv_dict)
884 for part in part_list:
885 (part, nv_dict) = part_dict.get (part.id, (None, {}))
886 for (name, ((voice, lyrics), mxlvoice)) in nv_dict.items ():
887 k = music_xml_voice_name_to_lily_name (part, name)
888 printer.dump ('%s = ' % k)
889 voice.print_ly (printer)
892 for l in lyrics.keys ():
893 lname = music_xml_lyrics_name_to_lily_name (part, name, l)
894 printer.dump ('%s = ' %lname )
895 lyrics[l].print_ly (printer)
900 return dict ([(elt,1) for elt in l]).keys ()
902 def print_score_setup (printer, part_list, voices):
903 part_dict = dict ([(p.id, p) for p in voices.keys ()])
907 for part_definition in part_list:
908 part_name = part_definition.id
909 part = part_dict.get (part_name)
911 error_message ('unknown part in part-list: %s' % part_name)
914 nv_dict = voices.get (part)
915 staves = reduce (lambda x,y: x+ y,
916 [mxlvoice._staves.keys ()
917 for (v, mxlvoice) in nv_dict.values ()],
921 staves = uniq_list (staves)
923 printer ('\\context PianoStaff << ')
927 staff_voices = [(music_xml_voice_name_to_lily_name (part, voice_name), voice_name, v)
928 for (voice_name, (v, mxlvoice)) in nv_dict.items ()
929 if mxlvoice._start_staff == s]
931 printer ('\\context Staff = "%s" << ' % s)
933 for (v, voice_name, (music, lyrics)) in staff_voices:
934 printer ('\\context Voice = "%s" \\%s' % (v,v))
937 # Assign the lyrics to that voice
938 for l in lyrics.keys ():
939 ll = music_xml_lyrics_name_to_lily_name (part, voice_name, l)
940 printer ('\\new Lyrics \\lyricsto "%s" \\%s' % (v, ll))
951 printer ('\\new Staff <<')
953 for (n,v) in nv_dict.items ():
954 ((music, lyrics), voice) = v
955 nn = music_xml_voice_name_to_lily_name (part, n)
956 printer ('\\context Voice = "%s" \\%s' % (nn,nn))
958 # Assign the lyrics to that voice
959 for l in lyrics.keys ():
960 ll = music_xml_lyrics_name_to_lily_name (part, n, l)
961 printer ('\\new Lyrics \\lyricsto "%s" \\%s' % (nn, ll))
970 def print_ly_preamble (printer, filename):
971 printer.dump_version ()
972 printer.print_verbatim ('%% automatically converted from %s\n' % filename)
974 def read_musicxml (filename, use_lxml):
978 tree = lxml.etree.parse (filename)
979 mxl_tree = musicxml.lxml_demarshal_node (tree.getroot ())
982 from xml.dom import minidom, Node
984 doc = minidom.parse(filename)
985 node = doc.documentElement
986 return musicxml.minidom_demarshal_node (node)
991 def convert (filename, options):
992 progress ("Reading MusicXML from %s ..." % filename)
994 tree = read_musicxml (filename, options.use_lxml)
997 id_instrument_map = {}
998 if tree.get_maybe_exist_typed_child (musicxml.Part_list):
999 mxl_pl = tree.get_maybe_exist_typed_child (musicxml.Part_list)
1000 part_list = mxl_pl.get_named_children ("score-part")
1002 # score information is contained in the <work>, <identification> or <movement-title> tags
1003 score_information = extract_score_information (tree)
1004 parts = tree.get_typed_children (musicxml.Part)
1005 voices = get_all_voices (parts)
1007 if not options.output_name:
1008 options.output_name = os.path.basename (filename)
1009 options.output_name = os.path.splitext (options.output_name)[0]
1010 elif re.match (".*\.ly", options.output_name):
1011 options.output_name = os.path.splitext (options.output_name)[0]
1014 defs_ly_name = options.output_name + '-defs.ly'
1015 driver_ly_name = options.output_name + '.ly'
1017 printer = musicexp.Output_printer()
1018 progress ("Output to `%s'" % defs_ly_name)
1019 printer.set_file (open (defs_ly_name, 'w'))
1021 print_ly_preamble (printer, filename)
1022 score_information.print_ly (printer)
1023 print_voice_definitions (printer, part_list, voices)
1028 progress ("Output to `%s'" % driver_ly_name)
1029 printer = musicexp.Output_printer()
1030 printer.set_file (open (driver_ly_name, 'w'))
1031 print_ly_preamble (printer, filename)
1032 printer.dump (r'\include "%s"' % os.path.basename (defs_ly_name))
1033 print_score_setup (printer, part_list, voices)
1038 def get_existing_filename_with_extension (filename, ext):
1039 if os.path.exists (filename):
1041 newfilename = filename + ".xml"
1042 if os.path.exists (newfilename):
1044 newfilename = filename + "xml"
1045 if os.path.exists (newfilename):
1050 opt_parser = option_parser()
1052 (options, args) = opt_parser.parse_args ()
1054 opt_parser.print_usage()
1057 # Allow the user to leave out the .xml or xml on the filename
1058 filename = get_existing_filename_with_extension (args[0], "xml")
1059 if filename and os.path.exists (filename):
1060 voices = convert (filename, options)
1062 progress ("Unable to find input file %s" % args[0])
1064 if __name__ == '__main__':