code: def _inject_melody(
      melody, start_step, encoder_decoder, event_sequences, inputs
  ):
      """A modify_events_callback method for generate_polyphonic_sequence.
  
      Should be called with functools.partial first, to fill in the melody and
      start_step arguments.
  
      Will extend the event sequence using events from the melody argument whenever
      the event sequence gets to a new step.
  
      Args:
        melody: The PolyphonicSequence to use to extend the event sequence.
        start_step: The length of the priming sequence in RNN steps.
        encoder_decoder: Supplied by the callback. The current
            EventSequenceEncoderDecoder.
        event_sequences: Supplied by the callback. The current EventSequence.
        inputs: Supplied by the callback. The current list of encoded events.
      """
      assert len(event_sequences) == len(inputs)
  
      for i in range(len(inputs)):
          event_sequence = event_sequences[i]
          input_ = inputs[i]
  
          # Only modify the event sequence if we're at the start of a new step or this
          # is the first step.
          if not (
              event_sequence[-1].event_type == PolyphonicEvent.STEP_END
              or not event_sequence
              or (
                  event_sequence[-1].event_type == PolyphonicEvent.START
                  and len(event_sequence) == 1
              )
          ):
              continue
  
          # Determine the current step event.
          event_step_count = 0
          for event in event_sequence:
              if event.event_type == PolyphonicEvent.STEP_END:
                  event_step_count += 1
  
          # Find the corresponding event in the input melody.
          melody_step_count = start_step
          for j, event in enumerate(melody):
              if event.event_type == PolyphonicEvent.STEP_END:
                  melody_step_count += 1
              if melody_step_count == event_step_count:
                  melody_pos = j + 1
                  while melody_pos < len(melody) and (
                      melody[melody_pos].event_type
                      != PolyphonicEvent.STEP_END
                  ):
                      event_sequence.append(melody[melody_pos])
                      input_.extend(
                          encoder_decoder.get_inputs_batch(
                              [event_sequence]
                          )[0]
                      )
                      melody_pos += 1
                  break
  
file_path: magenta/magenta/models/polyphony_rnn/polyphony_sequence_generator.py
imports: import copy
  import functools
  from magenta.models.polyphony_rnn import polyphony_lib
  from magenta.models.polyphony_rnn import polyphony_model
  from magenta.models.polyphony_rnn.polyphony_lib import PolyphonicEvent
  from magenta.models.shared import sequence_generator
  import note_seq
  import tensorflow.compat.v1
label_desc: Use image node_id 1 for calling a global function with example usage: _inject_melody(melody, start_step, encoder_decoder, event_sequences, inputs) without return types