Skip to content

Commit 74f33f4

Browse files
author
Cannon Lock
committed
Temp
1 parent dc4d3c5 commit 74f33f4

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

DeepLearning.py

+25-18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
import time
55
import random
66
import pandas as pd
7+
import numpy as np
8+
import glob
9+
import pickle
710
from music21 import converter, instrument, note, chord
811

12+
GENRES = ["Blues", "Country", "Indie", "Jazz", "Pop", "Psychedelic Rock", "Rock", "Soul"]
13+
14+
# TODO : (
15+
916
def train_network():
1017
""" Train a Neural Network to generate music """
1118
notes = get_notes()
@@ -23,27 +30,27 @@ def get_notes():
2330
""" Get all the notes and chords from the midi files in the ./midi_songs directory """
2431
notes = []
2532

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)
2836

29-
print("Parsing %s" % file)
37+
print("Parsing %s" % file)
3038

31-
notes_to_parse = None
39+
notes_to_parse = None
3240

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
3846

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))
4452

45-
with open('data/notes', 'wb') as filepath:
46-
pickle.dump(notes, filepath)
53+
np.save("notes", notes)
4754

4855
return notes
4956

@@ -70,7 +77,7 @@ def prepare_sequences(notes, n_vocab):
7077
n_patterns = len(network_input)
7178

7279
# 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))
7481
# normalize input
7582
network_input = network_input / float(n_vocab)
7683

@@ -116,5 +123,5 @@ def train(model, network_input, network_output):
116123
model.fit(network_input, network_output, epochs=200, batch_size=128, callbacks=callbacks_list)
117124

118125
if __name__ == '__main__':
119-
train_network()
126+
get_notes()
120127

0 commit comments

Comments
 (0)