asigalov61 commited on
Commit
3dead54
·
verified ·
1 Parent(s): 00b8e11

Upload 2 files

Browse files
Files changed (1) hide show
  1. TMIDIX.py +98 -1
TMIDIX.py CHANGED
@@ -51,7 +51,7 @@ r'''############################################################################
51
 
52
  ###################################################################################
53
 
54
- __version__ = "26.3.28"
55
 
56
  print('=' * 70)
57
  print('TMIDIX Python module')
@@ -18224,6 +18224,103 @@ def decode_bpe_corpus(encoded_corpus: Iterable[List[int]], id_to_token: Dict[int
18224
 
18225
  ###################################################################################
18226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18227
  print('Module loaded!')
18228
  print('=' * 70)
18229
  print('Enjoy! :)')
 
51
 
52
  ###################################################################################
53
 
54
+ __version__ = "26.4.13"
55
 
56
  print('=' * 70)
57
  print('TMIDIX Python module')
 
18224
 
18225
  ###################################################################################
18226
 
18227
+ closest_avg_val = lambda lst: (lambda m: min(lst, key=lambda x: abs(x - m)))(sum(lst)/len(lst))
18228
+
18229
+ ###################################################################################
18230
+
18231
+ def binary_matrix_to_rle_toks(binary_matrix,
18232
+ marker_sidx=256,
18233
+ marker_clip_val=127,
18234
+ encode_vels=True,
18235
+ vels_sidx=128
18236
+ ):
18237
+
18238
+ pmat = binary_matrix[0]
18239
+
18240
+ mcount = 0
18241
+
18242
+ seq = []
18243
+
18244
+ for mat in binary_matrix:
18245
+ if pmat != mat:
18246
+ seq.append(min(marker_clip_val, mcount)+marker_sidx)
18247
+ if all(t == 0 for t in pmat):
18248
+ seq.append(0)
18249
+
18250
+ if encode_vels:
18251
+ seq.append(0+vels_sidx)
18252
+
18253
+ else:
18254
+ if any(t > 1 for t in pmat):
18255
+ seq.extend(TMIDIX.flatten(zip([i for i in range(128) if pmat[i] != 0], [pmat[i]+vels_sidx for i in range(128) if pmat[i] != 0])))
18256
+
18257
+ else:
18258
+ seq.extend([i for i in range(128) if pmat[i] != 0])
18259
+
18260
+ mcount = 1
18261
+
18262
+ else:
18263
+ mcount += 1
18264
+
18265
+ pmat = mat
18266
+
18267
+ seq.append(min(marker_clip_val, mcount)+marker_sidx)
18268
+ if all(t == 0 for t in pmat):
18269
+ seq.append(0)
18270
+
18271
+ if encode_vels:
18272
+ seq.append(0+vels_sidx)
18273
+
18274
+ else:
18275
+ if any(t > 1 for t in pmat):
18276
+ seq.extend(TMIDIX.flatten(zip([i for i in range(128) if pmat[i] != 0], [pmat[i]+vels_sidx for i in range(128) if pmat[i] > 1 ])))
18277
+
18278
+ else:
18279
+ seq.extend([i for i in range(128) if pmat[i] != 0])
18280
+
18281
+ return seq
18282
+
18283
+ ###################################################################################
18284
+
18285
+ def rle_toks_to_binary_matrix(rle_toks,
18286
+ marker_sidx=256,
18287
+ vels_sidx=128
18288
+ ):
18289
+
18290
+ rows = []
18291
+ row = []
18292
+
18293
+ for t in rle_toks:
18294
+ if t >= marker_sidx:
18295
+ if row:
18296
+ rows.append(row)
18297
+
18298
+ row = [t]
18299
+
18300
+ else:
18301
+ row.append(t)
18302
+
18303
+ if row:
18304
+ rows.append(row)
18305
+
18306
+ matrix = []
18307
+
18308
+
18309
+ for row in rows:
18310
+ mat = [0] * 128
18311
+ marker = row[0]-marker_sidx
18312
+
18313
+ for p, v in [row[i:i+2] for i in range(1, len(row)-1, 2)]:
18314
+
18315
+ if p != 0 and v-vels_sidx != 0:
18316
+ mat[p] = v-vels_sidx
18317
+
18318
+ matrix.extend([mat] * marker)
18319
+
18320
+ return matrix
18321
+
18322
+ ###################################################################################
18323
+
18324
  print('Module loaded!')
18325
  print('=' * 70)
18326
  print('Enjoy! :)')