Upload TMIDIX.py
Browse files1470 unmodified lines1471147214731474147514764112 unmodified lines55895590559155925593559455954007 unmodified lines96039604960596069607960896091478 unmodified lines1108811089110901109111092110931470 unmodified linesimport tqdmfrom itertools import zip_longestfrom itertools import groupbyfrom collections import Counter4112 unmodified linesfor e in enhanced_melody_score_notes:dtime = max(0, min(max_score_time, e[1]-pe[1]))if dtime > max(durs):if chu:mel_chunks.append(chu)chu = []4007 unmodified linesall_patches = [e[6] for e in escore_notes]patches = ordered_set(all_patches)instruments = [alpha_str(Number2patch[p]) for p in patches if p < 128]1478 unmodified lineselse:return [ -1] * 6##################################################################################### This is the end of the TMIDI X Python module1470 unmodified lines147114721473147414751476147714784112 unmodified lines55915592559355945595559655974007 unmodified lines96059606960796089609961096111478 unmodified lines110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861470 unmodified linesimport tqdmimport multiprocessingfrom itertools import zip_longestfrom itertools import groupbyfrom collections import Counter4112 unmodified linesfor e in enhanced_melody_score_notes:dtime = max(0, min(max_score_time, e[1]-pe[1]))if dtime > stime:if chu:mel_chunks.append(chu)chu = []4007 unmodified linesall_patches = [e[6] for e in escore_notes]patches = ordered_set(all_patches)[:16]instruments = [alpha_str(Number2patch[p]) for p in patches if p < 128]1478 unmodified lineselse:return [ -1] * 6###################################################################################def escore_notes_core(escore_notes, core_len=128):cscore = chordify_score([1000, escore_notes])chords = []chords_idxs = []for i, c in enumerate(cscore):pitches = [e[4] for e in c if e[3] != 9]if pitches:tones_chord = sorted(set([p % 12 for p in pitches]))if tones_chord not in ALL_CHORDS_SORTED:tones_chord = check_and_fix_tones_chord(tones_chord)chords.append(ALL_CHORDS_SORTED.index(tones_chord))chords_idxs.append(i)mid = len(chords_idxs) // 2clen = core_len // 2sidx = chords_idxs[mid-clen]eidx = chords_idxs[mid+clen]core_chords = chords[mid-clen:mid+clen]core_score = flatten(cscore[sidx:eidx])return core_score, core_chords###################################################################################def multiprocessing_wrapper(function, data_list):with multiprocessing.Pool() as pool:results = []for result in tqdm.tqdm(pool.imap_unordered(function, data_list), total=len(data_list)):results.append(result)return results###################################################################################def rle_encode_ones(matrix, div_mod=-1):flat_list = [val for row in matrix for val in row]encoding = []i = 0while i < len(flat_list):if flat_list[i] == 1:start_index = icount = 1i += 1while i < len(flat_list) and flat_list[i] == 1:count += 1i += 1if div_mod > 0:encoding.append((start_index // div_mod, start_index % div_mod))else:encoding.append(start_index)else:i += 1return encoding###################################################################################def rle_decode_ones(encoding, size=(128, 128)):flat_list = [0] * (size[0] * size[1])for start_index in encoding:flat_list[start_index] = 1matrix = [flat_list[i * size[1]:(i + 1) * size[1]] for i in range(size[0])]return matrix##################################################################################### This is the end of the TMIDI X Python module
TMIDIX.py+95-2