def score_to_musicxml()

in moonlight/conversions/musicxml.py [0:0]


def score_to_musicxml(score):
  """Converts a `tensorflow.moonlight.Score` to MusicXML.

  Args:
    score: The OMR score.

  Returns:
    XML text.
  """
  musicxml = MusicXMLScore(score)
  measure_num = 0
  previous_note_start_time = 0
  previous_note_end_time = 0

  for page in score.page:
    for system in page.system:
      system_measures = measures.Measures(system)
      for system_measure_num in moves.xrange(system_measures.size()):
        for staff_num, staff in enumerate(system.staff):
          # Produce the measure, even if there are no glyphs.
          measure = musicxml.get_measure(staff_num, measure_num)

          for glyph in staff.glyph:
            if system_measures.get_measure(glyph) == system_measure_num:
              clef = _glyph_to_clef(glyph)
              if clef is not None:
                attributes = _get_attributes(measure)
                if attributes.find('clef') is not None:
                  attributes.remove(attributes.find('clef'))
                attributes.append(clef)
              note = _glyph_to_note(glyph)
              if note is not None:
                if (glyph.note.start_time == previous_note_start_time and
                    glyph.note.end_time == previous_note_end_time):
                  position = note.index(note.find('pitch'))
                  chord = etree.Element('chord')
                  note.insert(position, chord)
                previous_note_start_time = glyph.note.start_time
                previous_note_end_time = glyph.note.end_time
                measure.append(note)
        measure_num += 1
  # Add <divisions> and <time> to each part.
  for part in musicxml.score:
    # XPath indexing is 1-based.
    measure = part.find('measure[1]')
    if measure is not None:
      attributes = _get_attributes(measure, position=0)
      etree.SubElement(attributes, 'divisions').text = str(DIVISIONS)
      attributes.append(copy.deepcopy(TIME_SIGNATURE))
  return musicxml.to_string()