Upload TMIDIX.py
Browse files
TMIDIX.py
CHANGED
|
@@ -51,7 +51,7 @@ r'''############################################################################
|
|
| 51 |
|
| 52 |
###################################################################################
|
| 53 |
|
| 54 |
-
__version__ = "25.4.
|
| 55 |
|
| 56 |
print('=' * 70)
|
| 57 |
print('TMIDIX Python module')
|
|
@@ -3874,7 +3874,7 @@ def fix_monophonic_score_durations(monophonic_score):
|
|
| 3874 |
nmt = monophonic_score[i+1][1]
|
| 3875 |
|
| 3876 |
if note[1]+note[2] >= nmt:
|
| 3877 |
-
note_dur = nmt-note[1]-1
|
| 3878 |
else:
|
| 3879 |
note_dur = note[2]
|
| 3880 |
|
|
@@ -3892,7 +3892,7 @@ def fix_monophonic_score_durations(monophonic_score):
|
|
| 3892 |
nmt = monophonic_score[i+1][0]
|
| 3893 |
|
| 3894 |
if note[0]+note[1] >= nmt:
|
| 3895 |
-
note_dur = nmt-note[0]-1
|
| 3896 |
else:
|
| 3897 |
note_dur = note[1]
|
| 3898 |
|
|
@@ -11338,13 +11338,27 @@ def system_memory_utilization(return_dict=False):
|
|
| 11338 |
|
| 11339 |
###################################################################################
|
| 11340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11341 |
def create_files_list(datasets_paths=['./'],
|
| 11342 |
files_exts=['.mid', '.midi', '.kar', '.MID', '.MIDI', '.KAR'],
|
| 11343 |
-
use_md5_hashes=False,
|
| 11344 |
max_num_files_per_dir=-1,
|
| 11345 |
randomize_dir_files=False,
|
| 11346 |
max_total_files=-1,
|
| 11347 |
randomize_files_list=True,
|
|
|
|
|
|
|
| 11348 |
return_dupes=False,
|
| 11349 |
verbose=True
|
| 11350 |
):
|
|
@@ -11360,8 +11374,13 @@ def create_files_list(datasets_paths=['./'],
|
|
| 11360 |
filez_set = defaultdict(None)
|
| 11361 |
dupes_list = []
|
| 11362 |
|
| 11363 |
-
for dataset_addr in
|
| 11364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11365 |
|
| 11366 |
if randomize_dir_files:
|
| 11367 |
random.shuffle(filenames)
|
|
@@ -11374,22 +11393,27 @@ def create_files_list(datasets_paths=['./'],
|
|
| 11374 |
|
| 11375 |
for file in filenames[:max_num_files]:
|
| 11376 |
if file.endswith(files_exts):
|
| 11377 |
-
if
|
| 11378 |
-
|
| 11379 |
-
|
| 11380 |
-
|
| 11381 |
-
|
| 11382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11383 |
else:
|
| 11384 |
-
|
|
|
|
| 11385 |
|
|
|
|
|
|
|
| 11386 |
else:
|
| 11387 |
-
|
| 11388 |
-
|
| 11389 |
-
|
| 11390 |
-
else:
|
| 11391 |
-
dupes_list.append(os.path.join(dirpath, file))
|
| 11392 |
-
|
| 11393 |
filez = list(filez_set.values())
|
| 11394 |
|
| 11395 |
if verbose:
|
|
@@ -12847,7 +12871,7 @@ def convert_escore_notes_pitches_chords_signature(signature, convert_to_full_cho
|
|
| 12847 |
|
| 12848 |
pitches_counts = [c for c in signature if -1 < c[0] < 128]
|
| 12849 |
chords_counts = [c for c in signature if 127 < c[0] < len(SRC_CHORDS)+128]
|
| 12850 |
-
drums_counts = [[c[0]
|
| 12851 |
bad_chords_count = [c for c in signature if c[0] == -1]
|
| 12852 |
|
| 12853 |
new_chords_counts = []
|
|
@@ -12865,6 +12889,157 @@ def convert_escore_notes_pitches_chords_signature(signature, convert_to_full_cho
|
|
| 12865 |
|
| 12866 |
###################################################################################
|
| 12867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12868 |
print('Module loaded!')
|
| 12869 |
print('=' * 70)
|
| 12870 |
print('Enjoy! :)')
|
|
|
|
| 51 |
|
| 52 |
###################################################################################
|
| 53 |
|
| 54 |
+
__version__ = "25.4.30"
|
| 55 |
|
| 56 |
print('=' * 70)
|
| 57 |
print('TMIDIX Python module')
|
|
|
|
| 3874 |
nmt = monophonic_score[i+1][1]
|
| 3875 |
|
| 3876 |
if note[1]+note[2] >= nmt:
|
| 3877 |
+
note_dur = max(1, nmt-note[1]-1)
|
| 3878 |
else:
|
| 3879 |
note_dur = note[2]
|
| 3880 |
|
|
|
|
| 3892 |
nmt = monophonic_score[i+1][0]
|
| 3893 |
|
| 3894 |
if note[0]+note[1] >= nmt:
|
| 3895 |
+
note_dur = max(1, nmt-note[0]-1)
|
| 3896 |
else:
|
| 3897 |
note_dur = note[1]
|
| 3898 |
|
|
|
|
| 11338 |
|
| 11339 |
###################################################################################
|
| 11340 |
|
| 11341 |
+
def system_cpus_utilization(return_dict=False):
|
| 11342 |
+
|
| 11343 |
+
if return_dict:
|
| 11344 |
+
return {'num_cpus': psutil.cpu_count(),
|
| 11345 |
+
'cpus_util': psutil.cpu_percent()
|
| 11346 |
+
}
|
| 11347 |
+
|
| 11348 |
+
else:
|
| 11349 |
+
print('Number of CPUs:', psutil.cpu_count())
|
| 11350 |
+
print('CPUs utilization:', psutil.cpu_percent())
|
| 11351 |
+
|
| 11352 |
+
###################################################################################
|
| 11353 |
+
|
| 11354 |
def create_files_list(datasets_paths=['./'],
|
| 11355 |
files_exts=['.mid', '.midi', '.kar', '.MID', '.MIDI', '.KAR'],
|
|
|
|
| 11356 |
max_num_files_per_dir=-1,
|
| 11357 |
randomize_dir_files=False,
|
| 11358 |
max_total_files=-1,
|
| 11359 |
randomize_files_list=True,
|
| 11360 |
+
check_for_dupes=False,
|
| 11361 |
+
use_md5_hashes=False,
|
| 11362 |
return_dupes=False,
|
| 11363 |
verbose=True
|
| 11364 |
):
|
|
|
|
| 11374 |
filez_set = defaultdict(None)
|
| 11375 |
dupes_list = []
|
| 11376 |
|
| 11377 |
+
for dataset_addr in datasets_paths:
|
| 11378 |
+
|
| 11379 |
+
print('=' * 70)
|
| 11380 |
+
print('Processing', dataset_addr)
|
| 11381 |
+
print('=' * 70)
|
| 11382 |
+
|
| 11383 |
+
for dirpath, dirnames, filenames in tqdm.tqdm(os.walk(dataset_addr), disable=not verbose):
|
| 11384 |
|
| 11385 |
if randomize_dir_files:
|
| 11386 |
random.shuffle(filenames)
|
|
|
|
| 11393 |
|
| 11394 |
for file in filenames[:max_num_files]:
|
| 11395 |
if file.endswith(files_exts):
|
| 11396 |
+
if check_for_dupes:
|
| 11397 |
+
|
| 11398 |
+
if use_md5_hashes:
|
| 11399 |
+
md5_hash = hashlib.md5(open(os.path.join(dirpath, file), 'rb').read()).hexdigest()
|
| 11400 |
+
|
| 11401 |
+
if md5_hash not in filez_set:
|
| 11402 |
+
filez_set[md5_hash] = os.path.join(dirpath, file)
|
| 11403 |
+
|
| 11404 |
+
else:
|
| 11405 |
+
dupes_list.append(os.path.join(dirpath, file))
|
| 11406 |
+
|
| 11407 |
else:
|
| 11408 |
+
if file not in filez_set:
|
| 11409 |
+
filez_set[file] = os.path.join(dirpath, file)
|
| 11410 |
|
| 11411 |
+
else:
|
| 11412 |
+
dupes_list.append(os.path.join(dirpath, file))
|
| 11413 |
else:
|
| 11414 |
+
fpath = os.path.join(dirpath, file)
|
| 11415 |
+
filez_set[fpath] = fpath
|
| 11416 |
+
|
|
|
|
|
|
|
|
|
|
| 11417 |
filez = list(filez_set.values())
|
| 11418 |
|
| 11419 |
if verbose:
|
|
|
|
| 12871 |
|
| 12872 |
pitches_counts = [c for c in signature if -1 < c[0] < 128]
|
| 12873 |
chords_counts = [c for c in signature if 127 < c[0] < len(SRC_CHORDS)+128]
|
| 12874 |
+
drums_counts = [[c[0]+cdiff, c[1]] for c in signature if len(SRC_CHORDS)+127 < c[0] < len(SRC_CHORDS)+256]
|
| 12875 |
bad_chords_count = [c for c in signature if c[0] == -1]
|
| 12876 |
|
| 12877 |
new_chords_counts = []
|
|
|
|
| 12889 |
|
| 12890 |
###################################################################################
|
| 12891 |
|
| 12892 |
+
def convert_bytes_in_nested_list(lst, encoding='utf-8', errors='ignore'):
|
| 12893 |
+
|
| 12894 |
+
new_list = []
|
| 12895 |
+
|
| 12896 |
+
for item in lst:
|
| 12897 |
+
if isinstance(item, list):
|
| 12898 |
+
new_list.append(convert_bytes_in_nested_list(item))
|
| 12899 |
+
|
| 12900 |
+
elif isinstance(item, bytes):
|
| 12901 |
+
new_list.append(item.decode(encoding, errors=errors))
|
| 12902 |
+
|
| 12903 |
+
else:
|
| 12904 |
+
new_list.append(item)
|
| 12905 |
+
|
| 12906 |
+
return new_list
|
| 12907 |
+
|
| 12908 |
+
###################################################################################
|
| 12909 |
+
|
| 12910 |
+
def mult_pitches(pitches, min_oct=4, max_oct=6):
|
| 12911 |
+
|
| 12912 |
+
tones_chord = sorted(set([p % 12 for p in pitches]))
|
| 12913 |
+
|
| 12914 |
+
mult_ptcs = []
|
| 12915 |
+
|
| 12916 |
+
for t in tones_chord:
|
| 12917 |
+
for i in range(min_oct, max_oct):
|
| 12918 |
+
mult_ptcs.append((i*12)+t)
|
| 12919 |
+
|
| 12920 |
+
return mult_ptcs
|
| 12921 |
+
|
| 12922 |
+
###################################################################################
|
| 12923 |
+
|
| 12924 |
+
def find_next(pitches, cur_ptc):
|
| 12925 |
+
|
| 12926 |
+
i = 0
|
| 12927 |
+
|
| 12928 |
+
for i, p in enumerate(pitches):
|
| 12929 |
+
if p != cur_ptc:
|
| 12930 |
+
break
|
| 12931 |
+
|
| 12932 |
+
return i
|
| 12933 |
+
|
| 12934 |
+
###################################################################################
|
| 12935 |
+
|
| 12936 |
+
def ordered_groups(data, key_index):
|
| 12937 |
+
|
| 12938 |
+
def keyfunc(sublist):
|
| 12939 |
+
return sublist[key_index]
|
| 12940 |
+
|
| 12941 |
+
groups = []
|
| 12942 |
+
|
| 12943 |
+
for key, group in groupby(data, key=keyfunc):
|
| 12944 |
+
groups.append((key, list(group)))
|
| 12945 |
+
|
| 12946 |
+
return groups
|
| 12947 |
+
|
| 12948 |
+
###################################################################################
|
| 12949 |
+
|
| 12950 |
+
def merge_melody_notes(escore_notes, pitches_idx=4, max_dur=255):
|
| 12951 |
+
|
| 12952 |
+
groups = ordered_groups(escore_notes, pitches_idx)
|
| 12953 |
+
|
| 12954 |
+
merged_melody_notes = []
|
| 12955 |
+
|
| 12956 |
+
for i, (k, g) in enumerate(groups[:-1]):
|
| 12957 |
+
|
| 12958 |
+
if len(g) == 1:
|
| 12959 |
+
merged_melody_notes.extend(g)
|
| 12960 |
+
|
| 12961 |
+
else:
|
| 12962 |
+
dur = min(max_dur, groups[i+1][1][0][1] - g[0][1])
|
| 12963 |
+
|
| 12964 |
+
merged_melody_notes.append(['note',
|
| 12965 |
+
g[0][1],
|
| 12966 |
+
dur,
|
| 12967 |
+
g[0][3],
|
| 12968 |
+
g[0][4],
|
| 12969 |
+
g[0][5],
|
| 12970 |
+
g[0][6]
|
| 12971 |
+
])
|
| 12972 |
+
|
| 12973 |
+
return merged_melody_notes
|
| 12974 |
+
|
| 12975 |
+
###################################################################################
|
| 12976 |
+
|
| 12977 |
+
def add_expressive_melody_to_enhanced_score_notes(escore_notes,
|
| 12978 |
+
melody_start_chord=0,
|
| 12979 |
+
melody_notes_max_duration=255,
|
| 12980 |
+
melody_channel=3,
|
| 12981 |
+
melody_patch=40,
|
| 12982 |
+
melody_max_velocity=120,
|
| 12983 |
+
acc_max_velocity=90,
|
| 12984 |
+
return_melody=False
|
| 12985 |
+
):
|
| 12986 |
+
|
| 12987 |
+
|
| 12988 |
+
score = copy.deepcopy(escore_notes)
|
| 12989 |
+
|
| 12990 |
+
adjust_score_velocities(score, acc_max_velocity)
|
| 12991 |
+
|
| 12992 |
+
cscore = chordify_score([1000, score])
|
| 12993 |
+
|
| 12994 |
+
melody_pitches = [72]
|
| 12995 |
+
|
| 12996 |
+
for i, c in enumerate(cscore[melody_start_chord:]):
|
| 12997 |
+
|
| 12998 |
+
pitches = [e[4] for e in c if e[3] != 9]
|
| 12999 |
+
|
| 13000 |
+
if pitches:
|
| 13001 |
+
cptc = find_closest_value(mult_pitches(pitches), melody_pitches[-1])[0]
|
| 13002 |
+
melody_pitches.append(cptc)
|
| 13003 |
+
|
| 13004 |
+
song_f = []
|
| 13005 |
+
mel_f = []
|
| 13006 |
+
|
| 13007 |
+
idx = 1
|
| 13008 |
+
|
| 13009 |
+
for i, c in enumerate(cscore[:-1]):
|
| 13010 |
+
pitches = [e[4] for e in c if e[3] != 9]
|
| 13011 |
+
|
| 13012 |
+
if pitches and i >= melody_start_chord:
|
| 13013 |
+
dur = min(cscore[i+1][0][1] - c[0][1], melody_notes_max_duration)
|
| 13014 |
+
|
| 13015 |
+
mel_f.append(['note',
|
| 13016 |
+
c[0][1],
|
| 13017 |
+
dur,
|
| 13018 |
+
melody_channel,
|
| 13019 |
+
60+(melody_pitches[idx] % 24),
|
| 13020 |
+
100 + ((melody_pitches[idx] % 12) * 2),
|
| 13021 |
+
melody_patch
|
| 13022 |
+
])
|
| 13023 |
+
idx += 1
|
| 13024 |
+
|
| 13025 |
+
song_f.extend(c)
|
| 13026 |
+
|
| 13027 |
+
song_f.extend(cscore[-1])
|
| 13028 |
+
|
| 13029 |
+
adjust_score_velocities(mel_f, melody_max_velocity)
|
| 13030 |
+
|
| 13031 |
+
song_f = sorted(merge_melody_notes(mel_f, max_dur=melody_notes_max_duration) + song_f,
|
| 13032 |
+
key=lambda x: x[1]
|
| 13033 |
+
)
|
| 13034 |
+
|
| 13035 |
+
if return_melody:
|
| 13036 |
+
return mel_f
|
| 13037 |
+
|
| 13038 |
+
else:
|
| 13039 |
+
return song_f
|
| 13040 |
+
|
| 13041 |
+
###################################################################################
|
| 13042 |
+
|
| 13043 |
print('Module loaded!')
|
| 13044 |
print('=' * 70)
|
| 13045 |
print('Enjoy! :)')
|