class_docstr: Encodes list/array format piece into pianorolls and decodes into midi.
docstr: Encodes midi into pianorolls according to requested_shape.
docstr_tok: ["Encodes","midi","into","pianorolls","according","to","requested_shape","."]
code: def encode_midi_to_pianoroll(self, midi, requested_shape):
      """Encodes midi into pianorolls according to requested_shape."""
      # TODO(annahuang): Generalize to not requiring a requested shape.
      # TODO(annahuang): Assign instruments to SATB according to range of notes.
      bb, tt, pp, ii = requested_shape
      if not midi.instruments:
          return np.zeros(requested_shape)
      elif len(midi.instruments) > ii:
          raise ValueError(
              "Max number of instruments allowed %d < %d given." % ii,
              (len(midi.instruments)),
          )
      (
          unused_tempo_change_times,
          tempo_changes,
      ) = midi.get_tempo_changes()
      assert len(tempo_changes) == 1
  
      tf.logging.debug("# of instr %d", len(midi.instruments))
      # Encode each instrument separately.
      instr_rolls = [
          self.get_instr_pianoroll(instr, requested_shape)
          for instr in midi.instruments
      ]
      if len(instr_rolls) != ii:
          for unused_i in range(ii - len(instr_rolls)):
              instr_rolls.append(np.zeros_like(instr_rolls[0]))
  
      max_tt = np.max([roll.shape[0] for roll in instr_rolls])
      if tt < max_tt:
          tf.logging.warning(
              "WARNING: input midi is a longer sequence then the requested"
              "size (%d > %d)",
              max_tt,
              tt,
          )
      elif max_tt < tt:
          max_tt = tt
      pianorolls = np.zeros((bb, max_tt, pp, ii))
      for i, roll in enumerate(instr_rolls):
          pianorolls[:, : roll.shape[0], :, i] = np.tile(
              roll[:, :], (bb, 1, 1)
          )
      tf.logging.debug("Requested roll shape: %s", requested_shape)
      tf.logging.debug(
          "Roll argmax: %s",
          np.argmax(pianorolls, axis=2) + self.min_pitch,
      )
      return pianorolls
  
file_path: magenta/magenta/models/coconet/lib_pianoroll.py
imports: import numpy
  import pretty_midi
  import tensorflow.compat.v1
label_desc: Use image node_id 6 for calling the PianorollEncoderDecoder obj's underlying member method code with example usage: obj.encode_midi_to_pianoroll(midi, requested_shape) and returns: pianorolls, np