Exemple #1
0
  def decode(self, ids, strip_extraneous=False):
    """Transform a sequence of event indices into a performance MIDI file.

    Args:
      ids: List of performance event indices.
      strip_extraneous: Whether to strip EOS and padding from the end of `ids`.

    Returns:
      Path to the temporary file where the MIDI was saved.
    """
    if strip_extraneous:
      ids = text_encoder.strip_ids(ids, list(range(self.num_reserved_ids)))

    performance = magenta.music.Performance(
        quantized_sequence=None,
        steps_per_second=self._steps_per_second,
        num_velocity_bins=self._num_velocity_bins)
    for i in ids:
      performance.append(self._encoding.decode_event(i - self.num_reserved_ids))

    ns = performance.to_sequence()

    _, tmp_file_path = tempfile.mkstemp('_decode.mid')
    magenta.music.sequence_proto_to_midi_file(ns, tmp_file_path)

    return tmp_file_path
Exemple #2
0
    def decode_to_note_sequence(self, ids, strip_extraneous=False):
        """Transform a sequence of event indices into a performance NoteSequence.

    Args:
      ids: List of performance event indices.
      strip_extraneous: Whether to strip EOS and padding from the end of `ids`.

    Returns:
      A NoteSequence.
    """
        if strip_extraneous:
            ids = text_encoder.strip_ids(ids,
                                         list(range(self.num_reserved_ids)))

        # Decode indices corresponding to event n-grams back into the n-grams.
        event_ids = []
        for i in ids:
            if i >= self.unigram_vocab_size:
                event_ids += self._ngrams[i - self.unigram_vocab_size]
            else:
                event_ids.append(i)

        performance = note_seq.Performance(
            quantized_sequence=None,
            steps_per_second=self._steps_per_second,
            num_velocity_bins=self._num_velocity_bins)
        for i in event_ids:
            performance.append(
                self._encoding.decode_event(i - self.num_reserved_ids))

        ns = performance.to_sequence()

        return ns
  def decode(self, ids, strip_extraneous=False):
    """Transform a sequence of event indices into a performance MIDI file.

    Args:
      ids: List of performance event indices.
      strip_extraneous: Whether to strip EOS and padding from the end of `ids`.

    Returns:
      Path to the temporary file where the MIDI was saved.
    """
    if strip_extraneous:
      ids = text_encoder.strip_ids(ids, list(range(self.num_reserved_ids)))

    # Decode indices corresponding to event n-grams back into the n-grams.
    event_ids = []
    for i in ids:
      if i >= self.unigram_vocab_size:
        event_ids += self._ngrams[i - self.unigram_vocab_size]
      else:
        event_ids.append(i)

    performance = magenta.music.Performance(
        quantized_sequence=None,
        steps_per_second=self._steps_per_second,
        num_velocity_bins=self._num_velocity_bins)
    for i in event_ids:
      performance.append(self._encoding.decode_event(i - self.num_reserved_ids))

    ns = performance.to_sequence()

    _, tmp_file_path = tempfile.mkstemp('_decode.mid')
    magenta.music.sequence_proto_to_midi_file(ns, tmp_file_path)

    return tmp_file_path
Exemple #4
0
  def decode(self, ids, strip_extraneous=False):
    """Transform a sequence of event indices into a performance MIDI file.

    Args:
      ids: List of performance event indices.
      strip_extraneous: Whether to strip EOS and padding from the end of `ids`.

    Returns:
      Path to the temporary file where the MIDI was saved.
    """
    if strip_extraneous:
      ids = text_encoder.strip_ids(ids, list(range(self.num_reserved_ids)))

    # Decode indices corresponding to event n-grams back into the n-grams.
    event_ids = []
    for i in ids:
      if i >= self.unigram_vocab_size:
        event_ids += self._ngrams[i - self.unigram_vocab_size]
      else:
        event_ids.append(i)

    performance = note_seq.Performance(
        quantized_sequence=None,
        steps_per_second=self._steps_per_second,
        num_velocity_bins=self._num_velocity_bins)
    for i in event_ids:
      performance.append(self._encoding.decode_event(i - self.num_reserved_ids))

    ns = performance.to_sequence()

    _, tmp_file_path = tempfile.mkstemp('_decode.mid')
    note_seq.sequence_proto_to_midi_file(ns, tmp_file_path)

    return tmp_file_path
Exemple #5
0
 def decode(self, ids, strip_extraneous=False):
     """Converts a sequence of subtoken ids to a native string.
     Args:
         ids: a list of integers in the range [0, vocab_size)
         strip_extraneous: bool, whether to strip off extraneous tokens
         (EOS and PAD).
     Returns:
         a native string
     """
     if strip_extraneous:
         ids = strip_ids(ids, list(range(self._num_reserved_ids or 0)))
     if isinstance(ids, list):
         return self.sp.DecodeIds(list(map(lambda x: int(x), ids)))
     return self.sp.DecodeIds(ids.tolist())
Exemple #6
0
 def decode(self, ids, strip_extraneous=False):
     if strip_extraneous:
         ids = strip_ids(ids, list(range(self._num_reserved_ids or 0)))
     substrings = [self.all_symbols[idx] for idx in ids]
     concat = "".join(substrings)
     split = concat.split(EOW)
     tokens = []
     for subtoken in split:
         if subtoken:
             unescaped_token = remove_meta_symbols(subtoken)
             if unescaped_token:
                 tokens.append(unescaped_token)
     detokenized = t2t_tokenizer.decode(tokens)
     return detokenized