def _assign_barlines()

in moonlight/structure/barlines.py [0:0]


  def _assign_barlines(self, systems):
    """Assigns each barline to a system.

    Args:
      systems: The list of StaffSystem messages.
    """
    system_start = 0
    for system in systems:
      system_end = system_start + len(system.staff) - 1
      selected_barlines = set()
      blacklist_x = self._get_blacklist_x(system)
      for i in moves.xrange(len(self.barlines)):
        barline_x = self.barlines[i, 0, 0]
        start = self.barline_staff_start[i]
        end = self.barline_staff_end[i]
        if (not blacklist_x[barline_x] and
            system_start <= start <= end <= system_end):
          # Get the selected barlines which are close enough to the current
          # barline that they are probably a duplicate.
          close_barlines = [
              other_barline for other_barline in selected_barlines
              if abs(self.barlines[other_barline, 0, 0] -
                     barline_x) < self.close_barline_threshold
          ]

          def get_span(barline):
            return (self.barline_staff_end[barline] -
                    self.barline_staff_start[barline])

          # Assumes all barlines span the entire staff system.
          # Don't add a barline if we've already seen a duplicate unless it
          # spans more staves than the currently selected one.
          # TODO(ringw): This works for piano scores, but not multi-part
          # scores, which have one barline spanning the entire staff system at
          # the beginning and then one barline per staff for the following
          # measures. Make this more robust.
          if (all(end - start >= get_span(other_barline)
                  for other_barline in selected_barlines) and
              all(end - start > get_span(other_barline)
                  for other_barline in close_barlines)):
            selected_barlines.difference_update(close_barlines)
            selected_barlines.add(i)
      barline_xs = sorted(
          self.barlines[barline, 0, 0] for barline in selected_barlines)
      system.bar.extend(
          musicscore_pb2.StaffSystem.Bar(
              x=x, type=musicscore_pb2.StaffSystem.Bar.STANDARD_BAR)
          for x in barline_xs)

      system_start = system_end + 1