-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyphon.py
278 lines (219 loc) · 11.3 KB
/
polyphon.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from sys import argv
from decimal import *
from pyx import *
from operator import itemgetter
import binascii
import populate
import eventslistpopulator
import functions
import overlay
import tracknotes
import itertools
import copy
import math
class Midi(object):
#object that contains midi header data, a list of the tracks
#contained within the midi and various lists required to create
#the polyphon coordinates.
def __init__(self):
self.listOfTracks = populate.track_list(midiHex)
self.tracksOneToEnd = self.listOfTracks[1:]
self.header = populate.header_info(self.listOfTracks[0])
self.listOfTrackDicts = []
for i, track in enumerate(self.tracksOneToEnd):
self.listOfTrackDicts.append(eventslistpopulator.populate(i+1, track))
self.listOfAllEvents = populate.all_events(self.listOfTrackDicts)
self.listOfTempos = populate.midi_tempos(self.listOfTrackDicts)
self = populate.real_times(self)
def collapse(self):
#a method to check whether any bugs are introduced during the splitting
#up of the midi into events.
""" NEEDS FIXING """
self.collapsed = []
for j in range(0,len(self.listOfTrackDicts)):
self.collapsed.append(functions.collapse_events(
self.tracksOneToEnd[j],
self.listOfTrackDicts[j]['allEvents']))
if self.collapsed[j] == self.tracksOneToEnd[j]:
print "HOORAY!"
else:
print "BOOOOO!"
class Polyphon(object):
def __init__(self):
self.allNoteEvents = []
for k in sorted(midi.listOfAllEvents):
if k.eventType[0] == '8' or k.eventType[0] == '9':
self.allNoteEvents.append(k)
self.noteTimings = populate.note_timings(self.allNoteEvents)
self.imageNotes = populate.image_notes(self.noteTimings)
self.trackNotes = tracknotes.return_notes()
self.polyphonTracks = populate.polyphon_tracks(self.trackNotes, self.imageNotes)
self.polyphonTracks = populate.all_available_tracks(self.polyphonTracks)
self.polyphonTracks = populate.note_angles(self.polyphonTracks)
def evaluate_7mm_rule(self):
#Checks whether all notes obey the 7mm rule. i.e. if the minimum
#linear distance between any two consecutive notes on the same track
#falls below 7mm.
tooCloseCount = 0
sevenMmRule = Decimal(7)
textFileOutput = filename + ".txt"
f = open(textFileOutput, 'w')
fileLines = []
for k in self.polyphonTracks:
minAngle = ((sevenMmRule / k[1]) / (2 * Decimal(math.pi))) * 360
secondsPerDegree = Decimal(105)/Decimal(360)
for i, angle in enumerate(k[5]):
if i == 0:
angleBuff = angle[0]
continue
if angle[0] - angleBuff < minAngle:
fileLines.append("too close on track: " + str(k[0]))
fileLines.append("\n")
fileLines.append("(difference between notes " + \
str((angle[0] - angleBuff)*secondsPerDegree) + ") < " + \
"(minimum time " + str(minAngle*secondsPerDegree) + ")")
fileLines.append("\n")
if (angle[0] - angleBuff) == Decimal(0):
fileLines.append("two notes cannot occur on this " + \
"track at the same time")
else:
fileLines.append("note separation needs to be " + \
str(int((minAngle/(angle[0] - angleBuff))*100) - 100) + \
"% larger")
fileLines.append("\n")
fileLines.append("\n")
tooCloseCount += 1
angleBuff = angle[0]
continue
angleBuff = angle[0]
fileLines.append("too close count: " + str(tooCloseCount))
for line in fileLines:
f.write(line)
f.close()
def create360Warnings(self, warningTracks):
warningFile = filename + "_360warnings.txt"
with open(warningFile, 'w') as f:
for track in warningTracks:
for note in track:
line = "Track: " + str(note[0]) + \
" Start: " + str(round(note[1]*100.0, 1)) + \
"%" + " End: " + str(round(note[2]*100.0, 1)) + "%" +"\n"
f.write(line)
def create_visualisation(self):
text.set(mode="latex")
text.preamble(r'\usepackage{lmodern}')
canv = canvas.canvas()
#changed offset from 10 to 0
offset = 0
for i in range (1, 121):
#Creates the guiding line for each track and the "Track x" text
#at the beginning of each line.
p = path.line(0, i*2, 360, i*2)
canv.stroke(p, [style.linewidth(0.05), style.linestyle.solid])
txt = "Track " + str(i)
textArg = r'\fontsize{60}{20}\selectfont %s' % txt
canv.text(0,i*2,textArg)
sevenMmRule = Decimal(7)
warningTracks = []
for pTrack in self.polyphonTracks:
minAngle = ((sevenMmRule / pTrack[1]) / (2 * Decimal(math.pi))) * 360
angleBuff = 0
if not pTrack[5] == []:
notesPast360 = []
notesWithin5pc = []
for c, note in enumerate(pTrack[5]):
if note == []:
continue
position = note[0] + offset
fPosition = float(position)
p = path.line(fPosition, pTrack[0]*2, fPosition + 0.5, pTrack[0]*2)
minP = path.line(fPosition, pTrack[0]*2, fPosition + float(minAngle), pTrack[0]*2)
if (note[0] - angleBuff) < minAngle and (note[0] - angleBuff) >= 0.000001 and not (c == 0):
canv.stroke(minP, [style.linewidth(0.25), style.linestyle.solid, color.rgb.blue])
canv.stroke(p, [style.linewidth(0.5), style.linestyle.solid, color.rgb.red])
elif (note[0] - angleBuff) < minAngle and (note[0] - angleBuff) < 0.000001 and not (c == 0):
canv.stroke(minP, [style.linewidth(0.25), style.linestyle.solid, color.rgb.blue])
canv.stroke(p, [style.linewidth(0.5), style.linestyle.solid, color.rgb.green])
#added the below elif to check if notes go on past 360 degrees
elif (fPosition + float(minAngle)) >= 360:
notesPast360.append([pTrack[0], fPosition/360.0, (fPosition + float(minAngle))/360.0])
canv.stroke(minP, [style.linewidth(0.25), style.linestyle.solid, color.rgb.green])
canv.stroke(p, [style.linewidth(0.5), style.linestyle.solid])
else:
if (fPosition + float(minAngle)) >= 360.0*0.95:
notesWithin5pc.append([pTrack[0], fPosition/360.0, (fPosition + float(minAngle))/360.0])
canv.stroke(minP, [style.linewidth(0.25), style.linestyle.solid, color.rgb.blue])
canv.stroke(p, [style.linewidth(0.5), style.linestyle.solid])
angleBuff = note[0]
if notesPast360:
warningTracks.append(notesPast360)
#print notesWithin5pc
self.create360Warnings(warningTracks)
canv.writePDFfile(filename)
def create_projections(self):
trackProjections = []
highlightedProjections = []
for i, pTrack in enumerate(self.polyphonTracks):
trackProjections.append([])
#print pTrack
for note in pTrack[5]:
noteProjection = (Decimal(160000) / Decimal(360)) * note[0]
trackProjections[i].append(Decimal(160000) - noteProjection)
print note[1]
if note[1] == 127:
highlightedProjections.append(int(Decimal(160000) - noteProjection))
tempMin = Decimal(160000)
tempMax = Decimal(0)
for track in trackProjections:
for proj in track:
if proj > tempMax:
tempMax = proj
if proj < tempMin:
tempMin = proj
projBuffer = (((tempMax - Decimal(160000)) + tempMin)/Decimal(2))
tempTrackProjections = copy.deepcopy(trackProjections)
for i, track in enumerate(tempTrackProjections):
for j, proj in enumerate(track):
trackProjections[i][j] = (proj - projBuffer).quantize(0)
if True:
textFileOutput = filename + "_projections" + ".txt"
orderedTextFile = filename + "_ordered" + ".txt"
textFileProjections = []
orderableProjections = []
for i, track in enumerate(trackProjections):
if track == []:
textFileProjections.append("te\n")
continue
else:
for j, note in enumerate(track):
textFileProjections.append("t" + str(i + 1) + \
"p" + str(j + 1) + \
"-" + str(note) + "\n")
proj = []
proj.append("t" + str(i + 1) + "p" + str(j + 1))
proj.append(note)
orderableProjections.append(proj)
textFileProjections.append("te\n")
orderedProjections = sorted(orderableProjections, key=itemgetter(1))
#print orderedProjections
overlay.create_overlay(orderedProjections)
with open(textFileOutput, 'w') as projFile:
for item in textFileProjections:
projFile.write(item)
with open(orderedTextFile, 'w') as ordFile:
for item in list(reversed(orderedProjections)):
ordFile.write(item[0] + " " + str(item[1]) + "\n")
highlightedProjections.sort()
print highlightedProjections
#Set Decimal type precision to 12
getcontext().prec = 12
getcontext().rounding = "ROUND_HALF_UP"
script, midiFile = argv
filename = functions.extract_filename(midiFile)
#read midi file specified by 'filename'
midiHex = functions.read_file(midiFile)
midi = Midi()
polyphon = Polyphon()
polyphon.evaluate_7mm_rule()
polyphon.create_visualisation()
polyphon.create_projections()