-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmm-ornamentation-data.py
142 lines (108 loc) · 4.18 KB
/
hmm-ornamentation-data.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
from chorale import Chorale
import sys
import os
from copy import deepcopy
def readentries(filename):
entries = []
f = open(filename, "r")
for chor in f:
entries.append("bch" + ('%03d' % int(chor.split("\n")[0])) + ".txt")
f.close()
return entries
harmonydir = ""
modeldir = ""
datasetdir = harmonydir + "datasets/"
name = sys.argv[1]
prevstage = sys.argv[2]
prevoutputname = sys.argv[3]
train = sys.argv[4]
test = sys.argv[5]
# perl -I chorale-perl chorale-perl/hmm-ornamentation-data.pl ornamentation-dur chords-dur viterbi train_dur test_dur
modeldir += "model-" + name + "/"
prevstagedir = "model-" + prevstage + "/" + prevoutputname + "-results/"
if not os.path.exists(modeldir):
os.mkdir(modeldir)
os.mkdir(modeldir + "input")
os.mkdir(modeldir + "input-test")
os.mkdir(modeldir + "viterbi")
os.mkdir(modeldir + "sampled")
hiddensymbols = []
hiddensymbolseen = {}
visiblesymbols = []
visiblesymbolseen = {}
# carica due array con i nomi dei file
trainfiles = readentries(datasetdir + train)
testfiles = readentries(datasetdir + test)
files = []
if __name__ == '__main__':
for file in trainfiles:
files.append("music/" + file)
for file in testfiles:
files.append("music/" + file)
for file in testfiles:
files.append(prevstagedir + file)
choraleManager = Chorale()
for file in files:
[header, lines] = choraleManager.readchorale(file, False)
lines.append(choraleManager.AFTER)
lines.append(choraleManager.AFTER)
lines.append(choraleManager.AFTER)
lines.append(choraleManager.AFTER)
if prevstagedir in file:
filename = modeldir + "input-test/" + file.split("/").pop()
print("writing on " + filename)
outputfile = open(filename, "w+")
else:
filename = modeldir + "input/" + file.split("/").pop()
print("writing on " + filename)
outputfile = open(filename, "w+")
for i in range(0, len(lines), 4):
h = []
v = ["0"]
if i + 4 < len(lines):
c = lines[i]
c1 = lines[i + 1]
c2 = lines[i + 2]
c3 = lines[i + 3]
if c3[2] != "END":
c4 = lines[i + 4]
else:
c4 = deepcopy(c)
for j in range(3, 6):
h.append("0," + str(choraleManager.notepitch(c1[j]) - choraleManager.notepitch(c[j])) + ","
+ str(choraleManager.notepitch(c2[j]) - choraleManager.notepitch(c[j])) + ","
+ str(choraleManager.notepitch(c3[j]) - choraleManager.notepitch(c[j])))
v.append(str(choraleManager.notepitch(c4[j]) - choraleManager.notepitch(c[j])))
hiddensymbol = "/".join(h)
visiblesymbol = "/".join(v)
else:
hiddensymbol = "0,0,0,0/0,0,0,0/0,0,0,0"
for j in range(3, 6):
v.append(str(choraleManager.notepitch(c3[j]) - choraleManager.notepitch(c[j])))
visiblesymbol = "/".join(v)
try:
hiddensymbols.index(hiddensymbol)
except ValueError:
hiddensymbols.append(hiddensymbol)
try:
visiblesymbols.index(visiblesymbol)
except ValueError:
visiblesymbols.append(visiblesymbol)
outputfile.write(str(hiddensymbols.index(hiddensymbol)) + "\t" +
str(visiblesymbols.index(visiblesymbol)) + "\n")
outputfile.close()
f = open(modeldir + "SYMBOLS_HIDDEN", "w+")
f.write("\n".join(hiddensymbols))
f.close()
f = open(modeldir + "SYMBOLS_VISIBLE", "w+")
f.write("\n".join(visiblesymbols))
f.close()
f = open(modeldir + "PARAMETERS", "w+")
f.write("Name: " + name + "\n")
f.write("Hidden: Ornamented notes\n")
f.write("Visible: Part interval\n\n")
f.write("Hidden states: " + str(len(hiddensymbols)) + "\n")
f.write("Visible states: " + str(len(visiblesymbols)) + "\n")
f.write("Training data: " + train + "\n")
f.write("Test data: " + test + "\n")
f.close()