(self, note_sequence)
return ConverterTensors(inputs=seqs, outputs=seqs, controls=control_seqs)
Converts a `NoteSequence` to `ConverterTensors` obj.
Converts a `NoteSequence` to `ConverterTensors` obj.
Converts a NoteSequence to ConverterTensors obj .
def _to_tensors_fn(self, note_sequence):
    """Converts a `NoteSequence` to `ConverterTensors` obj."""
    try:
      quantized_sequence = note_seq.quantize_note_sequence(
          note_sequence, self._steps_per_quarter)
      if (note_seq.steps_per_bar_in_quantized_sequence(quantized_sequence) !=
          self._steps_per_bar):
        return ConverterTensors()
    except (note_seq.BadTimeSignatureError, note_seq.NonIntegerStepsPerBarError,
            note_seq.NegativeTimeError):
      return ConverterTensors()

    if (self._chord_encoding and not any(
        ta.annotation_type == CHORD_SYMBOL
        for ta in quantized_sequence.text_annotations)) or (
            self._condition_on_key and not quantized_sequence.key_signatures):
      # We are conditioning on chords and/or key but sequence does not have
      # them. Try to infer chords and optionally key.
      try:
        note_seq.infer_chords_for_sequence(
            quantized_sequence, add_key_signatures=self._condition_on_key)
      except note_seq.ChordInferenceError:
        return ConverterTensors()

      # The trio parts get extracted from the original NoteSequence, so copy the
      # inferred chords and keys back to that one.
      for qta in quantized_sequence.text_annotations:
        if qta.annotation_type == CHORD_SYMBOL:
          ta = note_sequence.text_annotations.add()
          ta.annotation_type = CHORD_SYMBOL
          ta.time = qta.time
          ta.text = qta.text
      for qks in quantized_sequence.key_signatures:
        ks = note_sequence.key_signatures.add()
        ks.time = qks.time
        ks.key = qks.key

    total_bars = int(
        np.ceil(quantized_sequence.total_quantized_steps / self._steps_per_bar))
    total_bars = min(total_bars, self._max_bars)

    # Assign an instrument class for each instrument, and compute its coverage.
    # If an instrument has multiple classes, it is considered INVALID.
    instrument_type = np.zeros(MAX_INSTRUMENT_NUMBER + 1, np.uint8)
    coverage = np.zeros((total_bars, MAX_INSTRUMENT_NUMBER + 1), np.bool)
    for note in quantized_sequence.notes:
      i = note.instrument
      if i > MAX_INSTRUMENT_NUMBER:
        tf.logging.warning('Skipping invalid instrument number: %d', i)
        continue
      if note.is_drum:
        inferred_type = self.InstrumentType.DRUMS
      else:
        inferred_type = self._program_map.get(
            note.program, self.InstrumentType.INVALID)
      if not instrument_type[i]:
        instrument_type[i] = inferred_type
      elif instrument_type[i] != inferred_type:
        instrument_type[i] = self.InstrumentType.INVALID

      start_bar = note.quantized_start_step // self._steps_per_bar
      end_bar = int(np.ceil(note.quantized_end_step / self._steps_per_bar))

      if start_bar >= total_bars:
        continue
      coverage[start_bar:min(end_bar, total_bars), i] = True

    # Group instruments by type.
    instruments_by_type = collections.defaultdict(list)
    for i, type_ in enumerate(instrument_type):
      if type_ not in (self.InstrumentType.UNK, self.InstrumentType.INVALID):
        instruments_by_type[type_].append(i)
    if len(instruments_by_type) < 3:
      # This NoteSequence doesn't have all 3 types.
      return ConverterTensors()

    # Encode individual instruments.
    # Set total time so that instruments will be padded correctly.
    note_sequence.total_time = (
        total_bars * self._steps_per_bar *
        60 / note_sequence.tempos[0].qpm / self._steps_per_quarter)
    encoded_instruments = {}
    encoded_controls = None
    for i in (instruments_by_type[self.InstrumentType.MEL] +
              instruments_by_type[self.InstrumentType.BASS]):
      tensors = self._melody_converter.to_tensors(
          _extract_instrument(note_sequence, i))
      if tensors.outputs:
        encoded_instruments[i] = tensors.outputs[0]
        if encoded_controls is None:
          encoded_controls = tensors.controls[0]
        elif not np.array_equal(encoded_controls, tensors.controls[0]):
          tf.logging.warning('Trio controls disagreement between instruments.')
      else:
        coverage[:, i] = False
    for i in instruments_by_type[self.InstrumentType.DRUMS]:
      tensors = self._drums_converter.to_tensors(
          _extract_instrument(note_sequence, i))
      if tensors.outputs:
        encoded_instruments[i] = tensors.outputs[0]
      else:
        coverage[:, i] = False

    # Fill in coverage gaps up to self._gap_bars.
    og_coverage = coverage.copy()
    for j in range(total_bars):
      coverage[j] = np.any(
          og_coverage[
              max(0, j-self._gap_bars):min(total_bars, j+self._gap_bars) + 1],
          axis=0)

    # Take cross product of instruments from each class and compute combined
    # encodings where they overlap.
    seqs = []
    control_seqs = []
    for grp in itertools.product(
        instruments_by_type[self.InstrumentType.MEL],
        instruments_by_type[self.InstrumentType.BASS],
        instruments_by_type[self.InstrumentType.DRUMS]):
      # Consider an instrument covered within gap_bars from the end if any of
      # the other instruments are. This allows more leniency when re-encoding
      # slices.
      grp_coverage = np.all(coverage[:, grp], axis=1)
      grp_coverage[:self._gap_bars] = np.any(coverage[:self._gap_bars, grp])
      grp_coverage[-self._gap_bars:] = np.any(coverage[-self._gap_bars:, grp])
      for j in range(total_bars - self._slice_bars + 1):
        if (np.all(grp_coverage[j:j + self._slice_bars]) and
            all(i in encoded_instruments for i in grp)):
          start_step = j * self._steps_per_bar
          end_step = (j + self._slice_bars) * self._steps_per_bar
          seqs.append(np.concatenate(
              [encoded_instruments[i][start_step:end_step] for i in grp],
              axis=-1))
          if encoded_controls is not None:
            control_seqs.append(encoded_controls[start_step:end_step])

    return ConverterTensors(inputs=seqs, outputs=seqs, controls=control_seqs)
def _to_tensors_fn ( self , note_sequence ) : try : quantized_sequence = note_seq . quantize_note_sequence ( note_sequence , self . _steps_per_quarter ) if ( note_seq . steps_per_bar_in_quantized_sequence ( quantized_sequence ) != self . _steps_per_bar ) : return ConverterTensors ( ) except ( note_seq . BadTimeSignatureError , note_seq . NonIntegerStepsPerBarError , note_seq . NegativeTimeError ) : return ConverterTensors ( ) if ( self . _chord_encoding and not any ( ta . annotation_type == CHORD_SYMBOL for ta in quantized_sequence . text_annotations ) ) or ( self . _condition_on_key and not quantized_sequence . key_signatures ) : # We are conditioning on chords and/or key but sequence does not have # them. Try to infer chords and optionally key. try : note_seq . infer_chords_for_sequence ( quantized_sequence , add_key_signatures = self . _condition_on_key ) except note_seq . ChordInferenceError : return ConverterTensors ( ) # The trio parts get extracted from the original NoteSequence, so copy the # inferred chords and keys back to that one. for qta in quantized_sequence . text_annotations : if qta . annotation_type == CHORD_SYMBOL : ta = note_sequence . text_annotations . add ( ) ta . annotation_type = CHORD_SYMBOL ta . time = qta . time ta . text = qta . text for qks in quantized_sequence . key_signatures : ks = note_sequence . key_signatures . add ( ) ks . time = qks . time ks . key = qks . key total_bars = int ( np . ceil ( quantized_sequence . total_quantized_steps / self . _steps_per_bar ) ) total_bars = min ( total_bars , self . _max_bars ) # Assign an instrument class for each instrument, and compute its coverage. # If an instrument has multiple classes, it is considered INVALID. instrument_type = np . zeros ( MAX_INSTRUMENT_NUMBER + 1 , np . uint8 ) coverage = np . zeros ( ( total_bars , MAX_INSTRUMENT_NUMBER + 1 ) , np . bool ) for note in quantized_sequence . notes : i = note . instrument if i > MAX_INSTRUMENT_NUMBER : tf . logging . warning ( 'Skipping invalid instrument number: %d' , i ) continue if note . is_drum : inferred_type = self . InstrumentType . DRUMS else : inferred_type = self . _program_map . get ( note . program , self . InstrumentType . INVALID ) if not instrument_type [ i ] : instrument_type [ i ] = inferred_type elif instrument_type [ i ] != inferred_type : instrument_type [ i ] = self . InstrumentType . INVALID start_bar = note . quantized_start_step // self . _steps_per_bar end_bar = int ( np . ceil ( note . quantized_end_step / self . _steps_per_bar ) ) if start_bar >= total_bars : continue coverage [ start_bar : min ( end_bar , total_bars ) , i ] = True # Group instruments by type. instruments_by_type = collections . defaultdict ( list ) for i , type_ in enumerate ( instrument_type ) : if type_ not in ( self . InstrumentType . UNK , self . InstrumentType . INVALID ) : instruments_by_type [ type_ ] . append ( i ) if len ( instruments_by_type ) < 3 : # This NoteSequence doesn't have all 3 types. return ConverterTensors ( ) # Encode individual instruments. # Set total time so that instruments will be padded correctly. note_sequence . total_time = ( total_bars * self . _steps_per_bar * 60 / note_sequence . tempos [ 0 ] . qpm / self . _steps_per_quarter ) encoded_instruments = { } encoded_controls = None for i in ( instruments_by_type [ self . InstrumentType . MEL ] + instruments_by_type [ self . InstrumentType . BASS ] ) : tensors = self . _melody_converter . to_tensors ( _extract_instrument ( note_sequence , i ) ) if tensors . outputs : encoded_instruments [ i ] = tensors . outputs [ 0 ] if encoded_controls is None : encoded_controls = tensors . controls [ 0 ] elif not np . array_equal ( encoded_controls , tensors . controls [ 0 ] ) : tf . logging . warning ( 'Trio controls disagreement between instruments.' ) else : coverage [ : , i ] = False for i in instruments_by_type [ self . InstrumentType . DRUMS ] : tensors = self . _drums_converter . to_tensors ( _extract_instrument ( note_sequence , i ) ) if tensors . outputs : encoded_instruments [ i ] = tensors . outputs [ 0 ] else : coverage [ : , i ] = False # Fill in coverage gaps up to self._gap_bars. og_coverage = coverage . copy ( ) for j in range ( total_bars ) : coverage [ j ] = np . any ( og_coverage [ max ( 0 , j - self . _gap_bars ) : min ( total_bars , j + self . _gap_bars ) + 1 ] , axis = 0 ) # Take cross product of instruments from each class and compute combined # encodings where they overlap. seqs = [ ] control_seqs = [ ] for grp in itertools . product ( instruments_by_type [ self . InstrumentType . MEL ] , instruments_by_type [ self . InstrumentType . BASS ] , instruments_by_type [ self . InstrumentType . DRUMS ] ) : # Consider an instrument covered within gap_bars from the end if any of # the other instruments are. This allows more leniency when re-encoding # slices. grp_coverage = np . all ( coverage [ : , grp ] , axis = 1 ) grp_coverage [ : self . _gap_bars ] = np . any ( coverage [ : self . _gap_bars , grp ] ) grp_coverage [ - self . _gap_bars : ] = np . any ( coverage [ - self . _gap_bars : , grp ] ) for j in range ( total_bars - self . _slice_bars + 1 ) : if ( np . all ( grp_coverage [ j : j + self . _slice_bars ] ) and all ( i in encoded_instruments for i in grp ) ) : start_step = j * self . _steps_per_bar end_step = ( j + self . _slice_bars ) * self . _steps_per_bar seqs . append ( np . concatenate ( [ encoded_instruments [ i ] [ start_step : end_step ] for i in grp ] , axis = - 1 ) ) if encoded_controls is not None : control_seqs . append ( encoded_controls [ start_step : end_step ] ) return ConverterTensors ( inputs = seqs , outputs = seqs , controls = control_seqs )