projectlosangeles commited on
Commit
28dfdc0
·
verified ·
1 Parent(s): 6ea89cc

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +300 -1
TMIDIX.py CHANGED
@@ -1816,7 +1816,7 @@ def plot_ms_SONG(ms_song,
1816
  plt.title(plot_title)
1817
 
1818
  if return_plt:
1819
- return plt
1820
 
1821
  plt.show()
1822
 
@@ -5091,6 +5091,305 @@ def delta_score_notes(score_notes,
5091
 
5092
  ###################################################################################
5093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5094
  # This is the end of the TMIDI X Python module
5095
 
5096
  ###################################################################################
 
1816
  plt.title(plot_title)
1817
 
1818
  if return_plt:
1819
+ return fig
1820
 
1821
  plt.show()
1822
 
 
5091
 
5092
  ###################################################################################
5093
 
5094
+ def check_and_fix_chords_in_chordified_score(chordified_score,
5095
+ channels_index=3,
5096
+ pitches_index=4
5097
+ ):
5098
+ fixed_chordified_score = []
5099
+
5100
+ bad_chords_counter = 0
5101
+
5102
+ for c in chordified_score:
5103
+
5104
+ tones_chord = sorted(set([t[pitches_index] % 12 for t in c if t[channels_index] != 9]))
5105
+
5106
+ if tones_chord:
5107
+
5108
+ if tones_chord not in ALL_CHORDS_SORTED:
5109
+ bad_chords_counter += 1
5110
+
5111
+ while tones_chord not in ALL_CHORDS_SORTED:
5112
+ tones_chord.pop(0)
5113
+
5114
+ new_chord = []
5115
+
5116
+ c.sort(key = lambda x: x[pitches_index], reverse=True)
5117
+
5118
+ for e in c:
5119
+ if e[channels_index] != 9:
5120
+ if e[pitches_index] % 12 in tones_chord:
5121
+ new_chord.append(e)
5122
+
5123
+ else:
5124
+ new_chord.append(e)
5125
+
5126
+ fixed_chordified_score.append(new_chord)
5127
+
5128
+ return fixed_chordified_score, bad_chords_counter
5129
+
5130
+ ###################################################################################
5131
+
5132
+ from itertools import combinations, groupby
5133
+
5134
+ ###################################################################################
5135
+
5136
+ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5137
+ channels_index=3,
5138
+ pitches_index=4,
5139
+ use_filtered_chords=True,
5140
+ skip_drums=False
5141
+ ):
5142
+ fixed_chordified_score = []
5143
+
5144
+ bad_chords_counter = 0
5145
+
5146
+ if use_filtered_chords:
5147
+ CHORDS = ALL_CHORDS_FILTERED
5148
+ else:
5149
+ CHORDS = ALL_CHORDS_SORTED
5150
+
5151
+ for c in chordified_score:
5152
+
5153
+ tones_chord = sorted(set([t[pitches_index] % 12 for t in c if t[channels_index] != 9]))
5154
+
5155
+ if tones_chord:
5156
+
5157
+ if tones_chord not in CHORDS:
5158
+
5159
+ pitches_chord = sorted(set([p[pitches_index] for p in c if p[channels_index] != 9]), reverse=True)
5160
+
5161
+ if len(tones_chord) == 2:
5162
+ tones_counts = Counter([p % 12 for p in pitches_chord]).most_common()
5163
+
5164
+ if tones_counts[0][1] > 1:
5165
+ tones_chord = [tones_counts[0][0]]
5166
+ elif tones_counts[1][1] > 1:
5167
+ tones_chord = [tones_counts[1][0]]
5168
+ else:
5169
+ tones_chord = [pitches_chord[0] % 12]
5170
+
5171
+ else:
5172
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord)-2, 0, -1) for comb in combinations(tones_chord, i+1)]
5173
+
5174
+ for co in tones_chord_combs:
5175
+ if co in CHORDS:
5176
+ tones_chord = co
5177
+ break
5178
+
5179
+ bad_chords_counter += 1
5180
+
5181
+ new_chord = []
5182
+
5183
+ c.sort(key = lambda x: x[pitches_index], reverse=True)
5184
+
5185
+ for e in c:
5186
+ if e[channels_index] != 9:
5187
+ if e[pitches_index] % 12 in tones_chord:
5188
+ new_chord.append(e)
5189
+
5190
+ else:
5191
+ if not skip_drums:
5192
+ new_chord.append(e)
5193
+
5194
+ fixed_chordified_score.append(new_chord)
5195
+
5196
+ return fixed_chordified_score, bad_chords_counter
5197
+
5198
+ ###################################################################################
5199
+
5200
+ def score_chord_to_tones_chord(chord,
5201
+ transpose_value=0,
5202
+ channels_index=3,
5203
+ pitches_index=4):
5204
+
5205
+ return sorted(set([(p[4]+transpose_value) % 12 for p in chord if p[channels_index] != 9]))
5206
+
5207
+ ###################################################################################
5208
+
5209
+ def grouped_set(seq):
5210
+ return [k for k, v in groupby(seq)]
5211
+
5212
+ ###################################################################################
5213
+
5214
+ def ordered_set(seq):
5215
+ dic = {}
5216
+ return [k for k, v in dic.fromkeys(seq).items()]
5217
+
5218
+ ###################################################################################
5219
+
5220
+ def add_melody_to_enhanced_score_notes(enhanced_score_notes,
5221
+ melody_start_time=0,
5222
+ melody_start_chord=0,
5223
+ melody_notes_min_duration=-1,
5224
+ melody_notes_max_duration=255,
5225
+ melody_duration_overlap_tolerance=4,
5226
+ melody_avg_duration_divider=2,
5227
+ melody_base_octave=5,
5228
+ melody_channel=3,
5229
+ melody_patch=40,
5230
+ melody_max_velocity=110,
5231
+ acc_max_velocity=90,
5232
+ pass_drums=True
5233
+ ):
5234
+
5235
+ if pass_drums:
5236
+ score = copy.deepcopy(enhanced_score_notes)
5237
+ else:
5238
+ score = [e for e in copy.deepcopy(enhanced_score_notes) if e[3] !=9]
5239
+
5240
+ if melody_notes_min_duration > 0:
5241
+ min_duration = melody_notes_min_duration
5242
+ else:
5243
+ durs = [d[2] for d in score]
5244
+ min_duration = Counter(durs).most_common()[0][0]
5245
+
5246
+ adjust_score_velocities(score, acc_max_velocity)
5247
+
5248
+ cscore = chordify_score([1000, score])
5249
+
5250
+ melody_score = []
5251
+ acc_score = []
5252
+
5253
+ pt = melody_start_time
5254
+
5255
+ for c in cscore[:melody_start_chord]:
5256
+ acc_score.extend(c)
5257
+
5258
+ for c in cscore[melody_start_chord:]:
5259
+
5260
+ durs = [d[2] if d[3] != 9 else -1 for d in c]
5261
+
5262
+ if not all(d == -1 for d in durs):
5263
+ ndurs = [d for d in durs if d != -1]
5264
+ avg_dur = (sum(ndurs) / len(ndurs)) / melody_avg_duration_divider
5265
+ best_dur = min(durs, key=lambda x:abs(x-avg_dur))
5266
+ pidx = durs.index(best_dur)
5267
+
5268
+ cc = copy.deepcopy(c[pidx])
5269
+
5270
+ if c[0][1] >= pt - melody_duration_overlap_tolerance and best_dur >= min_duration:
5271
+
5272
+ cc[3] = melody_channel
5273
+ cc[4] = (c[pidx][4] % 24)
5274
+ cc[5] = 100 + ((c[pidx][4] % 12) * 2)
5275
+ cc[6] = melody_patch
5276
+
5277
+ melody_score.append(cc)
5278
+ acc_score.extend(c)
5279
+
5280
+ pt = c[0][1]+c[pidx][2]
5281
+
5282
+ else:
5283
+ acc_score.extend(c)
5284
+
5285
+ else:
5286
+ acc_score.extend(c)
5287
+
5288
+ values = [e[4] % 24 for e in melody_score]
5289
+ smoothed = [values[0]]
5290
+ for i in range(1, len(values)):
5291
+ if abs(smoothed[-1] - values[i]) >= 12:
5292
+ if smoothed[-1] < values[i]:
5293
+ smoothed.append(values[i] - 12)
5294
+ else:
5295
+ smoothed.append(values[i] + 12)
5296
+ else:
5297
+ smoothed.append(values[i])
5298
+
5299
+ smoothed_melody = copy.deepcopy(melody_score)
5300
+
5301
+ for i, e in enumerate(smoothed_melody):
5302
+ e[4] = (melody_base_octave * 12) + smoothed[i]
5303
+
5304
+ for i, m in enumerate(smoothed_melody[1:]):
5305
+ if m[1] - smoothed_melody[i][1] < melody_notes_max_duration:
5306
+ smoothed_melody[i][2] = m[1] - smoothed_melody[i][1]
5307
+
5308
+ adjust_score_velocities(smoothed_melody, melody_max_velocity)
5309
+
5310
+ final_score = sorted(smoothed_melody + acc_score, key=lambda x: (x[1], -x[4]))
5311
+
5312
+ return final_score
5313
+
5314
+ ###################################################################################
5315
+
5316
+ def find_paths(list_of_lists, path=[]):
5317
+ if not list_of_lists:
5318
+ return [path]
5319
+ return [p for sublist in list_of_lists[0] for p in find_paths(list_of_lists[1:], path+[sublist])]
5320
+
5321
+ ###################################################################################
5322
+
5323
+ def recalculate_score_timings(score, start_time=0):
5324
+
5325
+ rscore = copy.deepcopy(score)
5326
+
5327
+ pe = rscore[0]
5328
+
5329
+ abs_time = start_time
5330
+
5331
+ for e in rscore:
5332
+
5333
+ dtime = e[1] - pe[1]
5334
+ pe = copy.deepcopy(e)
5335
+ abs_time += dtime
5336
+ e[1] = abs_time
5337
+
5338
+ return rscore
5339
+
5340
+ ###################################################################################
5341
+
5342
+ WHITE_NOTES = [0, 2, 4, 5, 7, 9, 11]
5343
+ BLACK_NOTES = [1, 3, 6, 8, 10]
5344
+
5345
+ ###################################################################################
5346
+
5347
+ ALL_CHORDS_FILTERED = [[0], [0, 3], [0, 3, 5], [0, 3, 5, 8], [0, 3, 5, 9], [0, 3, 5, 10], [0, 3, 7],
5348
+ [0, 3, 7, 10], [0, 3, 8], [0, 3, 9], [0, 3, 10], [0, 4], [0, 4, 6],
5349
+ [0, 4, 6, 9], [0, 4, 6, 10], [0, 4, 7], [0, 4, 7, 10], [0, 4, 8], [0, 4, 9],
5350
+ [0, 4, 10], [0, 5], [0, 5, 8], [0, 5, 9], [0, 5, 10], [0, 6], [0, 6, 9],
5351
+ [0, 6, 10], [0, 7], [0, 7, 10], [0, 8], [0, 9], [0, 10], [1], [1, 4],
5352
+ [1, 4, 6], [1, 4, 6, 9], [1, 4, 6, 10], [1, 4, 6, 11], [1, 4, 7],
5353
+ [1, 4, 7, 10], [1, 4, 7, 11], [1, 4, 8], [1, 4, 8, 11], [1, 4, 9], [1, 4, 10],
5354
+ [1, 4, 11], [1, 5], [1, 5, 8], [1, 5, 8, 11], [1, 5, 9], [1, 5, 10],
5355
+ [1, 5, 11], [1, 6], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 7], [1, 7, 10],
5356
+ [1, 7, 11], [1, 8], [1, 8, 11], [1, 9], [1, 10], [1, 11], [2], [2, 5],
5357
+ [2, 5, 8], [2, 5, 8, 11], [2, 5, 9], [2, 5, 10], [2, 5, 11], [2, 6], [2, 6, 9],
5358
+ [2, 6, 10], [2, 6, 11], [2, 7], [2, 7, 10], [2, 7, 11], [2, 8], [2, 8, 11],
5359
+ [2, 9], [2, 10], [2, 11], [3], [3, 5], [3, 5, 8], [3, 5, 8, 11], [3, 5, 9],
5360
+ [3, 5, 10], [3, 5, 11], [3, 7], [3, 7, 10], [3, 7, 11], [3, 8], [3, 8, 11],
5361
+ [3, 9], [3, 10], [3, 11], [4], [4, 6], [4, 6, 9], [4, 6, 10], [4, 6, 11],
5362
+ [4, 7], [4, 7, 10], [4, 7, 11], [4, 8], [4, 8, 11], [4, 9], [4, 10], [4, 11],
5363
+ [5], [5, 8], [5, 8, 11], [5, 9], [5, 10], [5, 11], [6], [6, 9], [6, 10],
5364
+ [6, 11], [7], [7, 10], [7, 11], [8], [8, 11], [9], [10], [11]]
5365
+
5366
+ ###################################################################################
5367
+
5368
+ def harmonize_enhanced_melody_score_notes(enhanced_melody_score_notes):
5369
+
5370
+ mel_tones = [e[4] % 12 for e in enhanced_melody_score_notes]
5371
+
5372
+ cur_chord = []
5373
+
5374
+ song = []
5375
+
5376
+ for i, m in enumerate(mel_tones):
5377
+ cur_chord.append(m)
5378
+ cc = sorted(set(cur_chord))
5379
+
5380
+ if cc in ALL_CHORDS_FILTERED:
5381
+ song.append(cc)
5382
+
5383
+ else:
5384
+ while sorted(set(cur_chord)) not in ALL_CHORDS_FILTERED:
5385
+ cur_chord.pop(0)
5386
+ cc = sorted(set(cur_chord))
5387
+ song.append(cc)
5388
+
5389
+ return song
5390
+
5391
+ ###################################################################################
5392
+
5393
  # This is the end of the TMIDI X Python module
5394
 
5395
  ###################################################################################