def apply()

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


  def apply(self, page):
    """Adds beams that intersect with note stems to the page.

    Beams should intersect with two or more stems. Beams are currently
    implemented as a bounding box, so we just see whether that box intersects
    with each stem.

    Args:
      page: A Page message.

    Returns:
      The same page, with `beam`s added to the `Glyph`s.
    """
    for system in page.system:
      for staff in system.staff:
        # Extend the beams by the staffline distance on either side. Beams may
        # end immediately at a stem, so give an extra allowance for that stem.
        extended_beams = self.beams.copy()
        extended_beams[:, COLUMNS.X0] -= staff.staffline_distance
        extended_beams[:, COLUMNS.X1] += staff.staffline_distance
        for glyph in staff.glyph:
          if glyph_types.is_beamed_notehead(glyph) and glyph.HasField('stem'):
            xs = [glyph.stem.start.x, glyph.stem.end.x]
            ys = [glyph.stem.start.y, glyph.stem.end.y]
            stem_bounding_box = np.asarray([[min(*xs), min(*ys)],
                                            [max(*xs), max(*ys)]])
            overlapping_beams = _get_overlapping_beams(stem_bounding_box,
                                                       extended_beams)
            glyph.beam.extend(
                musicscore_pb2.LineSegment(
                    start=musicscore_pb2.Point(
                        x=beam[COLUMNS.X0], y=beam[COLUMNS.Y0]),
                    end=musicscore_pb2.Point(
                        x=beam[COLUMNS.X1], y=beam[COLUMNS.Y1]))
                for beam in overlapping_beams)
    return page