asigalov61 commited on
Commit
f3c819f
1 Parent(s): e3ad9dc

Upload 2 files

Browse files
Files changed (2) hide show
  1. TMIDIX.py +2640 -171
  2. x_transformer_1_23_2.py +0 -0
TMIDIX.py CHANGED
@@ -471,19 +471,6 @@ def grep(score=None, channels=None):
471
  itrack += 1
472
  return new_score
473
 
474
- def play_score(score=None):
475
- r'''Converts the "score" to midi, and feeds it into 'aplaymidi -'
476
- '''
477
- if score == None:
478
- return
479
- import subprocess
480
- pipe = subprocess.Popen(['aplaymidi','-'], stdin=subprocess.PIPE)
481
- if score_type(score) == 'opus':
482
- pipe.stdin.write(opus2midi(score))
483
- else:
484
- pipe.stdin.write(score2midi(score))
485
- pipe.stdin.close()
486
-
487
  def score2stats(opus_or_score=None):
488
  r'''Returns a dict of some basic stats about the score, like
489
  bank_select (list of tuples (msb,lsb)),
@@ -1484,6 +1471,7 @@ from abc import ABC, abstractmethod
1484
  from difflib import SequenceMatcher as SM
1485
 
1486
  import statistics
 
1487
 
1488
  import matplotlib.pyplot as plt
1489
 
@@ -1762,7 +1750,10 @@ def plot_ms_SONG(ms_song,
1762
  note_height = 0.75,
1763
  show_grid_lines=False,
1764
  return_plt = False,
1765
- timings_multiplier=1
 
 
 
1766
  ):
1767
 
1768
  '''Tegridy ms SONG plotter/vizualizer'''
@@ -1816,10 +1807,22 @@ def plot_ms_SONG(ms_song,
1816
 
1817
  plt.title(plot_title)
1818
 
 
 
 
 
 
 
 
 
 
 
 
1819
  if return_plt:
1820
  return fig
1821
 
1822
  plt.show()
 
1823
 
1824
  ###################################################################################
1825
 
@@ -1932,7 +1935,7 @@ def Tegridy_Any_Pickle_File_Reader(input_file_name='TMIDI_Pickle_File', ext='.pi
1932
 
1933
  '''Tegridy Pickle File Loader
1934
 
1935
- Input: Full path and file name without extention
1936
  File extension if different from default .pickle
1937
 
1938
  Output: Standard Python 3 unpickled data object
@@ -1944,7 +1947,13 @@ def Tegridy_Any_Pickle_File_Reader(input_file_name='TMIDI_Pickle_File', ext='.pi
1944
  print('Tegridy Pickle File Loader')
1945
  print('Loading the pickle file. Please wait...')
1946
 
1947
- with open(input_file_name + ext, 'rb') as pickle_file:
 
 
 
 
 
 
1948
  content = pickle.load(pickle_file)
1949
 
1950
  if verbose:
@@ -2037,7 +2046,7 @@ def Optimus_MIDI_TXT_Processor(MIDI_file,
2037
 
2038
  #print('Loading MIDI file...')
2039
  midi_file = open(MIDI_file, 'rb')
2040
- if debug: print('Processing File:', file_address)
2041
 
2042
  try:
2043
  opus = midi2opus(midi_file.read())
@@ -3519,12 +3528,19 @@ def Tegridy_Split_List(list_to_split, split_value=0):
3519
 
3520
  # Binary chords functions
3521
 
3522
- def tones_chord_to_bits(chord):
 
3523
  bits = [0] * 12
 
3524
  for num in chord:
3525
  bits[num] = 1
3526
 
3527
- return bits
 
 
 
 
 
3528
 
3529
  def bits_to_tones_chord(bits):
3530
  return [i for i, bit in enumerate(bits) if bit == 1]
@@ -4581,86 +4597,34 @@ def ascii_text_words_counter(ascii_text):
4581
 
4582
  def check_and_fix_tones_chord(tones_chord):
4583
 
4584
- lst = tones_chord
4585
 
4586
- if len(lst) == 2:
4587
- if lst[1] - lst[0] == 1:
4588
- return [lst[-1]]
4589
- else:
4590
- if 0 in lst and 11 in lst:
4591
- lst.remove(0)
4592
- return lst
4593
-
4594
- non_consecutive = [lst[0]]
4595
-
4596
- if len(lst) > 2:
4597
- for i in range(1, len(lst) - 1):
4598
- if lst[i-1] + 1 != lst[i] and lst[i] + 1 != lst[i+1]:
4599
- non_consecutive.append(lst[i])
4600
- non_consecutive.append(lst[-1])
4601
-
4602
- if 0 in non_consecutive and 11 in non_consecutive:
4603
- non_consecutive.remove(0)
4604
 
4605
- return non_consecutive
4606
 
4607
  ###################################################################################
4608
 
4609
  def find_closest_tone(tones, tone):
4610
  return min(tones, key=lambda x:abs(x-tone))
4611
 
4612
- def advanced_check_and_fix_tones_chord(tones_chord, high_pitch=0):
4613
-
4614
- lst = tones_chord
4615
-
4616
- if 0 < high_pitch < 128:
4617
- ht = high_pitch % 12
4618
- else:
4619
- ht = 12
4620
 
4621
- cht = find_closest_tone(lst, ht)
4622
 
4623
- if len(lst) == 2:
4624
- if lst[1] - lst[0] == 1:
4625
- return [cht]
4626
- else:
4627
- if 0 in lst and 11 in lst:
4628
- if find_closest_tone([0, 11], cht) == 11:
4629
- lst.remove(0)
4630
- else:
4631
- lst.remove(11)
4632
- return lst
4633
-
4634
- non_consecutive = []
4635
-
4636
- if len(lst) > 2:
4637
- for i in range(0, len(lst) - 1):
4638
- if lst[i] + 1 != lst[i+1]:
4639
- non_consecutive.append(lst[i])
4640
- if lst[-1] - lst[-2] > 1:
4641
- non_consecutive.append(lst[-1])
4642
-
4643
- if cht not in non_consecutive:
4644
- non_consecutive.append(cht)
4645
- non_consecutive.sort()
4646
- if any(abs(non_consecutive[i+1] - non_consecutive[i]) == 1 for i in range(len(non_consecutive) - 1)):
4647
- final_list = [x for x in non_consecutive if x == cht or abs(x - cht) > 1]
4648
- else:
4649
- final_list = non_consecutive
4650
 
4651
- else:
4652
- final_list = non_consecutive
 
4653
 
4654
- if 0 in final_list and 11 in final_list:
4655
- if find_closest_tone([0, 11], cht) == 11:
4656
- final_list.remove(0)
4657
- else:
4658
- final_list.remove(11)
4659
 
4660
- if cht in final_list or ht in final_list:
4661
- return final_list
4662
- else:
4663
- return ['Error']
4664
 
4665
  ###################################################################################
4666
 
@@ -4687,23 +4651,66 @@ def create_similarity_matrix(list_of_values, matrix_length=0):
4687
 
4688
  ###################################################################################
4689
 
 
 
 
 
 
 
4690
  def augment_enhanced_score_notes(enhanced_score_notes,
4691
  timings_divider=16,
4692
  full_sorting=True,
4693
  timings_shift=0,
4694
- pitch_shift=0
 
 
 
4695
  ):
4696
 
4697
  esn = copy.deepcopy(enhanced_score_notes)
4698
 
4699
- for e in esn:
4700
- e[1] = int(e[1] / timings_divider) + timings_shift
4701
- e[2] = int(e[2] / timings_divider) + timings_shift
4702
- e[4] = e[4] + pitch_shift
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4703
 
4704
  if full_sorting:
4705
 
4706
- # Sorting by patch, pitch, then by start-time
4707
  esn.sort(key=lambda x: x[6])
4708
  esn.sort(key=lambda x: x[4], reverse=True)
4709
  esn.sort(key=lambda x: x[1])
@@ -4894,25 +4901,26 @@ def patch_list_from_enhanced_score_notes(enhanced_score_notes,
4894
  patches = [-1] * 16
4895
 
4896
  for idx, e in enumerate(enhanced_score_notes):
4897
- if e[3] != 9:
4898
- if patches[e[3]] == -1:
4899
- patches[e[3]] = e[6]
4900
- else:
4901
- if patches[e[3]] != e[6]:
4902
- if e[6] in patches:
4903
- e[3] = patches.index(e[6])
4904
- else:
4905
- if -1 in patches:
4906
- patches[patches.index(-1)] = e[6]
4907
  else:
4908
- patches[-1] = e[6]
 
 
 
4909
 
4910
- if verbose:
4911
- print('=' * 70)
4912
- print('WARNING! Composition has more than 15 patches!')
4913
- print('Conflict note number:', idx)
4914
- print('Conflict channel number:', e[3])
4915
- print('Conflict patch number:', e[6])
4916
 
4917
  patches = [p if p != -1 else default_patch for p in patches]
4918
 
@@ -4945,19 +4953,20 @@ def patch_enhanced_score_notes(enhanced_score_notes,
4945
  overflow_idx = -1
4946
 
4947
  for idx, e in enumerate(enhanced_score_notes):
4948
- if e[3] != 9:
4949
- if patches[e[3]] == -1:
4950
- patches[e[3]] = e[6]
4951
- else:
4952
- if patches[e[3]] != e[6]:
4953
- if e[6] in patches:
4954
- e[3] = patches.index(e[6])
4955
- else:
4956
- if -1 in patches:
4957
- patches[patches.index(-1)] = e[6]
4958
  else:
4959
- overflow_idx = idx
4960
- break
 
 
 
4961
 
4962
  enhanced_score_notes_with_patch_changes.append(e)
4963
 
@@ -4967,15 +4976,16 @@ def patch_enhanced_score_notes(enhanced_score_notes,
4967
 
4968
  if overflow_idx != -1:
4969
  for idx, e in enumerate(enhanced_score_notes[overflow_idx:]):
4970
- if e[3] != 9:
4971
- if e[6] not in patches:
4972
- if e[6] not in overflow_patches:
4973
- overflow_patches.append(e[6])
4974
- enhanced_score_notes_with_patch_changes.append(['patch_change', e[1], e[3], e[6]])
4975
- else:
4976
- e[3] = patches.index(e[6])
 
4977
 
4978
- enhanced_score_notes_with_patch_changes.append(e)
4979
 
4980
  #===========================================================================
4981
 
@@ -5142,8 +5152,10 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5142
  channels_index=3,
5143
  pitches_index=4,
5144
  patches_index=6,
5145
- use_filtered_chords=True,
 
5146
  remove_duplicate_pitches=True,
 
5147
  skip_drums=False
5148
  ):
5149
  fixed_chordified_score = []
@@ -5156,16 +5168,21 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5156
  else:
5157
  CHORDS = ALL_CHORDS_SORTED
5158
 
 
 
 
5159
  for c in chordified_score:
5160
 
 
 
5161
  if remove_duplicate_pitches:
5162
 
5163
- c.sort(key = lambda x: x[pitches_index], reverse=True)
5164
 
5165
  seen = set()
5166
  ddchord = []
5167
 
5168
- for cc in c:
5169
  if cc[channels_index] != 9:
5170
 
5171
  if tuple([cc[pitches_index], cc[patches_index]]) not in seen:
@@ -5177,9 +5194,9 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5177
  else:
5178
  ddchord.append(cc)
5179
 
5180
- c = copy.deepcopy(ddchord)
5181
 
5182
- tones_chord = sorted(set([t[pitches_index] % 12 for t in c if t[channels_index] != 9]))
5183
 
5184
  if tones_chord:
5185
 
@@ -5192,33 +5209,84 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5192
 
5193
  if tones_counts[0][1] > 1:
5194
  tones_chord = [tones_counts[0][0]]
 
5195
  elif tones_counts[1][1] > 1:
5196
  tones_chord = [tones_counts[1][0]]
 
5197
  else:
5198
  tones_chord = [pitches_chord[0] % 12]
5199
 
5200
  else:
5201
- tones_chord_combs = [list(comb) for i in range(len(tones_chord)-2, 0, -1) for comb in combinations(tones_chord, i+1)]
5202
 
5203
  for co in tones_chord_combs:
5204
  if co in CHORDS:
5205
  tones_chord = co
5206
  break
5207
 
 
 
 
5208
  bad_chords_counter += 1
5209
 
5210
- new_chord = []
5211
 
5212
- c.sort(key = lambda x: x[pitches_index], reverse=True)
 
5213
 
5214
- for e in c:
5215
  if e[channels_index] != 9:
5216
  if e[pitches_index] % 12 in tones_chord:
5217
- new_chord.append(e)
 
5218
 
5219
- else:
5220
- if not skip_drums:
5221
- new_chord.append(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5222
 
5223
  fixed_chordified_score.append(new_chord)
5224
 
@@ -5258,7 +5326,8 @@ def add_melody_to_enhanced_score_notes(enhanced_score_notes,
5258
  melody_patch=40,
5259
  melody_max_velocity=110,
5260
  acc_max_velocity=90,
5261
- pass_drums=True
 
5262
  ):
5263
 
5264
  if pass_drums:
@@ -5336,7 +5405,11 @@ def add_melody_to_enhanced_score_notes(enhanced_score_notes,
5336
 
5337
  adjust_score_velocities(smoothed_melody, melody_max_velocity)
5338
 
5339
- final_score = sorted(smoothed_melody + acc_score, key=lambda x: (x[1], -x[4]))
 
 
 
 
5340
 
5341
  return final_score
5342
 
@@ -5349,7 +5422,10 @@ def find_paths(list_of_lists, path=[]):
5349
 
5350
  ###################################################################################
5351
 
5352
- def recalculate_score_timings(score, start_time=0):
 
 
 
5353
 
5354
  rscore = copy.deepcopy(score)
5355
 
@@ -5359,10 +5435,10 @@ def recalculate_score_timings(score, start_time=0):
5359
 
5360
  for e in rscore:
5361
 
5362
- dtime = e[1] - pe[1]
5363
  pe = copy.deepcopy(e)
5364
  abs_time += dtime
5365
- e[1] = abs_time
5366
 
5367
  return rscore
5368
 
@@ -5406,11 +5482,11 @@ def harmonize_enhanced_melody_score_notes(enhanced_melody_score_notes):
5406
  cur_chord.append(m)
5407
  cc = sorted(set(cur_chord))
5408
 
5409
- if cc in ALL_CHORDS_FILTERED:
5410
  song.append(cc)
5411
 
5412
  else:
5413
- while sorted(set(cur_chord)) not in ALL_CHORDS_FILTERED:
5414
  cur_chord.pop(0)
5415
  cc = sorted(set(cur_chord))
5416
  song.append(cc)
@@ -5547,7 +5623,7 @@ def basic_enhanced_delta_score_notes_tokenizer(enhanced_delta_score_notes,
5547
  final_score_tokens_ints_seq = flatten(score_tokens_ints_seq)
5548
 
5549
  if max_seq_len > -1:
5550
- final_score_tokens_ints_seq = flat_score_tokens_ints_seq[:max_seq_len]
5551
 
5552
  if seq_pad_value > -1:
5553
  final_score_tokens_ints_seq += [seq_pad_value] * (max_seq_len - len(final_score_tokens_ints_seq))
@@ -5610,7 +5686,8 @@ def basic_enhanced_delta_score_notes_detokenizer(tokenized_seq,
5610
  def enhanced_chord_to_chord_token(enhanced_chord,
5611
  channels_index=3,
5612
  pitches_index=4,
5613
- use_filtered_chords=True
 
5614
  ):
5615
 
5616
  bad_chords_counter = 0
@@ -5621,6 +5698,9 @@ def enhanced_chord_to_chord_token(enhanced_chord,
5621
  else:
5622
  CHORDS = ALL_CHORDS_SORTED
5623
 
 
 
 
5624
  tones_chord = sorted(set([t[pitches_index] % 12 for t in enhanced_chord if t[channels_index] != 9]))
5625
 
5626
  original_tones_chord = copy.deepcopy(tones_chord)
@@ -6359,38 +6439,2427 @@ def transpose_pitches(pitches, transpose_value=0):
6359
 
6360
  ###################################################################################
6361
 
6362
- def reverse_enhanced_score_notes(enhanced_score_notes):
6363
 
6364
- score = recalculate_score_timings(enhanced_score_notes)
6365
 
6366
- cscore = chordify_score([1000, score])
 
 
 
 
 
6367
 
6368
- abs_dtimes = []
6369
 
6370
- for i, t in enumerate(cscore[:-1]):
6371
- abs_dtimes.append(cscore[i+1][0][1])
6372
- abs_dtimes.append(cscore[-1][0][1]+cscore[-1][0][2])
 
 
 
 
 
 
 
6373
 
6374
- new_dtimes = []
6375
- pt = abs_dtimes[-1]
6376
 
6377
- for t in abs_dtimes[::-1]:
6378
- new_dtimes.append(abs(pt-t))
6379
- pt = t
6380
 
6381
- new_mel = copy.deepcopy(cscore[::-1])
6382
 
6383
- time = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6384
 
6385
- for i, t in enumerate(new_mel):
6386
- time += new_dtimes[i]
6387
- for tt in t:
6388
- tt[1] = time
6389
 
6390
- return recalculate_score_timings(flatten(new_mel))
 
 
 
 
 
6391
 
6392
  ###################################################################################
6393
 
6394
- # This is the end of the TMIDI X Python module
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6396
  ###################################################################################
 
471
  itrack += 1
472
  return new_score
473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474
  def score2stats(opus_or_score=None):
475
  r'''Returns a dict of some basic stats about the score, like
476
  bank_select (list of tuples (msb,lsb)),
 
1471
  from difflib import SequenceMatcher as SM
1472
 
1473
  import statistics
1474
+ import math
1475
 
1476
  import matplotlib.pyplot as plt
1477
 
 
1750
  note_height = 0.75,
1751
  show_grid_lines=False,
1752
  return_plt = False,
1753
+ timings_multiplier=1,
1754
+ save_plt='',
1755
+ save_only_plt_image=True,
1756
+ save_transparent=False
1757
  ):
1758
 
1759
  '''Tegridy ms SONG plotter/vizualizer'''
 
1807
 
1808
  plt.title(plot_title)
1809
 
1810
+ if save_plt != '':
1811
+ if save_only_plt_image:
1812
+ plt.axis('off')
1813
+ plt.title('')
1814
+ plt.savefig(save_plt, transparent=save_transparent, bbox_inches='tight', pad_inches=0, facecolor='black')
1815
+ plt.close()
1816
+
1817
+ else:
1818
+ plt.savefig(save_plt)
1819
+ plt.close()
1820
+
1821
  if return_plt:
1822
  return fig
1823
 
1824
  plt.show()
1825
+ plt.close()
1826
 
1827
  ###################################################################################
1828
 
 
1935
 
1936
  '''Tegridy Pickle File Loader
1937
 
1938
+ Input: Full path and file name with or without extention
1939
  File extension if different from default .pickle
1940
 
1941
  Output: Standard Python 3 unpickled data object
 
1947
  print('Tegridy Pickle File Loader')
1948
  print('Loading the pickle file. Please wait...')
1949
 
1950
+ if os.path.basename(input_file_name).endswith(ext):
1951
+ fname = input_file_name
1952
+
1953
+ else:
1954
+ fname = input_file_name + ext
1955
+
1956
+ with open(fname, 'rb') as pickle_file:
1957
  content = pickle.load(pickle_file)
1958
 
1959
  if verbose:
 
2046
 
2047
  #print('Loading MIDI file...')
2048
  midi_file = open(MIDI_file, 'rb')
2049
+ if debug: print('Processing File:', MIDI_file)
2050
 
2051
  try:
2052
  opus = midi2opus(midi_file.read())
 
3528
 
3529
  # Binary chords functions
3530
 
3531
+ def tones_chord_to_bits(chord, reverse=True):
3532
+
3533
  bits = [0] * 12
3534
+
3535
  for num in chord:
3536
  bits[num] = 1
3537
 
3538
+ if reverse:
3539
+ bits.reverse()
3540
+ return bits
3541
+
3542
+ else:
3543
+ return bits
3544
 
3545
  def bits_to_tones_chord(bits):
3546
  return [i for i, bit in enumerate(bits) if bit == 1]
 
4597
 
4598
  def check_and_fix_tones_chord(tones_chord):
4599
 
4600
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord), 0, -1) for comb in combinations(tones_chord, i)]
4601
 
4602
+ for c in tones_chord_combs:
4603
+ if c in ALL_CHORDS_FULL:
4604
+ checked_tones_chord = c
4605
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4606
 
4607
+ return sorted(checked_tones_chord)
4608
 
4609
  ###################################################################################
4610
 
4611
  def find_closest_tone(tones, tone):
4612
  return min(tones, key=lambda x:abs(x-tone))
4613
 
4614
+ ###################################################################################
 
 
 
 
 
 
 
4615
 
4616
+ def advanced_check_and_fix_tones_chord(tones_chord, high_pitch=0):
4617
 
4618
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord), 0, -1) for comb in combinations(tones_chord, i)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4619
 
4620
+ for c in tones_chord_combs:
4621
+ if c in ALL_CHORDS_FULL:
4622
+ tchord = c
4623
 
4624
+ if 0 < high_pitch < 128 and len(tchord) == 1:
4625
+ tchord = [high_pitch % 12]
 
 
 
4626
 
4627
+ return tchord
 
 
 
4628
 
4629
  ###################################################################################
4630
 
 
4651
 
4652
  ###################################################################################
4653
 
4654
+ def ceil_with_precision(value, decimal_places):
4655
+ factor = 10 ** decimal_places
4656
+ return math.ceil(value * factor) / factor
4657
+
4658
+ ###################################################################################
4659
+
4660
  def augment_enhanced_score_notes(enhanced_score_notes,
4661
  timings_divider=16,
4662
  full_sorting=True,
4663
  timings_shift=0,
4664
+ pitch_shift=0,
4665
+ ceil_timings=False,
4666
+ round_timings=False,
4667
+ legacy_timings=False
4668
  ):
4669
 
4670
  esn = copy.deepcopy(enhanced_score_notes)
4671
 
4672
+ pe = enhanced_score_notes[0]
4673
+
4674
+ abs_time = max(0, int(enhanced_score_notes[0][1] / timings_divider))
4675
+
4676
+ for i, e in enumerate(esn):
4677
+
4678
+ dtime = (e[1] / timings_divider) - (pe[1] / timings_divider)
4679
+
4680
+ if round_timings:
4681
+ dtime = round(dtime)
4682
+
4683
+ else:
4684
+ if ceil_timings:
4685
+ dtime = math.ceil(dtime)
4686
+
4687
+ else:
4688
+ dtime = int(dtime)
4689
+
4690
+ if legacy_timings:
4691
+ abs_time = int(e[1] / timings_divider) + timings_shift
4692
+
4693
+ else:
4694
+ abs_time += dtime
4695
+
4696
+ e[1] = max(0, abs_time + timings_shift)
4697
+
4698
+ if round_timings:
4699
+ e[2] = max(1, round(e[2] / timings_divider)) + timings_shift
4700
+
4701
+ else:
4702
+ if ceil_timings:
4703
+ e[2] = max(1, math.ceil(e[2] / timings_divider)) + timings_shift
4704
+ else:
4705
+ e[2] = max(1, int(e[2] / timings_divider)) + timings_shift
4706
+
4707
+ e[4] = max(1, min(127, e[4] + pitch_shift))
4708
+
4709
+ pe = enhanced_score_notes[i]
4710
 
4711
  if full_sorting:
4712
 
4713
+ # Sorting by patch, reverse pitch and start-time
4714
  esn.sort(key=lambda x: x[6])
4715
  esn.sort(key=lambda x: x[4], reverse=True)
4716
  esn.sort(key=lambda x: x[1])
 
4901
  patches = [-1] * 16
4902
 
4903
  for idx, e in enumerate(enhanced_score_notes):
4904
+ if e[0] == 'note':
4905
+ if e[3] != 9:
4906
+ if patches[e[3]] == -1:
4907
+ patches[e[3]] = e[6]
4908
+ else:
4909
+ if patches[e[3]] != e[6]:
4910
+ if e[6] in patches:
4911
+ e[3] = patches.index(e[6])
 
 
4912
  else:
4913
+ if -1 in patches:
4914
+ patches[patches.index(-1)] = e[6]
4915
+ else:
4916
+ patches[-1] = e[6]
4917
 
4918
+ if verbose:
4919
+ print('=' * 70)
4920
+ print('WARNING! Composition has more than 15 patches!')
4921
+ print('Conflict note number:', idx)
4922
+ print('Conflict channel number:', e[3])
4923
+ print('Conflict patch number:', e[6])
4924
 
4925
  patches = [p if p != -1 else default_patch for p in patches]
4926
 
 
4953
  overflow_idx = -1
4954
 
4955
  for idx, e in enumerate(enhanced_score_notes):
4956
+ if e[0] == 'note':
4957
+ if e[3] != 9:
4958
+ if patches[e[3]] == -1:
4959
+ patches[e[3]] = e[6]
4960
+ else:
4961
+ if patches[e[3]] != e[6]:
4962
+ if e[6] in patches:
4963
+ e[3] = patches.index(e[6])
 
 
4964
  else:
4965
+ if -1 in patches:
4966
+ patches[patches.index(-1)] = e[6]
4967
+ else:
4968
+ overflow_idx = idx
4969
+ break
4970
 
4971
  enhanced_score_notes_with_patch_changes.append(e)
4972
 
 
4976
 
4977
  if overflow_idx != -1:
4978
  for idx, e in enumerate(enhanced_score_notes[overflow_idx:]):
4979
+ if e[0] == 'note':
4980
+ if e[3] != 9:
4981
+ if e[6] not in patches:
4982
+ if e[6] not in overflow_patches:
4983
+ overflow_patches.append(e[6])
4984
+ enhanced_score_notes_with_patch_changes.append(['patch_change', e[1], e[3], e[6]])
4985
+ else:
4986
+ e[3] = patches.index(e[6])
4987
 
4988
+ enhanced_score_notes_with_patch_changes.append(e)
4989
 
4990
  #===========================================================================
4991
 
 
5152
  channels_index=3,
5153
  pitches_index=4,
5154
  patches_index=6,
5155
+ use_filtered_chords=False,
5156
+ use_full_chords=True,
5157
  remove_duplicate_pitches=True,
5158
+ fix_bad_pitches=False,
5159
  skip_drums=False
5160
  ):
5161
  fixed_chordified_score = []
 
5168
  else:
5169
  CHORDS = ALL_CHORDS_SORTED
5170
 
5171
+ if use_full_chords:
5172
+ CHORDS = ALL_CHORDS_FULL
5173
+
5174
  for c in chordified_score:
5175
 
5176
+ chord = copy.deepcopy(c)
5177
+
5178
  if remove_duplicate_pitches:
5179
 
5180
+ chord.sort(key = lambda x: x[pitches_index], reverse=True)
5181
 
5182
  seen = set()
5183
  ddchord = []
5184
 
5185
+ for cc in chord:
5186
  if cc[channels_index] != 9:
5187
 
5188
  if tuple([cc[pitches_index], cc[patches_index]]) not in seen:
 
5194
  else:
5195
  ddchord.append(cc)
5196
 
5197
+ chord = copy.deepcopy(ddchord)
5198
 
5199
+ tones_chord = sorted(set([t[pitches_index] % 12 for t in chord if t[channels_index] != 9]))
5200
 
5201
  if tones_chord:
5202
 
 
5209
 
5210
  if tones_counts[0][1] > 1:
5211
  tones_chord = [tones_counts[0][0]]
5212
+
5213
  elif tones_counts[1][1] > 1:
5214
  tones_chord = [tones_counts[1][0]]
5215
+
5216
  else:
5217
  tones_chord = [pitches_chord[0] % 12]
5218
 
5219
  else:
5220
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord)-1, 0, -1) for comb in combinations(tones_chord, i)]
5221
 
5222
  for co in tones_chord_combs:
5223
  if co in CHORDS:
5224
  tones_chord = co
5225
  break
5226
 
5227
+ if len(tones_chord) == 1:
5228
+ tones_chord = [pitches_chord[0] % 12]
5229
+
5230
  bad_chords_counter += 1
5231
 
5232
+ chord.sort(key = lambda x: x[pitches_index], reverse=True)
5233
 
5234
+ new_chord = set()
5235
+ pipa = []
5236
 
5237
+ for e in chord:
5238
  if e[channels_index] != 9:
5239
  if e[pitches_index] % 12 in tones_chord:
5240
+ new_chord.add(tuple(e))
5241
+ pipa.append([e[pitches_index], e[patches_index]])
5242
 
5243
+ elif (e[pitches_index]+1) % 12 in tones_chord:
5244
+ e[pitches_index] += 1
5245
+ new_chord.add(tuple(e))
5246
+ pipa.append([e[pitches_index], e[patches_index]])
5247
+
5248
+ elif (e[pitches_index]-1) % 12 in tones_chord:
5249
+ e[pitches_index] -= 1
5250
+ new_chord.add(tuple(e))
5251
+ pipa.append([e[pitches_index], e[patches_index]])
5252
+
5253
+ if fix_bad_pitches:
5254
+
5255
+ bad_chord = set()
5256
+
5257
+ for e in chord:
5258
+ if e[channels_index] != 9:
5259
+
5260
+ if e[pitches_index] % 12 not in tones_chord:
5261
+ bad_chord.add(tuple(e))
5262
+
5263
+ elif (e[pitches_index]+1) % 12 not in tones_chord:
5264
+ bad_chord.add(tuple(e))
5265
+
5266
+ elif (e[pitches_index]-1) % 12 not in tones_chord:
5267
+ bad_chord.add(tuple(e))
5268
+
5269
+ for bc in bad_chord:
5270
+
5271
+ bc = list(bc)
5272
+
5273
+ tone = find_closest_tone(tones_chord, bc[pitches_index] % 12)
5274
+
5275
+ new_pitch = ((bc[pitches_index] // 12) * 12) + tone
5276
+
5277
+ if [new_pitch, bc[patches_index]] not in pipa:
5278
+ bc[pitches_index] = new_pitch
5279
+ new_chord.add(tuple(bc))
5280
+ pipa.append([[new_pitch], bc[patches_index]])
5281
+
5282
+ if not skip_drums:
5283
+ for e in c:
5284
+ if e[channels_index] == 9:
5285
+ new_chord.add(tuple(e))
5286
+
5287
+ new_chord = [list(e) for e in new_chord]
5288
+
5289
+ new_chord.sort(key = lambda x: (-x[pitches_index], x[patches_index]))
5290
 
5291
  fixed_chordified_score.append(new_chord)
5292
 
 
5326
  melody_patch=40,
5327
  melody_max_velocity=110,
5328
  acc_max_velocity=90,
5329
+ pass_drums=True,
5330
+ return_melody=False
5331
  ):
5332
 
5333
  if pass_drums:
 
5405
 
5406
  adjust_score_velocities(smoothed_melody, melody_max_velocity)
5407
 
5408
+ if return_melody:
5409
+ final_score = sorted(smoothed_melody, key=lambda x: (x[1], -x[4]))
5410
+
5411
+ else:
5412
+ final_score = sorted(smoothed_melody + acc_score, key=lambda x: (x[1], -x[4]))
5413
 
5414
  return final_score
5415
 
 
5422
 
5423
  ###################################################################################
5424
 
5425
+ def recalculate_score_timings(score,
5426
+ start_time=0,
5427
+ timings_index=1
5428
+ ):
5429
 
5430
  rscore = copy.deepcopy(score)
5431
 
 
5435
 
5436
  for e in rscore:
5437
 
5438
+ dtime = e[timings_index] - pe[timings_index]
5439
  pe = copy.deepcopy(e)
5440
  abs_time += dtime
5441
+ e[timings_index] = abs_time
5442
 
5443
  return rscore
5444
 
 
5482
  cur_chord.append(m)
5483
  cc = sorted(set(cur_chord))
5484
 
5485
+ if cc in ALL_CHORDS_FULL:
5486
  song.append(cc)
5487
 
5488
  else:
5489
+ while sorted(set(cur_chord)) not in ALL_CHORDS_FULL:
5490
  cur_chord.pop(0)
5491
  cc = sorted(set(cur_chord))
5492
  song.append(cc)
 
5623
  final_score_tokens_ints_seq = flatten(score_tokens_ints_seq)
5624
 
5625
  if max_seq_len > -1:
5626
+ final_score_tokens_ints_seq = final_score_tokens_ints_seq[:max_seq_len]
5627
 
5628
  if seq_pad_value > -1:
5629
  final_score_tokens_ints_seq += [seq_pad_value] * (max_seq_len - len(final_score_tokens_ints_seq))
 
5686
  def enhanced_chord_to_chord_token(enhanced_chord,
5687
  channels_index=3,
5688
  pitches_index=4,
5689
+ use_filtered_chords=False,
5690
+ use_full_chords=True
5691
  ):
5692
 
5693
  bad_chords_counter = 0
 
5698
  else:
5699
  CHORDS = ALL_CHORDS_SORTED
5700
 
5701
+ if use_full_chords:
5702
+ CHORDS = ALL_CHORDS_FULL
5703
+
5704
  tones_chord = sorted(set([t[pitches_index] % 12 for t in enhanced_chord if t[channels_index] != 9]))
5705
 
5706
  original_tones_chord = copy.deepcopy(tones_chord)
 
6439
 
6440
  ###################################################################################
6441
 
6442
+ def reverse_enhanced_score_notes(escore_notes):
6443
 
6444
+ score = recalculate_score_timings(escore_notes)
6445
 
6446
+ ematrix = escore_notes_to_escore_matrix(score, reverse_matrix=True)
6447
+ e_score = escore_matrix_to_original_escore_notes(ematrix)
6448
+
6449
+ reversed_score = recalculate_score_timings(e_score)
6450
+
6451
+ return reversed_score
6452
 
6453
+ ###################################################################################
6454
 
6455
+ def count_patterns(lst, sublist):
6456
+ count = 0
6457
+ idx = 0
6458
+ for i in range(len(lst) - len(sublist) + 1):
6459
+ if lst[idx:idx + len(sublist)] == sublist:
6460
+ count += 1
6461
+ idx += len(sublist)
6462
+ else:
6463
+ idx += 1
6464
+ return count
6465
 
6466
+ ###################################################################################
 
6467
 
6468
+ def find_lrno_patterns(seq):
 
 
6469
 
6470
+ all_seqs = Counter()
6471
 
6472
+ max_pat_len = math.ceil(len(seq) / 2)
6473
+
6474
+ num_iter = 0
6475
+
6476
+ for i in range(len(seq)):
6477
+ for j in range(i+1, len(seq)+1):
6478
+ if j-i <= max_pat_len:
6479
+ all_seqs[tuple(seq[i:j])] += 1
6480
+ num_iter += 1
6481
+
6482
+ max_count = 0
6483
+ max_len = 0
6484
+
6485
+ for val, count in all_seqs.items():
6486
+
6487
+ if max_len < len(val):
6488
+ max_count = max(2, count)
6489
+
6490
+ if count > 1:
6491
+ max_len = max(max_len, len(val))
6492
+ pval = val
6493
+
6494
+ max_pats = []
6495
+
6496
+ for val, count in all_seqs.items():
6497
+ if count == max_count and len(val) == max_len:
6498
+ max_pats.append(val)
6499
 
6500
+ found_patterns = []
 
 
 
6501
 
6502
+ for pat in max_pats:
6503
+ count = count_patterns(seq, list(pat))
6504
+ if count > 1:
6505
+ found_patterns.append([count, len(pat), pat])
6506
+
6507
+ return found_patterns
6508
 
6509
  ###################################################################################
6510
 
6511
+ def delta_pitches(escore_notes, pitches_index=4):
6512
+
6513
+ pitches = [p[pitches_index] for p in escore_notes]
6514
+
6515
+ return [a-b for a, b in zip(pitches[:-1], pitches[1:])]
6516
+
6517
+ ###################################################################################
6518
+
6519
+ def split_list(lst, val):
6520
+ return [lst[i:j] for i, j in zip([0] + [k + 1 for k, x in enumerate(lst) if x == val], [k for k, x in enumerate(lst) if x == val] + [len(lst)]) if j > i]
6521
+
6522
+ ###################################################################################
6523
+
6524
+ def even_timings(escore_notes,
6525
+ times_idx=1,
6526
+ durs_idx=2
6527
+ ):
6528
+
6529
+ esn = copy.deepcopy(escore_notes)
6530
+
6531
+ for e in esn:
6532
+
6533
+ if e[times_idx] != 0:
6534
+ if e[times_idx] % 2 != 0:
6535
+ e[times_idx] += 1
6536
+
6537
+ if e[durs_idx] % 2 != 0:
6538
+ e[durs_idx] += 1
6539
+
6540
+ return esn
6541
+
6542
+ ###################################################################################
6543
+
6544
+ def delta_score_to_abs_score(delta_score_notes,
6545
+ times_idx=1
6546
+ ):
6547
+
6548
+ abs_score = copy.deepcopy(delta_score_notes)
6549
+
6550
+ abs_time = 0
6551
+
6552
+ for i, e in enumerate(delta_score_notes):
6553
+
6554
+ dtime = e[times_idx]
6555
+
6556
+ abs_time += dtime
6557
+
6558
+ abs_score[i][times_idx] = abs_time
6559
+
6560
+ return abs_score
6561
+
6562
+ ###################################################################################
6563
+
6564
+
6565
+ def adjust_numbers_to_sum(numbers, target_sum):
6566
+
6567
+ current_sum = sum(numbers)
6568
+ difference = target_sum - current_sum
6569
+
6570
+ non_zero_elements = [(i, num) for i, num in enumerate(numbers) if num != 0]
6571
+
6572
+ total_non_zero = sum(num for _, num in non_zero_elements)
6573
+
6574
+ increments = []
6575
+ for i, num in non_zero_elements:
6576
+ proportion = num / total_non_zero
6577
+ increment = proportion * difference
6578
+ increments.append(increment)
6579
+
6580
+ for idx, (i, num) in enumerate(non_zero_elements):
6581
+ numbers[i] += int(round(increments[idx]))
6582
+
6583
+ current_sum = sum(numbers)
6584
+ difference = target_sum - current_sum
6585
+ non_zero_indices = [i for i, num in enumerate(numbers) if num != 0]
6586
+
6587
+ for i in range(abs(difference)):
6588
+ numbers[non_zero_indices[i % len(non_zero_indices)]] += 1 if difference > 0 else -1
6589
+
6590
+ return numbers
6591
+
6592
+ ###################################################################################
6593
+
6594
+ def find_next_bar(escore_notes, bar_time, start_note_idx, cur_bar):
6595
+ for e in escore_notes[start_note_idx:]:
6596
+ if e[1] // bar_time > cur_bar:
6597
+ return e, escore_notes.index(e)
6598
+
6599
+ ###################################################################################
6600
+
6601
+ def align_escore_notes_to_bars(escore_notes,
6602
+ bar_time=4000,
6603
+ trim_durations=False,
6604
+ split_durations=False
6605
+ ):
6606
+
6607
+ #=============================================================================
6608
+
6609
+ aligned_escore_notes = copy.deepcopy(escore_notes)
6610
+
6611
+ abs_time = 0
6612
+ nidx = 0
6613
+ delta = 0
6614
+ bcount = 0
6615
+ next_bar = [0]
6616
+
6617
+ #=============================================================================
6618
+
6619
+ while next_bar:
6620
+
6621
+ next_bar = find_next_bar(escore_notes, bar_time, nidx, bcount)
6622
+
6623
+ if next_bar:
6624
+
6625
+ gescore_notes = escore_notes[nidx:next_bar[1]]
6626
+ else:
6627
+ gescore_notes = escore_notes[nidx:]
6628
+
6629
+ original_timings = [delta] + [(b[1]-a[1]) for a, b in zip(gescore_notes[:-1], gescore_notes[1:])]
6630
+ adj_timings = adjust_numbers_to_sum(original_timings, bar_time)
6631
+
6632
+ for t in adj_timings:
6633
+
6634
+ abs_time += t
6635
+
6636
+ aligned_escore_notes[nidx][1] = abs_time
6637
+ aligned_escore_notes[nidx][2] -= int(bar_time // 200)
6638
+
6639
+ nidx += 1
6640
+
6641
+ if next_bar:
6642
+ delta = escore_notes[next_bar[1]][1]-escore_notes[next_bar[1]-1][1]
6643
+ bcount += 1
6644
+
6645
+ #=============================================================================
6646
+
6647
+ aligned_adjusted_escore_notes = []
6648
+ bcount = 0
6649
+
6650
+ for a in aligned_escore_notes:
6651
+ bcount = a[1] // bar_time
6652
+ nbtime = bar_time * (bcount+1)
6653
+
6654
+ if a[1]+a[2] > nbtime and a[3] != 9:
6655
+ if trim_durations or split_durations:
6656
+ ddiff = ((a[1]+a[2])-nbtime)
6657
+ aa = copy.deepcopy(a)
6658
+ aa[2] = a[2] - ddiff
6659
+ aligned_adjusted_escore_notes.append(aa)
6660
+
6661
+ if split_durations:
6662
+ aaa = copy.deepcopy(a)
6663
+ aaa[1] = a[1]+aa[2]
6664
+ aaa[2] = ddiff
6665
+
6666
+ aligned_adjusted_escore_notes.append(aaa)
6667
+
6668
+ else:
6669
+ aligned_adjusted_escore_notes.append(a)
6670
+
6671
+ else:
6672
+ aligned_adjusted_escore_notes.append(a)
6673
+
6674
+ #=============================================================================
6675
+
6676
+ return aligned_adjusted_escore_notes
6677
+
6678
+ ###################################################################################
6679
+
6680
+ def normalize_chord_durations(chord,
6681
+ dur_idx=2,
6682
+ norm_factor=100
6683
+ ):
6684
+
6685
+ nchord = copy.deepcopy(chord)
6686
+
6687
+ for c in nchord:
6688
+ c[dur_idx] = int(round(max(1 / norm_factor, c[dur_idx] // norm_factor) * norm_factor))
6689
+
6690
+ return nchord
6691
+
6692
+ ###################################################################################
6693
+
6694
+ def normalize_chordified_score_durations(chordified_score,
6695
+ dur_idx=2,
6696
+ norm_factor=100
6697
+ ):
6698
+
6699
+ ncscore = copy.deepcopy(chordified_score)
6700
+
6701
+ for cc in ncscore:
6702
+ for c in cc:
6703
+ c[dur_idx] = int(round(max(1 / norm_factor, c[dur_idx] // norm_factor) * norm_factor))
6704
+
6705
+ return ncscore
6706
+
6707
+ ###################################################################################
6708
+
6709
+ def horizontal_ordered_list_search(list_of_lists,
6710
+ query_list,
6711
+ start_idx=0,
6712
+ end_idx=-1
6713
+ ):
6714
+
6715
+ lol = list_of_lists
6716
+
6717
+ results = []
6718
+
6719
+ if start_idx > 0:
6720
+ lol = list_of_lists[start_idx:]
6721
+
6722
+ if start_idx == -1:
6723
+ idx = -1
6724
+ for i, l in enumerate(list_of_lists):
6725
+ try:
6726
+ idx = l.index(query_list[0])
6727
+ lol = list_of_lists[i:]
6728
+ break
6729
+ except:
6730
+ continue
6731
+
6732
+ if idx == -1:
6733
+ results.append(-1)
6734
+ return results
6735
+ else:
6736
+ results.append(i)
6737
+
6738
+ if end_idx != -1:
6739
+ lol = list_of_lists[start_idx:start_idx+max(end_idx, len(query_list))]
6740
+
6741
+ for i, q in enumerate(query_list):
6742
+ try:
6743
+ idx = lol[i].index(q)
6744
+ results.append(idx)
6745
+ except:
6746
+ results.append(-1)
6747
+ return results
6748
+
6749
+ return results
6750
+
6751
+ ###################################################################################
6752
+
6753
+ def escore_notes_to_escore_matrix(escore_notes,
6754
+ alt_velocities=False,
6755
+ flip_matrix=False,
6756
+ reverse_matrix=False
6757
+ ):
6758
+
6759
+ last_time = escore_notes[-1][1]
6760
+ last_notes = [e for e in escore_notes if e[1] == last_time]
6761
+ max_last_dur = max([e[2] for e in last_notes])
6762
+
6763
+ time_range = last_time+max_last_dur
6764
+
6765
+ channels_list = sorted(set([e[3] for e in escore_notes]))
6766
+
6767
+ escore_matrixes = []
6768
+
6769
+ for cha in channels_list:
6770
+
6771
+ escore_matrix = [[[-1, -1]] * 128 for _ in range(time_range)]
6772
+
6773
+ pe = escore_notes[0]
6774
+
6775
+ for i, note in enumerate(escore_notes):
6776
+
6777
+ etype, time, duration, channel, pitch, velocity, patch = note
6778
+
6779
+ time = max(0, time)
6780
+ duration = max(1, duration)
6781
+ channel = max(0, min(15, channel))
6782
+ pitch = max(0, min(127, pitch))
6783
+ velocity = max(0, min(127, velocity))
6784
+ patch = max(0, min(128, patch))
6785
+
6786
+ if alt_velocities:
6787
+ velocity -= (i % 2)
6788
+
6789
+ if channel == cha:
6790
+
6791
+ for t in range(time, min(time + duration, time_range)):
6792
+
6793
+ escore_matrix[t][pitch] = [velocity, patch]
6794
+
6795
+ pe = note
6796
+
6797
+ if flip_matrix:
6798
+
6799
+ temp_matrix = []
6800
+
6801
+ for m in escore_matrix:
6802
+ temp_matrix.append(m[::-1])
6803
+
6804
+ escore_matrix = temp_matrix
6805
 
6806
+ if reverse_matrix:
6807
+ escore_matrix = escore_matrix[::-1]
6808
+
6809
+ escore_matrixes.append(escore_matrix)
6810
+
6811
+ return [channels_list, escore_matrixes]
6812
+
6813
+ ###################################################################################
6814
+
6815
+ def escore_matrix_to_merged_escore_notes(full_escore_matrix,
6816
+ max_note_duration=4000
6817
+ ):
6818
+
6819
+ merged_escore_notes = []
6820
+
6821
+ mat_channels_list = full_escore_matrix[0]
6822
+
6823
+ for m, cha in enumerate(mat_channels_list):
6824
+
6825
+ escore_matrix = full_escore_matrix[1][m]
6826
+
6827
+ result = []
6828
+
6829
+ for j in range(len(escore_matrix[0])):
6830
+
6831
+ count = 1
6832
+
6833
+ for i in range(1, len(escore_matrix)):
6834
+
6835
+ if escore_matrix[i][j] != [-1, -1] and escore_matrix[i][j][1] == escore_matrix[i-1][j][1] and count < max_note_duration:
6836
+ count += 1
6837
+
6838
+ else:
6839
+ if count > 1:
6840
+ result.append([i-count, count, j, escore_matrix[i-1][j]])
6841
+
6842
+ count = 1
6843
+
6844
+ if count > 1:
6845
+ result.append([len(escore_matrix)-count, count, j, escore_matrix[-1][j]])
6846
+
6847
+ result.sort(key=lambda x: (x[0], -x[2]))
6848
+
6849
+ for r in result:
6850
+ merged_escore_notes.append(['note', r[0], r[1], cha, r[2], r[3][0], r[3][1]])
6851
+
6852
+ return sorted(merged_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
6853
+
6854
+ ###################################################################################
6855
+
6856
+ def escore_matrix_to_original_escore_notes(full_escore_matrix):
6857
+
6858
+ merged_escore_notes = []
6859
+
6860
+ mat_channels_list = full_escore_matrix[0]
6861
+
6862
+ for m, cha in enumerate(mat_channels_list):
6863
+
6864
+ escore_matrix = full_escore_matrix[1][m]
6865
+
6866
+ result = []
6867
+
6868
+ for j in range(len(escore_matrix[0])):
6869
+
6870
+ count = 1
6871
+
6872
+ for i in range(1, len(escore_matrix)):
6873
+
6874
+ if escore_matrix[i][j] != [-1, -1] and escore_matrix[i][j] == escore_matrix[i-1][j]:
6875
+ count += 1
6876
+
6877
+ else:
6878
+ if count > 1:
6879
+ result.append([i-count, count, j, escore_matrix[i-1][j]])
6880
+
6881
+ count = 1
6882
+
6883
+ if count > 1:
6884
+ result.append([len(escore_matrix)-count, count, j, escore_matrix[-1][j]])
6885
+
6886
+ result.sort(key=lambda x: (x[0], -x[2]))
6887
+
6888
+ for r in result:
6889
+ merged_escore_notes.append(['note', r[0], r[1], cha, r[2], r[3][0], r[3][1]])
6890
+
6891
+ return sorted(merged_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
6892
+
6893
+ ###################################################################################
6894
+
6895
+ def escore_notes_to_binary_matrix(escore_notes,
6896
+ channel=0,
6897
+ patch=0,
6898
+ flip_matrix=False,
6899
+ reverse_matrix=False
6900
+ ):
6901
+
6902
+ escore = [e for e in escore_notes if e[3] == channel and e[6] == patch]
6903
+
6904
+ if escore:
6905
+ last_time = escore[-1][1]
6906
+ last_notes = [e for e in escore if e[1] == last_time]
6907
+ max_last_dur = max([e[2] for e in last_notes])
6908
+
6909
+ time_range = last_time+max_last_dur
6910
+
6911
+ escore_matrix = []
6912
+
6913
+ escore_matrix = [[0] * 128 for _ in range(time_range)]
6914
+
6915
+ for note in escore:
6916
+
6917
+ etype, time, duration, chan, pitch, velocity, pat = note
6918
+
6919
+ time = max(0, time)
6920
+ duration = max(1, duration)
6921
+ chan = max(0, min(15, chan))
6922
+ pitch = max(0, min(127, pitch))
6923
+ velocity = max(0, min(127, velocity))
6924
+ pat = max(0, min(128, pat))
6925
+
6926
+ if channel == chan and patch == pat:
6927
+
6928
+ for t in range(time, min(time + duration, time_range)):
6929
+
6930
+ escore_matrix[t][pitch] = 1
6931
+
6932
+ if flip_matrix:
6933
+
6934
+ temp_matrix = []
6935
+
6936
+ for m in escore_matrix:
6937
+ temp_matrix.append(m[::-1])
6938
+
6939
+ escore_matrix = temp_matrix
6940
+
6941
+ if reverse_matrix:
6942
+ escore_matrix = escore_matrix[::-1]
6943
+
6944
+ return escore_matrix
6945
+
6946
+ else:
6947
+ return None
6948
+
6949
+ ###################################################################################
6950
+
6951
+ def binary_matrix_to_original_escore_notes(binary_matrix,
6952
+ channel=0,
6953
+ patch=0,
6954
+ velocity=-1
6955
+ ):
6956
+
6957
+ result = []
6958
+
6959
+ for j in range(len(binary_matrix[0])):
6960
+
6961
+ count = 1
6962
+
6963
+ for i in range(1, len(binary_matrix)):
6964
+
6965
+ if binary_matrix[i][j] != 0 and binary_matrix[i][j] == binary_matrix[i-1][j]:
6966
+ count += 1
6967
+
6968
+ else:
6969
+ if count > 1:
6970
+ result.append([i-count, count, j, binary_matrix[i-1][j]])
6971
+
6972
+ else:
6973
+ if binary_matrix[i-1][j] != 0:
6974
+ result.append([i-count, count, j, binary_matrix[i-1][j]])
6975
+
6976
+ count = 1
6977
+
6978
+ if count > 1:
6979
+ result.append([len(binary_matrix)-count, count, j, binary_matrix[-1][j]])
6980
+
6981
+ else:
6982
+ if binary_matrix[i-1][j] != 0:
6983
+ result.append([i-count, count, j, binary_matrix[i-1][j]])
6984
+
6985
+ result.sort(key=lambda x: (x[0], -x[2]))
6986
+
6987
+ original_escore_notes = []
6988
+
6989
+ vel = velocity
6990
+
6991
+ for r in result:
6992
+
6993
+ if velocity == -1:
6994
+ vel = max(40, r[2])
6995
+
6996
+ original_escore_notes.append(['note', r[0], r[1], channel, r[2], vel, patch])
6997
+
6998
+ return sorted(original_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
6999
+
7000
+ ###################################################################################
7001
+
7002
+ def escore_notes_averages(escore_notes,
7003
+ times_index=1,
7004
+ durs_index=2,
7005
+ chans_index=3,
7006
+ ptcs_index=4,
7007
+ vels_index=5,
7008
+ average_drums=False,
7009
+ score_is_delta=False,
7010
+ return_ptcs_and_vels=False
7011
+ ):
7012
+
7013
+ if score_is_delta:
7014
+ if average_drums:
7015
+ times = [e[times_index] for e in escore_notes if e[times_index] != 0]
7016
+ else:
7017
+ times = [e[times_index] for e in escore_notes if e[times_index] != 0 and e[chans_index] != 9]
7018
+
7019
+ else:
7020
+ descore_notes = delta_score_notes(escore_notes)
7021
+ if average_drums:
7022
+ times = [e[times_index] for e in descore_notes if e[times_index] != 0]
7023
+ else:
7024
+ times = [e[times_index] for e in descore_notes if e[times_index] != 0 and e[chans_index] != 9]
7025
+
7026
+ if average_drums:
7027
+ durs = [e[durs_index] for e in escore_notes]
7028
+ else:
7029
+ durs = [e[durs_index] for e in escore_notes if e[chans_index] != 9]
7030
+
7031
+ if return_ptcs_and_vels:
7032
+ if average_drums:
7033
+ ptcs = [e[ptcs_index] for e in escore_notes]
7034
+ vels = [e[vels_index] for e in escore_notes]
7035
+ else:
7036
+ ptcs = [e[ptcs_index] for e in escore_notes if e[chans_index] != 9]
7037
+ vels = [e[vels_index] for e in escore_notes if e[chans_index] != 9]
7038
+
7039
+ return [sum(times) / len(times), sum(durs) / len(durs), sum(ptcs) / len(ptcs), sum(vels) / len(vels)]
7040
+
7041
+ else:
7042
+ return [sum(times) / len(times), sum(durs) / len(durs)]
7043
+
7044
+ ###################################################################################
7045
+
7046
+ def adjust_escore_notes_timings(escore_notes,
7047
+ adj_k=1,
7048
+ times_index=1,
7049
+ durs_index=2,
7050
+ score_is_delta=False,
7051
+ return_delta_scpre=False
7052
+ ):
7053
+
7054
+ if score_is_delta:
7055
+ adj_escore_notes = copy.deepcopy(escore_notes)
7056
+ else:
7057
+ adj_escore_notes = delta_score_notes(escore_notes)
7058
+
7059
+ for e in adj_escore_notes:
7060
+
7061
+ if e[times_index] != 0:
7062
+ e[times_index] = max(1, round(e[times_index] * adj_k))
7063
+
7064
+ e[durs_index] = max(1, round(e[durs_index] * adj_k))
7065
+
7066
+ if return_delta_scpre:
7067
+ return adj_escore_notes
7068
+
7069
+ else:
7070
+ return delta_score_to_abs_score(adj_escore_notes)
7071
+
7072
+ ###################################################################################
7073
+
7074
+ def escore_notes_delta_times(escore_notes,
7075
+ times_index=1
7076
+ ):
7077
+
7078
+ descore_notes = delta_score_notes(escore_notes)
7079
+
7080
+ return [e[times_index] for e in descore_notes]
7081
+
7082
+ ###################################################################################
7083
+
7084
+ def escore_notes_durations(escore_notes,
7085
+ durs_index=1
7086
+ ):
7087
+
7088
+ descore_notes = delta_score_notes(escore_notes)
7089
+
7090
+ return [e[durs_index] for e in descore_notes]
7091
+
7092
+ ###################################################################################
7093
+
7094
+ def ordered_lists_match_ratio(src_list, trg_list):
7095
+
7096
+ zlist = list(zip(src_list, trg_list))
7097
+
7098
+ return sum([a == b for a, b in zlist]) / len(list(zlist))
7099
+
7100
+ ###################################################################################
7101
+
7102
+ def lists_intersections(src_list, trg_list):
7103
+ return list(set(src_list) & set(trg_list))
7104
+
7105
+ ###################################################################################
7106
+
7107
+ def transpose_escore_notes(escore_notes,
7108
+ transpose_value=0,
7109
+ channel_index=3,
7110
+ pitches_index=4
7111
+ ):
7112
+
7113
+ tr_escore_notes = copy.deepcopy(escore_notes)
7114
+
7115
+ for e in tr_escore_notes:
7116
+ if e[channel_index] != 9:
7117
+ e[pitches_index] = max(1, min(127, e[pitches_index] + transpose_value))
7118
+
7119
+ return tr_escore_notes
7120
+
7121
+ ###################################################################################
7122
+
7123
+ def transpose_escore_notes_to_pitch(escore_notes,
7124
+ target_pitch_value=60,
7125
+ channel_index=3,
7126
+ pitches_index=4
7127
+ ):
7128
+
7129
+ tr_escore_notes = copy.deepcopy(escore_notes)
7130
+
7131
+ transpose_delta = int(round(target_pitch_value)) - int(round(escore_notes_averages(escore_notes, return_ptcs_and_vels=True)[2]))
7132
+
7133
+ for e in tr_escore_notes:
7134
+ if e[channel_index] != 9:
7135
+ e[pitches_index] = max(1, min(127, e[pitches_index] + transpose_delta))
7136
+
7137
+ return tr_escore_notes
7138
+
7139
+ ###################################################################################
7140
+
7141
+ CHORDS_TYPES = ['WHITE', 'BLACK', 'UNKNOWN', 'MIXED WHITE', 'MIXED BLACK', 'MIXED GRAY']
7142
+
7143
+ ###################################################################################
7144
+
7145
+ def tones_chord_type(tones_chord,
7146
+ return_chord_type_index=True,
7147
+ use_filtered_chords=False,
7148
+ use_full_chords=True
7149
+ ):
7150
+
7151
+ WN = WHITE_NOTES
7152
+ BN = BLACK_NOTES
7153
+ MX = WHITE_NOTES + BLACK_NOTES
7154
+
7155
+ if use_filtered_chords:
7156
+ CHORDS = ALL_CHORDS_FILTERED
7157
+
7158
+ else:
7159
+ CHORDS = ALL_CHORDS_SORTED
7160
+
7161
+ if use_full_chords:
7162
+ CHORDS = ALL_CHORDS_FULL
7163
+
7164
+ tones_chord = sorted(tones_chord)
7165
+
7166
+ ctype = 'UNKNOWN'
7167
+
7168
+ if tones_chord in CHORDS:
7169
+
7170
+ if sorted(set(tones_chord) & set(WN)) == tones_chord:
7171
+ ctype = 'WHITE'
7172
+
7173
+ elif sorted(set(tones_chord) & set(BN)) == tones_chord:
7174
+ ctype = 'BLACK'
7175
+
7176
+ if len(tones_chord) > 1 and sorted(set(tones_chord) & set(MX)) == tones_chord:
7177
+
7178
+ if len(sorted(set(tones_chord) & set(WN))) == len(sorted(set(tones_chord) & set(BN))):
7179
+ ctype = 'MIXED GRAY'
7180
+
7181
+ elif len(sorted(set(tones_chord) & set(WN))) > len(sorted(set(tones_chord) & set(BN))):
7182
+ ctype = 'MIXED WHITE'
7183
+
7184
+ elif len(sorted(set(tones_chord) & set(WN))) < len(sorted(set(tones_chord) & set(BN))):
7185
+ ctype = 'MIXED BLACK'
7186
+
7187
+ if return_chord_type_index:
7188
+ return CHORDS_TYPES.index(ctype)
7189
+
7190
+ else:
7191
+ return ctype
7192
+
7193
+ ###################################################################################
7194
+
7195
+ def tone_type(tone,
7196
+ return_tone_type_index=True
7197
+ ):
7198
+
7199
+ tone = tone % 12
7200
+
7201
+ if tone in BLACK_NOTES:
7202
+ if return_tone_type_index:
7203
+ return CHORDS_TYPES.index('BLACK')
7204
+ else:
7205
+ return "BLACK"
7206
+
7207
+ else:
7208
+ if return_tone_type_index:
7209
+ return CHORDS_TYPES.index('WHITE')
7210
+ else:
7211
+ return "WHITE"
7212
+
7213
+ ###################################################################################
7214
+
7215
+ def lists_sym_differences(src_list, trg_list):
7216
+ return list(set(src_list) ^ set(trg_list))
7217
+
7218
+ ###################################################################################
7219
+
7220
+ def lists_differences(long_list, short_list):
7221
+ return list(set(long_list) - set(short_list))
7222
+
7223
+ ###################################################################################
7224
+
7225
+ def find_best_tones_chord(src_tones_chords,
7226
+ trg_tones_chords,
7227
+ find_longest=True
7228
+ ):
7229
+
7230
+ not_seen_trg_chords = []
7231
+
7232
+ max_len = 0
7233
+
7234
+ for tc in trg_tones_chords:
7235
+ if sorted(tc) in src_tones_chords:
7236
+ not_seen_trg_chords.append(sorted(tc))
7237
+ max_len = max(max_len, len(tc))
7238
+
7239
+ if not not_seen_trg_chords:
7240
+ max_len = len(max(trg_tones_chords, key=len))
7241
+ not_seen_trg_chords = trg_tones_chords
7242
+
7243
+ if find_longest:
7244
+ return random.choice([c for c in not_seen_trg_chords if len(c) == max_len])
7245
+
7246
+ else:
7247
+ return random.choice(not_seen_trg_chords)
7248
+
7249
+ ###################################################################################
7250
+
7251
+ def find_matching_tones_chords(tones_chord,
7252
+ matching_chord_length=-1,
7253
+ match_chord_type=True,
7254
+ use_filtered_chords=True,
7255
+ use_full_chords=True
7256
+ ):
7257
+
7258
+ if use_filtered_chords:
7259
+ CHORDS = ALL_CHORDS_FILTERED
7260
+ else:
7261
+ CHORDS = ALL_CHORDS_SORTED
7262
+
7263
+ if use_full_chords:
7264
+ CHORDS = ALL_CHORDS_FULL
7265
+
7266
+ tones_chord = sorted(tones_chord)
7267
+
7268
+ tclen = len(tones_chord)
7269
+
7270
+ tctype = tones_chord_type(tones_chord, use_filtered_chords=use_filtered_chords)
7271
+
7272
+ matches = []
7273
+
7274
+ for tc in CHORDS:
7275
+
7276
+ if matching_chord_length == -1:
7277
+ if len(tc) > tclen:
7278
+ if sorted(lists_intersections(tc, tones_chord)) == tones_chord:
7279
+ if match_chord_type:
7280
+ if tones_chord_type(tc, use_filtered_chords=use_filtered_chords) == tctype:
7281
+ tcdiffs = lists_differences(tc, tones_chord)
7282
+ if all(tone_type(d) == tctype % 3 for d in tcdiffs):
7283
+ matches.append(tc)
7284
+ else:
7285
+ matches.append(tc)
7286
+
7287
+ else:
7288
+
7289
+ if len(tc) == max(tclen, matching_chord_length):
7290
+ if sorted(lists_intersections(tc, tones_chord)) == tones_chord:
7291
+ if match_chord_type:
7292
+ if tones_chord_type(tc, use_filtered_chords=use_filtered_chords) == tctype:
7293
+ tcdiffs = lists_differences(tc, tones_chord)
7294
+ if all(tone_type(d) == tctype % 3 for d in tcdiffs):
7295
+ matches.append(tc)
7296
+ else:
7297
+ matches.append(tc)
7298
+
7299
+ return sorted(matches, key=len)
7300
+
7301
+ ###################################################################################
7302
+
7303
+ def adjust_list_of_values_to_target_average(list_of_values,
7304
+ trg_avg,
7305
+ min_value,
7306
+ max_value
7307
+ ):
7308
+
7309
+ filtered_values = [value for value in list_of_values if min_value <= value <= max_value]
7310
+
7311
+ if not filtered_values:
7312
+ return list_of_values
7313
+
7314
+ current_avg = sum(filtered_values) / len(filtered_values)
7315
+ scale_factor = trg_avg / current_avg
7316
+
7317
+ adjusted_values = [value * scale_factor for value in filtered_values]
7318
+
7319
+ total_difference = trg_avg * len(filtered_values) - sum(adjusted_values)
7320
+ adjustment_per_value = total_difference / len(filtered_values)
7321
+
7322
+ final_values = [value + adjustment_per_value for value in adjusted_values]
7323
+
7324
+ while abs(sum(final_values) / len(final_values) - trg_avg) > 1e-6:
7325
+ total_difference = trg_avg * len(final_values) - sum(final_values)
7326
+ adjustment_per_value = total_difference / len(final_values)
7327
+ final_values = [value + adjustment_per_value for value in final_values]
7328
+
7329
+ final_values = [round(value) for value in final_values]
7330
+
7331
+ adjusted_values = copy.deepcopy(list_of_values)
7332
+
7333
+ j = 0
7334
+
7335
+ for i in range(len(adjusted_values)):
7336
+ if min_value <= adjusted_values[i] <= max_value:
7337
+ adjusted_values[i] = final_values[j]
7338
+ j += 1
7339
+
7340
+ return adjusted_values
7341
+
7342
+ ###################################################################################
7343
+
7344
+ def adjust_escore_notes_to_average(escore_notes,
7345
+ trg_avg,
7346
+ min_value=1,
7347
+ max_value=4000,
7348
+ times_index=1,
7349
+ durs_index=2,
7350
+ score_is_delta=False,
7351
+ return_delta_scpre=False
7352
+ ):
7353
+ if score_is_delta:
7354
+ delta_escore_notes = copy.deepcopy(escore_notes)
7355
+
7356
+ else:
7357
+ delta_escore_notes = delta_score_notes(escore_notes)
7358
+
7359
+ times = [[e[times_index], e[durs_index]] for e in delta_escore_notes]
7360
+
7361
+ filtered_values = [value for value in times if min_value <= value[0] <= max_value]
7362
+
7363
+ if not filtered_values:
7364
+ return escore_notes
7365
+
7366
+ current_avg = sum([v[0] for v in filtered_values]) / len([v[0] for v in filtered_values])
7367
+ scale_factor = trg_avg / current_avg
7368
+
7369
+ adjusted_values = [[value[0] * scale_factor, value[1] * scale_factor] for value in filtered_values]
7370
+
7371
+ total_difference = trg_avg * len([v[0] for v in filtered_values]) - sum([v[0] for v in adjusted_values])
7372
+ adjustment_per_value = total_difference / len(filtered_values)
7373
+
7374
+ final_values = [[value[0] + adjustment_per_value, value[1] + adjustment_per_value] for value in adjusted_values]
7375
+
7376
+ while abs(sum([v[0] for v in final_values]) / len(final_values) - trg_avg) > 1e-6:
7377
+ total_difference = trg_avg * len(final_values) - sum([v[0] for v in final_values])
7378
+ adjustment_per_value = total_difference / len(final_values)
7379
+ final_values = [[value[0] + adjustment_per_value, value[1] + adjustment_per_value] for value in final_values]
7380
+
7381
+ final_values = [[round(value[0]), round(value[1])] for value in final_values]
7382
+
7383
+ adjusted_delta_score = copy.deepcopy(delta_escore_notes)
7384
+
7385
+ j = 0
7386
+
7387
+ for i in range(len(adjusted_delta_score)):
7388
+ if min_value <= adjusted_delta_score[i][1] <= max_value:
7389
+ adjusted_delta_score[i][times_index] = final_values[j][0]
7390
+ adjusted_delta_score[i][durs_index] = final_values[j][1]
7391
+ j += 1
7392
+
7393
+ adjusted_escore_notes = delta_score_to_abs_score(adjusted_delta_score)
7394
+
7395
+ if return_delta_scpre:
7396
+ return adjusted_delta_score
7397
+
7398
+ else:
7399
+ return adjusted_escore_notes
7400
+
7401
+ ###################################################################################
7402
+
7403
+ def harmonize_enhanced_melody_score_notes_to_ms_SONG(escore_notes,
7404
+ melody_velocity=-1,
7405
+ melody_channel=3,
7406
+ melody_patch=40,
7407
+ melody_base_octave=4,
7408
+ harmonized_tones_chords_velocity=-1,
7409
+ harmonized_tones_chords_channel=0,
7410
+ harmonized_tones_chords_patch=0
7411
+ ):
7412
+
7413
+ harmonized_tones_chords = harmonize_enhanced_melody_score_notes(escore_notes)
7414
+
7415
+ harm_escore_notes = []
7416
+
7417
+ time = 0
7418
+
7419
+ for i, note in enumerate(escore_notes):
7420
+
7421
+ time = note[1]
7422
+ dur = note[2]
7423
+ ptc = note[4]
7424
+
7425
+ if melody_velocity == -1:
7426
+ vel = int(110 + ((ptc % 12) * 1.5))
7427
+ else:
7428
+ vel = melody_velocity
7429
+
7430
+ harm_escore_notes.append(['note', time, dur, melody_channel, ptc, vel, melody_patch])
7431
+
7432
+ for t in harmonized_tones_chords[i]:
7433
+
7434
+ ptc = (melody_base_octave * 12) + t
7435
+
7436
+ if harmonized_tones_chords_velocity == -1:
7437
+ vel = int(80 + ((ptc % 12) * 1.5))
7438
+ else:
7439
+ vel = harmonized_tones_chords_velocity
7440
+
7441
+ harm_escore_notes.append(['note', time, dur, harmonized_tones_chords_channel, ptc, vel, harmonized_tones_chords_patch])
7442
+
7443
+ return sorted(harm_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
7444
+
7445
+ ###################################################################################
7446
+
7447
+ def check_and_fix_pitches_chord(pitches_chord,
7448
+ remove_duplicate_pitches=True,
7449
+ use_filtered_chords=False,
7450
+ use_full_chords=True,
7451
+ fix_bad_pitches=False,
7452
+ ):
7453
+
7454
+ if remove_duplicate_pitches:
7455
+ pitches_chord = sorted(set(pitches_chord), reverse=True)
7456
+ else:
7457
+ pitches_chord = sorted(pitches_chord, reverse=True)
7458
+
7459
+ if use_filtered_chords:
7460
+ CHORDS = ALL_CHORDS_FILTERED
7461
+ else:
7462
+ CHORDS = ALL_CHORDS_SORTED
7463
+
7464
+ if use_full_chords:
7465
+ CHORDS = ALL_CHORDS_FULL
7466
+
7467
+ chord = copy.deepcopy(pitches_chord)
7468
+
7469
+ tones_chord = sorted(set([t % 12 for t in chord]))
7470
+
7471
+ if tones_chord:
7472
+
7473
+ if tones_chord not in CHORDS:
7474
+
7475
+ if len(tones_chord) == 2:
7476
+ tones_counts = Counter([p % 12 for p in pitches_chord]).most_common()
7477
+
7478
+ if tones_counts[0][1] > 1:
7479
+ tones_chord = [tones_counts[0][0]]
7480
+
7481
+ elif tones_counts[1][1] > 1:
7482
+ tones_chord = [tones_counts[1][0]]
7483
+
7484
+ else:
7485
+ tones_chord = [pitches_chord[0] % 12]
7486
+
7487
+ else:
7488
+ tones_chord_combs = [list(comb) for i in range(len(tones_chord)-1, 0, -1) for comb in combinations(tones_chord, i)]
7489
+
7490
+ for co in tones_chord_combs:
7491
+ if co in CHORDS:
7492
+ tones_chord = co
7493
+ break
7494
+
7495
+ if len(tones_chord) == 1:
7496
+ tones_chord = [pitches_chord[0] % 12]
7497
+
7498
+ chord.sort(reverse=True)
7499
+
7500
+ new_chord = set()
7501
+ pipa = []
7502
+
7503
+ for e in chord:
7504
+ if e % 12 in tones_chord:
7505
+ new_chord.add(tuple([e]))
7506
+ pipa.append(e)
7507
+
7508
+ elif (e+1) % 12 in tones_chord:
7509
+ e += 1
7510
+ new_chord.add(tuple([e]))
7511
+ pipa.append(e)
7512
+
7513
+ elif (e-1) % 12 in tones_chord:
7514
+ e -= 1
7515
+ new_chord.add(tuple([e]))
7516
+ pipa.append(e)
7517
+
7518
+ if fix_bad_pitches:
7519
+
7520
+ bad_chord = set()
7521
+
7522
+ for e in chord:
7523
+
7524
+ if e % 12 not in tones_chord:
7525
+ bad_chord.add(tuple([e]))
7526
+
7527
+ elif (e+1) % 12 not in tones_chord:
7528
+ bad_chord.add(tuple([e]))
7529
+
7530
+ elif (e-1) % 12 not in tones_chord:
7531
+ bad_chord.add(tuple([e]))
7532
+
7533
+ for bc in bad_chord:
7534
+
7535
+ bc = list(bc)
7536
+
7537
+ tone = find_closest_tone(tones_chord, bc[0] % 12)
7538
+
7539
+ new_pitch = ((bc[0] // 12) * 12) + tone
7540
+
7541
+ if new_pitch not in pipa:
7542
+ new_chord.add(tuple([new_pitch]))
7543
+ pipa.append(new_pitch)
7544
+
7545
+ new_pitches_chord = [e[0] for e in new_chord]
7546
+
7547
+ return sorted(new_pitches_chord, reverse=True)
7548
+
7549
+ ###################################################################################
7550
+
7551
+ ALL_CHORDS_TRANS = [[0], [0, 4], [0, 4, 7], [0, 4, 8], [0, 5], [0, 6], [0, 7], [0, 8], [1], [1, 5],
7552
+ [1, 5, 9], [1, 6], [1, 7], [1, 8], [1, 9], [2], [2, 6], [2, 6, 10], [2, 7],
7553
+ [2, 8], [2, 9], [2, 10], [3], [3, 7], [3, 7, 11], [3, 8], [3, 9], [3, 10],
7554
+ [3, 11], [4], [4, 7], [4, 7, 11], [4, 8], [4, 9], [4, 10], [4, 11], [5],
7555
+ [5, 9], [5, 10], [5, 11], [6], [6, 10], [6, 11], [7], [7, 11], [8], [9], [10],
7556
+ [11]]
7557
+
7558
+ ###################################################################################
7559
+
7560
+ def minkowski_distance(x, y, p=3, pad_value=float('inf')):
7561
+
7562
+ if len(x) != len(y):
7563
+ return -1
7564
+
7565
+ distance = 0
7566
+
7567
+ for i in range(len(x)):
7568
+
7569
+ if x[i] == pad_value or y[i] == pad_value:
7570
+ continue
7571
+
7572
+ distance += abs(x[i] - y[i]) ** p
7573
+
7574
+ return distance ** (1 / p)
7575
+
7576
+ ###################################################################################
7577
+
7578
+ def dot_product(x, y, pad_value=None):
7579
+ return sum(xi * yi for xi, yi in zip(x, y) if xi != pad_value and yi != pad_value)
7580
+
7581
+ def norm(vector, pad_value=None):
7582
+ return sum(xi ** 2 for xi in vector if xi != pad_value) ** 0.5
7583
+
7584
+ def cosine_similarity(x, y, pad_value=None):
7585
+ if len(x) != len(y):
7586
+ return -1
7587
+
7588
+ dot_prod = dot_product(x, y, pad_value)
7589
+ norm_x = norm(x, pad_value)
7590
+ norm_y = norm(y, pad_value)
7591
+
7592
+ if norm_x == 0 or norm_y == 0:
7593
+ return 0.0
7594
+
7595
+ return dot_prod / (norm_x * norm_y)
7596
+
7597
+ ###################################################################################
7598
+
7599
+ def hamming_distance(arr1, arr2, pad_value):
7600
+ return sum(el1 != el2 for el1, el2 in zip(arr1, arr2) if el1 != pad_value and el2 != pad_value)
7601
+
7602
+ ###################################################################################
7603
+
7604
+ def jaccard_similarity(arr1, arr2, pad_value):
7605
+ intersection = sum(el1 and el2 for el1, el2 in zip(arr1, arr2) if el1 != pad_value and el2 != pad_value)
7606
+ union = sum((el1 or el2) for el1, el2 in zip(arr1, arr2) if el1 != pad_value or el2 != pad_value)
7607
+ return intersection / union if union != 0 else 0
7608
+
7609
+ ###################################################################################
7610
+
7611
+ def pearson_correlation(arr1, arr2, pad_value):
7612
+ filtered_pairs = [(el1, el2) for el1, el2 in zip(arr1, arr2) if el1 != pad_value and el2 != pad_value]
7613
+ if not filtered_pairs:
7614
+ return 0
7615
+ n = len(filtered_pairs)
7616
+ sum1 = sum(el1 for el1, el2 in filtered_pairs)
7617
+ sum2 = sum(el2 for el1, el2 in filtered_pairs)
7618
+ sum1_sq = sum(el1 ** 2 for el1, el2 in filtered_pairs)
7619
+ sum2_sq = sum(el2 ** 2 for el1, el2 in filtered_pairs)
7620
+ p_sum = sum(el1 * el2 for el1, el2 in filtered_pairs)
7621
+ num = p_sum - (sum1 * sum2 / n)
7622
+ den = ((sum1_sq - sum1 ** 2 / n) * (sum2_sq - sum2 ** 2 / n)) ** 0.5
7623
+ if den == 0:
7624
+ return 0
7625
+ return num / den
7626
+
7627
+ ###################################################################################
7628
+
7629
+ def calculate_combined_distances(array_of_arrays,
7630
+ combine_hamming_distance=True,
7631
+ combine_jaccard_similarity=True,
7632
+ combine_pearson_correlation=True,
7633
+ pad_value=None
7634
+ ):
7635
+
7636
+ binary_arrays = array_of_arrays
7637
+ binary_array_len = len(binary_arrays)
7638
+
7639
+ hamming_distances = [[0] * binary_array_len for _ in range(binary_array_len)]
7640
+ jaccard_similarities = [[0] * binary_array_len for _ in range(binary_array_len)]
7641
+ pearson_correlations = [[0] * binary_array_len for _ in range(binary_array_len)]
7642
+
7643
+ for i in range(binary_array_len):
7644
+ for j in range(i + 1, binary_array_len):
7645
+ hamming_distances[i][j] = hamming_distance(binary_arrays[i], binary_arrays[j], pad_value)
7646
+ hamming_distances[j][i] = hamming_distances[i][j]
7647
+
7648
+ jaccard_similarities[i][j] = jaccard_similarity(binary_arrays[i], binary_arrays[j], pad_value)
7649
+ jaccard_similarities[j][i] = jaccard_similarities[i][j]
7650
+
7651
+ pearson_correlations[i][j] = pearson_correlation(binary_arrays[i], binary_arrays[j], pad_value)
7652
+ pearson_correlations[j][i] = pearson_correlations[i][j]
7653
+
7654
+ max_hamming = max(max(row) for row in hamming_distances)
7655
+ min_hamming = min(min(row) for row in hamming_distances)
7656
+ normalized_hamming = [[(val - min_hamming) / (max_hamming - min_hamming) for val in row] for row in hamming_distances]
7657
+
7658
+ max_jaccard = max(max(row) for row in jaccard_similarities)
7659
+ min_jaccard = min(min(row) for row in jaccard_similarities)
7660
+ normalized_jaccard = [[(val - min_jaccard) / (max_jaccard - min_jaccard) for val in row] for row in jaccard_similarities]
7661
+
7662
+ max_pearson = max(max(row) for row in pearson_correlations)
7663
+ min_pearson = min(min(row) for row in pearson_correlations)
7664
+ normalized_pearson = [[(val - min_pearson) / (max_pearson - min_pearson) for val in row] for row in pearson_correlations]
7665
+
7666
+ selected_metrics = 0
7667
+
7668
+ if combine_hamming_distance:
7669
+ selected_metrics += normalized_hamming[i][j]
7670
+
7671
+ if combine_jaccard_similarity:
7672
+ selected_metrics += (1 - normalized_jaccard[i][j])
7673
+
7674
+ if combine_pearson_correlation:
7675
+ selected_metrics += (1 - normalized_pearson[i][j])
7676
+
7677
+ combined_metric = [[selected_metrics for i in range(binary_array_len)] for j in range(binary_array_len)]
7678
+
7679
+ return combined_metric
7680
+
7681
+ ###################################################################################
7682
+
7683
+ def tones_chords_to_bits(tones_chords):
7684
+
7685
+ bits_tones_chords = []
7686
+
7687
+ for c in tones_chords:
7688
+
7689
+ c.sort()
7690
+
7691
+ bits = tones_chord_to_bits(c)
7692
+
7693
+ bits_tones_chords.append(bits)
7694
+
7695
+ return bits_tones_chords
7696
+
7697
+ ###################################################################################
7698
+
7699
+ def tones_chords_to_ints(tones_chords):
7700
+
7701
+ ints_tones_chords = []
7702
+
7703
+ for c in tones_chords:
7704
+
7705
+ c.sort()
7706
+
7707
+ bits = tones_chord_to_bits(c)
7708
+
7709
+ number = bits_to_int(bits)
7710
+
7711
+ ints_tones_chords.append(number)
7712
+
7713
+ return ints_tones_chords
7714
+
7715
+ ###################################################################################
7716
+
7717
+ def tones_chords_to_types(tones_chords,
7718
+ return_chord_type_index=False
7719
+ ):
7720
+
7721
+ types_tones_chords = []
7722
+
7723
+ for c in tones_chords:
7724
+
7725
+ c.sort()
7726
+
7727
+ ctype = tones_chord_type(c, return_chord_type_index=return_chord_type_index)
7728
+
7729
+ types_tones_chords.append(ctype)
7730
+
7731
+ return types_tones_chords
7732
+
7733
+ ###################################################################################
7734
+
7735
+ def morph_tones_chord(tones_chord,
7736
+ trg_tone,
7737
+ use_filtered_chords=True,
7738
+ use_full_chords=True
7739
+ ):
7740
+
7741
+ src_tones_chord = sorted(sorted(set(tones_chord)) + [trg_tone])
7742
+
7743
+ combs = [list(comb) for i in range(len(src_tones_chord), 0, -1) for comb in combinations(src_tones_chord, i) if trg_tone in list(comb)]
7744
+
7745
+ matches = []
7746
+
7747
+ if use_filtered_chords:
7748
+ CHORDS = ALL_CHORDS_FILTERED
7749
+
7750
+ else:
7751
+ CHORDS = ALL_CHORDS_SORTED
7752
+
7753
+ if use_full_chords:
7754
+ CHORDS = ALL_CHORDS_FULL
7755
+
7756
+ for c in combs:
7757
+ if sorted(set(c)) in CHORDS:
7758
+ matches.append(sorted(set(c)))
7759
+
7760
+ max_len = len(max(matches, key=len))
7761
+
7762
+ return random.choice([m for m in matches if len(m) == max_len])
7763
+
7764
+ ###################################################################################
7765
+
7766
+ def compress_binary_matrix(binary_matrix,
7767
+ only_compress_zeros=False,
7768
+ return_compression_ratio=False
7769
+ ):
7770
+
7771
+ compressed_bmatrix = []
7772
+
7773
+ zm = [0] * len(binary_matrix[0])
7774
+ pm = [0] * len(binary_matrix[0])
7775
+
7776
+ mcount = 0
7777
+
7778
+ for m in binary_matrix:
7779
+
7780
+ if only_compress_zeros:
7781
+ if m != zm:
7782
+ compressed_bmatrix.append(m)
7783
+ mcount += 1
7784
+
7785
+ else:
7786
+ if m != pm:
7787
+ compressed_bmatrix.append(m)
7788
+ mcount += 1
7789
+
7790
+ pm = m
7791
+
7792
+ if return_compression_ratio:
7793
+ return [compressed_bmatrix, mcount / len(binary_matrix)]
7794
+
7795
+ else:
7796
+ return compressed_bmatrix
7797
+
7798
+ ###################################################################################
7799
+
7800
+ def solo_piano_escore_notes(escore_notes,
7801
+ channels_index=3,
7802
+ pitches_index=4,
7803
+ patches_index=6,
7804
+ keep_drums=False,
7805
+ ):
7806
+
7807
+ cscore = chordify_score([1000, escore_notes])
7808
+
7809
+ sp_escore_notes = []
7810
+
7811
+ for c in cscore:
7812
+
7813
+ seen = []
7814
+ chord = []
7815
+
7816
+ for cc in c:
7817
+ if cc[pitches_index] not in seen:
7818
+
7819
+ if cc[channels_index] != 9:
7820
+ cc[channels_index] = 0
7821
+ cc[patches_index] = 0
7822
+
7823
+ chord.append(cc)
7824
+ seen.append(cc[pitches_index])
7825
+
7826
+ else:
7827
+ if keep_drums:
7828
+ chord.append(cc)
7829
+ seen.append(cc[pitches_index])
7830
+
7831
+ sp_escore_notes.append(chord)
7832
+
7833
+ return flatten(sp_escore_notes)
7834
+
7835
+ ###################################################################################
7836
+
7837
+ def strip_drums_from_escore_notes(escore_notes,
7838
+ channels_index=3
7839
+ ):
7840
+
7841
+ return [e for e in escore_notes if e[channels_index] != 9]
7842
+
7843
+ ###################################################################################
7844
+
7845
+ def fixed_escore_notes_timings(escore_notes,
7846
+ fixed_durations=False,
7847
+ fixed_timings_multiplier=1,
7848
+ custom_fixed_time=-1,
7849
+ custom_fixed_dur=-1
7850
+ ):
7851
+
7852
+ fixed_timings_escore_notes = delta_score_notes(escore_notes, even_timings=True)
7853
+
7854
+ mode_time = round(Counter([e[1] for e in fixed_timings_escore_notes if e[1] != 0]).most_common()[0][0] * fixed_timings_multiplier)
7855
+
7856
+ if mode_time % 2 != 0:
7857
+ mode_time += 1
7858
+
7859
+ mode_dur = round(Counter([e[2] for e in fixed_timings_escore_notes if e[2] != 0]).most_common()[0][0] * fixed_timings_multiplier)
7860
+
7861
+ if mode_dur % 2 != 0:
7862
+ mode_dur += 1
7863
+
7864
+ for e in fixed_timings_escore_notes:
7865
+ if e[1] != 0:
7866
+
7867
+ if custom_fixed_time > 0:
7868
+ e[1] = custom_fixed_time
7869
+
7870
+ else:
7871
+ e[1] = mode_time
7872
+
7873
+ if fixed_durations:
7874
+
7875
+ if custom_fixed_dur > 0:
7876
+ e[2] = custom_fixed_dur
7877
+
7878
+ else:
7879
+ e[2] = mode_dur
7880
+
7881
+ return delta_score_to_abs_score(fixed_timings_escore_notes)
7882
+
7883
+ ###################################################################################
7884
+
7885
+ def cubic_kernel(x):
7886
+ abs_x = abs(x)
7887
+ if abs_x <= 1:
7888
+ return 1.5 * abs_x**3 - 2.5 * abs_x**2 + 1
7889
+ elif abs_x <= 2:
7890
+ return -0.5 * abs_x**3 + 2.5 * abs_x**2 - 4 * abs_x + 2
7891
+ else:
7892
+ return 0
7893
+
7894
+ ###################################################################################
7895
+
7896
+ def resize_matrix(matrix, new_height, new_width):
7897
+ old_height = len(matrix)
7898
+ old_width = len(matrix[0])
7899
+ resized_matrix = [[0] * new_width for _ in range(new_height)]
7900
+
7901
+ for i in range(new_height):
7902
+ for j in range(new_width):
7903
+ old_i = i * old_height / new_height
7904
+ old_j = j * old_width / new_width
7905
+
7906
+ value = 0
7907
+ total_weight = 0
7908
+ for m in range(-1, 3):
7909
+ for n in range(-1, 3):
7910
+ i_m = min(max(int(old_i) + m, 0), old_height - 1)
7911
+ j_n = min(max(int(old_j) + n, 0), old_width - 1)
7912
+
7913
+ if matrix[i_m][j_n] == 0:
7914
+ continue
7915
+
7916
+ weight = cubic_kernel(old_i - i_m) * cubic_kernel(old_j - j_n)
7917
+ value += matrix[i_m][j_n] * weight
7918
+ total_weight += weight
7919
+
7920
+ if total_weight > 0:
7921
+ value /= total_weight
7922
+
7923
+ resized_matrix[i][j] = int(value > 0.5)
7924
+
7925
+ return resized_matrix
7926
+
7927
+ ###################################################################################
7928
+
7929
+ def square_binary_matrix(binary_matrix,
7930
+ matrix_size=128,
7931
+ use_fast_squaring=False,
7932
+ return_plot_points=False
7933
+ ):
7934
+
7935
+ if use_fast_squaring:
7936
+
7937
+ step = round(len(binary_matrix) / matrix_size)
7938
+
7939
+ samples = []
7940
+
7941
+ for i in range(0, len(binary_matrix), step):
7942
+ samples.append(tuple([tuple(d) for d in binary_matrix[i:i+step]]))
7943
+
7944
+ resized_matrix = []
7945
+
7946
+ zmatrix = [[0] * matrix_size]
7947
+
7948
+ for s in samples:
7949
+
7950
+ samples_counts = Counter(s).most_common()
7951
+
7952
+ best_sample = tuple([0] * matrix_size)
7953
+ pm = tuple(zmatrix[0])
7954
+
7955
+ for sc in samples_counts:
7956
+ if sc[0] != tuple(zmatrix[0]) and sc[0] != pm:
7957
+ best_sample = sc[0]
7958
+ pm = sc[0]
7959
+ break
7960
+
7961
+ pm = sc[0]
7962
+
7963
+ resized_matrix.append(list(best_sample))
7964
+
7965
+ resized_matrix = resized_matrix[:matrix_size]
7966
+ resized_matrix += zmatrix * (matrix_size - len(resized_matrix))
7967
+
7968
+ else:
7969
+ resized_matrix = resize_matrix(binary_matrix, matrix_size, matrix_size)
7970
+
7971
+ points = [(i, j) for i in range(matrix_size) for j in range(matrix_size) if resized_matrix[i][j] == 1]
7972
+
7973
+ if return_plot_points:
7974
+ return [resized_matrix, points]
7975
+
7976
+ else:
7977
+ return resized_matrix
7978
+
7979
+ ###################################################################################
7980
+
7981
+ def mean(matrix):
7982
+ return sum(sum(row) for row in matrix) / (len(matrix) * len(matrix[0]))
7983
+
7984
+ ###################################################################################
7985
+
7986
+ def variance(matrix, mean_value):
7987
+ return sum(sum((element - mean_value) ** 2 for element in row) for row in matrix) / (len(matrix) * len(matrix[0]))
7988
+
7989
+ ###################################################################################
7990
+
7991
+ def covariance(matrix1, matrix2, mean1, mean2):
7992
+ return sum(sum((matrix1[i][j] - mean1) * (matrix2[i][j] - mean2) for j in range(len(matrix1[0]))) for i in range(len(matrix1))) / (len(matrix1) * len(matrix1[0]))
7993
+
7994
+ ###################################################################################
7995
+
7996
+ def ssim_index(matrix1, matrix2, bit_depth=1):
7997
+
7998
+ if len(matrix1) != len(matrix2) and len(matrix1[0]) != len(matrix2[0]):
7999
+ return -1
8000
+
8001
+ K1, K2 = 0.01, 0.03
8002
+ L = bit_depth
8003
+ C1 = (K1 * L) ** 2
8004
+ C2 = (K2 * L) ** 2
8005
+
8006
+ mu1 = mean(matrix1)
8007
+ mu2 = mean(matrix2)
8008
+
8009
+ sigma1_sq = variance(matrix1, mu1)
8010
+ sigma2_sq = variance(matrix2, mu2)
8011
+
8012
+ sigma12 = covariance(matrix1, matrix2, mu1, mu2)
8013
+
8014
+ ssim = ((2 * mu1 * mu2 + C1) * (2 * sigma12 + C2)) / ((mu1 ** 2 + mu2 ** 2 + C1) * (sigma1_sq + sigma2_sq + C2))
8015
+
8016
+ return ssim
8017
+
8018
+ ###################################################################################
8019
+
8020
+ def find_most_similar_matrix(array_of_matrices,
8021
+ trg_matrix,
8022
+ matrices_bit_depth=1,
8023
+ return_most_similar_index=False
8024
+ ):
8025
+
8026
+ max_ssim = -float('inf')
8027
+ most_similar_index = -1
8028
+
8029
+ for i, matrix in enumerate(array_of_matrices):
8030
+
8031
+ ssim = ssim_index(matrix, trg_matrix, bit_depth=matrices_bit_depth)
8032
+
8033
+ if ssim > max_ssim:
8034
+ max_ssim = ssim
8035
+ most_similar_index = i
8036
+
8037
+ if return_most_similar_index:
8038
+ return most_similar_index
8039
+
8040
+ else:
8041
+ return array_of_matrices[most_similar_index]
8042
+
8043
+ ###################################################################################
8044
+
8045
+ def chord_to_pchord(chord):
8046
+
8047
+ pchord = []
8048
+
8049
+ for cc in chord:
8050
+ if cc[3] != 9:
8051
+ pchord.append(cc[4])
8052
+
8053
+ return pchord
8054
+
8055
+ ###################################################################################
8056
+
8057
+ def summarize_escore_notes(escore_notes,
8058
+ summary_length_in_chords=128,
8059
+ preserve_timings=True,
8060
+ preserve_durations=False,
8061
+ time_threshold=12,
8062
+ min_sum_chord_len=2,
8063
+ use_tones_chords=True
8064
+ ):
8065
+
8066
+ cscore = chordify_score([d[1:] for d in delta_score_notes(escore_notes)])
8067
+
8068
+ summary_length_in_chords = min(len(cscore), summary_length_in_chords)
8069
+
8070
+ ltthresh = time_threshold // 2
8071
+ uttresh = time_threshold * 2
8072
+
8073
+ mc_time = Counter([c[0][0] for c in cscore if c[0][2] != 9 and ltthresh < c[0][0] < uttresh]).most_common()[0][0]
8074
+
8075
+ pchords = []
8076
+
8077
+ for c in cscore:
8078
+ if use_tones_chords:
8079
+ pchords.append([c[0][0]] + pitches_to_tones_chord(chord_to_pchord(c)))
8080
+
8081
+ else:
8082
+ pchords.append([c[0][0]] + chord_to_pchord(c))
8083
+
8084
+ step = round(len(pchords) / summary_length_in_chords)
8085
+
8086
+ samples = []
8087
+
8088
+ for i in range(0, len(pchords), step):
8089
+ samples.append(tuple([tuple(d) for d in pchords[i:i+step]]))
8090
+
8091
+ summarized_escore_notes = []
8092
+
8093
+ for i, s in enumerate(samples):
8094
+
8095
+ best_chord = list([v[0] for v in Counter(s).most_common() if v[0][0] == mc_time and len(v[0]) > min_sum_chord_len])
8096
+
8097
+ if not best_chord:
8098
+ best_chord = list([v[0] for v in Counter(s).most_common() if len(v[0]) > min_sum_chord_len])
8099
+
8100
+ if not best_chord:
8101
+ best_chord = list([Counter(s).most_common()[0][0]])
8102
+
8103
+ chord = copy.deepcopy(cscore[[ss for ss in s].index(best_chord[0])+(i*step)])
8104
+
8105
+ if preserve_timings:
8106
+
8107
+ if not preserve_durations:
8108
+
8109
+ if i > 0:
8110
+
8111
+ pchord = summarized_escore_notes[-1]
8112
+
8113
+ for pc in pchord:
8114
+ pc[1] = min(pc[1], chord[0][0])
8115
+
8116
+ else:
8117
+
8118
+ chord[0][0] = 1
8119
+
8120
+ for c in chord:
8121
+ c[1] = 1
8122
+
8123
+ summarized_escore_notes.append(chord)
8124
+
8125
+ summarized_escore_notes = summarized_escore_notes[:summary_length_in_chords]
8126
+
8127
+ return [['note'] + d for d in delta_score_to_abs_score(flatten(summarized_escore_notes), times_idx=0)]
8128
+
8129
+ ###################################################################################
8130
+
8131
+ def compress_patches_in_escore_notes(escore_notes,
8132
+ num_patches=4,
8133
+ group_patches=False
8134
+ ):
8135
+
8136
+ if num_patches > 4:
8137
+ n_patches = 4
8138
+ elif num_patches < 1:
8139
+ n_patches = 1
8140
+ else:
8141
+ n_patches = num_patches
8142
+
8143
+ if group_patches:
8144
+ patches_set = sorted(set([e[6] for e in escore_notes]))
8145
+ trg_patch_list = []
8146
+ seen = []
8147
+ for p in patches_set:
8148
+ if p // 8 not in seen:
8149
+ trg_patch_list.append(p)
8150
+ seen.append(p // 8)
8151
+
8152
+ trg_patch_list = sorted(trg_patch_list)
8153
+
8154
+ else:
8155
+ trg_patch_list = sorted(set([e[6] for e in escore_notes]))
8156
+
8157
+ if 128 in trg_patch_list and n_patches > 1:
8158
+ trg_patch_list = trg_patch_list[:n_patches-1] + [128]
8159
+ else:
8160
+ trg_patch_list = trg_patch_list[:n_patches]
8161
+
8162
+ new_escore_notes = []
8163
+
8164
+ for e in escore_notes:
8165
+ if e[6] in trg_patch_list:
8166
+ new_escore_notes.append(e)
8167
+
8168
+ return new_escore_notes
8169
+
8170
+ ###################################################################################
8171
+
8172
+ def compress_patches_in_escore_notes_chords(escore_notes,
8173
+ max_num_patches_per_chord=4,
8174
+ group_patches=True,
8175
+ root_grouped_patches=False
8176
+ ):
8177
+
8178
+ if max_num_patches_per_chord > 4:
8179
+ n_patches = 4
8180
+ elif max_num_patches_per_chord < 1:
8181
+ n_patches = 1
8182
+ else:
8183
+ n_patches = max_num_patches_per_chord
8184
+
8185
+ cscore = chordify_score([1000, sorted(escore_notes, key=lambda x: (x[1], x[6]))])
8186
+
8187
+ new_escore_notes = []
8188
+
8189
+ for c in cscore:
8190
+
8191
+ if group_patches:
8192
+ patches_set = sorted(set([e[6] for e in c]))
8193
+ trg_patch_list = []
8194
+ seen = []
8195
+ for p in patches_set:
8196
+ if p // 8 not in seen:
8197
+ trg_patch_list.append(p)
8198
+ seen.append(p // 8)
8199
+
8200
+ trg_patch_list = sorted(trg_patch_list)
8201
+
8202
+ else:
8203
+ trg_patch_list = sorted(set([e[6] for e in c]))
8204
+
8205
+ if 128 in trg_patch_list and n_patches > 1:
8206
+ trg_patch_list = trg_patch_list[:n_patches-1] + [128]
8207
+ else:
8208
+ trg_patch_list = trg_patch_list[:n_patches]
8209
+
8210
+ for ccc in c:
8211
+
8212
+ cc = copy.deepcopy(ccc)
8213
+
8214
+ if group_patches:
8215
+ if cc[6] // 8 in [t // 8 for t in trg_patch_list]:
8216
+ if root_grouped_patches:
8217
+ cc[6] = (cc[6] // 8) * 8
8218
+ new_escore_notes.append(cc)
8219
+
8220
+ else:
8221
+ if cc[6] in trg_patch_list:
8222
+ new_escore_notes.append(cc)
8223
+
8224
+ return new_escore_notes
8225
+
8226
+ ###################################################################################
8227
+
8228
+ def escore_notes_to_image_matrix(escore_notes,
8229
+ num_img_channels=3,
8230
+ filter_out_zero_rows=False,
8231
+ filter_out_duplicate_rows=False,
8232
+ flip_matrix=False,
8233
+ reverse_matrix=False
8234
+ ):
8235
+
8236
+ escore_notes = sorted(escore_notes, key=lambda x: (x[1], x[6]))
8237
+
8238
+ if num_img_channels > 1:
8239
+ n_mat_channels = 3
8240
+ else:
8241
+ n_mat_channels = 1
8242
+
8243
+ if escore_notes:
8244
+ last_time = escore_notes[-1][1]
8245
+ last_notes = [e for e in escore_notes if e[1] == last_time]
8246
+ max_last_dur = max([e[2] for e in last_notes])
8247
+
8248
+ time_range = last_time+max_last_dur
8249
+
8250
+ escore_matrix = []
8251
+
8252
+ escore_matrix = [[0] * 128 for _ in range(time_range)]
8253
+
8254
+ for note in escore_notes:
8255
+
8256
+ etype, time, duration, chan, pitch, velocity, pat = note
8257
+
8258
+ time = max(0, time)
8259
+ duration = max(2, duration)
8260
+ chan = max(0, min(15, chan))
8261
+ pitch = max(0, min(127, pitch))
8262
+ velocity = max(0, min(127, velocity))
8263
+ patch = max(0, min(128, pat))
8264
+
8265
+ if chan != 9:
8266
+ pat = patch + 128
8267
+ else:
8268
+ pat = 127
8269
+
8270
+ seen_pats = []
8271
+
8272
+ for t in range(time, min(time + duration, time_range)):
8273
+
8274
+ mat_value = escore_matrix[t][pitch]
8275
+
8276
+ mat_value_0 = (mat_value // (256 * 256)) % 256
8277
+ mat_value_1 = (mat_value // 256) % 256
8278
+
8279
+ cur_num_chans = 0
8280
+
8281
+ if 0 < mat_value < 256 and pat not in seen_pats:
8282
+ cur_num_chans = 1
8283
+ elif 256 < mat_value < (256 * 256) and pat not in seen_pats:
8284
+ cur_num_chans = 2
8285
+
8286
+ if cur_num_chans < n_mat_channels:
8287
+
8288
+ if n_mat_channels == 1:
8289
+
8290
+ escore_matrix[t][pitch] = pat
8291
+ seen_pats.append(pat)
8292
+
8293
+ elif n_mat_channels == 3:
8294
+
8295
+ if cur_num_chans == 0:
8296
+ escore_matrix[t][pitch] = pat
8297
+ seen_pats.append(pat)
8298
+ elif cur_num_chans == 1:
8299
+ escore_matrix[t][pitch] = (256 * 256 * mat_value_0) + (256 * pat)
8300
+ seen_pats.append(pat)
8301
+ elif cur_num_chans == 2:
8302
+ escore_matrix[t][pitch] = (256 * 256 * mat_value_0) + (256 * mat_value_1) + pat
8303
+ seen_pats.append(pat)
8304
+
8305
+ if filter_out_zero_rows:
8306
+ escore_matrix = [e for e in escore_matrix if sum(e) != 0]
8307
+
8308
+ if filter_out_duplicate_rows:
8309
+
8310
+ dd_escore_matrix = []
8311
+
8312
+ pr = [-1] * 128
8313
+ for e in escore_matrix:
8314
+ if e != pr:
8315
+ dd_escore_matrix.append(e)
8316
+ pr = e
8317
+
8318
+ escore_matrix = dd_escore_matrix
8319
+
8320
+ if flip_matrix:
8321
+
8322
+ temp_matrix = []
8323
+
8324
+ for m in escore_matrix:
8325
+ temp_matrix.append(m[::-1])
8326
+
8327
+ escore_matrix = temp_matrix
8328
+
8329
+ if reverse_matrix:
8330
+ escore_matrix = escore_matrix[::-1]
8331
+
8332
+ return escore_matrix
8333
+
8334
+ else:
8335
+ return None
8336
+
8337
+ ###################################################################################
8338
+
8339
+ def find_value_power(value, number):
8340
+ return math.floor(math.log(value, number))
8341
+
8342
+ ###################################################################################
8343
+
8344
+ def image_matrix_to_original_escore_notes(image_matrix,
8345
+ velocity=-1
8346
+ ):
8347
+
8348
+ result = []
8349
+
8350
+ for j in range(len(image_matrix[0])):
8351
+
8352
+ count = 1
8353
+
8354
+ for i in range(1, len(image_matrix)):
8355
+
8356
+ if image_matrix[i][j] != 0 and image_matrix[i][j] == image_matrix[i-1][j]:
8357
+ count += 1
8358
+
8359
+ else:
8360
+ if count > 1:
8361
+ result.append([i-count, count, j, image_matrix[i-1][j]])
8362
+
8363
+ else:
8364
+ if image_matrix[i-1][j] != 0:
8365
+ result.append([i-count, count, j, image_matrix[i-1][j]])
8366
+
8367
+ count = 1
8368
+
8369
+ if count > 1:
8370
+ result.append([len(image_matrix)-count, count, j, image_matrix[-1][j]])
8371
+
8372
+ else:
8373
+ if image_matrix[i-1][j] != 0:
8374
+ result.append([i-count, count, j, image_matrix[i-1][j]])
8375
+
8376
+ result.sort(key=lambda x: (x[0], -x[2]))
8377
+
8378
+ original_escore_notes = []
8379
+
8380
+ vel = velocity
8381
+
8382
+ for r in result:
8383
+
8384
+ if velocity == -1:
8385
+ vel = max(40, r[2])
8386
+
8387
+ ptc0 = 0
8388
+ ptc1 = 0
8389
+ ptc2 = 0
8390
+
8391
+ if find_value_power(r[3], 256) == 0:
8392
+ ptc0 = r[3] % 256
8393
+
8394
+ elif find_value_power(r[3], 256) == 1:
8395
+ ptc0 = r[3] // 256
8396
+ ptc1 = (r[3] // 256) % 256
8397
+
8398
+ elif find_value_power(r[3], 256) == 2:
8399
+ ptc0 = (r[3] // 256) // 256
8400
+ ptc1 = (r[3] // 256) % 256
8401
+ ptc2 = r[3] % 256
8402
+
8403
+ ptcs = [ptc0, ptc1, ptc2]
8404
+ patches = [p for p in ptcs if p != 0]
8405
+
8406
+ for i, p in enumerate(patches):
8407
+
8408
+ if p < 128:
8409
+ patch = 128
8410
+ channel = 9
8411
+
8412
+ else:
8413
+ patch = p % 128
8414
+ chan = p // 8
8415
+
8416
+ if chan == 9:
8417
+ chan += 1
8418
+
8419
+ channel = min(15, chan)
8420
+
8421
+ original_escore_notes.append(['note', r[0], r[1], channel, r[2], vel, patch])
8422
+
8423
+ output_score = sorted(original_escore_notes, key=lambda x: (x[1], -x[4], x[6]))
8424
+
8425
+ adjust_score_velocities(output_score, 127)
8426
+
8427
+ return output_score
8428
+
8429
+ ###################################################################################
8430
+
8431
+ def escore_notes_delta_times(escore_notes,
8432
+ timings_index=1,
8433
+ channels_index=3,
8434
+ omit_zeros=False,
8435
+ omit_drums=False
8436
+ ):
8437
+
8438
+ if omit_drums:
8439
+
8440
+ score = [e for e in escore_notes if e[channels_index] != 9]
8441
+ dtimes = [score[0][timings_index]] + [b[timings_index]-a[timings_index] for a, b in zip(score[:-1], score[1:])]
8442
+
8443
+ else:
8444
+ dtimes = [escore_notes[0][timings_index]] + [b[timings_index]-a[timings_index] for a, b in zip(escore_notes[:-1], escore_notes[1:])]
8445
+
8446
+ if omit_zeros:
8447
+ dtimes = [d for d in dtimes if d != 0]
8448
+
8449
+ return dtimes
8450
+
8451
+ ###################################################################################
8452
+
8453
+ def monophonic_check(escore_notes, times_index=1):
8454
+ return len(escore_notes) == len(set([e[times_index] for e in escore_notes]))
8455
+
8456
+ ###################################################################################
8457
+
8458
+ def count_escore_notes_patches(escore_notes, patches_index=6):
8459
+ return [list(c) for c in Counter([e[patches_index] for e in escore_notes]).most_common()]
8460
+
8461
+ ###################################################################################
8462
+
8463
+ def escore_notes_medley(list_of_escore_notes,
8464
+ list_of_labels=None,
8465
+ pause_time_value=255
8466
+ ):
8467
+
8468
+ if list_of_labels is not None:
8469
+ labels = [str(l) for l in list_of_labels] + ['No label'] * (len(list_of_escore_notes)-len(list_of_labels))
8470
+
8471
+ medley = []
8472
+
8473
+ time = 0
8474
+
8475
+ for i, m in enumerate(list_of_escore_notes):
8476
+
8477
+ if list_of_labels is not None:
8478
+ medley.append(['text_event', time, labels[i]])
8479
+
8480
+ pe = m[0]
8481
+
8482
+ for mm in m:
8483
+
8484
+ time += mm[1] - pe[1]
8485
+
8486
+ mmm = copy.deepcopy(mm)
8487
+ mmm[1] = time
8488
+
8489
+ medley.append(mmm)
8490
+
8491
+ pe = mm
8492
+
8493
+ time += pause_time_value
8494
+
8495
+ return medley
8496
+
8497
+ ###################################################################################
8498
+
8499
+ def proportions_counter(list_of_values):
8500
+
8501
+ counts = Counter(list_of_values).most_common()
8502
+ clen = sum([c[1] for c in counts])
8503
+
8504
+ return [[c[0], c[1], c[1] / clen] for c in counts]
8505
+
8506
+ ###################################################################################
8507
+
8508
+ def smooth_escore_notes(escore_notes):
8509
+
8510
+ values = [e[4] % 24 for e in escore_notes]
8511
+
8512
+ smoothed = [values[0]]
8513
+
8514
+ for i in range(1, len(values)):
8515
+ if abs(smoothed[-1] - values[i]) >= 12:
8516
+ if smoothed[-1] < values[i]:
8517
+ smoothed.append(values[i] - 12)
8518
+ else:
8519
+ smoothed.append(values[i] + 12)
8520
+ else:
8521
+ smoothed.append(values[i])
8522
+
8523
+ smoothed_score = copy.deepcopy(escore_notes)
8524
+
8525
+ for i, e in enumerate(smoothed_score):
8526
+ esn_octave = escore_notes[i][4] // 12
8527
+ e[4] = (esn_octave * 12) + smoothed[i]
8528
+
8529
+ return smoothed_score
8530
+
8531
+ ###################################################################################
8532
+
8533
+ def add_base_to_escore_notes(escore_notes,
8534
+ base_octave=2,
8535
+ base_channel=2,
8536
+ base_patch=35,
8537
+ base_max_velocity=120,
8538
+ return_base=False
8539
+ ):
8540
+
8541
+
8542
+ score = copy.deepcopy(escore_notes)
8543
+
8544
+ cscore = chordify_score([1000, score])
8545
+
8546
+ base_score = []
8547
+
8548
+ for c in cscore:
8549
+ chord = sorted([e for e in c if e[3] != 9], key=lambda x: x[4], reverse=True)
8550
+ base_score.append(chord[-1])
8551
+
8552
+ base_score = smooth_escore_notes(base_score)
8553
+
8554
+ for e in base_score:
8555
+ e[3] = base_channel
8556
+ e[4] = (base_octave * 12) + (e[4] % 12)
8557
+ e[5] = e[4]
8558
+ e[6] = base_patch
8559
+
8560
+ adjust_score_velocities(base_score, base_max_velocity)
8561
+
8562
+ if return_base:
8563
+ final_score = sorted(base_score, key=lambda x: (x[1], -x[4], x[6]))
8564
+
8565
+ else:
8566
+ final_score = sorted(escore_notes + base_score, key=lambda x: (x[1], -x[4], x[6]))
8567
+
8568
+ return final_score
8569
+
8570
+ ###################################################################################
8571
+
8572
+ def add_drums_to_escore_notes(escore_notes,
8573
+ heavy_drums_pitches=[36, 38, 47],
8574
+ heavy_drums_velocity=110,
8575
+ light_drums_pitches=[51, 54],
8576
+ light_drums_velocity=127,
8577
+ drums_max_velocity=127,
8578
+ drums_ratio_time_divider=4,
8579
+ return_drums=False
8580
+ ):
8581
+
8582
+ score = copy.deepcopy([e for e in escore_notes if e[3] != 9])
8583
+
8584
+ cscore = chordify_score([1000, score])
8585
+
8586
+ drums_score = []
8587
+
8588
+ for c in cscore:
8589
+ min_dur = max(1, min([e[2] for e in c]))
8590
+ if not (c[0][1] % drums_ratio_time_divider):
8591
+ drum_note = ['note', c[0][1], min_dur, 9, heavy_drums_pitches[c[0][4] % len(heavy_drums_pitches)], heavy_drums_velocity, 128]
8592
+ else:
8593
+ drum_note = ['note', c[0][1], min_dur, 9, light_drums_pitches[c[0][4] % len(light_drums_pitches)], light_drums_velocity, 128]
8594
+ drums_score.append(drum_note)
8595
+
8596
+ adjust_score_velocities(drums_score, drums_max_velocity)
8597
+
8598
+ if return_drums:
8599
+ final_score = sorted(drums_score, key=lambda x: (x[1], -x[4], x[6]))
8600
+
8601
+ else:
8602
+ final_score = sorted(score + drums_score, key=lambda x: (x[1], -x[4], x[6]))
8603
+
8604
+ return final_score
8605
+
8606
+ ###################################################################################
8607
+
8608
+ def find_pattern_start_indexes(values, pattern):
8609
+
8610
+ start_indexes = []
8611
+
8612
+ count = 0
8613
+
8614
+ for i in range(len(values)- len(pattern)):
8615
+ chunk = values[i:i+len(pattern)]
8616
+
8617
+ if chunk == pattern:
8618
+ start_indexes.append(i)
8619
+
8620
+ return start_indexes
8621
+
8622
+ ###################################################################################
8623
+
8624
+ def escore_notes_lrno_pattern(escore_notes, mode='chords'):
8625
+
8626
+ cscore = chordify_score([1000, escore_notes])
8627
+
8628
+ checked_cscore = advanced_check_and_fix_chords_in_chordified_score(cscore)
8629
+
8630
+ chords_toks = []
8631
+ chords_idxs = []
8632
+
8633
+ for i, c in enumerate(checked_cscore[0]):
8634
+
8635
+ pitches = sorted([p[4] for p in c if p[3] != 9], reverse=True)
8636
+ tchord = pitches_to_tones_chord(pitches)
8637
+
8638
+ if tchord:
8639
+
8640
+ if mode == 'chords':
8641
+ token = ALL_CHORDS_FULL.index(tchord)
8642
+
8643
+ elif mode == 'high pitches':
8644
+ token = pitches[0]
8645
+
8646
+ elif mode == 'high pitches tones':
8647
+ token = pitches[0] % 12
8648
+
8649
+ else:
8650
+ token = ALL_CHORDS_FULL.index(tchord)
8651
+
8652
+ chords_toks.append(token)
8653
+ chords_idxs.append(i)
8654
+
8655
+ lrno_pats = find_lrno_patterns(chords_toks)
8656
+
8657
+ if lrno_pats:
8658
+
8659
+ lrno_pattern = list(lrno_pats[0][2])
8660
+
8661
+ start_idx = chords_idxs[find_pattern_start_indexes(chords_toks, lrno_pattern)[0]]
8662
+ end_idx = chords_idxs[start_idx + len(lrno_pattern)]
8663
+
8664
+ return recalculate_score_timings(flatten(cscore[start_idx:end_idx]))
8665
+
8666
+ else:
8667
+ return None
8668
+
8669
+ ###################################################################################
8670
+
8671
+ def chordified_score_pitches(chordified_score,
8672
+ mode='dominant',
8673
+ return_tones=False,
8674
+ omit_drums=True,
8675
+ score_patch=-1,
8676
+ channels_index=3,
8677
+ pitches_index=4,
8678
+ patches_index=6
8679
+ ):
8680
+
8681
+ results = []
8682
+
8683
+ for c in chordified_score:
8684
+
8685
+ if -1 < score_patch < 128:
8686
+ ptcs = sorted([e[pitches_index] for e in c if e[channels_index] != 9 and e[patches_index] == score_patch], reverse=True)
8687
+
8688
+ else:
8689
+ ptcs = sorted([e[pitches_index] for e in c if e[channels_index] != 9], reverse=True)
8690
+
8691
+ if ptcs:
8692
+
8693
+ if mode == 'dominant':
8694
+
8695
+ mtone = statistics.mode([p % 12 for p in ptcs])
8696
+
8697
+ if return_tones:
8698
+ results.append(mtone)
8699
+
8700
+ else:
8701
+ results.append(sorted(set([p for p in ptcs if p % 12 == mtone]), reverse=True))
8702
+
8703
+ elif mode == 'high':
8704
+
8705
+ if return_tones:
8706
+ results.append(ptcs[0] % 12)
8707
+
8708
+ else:
8709
+ results.append([ptcs[0]])
8710
+
8711
+ elif mode == 'base':
8712
+
8713
+ if return_tones:
8714
+ results.append(ptcs[-1] % 12)
8715
+
8716
+ else:
8717
+ results.append([ptcs[-1]])
8718
+
8719
+ elif mode == 'average':
8720
+
8721
+ if return_tones:
8722
+ results.append(statistics.mean(ptcs) % 12)
8723
+
8724
+ else:
8725
+ results.append([statistics.mean(ptcs)])
8726
+
8727
+ else:
8728
+
8729
+ mtone = statistics.mode([p % 12 for p in ptcs])
8730
+
8731
+ if return_tones:
8732
+ results.append(mtone)
8733
+
8734
+ else:
8735
+ results.append(sorted(set([p for p in ptcs if p % 12 == mtone]), reverse=True))
8736
+
8737
+ else:
8738
+
8739
+ if not omit_drums:
8740
+
8741
+ if return_tones:
8742
+ results.append(-1)
8743
+
8744
+ else:
8745
+ results.append([-1])
8746
+
8747
+ return results
8748
+
8749
+ ###################################################################################
8750
+
8751
+ def escore_notes_times_tones(escore_notes,
8752
+ tones_mode='dominant',
8753
+ return_abs_times=True,
8754
+ omit_drums=False
8755
+ ):
8756
+
8757
+ cscore = chordify_score([1000, escore_notes])
8758
+
8759
+ tones = chordified_score_pitches(cscore, return_tones=True, mode=tones_mode, omit_drums=omit_drums)
8760
+
8761
+ if return_abs_times:
8762
+ times = sorted([c[0][1] for c in cscore])
8763
+
8764
+ else:
8765
+ times = escore_notes_delta_times(escore_notes, omit_zeros=True, omit_drums=omit_drums)
8766
+
8767
+ if len(times) != len(tones):
8768
+ times = [0] + times
8769
+
8770
+ return [[t, to] for t, to in zip(times, tones)]
8771
+
8772
+ ###################################################################################
8773
+
8774
+ def escore_notes_middle(escore_notes,
8775
+ length=10,
8776
+ use_chords=True
8777
+ ):
8778
+
8779
+ if use_chords:
8780
+ score = chordify_score([1000, escore_notes])
8781
+
8782
+ else:
8783
+ score = escore_notes
8784
+
8785
+ middle_idx = len(score) // 2
8786
+
8787
+ slen = min(len(score) // 2, length // 2)
8788
+
8789
+ start_idx = middle_idx - slen
8790
+ end_idx = middle_idx + slen
8791
+
8792
+ if use_chords:
8793
+ return flatten(score[start_idx:end_idx])
8794
+
8795
+ else:
8796
+ return score[start_idx:end_idx]
8797
+
8798
+ ###################################################################################
8799
+
8800
+ ALL_CHORDS_FULL = [[0], [0, 3], [0, 3, 5], [0, 3, 5, 8], [0, 3, 5, 9], [0, 3, 5, 10], [0, 3, 6],
8801
+ [0, 3, 6, 9], [0, 3, 6, 10], [0, 3, 7], [0, 3, 7, 10], [0, 3, 8], [0, 3, 9],
8802
+ [0, 3, 10], [0, 4], [0, 4, 6], [0, 4, 6, 9], [0, 4, 6, 10], [0, 4, 7],
8803
+ [0, 4, 7, 10], [0, 4, 8], [0, 4, 9], [0, 4, 10], [0, 5], [0, 5, 8], [0, 5, 9],
8804
+ [0, 5, 10], [0, 6], [0, 6, 9], [0, 6, 10], [0, 7], [0, 7, 10], [0, 8], [0, 9],
8805
+ [0, 10], [1], [1, 4], [1, 4, 6], [1, 4, 6, 9], [1, 4, 6, 10], [1, 4, 6, 11],
8806
+ [1, 4, 7], [1, 4, 7, 10], [1, 4, 7, 11], [1, 4, 8], [1, 4, 8, 11], [1, 4, 9],
8807
+ [1, 4, 10], [1, 4, 11], [1, 5], [1, 5, 8], [1, 5, 8, 11], [1, 5, 9],
8808
+ [1, 5, 10], [1, 5, 11], [1, 6], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 7],
8809
+ [1, 7, 10], [1, 7, 11], [1, 8], [1, 8, 11], [1, 9], [1, 10], [1, 11], [2],
8810
+ [2, 5], [2, 5, 8], [2, 5, 8, 11], [2, 5, 9], [2, 5, 10], [2, 5, 11], [2, 6],
8811
+ [2, 6, 9], [2, 6, 10], [2, 6, 11], [2, 7], [2, 7, 10], [2, 7, 11], [2, 8],
8812
+ [2, 8, 11], [2, 9], [2, 10], [2, 11], [3], [3, 5], [3, 5, 8], [3, 5, 8, 11],
8813
+ [3, 5, 9], [3, 5, 10], [3, 5, 11], [3, 6], [3, 6, 9], [3, 6, 10], [3, 6, 11],
8814
+ [3, 7], [3, 7, 10], [3, 7, 11], [3, 8], [3, 8, 11], [3, 9], [3, 10], [3, 11],
8815
+ [4], [4, 6], [4, 6, 9], [4, 6, 10], [4, 6, 11], [4, 7], [4, 7, 10], [4, 7, 11],
8816
+ [4, 8], [4, 8, 11], [4, 9], [4, 10], [4, 11], [5], [5, 8], [5, 8, 11], [5, 9],
8817
+ [5, 10], [5, 11], [6], [6, 9], [6, 10], [6, 11], [7], [7, 10], [7, 11], [8],
8818
+ [8, 11], [9], [10], [11]]
8819
+
8820
+ ###################################################################################
8821
+
8822
+ def escore_notes_to_parsons_code(escore_notes,
8823
+ times_index=1,
8824
+ pitches_index=4,
8825
+ return_as_list=False
8826
+ ):
8827
+
8828
+ parsons = "*"
8829
+ parsons_list = []
8830
+
8831
+ prev = ['note', -1, -1, -1, -1, -1, -1]
8832
+
8833
+ for e in escore_notes:
8834
+ if e[times_index] != prev[times_index]:
8835
+
8836
+ if e[pitches_index] > prev[pitches_index]:
8837
+ parsons += "U"
8838
+ parsons_list.append(1)
8839
+
8840
+ elif e[pitches_index] < prev[pitches_index]:
8841
+ parsons += "D"
8842
+ parsons_list.append(-1)
8843
+
8844
+ elif e[pitches_index] == prev[pitches_index]:
8845
+ parsons += "R"
8846
+ parsons_list.append(0)
8847
+
8848
+ prev = e
8849
+
8850
+ if return_as_list:
8851
+ return parsons_list
8852
+
8853
+ else:
8854
+ return parsons
8855
+
8856
+ ###################################################################################
8857
+
8858
+ def all_consequtive(list_of_values):
8859
+ return all(b > a for a, b in zip(list_of_values[:-1], list_of_values[1:]))
8860
+
8861
+ ###################################################################################
8862
+ #
8863
+ # This is the end of the TMIDI X Python module
8864
+ #
8865
  ###################################################################################
x_transformer_1_23_2.py CHANGED
The diff for this file is too large to render. See raw diff