(generator)
Generates melodies and saves them as MIDI files.

  Uses the options specified by the flags defined in this module.

  Args:
    generator: The MelodyRnnSequenceGenerator to use for generation.
Generates melodies and saves them as MIDI files.
Generates melodies and saves them as MIDI files .
def run_with_flags(generator):
  """Generates melodies and saves them as MIDI files.

  Uses the options specified by the flags defined in this module.

  Args:
    generator: The MelodyRnnSequenceGenerator to use for generation.
  """
  if not FLAGS.output_dir:
    tf.logging.fatal('--output_dir required')
    return
  FLAGS.output_dir = os.path.expanduser(FLAGS.output_dir)

  primer_midi = None
  if FLAGS.primer_midi:
    primer_midi = os.path.expanduser(FLAGS.primer_midi)

  if not tf.gfile.Exists(FLAGS.output_dir):
    tf.gfile.MakeDirs(FLAGS.output_dir)

  primer_sequence = None
  qpm = FLAGS.qpm if FLAGS.qpm else note_seq.DEFAULT_QUARTERS_PER_MINUTE
  if FLAGS.primer_melody:
    primer_melody = note_seq.Melody(ast.literal_eval(FLAGS.primer_melody))
    primer_sequence = primer_melody.to_sequence(qpm=qpm)
  elif primer_midi:
    primer_sequence = note_seq.midi_file_to_sequence_proto(primer_midi)
    if primer_sequence.tempos and primer_sequence.tempos[0].qpm:
      qpm = primer_sequence.tempos[0].qpm
  else:
    tf.logging.warning(
        'No priming sequence specified. Defaulting to a single middle C.')
    primer_melody = note_seq.Melody([60])
    primer_sequence = primer_melody.to_sequence(qpm=qpm)

  # Derive the total number of seconds to generate based on the QPM of the
  # priming sequence and the num_steps flag.
  seconds_per_step = 60.0 / qpm / generator.steps_per_quarter
  total_seconds = FLAGS.num_steps * seconds_per_step

  # Specify start/stop time for generation based on starting generation at the
  # end of the priming sequence and continuing until the sequence is num_steps
  # long.
  generator_options = generator_pb2.GeneratorOptions()
  if primer_sequence:
    input_sequence = primer_sequence
    # Set the start time to begin on the next step after the last note ends.
    if primer_sequence.notes:
      last_end_time = max(n.end_time for n in primer_sequence.notes)
    else:
      last_end_time = 0
    generate_section = generator_options.generate_sections.add(
        start_time=last_end_time + seconds_per_step,
        end_time=total_seconds)

    if generate_section.start_time >= generate_section.end_time:
      tf.logging.fatal(
          'Priming sequence is longer than the total number of steps '
          'requested: Priming sequence length: %s, Generation length '
          'requested: %s',
          generate_section.start_time, total_seconds)
      return
  else:
    input_sequence = music_pb2.NoteSequence()
    input_sequence.tempos.add().qpm = qpm
    generate_section = generator_options.generate_sections.add(
        start_time=0,
        end_time=total_seconds)
  generator_options.args['temperature'].float_value = FLAGS.temperature
  generator_options.args['beam_size'].int_value = FLAGS.beam_size
  generator_options.args['branch_factor'].int_value = FLAGS.branch_factor
  generator_options.args[
      'steps_per_iteration'].int_value = FLAGS.steps_per_iteration
  tf.logging.debug('input_sequence: %s', input_sequence)
  tf.logging.debug('generator_options: %s', generator_options)

  # Make the generate request num_outputs times and save the output as midi
  # files.
  date_and_time = time.strftime('%Y-%m-%d_%H%M%S')
  digits = len(str(FLAGS.num_outputs))
  for i in range(FLAGS.num_outputs):
    generated_sequence = generator.generate(input_sequence, generator_options)

    midi_filename = '%s_%s.mid' % (date_and_time, str(i + 1).zfill(digits))
    midi_path = os.path.join(FLAGS.output_dir, midi_filename)
    note_seq.sequence_proto_to_midi_file(generated_sequence, midi_path)

  tf.logging.info('Wrote %d MIDI files to %s',
                  FLAGS.num_outputs, FLAGS.output_dir)
def run_with_flags ( generator ) : if not FLAGS . output_dir : tf . logging . fatal ( '--output_dir required' ) return FLAGS . output_dir = os . path . expanduser ( FLAGS . output_dir ) primer_midi = None if FLAGS . primer_midi : primer_midi = os . path . expanduser ( FLAGS . primer_midi ) if not tf . gfile . Exists ( FLAGS . output_dir ) : tf . gfile . MakeDirs ( FLAGS . output_dir ) primer_sequence = None qpm = FLAGS . qpm if FLAGS . qpm else note_seq . DEFAULT_QUARTERS_PER_MINUTE if FLAGS . primer_melody : primer_melody = note_seq . Melody ( ast . literal_eval ( FLAGS . primer_melody ) ) primer_sequence = primer_melody . to_sequence ( qpm = qpm ) elif primer_midi : primer_sequence = note_seq . midi_file_to_sequence_proto ( primer_midi ) if primer_sequence . tempos and primer_sequence . tempos [ 0 ] . qpm : qpm = primer_sequence . tempos [ 0 ] . qpm else : tf . logging . warning ( 'No priming sequence specified. Defaulting to a single middle C.' ) primer_melody = note_seq . Melody ( [ 60 ] ) primer_sequence = primer_melody . to_sequence ( qpm = qpm ) # Derive the total number of seconds to generate based on the QPM of the # priming sequence and the num_steps flag. seconds_per_step = 60.0 / qpm / generator . steps_per_quarter total_seconds = FLAGS . num_steps * seconds_per_step # Specify start/stop time for generation based on starting generation at the # end of the priming sequence and continuing until the sequence is num_steps # long. generator_options = generator_pb2 . GeneratorOptions ( ) if primer_sequence : input_sequence = primer_sequence # Set the start time to begin on the next step after the last note ends. if primer_sequence . notes : last_end_time = max ( n . end_time for n in primer_sequence . notes ) else : last_end_time = 0 generate_section = generator_options . generate_sections . add ( start_time = last_end_time + seconds_per_step , end_time = total_seconds ) if generate_section . start_time >= generate_section . end_time : tf . logging . fatal ( 'Priming sequence is longer than the total number of steps ' 'requested: Priming sequence length: %s, Generation length ' 'requested: %s' , generate_section . start_time , total_seconds ) return else : input_sequence = music_pb2 . NoteSequence ( ) input_sequence . tempos . add ( ) . qpm = qpm generate_section = generator_options . generate_sections . add ( start_time = 0 , end_time = total_seconds ) generator_options . args [ 'temperature' ] . float_value = FLAGS . temperature generator_options . args [ 'beam_size' ] . int_value = FLAGS . beam_size generator_options . args [ 'branch_factor' ] . int_value = FLAGS . branch_factor generator_options . args [ 'steps_per_iteration' ] . int_value = FLAGS . steps_per_iteration tf . logging . debug ( 'input_sequence: %s' , input_sequence ) tf . logging . debug ( 'generator_options: %s' , generator_options ) # Make the generate request num_outputs times and save the output as midi # files. date_and_time = time . strftime ( '%Y-%m-%d_%H%M%S' ) digits = len ( str ( FLAGS . num_outputs ) ) for i in range ( FLAGS . num_outputs ) : generated_sequence = generator . generate ( input_sequence , generator_options ) midi_filename = '%s_%s.mid' % ( date_and_time , str ( i + 1 ) . zfill ( digits ) ) midi_path = os . path . join ( FLAGS . output_dir , midi_filename ) note_seq . sequence_proto_to_midi_file ( generated_sequence , midi_path ) tf . logging . info ( 'Wrote %d MIDI files to %s' , FLAGS . num_outputs , FLAGS . output_dir )