4
4
import time
5
5
import random
6
6
import pandas as pd
7
+ import numpy as np
8
+ import glob
9
+ import pickle
7
10
from music21 import converter , instrument , note , chord
8
11
12
+ GENRES = ["Blues" , "Country" , "Indie" , "Jazz" , "Pop" , "Psychedelic Rock" , "Rock" , "Soul" ]
13
+
14
+ # TODO : (
15
+
9
16
def train_network ():
10
17
""" Train a Neural Network to generate music """
11
18
notes = get_notes ()
@@ -23,27 +30,27 @@ def get_notes():
23
30
""" Get all the notes and chords from the midi files in the ./midi_songs directory """
24
31
notes = []
25
32
26
- for file in glob .glob ("midi_songs/*.mid" ):
27
- midi = converter .parse (file )
33
+ for j in GENRES :
34
+ for file in glob .glob ("../TrainingData/" + j + "/*.mid" ):
35
+ midi = converter .parse (file )
28
36
29
- print ("Parsing %s" % file )
37
+ print ("Parsing %s" % file )
30
38
31
- notes_to_parse = None
39
+ notes_to_parse = None
32
40
33
- try : # file has instrument parts
34
- s2 = instrument .partitionByInstrument (midi )
35
- notes_to_parse = s2 .parts [0 ].recurse ()
36
- except : # file has notes in a flat structure
37
- notes_to_parse = midi .flat .notes
41
+ try : # file has instrument parts
42
+ s2 = instrument .partitionByInstrument (midi )
43
+ notes_to_parse = s2 .parts [0 ].recurse ()
44
+ except : # file has notes in a flat structure
45
+ notes_to_parse = midi .flat .notes
38
46
39
- for element in notes_to_parse :
40
- if isinstance (element , note .Note ):
41
- notes .append (str (element .pitch ))
42
- elif isinstance (element , chord .Chord ):
43
- notes .append ('.' .join (str (n ) for n in element .normalOrder ))
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 ))
44
52
45
- with open ('data/notes' , 'wb' ) as filepath :
46
- pickle .dump (notes , filepath )
53
+ np .save ("notes" , notes )
47
54
48
55
return notes
49
56
@@ -70,7 +77,7 @@ def prepare_sequences(notes, n_vocab):
70
77
n_patterns = len (network_input )
71
78
72
79
# reshape the input into a format compatible with LSTM layers
73
- network_input = numpy .reshape (network_input , (n_patterns , sequence_length , 1 ))
80
+ network_input = np .reshape (network_input , (n_patterns , sequence_length , 1 ))
74
81
# normalize input
75
82
network_input = network_input / float (n_vocab )
76
83
@@ -116,5 +123,5 @@ def train(model, network_input, network_output):
116
123
model .fit (network_input , network_output , epochs = 200 , batch_size = 128 , callbacks = callbacks_list )
117
124
118
125
if __name__ == '__main__' :
119
- train_network ()
126
+ get_notes ()
120
127
0 commit comments