-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoTabber.py
426 lines (400 loc) · 14.8 KB
/
autoTabber.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from math import *
class NoteFormatConverter:
def __init__(self):
pass
def filenameGuitarNotesToScoreNotes(self, filename):
f = open(filename)
content = ""
for line in f:
content += line
content = content.replace('\t',' ')
content = content.replace('\n','')
f.close()
guitarNotes = self.stringToGuitarNotes(content)
scoreNotes = [ScoreNote.fromGuitarNote(guitarNote) for guitarNote in guitarNotes]
return scoreNotes
def stringToGuitarNotes(self, string):
res = []
stringGuitarNotes = string.rstrip().split(" ")
for time in range(len(stringGuitarNotes)):
stringGuitarNotesInCurrentTime = self.getstringGuitarNotes(stringGuitarNotes[time])
notesInCurrentTime = [GuitarNote.fromString(stringNote,time) for stringNote in stringGuitarNotesInCurrentTime]
# Sort Guitar Notes played at current time by pitch (so that all information conveyed by the input is destroyed, except for note pitch)
for i in range(len(notesInCurrentTime)):
for j in range(i,len(notesInCurrentTime)):
if notesInCurrentTime[i].getPitch() > notesInCurrentTime[j].getPitch():
notesInCurrentTime[i], notesInCurrentTime[j] = notesInCurrentTime[j], notesInCurrentTime[i]
for note in notesInCurrentTime:
res.append(note)
return res
def getstringGuitarNotes(self, string):
res = []
n = len(string)
left = 0
while left < n:
# find end of current note
right = left + 1
while right + 1 < n and not(string[right + 1] in GuitarNote.allowedStrings):
right += 1
currentNote = string[left:right + 1]
res.append(currentNote)
left = right + 1
return res
class ScoreNote:
@classmethod
def fromGuitarNote(cls,guitarNote):
return cls(GuitarNote.notePitch[guitarNote.string] + guitarNote.fret, guitarNote.time)
def __init__(self,pitch,time):
self.pitch = pitch
self.time = time
def pitch(self):
return self.pitch
def __str__(self):
return str(self.pitch)
class GuitarNote:
maxFrets = 22
allowedStrings = ('e','B','G','D','A','E')
allowedFingers = (1,2,3,4)
notePitch = dict()
notePitch['E'] = 0
notePitch['A'] = notePitch['E'] + 5
notePitch['D'] = notePitch['A'] + 5
notePitch['G'] = notePitch['D'] + 5
notePitch['B'] = notePitch['G'] + 4
notePitch['e'] = notePitch['B'] + 5
stringLevel= dict()
stringLevel['E'] = 6
stringLevel['A'] = 5
stringLevel['D'] = 4
stringLevel['G'] = 3
stringLevel['B'] = 2
stringLevel['e'] = 1
@classmethod
def fromString(cls, stringToConstructFrom, time):
assert len(stringToConstructFrom) >= 2
string = stringToConstructFrom[0]
fret = int(stringToConstructFrom[1:])
assert string in cls.allowedStrings, "inexistent string: %s"%string
#assert 0 <= fret and fret <= cls.maxFrets, "fret is out of range: %i"%fret #Downgrade this assert to allow e.g. 'E27E29E32'
return cls(string, fret, time)
def __init__(self, string, fret, time):
self.string = string
self.fret = fret
self.time = time
def string(self):
return self.string
def fret(self):
return self.fret
def __str__(self):
#return str(self.string) + str(self.fret)
return str(self.stringLevel[self.string]) + "_" + str(self.fret)
def getPitch(self):
return GuitarNote.notePitch[self.string] + self.fret
def getStringLevel(self):
return GuitarNote.stringLevel[self.string]
class State:
def __init__(self, stringString, fret, finger, time):
assert stringString in GuitarNote.stringLevel
self.stringString = stringString
self.fret = fret
self.finger = finger
self.time = time
self.stringLevel = GuitarNote.stringLevel[stringString]
def __str__(self):
return str(GuitarNote.stringLevel[self.stringString]) + "_" + str(self.fret) + "_f" + str(self.finger) + "_t" + str(self.time)
def indexFingerPosition(self):
return self.fret + 1 - self.finger
def sameIndexFingerPosition(state1,state2):
return state1.indexFingerPosition() == state2.indexFingerPosition()
def calculateCompatibleStates(scoreNote):
# Complexity: O(S x H)
res = []
for string in GuitarNote.allowedStrings:
fret = scoreNote.pitch - GuitarNote.notePitch[string]
if 0 <= fret and fret <= GuitarNote.maxFrets:
guitarNote = GuitarNote(string, fret, scoreNote.time)
for finger in GuitarNote.allowedFingers:
compatibleState = State(string, fret, finger, scoreNote.time)
res.append(compatibleState)
return res
def buildHiddenStatesFromFilename(filename):
c = NoteFormatConverter()
scoreNotes = c.filenameGuitarNotesToScoreNotes(filename)
hiddenStates = buildHiddenStatesFromScoreNotes(scoreNotes)
return hiddenStates
def buildHiddenStatesFromScoreNotes(scoreNotes):
hiddenStates = []
for scoreNote in scoreNotes:
# Determine all compatible states for this note
compatibleStates = calculateCompatibleStates(scoreNote)
hiddenStates.append(compatibleStates)
return hiddenStates
def convertStringGuitarNotesToGuitarNotes(stringGuitarNotes):
c = NoteFormatConverter()
guitarNotes = c.stringToGuitarNotes(stringGuitarNotes)
return guitarNotes
def convertStringGuitarNotesToScoreNotes(stringGuitarNotes):
guitarNotes = convertStringGuitarNotesToGuitarNotes(stringGuitarNotes)
scoreNotes = [ScoreNote.fromGuitarNote(guitarNote) for guitarNote in guitarNotes]
return scoreNotes
def stateScore(state):
# Complexity: O(1)
badness = 0
# Using the pinky has a cost.
if state.finger == 4:
badness += Graph.WEIGHT_PINKY
# Playing on higher frets has a cost
badness += state.indexFingerPosition() * Graph.WEIGHT_INDEX_FINGER_POSITION
return -badness
def logPrior(state):
# Complexity: O(1)
return stateScore(state)
def moveScore(s1,s2):
# Complexity: O(1)
badness = 0
# same time => strings played bottom-up
if s1.time == s2.time and s1.stringLevel <= s2.stringLevel:
badness += Graph.INF
# Changing IFP has a cost
badness += abs(s1.indexFingerPosition() - s2.indexFingerPosition()) * Graph.WEIGHT_IFP_DELTA
return -badness
def computeTransitionLogLikelihood(state1, state2):
# Complexity: O(1)
score = moveScore(state1,state2) + stateScore(state2)
return score
class Penalties:
def __init__(self,wPinky = 1,wIndexFingerPosition = 0.1,wIFPDelta = 2):
self.wPinky = wPinky
self.wIndexFingerPosition = wIndexFingerPosition
self.wIFPDelta = wIFPDelta
class Graph:
# Global parameters
WEIGHT_PINKY = 0
WEIGHT_INDEX_FINGER_POSITION = 0
WEIGHT_IFP_DELTA = 0
INF = 100000000
@classmethod
def setPenalties(cls,penalties):
if hasattr(penalties, 'wPinky'):
cls.WEIGHT_PINKY = penalties.wPinky
if hasattr(penalties, 'wIndexFingerPosition'):
cls.WEIGHT_INDEX_FINGER_POSITION = penalties.wIndexFingerPosition
if hasattr(penalties, 'wIFPDelta'):
cls.WEIGHT_IFP_DELTA = penalties.wIFPDelta
def __init__(self, hiddenStates):
# Complexity: O(M x S^2 x H^2)
self.hiddenStates = hiddenStates
self.resizeGraph(hiddenStates)
for column in range(self.columns - 1):
# Conectar los estados de esta capa con los de la siguiete
for rowBefore in range(self.columnSizes[column]):
for rowAfter in range(self.columnSizes[column + 1]):
transitionLogLikelihood = computeTransitionLogLikelihood(self.hiddenStates[column][rowBefore],self.hiddenStates[column + 1][rowAfter])
self.g[column + 1][rowAfter][rowBefore] = transitionLogLikelihood
def resizeGraph(self, hiddenStates):
# Complexity: O(M x S^2 x H^2)
self.hiddenStates = hiddenStates
self.columns = len(hiddenStates)
self.columnSizes = [len(hiddenStates[column]) for column in range(self.columns)]
self.g = [None for column in range(self.columns)]
for column in range(1,self.columns):
self.g[column] = [None for columnSize in range(self.columnSizes[column])]
for row in range(self.columnSizes[column]):
self.g[column][row] = [0 for neighbour in range(self.columnSizes[column - 1])]
def longestPath(self):
# Complexity: O(M x S^2 x H^2)
self.createDPStructs()
# Base case
for row in range(self.columnSizes[0]):
self.dp[0][row] = logPrior(self.hiddenStates[0][row])
# Recursive cases
for column in range(1,self.columns):
for row in range(self.columnSizes[column]):
# dp[column][row]
for prevRow in range(self.columnSizes[column - 1]):
newScore = self.dp[column - 1][prevRow] + self.g[column][row][prevRow]
if newScore > self.dp[column][row]:
self.dp[column][row] = newScore
self.prev[column][row] = prevRow
# Recover longest paths
longestPath = []
currCol = self.columns - 1
currRow = 0
for row in range(self.columnSizes[self.columns - 1]):
if self.dp[self.columns - 1][row] > self.dp[self.columns - 1][currRow]:
currRow = row
print("Longest path has length: %f\n"%self.dp[self.columns - 1][currRow])
longestPath.append(currRow)
for currCol in range(self.columns - 1,0,-1):
currRow = self.prev[currCol][currRow]
longestPath.append(currRow)
longestPath = longestPath[::-1]
return longestPath
def createDPStructs(self):
# Complexity: O(M x S x H)
self.dp = [None for column in range(self.columns)]
self.prev = [None for column in range(self.columns)]
for column in range(self.columns):
self.dp[column] = [-self.INF for row in range(self.columnSizes[column])]
self.prev[column] = [-1 for row in range(self.columnSizes[column])]
def mostLikelyExplanation(self):
# Complexity: O(M x S^2 x H^2)
longestPath = self.longestPath()
assert len(longestPath) == self.columns
res = []
for column in range(self.columns):
row = longestPath[column]
state = self.hiddenStates[column][row]
res.append(state)
return res
# For local run as when written
def getOutputString(states):
n = max([state.time for state in states]) + 1
tab = [[None for c in range(n)] for r in range(len(GuitarNote.allowedStrings))]
for stateId in range(len(states)):
state = states[stateId]
stringLevel = state.stringLevel
column = state.time
tab[stringLevel - 1][column] = str(state.fret)
outputStr = ""
for row in range(len(GuitarNote.allowedStrings)):
outputStr += GuitarNote.allowedStrings[row] + ":\t"
for column in range(n):
if tab[row][column] is not None:
outputStr += str(tab[row][column])
outputStr += "\t"
outputStr += "\n"
return outputStr
def getOutputTabHTML(states):
n = max([state.time for state in states]) + 1
tab = [[None for c in range(n)] for r in range(len(GuitarNote.allowedStrings))]
for stateId in range(len(states)):
state = states[stateId]
stringLevel = state.stringLevel
column = state.time
tab[stringLevel - 1][column] = str(state.fret)
#~ outputStr = '<table style="width:100%">'
outputStr = '<table>'
for row in range(len(GuitarNote.allowedStrings)):
outputStr += '<tr>'
outputStr += '<td>'
outputStr += GuitarNote.allowedStrings[row] + ":"
outputStr += '</td>'
for column in range(n):
outputStr += '<td></td>'
outputStr += '<td>'
if tab[row][column] is not None:
outputStr += str(tab[row][column])
else:
outputStr += '-'
outputStr += '</td>'
outputStr += '</tr>'
outputStr += '</table>'
return outputStr
def toMusixtex(states):
# For latex
res = "\setlength\parindent{0pt}\\begin{music}\instrumentnumber{1}\\nobarnumbers\TAB1\setlines1{6}\startpiece"
n = max([state.time for state in states]) + 1
tab = [[None for c in range(n)] for r in range(len(GuitarNote.allowedStrings))]
for stateId in range(len(states)):
state = states[stateId]
stringLevel = state.stringLevel
column = state.time
tab[stringLevel - 1][column] = str(state.fret)
for t in range(n):
notesAtThisTime = "\\notes"
for stringLevel in range(1,len(GuitarNote.allowedStrings) + 1):
if tab[stringLevel - 1][t] is not None:
notesAtThisTime += "\str{" + str(len(GuitarNote.allowedStrings) + 1 - stringLevel) + "}{" + str(tab[stringLevel - 1][t]) + "}"
notesAtThisTime += "\en"
res += notesAtThisTime
res += "\endpiece\end{music}"
return res
def getOutputFingeringHTML(states):
n = max([state.time for state in states]) + 1
tab = [[None for c in range(n)] for r in range(len(GuitarNote.allowedStrings))]
for stateId in range(len(states)):
state = states[stateId]
stringLevel = state.stringLevel
column = state.time
tab[stringLevel - 1][column] = str(state.finger)
#~ outputStr = '<table style="width:100%">'
outputStr = '<table>'
for row in range(len(GuitarNote.allowedStrings)):
outputStr += '<tr>'
outputStr += '<td>'
outputStr += GuitarNote.allowedStrings[row] + ":"
outputStr += '</td>'
for column in range(n):
outputStr += '<td></td>'
outputStr += '<td>'
if tab[row][column] is not None:
outputStr += str(tab[row][column])
else:
outputStr += '-'
outputStr += '</td>'
outputStr += '</tr>'
outputStr += '</table>'
return outputStr
def getOutputScoreExplanationHTML(states):
n = max([state.time for state in states]) + 1
tab = [[None for c in range(n)] for r in range(len(GuitarNote.allowedStrings))]
for stateId in range(len(states)):
state = states[stateId]
stringLevel = state.stringLevel
column = state.time
if stateId == 0:
tab[stringLevel - 1][column] = logPrior(state)
else:
previousState = states[stateId - 1]
tab[stringLevel - 1][column] = computeTransitionLogLikelihood(previousState,state)
#~ outputStr = '<table style="width:100%">'
outputStr = '<table>'
for row in range(len(GuitarNote.allowedStrings)):
outputStr += '<tr>'
outputStr += '<td>'
outputStr += GuitarNote.allowedStrings[row] + ":"
outputStr += '</td>'
for column in range(n):
outputStr += '<td></td>'
outputStr += '<td>'
if tab[row][column] is not None:
outputStr += str(tab[row][column])
else:
outputStr += '-'
outputStr += '</td>'
outputStr += '</tr>'
outputStr += '</table>'
return outputStr
def calcAccuracy(guitarNotes,states):
assert(len(guitarNotes) == len(states))
assert(len(guitarNotes) > 0)
N = len(guitarNotes)
correct = 0
for i in range(N):
guitarNote = guitarNotes[i]
state = states[i]
if guitarNote.getStringLevel() == state.stringLevel and guitarNote.fret == state.fret:
correct += 1
return correct * 100 / N
def autoTab(stringGuitarNotes,wPinky,wIndexFingerPosition,wIFPDelta):
print("Processing stringGuitarNotes:\n%s"%stringGuitarNotes)
# Keep only the pitch of the input notes (when several notes are played simultaneously, sort them by pitch)
scoreNotes = convertStringGuitarNotesToScoreNotes(stringGuitarNotes)
# Set penalties
Graph.setPenalties(Penalties(float(wPinky),float(wIndexFingerPosition),float(wIFPDelta)))
# Get DAG nodes
hiddenStates = buildHiddenStatesFromScoreNotes(scoreNotes)
# Build graph
g = Graph(hiddenStates)
# Compute MLE
mostLikelyExplanation = g.mostLikelyExplanation()
# Compute number of correct notes
guitarNotes = convertStringGuitarNotesToGuitarNotes(stringGuitarNotes)
accuracy = calcAccuracy(guitarNotes,mostLikelyExplanation)
# Format results
tabHTML = getOutputTabHTML(mostLikelyExplanation)
fingeringHTML = getOutputFingeringHTML(mostLikelyExplanation)
scoreExplanationHTML = getOutputScoreExplanationHTML(mostLikelyExplanation)
print(toMusixtex(mostLikelyExplanation))
return ["Tab: (I got %i%% of the input tab correctly)\n"%accuracy + tabHTML,"Finger Annotations:\n" + fingeringHTML, 'Score Explanation:\n' + scoreExplanationHTML]