X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=scripts%2Fmusicxml2ly.py;h=7a8e8c407165cc310fe7f54aaa3065b92537b02b;hb=ade39b0e10fdf852db6254afac36473a1c66a4d3;hp=c24dccb5bdb7e068b0f3ec629b7db83ab74f8109;hpb=baf8c957f8021e07d5ad4503f0eb45309e77ef55;p=lilypond.git diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index c24dccb5bd..7a8e8c4071 100644 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -1,4 +1,4 @@ -#!@PYTHON@ +#!@TARGET_PYTHON@ import optparse import sys @@ -7,31 +7,9 @@ import os import string from gettext import gettext as _ - - - -datadir = '@local_lilypond_datadir@' -if not os.path.isdir (datadir): - datadir = '@lilypond_datadir@' -if os.environ.has_key ('LILYPONDPREFIX'): - datadir = os.environ['LILYPONDPREFIX'] - while datadir[-1] == os.sep: - datadir = datadir[:-1] - -if os.path.exists (os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/')): - datadir = os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/') -elif os.path.exists (os.path.join (datadir, 'share/lilypond/current/')): - datadir = os.path.join (datadir, 'share/lilypond/current/') - -sys.path.insert (0, os.path.join (datadir, 'python')) - -# dynamic relocation, for GUB binaries. -bindir = os.path.split (sys.argv[0])[0] - -for prefix_component in ['share', 'lib']: - datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % prefix_component) - sys.path.insert (0, datadir) - +""" +@relocate-preamble@ +""" import lilylib as ly @@ -121,7 +99,7 @@ def musicxml_key_to_lily (attributes): try: (n,a) = { 'major' : (0,0), - 'minor' : (6,0), + 'minor' : (5,0), }[mode] start_pitch.step = n start_pitch.alteration = a @@ -135,7 +113,6 @@ def musicxml_key_to_lily (attributes): fifth.step *= -1 fifth.normalize () - start_pitch = musicexp.Pitch() for x in range (fifths): start_pitch = start_pitch.transposed (fifth) @@ -195,6 +172,7 @@ instrument_drumtype_dict = { 'Acoustic Snare Drum': 'acousticsnare', 'Side Stick': 'sidestick', 'Open Triangle': 'opentriangle', + 'Mute Triangle': 'mutetriangle', 'Tambourine': 'tambourine', } @@ -219,11 +197,14 @@ def musicxml_note_to_lily_main_event (n): event = musicexp.RestEvent() elif n.instrument_name: event = musicexp.NoteEvent () - event.drum_type = instrument_drumtype_dict[n.instrument_name] - + try: + event.drum_type = instrument_drumtype_dict[n.instrument_name] + except KeyError: + n.message ("drum %s type unknow, please add to instrument_drumtype_dict" % n.instrument_name) + event.drum_type = 'acousticsnare' if not event: - n.message ("could not find suitable event") + n.message ("cannot find suitable event") event.duration = musicxml_duration_to_lily (n) return event @@ -275,7 +256,8 @@ class LilyPondVoiceBuilder: diff = moment - current_end if diff < Rational (0): - raise NegativeSkip(current_end, moment) + print 'Negative skip', diff + diff = Rational (0) if diff > Rational (0): skip = musicexp.SkipEvent() @@ -287,13 +269,18 @@ class LilyPondVoiceBuilder: self.add_music (evc, diff) def last_event_chord (self, starting_at): + + value = None if (self.elements and isinstance (self.elements[-1], musicexp.EventChord) and self.begin_moment == starting_at): - return self.elements[-1] + value = self.elements[-1] else: self.jumpto (starting_at) - return None + value = None + + return value + def correct_negative_skip (self, goto): self.end_moment = goto self.begin_moment = goto @@ -305,19 +292,26 @@ def musicxml_voice_to_lily_voice (voice): modes_found = {} voice_builder = LilyPondVoiceBuilder() + for n in voice._elements: if n.get_name () == 'forward': continue - try: - voice_builder.jumpto (n._when) - except NegativeSkip, neg: - voice_builder.correct_negative_skip (n._when) - n.message ("Negative skip? from %s to %s, diff %s" % (neg.here, neg.dest, neg.dest - neg.here)) + if not n.get_maybe_exist_named_child ('chord'): + try: + voice_builder.jumpto (n._when) + except NegativeSkip, neg: + voice_builder.correct_negative_skip (n._when) + n.message ("Negative skip? from %s to %s, diff %s" % (neg.here, neg.dest, neg.dest - neg.here)) if isinstance (n, musicxml.Attributes): if n.is_first () and n._measure_position == Rational (0): - voice_builder.add_bar_check (int (n.get_parent ().number)) + try: + number = int (n.get_parent ().number) + except ValueError: + number = 0 + + voice_builder.add_bar_check (number) for a in musicxml_attributes_to_lily (n): voice_builder.add_music (a, Rational (0)) continue @@ -334,7 +328,10 @@ def musicxml_voice_to_lily_voice (voice): continue if n.is_first () and n._measure_position == Rational (0): - num = int (n.get_parent ().number) + try: + num = int (n.get_parent ().number) + except ValueError: + num = 0 voice_builder.add_bar_check (num) main_event = musicxml_note_to_lily_main_event (n) @@ -349,10 +346,9 @@ def musicxml_voice_to_lily_voice (voice): ev_chord = voice_builder.last_event_chord (n._when) if not ev_chord: ev_chord = musicexp.EventChord() + voice_builder.add_music (ev_chord, n._duration) - ev_chord.append (main_event) - voice_builder.add_music (ev_chord, n._duration) notations = n.get_maybe_exist_typed_child (musicxml.Notations) tuplet_event = None @@ -475,44 +471,41 @@ def get_all_voices (parts): def option_parser (): - p = ly.get_option_parser(usage='musicxml2ly FILE.xml', - version = """%prog (LilyPond) @TOPLEVEL_VERSION@ - -This program is free software. It is covered by the GNU General Public + p = ly.get_option_parser(usage=_ ("musicxml2ly FILE.xml"), + version=('''%prog (LilyPond) @TOPLEVEL_VERSION@\n\n''' + + +_ ("""This program is free software. It is covered by the GNU General Public License and you are welcome to change it and/or distribute copies of it -under certain conditions. Invoke as `lilypond --warranty' for more -information. - +under certain conditions. Invoke as `%s --warranty' for more +information.""") % 'lilypond' ++ """ Copyright (c) 2005--2006 by Han-Wen Nienhuys and Jan Nieuwenhuizen -""", - - description = - """Convert MusicXML file to LilyPond input. -""" - ) +"""), + description=_ ("Convert %s to LilyPond input.") % 'MusicXML' + "\n") p.add_option ('-v', '--verbose', - action = "store_true", - dest = 'verbose', - help = 'be verbose') + action="store_true", + dest='verbose', + help=_ ("be verbose")) p.add_option ('', '--lxml', action="store_true", default=False, dest="use_lxml", - help="Use lxml.etree; uses less memory and cpu time.") + help=_ ("Use lxml.etree; uses less memory and cpu time.")) p.add_option ('-o', '--output', - metavar = 'FILE', + metavar=_ ("FILE"), action="store", default=None, type='string', dest='output_name', - help='set output file') - - p.add_option_group ('', description = '''Report bugs via http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs -''') + help=_ ("set output filename to FILE")) + p.add_option_group ('bugs', + description=(_ ("Report bugs via") + + ''' http://post.gmane.org/post.php''' + '''?group=gmane.comp.gnu.lilypond.bugs\n''')) return p def music_xml_voice_name_to_lily_name (part, name):