5 datadir = '@local_lilypond_datadir@'
6 if not os.path.isdir (datadir):
7 datadir = '@lilypond_datadir@'
8 if os.environ.has_key ('LILYPONDPREFIX'):
9 datadir = os.environ['LILYPONDPREFIX']
10 while datadir[-1] == os.sep:
11 datadir = datadir[:-1]
13 if os.path.exists (os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/')):
14 datadir = os.path.join (datadir, 'share/lilypond/@TOPLEVEL_VERSION@/')
15 elif os.path.exists (os.path.join (datadir, 'share/lilypond/current/')):
16 datadir = os.path.join (datadir, 'share/lilypond/current/')
18 sys.path.insert (0, os.path.join (datadir, 'python'))
22 from rational import Rational
24 def musicxml_duration_to_lily (mxl_note):
25 d = musicexp.Duration ()
26 if mxl_note.get_maybe_exist_typed_child (musicxml.Type):
27 d.duration_log = mxl_note.get_duration_log ()
29 d.factor = mxl_note._duration
32 d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
33 d.factor = mxl_note._duration / d.get_length ()
37 def musicxml_voice_to_lily_voice (voice):
42 if not n.__class__.__name__ == 'Note':
49 mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
53 pitch = musicxml_pitch_to_lily (mxl_pitch)
54 event = musicexp.NoteEvent()
56 elif n.get_maybe_exist_typed_child (musicxml.Rest):
57 event = musicexp.RestEvent()
59 event.duration = musicxml_duration_to_lily (n)
61 if None == n.get_maybe_exist_typed_child (musicxml.Chord):
63 ly_now += ly_voice[-1].get_length ()
66 diff = n._when - ly_now
67 if diff < Rational (0):
68 print 'huh: negative skip', n._when, ly_now, n._duration
69 diff = Rational (1,314159265)
72 skip = musicexp.SkipEvent()
73 skip.duration.duration_log = 0
74 skip.duration.factor = diff
76 evc = musicexp.EventChord ()
77 evc.elements.append (skip)
81 ly_voice.append (musicexp.EventChord())
85 ev_chord = ly_voice[-1]
86 ev_chord.elements.append (event)
89 seq_music = musicexp.SequentialMusic()
92 seq_music.elements = ly_voice
96 def musicxml_id_to_lily (id):
97 digits = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
101 d = digits.index (dig) + 1
102 dig = dig[0].upper() + dig[1:]
103 id = re.sub ('%d' % d, dig, id)
105 id = re.sub ('[^a-zA-Z]', 'X', id)
109 def musicxml_pitch_to_lily (mxl_pitch):
111 p.alteration = mxl_pitch.get_alteration ()
112 p.step = (ord (mxl_pitch.get_step ()) - ord ('A') + 7 - 2) % 7
113 p.octave = mxl_pitch.get_octave () -4
116 def get_all_voices (parts):
121 voice_dict = p.get_voices ()
123 for (id, voice) in voice_dict.items ():
124 m = musicxml_voice_to_lily_voice (voice)
125 m_name = 'Part' + p.name + 'Voice' + id
126 m_name = musicxml_id_to_lily (m_name)
127 all_voices[m_name] = m
131 printer = musicexp.Output_printer()
134 tree = musicxml.read_musicxml (sys.argv[1])
135 parts = tree.get_typed_children (musicxml.Part)
137 voices = get_all_voices (parts)
138 for (k,v) in voices.items():
140 v.print_ly (printer.dump)