def main(unused_argv): absl.flags.FLAGS.alsologtostderr = True # Load the model flags = lib_flags.Flags( { 'batch_size_schedule': [FLAGS.batch_size], 'tfds_data_dir': FLAGS.tfds_data_dir }) model = lib_model.Model.load_from_path(FLAGS.ckpt_dir, flags) # Make an output directory if it doesn't exist output_dir = util.expand_path(FLAGS.output_dir) if not tf.gfile.Exists(output_dir): tf.gfile.MakeDirs(output_dir) if FLAGS.midi_file: # If a MIDI file is provided, synthesize interpolations across the clip unused_ns, notes = gu.load_midi(FLAGS.midi_file) # Distribute latent vectors linearly in time z_instruments, t_instruments = gu.get_random_instruments( model, notes['end_times'][-1], secs_per_instrument=FLAGS.secs_per_instrument) # Get latent vectors for each note z_notes = gu.get_z_notes(notes['start_times'], z_instruments, t_instruments) # Generate audio for each note print('Generating {} samples...'.format(len(z_notes))) audio_notes = model.generate_samples_from_z(z_notes, notes['pitches']) # Make a single audio clip audio_clip = gu.combine_notes(audio_notes, notes['start_times'], notes['end_times'], notes['velocities']) # Write the wave files fname = os.path.join(output_dir, 'generated_clip.wav') gu.save_wav(audio_clip, fname) else: # Otherwise, just generate a batch of random sounds waves = model.generate_samples(FLAGS.batch_size) # Write the wave files for i in range(len(waves)): fname = os.path.join(output_dir, 'generated_{}.wav'.format(i)) gu.save_wav(waves[i], fname)
def main(unused_argv): absl.flags.FLAGS.alsologtostderr = True # Load the model flags = lib_flags.Flags({'batch_size_schedule': [FLAGS.batch_size]}) model = lib_model.Model.load_from_path(FLAGS.ckpt_dir, flags) # Make an output directory if it doesn't exist output_dir = util.expand_path(FLAGS.output_dir) if not tf.gfile.Exists(output_dir): tf.gfile.MakeDirs(output_dir) if FLAGS.midi_file: # If a MIDI file is provided, synthesize interpolations across the clip unused_ns, notes = gu.load_midi(FLAGS.midi_file) # Distribute latent vectors linearly in time z_instruments, t_instruments = gu.get_random_instruments( model, notes['end_times'][-1], secs_per_instrument=FLAGS.secs_per_instrument) # Get latent vectors for each note z_notes = gu.get_z_notes(notes['start_times'], z_instruments, t_instruments) # Generate audio for each note print('Generating {} samples...'.format(len(z_notes))) audio_notes = model.generate_samples_from_z(z_notes, notes['pitches']) # Make a single audio clip audio_clip = gu.combine_notes(audio_notes, notes['start_times'], notes['end_times'], notes['velocities']) # Write the wave files fname = os.path.join(output_dir, 'generated_clip.wav') gu.save_wav(audio_clip, fname) else: # Otherwise, just generate a batch of random sounds waves = model.generate_samples(FLAGS.batch_size) # Write the wave files for i in range(len(waves)): fname = os.path.join(output_dir, 'generated_{}.wav'.format(i)) gu.save_wav(waves[i], fname)
def generate_audio(notes: dict, seconds_per_instrument: int = 5, batch_size: int = 16, checkpoint_dir: str = "checkpoints/acoustic_only") \ -> np.ndarray: """ Generates an audio clip from the notes information dictionary, by randomly sampling "instruments" from the latent space (sounds of a given time), and generating samples from them. :param notes: the notes dictionary, must come from magenta.models.gansynth.lib.generate_util.load_midi :param seconds_per_instrument: the number of seconds for each instrument :param batch_size: the batch size for the model :param checkpoint_dir: the checkpoint folder """ flags = lib_flags.Flags({"batch_size_schedule": [batch_size]}) model = lib_model.Model.load_from_path(checkpoint_dir, flags) # Distribute latent vectors linearly in time z_instruments, t_instruments = get_random_instruments( model, notes["end_times"][-1], secs_per_instrument=seconds_per_instrument) # Get latent vectors for each note z_notes = get_z_notes(notes["start_times"], z_instruments, t_instruments) # Generate audio for each note audio_notes = model.generate_samples_from_z(z_notes, notes["pitches"]) # Make a single audio clip audio_clip = combine_notes(audio_notes, notes["start_times"], notes["end_times"], notes["velocities"]) return audio_clip
def main(unused_argv): absl.flags.FLAGS.alsologtostderr = True # Load the model flags = lib_flags.Flags({ 'batch_size_schedule': [FLAGS.batch_size], **({ 'tfds_data_dir': FLAGS.tfds_data_dir } if FLAGS.tfds_data_dir else {}) }) model = lib_model.Model.load_from_path(FLAGS.ckpt_dir, flags) # Make an output directory if it doesn't exist output_dir = util.expand_path(FLAGS.output_dir) if not tf.gfile.Exists(output_dir): tf.gfile.MakeDirs(output_dir) if FLAGS.seed != None: np.random.seed(seed=FLAGS.seed) tf.random.set_random_seed(FLAGS.seed) layer_offsets = {} if FLAGS.edits_file: with open(FLAGS.edits_file, "rb") as fp: edits_dict = pickle.load(fp) assert "layer" in edits_dict assert "comp" in edits_dict directions = edits_dict["comp"] amounts = np.zeros(edits_dict["comp"].shape[:1], dtype=np.float32) amounts[:len(list(map(float, FLAGS.edits)))] = FLAGS.edits scaled_directions = amounts.reshape(-1, 1, 1, 1) * directions linear_combination = np.sum(scaled_directions, axis=0) linear_combination_batch = np.repeat(linear_combination.reshape( 1, *linear_combination.shape), FLAGS.batch_size, axis=0) layer_offsets[edits_dict["layer"]] = linear_combination_batch if FLAGS.midi_file: # If a MIDI file is provided, synthesize interpolations across the clip unused_ns, notes = gu.load_midi(FLAGS.midi_file) # Distribute latent vectors linearly in time z_instruments, t_instruments = gu.get_random_instruments( model, notes['end_times'][-1], secs_per_instrument=FLAGS.secs_per_instrument) # Get latent vectors for each note z_notes = gu.get_z_notes(notes['start_times'], z_instruments, t_instruments) # Generate audio for each note print('Generating {} samples...'.format(len(z_notes))) audio_notes = model.generate_samples_from_z( z_notes, notes['pitches'], layer_offsets=layer_offsets) # Make a single audio clip audio_clip = gu.combine_notes(audio_notes, notes['start_times'], notes['end_times'], notes['velocities']) # Write the wave files fname = os.path.join(output_dir, 'generated_clip.wav') gu.save_wav(audio_clip, fname) else: # Otherwise, just generate a batch of random sounds waves = model.generate_samples(FLAGS.batch_size, pitch=FLAGS.pitch, layer_offsets=layer_offsets) # Write the wave files for i in range(len(waves)): fname = os.path.join(output_dir, 'generated_{}.wav'.format(i)) gu.save_wav(waves[i], fname)
def main(unused_argv): absl.flags.FLAGS.alsologtostderr = True # Load the model flags = lib_flags.Flags({'batch_size_schedule': [FLAGS.batch_size]}) model = lib_model.Model.load_from_path(FLAGS.ckpt_dir, flags) # Make an output directory if it doesn't exist output_dir = util.expand_path(FLAGS.output_dir) if not tf.gfile.Exists(output_dir): tf.gfile.MakeDirs(output_dir) if FLAGS.midi_file: # If a MIDI file is provided, synthesize interpolations across the clip unused_ns, notes = gu.load_midi(FLAGS.midi_file) # Distribute latent vectors linearly in time z_instruments, t_instruments = gu.get_random_instruments( model, notes['end_times'][-1], secs_per_instrument=FLAGS.secs_per_instrument) # Get latent vectors for each note z_notes = gu.get_z_notes(notes['start_times'], z_instruments, t_instruments) # Generate audio for each note print('Generating {} samples...'.format(len(z_notes))) audio_notes = model.generate_samples_from_z(z_notes, notes['pitches']) # Make a single audio clip audio_clip = gu.combine_notes(audio_notes, notes['start_times'], notes['end_times'], notes['velocities']) # Write the wave files fname = os.path.join(output_dir, 'generated_clip.wav') gu.save_wav(audio_clip, fname) else: # Otherwise, just generate a batch of random sounds # waves = model.generate_samples(FLAGS.batch_size) # original waves, z = model.generate_samples( FLAGS.batch_size, pitch=44 ) #DEBUG: generate on singular pitch (range: 24-84), return latent vectors # Write the wave files for i in range(len(waves)): fname = os.path.join(output_dir, 'generated_{}.wav'.format(i)) gu.save_wav(waves[i], fname) # DEBUG: write z to file for later analysis fname = os.path.join(output_dir, 'z.p') pickle.dump(z, open(fname, 'wb')) # DEBUG: flag samples based on similar latent variables flagged = get_flagged_latents(z, n=10) print("\nflagged (z):") for i in flagged: print(i) # DEBUG: flag samples based on similar waveforms flagged = get_flagged_waves_par(waves, n=10, frac=0.01) print("\nflagged (waves):") for i in flagged: print(i) fname = os.path.join(output_dir, '_sim_{}-{}.wav'.format(i[0][0], i[0][1])) gu.save_wav(np.array(list(waves[i[0][0]]) + list(waves[i[0][1]])), fname)