From 8b00b206575712ba24b946762f28e4443586417c Mon Sep 17 00:00:00 2001 From: Cezary Pukownik Date: Thu, 30 May 2019 23:13:03 +0200 Subject: [PATCH] genereting 4 beats instead of 1; better plot; save drums to midi --- project/generate.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/project/generate.py b/project/generate.py index 333712a..9a5023b 100644 --- a/project/generate.py +++ b/project/generate.py @@ -3,6 +3,7 @@ import numpy as np import midi import tensorflow as tf +import pypianoroll as roll from keras.layers import Input, Dense, Conv2D from keras.models import Model from tensorflow.keras import layers @@ -17,20 +18,22 @@ trained_model_path = sys.argv[1] output_path = sys.argv[2] treshold = float(sys.argv[3]) -#random seed -generate_seed = np.random.rand(12288).reshape(1,96,128) - # load and predict model = pickle.load(open(trained_model_path, 'rb')) -generated_sample = model.predict(generate_seed) -generated_sample = generated_sample.reshape(96,128) + +music = np.empty((4,96,128)) +for x in range(4): + generate_seed = np.random.randint(0, 127, 12288).reshape(1,96,128) + music[x] = model.predict(generate_seed).reshape(96,128) + +generated_sample = music.reshape(4*96,128) # binarize generated music generated_sample = generated_sample > treshold * generated_sample.max() +# generated_sample = np.clip(generated_sample,0,1) * 128 -#save to midi -midi.to_midi(generated_sample, output_path='{}.mid'.format(output_path) ) +# save to midi +generated_midi = midi.to_midi(generated_sample, output_path='{}.mid'.format(output_path), is_drum=True, program=0, ) -#save piano roll to png -plt.imshow(generated_sample, cmap = plt.get_cmap('gray')) -plt.savefig('{}.png'.format(output_path)) +#save plot for preview +roll.plot(generated_midi, filename='{}.png'.format(output_path))