Upload TMIDIX.py
Browse files50 unmodified lines515253545556573816 unmodified lines387438753876387738783879388011 unmodified lines38923893389438953896389738987439 unmodified lines113381133911340113411134211343113441134511346113471134811349113509 unmodified lines11360113611136211363113641136511366113676 unmodified lines113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951451 unmodified lines1284712848128491285012851128521285311 unmodified lines12865128661286712868128691287050 unmodified lines###################################################################################__version__ = "25.4.11"print('=' * 70)print('TMIDIX Python module')3816 unmodified linesnmt = monophonic_score[i+1][1]if note[1]+note[2] >= nmt:note_dur = nmt-note[1]-1else:note_dur = note[2]11 unmodified linesnmt = monophonic_score[i+1][0]if note[0]+note[1] >= nmt:note_dur = nmt-note[0]-1else:note_dur = note[1]7439 unmodified lines###################################################################################def create_files_list(datasets_paths=['./'],files_exts=['.mid', '.midi', '.kar', '.MID', '.MIDI', '.KAR'],use_md5_hashes=False,max_num_files_per_dir=-1,randomize_dir_files=False,max_total_files=-1,randomize_files_list=True,return_dupes=False,verbose=True):9 unmodified linesfilez_set = defaultdict(None)dupes_list = []for dataset_addr in tqdm.tqdm(datasets_paths, disable=not verbose):for dirpath, dirnames, filenames in os.walk(dataset_addr):if randomize_dir_files:random.shuffle(filenames)6 unmodified linesfor file in filenames[:max_num_files]:if file.endswith(files_exts):if use_md5_hashes:md5_hash = hashlib.md5(open(os.path.join(dirpath, file), 'rb').read()).hexdigest()if md5_hash not in filez_set:filez_set[md5_hash] = os.path.join(dirpath, file)else:dupes_list.append(os.path.join(dirpath, file))else:if file not in filez_set:filez_set[file] = os.path.join(dirpath, file)else:dupes_list.append(os.path.join(dirpath, file))filez = list(filez_set.values())if verbose:1451 unmodified linespitches_counts = [c for c in signature if -1 < c[0] < 128]chords_counts = [c for c in signature if 127 < c[0] < len(SRC_CHORDS)+128]drums_counts = [[c[0]-cdiff, c[1]] for c in signature if len(SRC_CHORDS)+127 < c[0] < len(SRC_CHORDS)+256]bad_chords_count = [c for c in signature if c[0] == -1]new_chords_counts = []11 unmodified lines###################################################################################print('Module loaded!')print('=' * 70)print('Enjoy! :)')50 unmodified lines515253545556573816 unmodified lines387438753876387738783879388011 unmodified lines38923893389438953896389738987439 unmodified lines1133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113649 unmodified lines113741137511376113771137811379113801138111382113831138411385113866 unmodified lines1139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191451 unmodified lines1287112872128731287412875128761287711 unmodified lines1288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304550 unmodified lines###################################################################################__version__ = "25.4.30"print('=' * 70)print('TMIDIX Python module')3816 unmodified linesnmt = monophonic_score[i+1][1]if note[1]+note[2] >= nmt:note_dur = max(1, nmt-note[1]-1)else:note_dur = note[2]11 unmodified linesnmt = monophonic_score[i+1][0]if note[0]+note[1] >= nmt:note_dur = max(1, nmt-note[0]-1)else:note_dur = note[1]7439 unmodified lines###################################################################################def system_cpus_utilization(return_dict=False):if return_dict:return {'num_cpus': psutil.cpu_count(),'cpus_util': psutil.cpu_percent()}else:print('Number of CPUs:', psutil.cpu_count())print('CPUs utilization:', psutil.cpu_percent())###################################################################################def create_files_list(datasets_paths=['./'],files_exts=['.mid', '.midi', '.kar', '.MID', '.MIDI', '.KAR'],max_num_files_per_dir=-1,randomize_dir_files=False,max_total_files=-1,randomize_files_list=True,check_for_dupes=False,use_md5_hashes=False,return_dupes=False,verbose=True):9 unmodified linesfilez_set = defaultdict(None)dupes_list = []for dataset_addr in datasets_paths:print('=' * 70)print('Processing', dataset_addr)print('=' * 70)for dirpath, dirnames, filenames in tqdm.tqdm(os.walk(dataset_addr), disable=not verbose):if randomize_dir_files:random.shuffle(filenames)6 unmodified linesfor file in filenames[:max_num_files]:if file.endswith(files_exts):if check_for_dupes:if use_md5_hashes:md5_hash = hashlib.md5(open(os.path.join(dirpath, file), 'rb').read()).hexdigest()if md5_hash not in filez_set:filez_set[md5_hash] = os.path.join(dirpath, file)else:dupes_list.append(os.path.join(dirpath, file))else:if file not in filez_set:filez_set[file] = os.path.join(dirpath, file)else:dupes_list.append(os.path.join(dirpath, file))else:fpath = os.path.join(dirpath, file)filez_set[fpath] = fpathfilez = list(filez_set.values())if verbose:1451 unmodified linespitches_counts = [c for c in signature if -1 < c[0] < 128]chords_counts = [c for c in signature if 127 < c[0] < len(SRC_CHORDS)+128]drums_counts = [[c[0]+cdiff, c[1]] for c in signature if len(SRC_CHORDS)+127 < c[0] < len(SRC_CHORDS)+256]bad_chords_count = [c for c in signature if c[0] == -1]new_chords_counts = []11 unmodified lines###################################################################################def convert_bytes_in_nested_list(lst, encoding='utf-8', errors='ignore'):new_list = []for item in lst:if isinstance(item, list):new_list.append(convert_bytes_in_nested_list(item))elif isinstance(item, bytes):new_list.append(item.decode(encoding, errors=errors))else:new_list.append(item)return new_list###################################################################################def mult_pitches(pitches, min_oct=4, max_oct=6):tones_chord = sorted(set([p % 12 for p in pitches]))mult_ptcs = []for t in tones_chord:for i in range(min_oct, max_oct):mult_ptcs.append((i*12)+t)return mult_ptcs###################################################################################def find_next(pitches, cur_ptc):i = 0for i, p in enumerate(pitches):if p != cur_ptc:breakreturn i###################################################################################def ordered_groups(data, key_index):def keyfunc(sublist):return sublist[key_index]groups = []for key, group in groupby(data, key=keyfunc):groups.append((key, list(group)))return groups###################################################################################def merge_melody_notes(escore_notes, pitches_idx=4, max_dur=255):groups = ordered_groups(escore_notes, pitches_idx)merged_melody_notes = []for i, (k, g) in enumerate(groups[:-1]):if len(g) == 1:merged_melody_notes.extend(g)else:dur = min(max_dur, groups[i+1][1][0][1] - g[0][1])merged_melody_notes.append(['note',g[0][1],dur,g[0][3],g[0][4],g[0][5],g[0][6]])return merged_melody_notes###################################################################################def add_expressive_melody_to_enhanced_score_notes(escore_notes,melody_start_chord=0,melody_notes_max_duration=255,melody_channel=3,melody_patch=40,melody_max_velocity=120,acc_max_velocity=90,return_melody=False):score = copy.deepcopy(escore_notes)adjust_score_velocities(score, acc_max_velocity)cscore = chordify_score([1000, score])melody_pitches = [72]for i, c in enumerate(cscore[melody_start_chord:]):pitches = [e[4] for e in c if e[3] != 9]if pitches:cptc = find_closest_value(mult_pitches(pitches), melody_pitches[-1])[0]melody_pitches.append(cptc)song_f = []mel_f = []idx = 1for i, c in enumerate(cscore[:-1]):pitches = [e[4] for e in c if e[3] != 9]if pitches and i >= melody_start_chord:dur = min(cscore[i+1][0][1] - c[0][1], melody_notes_max_duration)mel_f.append(['note',c[0][1],dur,melody_channel,60+(melody_pitches[idx] % 24),100 + ((melody_pitches[idx] % 12) * 2),melody_patch])idx += 1song_f.extend(c)song_f.extend(cscore[-1])adjust_score_velocities(mel_f, melody_max_velocity)song_f = sorted(merge_melody_notes(mel_f, max_dur=melody_notes_max_duration) + song_f,key=lambda x: x[1])if return_melody:return mel_felse:return song_f###################################################################################print('Module loaded!')print('=' * 70)print('Enjoy! :)')
TMIDIX.py+195-20