]> git.donarmstrong.com Git - lilypond.git/blob - scripts/musicxml2ly.py
use sed-atfiles to put relocation handling in python scripts.
[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 from gettext import gettext as _
9
10 """
11 @relocate-preamble@
12 """
13
14 import lilylib as ly
15
16 import musicxml
17 import musicexp
18
19 from rational import Rational
20
21
22 def progress (str):
23     sys.stderr.write (str + '\n')
24     sys.stderr.flush ()
25     
26
27 def musicxml_duration_to_lily (mxl_note):
28     d = musicexp.Duration ()
29     if mxl_note.get_maybe_exist_typed_child (musicxml.Type):
30         d.duration_log = mxl_note.get_duration_log ()
31     else:
32         d.duration_log = 0
33
34     d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
35     d.factor = mxl_note._duration / d.get_length ()
36
37     return d         
38
39 def group_tuplets (music_list, events):
40
41
42     """Collect Musics from
43     MUSIC_LIST demarcated by EVENTS_LIST in TimeScaledMusic objects.
44     """
45
46     
47     indices = []
48
49     j = 0
50     for (ev_chord, tuplet_elt, fraction) in events:
51         while (j < len (music_list)):
52             if music_list[j]== ev_chord:
53                 break
54             j += 1
55         if tuplet_elt.type == 'start':
56             indices.append ((j, None, fraction))
57         elif tuplet_elt.type == 'stop':
58             indices[-1] = (indices[-1][0], j, indices[-1][2])
59
60     new_list = []
61     last = 0
62     for (i1, i2, frac) in indices:
63         if i1 >= i2:
64             continue
65
66         new_list.extend (music_list[last:i1])
67         seq = musicexp.SequentialMusic ()
68         last = i2 + 1
69         seq.elements = music_list[i1:last]
70
71         tsm = musicexp.TimeScaledMusic ()
72         tsm.element = seq
73
74         tsm.numerator = frac[0]
75         tsm.denominator  = frac[1]
76
77         new_list.append (tsm)
78
79     new_list.extend (music_list[last:])
80     return new_list
81
82
83 def musicxml_clef_to_lily (attributes):
84     change = musicexp.ClefChange ()
85     change.type = attributes.get_clef_sign ()
86     return change
87     
88 def musicxml_time_to_lily (attributes):
89     (beats, type) = attributes.get_time_signature ()
90
91     change = musicexp.TimeSignatureChange()
92     change.fraction = (beats, type)
93     
94     return change
95
96 def musicxml_key_to_lily (attributes):
97     start_pitch  = musicexp.Pitch ()
98     (fifths, mode) = attributes.get_key_signature () 
99     try:
100         (n,a) = {
101             'major' : (0,0),
102             'minor' : (5,0),
103             }[mode]
104         start_pitch.step = n
105         start_pitch.alteration = a
106     except  KeyError:
107         print 'unknown mode', mode
108
109     fifth = musicexp.Pitch()
110     fifth.step = 4
111     if fifths < 0:
112         fifths *= -1
113         fifth.step *= -1
114         fifth.normalize ()
115     
116     for x in range (fifths):
117         start_pitch = start_pitch.transposed (fifth)
118
119     start_pitch.octave = 0
120
121     change = musicexp.KeySignatureChange()
122     change.mode = mode
123     change.tonic = start_pitch
124     return change
125     
126 def musicxml_attributes_to_lily (attrs):
127     elts = []
128     attr_dispatch =  {
129         'clef': musicxml_clef_to_lily,
130         'time': musicxml_time_to_lily,
131         'key': musicxml_key_to_lily
132     }
133     for (k, func) in attr_dispatch.items ():
134         childs = attrs.get_named_children (k)
135
136         ## ugh: you get clefs spread over staves for piano
137         if childs:
138             elts.append (func (attrs))
139     
140     return elts
141
142 spanner_event_dict = {
143     'slur' : musicexp.SlurEvent,
144     'beam' : musicexp.BeamEvent,
145 }        
146 spanner_type_dict = {
147     'start': -1,
148     'begin': -1,
149     'stop': 1,
150     'end' : 1
151 }
152
153 def musicxml_spanner_to_lily_event (mxl_event):
154     ev = None
155     
156     name = mxl_event.get_name()
157     try:
158         func = spanner_event_dict[name]
159         ev = func()
160     except KeyError:
161         print 'unknown span event ', mxl_event
162
163     try:
164         key = mxl_event.get_type ()
165         ev.span_direction = spanner_type_dict[key]
166     except KeyError:
167         print 'unknown span type', key, 'for', name
168
169     return ev
170
171 instrument_drumtype_dict = {
172     'Acoustic Snare Drum': 'acousticsnare',
173     'Side Stick': 'sidestick',
174     'Open Triangle': 'opentriangle',
175     'Mute Triangle': 'mutetriangle',
176     'Tambourine': 'tambourine',
177     
178 }
179
180 def musicxml_note_to_lily_main_event (n):
181     pitch  = None
182     duration = None
183         
184     mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
185     event = None
186     if mxl_pitch:
187         pitch = musicxml_pitch_to_lily (mxl_pitch)
188         event = musicexp.NoteEvent()
189         event.pitch = pitch
190
191         acc = n.get_maybe_exist_named_child ('accidental')
192         if acc:
193             # let's not force accs everywhere. 
194             event.cautionary = acc.editorial
195         
196     elif n.get_maybe_exist_typed_child (musicxml.Rest):
197         event = musicexp.RestEvent()
198     elif n.instrument_name:
199         event = musicexp.NoteEvent ()
200         try:
201             event.drum_type = instrument_drumtype_dict[n.instrument_name]
202         except KeyError:
203             n.message ("drum %s type unknow, please add to instrument_drumtype_dict" % n.instrument_name)
204             event.drum_type = 'acousticsnare'
205     
206     if not event:
207         n.message ("cannot find suitable event")
208
209     event.duration = musicxml_duration_to_lily (n)
210     return event
211
212
213 ## todo
214 class NegativeSkip:
215     def __init__ (self, here, dest):
216         self.here = here
217         self.dest = dest
218
219 class LilyPondVoiceBuilder:
220     def __init__ (self):
221         self.elements = []
222         self.end_moment = Rational (0)
223         self.begin_moment = Rational (0)
224         self.pending_multibar = Rational (0)
225
226     def _insert_multibar (self):
227         r = musicexp.MultiMeasureRest ()
228         r.duration = musicexp.Duration()
229         r.duration.duration_log = 0
230         r.duration.factor = self.pending_multibar
231         self.elements.append (r)
232         self.begin_moment = self.end_moment
233         self.end_moment = self.begin_moment + self.pending_multibar
234         self.pending_multibar = Rational (0)
235         
236     def add_multibar_rest (self, duration):
237         self.pending_multibar += duration
238         
239         
240     def add_music (self, music, duration):
241         assert isinstance (music, musicexp.Music)
242         if self.pending_multibar > Rational (0):
243             self._insert_multibar ()
244
245         self.elements.append (music)
246         self.begin_moment = self.end_moment
247         self.end_moment = self.begin_moment + duration 
248
249     def add_bar_check (self, number):
250         b = musicexp.BarCheck ()
251         b.bar_number = number
252         self.add_music (b, Rational (0))
253
254     def jumpto (self, moment):
255         current_end = self.end_moment + self.pending_multibar
256         diff = moment - current_end
257         
258         if diff < Rational (0):
259             print 'Negative skip', diff
260             diff = Rational (0)
261
262         if diff > Rational (0):
263             skip = musicexp.SkipEvent()
264             skip.duration.duration_log = 0
265             skip.duration.factor = diff
266
267             evc = musicexp.EventChord ()
268             evc.elements.append (skip)
269             self.add_music (evc, diff)
270                 
271     def last_event_chord (self, starting_at):
272
273         value = None
274         if (self.elements
275             and isinstance (self.elements[-1], musicexp.EventChord)
276             and self.begin_moment == starting_at):
277             value = self.elements[-1]
278         else:
279             self.jumpto (starting_at)
280             value = None
281
282         return value
283         
284     def correct_negative_skip (self, goto):
285         self.end_moment = goto
286         self.begin_moment = goto
287         evc = musicexp.EventChord ()
288         self.elements.append (evc)
289         
290 def musicxml_voice_to_lily_voice (voice):
291     tuplet_events = []
292     modes_found = {}
293
294     voice_builder = LilyPondVoiceBuilder()
295
296     for n in voice._elements:
297         if n.get_name () == 'forward':
298             continue
299
300         if not n.get_maybe_exist_named_child ('chord'):
301             try:
302                 voice_builder.jumpto (n._when)
303             except NegativeSkip, neg:
304                 voice_builder.correct_negative_skip (n._when)
305                 n.message ("Negative skip? from %s to %s, diff %s" % (neg.here, neg.dest, neg.dest - neg.here))
306             
307         if isinstance (n, musicxml.Attributes):
308             if n.is_first () and n._measure_position == Rational (0):
309                 try:
310                     number = int (n.get_parent ().number)
311                 except ValueError:
312                     number = 0
313                 
314                 voice_builder.add_bar_check (number)
315             for a in musicxml_attributes_to_lily (n):
316                 voice_builder.add_music (a, Rational (0))
317             continue
318
319         if not n.__class__.__name__ == 'Note':
320             print 'not a Note or Attributes?', n
321             continue
322
323         rest = n.get_maybe_exist_typed_child (musicxml.Rest)
324         if (rest
325             and rest.is_whole_measure ()):
326
327             voice_builder.add_multibar_rest (n._duration)
328             continue
329
330         if n.is_first () and n._measure_position == Rational (0):
331             try: 
332                 num = int (n.get_parent ().number)
333             except ValueError:
334                 num = 0
335             voice_builder.add_bar_check (num)
336         
337         main_event = musicxml_note_to_lily_main_event (n)
338
339         try:
340             if main_event.drum_type:
341                 modes_found['drummode'] = True
342         except AttributeError:
343             pass
344
345
346         ev_chord = voice_builder.last_event_chord (n._when)
347         if not ev_chord: 
348             ev_chord = musicexp.EventChord()
349             voice_builder.add_music (ev_chord, n._duration)
350
351         ev_chord.append (main_event)
352         
353         notations = n.get_maybe_exist_typed_child (musicxml.Notations)
354         tuplet_event = None
355         span_events = []
356         if notations:
357             if notations.get_tuplet():
358                 tuplet_event = notations.get_tuplet()
359                 mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
360                 frac = (1,1)
361                 if mod:
362                     frac = mod.get_fraction ()
363                 
364                 tuplet_events.append ((ev_chord, tuplet_event, frac))
365
366             slurs = [s for s in notations.get_named_children ('slur')
367                 if s.get_type () in ('start','stop')]
368             if slurs:
369                 if len (slurs) > 1:
370                     print 'more than 1 slur?'
371
372                 lily_ev = musicxml_spanner_to_lily_event (slurs[0])
373                 ev_chord.append (lily_ev)
374
375             mxl_tie = notations.get_tie ()
376             if mxl_tie and mxl_tie.type == 'start':
377                 ev_chord.append (musicexp.TieEvent ())
378
379         mxl_beams = [b for b in n.get_named_children ('beam')
380                      if (b.get_type () in ('begin', 'end')
381                          and b.is_primary ())] 
382         if mxl_beams:
383             beam_ev = musicxml_spanner_to_lily_event (mxl_beams[0])
384             if beam_ev:
385                 ev_chord.append (beam_ev)
386             
387         if tuplet_event:
388             mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
389             frac = (1,1)
390             if mod:
391                 frac = mod.get_fraction ()
392                 
393             tuplet_events.append ((ev_chord, tuplet_event, frac))
394
395     ## force trailing mm rests to be written out.   
396     voice_builder.add_music (musicexp.EventChord (), Rational (0))
397     
398     ly_voice = group_tuplets (voice_builder.elements, tuplet_events)
399
400     seq_music = musicexp.SequentialMusic()
401
402     if 'drummode' in modes_found.keys ():
403         ## \key <pitch> barfs in drummode.
404         ly_voice = [e for e in ly_voice
405                     if not isinstance(e, musicexp.KeySignatureChange)]
406     
407     seq_music.elements = ly_voice
408
409     
410     
411     if len (modes_found) > 1:
412        print 'Too many modes found', modes_found.keys ()
413
414     return_value = seq_music
415     for mode in modes_found.keys ():
416         v = musicexp.ModeChangingMusicWrapper()
417         v.element = return_value
418         v.mode = mode
419         return_value = v
420     
421     return return_value
422
423
424 def musicxml_id_to_lily (id):
425     digits = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
426               'nine', 'ten']
427     
428     for dig in digits:
429         d = digits.index (dig) + 1
430         dig = dig[0].upper() + dig[1:]
431         id = re.sub ('%d' % d, dig, id)
432
433     id = re.sub  ('[^a-zA-Z]', 'X', id)
434     return id
435
436
437 def musicxml_pitch_to_lily (mxl_pitch):
438     p = musicexp.Pitch()
439     p.alteration = mxl_pitch.get_alteration ()
440     p.step = (ord (mxl_pitch.get_step ()) - ord ('A') + 7 - 2) % 7
441     p.octave = mxl_pitch.get_octave () - 4
442     return p
443
444 def voices_in_part (part):
445     """Return a Name -> Voice dictionary for PART"""
446     part.interpret ()
447     part.extract_voices ()
448     voice_dict = part.get_voices ()
449
450     return voice_dict
451
452 def voices_in_part_in_parts (parts):
453     """return a Part -> Name -> Voice dictionary"""
454     return dict([(p, voices_in_part (p)) for p in parts])
455
456
457 def get_all_voices (parts):
458     all_voices = voices_in_part_in_parts (parts)
459
460     all_ly_voices = {}
461     for p, name_voice in all_voices.items ():
462
463         part_ly_voices = {}
464         for n, v in name_voice.items ():
465             progress ("Converting to LilyPond expressions...")
466             part_ly_voices[n] = (musicxml_voice_to_lily_voice (v), v)
467
468         all_ly_voices[p] = part_ly_voices
469         
470     return all_ly_voices
471
472
473 def option_parser ():
474     p = ly.get_option_parser(usage=_ ("musicxml2ly FILE.xml"),
475                              version=('''%prog (LilyPond) @TOPLEVEL_VERSION@\n\n'''
476                                       +
477 _ ("""This program is free software.  It is covered by the GNU General Public
478 License and you are welcome to change it and/or distribute copies of it
479 under certain conditions.  Invoke as `%s --warranty' for more
480 information.""") % 'lilypond'
481 + """
482 Copyright (c) 2005--2006 by
483     Han-Wen Nienhuys <hanwen@xs4all.nl> and
484     Jan Nieuwenhuizen <janneke@gnu.org>
485 """),
486                              description=_ ("Convert %s to LilyPond input.") % 'MusicXML' + "\n")
487     p.add_option ('-v', '--verbose',
488                   action="store_true",
489                   dest='verbose',
490                   help=_ ("be verbose"))
491
492     p.add_option ('', '--lxml',
493                   action="store_true",
494                   default=False,
495                   dest="use_lxml",
496                   help=_ ("Use lxml.etree; uses less memory and cpu time."))
497     
498     p.add_option ('-o', '--output',
499                   metavar=_ ("FILE"),
500                   action="store",
501                   default=None,
502                   type='string',
503                   dest='output_name',
504                   help=_ ("set output filename to FILE"))
505     p.add_option_group ('bugs',
506                         description=(_ ("Report bugs via")
507                                      + ''' http://post.gmane.org/post.php'''
508                                      '''?group=gmane.comp.gnu.lilypond.bugs\n'''))
509     return p
510
511 def music_xml_voice_name_to_lily_name (part, name):
512     str = "Part%sVoice%s" % (part.id, name)
513     return musicxml_id_to_lily (str) 
514
515 def print_voice_definitions (printer, voices):
516     for (part, nv_dict) in voices.items():
517         
518         for (name, (voice, mxlvoice)) in nv_dict.items ():
519             k = music_xml_voice_name_to_lily_name (part, name)
520             printer.dump ('%s = ' % k)
521             voice.print_ly (printer)
522             printer.newline()
523
524             
525 def uniq_list (l):
526     return dict ([(elt,1) for elt in l]).keys ()
527     
528 def print_score_setup (printer, part_list, voices):
529     part_dict = dict ([(p.id, p) for p in voices.keys ()]) 
530
531     printer ('<<')
532     printer.newline ()
533     for part_definition in part_list:
534         part_name = part_definition.id
535         try:
536             part = part_dict[part_name]
537         except KeyError:
538             print 'unknown part in part-list:', part_name
539             continue
540
541         nv_dict = voices[part]
542         staves = reduce (lambda x,y: x+ y,
543                 [mxlvoice._staves.keys ()
544                  for (v, mxlvoice) in nv_dict.values ()],
545                 [])
546
547         if len (staves) > 1:
548             staves = uniq_list (staves)
549             staves.sort ()
550             printer ('\\context PianoStaff << ')
551             printer.newline ()
552             
553             for s in staves:
554                 staff_voices = [music_xml_voice_name_to_lily_name (part, voice_name)
555                         for (voice_name, (v, mxlvoice)) in nv_dict.items ()
556                         if mxlvoice._start_staff == s]
557                 
558                 printer ('\\context Staff = "%s" << ' % s)
559                 printer.newline ()
560                 for v in staff_voices:
561                     printer ('\\context Voice = "%s"  \\%s' % (v,v))
562                     printer.newline ()
563                 printer ('>>')
564                 printer.newline ()
565                 
566             printer ('>>')
567             printer.newline ()
568             
569         else:
570             printer ('\\new Staff <<')
571             printer.newline ()
572             for (n,v) in nv_dict.items ():
573
574                 n = music_xml_voice_name_to_lily_name (part, n) 
575                 printer ('\\context Voice = "%s"  \\%s' % (n,n))
576             printer ('>>')
577             printer.newline ()
578             
579     printer ('>>')
580     printer.newline ()
581
582 def print_ly_preamble (printer, filename):
583     printer.dump_version ()
584     printer.print_verbatim ('%% converted from %s\n' % filename)
585
586 def read_musicxml (filename, use_lxml):
587     if use_lxml:
588         import lxml.etree
589         
590         tree = lxml.etree.parse (filename)
591         mxl_tree = musicxml.lxml_demarshal_node (tree.getroot ())
592         return mxl_tree
593     else:
594         from xml.dom import minidom, Node
595         
596         doc = minidom.parse(filename)
597         node = doc.documentElement
598         return musicxml.minidom_demarshal_node (node)
599
600     return None
601
602
603 def convert (filename, options):
604     progress ("Reading MusicXML from %s ..." % filename)
605     
606     tree = read_musicxml (filename, options.use_lxml)
607
608     part_list = []
609     id_instrument_map = {}
610     if tree.get_maybe_exist_typed_child (musicxml.Part_list):
611         mxl_pl = tree.get_maybe_exist_typed_child (musicxml.Part_list)
612         part_list = mxl_pl.get_named_children ("score-part")
613         
614     parts = tree.get_typed_children (musicxml.Part)
615     voices = get_all_voices (parts)
616
617     if not options.output_name:
618         options.output_name = os.path.basename (filename) 
619         options.output_name = os.path.splitext (options.output_name)[0]
620
621
622     defs_ly_name = options.output_name + '-defs.ly'
623     driver_ly_name = options.output_name + '.ly'
624
625     printer = musicexp.Output_printer()
626     progress ("Output to `%s'" % defs_ly_name)
627     printer.set_file (open (defs_ly_name, 'w'))
628
629     print_ly_preamble (printer, filename)
630     print_voice_definitions (printer, voices)
631     
632     printer.close ()
633     
634     
635     progress ("Output to `%s'" % driver_ly_name)
636     printer = musicexp.Output_printer()
637     printer.set_file (open (driver_ly_name, 'w'))
638     print_ly_preamble (printer, filename)
639     printer.dump (r'\include "%s"' % defs_ly_name)
640     print_score_setup (printer, part_list, voices)
641     printer.newline ()
642
643     return voices
644
645
646 def main ():
647     opt_parser = option_parser()
648
649     (options, args) = opt_parser.parse_args ()
650     if not args:
651         opt_parser.print_usage()
652         sys.exit (2)
653
654     voices = convert (args[0], options)
655
656 if __name__ == '__main__':
657     main()