projectlosangeles commited on
Commit
e8c175e
1 Parent(s): dbe77a4

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +143 -2
TMIDIX.py CHANGED
@@ -9111,10 +9111,151 @@ def count_bad_chords_in_chordified_score(chordified_score,
9111
  if tones_chord not in CHORDS:
9112
  bad_chords_count += 1
9113
 
9114
- return [bad_chords_count, len(chordified_score)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9115
 
9116
  ###################################################################################
9117
  #
9118
  # This is the end of the TMIDI X Python module
9119
  #
9120
- ###################################################################################
 
9111
  if tones_chord not in CHORDS:
9112
  bad_chords_count += 1
9113
 
9114
+ return [bad_chords_count, len(chordified_score)]
9115
+
9116
+ ###################################################################################
9117
+
9118
+ def needleman_wunsch_aligner(seq1,
9119
+ seq2,
9120
+ align_idx,
9121
+ gap_penalty=-1,
9122
+ match_score=2,
9123
+ mismatch_penalty=-1
9124
+ ):
9125
+
9126
+ n = len(seq1)
9127
+ m = len(seq2)
9128
+
9129
+ score_matrix = [[0] * (m + 1) for _ in range(n + 1)]
9130
+
9131
+ for i in range(1, n + 1):
9132
+ score_matrix[i][0] = gap_penalty * i
9133
+ for j in range(1, m + 1):
9134
+ score_matrix[0][j] = gap_penalty * j
9135
+
9136
+ for i in range(1, n + 1):
9137
+ for j in range(1, m + 1):
9138
+ match = score_matrix[i-1][j-1] + (match_score if seq1[i-1][align_idx] == seq2[j-1][align_idx] else mismatch_penalty)
9139
+ delete = score_matrix[i-1][j] + gap_penalty
9140
+ insert = score_matrix[i][j-1] + gap_penalty
9141
+ score_matrix[i][j] = max(match, delete, insert)
9142
+
9143
+ align1, align2 = [], []
9144
+ i, j = n, m
9145
+
9146
+ while i > 0 and j > 0:
9147
+
9148
+ score = score_matrix[i][j]
9149
+ score_diag = score_matrix[i-1][j-1]
9150
+ score_up = score_matrix[i-1][j]
9151
+ score_left = score_matrix[i][j-1]
9152
+
9153
+ if score == score_diag + (match_score if seq1[i-1][align_idx] == seq2[j-1][align_idx] else mismatch_penalty):
9154
+ align1.append(seq1[i-1])
9155
+ align2.append(seq2[j-1])
9156
+ i -= 1
9157
+ j -= 1
9158
+ elif score == score_up + gap_penalty:
9159
+ align1.append(seq1[i-1])
9160
+ align2.append([None] * 6)
9161
+ i -= 1
9162
+ elif score == score_left + gap_penalty:
9163
+ align1.append([None] * 6)
9164
+ align2.append(seq2[j-1])
9165
+ j -= 1
9166
+
9167
+ while i > 0:
9168
+ align1.append(seq1[i-1])
9169
+ align2.append([None] * 6)
9170
+ i -= 1
9171
+ while j > 0:
9172
+ align1.append([None] * 6)
9173
+ align2.append(seq2[j-1])
9174
+ j -= 1
9175
+
9176
+ align1.reverse()
9177
+ align2.reverse()
9178
+
9179
+ return align1, align2
9180
+
9181
+ ###################################################################################
9182
+
9183
+ def align_escore_notes_to_escore_notes(src_escore_notes,
9184
+ trg_escore_notes,
9185
+ recalculate_scores_timings=True,
9186
+ pitches_idx=4
9187
+ ):
9188
+
9189
+ if recalculate_scores_timings:
9190
+ src_escore_notes = recalculate_score_timings(src_escore_notes)
9191
+ trg_escore_notes = recalculate_score_timings(trg_escore_notes)
9192
+
9193
+ src_align1, trg_align2 = needleman_wunsch_aligner(src_escore_notes, trg_escore_notes, pitches_idx)
9194
+
9195
+ aligned_scores = [[al[0], al[1]] for al in zip(src_align1, trg_align2) if al[0][0] is not None and al[1][0] is not None]
9196
+
9197
+ return aligned_scores
9198
+
9199
+ ###################################################################################
9200
+
9201
+ def t_to_n(arr, si, t):
9202
+
9203
+ ct = 0
9204
+ ci = si
9205
+
9206
+ while ct + arr[ci][1] < t and ci < len(arr)-1:
9207
+ ct += arr[ci][1]
9208
+ ci += 1
9209
+
9210
+ return ci+1
9211
+
9212
+ ###################################################################################
9213
+
9214
+ def max_sum_chunk_idxs(arr, t=255):
9215
+
9216
+ n = t_to_n(arr, 0, t)
9217
+
9218
+ if n > len(arr):
9219
+ return [0, n]
9220
+
9221
+ max_sum = 0
9222
+ max_sum_start_index = 0
9223
+
9224
+ max_sum_start_idxs = [0, len(arr), sum([a[0] for a in arr])]
9225
+
9226
+ for i in range(len(arr)):
9227
+
9228
+ n = t_to_n(arr, i, t)
9229
+
9230
+ current_sum = sum([a[0] for a in arr[i:n]])
9231
+ current_time = sum([a[1] for a in arr[i:n]])
9232
+
9233
+ if current_sum > max_sum and current_time <= t:
9234
+ max_sum = current_sum
9235
+ max_sum_start_idxs = [i, n, max_sum]
9236
+
9237
+ return max_sum_start_idxs
9238
+
9239
+ ###################################################################################
9240
+
9241
+ def find_highest_density_escore_notes_chunk(escore_notes, max_chunk_time=512):
9242
+
9243
+ dscore = delta_score_notes(escore_notes)
9244
+
9245
+ cscore = chordify_score([d[1:] for d in dscore])
9246
+
9247
+ notes_counts = [[len(c), c[0][0]] for c in cscore]
9248
+
9249
+ msc_idxs = max_sum_chunk_idxs(notes_counts, max_chunk_time)
9250
+
9251
+ chunk_dscore = [['note'] + c for c in flatten(cscore[msc_idxs[0]:msc_idxs[1]])]
9252
+
9253
+ chunk_escore = recalculate_score_timings(delta_score_to_abs_score(chunk_dscore))
9254
+
9255
+ return chunk_escore
9256
 
9257
  ###################################################################################
9258
  #
9259
  # This is the end of the TMIDI X Python module
9260
  #
9261
+ ###################################################################################