def compose(self, index, num_measures=16):
        """Use a pre-trained neural network to compose a melody.
        """
        np.set_printoptions(threshold=np.nan)

        for diversity in [0.2, 0.4, 0.6, 0.8, 1.0, 1.2]:
            print
            print '----- diversity:', diversity

            SEED = self.dataset[3].transpose()
            SEED = SEED[:self.window_size-1]  # Use the window at the start. Subtract 1 since normal window size includes prediction.
            melody = np.expand_dims(SEED, axis=0)
            #print len(SEED)
            #print len(melody)

            for i in range(num_measures * BEATS_PER_MEASURE - len(SEED)):
                #print 'melody.shape', melody.shape
                x = np.expand_dims(np.array(melody[0][i:i + self.window_size]), axis=0)
                #print 'i:', i
                #print 'x:', x
                #print 'x.shape', x.shape

                next_frame = self.model.predict(x, verbose=0)[0]

                #print 'next_frame normalized:', next_frame
                #print 'melody.shape', melody.shape
                #print 'next_frame.shape', next_frame.shape
                #print 'next_frame raw:', next_frame

                if SAMPLE_FROM_MELODY_PROBS:
                    # sample from melody probabilities.
                    next_frame = self._sample_melody(next_frame, MELODY_INDICES_RANGE, diversity)
                else:
                    # Winner-takes-all on melody to force monophonic, and force other floats in vector to 0 or 1.
                    next_frame = self._winner_takes_all(next_frame, MELODY_INDICES_RANGE)

                next_frame = self._get_binary_vector(next_frame)
                next_frame = np.expand_dims(next_frame, axis=0)

                #print 'next_frame normalized:', next_frame
                #print 'melody.shape', melody.shape
                #print 'next_frame.shape', next_frame.shape


                melody = np.concatenate([melody, np.expand_dims(next_frame, axis=0)], axis=1)
                #print 'Appended melody:', melody

                # end of for loop

            # Done with melody.
            #print 'Final melody:', melody
            #print
            # Record the melody matrix to disk.
            melody_csv = 'output/random_%d_%.2f.csv' % (index, diversity)
            np.savetxt(melody_csv, melody[0], fmt='%d', delimiter=',')
            #melody[0].tofile(melody_csv, format='%d', sep=',')
            # Convert to MIDI and write to disk.
            exporter = MidiExporter(melody[0])
            exporter.create_midi_file('output/random_%d_%.2f.midi' % (index, diversity))
예제 #2
0
__author__ = 'epnichols'

import glob
import os
from exporter.neural_net_to_midi import MidiExporter

import numpy as np
np.set_printoptions(threshold=np.nan)

BASE_PATH = '../output'

if __name__ == '__main__':
    # Loop over all files
    for f in glob.glob('%s/*.csv' % BASE_PATH):
        # Load numpy array.
        melody = np.loadtxt(f, delimiter=',')
        filename = os.path.basename(f)[:-4]
        outfile = '%s/%s.midi' % (BASE_PATH, filename)
        print '\ninput:', f

        exporter = MidiExporter(melody)
        exporter.create_midi_file(outfile)
    def compose(self, index, num_measures=16):
        """Use a pre-trained neural network to compose a melody.
        """
        np.set_printoptions(threshold=np.nan)

        for diversity in [0.2, 0.4, 0.6, 0.8, 1.0, 1.2]:
            print
            print '----- diversity:', diversity

            SEED = self.dataset[3].transpose()
            SEED = SEED[:self.window_size -
                        1]  # Use the window at the start. Subtract 1 since normal window size includes prediction.
            melody = np.expand_dims(SEED, axis=0)
            #print len(SEED)
            #print len(melody)

            for i in range(num_measures * BEATS_PER_MEASURE - len(SEED)):
                #print 'melody.shape', melody.shape
                x = np.expand_dims(np.array(melody[0][i:i + self.window_size]),
                                   axis=0)
                #print 'i:', i
                #print 'x:', x
                #print 'x.shape', x.shape

                next_frame = self.model.predict(x, verbose=0)[0]

                #print 'next_frame normalized:', next_frame
                #print 'melody.shape', melody.shape
                #print 'next_frame.shape', next_frame.shape
                #print 'next_frame raw:', next_frame

                if SAMPLE_FROM_MELODY_PROBS:
                    # sample from melody probabilities.
                    next_frame = self._sample_melody(next_frame,
                                                     MELODY_INDICES_RANGE,
                                                     diversity)
                else:
                    # Winner-takes-all on melody to force monophonic, and force other floats in vector to 0 or 1.
                    next_frame = self._winner_takes_all(
                        next_frame, MELODY_INDICES_RANGE)

                next_frame = self._get_binary_vector(next_frame)
                next_frame = np.expand_dims(next_frame, axis=0)

                #print 'next_frame normalized:', next_frame
                #print 'melody.shape', melody.shape
                #print 'next_frame.shape', next_frame.shape

                melody = np.concatenate(
                    [melody, np.expand_dims(next_frame, axis=0)], axis=1)
                #print 'Appended melody:', melody

                # end of for loop

            # Done with melody.
            #print 'Final melody:', melody
            #print
            # Record the melody matrix to disk.
            melody_csv = 'output/random_%d_%.2f.csv' % (index, diversity)
            np.savetxt(melody_csv, melody[0], fmt='%d', delimiter=',')
            #melody[0].tofile(melody_csv, format='%d', sep=',')
            # Convert to MIDI and write to disk.
            exporter = MidiExporter(melody[0])
            exporter.create_midi_file('output/random_%d_%.2f.midi' %
                                      (index, diversity))