+ d = musicexp.Duration ()
+ if mxl_note.get_maybe_exist_typed_child (musicxml.Type):
+ d.duration_log = mxl_note.get_duration_log ()
+ else:
+ d.duration_log = 0
+
+ d.dots = len (mxl_note.get_typed_children (musicxml.Dot))
+ d.factor = mxl_note._duration / d.get_length ()
+
+ return d
+
+def group_tuplets (music_list, events):
+
+
+ """Collect Musics from
+ MUSIC_LIST demarcated by EVENTS_LIST in TimeScaledMusic objects.
+ """
+
+
+ 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 (attributes):
+ change = musicexp.ClefChange ()
+ change.type = attributes.get_clef_sign ()
+ return change
+
+def musicxml_time_to_lily (attributes):
+ (beats, type) = attributes.get_time_signature ()
+
+ change = musicexp.TimeSignatureChange()
+ change.fraction = (beats, type)
+
+ return change
+
+def musicxml_key_to_lily (attributes):
+ start_pitch = musicexp.Pitch ()
+ (fifths, mode) = attributes.get_key_signature ()
+ try:
+ (n,a) = {
+ 'major' : (0,0),
+ 'minor' : (5,0),
+ }[mode]
+ start_pitch.step = n
+ start_pitch.alteration = a
+ except KeyError:
+ print 'unknown mode', mode
+
+ fifth = musicexp.Pitch()
+ fifth.step = 4
+ if fifths < 0:
+ fifths *= -1
+ fifth.step *= -1
+ fifth.normalize ()
+
+ for x in range (fifths):
+ start_pitch = start_pitch.transposed (fifth)
+
+ start_pitch.octave = 0
+
+ change = musicexp.KeySignatureChange()
+ change.mode = mode
+ change.tonic = start_pitch
+ 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 (attrs))
+
+ return elts
+
+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
+
+instrument_drumtype_dict = {
+ 'Acoustic Snare Drum': 'acousticsnare',
+ 'Side Stick': 'sidestick',
+ 'Open Triangle': 'opentriangle',
+ 'Mute Triangle': 'mutetriangle',
+ 'Tambourine': 'tambourine',
+
+}
+
+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()
+ elif n.instrument_name:
+ event = musicexp.NoteEvent ()
+ 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 ("cannot find suitable event")
+
+ event.duration = musicxml_duration_to_lily (n)
+ return event
+
+
+## todo
+class NegativeSkip:
+ def __init__ (self, here, dest):
+ self.here = here
+ self.dest = dest
+
+class LilyPondVoiceBuilder:
+ def __init__ (self):
+ self.elements = []
+ self.end_moment = Rational (0)
+ self.begin_moment = Rational (0)
+ self.pending_multibar = Rational (0)
+
+ def _insert_multibar (self):
+ r = musicexp.MultiMeasureRest ()
+ r.duration = musicexp.Duration()
+ r.duration.duration_log = 0
+ r.duration.factor = self.pending_multibar
+ self.elements.append (r)
+ self.begin_moment = self.end_moment
+ self.end_moment = self.begin_moment + self.pending_multibar
+ self.pending_multibar = Rational (0)
+
+ def add_multibar_rest (self, duration):
+ self.pending_multibar += duration
+
+
+ def add_music (self, music, duration):
+ assert isinstance (music, musicexp.Music)
+ if self.pending_multibar > Rational (0):
+ self._insert_multibar ()
+
+ self.elements.append (music)
+ self.begin_moment = self.end_moment
+ self.end_moment = self.begin_moment + duration
+
+ def add_bar_check (self, number):
+ b = musicexp.BarCheck ()
+ b.bar_number = number
+ self.add_music (b, Rational (0))
+
+ def jumpto (self, moment):
+ current_end = self.end_moment + self.pending_multibar
+ diff = moment - current_end
+
+ if diff < Rational (0):
+ print 'Negative skip', diff
+ diff = Rational (0)
+
+ if diff > Rational (0):
+ skip = musicexp.SkipEvent()
+ skip.duration.duration_log = 0
+ skip.duration.factor = diff
+
+ evc = musicexp.EventChord ()
+ evc.elements.append (skip)
+ 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):
+ value = self.elements[-1]
+ else:
+ self.jumpto (starting_at)
+ value = None
+
+ return value
+
+ def correct_negative_skip (self, goto):
+ self.end_moment = goto
+ self.begin_moment = goto
+ evc = musicexp.EventChord ()
+ self.elements.append (evc)
+