asigalov61 commited on
Commit
2846f62
1 Parent(s): 86498fa

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +391 -0
TMIDIX.py CHANGED
@@ -7034,6 +7034,397 @@ def transpose_escore_notes_to_pitch(escore_notes,
7034
 
7035
  ###################################################################################
7036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7037
  # This is the end of the TMIDI X Python module
7038
 
7039
  ###################################################################################
 
7034
 
7035
  ###################################################################################
7036
 
7037
+ CHORDS_TYPES = ['WHITE', 'BLACK', 'UNKNOWN', 'MIXED WHITE', 'MIXED BLACK', 'MIXED GRAY']
7038
+
7039
+ ###################################################################################
7040
+
7041
+ def tones_chord_type(tones_chord,
7042
+ return_chord_type_index=True,
7043
+ use_filtered_chords=True
7044
+ ):
7045
+
7046
+ WN = WHITE_NOTES
7047
+ BN = BLACK_NOTES
7048
+ MX = WHITE_NOTES + BLACK_NOTES
7049
+
7050
+ if use_filtered_chords:
7051
+ CHORDS = ALL_CHORDS_FILTERED
7052
+
7053
+ else:
7054
+ CHORDS = ALL_CHORDS_SORTED
7055
+
7056
+ tones_chord = sorted(tones_chord)
7057
+
7058
+ ctype = 'UNKNOWN'
7059
+
7060
+ if tones_chord in CHORDS:
7061
+
7062
+ if sorted(set(tones_chord) & set(WN)) == tones_chord:
7063
+ ctype = 'WHITE'
7064
+
7065
+ elif sorted(set(tones_chord) & set(BN)) == tones_chord:
7066
+ ctype = 'BLACK'
7067
+
7068
+ if len(tones_chord) > 1 and sorted(set(tones_chord) & set(MX)) == tones_chord:
7069
+
7070
+ if len(sorted(set(tones_chord) & set(WN))) == len(sorted(set(tones_chord) & set(BN))):
7071
+ ctype = 'MIXED GRAY'
7072
+
7073
+ elif len(sorted(set(tones_chord) & set(WN))) > len(sorted(set(tones_chord) & set(BN))):
7074
+ ctype = 'MIXED WHITE'
7075
+
7076
+ elif len(sorted(set(tones_chord) & set(WN))) < len(sorted(set(tones_chord) & set(BN))):
7077
+ ctype = 'MIXED BLACK'
7078
+
7079
+ if return_chord_type_index:
7080
+ return CHORDS_TYPES.index(ctype)
7081
+
7082
+ else:
7083
+ return ctype
7084
+
7085
+ ###################################################################################
7086
+
7087
+ def tone_type(tone,
7088
+ return_tone_type_index=True
7089
+ ):
7090
+
7091
+ tone = tone % 12
7092
+
7093
+ if tone in BLACK_NOTES:
7094
+ if return_tone_type_index:
7095
+ return CHORDS_TYPES.index('BLACK')
7096
+ else:
7097
+ return "BLACK"
7098
+
7099
+ else:
7100
+ if return_tone_type_index:
7101
+ return CHORDS_TYPES.index('WHITE')
7102
+ else:
7103
+ return "WHITE"
7104
+
7105
+ ###################################################################################
7106
+
7107
+ def lists_sym_differences(src_list, trg_list):
7108
+ return list(set(src_list) ^ set(trg_list))
7109
+
7110
+ ###################################################################################
7111
+
7112
+ def lists_differences(long_list, short_list):
7113
+ return list(set(long_list) - set(short_list))
7114
+
7115
+ ###################################################################################
7116
+
7117
+ def find_best_tones_chord(src_tones_chords,
7118
+ trg_tones_chords,
7119
+ find_longest=True
7120
+ ):
7121
+
7122
+ not_seen_trg_chords = []
7123
+
7124
+ max_len = 0
7125
+
7126
+ for tc in trg_tones_chords:
7127
+ if sorted(tc) in src_tones_chords:
7128
+ not_seen_trg_chords.append(sorted(tc))
7129
+ max_len = max(max_len, len(tc))
7130
+
7131
+ if not not_seen_trg_chords:
7132
+ max_len = len(max(trg_tones_chords, key=len))
7133
+ not_seen_trg_chords = trg_tones_chords
7134
+
7135
+ if find_longest:
7136
+ return random.choice([c for c in not_seen_trg_chords if len(c) == max_len])
7137
+
7138
+ else:
7139
+ return random.choice(not_seen_trg_chords)
7140
+
7141
+ ###################################################################################
7142
+
7143
+ def find_matching_tones_chords(tones_chord,
7144
+ matching_chord_length=-1,
7145
+ match_chord_type=True,
7146
+ use_filtered_chords=True
7147
+ ):
7148
+
7149
+ if use_filtered_chords:
7150
+ CHORDS = ALL_CHORDS_FILTERED
7151
+ else:
7152
+ CHORDS = ALL_CHORDS_SORTED
7153
+
7154
+ tones_chord = sorted(tones_chord)
7155
+
7156
+ tclen = len(tones_chord)
7157
+
7158
+ tctype = tones_chord_type(tones_chord, use_filtered_chords=use_filtered_chords)
7159
+
7160
+ matches = []
7161
+
7162
+ for tc in CHORDS:
7163
+
7164
+ if matching_chord_length == -1:
7165
+ if len(tc) > tclen:
7166
+ if sorted(lists_intersections(tc, tones_chord)) == tones_chord:
7167
+ if match_chord_type:
7168
+ if tones_chord_type(tc, use_filtered_chords=use_filtered_chords) == tctype:
7169
+ tcdiffs = lists_differences(tc, tones_chord)
7170
+ if all(tone_type(d) == tctype % 3 for d in tcdiffs):
7171
+ matches.append(tc)
7172
+ else:
7173
+ matches.append(tc)
7174
+
7175
+ else:
7176
+
7177
+ if len(tc) == max(tclen, matching_chord_length):
7178
+ if sorted(lists_intersections(tc, tones_chord)) == tones_chord:
7179
+ if match_chord_type:
7180
+ if tones_chord_type(tc, use_filtered_chords=use_filtered_chords) == tctype:
7181
+ tcdiffs = lists_differences(tc, tones_chord)
7182
+ if all(tone_type(d) == tctype % 3 for d in tcdiffs):
7183
+ matches.append(tc)
7184
+ else:
7185
+ matches.append(tc)
7186
+
7187
+ return sorted(matches, key=len)
7188
+
7189
+ ###################################################################################
7190
+
7191
+ def adjust_list_of_values_to_target_average(list_of_values,
7192
+ trg_avg,
7193
+ min_value,
7194
+ max_value
7195
+ ):
7196
+
7197
+ filtered_values = [value for value in list_of_values if min_value <= value <= max_value]
7198
+
7199
+ if not filtered_values:
7200
+ return list_of_values
7201
+
7202
+ current_avg = sum(filtered_values) / len(filtered_values)
7203
+ scale_factor = trg_avg / current_avg
7204
+
7205
+ adjusted_values = [value * scale_factor for value in filtered_values]
7206
+
7207
+ total_difference = trg_avg * len(filtered_values) - sum(adjusted_values)
7208
+ adjustment_per_value = total_difference / len(filtered_values)
7209
+
7210
+ final_values = [value + adjustment_per_value for value in adjusted_values]
7211
+
7212
+ while abs(sum(final_values) / len(final_values) - trg_avg) > 1e-6:
7213
+ total_difference = trg_avg * len(final_values) - sum(final_values)
7214
+ adjustment_per_value = total_difference / len(final_values)
7215
+ final_values = [value + adjustment_per_value for value in final_values]
7216
+
7217
+ final_values = [round(value) for value in final_values]
7218
+
7219
+ adjusted_values = copy.deepcopy(list_of_values)
7220
+
7221
+ j = 0
7222
+
7223
+ for i in range(len(adjusted_values)):
7224
+ if min_value <= adjusted_values[i] <= max_value:
7225
+ adjusted_values[i] = final_values[j]
7226
+ j += 1
7227
+
7228
+ return adjusted_values
7229
+
7230
+ ###################################################################################
7231
+
7232
+ def adjust_escore_notes_to_average(escore_notes,
7233
+ trg_avg,
7234
+ min_value=1,
7235
+ max_value=4000,
7236
+ times_index=1,
7237
+ durs_index=2,
7238
+ score_is_delta=False,
7239
+ return_delta_scpre=False
7240
+ ):
7241
+ if score_is_delta:
7242
+ delta_escore_notes = copy.deepcopy(escore_notes)
7243
+
7244
+ else:
7245
+ delta_escore_notes = delta_score_notes(escore_notes)
7246
+
7247
+ times = [[e[times_index], e[durs_index]] for e in delta_escore_notes]
7248
+
7249
+ filtered_values = [value for value in times if min_value <= value[0] <= max_value]
7250
+
7251
+ if not filtered_values:
7252
+ return escore_notes
7253
+
7254
+ current_avg = sum([v[0] for v in filtered_values]) / len([v[0] for v in filtered_values])
7255
+ scale_factor = trg_avg / current_avg
7256
+
7257
+ adjusted_values = [[value[0] * scale_factor, value[1] * scale_factor] for value in filtered_values]
7258
+
7259
+ total_difference = trg_avg * len([v[0] for v in filtered_values]) - sum([v[0] for v in adjusted_values])
7260
+ adjustment_per_value = total_difference / len(filtered_values)
7261
+
7262
+ final_values = [[value[0] + adjustment_per_value, value[1] + adjustment_per_value] for value in adjusted_values]
7263
+
7264
+ while abs(sum([v[0] for v in final_values]) / len(final_values) - trg_avg) > 1e-6:
7265
+ total_difference = trg_avg * len(final_values) - sum([v[0] for v in final_values])
7266
+ adjustment_per_value = total_difference / len(final_values)
7267
+ final_values = [[value[0] + adjustment_per_value, value[1] + adjustment_per_value] for value in final_values]
7268
+
7269
+ final_values = [[round(value[0]), round(value[1])] for value in final_values]
7270
+
7271
+ adjusted_delta_score = copy.deepcopy(delta_escore_notes)
7272
+
7273
+ j = 0
7274
+
7275
+ for i in range(len(adjusted_delta_score)):
7276
+ if min_value <= adjusted_delta_score[i][1] <= max_value:
7277
+ adjusted_delta_score[i][times_index] = final_values[j][0]
7278
+ adjusted_delta_score[i][durs_index] = final_values[j][1]
7279
+ j += 1
7280
+
7281
+ adjusted_escore_notes = delta_score_to_abs_score(adjusted_delta_score)
7282
+
7283
+ if return_delta_scpre:
7284
+ return adjusted_delta_score
7285
+
7286
+ else:
7287
+ return adjusted_escore_notes
7288
+
7289
+ ###################################################################################
7290
+
7291
+ def harmonize_enhanced_melody_score_notes_to_ms_SONG(escore_notes,
7292
+ melody_velocity=-1,
7293
+ melody_channel=3,
7294
+ melody_patch=40,
7295
+ melody_base_octave=4,
7296
+ harmonized_tones_chords_velocity=-1,
7297
+ harmonized_tones_chords_channel=0,
7298
+ harmonized_tones_chords_patch=0
7299
+ ):
7300
+
7301
+ harmonized_tones_chords = harmonize_enhanced_melody_score_notes(escore_notes)
7302
+
7303
+ harm_escore_notes = []
7304
+
7305
+ time = 0
7306
+
7307
+ for i, note in enumerate(escore_notes):
7308
+
7309
+ time = note[1]
7310
+ dur = note[2]
7311
+ ptc = note[4]
7312
+
7313
+ if melody_velocity == -1:
7314
+ vel = int(110 + ((ptc % 12) * 1.5))
7315
+ else:
7316
+ vel = melody_velocity
7317
+
7318
+ harm_escore_notes.append(['note', time, dur, melody_channel, ptc, vel, melody_patch])
7319
+
7320
+ for t in harmonized_tones_chords[i]:
7321
+
7322
+ ptc = (melody_base_octave * 12) + t
7323
+
7324
+ if harmonized_tones_chords_velocity == -1:
7325
+ vel = int(80 + ((ptc % 12) * 1.5))
7326
+ else:
7327
+ vel = harmonized_tones_chords_velocity
7328
+
7329
+ harm_escore_notes.append(['note', time, dur, harmonized_tones_chords_channel, ptc, vel, harmonized_tones_chords_patch])
7330
+
7331
+ return sorted(harm_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
7332
+
7333
+ ###################################################################################
7334
+
7335
+ def check_and_fix_pitches_chord(pitches_chord,
7336
+ use_filtered_chords=True
7337
+ ):
7338
+
7339
+ pitches_chord = sorted(pitches_chord, reverse=True)
7340
+
7341
+ if use_filtered_chords:
7342
+ CHORDS = ALL_CHORDS_FILTERED
7343
+ else:
7344
+ CHORDS = ALL_CHORDS_SORTED
7345
+
7346
+ tones_chord = sorted(set([p % 12 for p in pitches_chord]))
7347
+
7348
+ if tones_chord not in CHORDS:
7349
+
7350
+ if len(tones_chord) == 2:
7351
+
7352
+ tones_counts = Counter([p % 12 for p in pitches_chord]).most_common()
7353
+
7354
+ if tones_counts[0][1] > 1:
7355
+ tones_chord = [tones_counts[0][0]]
7356
+ elif tones_counts[1][1] > 1:
7357
+ tones_chord = [tones_counts[1][0]]
7358
+ else:
7359
+ tones_chord = [pitches_chord[0] % 12]
7360
+
7361
+ if len(tones_chord) > 2:
7362
+
7363
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord)-2, 0, -1) for comb in combinations(tones_chord, i+1)]
7364
+
7365
+ for co in tones_chord_combs:
7366
+ if co in CHORDS:
7367
+ tones_chord = co
7368
+ break
7369
+
7370
+ new_pitches_chord = []
7371
+
7372
+ for p in pitches_chord:
7373
+
7374
+ if p % 12 in tones_chord:
7375
+ new_pitches_chord.append(p)
7376
+
7377
+ return sorted(new_pitches_chord, reverse=True)
7378
+
7379
+ ###################################################################################
7380
+
7381
+ ALL_CHORDS_TRANS = [[0], [0, 4], [0, 4, 7], [0, 4, 8], [0, 5], [0, 6], [0, 7], [0, 8], [1], [1, 5],
7382
+ [1, 5, 9], [1, 6], [1, 7], [1, 8], [1, 9], [2], [2, 6], [2, 6, 10], [2, 7],
7383
+ [2, 8], [2, 9], [2, 10], [3], [3, 7], [3, 7, 11], [3, 8], [3, 9], [3, 10],
7384
+ [3, 11], [4], [4, 7], [4, 7, 11], [4, 8], [4, 9], [4, 10], [4, 11], [5],
7385
+ [5, 9], [5, 10], [5, 11], [6], [6, 10], [6, 11], [7], [7, 11], [8], [9], [10],
7386
+ [11]]
7387
+
7388
+ ###################################################################################
7389
+
7390
+ def minkowski_distance(x, y, p=3):
7391
+
7392
+ if len(x) != len(y):
7393
+ return -1
7394
+
7395
+ distance = 0
7396
+
7397
+ for i in range(len(x)):
7398
+ distance += abs(x[i] - y[i]) ** p
7399
+
7400
+ return distance ** (1 / p)
7401
+
7402
+ ###################################################################################
7403
+
7404
+ def dot_product(x, y):
7405
+ return sum(xi * yi for xi, yi in zip(x, y))
7406
+
7407
+ def norm(vector):
7408
+ return sum(xi ** 2 for xi in vector) ** 0.5
7409
+
7410
+ def cosine_similarity(x, y):
7411
+
7412
+ if len(x) != len(y):
7413
+ return -1
7414
+
7415
+ dot_prod = dot_product(x, y)
7416
+
7417
+ norm_x = norm(x)
7418
+ norm_y = norm(y)
7419
+
7420
+ if norm_x == 0 or norm_y == 0:
7421
+ return 0.0
7422
+
7423
+ else:
7424
+ return dot_prod / (norm_x * norm_y)
7425
+
7426
+ ###################################################################################
7427
+
7428
  # This is the end of the TMIDI X Python module
7429
 
7430
  ###################################################################################