- def interpret (self):
- """Set durations and starting points."""
-
- now = Rational (0)
- factor = Rational (1)
- attr_dict = {}
- measures = self.get_typed_children (Measure)
-
- for m in measures:
- for n in m.get_all_children ():
- dur = Rational (0)
-
- if n.__class__ == Attributes:
- n.set_attributes_from_previous (attr_dict)
- n.read_self ()
- attr_dict = n._dict.copy ()
-
- factor = Rational (1,
- string.atoi (attr_dict['divisions']
- .get_text ()))
- elif (n.get_maybe_exist_typed_child (Duration)
- and not n.get_maybe_exist_typed_child (Chord)):
- mxl_dur = n.get_maybe_exist_typed_child (Duration)
- dur = mxl_dur.get_length () * factor
- if n.get_name() == 'backup':
- dur = - dur
- if n.get_maybe_exist_typed_child (Grace):
- dur = Rational (0)
-
- n._when = now
- n._duration = dur
- now += dur
-
- def extract_voices (part):
- voices = {}
- measures = part.get_typed_children (Measure)
- elements = []
- for m in measures:
- elements.extend (m.get_all_children ())
-
- start_attr = None
- for n in elements:
- voice_id = n.get_maybe_exist_typed_child (class_dict['voice'])
-
- if not (voice_id or isinstance (n, Attributes)):
- continue
-
- if isinstance (n, Attributes) and not start_attr:
- start_attr = n
- continue
-
- if isinstance (n, Attributes):
- for v in voices.values ():
- v.append (n)
- continue
-
- id = voice_id.get_text ()
- if not voices.has_key (id):
- voices[id] = []
-
- voices[id].append (n)
-
- if start_attr:
- for (k,v) in voices.items ():
- v.insert (0, start_attr)
-
- part._voices = voices
- def get_voices (self):
- return self._voices
+ def __init__ (self):
+ Music_xml_node.__init__ (self)
+ self._voices = []
+
+ def get_part_list (self):
+ n = self
+ while n and n.get_name() != 'score-partwise':
+ n = n._parent
+
+ return n.get_named_child ('part-list')
+
+ def interpret (self):
+ """Set durations and starting points."""
+
+ part_list = self.get_part_list ()
+
+ now = Rational (0)
+ factor = Rational (1)
+ attributes_dict = {}
+ attributes_object = None
+ measures = self.get_typed_children (Measure)
+ last_moment = Rational (-1)
+ last_measure_position = Rational (-1)
+ for m in measures:
+ measure_start_moment = now
+ measure_position = Rational (0)
+ for n in m.get_all_children ():
+ dur = Rational (0)
+
+ if n.__class__ == Attributes:
+ n.set_attributes_from_previous (attributes_dict)
+ n.read_self ()
+ attributes_dict = n._dict.copy ()
+ attributes_object = n
+
+ factor = Rational (1,
+ int (attributes_dict['divisions'].get_text ()))
+
+
+ if (n.get_maybe_exist_typed_child (Duration)):
+ mxl_dur = n.get_maybe_exist_typed_child (Duration)
+ dur = mxl_dur.get_length () * factor
+
+ if n.get_name() == 'backup':
+ dur = - dur
+ if n.get_maybe_exist_typed_child (Grace):
+ dur = Rational (0)
+
+ rest = n.get_maybe_exist_typed_child (Rest)
+ if (rest
+ and attributes_object
+ and attributes_object.get_measure_length () == dur):
+
+ rest._is_whole_measure = True
+
+ if (dur > Rational (0)
+ and n.get_maybe_exist_typed_child (Chord)):
+ now = last_moment
+ measure_position = last_measure_position
+
+ last_moment = now
+ last_measure_position = measure_position
+
+ n._when = now
+ n._measure_position = measure_position
+ n._duration = dur
+ now += dur
+ measure_position += dur
+ if n._name == 'note':
+ instrument = n.get_maybe_exist_named_child ('instrument')
+ if instrument:
+ n.instrument_name = part_list.get_instrument (instrument.id)
+
+ if attributes_object:
+ length = attributes_object.get_measure_length ()
+ new_now = measure_start_moment + length
+
+ if now <> new_now:
+ problem = 'incomplete'
+ if now > new_now:
+ problem = 'overfull'
+
+ ## only for verbose operation.
+ if problem <> 'incomplete':
+ m.message ('%s measure? Expected: %s, Difference: %s' % (problem, now, new_now - now))
+
+ now = new_now
+
+ def extract_voices (part):
+ voices = {}
+ measures = part.get_typed_children (Measure)
+ elements = []
+ for m in measures:
+ elements.extend (m.get_all_children ())
+
+ start_attr = None
+ for n in elements:
+ voice_id = n.get_maybe_exist_typed_child (class_dict['voice'])
+
+ if not (voice_id or isinstance (n, Attributes)):
+ continue
+
+ if isinstance (n, Attributes) and not start_attr:
+ start_attr = n
+ continue
+
+ if isinstance (n, Attributes):
+ for v in voices.values ():
+ v.add_element (n)
+ continue
+
+ id = voice_id.get_text ()
+ if not voices.has_key (id):
+ voices[id] = Musicxml_voice()
+
+ voices[id].add_element (n)
+
+ if start_attr:
+ for (k,v) in voices.items ():
+ v.insert (0, start_attr)
+
+ part._voices = voices
+
+ def get_voices (self):
+ return self._voices