-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage2.py
153 lines (131 loc) · 3.46 KB
/
stage2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import cv2
import os
import numpy as np
import NN_notes
import music_player
CROPPED_NOTES_PATH = "cropped_notes/"
DATABASE_NOTES_PATH = "database_notes/"
NOTE_LENGTH = 3
REST_LENGTH = 2
REST = "rest"
INNER_NOTE_REST = 0.3
# EXAMPLE MELODIES:
EXAMPLE_MELODY = ["g1", "e1", "e1", "rest", "f1", "d1", "d1", "rest", "c1", "d1", "e1", "f1", "g1", "g1", "g1"]
PLAYABLE_MELODY_2 = [
{
"type" : 0,
"length" : 0.25,
"freq" : 440,
},
{
"type" : 0,
"length" : 0.25,
"freq" : 480,
},
{
"type" : 0,
"length" : 0.25,
"freq" : 550,
},
{
"type" : 0,
"length" : 0.25,
"freq" : 700,
}
]
# musical constants
OCTAVE_LENGTH = 12
EMPTY_FREQ = 0
TYPES = {
"note" : 0,
"rest" : 1,
}
LENGTHS = {
"sixteenth" : 0.0625,
"eighth" : 0.125,
"quarter" : 0.25,
"half" : 0.5,
"whole" : 1,
}
NUMS = {
"c" : 1,
"d" : 3,
"e" : 5,
"f" : 6,
"g" : 8,
"a" : 10,
"b" : 12,
"h" : 12,
}
# no point in trying to understand this... musical stuff
def calc_freq(note_name):
note_num = (OCTAVE_LENGTH*int(note_name[1]) + NUMS[note_name[0]])
return 440 * (2 **(float(note_num-22)/12))
def create_melody(extracted_notes, my_notes):
melody = []
for note in extracted_notes:
melody.append(my_notes[note])
melody.append({
"type" : 1,
"length" : INNER_NOTE_REST,
"freq" : EMPTY_FREQ
})
return melody
def create_melody2(actual_notes):
melody = []
for note in actual_notes:
freq = EMPTY_FREQ
if note != "rest":
freq = calc_freq(note)
melody.append({
"type" : 0,
"length" : (1-INNER_NOTE_REST)*0.5,
"freq" : freq
})
melody.append({
"type" : 1,
"length" : INNER_NOTE_REST*0.5,
"freq" : EMPTY_FREQ
})
return melody
def main():
data_notes = []
# extract all filenames from the database
for filename in os.listdir(DATABASE_NOTES_PATH):
name_length = NOTE_LENGTH
if REST in filename:
name_length = REST_LENGTH
# add to database
if "other" not in filename:
data_notes.append('-'.join(filename.split('-')[:name_length]))
# sort lexicographically and remove clones
data_notes = sorted(list(set(data_notes)))
# create the database for notes:
my_notes = {}
for i in range(len(data_notes)):
note = data_notes[i].split('-')
my_notes[i] = {
"type" : TYPES[note[0]],
"length" : (1-INNER_NOTE_REST) * LENGTHS[note[1]]
}
if my_notes[i]["type"] == TYPES["rest"]:
my_notes["freq"] = EMPTY_FREQ
else:
my_notes["freq"] = calc_freq("%s0" % note[2])
# now we have database. call neural_network for each note picture
extracted_notes = []
for filename in sorted(os.listdir(CROPPED_NOTES_PATH)):
my_net = NN_notes.NN()
im = cv2.imread(filename, 0)
im = cv2.resize(im, (30, 50))
im = np.reshape(im, 50*30)
im = im.tolist()
extracted_notes.append(my_net.classify(np.array([im])))
# create melody from NN output
melody = create_melody(extracted_notes, my_notes)
# now extracted notes contains the entire melody
# TODO: add "second hand"
player = music_player.MusicPlayer(melody)
player.add_melody()
if __name__ == "__main__":
main()