asigalov61 commited on
Commit
86967ea
·
verified ·
1 Parent(s): 32bd50f

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +108 -18
TMIDIX.py CHANGED
@@ -1471,6 +1471,8 @@ import csv
1471
 
1472
  import tqdm
1473
 
 
 
1474
  from itertools import zip_longest
1475
  from itertools import groupby
1476
  from collections import Counter
@@ -5589,7 +5591,7 @@ def split_melody(enhanced_melody_score_notes,
5589
  for e in enhanced_melody_score_notes:
5590
  dtime = max(0, min(max_score_time, e[1]-pe[1]))
5591
 
5592
- if dtime > max(durs):
5593
  if chu:
5594
  mel_chunks.append(chu)
5595
  chu = []
@@ -9603,7 +9605,7 @@ def escore_notes_to_text_description(escore_notes,
9603
 
9604
  all_patches = [e[6] for e in escore_notes]
9605
 
9606
- patches = ordered_set(all_patches)
9607
 
9608
  instruments = [alpha_str(Number2patch[p]) for p in patches if p < 128]
9609
 
@@ -9762,7 +9764,6 @@ def escore_notes_to_text_description(escore_notes,
9762
 
9763
  description += 'This ' + mood + ' melody has '
9764
 
9765
-
9766
  else:
9767
  description += 'TThis drum track has '
9768
 
@@ -9776,19 +9777,17 @@ def escore_notes_to_text_description(escore_notes,
9776
  if instruments:
9777
 
9778
  if comp_type not in ['monophonic melody', 'drum track']:
 
9779
  description += 'The song '
9780
 
9781
- else:
9782
- description += 'The melody '
9783
-
9784
- if len(instruments) == 1:
9785
- description += 'is played on a solo ' + instruments[0] + '.'
9786
 
9787
- description += '\n'
9788
-
9789
- else:
9790
- description += 'features ' + NUMERALS[max(0, min(15, len(instruments)-1))] + ' instruments: '
9791
- description += ', '.join(instruments[:-1]) + ' and ' + instruments[-1] + '.'
 
9792
 
9793
  description += '\n'
9794
 
@@ -9829,7 +9828,7 @@ def escore_notes_to_text_description(escore_notes,
9829
  description += 'The song has ' + NUMERALS[len(lead_melodies)-1] + ' distinct lead ' + mword + ' played on '
9830
 
9831
  if len(lead_melodies) > 1:
9832
- description += ', '.join(lead_melodies[:-1][0]) + ' and ' + lead_melodies[-1][0] + '.'
9833
 
9834
  else:
9835
  description += lead_melodies[0][0] + '.'
@@ -9847,7 +9846,7 @@ def escore_notes_to_text_description(escore_notes,
9847
  description += 'The song has ' + NUMERALS[len(base_melodies)-1] + ' distinct base ' + mword + ' played on '
9848
 
9849
  if len(base_melodies) > 1:
9850
- description += ', '.join(base_melodies[:-1][0]) + ' and ' + base_melodies[-1][0] + '.'
9851
 
9852
  else:
9853
  description += base_melodies[0][0] + '.'
@@ -9865,7 +9864,7 @@ def escore_notes_to_text_description(escore_notes,
9865
  description += 'The song has ' + NUMERALS[len(lead_melodies)-1] + ' distinct lead ' + lmword + ' played on '
9866
 
9867
  if len(lead_melodies) > 1:
9868
- description += ', '.join(lead_melodies[:-1][0]) + ' and ' + lead_melodies[-1][0] + '.'
9869
 
9870
  else:
9871
  description += lead_melodies[0][0] + '.'
@@ -9879,7 +9878,7 @@ def escore_notes_to_text_description(escore_notes,
9879
  description += ' And ' + NUMERALS[len(base_melodies)-1] + ' distinct base ' + bmword + ' played on '
9880
 
9881
  if len(base_melodies) > 1:
9882
- description += ', '.join(base_melodies[:-1][0]) + ' and ' + base_melodies[-1][0] + '.'
9883
 
9884
  else:
9885
  description += base_melodies[0][0] + '.'
@@ -11048,7 +11047,7 @@ LEAD_INSTRUMENTS = [0, 1, 2, 3, 4, 5, 6, 7, # Piano
11048
  8, 9, 10, 11, 12, 13, 14, 15, # Chromatic Percussion
11049
  16, 17, 18, 19, 20, 21, 22, 23, # Organ
11050
  24, 25, 26, 27, 28, 29, 30, 31, # Guitar
11051
- 40, 41, # Strings
11052
  52, 53, 54, # Ensemble
11053
  56, 57, 59, 60, # Brass
11054
  64, 65, 66, 67, 68, 69, 70, 71, # Reed
@@ -11091,6 +11090,97 @@ def escore_notes_pitches_range(escore_notes,
11091
  else:
11092
  return [ -1] * 6
11093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11094
  ###################################################################################
11095
  #
11096
  # This is the end of the TMIDI X Python module
 
1471
 
1472
  import tqdm
1473
 
1474
+ import multiprocessing
1475
+
1476
  from itertools import zip_longest
1477
  from itertools import groupby
1478
  from collections import Counter
 
5591
  for e in enhanced_melody_score_notes:
5592
  dtime = max(0, min(max_score_time, e[1]-pe[1]))
5593
 
5594
+ if dtime > stime:
5595
  if chu:
5596
  mel_chunks.append(chu)
5597
  chu = []
 
9605
 
9606
  all_patches = [e[6] for e in escore_notes]
9607
 
9608
+ patches = ordered_set(all_patches)[:16]
9609
 
9610
  instruments = [alpha_str(Number2patch[p]) for p in patches if p < 128]
9611
 
 
9764
 
9765
  description += 'This ' + mood + ' melody has '
9766
 
 
9767
  else:
9768
  description += 'TThis drum track has '
9769
 
 
9777
  if instruments:
9778
 
9779
  if comp_type not in ['monophonic melody', 'drum track']:
9780
+
9781
  description += 'The song '
9782
 
9783
+ if len(instruments) > 1:
 
 
 
 
9784
 
9785
+ description += 'features ' + NUMERALS[max(0, min(15, len(instruments)-1))] + ' instruments: '
9786
+ description += ', '.join(instruments[:-1]) + ' and ' + instruments[-1] + '.'
9787
+
9788
+ else:
9789
+ description += 'features one instrument: ' + instruments[0] + '.'
9790
+
9791
 
9792
  description += '\n'
9793
 
 
9828
  description += 'The song has ' + NUMERALS[len(lead_melodies)-1] + ' distinct lead ' + mword + ' played on '
9829
 
9830
  if len(lead_melodies) > 1:
9831
+ description += ', '.join([l[0] for l in lead_melodies[:-1]]) + ' and ' + lead_melodies[-1][0] + '.'
9832
 
9833
  else:
9834
  description += lead_melodies[0][0] + '.'
 
9846
  description += 'The song has ' + NUMERALS[len(base_melodies)-1] + ' distinct base ' + mword + ' played on '
9847
 
9848
  if len(base_melodies) > 1:
9849
+ description += ', '.join([b[0] for b in base_melodies[:-1]]) + ' and ' + base_melodies[-1][0] + '.'
9850
 
9851
  else:
9852
  description += base_melodies[0][0] + '.'
 
9864
  description += 'The song has ' + NUMERALS[len(lead_melodies)-1] + ' distinct lead ' + lmword + ' played on '
9865
 
9866
  if len(lead_melodies) > 1:
9867
+ description += ', '.join([l[0] for l in lead_melodies[:-1]]) + ' and ' + lead_melodies[-1][0] + '.'
9868
 
9869
  else:
9870
  description += lead_melodies[0][0] + '.'
 
9878
  description += ' And ' + NUMERALS[len(base_melodies)-1] + ' distinct base ' + bmword + ' played on '
9879
 
9880
  if len(base_melodies) > 1:
9881
+ description += ', '.join([b[0] for b in base_melodies[:-1]]) + ' and ' + base_melodies[-1][0] + '.'
9882
 
9883
  else:
9884
  description += base_melodies[0][0] + '.'
 
11047
  8, 9, 10, 11, 12, 13, 14, 15, # Chromatic Percussion
11048
  16, 17, 18, 19, 20, 21, 22, 23, # Organ
11049
  24, 25, 26, 27, 28, 29, 30, 31, # Guitar
11050
+ 40, 41, 46, # Strings
11051
  52, 53, 54, # Ensemble
11052
  56, 57, 59, 60, # Brass
11053
  64, 65, 66, 67, 68, 69, 70, 71, # Reed
 
11090
  else:
11091
  return [ -1] * 6
11092
 
11093
+ ###################################################################################
11094
+
11095
+ def escore_notes_core(escore_notes, core_len=128):
11096
+
11097
+ cscore = chordify_score([1000, escore_notes])
11098
+
11099
+ chords = []
11100
+ chords_idxs = []
11101
+
11102
+ for i, c in enumerate(cscore):
11103
+
11104
+ pitches = [e[4] for e in c if e[3] != 9]
11105
+
11106
+ if pitches:
11107
+ tones_chord = sorted(set([p % 12 for p in pitches]))
11108
+
11109
+ if tones_chord not in ALL_CHORDS_SORTED:
11110
+ tones_chord = check_and_fix_tones_chord(tones_chord)
11111
+
11112
+ chords.append(ALL_CHORDS_SORTED.index(tones_chord))
11113
+ chords_idxs.append(i)
11114
+
11115
+ mid = len(chords_idxs) // 2
11116
+ clen = core_len // 2
11117
+
11118
+ sidx = chords_idxs[mid-clen]
11119
+ eidx = chords_idxs[mid+clen]
11120
+
11121
+ core_chords = chords[mid-clen:mid+clen]
11122
+ core_score = flatten(cscore[sidx:eidx])
11123
+
11124
+ return core_score, core_chords
11125
+
11126
+ ###################################################################################
11127
+
11128
+ def multiprocessing_wrapper(function, data_list):
11129
+
11130
+ with multiprocessing.Pool() as pool:
11131
+
11132
+ results = []
11133
+
11134
+ for result in tqdm.tqdm(pool.imap_unordered(function, data_list), total=len(data_list)):
11135
+ results.append(result)
11136
+
11137
+ return results
11138
+
11139
+ ###################################################################################
11140
+
11141
+ def rle_encode_ones(matrix, div_mod=-1):
11142
+
11143
+ flat_list = [val for row in matrix for val in row]
11144
+
11145
+ encoding = []
11146
+ i = 0
11147
+
11148
+ while i < len(flat_list):
11149
+
11150
+ if flat_list[i] == 1:
11151
+
11152
+ start_index = i
11153
+ count = 1
11154
+ i += 1
11155
+
11156
+ while i < len(flat_list) and flat_list[i] == 1:
11157
+ count += 1
11158
+ i += 1
11159
+
11160
+ if div_mod > 0:
11161
+ encoding.append((start_index // div_mod, start_index % div_mod))
11162
+
11163
+ else:
11164
+ encoding.append(start_index)
11165
+
11166
+ else:
11167
+ i += 1
11168
+
11169
+ return encoding
11170
+
11171
+ ###################################################################################
11172
+
11173
+ def rle_decode_ones(encoding, size=(128, 128)):
11174
+
11175
+ flat_list = [0] * (size[0] * size[1])
11176
+
11177
+ for start_index in encoding:
11178
+ flat_list[start_index] = 1
11179
+
11180
+ matrix = [flat_list[i * size[1]:(i + 1) * size[1]] for i in range(size[0])]
11181
+
11182
+ return matrix
11183
+
11184
  ###################################################################################
11185
  #
11186
  # This is the end of the TMIDI X Python module