Example #1
0
    def convert(self,
                latent_vector,
                cf,
                filename,
                im='hierdec-trio_16bar',
                temperature=0.5,
                length=32):
        """Convert a song from genre X to genre Y
        Args:
            latent vector: latent vector of the song in X
            cf: conversion factor - to what degree should X be converted to Y"""

        new_lv = latent_vector + cf * self.d

        model = self.trio_models[im]
        output_sequence = model.decode(length=length,
                                       z=new_lv,
                                       temperature=temperature)

        final_seq = concatenate_sequences(
            output_sequence
        )  #, [64.0] * len(output_sequence)) ### Change 64 back to 32.0

        self.download(final_seq,
                      './content/output/converted_%s.mid' % filename)
        print 'Convertion successful. converted_%s.mid created.' % filename
Example #2
0
def interpolate(model,
                start_seq,
                end_seq,
                num_steps,
                max_length=32,
                assert_same_length=True,
                temperature=0.5,
                individual_duration=4.0):
    """Interpolates between a start and end sequence."""
    _, mu, _ = model.encode([start_seq, end_seq], assert_same_length)
    z = np.array(
        [slerp(mu[0], mu[1], t) for t in np.linspace(0, 1, num_steps)])
    note_sequences = model.decode(length=max_length,
                                  z=z,
                                  temperature=temperature)

    print 'Start Seq Reconstruction'
    play(note_sequences[0])
    print 'End Seq Reconstruction'
    play(note_sequences[-1])
    print 'Mean Sequence'
    play(note_sequences[num_steps // 2])
    print 'Start -> End Interpolation'
    interp_seq = concatenate_sequences(note_sequences, [individual_duration] *
                                       len(note_sequences))
    play(interp_seq)
    mm.plot_sequence(interp_seq)
    return interp_seq if num_steps > 3 else note_sequences[num_steps // 2]
Example #3
0
def landing():
    form = LandingForm()
    if form.validate_on_submit():
        num_bars = form.num_bars.data
        temperature = form.temperature.data
        config = configs.CONFIG_MAP['hier-multiperf_vel_1bar_med']
        weight_name = 'model_fb256.ckpt'
        model = TrainedModel(config,
                             batch_size=BATCH_SIZE,
                             checkpoint_dir_or_path=os.path.join(
                                 app.root_path, 'content', weight_name))
        model._config.data_converter._max_tensors_per_input = None

        z1 = np.random.normal(size=[Z_SIZE])
        z2 = np.random.normal(size=[Z_SIZE])
        z = np.array([slerp(z1, z2, t) for t in np.linspace(0, 1, num_bars)])

        seqs = model.decode(length=TOTAL_STEPS, z=z, temperature=temperature)

        trim_sequences(seqs)
        fix_instruments_for_concatenation(seqs)
        interp_ns = concatenate_sequences(seqs)
        f_ext = '.mid'
        random_hex = secrets.token_hex(8)
        music_fn = random_hex + f_ext
        music_mp3 = random_hex + ".mp3"
        #Save music to disk
        mm.sequence_proto_to_midi_file(interp_ns, music_fn)

        ### Move audio to a specified path
        source_path = "/home/pratheesh/Flask_Blog/" + music_mp3
        destination_path = os.path.join(app.root_path, 'static/music',
                                        music_mp3)

        cmd_to_wav = "fluidsynth -F " + random_hex + ".wav /usr/share/sounds/sf2/FluidR3_GM.sf2 " + music_fn
        print(cmd_to_wav)
        os.system(cmd_to_wav)
        cmd_to_mp3 = "lame --preset standard " + random_hex + ".wav " + random_hex + ".mp3"
        print(cmd_to_mp3)
        os.system(cmd_to_mp3)
        #shutil.move(source_path, destination_path)
        #os.replace(source_path, destination_path)
        print("moving file")
        os.rename(source_path, destination_path)
        os.remove(music_fn)
        os.remove(random_hex + ".wav")

        music_file = url_for('static', filename='music/' + music_mp3)

        return render_template('music_output.html', music_file=music_file)

    return render_template('landing.html', form=form)
Example #4
0
def decode_vectors(model_file, vectors, concatenate=False):
    temperature = 1.0  #param: min:0.1, max:1.5
    config = configs.CONFIG_MAP['flat-mel_16bar']
    model = TrainedModel(config,
                         batch_size=512,
                         checkpoint_dir_or_path=model_file)
    resulting_midis = model.decode(vectors, length=256)
    if concatenate:
        concatenated_midis = concatenate_sequences(resulting_midis)
        download(concatenated_midis, "concatenated_midi.mid")
        print("created 1 midi.")
    else:
        for i, p in enumerate(resulting_midis):
            download(p, "newly_created_" + str(i) + ".mid")
            print("created " + str(len(resulting_midis)) + " midis")
Example #5
0
  def testConcatenateSequences(self):
    sequence1 = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        sequence1, 0,
        [(60, 100, 0.0, 1.0), (72, 100, 0.5, 1.5)])
    sequence2 = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        sequence2, 0,
        [(59, 100, 0.0, 1.0), (71, 100, 0.5, 1.5)])

    expected_sequence = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        expected_sequence, 0,
        [(60, 100, 0.0, 1.0), (72, 100, 0.5, 1.5),
         (59, 100, 1.5, 2.5), (71, 100, 2.0, 3.0)])

    cat_seq = sequences_lib.concatenate_sequences([sequence1, sequence2])
    self.assertProtoEquals(expected_sequence, cat_seq)
Example #6
0
    def generate(self, empty=False,
                 num_bars=64, temperature=0.5, backup_seq=None,
                 chord_arr=None):
        # Interpolation, Repeating Chord Progression
        if chord_arr is None:
            if backup_seq is not None:
                self.sequence = copy.deepcopy(backup_seq)

            if hasattr(self, 'temperature'):
                temperature = self.temperature
            copy_sequence = copy.deepcopy(self.sequence)

            quantized_sequence = mm.quantize_note_sequence(copy_sequence, 8)
            # infer chords for sequence is a bit more natural
            mm.infer_chords_for_sequence(quantized_sequence)

            chords = []
            for annotation in quantized_sequence.text_annotations:
                if annotation.annotation_type == CHORD_SYMBOL:
                    chord_name = annotation.text
                    chords.append(chord_name)
        else:
            # follow a user defined chord progression
            chords = chord_arr
        mod_val = len(chords)
        z1 = np.random.normal(size=[Z_SIZE])
        z2 = np.random.normal(size=[Z_SIZE])
        z = np.array([self.slerp(z1, z2, t)
                    for t in np.linspace(0, 1, num_bars)])

        seqs = [
            self.model.decode(length=TOTAL_STEPS, z=z[i:i+1, :], temperature=temperature,
                        c_input=self.chord_encoding(chords[i % mod_val]))[0]
            for i in range(num_bars)
        ]

        self.fix_instruments_for_concatenation(seqs)
        prog_ns = concatenate_sequences(seqs)
        request_dict = self.put_request_dict
        generated_sequence_2_mp3(prog_ns, f"{self.unique_id}",
                                 request_dict=request_dict)
Example #7
0
 def interpolate(self,
                 model,
                 start_seq,
                 end_seq,
                 num_steps,
                 max_length=32,
                 assert_same_length=True,
                 temperature=0.5,
                 individual_duration=4.0):
     """Interpolates between a start and end sequence."""
     print 'Interpolating...'
     _, mu, _ = model.encode([start_seq, end_seq], assert_same_length)
     z = np.array([
         self.slerp(mu[0], mu[1], t) for t in np.linspace(0, 1, num_steps)
     ])
     note_sequences = model.decode(length=max_length,
                                   z=z,
                                   temperature=temperature)
     print 'Start -> End Interpolation'
     interp_seq = concatenate_sequences(
         note_sequences, [individual_duration] * len(note_sequences))
     return interp_seq if num_steps > 3 else note_sequences[num_steps // 2]
Example #8
0
    def get_mean(self,
                 extracted_trios,
                 store,
                 im='hierdec-trio_16bar',
                 temperature=0.5,
                 genre='undefined'):
        """Get the mean latent vector for a genre
        Args:
            extracted_trios: ___
            im: interpolation model"""
        print 'Getting mean latent vector...'
        model = self.trio_models[im]
        ph = np.zeros((1, 512))
        counter = 0
        for i, trio in enumerate(extracted_trios):
            try:
                _, mu, _ = model.encode([trio], assert_same_length=True)
                ph += mu
                counter += 1
                print 'Encoding trio %d/%d' % (i, len(extracted_trios) - 1)
            except:
                print 'Cannot encode trio %d' % i
        mean_mu = ph / counter
        if store == 1:
            self.m1 = mean_mu
        if store == 2:
            self.m2 = mean_mu

        output_sequence = model.decode(length=32,
                                       z=mean_mu,
                                       temperature=temperature)

        final_seq = concatenate_sequences(output_sequence,
                                          [32.0] * len(output_sequence))

        self.download(final_seq, './content/output/%s_truemean.mid' % genre)
        print 'Mean %s acquired' % genre
Example #9
0
  def testConcatenateSequencesWithSpecifiedDurations(self):
    sequence1 = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        sequence1, 0, [(60, 100, 0.0, 1.0), (72, 100, 0.5, 1.5)])
    sequence2 = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        sequence2, 0,
        [(59, 100, 0.0, 1.0)])
    sequence3 = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        sequence3, 0,
        [(72, 100, 0.0, 1.0), (73, 100, 0.5, 1.5)])

    expected_sequence = copy.copy(self.note_sequence)
    testing_lib.add_track_to_sequence(
        expected_sequence, 0,
        [(60, 100, 0.0, 1.0), (72, 100, 0.5, 1.5),
         (59, 100, 2.0, 3.0),
         (72, 100, 3.5, 4.5), (73, 100, 4.0, 5.0)])

    cat_seq = sequences_lib.concatenate_sequences(
        [sequence1, sequence2, sequence3],
        sequence_durations=[2, 1.5, 2])
    self.assertProtoEquals(expected_sequence, cat_seq)
Example #10
0
chord_2 = 'Caug' #@param {type:"string"}
chord_3 = 'Am' #@param {type:"string"}
chord_4 = 'E' #@param {type:"string"}
chords = [chord_1, chord_2, chord_3, chord_4]

temperature = 0.2 #@param {type:"slider", min:0.01, max:1.5, step:0.01}
z = np.random.normal(size=[1, Z_SIZE])
seqs = [
    model.decode(length=TOTAL_STEPS, z=z, temperature=temperature,
                 c_input=chord_encoding(c))[0]
    for c in chords
]

trim_sequences(seqs)
fix_instruments_for_concatenation(seqs)
prog_ns = concatenate_sequences(seqs)

play(prog_ns)
mm.plot_sequence(prog_ns)

#@title (Optional) Save Arrangement to MIDI
download(prog_ns, '_'.join(chords) + '.mid')

#@title Style Interpolation, Repeating Chord Progression

chord_1 = 'Dm' #@param {type:"string"}
chord_2 = 'F' #@param {type:"string"}
chord_3 = 'Am' #@param {type:"string"}
chord_4 = 'G' #@param {type:"string"}
chords = [chord_1, chord_2, chord_3, chord_4]
Example #11
0
def gen_chords():
    form = ChordForm()
    print("Chords CHOICES from form:" + str(form.chord_1.choices))
    form.chord_2.choices = [(chord_id, chord_name)
                            for chord_id, chord_name in form.chord_1.choices]
    form.chord_3.choices = [(chord_id, chord_name)
                            for chord_id, chord_name in form.chord_1.choices]
    form.chord_4.choices = [(chord_id, chord_name)
                            for chord_id, chord_name in form.chord_1.choices]
    if form.validate_on_submit():
        chord_1 = form.chord_1.data
        chord_2 = form.chord_2.data
        chord_3 = form.chord_3.data
        chord_4 = form.chord_4.data
        chords = [chord_1, chord_2, chord_3, chord_4]

        num_bars = form.num_bars.data
        temperature = form.temperature.data

        config = configs.CONFIG_MAP['hier-multiperf_vel_1bar_med_chords']
        weight_name = 'model_chords_fb64.ckpt'
        model = TrainedModel(config,
                             batch_size=BATCH_SIZE,
                             checkpoint_dir_or_path=os.path.join(
                                 app.root_path, 'content', weight_name))

        z1 = np.random.normal(size=[Z_SIZE])
        z2 = np.random.normal(size=[Z_SIZE])
        z = np.array([slerp(z1, z2, t) for t in np.linspace(0, 1, num_bars)])

        seqs = [
            model.decode(length=TOTAL_STEPS,
                         z=z[i:i + 1, :],
                         temperature=temperature,
                         c_input=chord_encoding(chords[i % 4]))[0]
            for i in range(num_bars)
        ]

        trim_sequences(seqs)
        fix_instruments_for_concatenation(seqs)
        prog_interp_ns = concatenate_sequences(seqs)

        f_ext = '.mid'
        random_hex = secrets.token_hex(8)
        music_fn = random_hex + f_ext
        music_mp3 = random_hex + ".mp3"

        #Save music to disk
        mm.sequence_proto_to_midi_file(prog_interp_ns, music_fn)

        ### Move audio to a specified path
        source_path = "/home/pratheesh/Flask_Blog/" + music_mp3
        destination_path = os.path.join(app.root_path, 'static/music',
                                        music_mp3)

        cmd_to_wav = "fluidsynth -F " + random_hex + ".wav /usr/share/sounds/sf2/FluidR3_GM.sf2 " + music_fn
        print(cmd_to_wav)
        os.system(cmd_to_wav)
        cmd_to_mp3 = "lame --preset standard " + random_hex + ".wav " + random_hex + ".mp3"
        print(cmd_to_mp3)
        os.system(cmd_to_mp3)
        #shutil.move(source_path, destination_path)
        #os.replace(source_path, destination_path)
        print("moving file")
        os.rename(source_path, destination_path)
        os.remove(music_fn)
        os.remove(random_hex + ".wav")

        music_file = url_for('static', filename='music/' + music_mp3)

        return render_template('music_output.html', music_file=music_file)

    return render_template('gen_chords.html', form=form)
Example #12
0
def run(config_map):
    '''
    Load model params, save config file and start trainer.

    Args:
        config_map: Dictionary mapping configuration name to Config object.

    Raises:
        ValueError: if required flags are missing or invalid.
    '''
    date_and_time = time.strftime('%Y-%m-%d_%H%M%S')
    
    if FLAGS.run_dir is None == FLAGS.checkpoint_file is None:
        raise ValueError(
            'Exactly one of `--run_dir` or `--checkpoint_file` must be specified.')
    if FLAGS.output_dir is None:
        raise ValueError('`--output_dir` is required.')
    tf.gfile.MakeDirs(FLAGS.output_dir)
    
    if FLAGS.example_midi_dir is None:
        raise ValueError(
            'example_midi_dir is required.')
    
    if FLAGS.config not in config_map:
        raise ValueError('Invalid config name: %s' % FLAGS.config)
    config = config_map[FLAGS.config]
    config.data_converter.max_tensors_per_item = None
    
    config_midime = MidiMeConfig
    if FLAGS.config_midime is not None:
        update(config_midime, FLAGS.config_midime)
            
    logging.info('Loading model...')
    if FLAGS.run_dir:
        checkpoint_dir_or_path = os.path.expanduser(
            os.path.join(FLAGS.run_dir, 'train'))
    else:
        checkpoint_dir_or_path = os.path.expanduser(FLAGS.checkpoint_file)
    model = TrainedModel(
        config, batch_size=min(FLAGS.max_batch_size, FLAGS.num_outputs),
        checkpoint_dir_or_path=checkpoint_dir_or_path)
    
    example_sequences = []
    
    example_midi_dir = os.path.expanduser(FLAGS.example_midi_dir)
    files_in_dir = tf.gfile.ListDirectory(os.path.join(example_midi_dir))
    for file_in_dir in files_in_dir:
        full_file_path = os.path.join(example_midi_dir, file_in_dir)
        try: 
            example_sequences.append(mm.midi_file_to_note_sequence(full_file_path))
        except:
            raise ValueError('%s' %full_file_path)
    
    trimSilence(example_sequences)
    for i in example_sequences:
        i.tempos[0].time = 0
        del i.tempos[1:]
    
    #trim_sequences(example_sequences)
    #chunks = example_sequences
    
    chunks = getChunks(example_sequences, 16)
    
    chords = ['B', 'D#m', 'G#m', 'B', 'E', 'B', 'C#m', 'G#m', 'B', 'D#m', 'Gm', 'B', 'E', 'B', 'C#m', 'G#m']
       
    latent = model.encode(chunks)[0]
    midime = train_model(latent, config_midime)
    
    samples = []
    for i in range(FLAGS.num_outputs):
        s = midime.sample(1)
        seqs = [model.decode(s, config.hparams.max_seq_len, 0.2, chord_encoding(c, config.hparams.max_seq_len, 49))[0]
                for c in chords]
        fix_instruments_for_concatenation(seqs)
        samples.append(concatenate_sequences(seqs))
    
    basename = os.path.join(
        FLAGS.output_dir,
        '%s_%s-*-of-%03d.mid' %
        (FLAGS.config, date_and_time, FLAGS.num_outputs))
    logging.info('Outputting %d files as `%s`...', FLAGS.num_outputs, basename)
    
    for i, ns in enumerate(samples):
        mm.sequence_proto_to_midi_file(ns, basename.replace('*', '%03d' % i))
        
    logging.info('Done.')