-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze_notes.py
45 lines (38 loc) · 1.75 KB
/
analyze_notes.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
from music21 import *
import os
import pickle5 as pickle
notes = []
path = "music_gen/midi_files/"
for dir in os.listdir(path):
num_midis = len(os.listdir(path + "/" + dir))
progress = 0
for file in os.listdir(path + "/" + dir)[0:5]: #if we use more than this, each epoch becomes HUGE
parsed_midi = converter.parse(path + dir + "/" + file)
notes_to_parse = None
parts = instrument.partitionByInstrument(parsed_midi)
try:
if parts: # file has instrument parts
notes_to_parse = parts.parts[1].recurse()
else: # file has notes in a flat structure
notes_to_parse = parsed_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))
except:
if parts: # file has instrument parts
notes_to_parse = parts.parts[0].recurse()
else: # file has notes in a flat structure
notes_to_parse = parsed_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))
progress += 1
if progress % 5 == 0:
print("Analyzed: ", progress, "/", num_midis)
print("Done analyzing Midis!")
with open("music_gen/notes.pickle", "wb") as f:
pickle.dump(notes, f, protocol=pickle.HIGHEST_PROTOCOL)