+
+span_event_dict = {
+ 'start': -1,
+ 'stop': 1
+}
+
+def group_tuplets (music_list, events):
+ indices = []
+
+ j = 0
+ for (ev_chord, tuplet_elt, fraction) in events:
+ while (j < len (music_list)):
+ if music_list[j]== ev_chord:
+ break
+ j += 1
+ if tuplet_elt.type == 'start':
+ indices.append ((j, None, fraction))
+ elif tuplet_elt.type == 'stop':
+ indices[-1] = (indices[-1][0], j, indices[-1][2])
+
+ new_list = []
+ last = 0
+ for (i1, i2, frac) in indices:
+ if i1 >= i2:
+ continue
+
+ new_list.extend (music_list[last:i1])
+ seq = musicexp.SequentialMusic ()
+ last = i2 + 1
+ seq.elements = music_list[i1:last]
+
+ tsm = musicexp.TimeScaledMusic ()
+ tsm.element = seq
+
+ tsm.numerator = frac[0]
+ tsm.denominator = frac[1]
+
+ new_list.append (tsm)
+
+ 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 ()
+ if sign:
+ change.type = sign.get_text ()
+ return change
+
+
+def musicxml_time_to_lily (mxl):
+ beats = mxl.get_maybe_exist_named_child ('beats')
+ type = mxl.get_maybe_exist_named_child ('beat-type')
+ change = musicexp.TimeSignatureChange()
+ change.fraction = (string.atoi(beats.get_text ()),
+ string.atoi(type.get_text ()))
+
+ return change
+
+def musicxml_key_to_lily (mxl):
+ mode = mxl.get_maybe_exist_named_child ('mode').get_text ()
+ fifths = string.atoi (mxl.get_maybe_exist_named_child ('fifths').get_text ())
+
+ fifth = musicexp.Pitch()
+ fifth.step = 4
+ if fifths < 0:
+ fifths *= -1
+ fifth.step *= -1
+ fifth.normalize ()
+
+ c = musicexp.Pitch()
+ for x in range (fifths):
+ c = c.transposed (fifth)
+
+ c.octave = 0
+
+ change = musicexp.KeySignatureChange()
+ change.mode = mode
+ change.tonic = c
+ return change
+
+def musicxml_attributes_to_lily (attrs):
+ elts = []
+ attr_dispatch = {
+ 'clef': musicxml_clef_to_lily,
+ 'time': musicxml_time_to_lily,
+ 'key': musicxml_key_to_lily
+ }
+ for (k, func) in attr_dispatch.items ():
+ childs = attrs.get_named_children (k)
+
+ ## ugh: you get clefs spread over staves for piano
+ if childs:
+ elts.append (func (childs[0]))
+
+ 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)
+
+ return ly_voice