asigalov61 commited on
Commit
650e94e
1 Parent(s): 74f5b97

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +215 -30
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)),
@@ -2059,7 +2046,7 @@ def Optimus_MIDI_TXT_Processor(MIDI_file,
2059
 
2060
  #print('Loading MIDI file...')
2061
  midi_file = open(MIDI_file, 'rb')
2062
- if debug: print('Processing File:', file_address)
2063
 
2064
  try:
2065
  opus = midi2opus(midi_file.read())
@@ -5166,8 +5153,9 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5166
  pitches_index=4,
5167
  patches_index=6,
5168
  use_filtered_chords=False,
5169
- use_full_chords=True,
5170
  remove_duplicate_pitches=True,
 
5171
  fix_bad_pitches=False,
5172
  skip_drums=False
5173
  ):
@@ -5221,22 +5209,64 @@ def advanced_check_and_fix_chords_in_chordified_score(chordified_score,
5221
  tones_counts = Counter([p % 12 for p in pitches_chord]).most_common()
5222
 
5223
  if tones_counts[0][1] > 1:
5224
- tones_chord = [tones_counts[0][0]]
 
5225
 
5226
  elif tones_counts[1][1] > 1:
5227
- tones_chord = [tones_counts[1][0]]
 
5228
 
5229
  else:
5230
- tones_chord = [pitches_chord[0] % 12]
 
5231
 
5232
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5233
  tones_chord_combs = [list(comb) for i in range(len(tones_chord)-1, 0, -1) for comb in combinations(tones_chord, i)]
5234
 
5235
  for co in tones_chord_combs:
5236
  if co in CHORDS:
5237
- tones_chord = co
5238
  break
5239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5240
  if len(tones_chord) == 1:
5241
  tones_chord = [pitches_chord[0] % 12]
5242
 
@@ -5636,7 +5666,7 @@ def basic_enhanced_delta_score_notes_tokenizer(enhanced_delta_score_notes,
5636
  final_score_tokens_ints_seq = flatten(score_tokens_ints_seq)
5637
 
5638
  if max_seq_len > -1:
5639
- final_score_tokens_ints_seq = flat_score_tokens_ints_seq[:max_seq_len]
5640
 
5641
  if seq_pad_value > -1:
5642
  final_score_tokens_ints_seq += [seq_pad_value] * (max_seq_len - len(final_score_tokens_ints_seq))
@@ -8154,7 +8184,7 @@ def compress_patches_in_escore_notes(escore_notes,
8154
  n_patches = num_patches
8155
 
8156
  if group_patches:
8157
- patches_set = sorted(set([e[6] for e in c]))
8158
  trg_patch_list = []
8159
  seen = []
8160
  for p in patches_set:
@@ -8165,7 +8195,7 @@ def compress_patches_in_escore_notes(escore_notes,
8165
  trg_patch_list = sorted(trg_patch_list)
8166
 
8167
  else:
8168
- trg_patch_list = sorted(set([e[6] for e in c]))
8169
 
8170
  if 128 in trg_patch_list and n_patches > 1:
8171
  trg_patch_list = trg_patch_list[:n_patches-1] + [128]
@@ -8834,33 +8864,188 @@ ALL_CHORDS_FULL = [[0], [0, 3], [0, 3, 5], [0, 3, 5, 8], [0, 3, 5, 9], [0, 3, 5,
8834
 
8835
  def escore_notes_to_parsons_code(escore_notes,
8836
  times_index=1,
8837
- pitches_index=4
 
8838
  ):
8839
 
8840
- parsons = ""
 
8841
 
8842
  prev = ['note', -1, -1, -1, -1, -1, -1]
8843
 
8844
  for e in escore_notes:
8845
  if e[times_index] != prev[times_index]:
8846
 
8847
- if parsons == "":
8848
- parsons += "*"
8849
-
8850
- elif e[pitches_index] > prev[pitches_index]:
8851
  parsons += "U"
 
8852
 
8853
  elif e[pitches_index] < prev[pitches_index]:
8854
  parsons += "D"
 
8855
 
8856
  elif e[pitches_index] == prev[pitches_index]:
8857
  parsons += "R"
 
8858
 
8859
  prev = e
8860
 
8861
- return parsons
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8862
 
8863
  ###################################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8864
  #
8865
  # This is the end of the TMIDI X Python module
8866
  #
 
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)),
 
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())
 
5153
  pitches_index=4,
5154
  patches_index=6,
5155
  use_filtered_chords=False,
5156
+ use_full_chords=False,
5157
  remove_duplicate_pitches=True,
5158
+ fix_bad_tones_chords=False,
5159
  fix_bad_pitches=False,
5160
  skip_drums=False
5161
  ):
 
5209
  tones_counts = Counter([p % 12 for p in pitches_chord]).most_common()
5210
 
5211
  if tones_counts[0][1] > 1:
5212
+ good_tone = tones_counts[0][0]
5213
+ bad_tone = tones_counts[1][0]
5214
 
5215
  elif tones_counts[1][1] > 1:
5216
+ good_tone = tones_counts[1][0]
5217
+ bad_tone = tones_counts[0][0]
5218
 
5219
  else:
5220
+ good_tone = pitches_chord[0] % 12
5221
+ bad_tone = [t for t in tones_chord if t != good_tone][0]
5222
 
5223
+ tones_chord = [good_tone]
5224
+
5225
+ if fix_bad_tones_chords:
5226
+
5227
+ if good_tone > bad_tone:
5228
+
5229
+ if sorted([good_tone, (12+(bad_tone+1)) % 12]) in CHORDS:
5230
+ tones_chord = sorted([good_tone, (12+(bad_tone-1)) % 12])
5231
+
5232
+ elif sorted([good_tone, (12+(bad_tone-1)) % 12]) in CHORDS:
5233
+ tones_chord = sorted([good_tone, (12+(bad_tone+1)) % 12])
5234
+
5235
+ else:
5236
+
5237
+ if sorted([good_tone, (12+(bad_tone-1)) % 12]) in CHORDS:
5238
+ tones_chord = sorted([good_tone, (12+(bad_tone-1)) % 12])
5239
+
5240
+ elif sorted([good_tone, (12+(bad_tone+1)) % 12]) in CHORDS:
5241
+ tones_chord = sorted([good_tone, (12+(bad_tone+1)) % 12])
5242
+
5243
+ if len(tones_chord) > 2:
5244
  tones_chord_combs = [list(comb) for i in range(len(tones_chord)-1, 0, -1) for comb in combinations(tones_chord, i)]
5245
 
5246
  for co in tones_chord_combs:
5247
  if co in CHORDS:
 
5248
  break
5249
 
5250
+ if fix_bad_tones_chords:
5251
+
5252
+ dt_chord = list(set(co) ^ set(tones_chord))
5253
+
5254
+ for t in dt_chord:
5255
+ tones_chord.append((12+(t+1)) % 12)
5256
+ tones_chord.append((12+(t-1)) % 12)
5257
+
5258
+ ex_tones_chord = sorted(set(tones_chord))
5259
+
5260
+ tones_chord_combs = [list(comb) for i in range(4, 0, -2) for comb in combinations(ex_tones_chord, i) if all(t in list(comb) for t in co)]
5261
+
5262
+ for eco in tones_chord_combs:
5263
+ if eco in CHORDS:
5264
+ tones_chord = eco
5265
+ break
5266
+
5267
+ else:
5268
+ tones_chord = co
5269
+
5270
  if len(tones_chord) == 1:
5271
  tones_chord = [pitches_chord[0] % 12]
5272
 
 
5666
  final_score_tokens_ints_seq = flatten(score_tokens_ints_seq)
5667
 
5668
  if max_seq_len > -1:
5669
+ final_score_tokens_ints_seq = final_score_tokens_ints_seq[:max_seq_len]
5670
 
5671
  if seq_pad_value > -1:
5672
  final_score_tokens_ints_seq += [seq_pad_value] * (max_seq_len - len(final_score_tokens_ints_seq))
 
8184
  n_patches = num_patches
8185
 
8186
  if group_patches:
8187
+ patches_set = sorted(set([e[6] for e in escore_notes]))
8188
  trg_patch_list = []
8189
  seen = []
8190
  for p in patches_set:
 
8195
  trg_patch_list = sorted(trg_patch_list)
8196
 
8197
  else:
8198
+ trg_patch_list = sorted(set([e[6] for e in escore_notes]))
8199
 
8200
  if 128 in trg_patch_list and n_patches > 1:
8201
  trg_patch_list = trg_patch_list[:n_patches-1] + [128]
 
8864
 
8865
  def escore_notes_to_parsons_code(escore_notes,
8866
  times_index=1,
8867
+ pitches_index=4,
8868
+ return_as_list=False
8869
  ):
8870
 
8871
+ parsons = "*"
8872
+ parsons_list = []
8873
 
8874
  prev = ['note', -1, -1, -1, -1, -1, -1]
8875
 
8876
  for e in escore_notes:
8877
  if e[times_index] != prev[times_index]:
8878
 
8879
+ if e[pitches_index] > prev[pitches_index]:
 
 
 
8880
  parsons += "U"
8881
+ parsons_list.append(1)
8882
 
8883
  elif e[pitches_index] < prev[pitches_index]:
8884
  parsons += "D"
8885
+ parsons_list.append(-1)
8886
 
8887
  elif e[pitches_index] == prev[pitches_index]:
8888
  parsons += "R"
8889
+ parsons_list.append(0)
8890
 
8891
  prev = e
8892
 
8893
+ if return_as_list:
8894
+ return parsons_list
8895
+
8896
+ else:
8897
+ return parsons
8898
+
8899
+ ###################################################################################
8900
+
8901
+ def all_consequtive(list_of_values):
8902
+ return all(b > a for a, b in zip(list_of_values[:-1], list_of_values[1:]))
8903
+
8904
+ ###################################################################################
8905
+
8906
+ def escore_notes_patches(escore_notes, patches_index=6):
8907
+ return sorted(set([e[patches_index] for e in escore_notes]))
8908
+
8909
+ ###################################################################################
8910
+
8911
+ def build_suffix_array(lst):
8912
+
8913
+ n = len(lst)
8914
+
8915
+ suffixes = [(lst[i:], i) for i in range(n)]
8916
+ suffixes.sort()
8917
+ suffix_array = [suffix[1] for suffix in suffixes]
8918
+
8919
+ return suffix_array
8920
+
8921
+ ###################################################################################
8922
+
8923
+ def build_lcp_array(lst, suffix_array):
8924
+
8925
+ n = len(lst)
8926
+ rank = [0] * n
8927
+ lcp = [0] * n
8928
+
8929
+ for i, suffix in enumerate(suffix_array):
8930
+ rank[suffix] = i
8931
+
8932
+ h = 0
8933
+
8934
+ for i in range(n):
8935
+ if rank[i] > 0:
8936
+
8937
+ j = suffix_array[rank[i] - 1]
8938
+
8939
+ while i + h < n and j + h < n and lst[i + h] == lst[j + h]:
8940
+ h += 1
8941
+
8942
+ lcp[rank[i]] = h
8943
+
8944
+ if h > 0:
8945
+ h -= 1
8946
+
8947
+ return lcp
8948
 
8949
  ###################################################################################
8950
+
8951
+ def find_lrno_pattern_fast(lst):
8952
+ n = len(lst)
8953
+ if n == 0:
8954
+ return []
8955
+
8956
+ suffix_array = build_suffix_array(lst)
8957
+ lcp_array = build_lcp_array(lst, suffix_array)
8958
+
8959
+ max_len = 0
8960
+ start_index = 0
8961
+
8962
+ for i in range(1, n):
8963
+ if lcp_array[i] > max_len:
8964
+ if suffix_array[i] + lcp_array[i] <= suffix_array[i - 1] or suffix_array[i - 1] + lcp_array[i - 1] <= suffix_array[i]:
8965
+ max_len = lcp_array[i]
8966
+ start_index = suffix_array[i]
8967
+
8968
+ return lst[start_index:start_index + max_len]
8969
+
8970
+ ###################################################################################
8971
+
8972
+ def find_chunk_indexes(original_list, chunk, ignore_index=-1):
8973
+
8974
+ chunk_length = len(chunk)
8975
+
8976
+ for i in range(len(original_list) - chunk_length + 1):
8977
+
8978
+ chunk_index = 0
8979
+ start_index = ignore_index
8980
+
8981
+ for j in range(i, len(original_list)):
8982
+ if original_list[j] == chunk[chunk_index]:
8983
+
8984
+ if start_index == ignore_index:
8985
+ start_index = j
8986
+
8987
+ chunk_index += 1
8988
+
8989
+ if chunk_index == chunk_length:
8990
+ return [start_index, j]
8991
+
8992
+ elif original_list[j] != ignore_index:
8993
+ break
8994
+
8995
+ return None
8996
+
8997
+ ###################################################################################
8998
+
8999
+ def escore_notes_lrno_pattern_fast(escore_notes,
9000
+ channels_index=3,
9001
+ pitches_index=4,
9002
+ zero_start_time=True
9003
+ ):
9004
+
9005
+ cscore = chordify_score([1000, escore_notes])
9006
+
9007
+ score_chords = []
9008
+
9009
+ for c in cscore:
9010
+
9011
+ tchord = sorted(set([e[pitches_index] % 12 for e in c if e[channels_index] != 9]))
9012
+
9013
+ chord_tok = -1
9014
+
9015
+ if tchord:
9016
+
9017
+ if tchord not in ALL_CHORDS_FULL:
9018
+ tchord = check_and_fix_tones_chord(tchord)
9019
+
9020
+ chord_tok = ALL_CHORDS_FULL.index(tchord)
9021
+
9022
+ score_chords.append(chord_tok)
9023
+
9024
+ schords = [c for c in score_chords if c != -1]
9025
+
9026
+ lrno = find_lrno_pattern_fast(schords)
9027
+
9028
+ if lrno:
9029
+
9030
+ sidx, eidx = find_chunk_indexes(score_chords, lrno)
9031
+
9032
+ escore_notes_lrno_pattern = flatten(cscore[sidx:eidx+1])
9033
+
9034
+ if escore_notes_lrno_pattern is not None:
9035
+
9036
+ if zero_start_time:
9037
+ return recalculate_score_timings(escore_notes_lrno_pattern)
9038
+
9039
+ else:
9040
+ return escore_notes_lrno_pattern
9041
+
9042
+ else:
9043
+ return None
9044
+
9045
+ else:
9046
+ return None
9047
+
9048
+ ###################################################################################
9049
  #
9050
  # This is the end of the TMIDI X Python module
9051
  #