(generator)
Generates pianoroll tracks and saves them as MIDI files.

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

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

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

  Args:
    generator: The PianorollRnnNadeSequenceGenerator to use for generation.
  """
  if not FLAGS.output_dir:
    tf.logging.fatal('--output_dir required')
    return
  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(output_dir):
    tf.gfile.MakeDirs(output_dir)

  primer_sequence = None
  qpm = FLAGS.qpm if FLAGS.qpm else 60
  if FLAGS.primer_pitches:
    primer_sequence = music_pb2.NoteSequence()
    primer_sequence.tempos.add().qpm = qpm
    primer_sequence.ticks_per_quarter = note_seq.STANDARD_PPQ
    for pitch in ast.literal_eval(FLAGS.primer_pitches):
      note = primer_sequence.notes.add()
      note.start_time = 0
      note.end_time = 60.0 / qpm
      note.pitch = pitch
      note.velocity = 100
    primer_sequence.total_time = primer_sequence.notes[-1].end_time
  elif FLAGS.primer_pianoroll:
    primer_pianoroll = note_seq.PianorollSequence(
        events_list=ast.literal_eval(FLAGS.primer_pianoroll),
        steps_per_quarter=4,
        shift_range=True)
    primer_sequence = primer_pianoroll.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 empty sequence.')
    primer_sequence = music_pb2.NoteSequence()
    primer_sequence.tempos.add().qpm = qpm
    primer_sequence.ticks_per_quarter = note_seq.STANDARD_PPQ

  # Derive the total number of seconds to generate.
  seconds_per_step = 60.0 / qpm / generator.steps_per_quarter
  generate_end_time = 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()
  # Set the start time to begin when the last note ends.
  generate_section = generator_options.generate_sections.add(
      start_time=primer_sequence.total_time,
      end_time=generate_end_time)

  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, Total length '
        'requested: %s',
        generate_section.start_time, generate_end_time)
    return

  generator_options.args['beam_size'].int_value = FLAGS.beam_size
  generator_options.args['branch_factor'].int_value = FLAGS.branch_factor

  tf.logging.info('primer_sequence: %s', primer_sequence)
  tf.logging.info('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(primer_sequence, generator_options)

    midi_filename = '%s_%s.mid' % (date_and_time, str(i + 1).zfill(digits))
    midi_path = os.path.join(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, output_dir)
def run_with_flags ( generator ) : if not FLAGS . output_dir : tf . logging . fatal ( '--output_dir required' ) return 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 ( output_dir ) : tf . gfile . MakeDirs ( output_dir ) primer_sequence = None qpm = FLAGS . qpm if FLAGS . qpm else 60 if FLAGS . primer_pitches : primer_sequence = music_pb2 . NoteSequence ( ) primer_sequence . tempos . add ( ) . qpm = qpm primer_sequence . ticks_per_quarter = note_seq . STANDARD_PPQ for pitch in ast . literal_eval ( FLAGS . primer_pitches ) : note = primer_sequence . notes . add ( ) note . start_time = 0 note . end_time = 60.0 / qpm note . pitch = pitch note . velocity = 100 primer_sequence . total_time = primer_sequence . notes [ - 1 ] . end_time elif FLAGS . primer_pianoroll : primer_pianoroll = note_seq . PianorollSequence ( events_list = ast . literal_eval ( FLAGS . primer_pianoroll ) , steps_per_quarter = 4 , shift_range = True ) primer_sequence = primer_pianoroll . 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 empty sequence.' ) primer_sequence = music_pb2 . NoteSequence ( ) primer_sequence . tempos . add ( ) . qpm = qpm primer_sequence . ticks_per_quarter = note_seq . STANDARD_PPQ # Derive the total number of seconds to generate. seconds_per_step = 60.0 / qpm / generator . steps_per_quarter generate_end_time = 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 ( ) # Set the start time to begin when the last note ends. generate_section = generator_options . generate_sections . add ( start_time = primer_sequence . total_time , end_time = generate_end_time ) 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, Total length ' 'requested: %s' , generate_section . start_time , generate_end_time ) return generator_options . args [ 'beam_size' ] . int_value = FLAGS . beam_size generator_options . args [ 'branch_factor' ] . int_value = FLAGS . branch_factor tf . logging . info ( 'primer_sequence: %s' , primer_sequence ) tf . logging . info ( '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 ( primer_sequence , generator_options ) midi_filename = '%s_%s.mid' % ( date_and_time , str ( i + 1 ) . zfill ( digits ) ) midi_path = os . path . join ( 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 , output_dir )