-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIA.py
More file actions
198 lines (191 loc) · 9.32 KB
/
SIA.py
File metadata and controls
198 lines (191 loc) · 9.32 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import sys
import timeit
import matplotlib.pyplot as plt
def SIA(notey, timex):
plt.scatter(timex, notey)
plt.xlabel('Note in Track')
plt.ylabel('Notepitch')
#plt.show()
notearray = []
position = []
tracklen = len(notey)
Gausssum = 1
zaehler = 0
vektorinformation = [0]
vektorinformation.append(0)
while len(notearray) < ((tracklen*(tracklen+1)/2)-tracklen): # Calculation of Vektors
vektorinformation[0] = int(notey[Gausssum] - notey[zaehler])
vektorinformation[1] = int(timex[Gausssum] - timex[zaehler])
notearray.append(', '.join(str(x) for x in vektorinformation))
position.append(str(Gausssum + 1) + ',' + str(zaehler + 1))
if len(notearray) == (Gausssum*(Gausssum+1)/2):
Gausssum += 1
zaehler = 0
else:
zaehler += 1
notearray, position = Quicksort(0, len(notearray)-1, notearray, position)
#plt.show()
return notearray, position
def Quicksort(left, right, notearray, position):
if len(notearray) <= 1:
return notearray, position
smallernote = []
biggernote = []
smallerposition = []
biggerposition = []
informationpivot = notearray[right].split(',')
informationpivot[0], informationpivot[1] = int(informationpivot[0]), int(informationpivot[1])
while True:
informationleft = notearray[left].split(',')
informationleft[0], informationleft[1] = int(informationleft[0]), int(informationleft[1])
informationright = notearray[right].split(',')
informationright[0], informationright[1] = int(informationright[0]), int(informationright[1])
if informationleft[0] < informationpivot[0]: # left is smaller than pivot -> index + 1
smallernote.append(notearray[left])
smallerposition.append(position[left])
left += 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
else:
if informationleft[0] > informationpivot[0] or informationleft[0] == informationpivot[0] and informationleft[1] >= informationpivot[1]: # left is bigger than pivot -> beginn search for bigger element
if informationright[0] > informationpivot[0]:
biggernote.insert(0, notearray[right])
biggerposition.insert(0, position[right])
right -= 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
elif informationright[0] < informationpivot[0] or informationright[0] == informationpivot[0] and informationright[1] <= informationpivot[1]:
if right == left:
biggernote.insert(0, notearray[left])
biggerposition.insert(0, position[left])
left += 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
else:
smallernote.append(notearray[right])
smallerposition.append(position[right])
biggernote.insert(0, notearray[left])
biggerposition.insert(0, position[left])
left += 1
right -= 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
else:
biggernote.insert(0, notearray[right])
biggerposition.insert(0, position[right])
right -= 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
else:
smallernote.append(notearray[left])
smallerposition.append(position[left])
left += 1
if left > right:
notearrayleft, positionleft = Quicksort(0, len(smallernote) - 1, smallernote, smallerposition)
notearrayright, positionright = Quicksort(0, len(biggerposition) - 1, biggernote, biggerposition)
break
notearray = notearrayleft+notearrayright
position = positionleft+positionright
return notearray, position
if __name__ == '__main__':
timestart = timeit.default_timer()
normalstdout = sys.stdout
#filename = 'beethoven_ode_to_joy.mid' # ToDo: Variabel machen
f = open('Darude SandstormTest.txt', 'r')
p = open('Ergebnis Patternsuche SIATEC.txt', 'w')
sys.stdout = p
notestring = ''
timestring = ''
timex = []
notey = []
track = 1
trackstr = '1'
while True:
nextline = f.readline()
nextline = nextline[:-1]
if nextline == "":
track += 1
trackstr = str(track)
if len(timex) > 0:
print('===Track ' + str(track-1) + '===\n')
notearray, positionsolution = SIA(notey, timex)
difference = []
positiontemp = []
position = []
itterator = 0
patternlength = 0
patternstart = []
patternend = []
same = False
while itterator < len(notearray) - 1:
patterninformation = notearray[itterator].split(', ')
nextpatterninformation = notearray[itterator + 1].split(', ')
if nextpatterninformation[0] == patterninformation[0] and nextpatterninformation[1] == patterninformation[1]:
same = True
positiontemp.append(positionsolution[itterator])
elif (nextpatterninformation[0] != patterninformation[0] or nextpatterninformation[1] != patterninformation[1]) and same:
same = False
positiontemp.append(positionsolution[itterator])
positiontemp.sort()
position.append(positiontemp)
positiontemp = []
elif (nextpatterninformation[0] != patterninformation[0] or nextpatterninformation[1] != patterninformation[1]) and same is False:
del notearray[itterator]
del positionsolution[itterator]
if len(positiontemp) != 0:
positiontemp.sort()
position.append(positiontemp)
positiontemp = []
continue
itterator += 1
if nextpatterninformation[0] != patterninformation[0] or nextpatterninformation[1] != patterninformation[1]: # Sort out some results
del notearray[len(notearray) - 1]
itterator = 0
while itterator < len(notearray) - 1:
sort1 = notearray[itterator].split(', ')
sort1[0], sort1[1] = int(sort1[0]), int(sort1[1])
sort2 = notearray[itterator + 1].split(', ')
sort2[0], sort2[1] = int(sort2[0]), int(sort2[1])
if sort1[0] == sort2[0] and sort1[1] == sort2[1]:
del notearray[itterator+1]
continue
itterator += 1
itterator = 0
while itterator < len(position):
sort1 = str(position[itterator]).split(', ')
if len(sort1) < 3:
del position[itterator]
del notearray[itterator]
continue
itterator += 1
for i in range(0, len(notearray)):
print('Differenz Note/Zeit:\t\t' + notearray[i])
print('Zu Position, von Position:\t' + str(position[i]) + '\n')
f.seek(0)
timex = []
notey = []
else:
break
else:
tempstring = nextline.split(',')
if tempstring[2] == trackstr:
timex.append(int(tempstring[0]))
notey.append(int(tempstring[1]))
timeend = timeit.default_timer()
totaltime = timeend - timestart
print(str(totaltime) + ' sec')
sys.stdout = normalstdout
f.close()
p.close()
# LittleTranscriptor nehmen
# https://www.youtube.com/watch?v=7K4hBdokhbE