import musicexp
from rational import Rational
+
+def progress (str):
+ sys.stderr.write (str + '\n')
+ sys.stderr.flush ()
+
+
def musicxml_duration_to_lily (mxl_note):
d = musicexp.Duration ()
if mxl_note.get_maybe_exist_typed_child (musicxml.Type):
return d
-span_event_dict = {
- 'start': -1,
- 'stop': 1
-}
-
def group_tuplets (music_list, events):
+
+
+ """Collect Musics from
+ MUSIC_LIST demarcated by EVENTS_LIST in TimeScaledMusic objects.
+ """
+
+
indices = []
j = 0
new_list.extend (music_list[last:])
return new_list
+
def musicxml_clef_to_lily (mxl):
sign = mxl.get_maybe_exist_named_child ('sign')
change = musicexp.ClefChange ()
return change
def musicxml_key_to_lily (mxl):
- mode = mxl.get_maybe_exist_named_child ('mode').get_text ()
+ start_pitch = musicexp.Pitch ()
+ try:
+ mode = mxl.get_maybe_exist_named_child ('mode')
+ if mode:
+ mode = mode.get_text ()
+ else:
+ mode = 'major'
+
+ (n,a) = { 'major' : (0,0),
+ 'minor' : (6,0),
+ }[mode]
+ start_pitch.step = n
+ start_pitch.alteration = a
+ except KeyError:
+ print 'unknown mode', mode
+
fifths = string.atoi (mxl.get_maybe_exist_named_child ('fifths').get_text ())
fifth = musicexp.Pitch()
fifth.step *= -1
fifth.normalize ()
- c = musicexp.Pitch()
+ start_pitch = musicexp.Pitch()
for x in range (fifths):
- c = c.transposed (fifth)
+ start_pitch = start_pitch.transposed (fifth)
- c.octave = 0
+ start_pitch.octave = 0
change = musicexp.KeySignatureChange()
change.mode = mode
- change.tonic = c
+ change.tonic = start_pitch
return change
def musicxml_attributes_to_lily (attrs):
return elts
-def insert_measure_start_comments (ly_voice, indices):
- idxs = indices[:]
- idxs.reverse ()
- for i in idxs:
- c = musicexp.Comment()
- c.text = ''
- ly_voice.insert (i, c)
+def create_skip_music (duration):
+ skip = musicexp.SkipEvent()
+ skip.duration.duration_log = 0
+ skip.duration.factor = duration
+
+ evc = musicexp.EventChord ()
+ evc.append (skip)
+ return evc
- return ly_voice
+spanner_event_dict = {
+ 'slur' : musicexp.SlurEvent,
+ 'beam' : musicexp.BeamEvent,
+}
+spanner_type_dict = {
+ 'start': -1,
+ 'begin': -1,
+ 'stop': 1,
+ 'end' : 1
+}
+
+def musicxml_spanner_to_lily_event (mxl_event):
+ ev = None
+ name = mxl_event.get_name()
+ try:
+ func = spanner_event_dict[name]
+ ev = func()
+ except KeyError:
+ print 'unknown span event ', mxl_event
+
+ try:
+ key = mxl_event.get_type ()
+ ev.span_direction = spanner_type_dict[key]
+ except KeyError:
+ print 'unknown span type', key, 'for', name
+
+ return ev
+
+def musicxml_note_to_lily_main_event (n):
+ pitch = None
+ duration = None
+
+ mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
+ event = None
+ if mxl_pitch:
+ pitch = musicxml_pitch_to_lily (mxl_pitch)
+ event = musicexp.NoteEvent()
+ event.pitch = pitch
+
+ acc = n.get_maybe_exist_named_child ('accidental')
+ if acc:
+ # let's not force accs everywhere.
+ event.cautionary = acc.editorial
+
+ elif n.get_maybe_exist_typed_child (musicxml.Rest):
+ event = musicexp.RestEvent()
+
+ event.duration = musicxml_duration_to_lily (n)
+ return event
+
def musicxml_voice_to_lily_voice (voice):
-
ly_voice = []
ly_now = Rational (0)
+ pending_skip = Rational (0)
tuplet_events = []
- measure_start_indices = []
- for n in voice:
- if n.is_first ():
- measure_start_indices.append (len (ly_voice))
-
+ for n in voice._elements:
+ if n.get_name () == 'forward':
+ continue
+
if isinstance (n, musicxml.Attributes):
+ ly_now += pending_skip
+ pending_skip = Rational (0)
+
ly_voice.extend (musicxml_attributes_to_lily (n))
continue
if not n.__class__.__name__ == 'Note':
- print 'not a Note or Attributes?'
+ print 'not a Note or Attributes?', n
continue
+
+ if n.is_first () and ly_voice:
+ ly_voice[-1].comment += '\n'
-
- pitch = None
- duration = None
-
- mxl_pitch = n.get_maybe_exist_typed_child (musicxml.Pitch)
- event = None
- notations = n.get_maybe_exist_typed_child (musicxml.Notations)
- tuplet_event = None
- slur_event = None
- if notations:
- tuplet_event = notations.get_tuplet ()
- slur_event = notations.get_slur ()
-
- if mxl_pitch:
- pitch = musicxml_pitch_to_lily (mxl_pitch)
- event = musicexp.NoteEvent()
- event.pitch = pitch
- elif n.get_maybe_exist_typed_child (musicxml.Rest):
- event = musicexp.RestEvent()
-
- event.duration = musicxml_duration_to_lily (n)
+ main_event = musicxml_note_to_lily_main_event (n)
+
ev_chord = None
if None == n.get_maybe_exist_typed_child (musicxml.Chord):
- if ly_voice:
- ly_now += ly_voice[-1].get_length ()
+ ly_now += pending_skip
+ pending_skip = main_event.get_length ()
if ly_now <> n._when:
- diff = n._when - ly_now
+ diff = n._when - ly_now
+
if diff < Rational (0):
print 'huh: negative skip', n._when, ly_now, n._duration
diff = Rational (1,314159265)
-
- skip = musicexp.SkipEvent()
- skip.duration.duration_log = 0
- skip.duration.factor = diff
- evc = musicexp.EventChord ()
- evc.elements.append (skip)
- ly_voice.append (evc)
+ ly_voice.append (create_skip_music (diff))
ly_now = n._when
ly_voice.append (musicexp.EventChord())
else:
pass
-
+
ev_chord = ly_voice[-1]
- ev_chord.elements.append (event)
-
+ ev_chord.append (main_event)
+
+ notations = n.get_maybe_exist_typed_child (musicxml.Notations)
+ tuplet_event = None
+ span_events = []
+ if notations:
+ if notations.get_tuplet():
+ tuplet_event = notations.get_tuplet()
+ mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
+ frac = (1,1)
+ if mod:
+ frac = mod.get_fraction ()
+
+ tuplet_events.append ((ev_chord, tuplet_event, frac))
+
+ slurs = [s for s in notations.get_named_children ('slur')
+ if s.get_type () in ('start','stop')]
+ if slurs:
+ if len (slurs) > 1:
+ print 'more than 1 slur?'
+
+ lily_ev = musicxml_spanner_to_lily_event (slurs[0])
+ ev_chord.append (lily_ev)
+
+ mxl_tie = notations.get_tie ()
+ if mxl_tie and mxl_tie.type == 'start':
+ ev_chord.append (musicexp.TieEvent ())
+
+ mxl_beams = [b for b in n.get_named_children ('beam')
+ if b.get_type () in ('begin', 'end')]
+ if mxl_beams:
+ beam_ev = musicxml_spanner_to_lily_event (mxl_beams[0])
+ if beam_ev:
+ ev_chord.append (beam_ev)
+
if tuplet_event:
mod = n.get_maybe_exist_typed_child (musicxml.Time_modification)
frac = (1,1)
frac = mod.get_fraction ()
tuplet_events.append ((ev_chord, tuplet_event, frac))
-
- if slur_event:
- sp = musicexp.SlurEvent()
- try:
- sp.span_direction = span_event_dict[slur_event.type]
- ev_chord.elements.append (sp)
- except KeyError:
- pass
-
- ly_voice = insert_measure_start_comments (ly_voice, measure_start_indices)
+
ly_voice = group_tuplets (ly_voice, tuplet_events)
seq_music = musicexp.SequentialMusic()
p = musicexp.Pitch()
p.alteration = mxl_pitch.get_alteration ()
p.step = (ord (mxl_pitch.get_step ()) - ord ('A') + 7 - 2) % 7
- p.octave = mxl_pitch.get_octave () -4
+ p.octave = mxl_pitch.get_octave () - 4
return p
+
+
+def voices_in_part (part):
+ """Return a Name -> Voice dictionary for PART"""
+ part.interpret ()
+ part.extract_voices ()
+ voice_dict = part.get_voices ()
+
+ return voice_dict
+
+def voices_in_part_in_parts (parts):
+ """return a Part -> Name -> Voice dictionary"""
+ return dict([(p, voices_in_part (p)) for p in parts])
+
+
def get_all_voices (parts):
- all_voices = {}
- for p in parts:
- p.interpret ()
- p.extract_voices ()
- voice_dict = p.get_voices ()
-
- for (id, voice) in voice_dict.items ():
- m = musicxml_voice_to_lily_voice (voice)
- m_name = 'Part' + p.id + 'Voice' + id
- m_name = musicxml_id_to_lily (m_name)
- all_voices[m_name] = m
+ all_voices = voices_in_part_in_parts (parts)
- return all_voices
+ all_ly_voices = {}
+ for p, name_voice in all_voices.items ():
+
+ part_ly_voices = {}
+ for n, v in name_voice.items ():
+ progress ("Converting to LilyPond expressions...")
+ part_ly_voices[n] = (musicxml_voice_to_lily_voice (v), v)
+
+ all_ly_voices[p] = part_ly_voices
+
+ return all_ly_voices
class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
def format_heading(self, heading):
under certain conditions. Invoke as `lilypond --warranty' for more
information.
-Copyright (c) 2005 by
+Copyright (c) 2005--2006 by
Han-Wen Nienhuys <hanwen@xs4all.nl> and
Jan Nieuwenhuizen <janneke@gnu.org>
""",
p.formatter = NonDentedHeadingFormatter ()
return p
+def music_xml_voice_name_to_lily_name (part, name):
+ str = "Part%sVoice%s" % (part.id, name)
+ return musicxml_id_to_lily (str)
+
+def print_voice_definitions (printer, voices):
+ for (part, nv_dict) in voices.items():
+ for (name, (voice, mxlvoice)) in nv_dict.items ():
+ k = music_xml_voice_name_to_lily_name (part, name)
+ printer.dump ('%s = ' % k)
+ voice.print_ly (printer)
+ printer.newline()
+def uniq_list (l):
+ return dict ([(elt,1) for elt in l]).keys ()
+
+def print_score_setup (printer, part_list, voices):
+ part_dict = dict ([(p.id, p) for p in voices.keys ()])
+
+ printer ('<<')
+ printer.newline ()
+ for part_definition in part_list:
+ part_name = part_definition.id
+ try:
+ part = part_dict[part_name]
+ except KeyError:
+ print 'unknown part in part-list:', part_name
+ continue
+
+ nv_dict = voices[part]
+ staves = reduce (lambda x,y: x+ y,
+ [mxlvoice._staves.keys ()
+ for (v, mxlvoice) in nv_dict.values ()],
+ [])
+
+ if len (staves) > 1:
+ staves = uniq_list (staves)
+ staves.sort ()
+ printer ('\\context PianoStaff << ')
+ printer.newline ()
+
+ for s in staves:
+ staff_voices = [music_xml_voice_name_to_lily_name (part, voice_name)
+ for (voice_name, (v, mxlvoice)) in nv_dict.items ()
+ if mxlvoice._start_staff == s]
+
+ printer ('\\context Staff = "%s" << ' % s)
+ printer.newline ()
+ for v in staff_voices:
+ printer ('\\context Voice = "%s" \\%s' % (v,v))
+ printer.newline ()
+ printer ('>>')
+ printer.newline ()
+
+ printer ('>>')
+ printer.newline ()
+
+ else:
+ printer ('\\new Staff <<')
+ printer.newline ()
+ for (n,v) in nv_dict.items ():
+
+ n = music_xml_voice_name_to_lily_name (part, n)
+ printer ('\\context Voice = "%s" \\%s' % (n,n))
+ printer ('>>')
+ printer.newline ()
+
+
+ printer ('>>')
+ printer.newline ()
+
+
+
+def print_ly_preamble (printer, filename):
+ printer.dump_version ()
+ printer.print_verbatim ('%% converted from %s\n' % filename)
def convert (filename, output_name):
-
printer = musicexp.Output_printer()
+ progress ("Reading MusicXML...")
+
tree = musicxml.read_musicxml (filename)
parts = tree.get_typed_children (musicxml.Part)
-
voices = get_all_voices (parts)
+ part_list = []
+ if tree.get_maybe_exist_typed_child (musicxml.Part_list):
+ pl = tree.get_maybe_exist_typed_child (musicxml.Part_list)
+ part_list = pl.get_named_children ("score-part")
+
+ if not output_name:
+ output_name = os.path.basename (filename)
+ output_name = os.path.splitext (output_name)[0] + '.ly'
- if output_name:
- printer.file = open (output_name,'w')
- for (k,v) in voices.items():
- printer.dump ('%s = ' % k)
- v.print_ly (printer)
- printer.newline()
+ if output_name:
+ progress ("Output to `%s'" % output_name)
+ printer.set_file (open (output_name, 'w'))
+
+ progress ("Printing as .ly...")
+ print_ly_preamble (printer, filename)
+ print_voice_definitions (printer, voices)
+ print_score_setup (printer, part_list, voices)
+ printer.newline ()
return voices
-opt_parser = option_parser()
+def main ():
+ opt_parser = option_parser()
+
+ (options, args) = opt_parser.parse_args ()
+ if not args:
+ opt_parser.print_usage()
+ sys.exit (2)
-(options, args) = opt_parser.parse_args ()
-if options.version:
- opt_parser.print_version()
- sys.exit (0)
-if not args:
- opt_parser.print_usage()
- sys.exit (2)
+ voices = convert (args[0], options.output)
-voices = convert (args[0], options.output)
+if __name__ == '__main__':
+ main()