@@ -26,7 +26,21 @@ def train_network():
26
26
27
27
train (model , network_input , network_output )
28
28
29
- def get_notes ():
29
+ def get_note_str (note , duration ):
30
+ return note .nameWithOctave + duration
31
+
32
+ def get_element_str (el , includeDuration ):
33
+ duration = "-" + str (round (float (el .quarterLength ), 3 )) if includeDuration else ""
34
+
35
+ if isinstance (el , note .Note ):
36
+ return get_note_str (el , duration )
37
+ elif isinstance (el , note .Rest ):
38
+ return el .name + duration
39
+ elif isinstance (el , chord .Chord ):
40
+ note_strings = [get_note_str (n , duration ) for n in el .notes ]
41
+ return " " .join (sorted (note_strings ))
42
+
43
+ def get_notes (includeDuration = False ):
30
44
""" Get all the notes and chords from the midi files in the ./midi_songs directory """
31
45
notes = []
32
46
@@ -44,11 +58,9 @@ def get_notes():
44
58
except : # file has notes in a flat structure
45
59
notes_to_parse = midi .flat .notes
46
60
47
- for element in notes_to_parse :
48
- if isinstance (element , note .Note ):
49
- notes .append (str (element .pitch ))
50
- elif isinstance (element , chord .Chord ):
51
- notes .append ('.' .join (str (n ) for n in element .normalOrder ))
61
+ for el in notes_to_parse :
62
+ if isinstance (el , note .Note ) or isinstance (el , chord .Chord ) or isinstance (el , note .Rest ):
63
+ notes .append (get_element_str (el , includeDuration ))
52
64
53
65
np .save ("notes" , notes )
54
66
@@ -123,5 +135,5 @@ def train(model, network_input, network_output):
123
135
model .fit (network_input , network_output , epochs = 200 , batch_size = 128 , callbacks = callbacks_list )
124
136
125
137
if __name__ == '__main__' :
126
- get_notes ()
138
+ a = get_notes ()
127
139
0 commit comments