-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_make_dynamics_split.py
207 lines (168 loc) · 7.34 KB
/
midi_make_dynamics_split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import util as um
import musicnoteconv as mnc
from itertools import combinations
import csv
import random
import os
import polars as pl
dyn_ltr = ["pp", "p", "mp", "mf", "f", "ff"]
# (127 - 22)/(num - 1)
dyn_num = [22, 43, 64, 85, 106, 127]
dyn = {x:y for (x,y) in zip(dyn_ltr, dyn_num)}
# note: doesn't go all the way to end_dyn (one short)
def make_dynamic_ramp(start_num, end_num, num_notes):
return [int(start_num + i*((end_num - start_num)/num_notes)) for i in range(num_notes)]
# flat = stay at first dynamic of pair
# hairpin = go from dyn1 to dyn2 to dyn1 smoothly over (1/2 + 1/2)
#revhairpin = go from dyn2 to dyn1 to dyn2 smoothyl over (1/2 + 1/2)
# cresc = go from dyn1 to dyn2 smoothly over 1
# decresc = go from dyn2 to dyn1 smoothyl over 1
# subf = go from dyn1 to dyn1 (1/2 at dyn1, 1/2 at dyn2)
# subp = go from dyn2 to dyn1 (1/2 at dyn2, 1/2 at dyn1)
# dyn1 < dyn2
def get_velocities(dyn1, dyn2, num_notes, dyn_type="flat"):
start_num = dyn[dyn1]
end_num = dyn[dyn2]
# num_notes should be even...
half_num = int(num_notes/2)
ret = []
if dyn_type == "flat":
ret = [start_num] * num_notes
elif dyn_type == "hairpin":
ret1 = make_dynamic_ramp(start_num, end_num, half_num)
ret2 = make_dynamic_ramp(end_num, start_num, half_num)
ret = ret1 + ret2
elif dyn_type == "revhairpin":
ret1 = make_dynamic_ramp(end_num, start_num, half_num)
ret2 = make_dynamic_ramp(start_num, end_num, half_num)
ret = ret1 + ret2
elif dyn_type == "cresc":
ret = make_dynamic_ramp(start_num, end_num, num_notes)
elif dyn_type == "decresc":
ret = make_dynamic_ramp(end_num, start_num, num_notes)
elif dyn_type == "subf":
ret1 = [start_num] * half_num
ret2 = [end_num] * half_num
ret = ret1 + ret2
elif dyn_type == "subp":
ret1 = [end_num] * half_num
ret2 = [start_num] * half_num
ret = ret1 + ret2
return ret
midinote = 60 # midi note to use
ticks_per_beat = 1000
cur_bpm = 120
end_padding = 10
tempo_microsec = mido.bpm2tempo(cur_bpm)
sustain = 1.0
beg_padding = 0
num_bars = 2
instruments = ['Tinkle Bell','Agogo','Steel Drums','Woodblock','Taiko Drum','Melodic Tom','Synth Drum']
#instruments = ['Agogo','Woodblock']
# always goes from soft to loud
#dyn_to_use = dyn_ltr
dyn_to_use = ["p", "ff"]
dyn_pair = [x for x in combinations(dyn_to_use, 2)]
# total number of notes = dur * subdiv * num_bars
dur_subdiv = [(4,2),(4,3)]
dyn_type = ["flat", "hairpin", "revhairpin", "cresc", "decresc", "subf", "subp"]
data_profile = True
data_csv = True
data_png = True
out_name = 'dyn_split'
out_csv = f'{out_name}.csv'
out_dir = "csv"
dp_dir = 'dataprof'
inst_pairs_train = 15
opath = os.path.join(out_dir, out_csv)
seed = 5
midinote = 60 # midi note to use
ticks_per_beat = 1000
velocity = 127
end_padding = 10
sustain = 1.0
dur = 1
subdiv = 1
num_trks = 2
beg_padding = 0
do_reverse = True
instruments = ['Tinkle Bell','Agogo','Steel Drums','Woodblock','Taiko Drum','Melodic Tom','Synth Drum']
#instruments = ['Agogo','Woodblock']
inst_combos = [x for x in combinations(instruments, 2)]
um.shuffle_list(inst_combos)
poly_pairs = [(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(3,5),(5,7)]
train_poly = set([(3,5), (3,4), (4,5), (5,6), (6,7)])
valtest_poly = set([(2,3), (7,8), (5,7)])
bpm_bars = [(120, 2), (180, 3)]
runs = 2 if do_reverse == True else 1
if os.path.exists(out_dir) == False:
os.makedirs(out_dir)
fieldnames = ['name','inst', 'num_bars', 'dyntype', 'set']
bar_charts = ['bpm', 'poly', 'pair', 'set']
with open(opath, 'w') as csvf:
csvw = csv.DictWriter(csvf,fieldnames=fieldnames)
csvw.writeheader()
for _ds in dur_subdiv:
dur = _ds[0]
subdiv = _ds[1]
notes_per_bar = dur * subdiv
total_notes = notes_per_bar * num_bars
for _dt in dyn_type:
for _dp in dyn_pair:
dyn1 = _dp[0]
dyn2 = _dp[1]
_v = get_velocities(dyn1, dyn2, total_notes, dyn_type=_dt)
for _inst in instruments:
ch_num = um.get_inst_program_number(_inst)
short_name = ''.join(_inst.split(' '))
mid = None
mid = mido.MidiFile(type=1, ticks_per_beat=ticks_per_beat)
mid.tracks.append(mido.MidiTrack())
mid.tracks[0].append(mido.MetaMessage('set_tempo', tempo = tempo_microsec, time = 0))
mid.tracks[0].append(mido.Message('program_change', program=ch_num, time =0, channel=0))
d_on, d_off = um.notedur_to_ticks(dur, subdiv = subdiv, ticks_per_beat = ticks_per_beat, sustain = sustain)
dynstr = f"{_dt}-{dyn1}_{dyn2}"
durstr = f"{dur}_{subdiv}"
outname = f"{short_name}-{dynstr}-{durstr}.mid"
#print(outname)
# keep track of ticks left from last bar
last_ticks_left = 0
for curbar in range(num_bars):
# keep track of ticks left over
ticks_left = ticks_per_beat * 4 # 4/4 bar
for notenum in range(notes_per_bar):
start_time = d_off
realnum = notenum + (curbar * notes_per_bar)
curvel = _v[realnum]
if notenum == 0:
start_time = 0
mid.tracks[0].append(mido.Message('note_on', note=midinote, velocity=curvel, time=start_time + last_ticks_left, channel=0))
ticks_left -= start_time
last_ticks_left = 0
end_time = d_on
mid.tracks[0].append(mido.Message('note_off', note=midinote, velocity=curvel, time=end_time, channel=0))
ticks_left -= end_time
if notenum == (notes_per_bar) - 1:
last_ticks_left = max(0,ticks_left)
if end_padding > 0 or last_ticks_left > 0:
if end_padding > 0:
# only delay note_on by last ticks left, delay note_off by end_padding
# so total is last ticks left + end_padding
mid.tracks[0].append(mido.Message('note_on', note=midinote, velocity=0, time=last_ticks_left, channel=0))
mid.tracks[0].append(mido.Message('note_off', note=midinote, velocity=0, time=end_padding, channel=0))
else:
# don't delay note on, delay note off by last ticks left
mid.tracks[0].append(mido.Message('note_on', note=midinote, velocity=0, time=0,channel=0))
mid.tracks[0].append(mido.Message('note_off', note=midinote, velocity=0, time=end_padding, channel=0))
mid.tracks[0].append(mido.MetaMessage('end_of_track', time=0))
um.save_midi(mid, outname)
if data_profile == True:
data = pl.scan_csv(opath).collect()
for cat in bar_charts:
um.profile_category(data, cat, ds=out_name, profile_type='overall')
set_types = data['set'].value_counts()['set']
for set_type in set_types:
curset = data.filter(pl.col('set') == set_type)
for cat in bar_charts:
if cat != 'set':
um.profile_category(curset, cat, ds=out_name, profile_type=set_type, save_csv=data_csv, save_png = data_png)