Upload TMIDIX.py
Browse files
TMIDIX.py
CHANGED
|
@@ -51,7 +51,7 @@ r'''############################################################################
|
|
| 51 |
|
| 52 |
###################################################################################
|
| 53 |
|
| 54 |
-
__version__ = "25.
|
| 55 |
|
| 56 |
print('=' * 70)
|
| 57 |
print('TMIDIX Python module')
|
|
@@ -1486,6 +1486,7 @@ import multiprocessing
|
|
| 1486 |
from itertools import zip_longest
|
| 1487 |
from itertools import groupby
|
| 1488 |
from itertools import cycle
|
|
|
|
| 1489 |
|
| 1490 |
from collections import Counter
|
| 1491 |
from collections import defaultdict
|
|
@@ -3907,8 +3908,9 @@ def chordify_score(score,
|
|
| 3907 |
return None
|
| 3908 |
|
| 3909 |
def fix_monophonic_score_durations(monophonic_score,
|
| 3910 |
-
min_notes_gap=
|
| 3911 |
-
min_notes_dur=1
|
|
|
|
| 3912 |
):
|
| 3913 |
|
| 3914 |
fixed_score = []
|
|
@@ -3923,7 +3925,11 @@ def fix_monophonic_score_durations(monophonic_score,
|
|
| 3923 |
if note[1]+note[2] >= nmt:
|
| 3924 |
note_dur = max(1, nmt-note[1]-min_notes_gap)
|
| 3925 |
else:
|
| 3926 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3927 |
|
| 3928 |
new_note = [note[0], note[1], note_dur] + note[3:]
|
| 3929 |
|
|
@@ -3941,9 +3947,13 @@ def fix_monophonic_score_durations(monophonic_score,
|
|
| 3941 |
nmt = monophonic_score[i+1][0]
|
| 3942 |
|
| 3943 |
if note[0]+note[1] >= nmt:
|
| 3944 |
-
|
| 3945 |
else:
|
| 3946 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3947 |
|
| 3948 |
new_note = [note[0], note_dur] + note[2:]
|
| 3949 |
|
|
@@ -3957,8 +3967,6 @@ def fix_monophonic_score_durations(monophonic_score,
|
|
| 3957 |
|
| 3958 |
###################################################################################
|
| 3959 |
|
| 3960 |
-
from itertools import product
|
| 3961 |
-
|
| 3962 |
ALL_CHORDS = [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1], [0, 9], [2, 5],
|
| 3963 |
[4, 7], [7, 10], [2, 11], [0, 3], [6, 9], [1, 4], [8, 11], [5, 8], [1, 10],
|
| 3964 |
[3, 6], [0, 4], [5, 9], [7, 11], [0, 7], [0, 5], [2, 10], [2, 7], [2, 9],
|
|
@@ -14716,6 +14724,214 @@ def int_to_tones_chord(integer,
|
|
| 14716 |
|
| 14717 |
###################################################################################
|
| 14718 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14719 |
print('Module loaded!')
|
| 14720 |
print('=' * 70)
|
| 14721 |
print('Enjoy! :)')
|
|
|
|
| 51 |
|
| 52 |
###################################################################################
|
| 53 |
|
| 54 |
+
__version__ = "25.9.19"
|
| 55 |
|
| 56 |
print('=' * 70)
|
| 57 |
print('TMIDIX Python module')
|
|
|
|
| 1486 |
from itertools import zip_longest
|
| 1487 |
from itertools import groupby
|
| 1488 |
from itertools import cycle
|
| 1489 |
+
from itertools import product
|
| 1490 |
|
| 1491 |
from collections import Counter
|
| 1492 |
from collections import defaultdict
|
|
|
|
| 3908 |
return None
|
| 3909 |
|
| 3910 |
def fix_monophonic_score_durations(monophonic_score,
|
| 3911 |
+
min_notes_gap=0,
|
| 3912 |
+
min_notes_dur=1,
|
| 3913 |
+
extend_durs=False
|
| 3914 |
):
|
| 3915 |
|
| 3916 |
fixed_score = []
|
|
|
|
| 3925 |
if note[1]+note[2] >= nmt:
|
| 3926 |
note_dur = max(1, nmt-note[1]-min_notes_gap)
|
| 3927 |
else:
|
| 3928 |
+
if extend_durs:
|
| 3929 |
+
note_dur = max(1, nmt-note[1]-min_notes_gap)
|
| 3930 |
+
|
| 3931 |
+
else:
|
| 3932 |
+
note_dur = note[2]
|
| 3933 |
|
| 3934 |
new_note = [note[0], note[1], note_dur] + note[3:]
|
| 3935 |
|
|
|
|
| 3947 |
nmt = monophonic_score[i+1][0]
|
| 3948 |
|
| 3949 |
if note[0]+note[1] >= nmt:
|
| 3950 |
+
note_dur = max(1, nmt-note[0]-min_notes_gap)
|
| 3951 |
else:
|
| 3952 |
+
if extend_durs:
|
| 3953 |
+
note_dur = max(1, nmt-note[0]-min_notes_gap)
|
| 3954 |
+
|
| 3955 |
+
else:
|
| 3956 |
+
note_dur = note[1]
|
| 3957 |
|
| 3958 |
new_note = [note[0], note_dur] + note[2:]
|
| 3959 |
|
|
|
|
| 3967 |
|
| 3968 |
###################################################################################
|
| 3969 |
|
|
|
|
|
|
|
| 3970 |
ALL_CHORDS = [[0], [7], [5], [9], [2], [4], [11], [10], [8], [6], [3], [1], [0, 9], [2, 5],
|
| 3971 |
[4, 7], [7, 10], [2, 11], [0, 3], [6, 9], [1, 4], [8, 11], [5, 8], [1, 10],
|
| 3972 |
[3, 6], [0, 4], [5, 9], [7, 11], [0, 7], [0, 5], [2, 10], [2, 7], [2, 9],
|
|
|
|
| 14724 |
|
| 14725 |
###################################################################################
|
| 14726 |
|
| 14727 |
+
def fix_bad_chords_in_escore_notes(escore_notes,
|
| 14728 |
+
use_full_chords=False,
|
| 14729 |
+
return_bad_chords_count=False
|
| 14730 |
+
):
|
| 14731 |
+
|
| 14732 |
+
if use_full_chords:
|
| 14733 |
+
CHORDS = ALL_CHORDS_FULL
|
| 14734 |
+
|
| 14735 |
+
else:
|
| 14736 |
+
CHORDS = ALL_CHORDS_SORTED
|
| 14737 |
+
|
| 14738 |
+
bcount = 0
|
| 14739 |
+
|
| 14740 |
+
if escore_notes:
|
| 14741 |
+
|
| 14742 |
+
chords = chordify_score([1000, escore_notes])
|
| 14743 |
+
|
| 14744 |
+
fixed_chords = []
|
| 14745 |
+
|
| 14746 |
+
for c in chords:
|
| 14747 |
+
c.sort(key=lambda x: x[3])
|
| 14748 |
+
|
| 14749 |
+
if len(c) > 1:
|
| 14750 |
+
|
| 14751 |
+
groups = groupby(c, key=lambda x: x[3])
|
| 14752 |
+
|
| 14753 |
+
for cha, gr in groups:
|
| 14754 |
+
|
| 14755 |
+
if cha != 9:
|
| 14756 |
+
|
| 14757 |
+
gr = list(gr)
|
| 14758 |
+
|
| 14759 |
+
tones_chord = sorted(set([p[4] % 12 for p in gr]))
|
| 14760 |
+
|
| 14761 |
+
if tones_chord not in CHORDS:
|
| 14762 |
+
tones_chord = check_and_fix_tones_chord(tones_chord,
|
| 14763 |
+
use_full_chords=use_full_chords
|
| 14764 |
+
)
|
| 14765 |
+
|
| 14766 |
+
bcount += 1
|
| 14767 |
+
|
| 14768 |
+
ngr = []
|
| 14769 |
+
|
| 14770 |
+
for n in gr:
|
| 14771 |
+
if n[4] % 12 in tones_chord:
|
| 14772 |
+
ngr.append(n)
|
| 14773 |
+
|
| 14774 |
+
fixed_chords.extend(ngr)
|
| 14775 |
+
|
| 14776 |
+
else:
|
| 14777 |
+
fixed_chords.extend(gr)
|
| 14778 |
+
|
| 14779 |
+
|
| 14780 |
+
else:
|
| 14781 |
+
fixed_chords.extend(c)
|
| 14782 |
+
|
| 14783 |
+
fixed_chords.sort(key=lambda x: (x[1], -x[4]))
|
| 14784 |
+
|
| 14785 |
+
if return_bad_chords_count:
|
| 14786 |
+
return fixed_chords, bcount
|
| 14787 |
+
|
| 14788 |
+
else:
|
| 14789 |
+
return fixed_chords
|
| 14790 |
+
|
| 14791 |
+
else:
|
| 14792 |
+
if return_bad_chords_count:
|
| 14793 |
+
return escore_notes, bcount
|
| 14794 |
+
|
| 14795 |
+
else:
|
| 14796 |
+
return escore_notes
|
| 14797 |
+
|
| 14798 |
+
###################################################################################
|
| 14799 |
+
|
| 14800 |
+
def remove_events_from_escore_notes(escore_notes,
|
| 14801 |
+
ele_idx=2,
|
| 14802 |
+
ele_vals=[1],
|
| 14803 |
+
chan_idx=3,
|
| 14804 |
+
skip_drums=True
|
| 14805 |
+
):
|
| 14806 |
+
|
| 14807 |
+
new_escore_notes = []
|
| 14808 |
+
|
| 14809 |
+
for e in escore_notes:
|
| 14810 |
+
if skip_drums:
|
| 14811 |
+
if e[ele_idx] not in ele_vals or e[chan_idx] == 9:
|
| 14812 |
+
new_escore_notes.append(e)
|
| 14813 |
+
|
| 14814 |
+
else:
|
| 14815 |
+
if e[ele_idx] not in ele_vals:
|
| 14816 |
+
new_escore_notes.append(e)
|
| 14817 |
+
|
| 14818 |
+
return new_escore_notes
|
| 14819 |
+
|
| 14820 |
+
###################################################################################
|
| 14821 |
+
|
| 14822 |
+
def flatten_spikes(arr):
|
| 14823 |
+
|
| 14824 |
+
if len(arr) < 3:
|
| 14825 |
+
return arr[:]
|
| 14826 |
+
|
| 14827 |
+
result = arr[:]
|
| 14828 |
+
|
| 14829 |
+
for i in range(1, len(arr) - 1):
|
| 14830 |
+
prev, curr, next_ = arr[i - 1], arr[i], arr[i + 1]
|
| 14831 |
+
|
| 14832 |
+
if (prev <= next_ and (curr > prev and curr > next_)) or \
|
| 14833 |
+
(prev >= next_ and (curr < prev and curr < next_)):
|
| 14834 |
+
result[i] = max(min(prev, next_), min(max(prev, next_), curr))
|
| 14835 |
+
|
| 14836 |
+
return result
|
| 14837 |
+
|
| 14838 |
+
###################################################################################
|
| 14839 |
+
|
| 14840 |
+
def flatten_spikes_advanced(arr, window=1):
|
| 14841 |
+
|
| 14842 |
+
if len(arr) < 3:
|
| 14843 |
+
return arr[:]
|
| 14844 |
+
|
| 14845 |
+
result = arr[:]
|
| 14846 |
+
n = len(arr)
|
| 14847 |
+
|
| 14848 |
+
def is_spike(i):
|
| 14849 |
+
left = arr[i - window:i]
|
| 14850 |
+
right = arr[i + 1:i + 1 + window]
|
| 14851 |
+
|
| 14852 |
+
if not left or not right:
|
| 14853 |
+
return False
|
| 14854 |
+
|
| 14855 |
+
avg_left = sum(left) / len(left)
|
| 14856 |
+
avg_right = sum(right) / len(right)
|
| 14857 |
+
|
| 14858 |
+
if arr[i] > avg_left and arr[i] > avg_right:
|
| 14859 |
+
return True
|
| 14860 |
+
|
| 14861 |
+
if arr[i] < avg_left and arr[i] < avg_right:
|
| 14862 |
+
return True
|
| 14863 |
+
|
| 14864 |
+
return False
|
| 14865 |
+
|
| 14866 |
+
for i in range(window, n - window):
|
| 14867 |
+
if is_spike(i):
|
| 14868 |
+
neighbors = arr[i - window:i] + arr[i + 1:i + 1 + window]
|
| 14869 |
+
result[i] = int(sorted(neighbors)[len(neighbors) // 2])
|
| 14870 |
+
|
| 14871 |
+
return result
|
| 14872 |
+
|
| 14873 |
+
###################################################################################
|
| 14874 |
+
|
| 14875 |
+
def add_smooth_melody_to_enhanced_score_notes(escore_notes,
|
| 14876 |
+
melody_channel=3,
|
| 14877 |
+
min_notes_gap=0,
|
| 14878 |
+
exclude_durs=[1, 2],
|
| 14879 |
+
adv_flattening=True,
|
| 14880 |
+
extend_durs=False,
|
| 14881 |
+
max_mel_vels=127,
|
| 14882 |
+
max_acc_vels=80,
|
| 14883 |
+
return_melody=False
|
| 14884 |
+
):
|
| 14885 |
+
|
| 14886 |
+
escore_notes1 = remove_duplicate_pitches_from_escore_notes(escore_notes)
|
| 14887 |
+
|
| 14888 |
+
escore_notes2 = fix_escore_notes_durations(escore_notes1,
|
| 14889 |
+
min_notes_gap=min_notes_gap
|
| 14890 |
+
)
|
| 14891 |
+
|
| 14892 |
+
escore_notes3 = fix_bad_chords_in_escore_notes(escore_notes2)
|
| 14893 |
+
|
| 14894 |
+
escore_notes4 = remove_events_from_escore_notes(escore_notes3,
|
| 14895 |
+
ele_vals=exclude_durs
|
| 14896 |
+
)
|
| 14897 |
+
|
| 14898 |
+
escore_notes5 = add_expressive_melody_to_enhanced_score_notes(escore_notes4,
|
| 14899 |
+
return_melody=True,
|
| 14900 |
+
melody_channel=melody_channel
|
| 14901 |
+
)
|
| 14902 |
+
|
| 14903 |
+
mel_score = remove_events_from_escore_notes(escore_notes5,
|
| 14904 |
+
ele_vals=exclude_durs
|
| 14905 |
+
)
|
| 14906 |
+
|
| 14907 |
+
pitches = [p[4] for p in mel_score]
|
| 14908 |
+
|
| 14909 |
+
if adv_flattening:
|
| 14910 |
+
res = flatten_spikes_advanced(pitches)
|
| 14911 |
+
|
| 14912 |
+
else:
|
| 14913 |
+
res = flatten_spikes(pitches)
|
| 14914 |
+
|
| 14915 |
+
mel_score3 = copy.deepcopy(mel_score)
|
| 14916 |
+
|
| 14917 |
+
for i, e in enumerate(mel_score3):
|
| 14918 |
+
e[4] = res[i]
|
| 14919 |
+
|
| 14920 |
+
mel_score3 = fix_monophonic_score_durations(merge_melody_notes(mel_score3),
|
| 14921 |
+
extend_durs=extend_durs
|
| 14922 |
+
)
|
| 14923 |
+
|
| 14924 |
+
adjust_score_velocities(mel_score3, max_mel_vels)
|
| 14925 |
+
adjust_score_velocities(escore_notes4, max_acc_vels)
|
| 14926 |
+
|
| 14927 |
+
if return_melody:
|
| 14928 |
+
return sorted(mel_score3, key=lambda x: (x[1], -x[4]))
|
| 14929 |
+
|
| 14930 |
+
else:
|
| 14931 |
+
return sorted(mel_score3 + escore_notes4, key=lambda x: (x[1], -x[4]))
|
| 14932 |
+
|
| 14933 |
+
###################################################################################
|
| 14934 |
+
|
| 14935 |
print('Module loaded!')
|
| 14936 |
print('=' * 70)
|
| 14937 |
print('Enjoy! :)')
|