Skip to content

Commit ed7fdbb

Browse files
committed
Added generate.py
This code is used to generate new music (in MIDI format) using the trained model (model.hdf5).
1 parent 1801c11 commit ed7fdbb

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

generate.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Generate music using the model trained earlier
2+
3+
from keras.models import load_model
4+
import numpy as np
5+
import pickle
6+
from music21 import converter, instrument, note, chord, stream
7+
8+
notes = []
9+
10+
with open ("notes", "rb") as file:
11+
notes = pickle.load(file)
12+
13+
pitch_names = sorted(set(notes))
14+
15+
model = load_model("model.hdf5")
16+
17+
# Create a mapping from int to element
18+
int_to_element = dict ((num, element) for num, element in enumerate (pitch_names))
19+
element_to_int = dict ((element, num) for num, element in int_to_element.items())
20+
21+
sequence_length = 100
22+
23+
test_input = []
24+
25+
for i in range (len(notes) - sequence_length):
26+
seq_inp = notes[i:i+sequence_length]
27+
test_input.append([element_to_int[ch] for ch in seq_inp])
28+
29+
vocab_len = 359
30+
31+
# Randomly select a sequence of notes from test_input
32+
start = np.random.randint(len(test_input)-1)
33+
34+
35+
# Feed this to model and get prediction
36+
pattern = test_input[start]
37+
final_prediction = []
38+
print("Running the model...")
39+
40+
for note_index in range(200):
41+
pred_inp = np.reshape(pattern, (1, len(pattern), 1))
42+
inp = pred_inp/float(vocab_len)
43+
44+
prediction = model.predict(inp, verbose=0)
45+
idx = np.argmax(prediction)
46+
result = int_to_element[idx]
47+
48+
# Append this predicted note output to final_prediction
49+
final_prediction.append(result)
50+
51+
# Next input to model should also have same size
52+
# So use this same input, but drop the first note and append the predicted note
53+
pattern = pattern[1:]
54+
pattern.append(idx)
55+
56+
print("Music generated!")
57+
# Create MIDI files
58+
print("Creating MIDI file...")
59+
60+
offset = 0 #Time
61+
final_notes = []
62+
63+
for pattern in final_prediction:
64+
#If pattern is a chord
65+
if ('+' in pattern ) or pattern.isdigit():
66+
notes_in_chord = pattern.split('+')
67+
temp_notes = []
68+
69+
for curr_note in notes_in_chord:
70+
# For each note in the chord, create a new Note object
71+
new_note = note.Note(int(curr_note))
72+
new_note.storedInstrument = instrument.Piano()
73+
temp_notes.append(new_note)
74+
75+
new_chord = chord.Chord(temp_notes)
76+
new_chord.offset = offset
77+
final_notes.append(new_chord)
78+
79+
#If pattern is a note
80+
else:
81+
curr_note = note.Note(pattern)
82+
curr_note.offset = offset
83+
curr_note.storedInstrument = instrument.Piano()
84+
final_notes.append(curr_note)
85+
86+
offset += 0.5
87+
88+
89+
# Create a stream object from the generated notes
90+
MIDI_stream = stream.Stream(final_notes)
91+
MIDI_stream.write('midi', fp = 'output.mid')
92+
print("Music saved as \"output.mid\" in current directory")

0 commit comments

Comments
 (0)