-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_process.py
53 lines (38 loc) · 1.5 KB
/
data_process.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from music21 import converter, instrument, note, chord
import glob
import pickle
import os
# implementation based on https://towardsdatascience.com/how-to-generate-music-using-a-lstm-neural-network-in-keras-68786834d4c5
def get_songs():
songs = []
for file in glob.glob("data/*.mid"):
print("Parsing", file)
try:
notes = []
name = os.path.splitext(os.path.basename(file))[0]
midi = converter.parse(file)
notes_to_parse = None
parts = instrument.partitionByInstrument(midi)
if parts: # file has instrument parts
notes_to_parse = parts.parts[0].recurse()
else: # file has notes in a flat structure
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
songs.append({"name": name, "notes": notes})
except Exception as err:
print("Parsing Failed", file)
print("Error:", err)
pickle.dump(songs, open("songs.p", "wb"))
return songs
def load_songs():
songs = pickle.load(open("songs.p", "rb"))
for song in songs:
print("Load", song["name"])
print("First 10 notes:", song["notes"][:10])
if __name__ == "__main__":
# get_songs()
load_songs()