class_docstr: Convert between performance event indices and (filenames of) MIDI files.
class_docstr_tok: ["Convert","between","performance","event","indices","and","(","filenames","of",")","MIDI","files","."]
docstr: Initialize a MidiPerformanceEncoder object.
  
  Encodes MIDI using a performance event encoding. Index 0 is unused as it is
  reserved for padding. Index 1 is unused unless `add_eos` is True, in which
  case it is appended to all encoded performances.
  
  If `ngrams` is specified, vocabulary is augmented with a set of n-grams over
  the original performance event vocabulary. When encoding, these n-grams will
  be replaced with new event indices. When decoding, the new indices will be
  expanded back into the original n-grams.
  
  No actual encoder interface is defined in Tensor2Tensor, but this class
  contains the same functions as TextEncoder, ImageEncoder, and AudioEncoder.
  
  Args:
    steps_per_second: Number of steps per second at which to quantize. Also
        used to determine number of time shift events (up to one second).
    num_velocity_bins: Number of quantized velocity bins to use.
    min_pitch: Minimum MIDI pitch to encode.
    max_pitch: Maximum MIDI pitch to encode (inclusive).
    add_eos: Whether or not to add an EOS event to the end of encoded
        performances.
    ngrams: Optional list of performance event n-grams (tuples) to be
        represented by new indices. N-grams must have length at least 2 and
        should be pre-offset by the number of reserved IDs.
  
  Raises:
    ValueError: If any n-gram has length less than 2, or contains one of the
        reserved IDs.
code: def __init__(
      self,
      steps_per_second,
      num_velocity_bins,
      min_pitch,
      max_pitch,
      add_eos=False,
      ngrams=None,
  ):
      """Initialize a MidiPerformanceEncoder object.
  
      Encodes MIDI using a performance event encoding. Index 0 is unused as it is
      reserved for padding. Index 1 is unused unless `add_eos` is True, in which
      case it is appended to all encoded performances.
  
      If `ngrams` is specified, vocabulary is augmented with a set of n-grams over
      the original performance event vocabulary. When encoding, these n-grams will
      be replaced with new event indices. When decoding, the new indices will be
      expanded back into the original n-grams.
  
      No actual encoder interface is defined in Tensor2Tensor, but this class
      contains the same functions as TextEncoder, ImageEncoder, and AudioEncoder.
  
      Args:
        steps_per_second: Number of steps per second at which to quantize. Also
            used to determine number of time shift events (up to one second).
        num_velocity_bins: Number of quantized velocity bins to use.
        min_pitch: Minimum MIDI pitch to encode.
        max_pitch: Maximum MIDI pitch to encode (inclusive).
        add_eos: Whether or not to add an EOS event to the end of encoded
            performances.
        ngrams: Optional list of performance event n-grams (tuples) to be
            represented by new indices. N-grams must have length at least 2 and
            should be pre-offset by the number of reserved IDs.
  
      Raises:
        ValueError: If any n-gram has length less than 2, or contains one of the
            reserved IDs.
      """
      self._steps_per_second = steps_per_second
      self._num_velocity_bins = num_velocity_bins
      self._add_eos = add_eos
      self._ngrams = ngrams or []
  
      for ngram in self._ngrams:
          if len(ngram) < 2:
              raise ValueError(
                  "All n-grams must have length at least 2."
              )
          if any(i < self.num_reserved_ids for i in ngram):
              raise ValueError("N-grams cannot contain reserved IDs.")
  
      self._encoding = note_seq.PerformanceOneHotEncoding(
          num_velocity_bins=num_velocity_bins,
          max_shift_steps=steps_per_second,
          min_pitch=min_pitch,
          max_pitch=max_pitch,
      )
  
      # Create a trie mapping n-grams to new indices.
      ngram_ids = range(
          self.unigram_vocab_size,
          self.unigram_vocab_size + len(self._ngrams),
      )
      self._ngrams_trie = pygtrie.Trie(zip(self._ngrams, ngram_ids))
  
      # Also add all unigrams to the trie.
      self._ngrams_trie.update(
          zip(
              [(i,) for i in range(self.unigram_vocab_size)],
              range(self.unigram_vocab_size),
          )
      )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import tempfile
  import note_seq
  from note_seq import performance_lib
  import pygtrie
  from tensor2tensor.data_generators import text_encoder
label_desc: Use image node_id 1 to create a new MidiPerformanceEncoder object from inherited base classes: object with example: obj = MidiPerformanceEncoder(steps_per_second, num_velocity_bins, min_pitch, max_pitch, add_eos, ngrams)