-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.py
328 lines (290 loc) · 9.73 KB
/
helpers.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Helper functions to create melodies and music21 score objects
"""
import os
from fractions import Fraction
from itertools import product
import pandas as pd
from tqdm import tqdm
from typing import Union
import music21
from music21 import note
from constants_file_names import *
from constants_latent_factors import *
SLUR_SYMBOL = '__'
TICK_VALUES = [
0,
Fraction(1, 2),
]
def create_latent_info_df() -> pd.DataFrame:
"""
Creates and returns the data-frame object containing the latent factors information
Returns:
pandas data-frame object
"""
tonic_list = []
octave_list = []
scale_list = []
rhy1_list = []
rhy2_list = []
dir1_list = []
dir2_list = []
dir3_list = []
dir4_list = []
all_combinations = product(
TONIC_DICT.keys(),
OCTAVE_DICT.keys(),
SCALE_DICT.keys(),
RHYTHM_DICT.keys(),
RHYTHM_DICT.keys(),
ARP_DICT.keys(),
ARP_DICT.keys(),
ARP_DICT.keys(),
ARP_DICT.keys()
)
for params in tqdm(all_combinations):
tonic_list.append(TONIC_DICT[params[0]])
octave_list.append(OCTAVE_DICT[params[1]])
scale_list.append(SCALE_DICT[params[2]])
rhy1_list.append(params[3])
rhy2_list.append(params[4])
dir1_list.append(ARP_DICT[params[5]])
dir2_list.append(ARP_DICT[params[6]])
dir3_list.append(ARP_DICT[params[7]])
dir4_list.append(ARP_DICT[params[8]])
d = {
'tonic': tonic_list,
'octave': octave_list,
'scale': scale_list,
'rhythm_bar1': rhy1_list,
'rhythm_bar2': rhy2_list,
'arp_chord1': dir1_list,
'arp_chord2': dir2_list,
'arp_chord3': dir3_list,
'arp_chord4': dir4_list
}
latent_df = pd.DataFrame(data=d)
return latent_df
def get_latent_info() -> pd.DataFrame:
"""
Reads the latent factors info from stored LATENT_INFO_CSV (see constants_file_names.py) file.
If file doesn't exist, creates and saves it
Returns:
pandas data-frame object
"""
cur_dir = os.path.dirname(os.path.realpath(__file__))
latent_info_path = os.path.join(cur_dir, LATENT_INFO_CSV)
if os.path.exists(latent_info_path):
latent_df = pd.read_csv(latent_info_path, index_col=0)
else:
latent_df = create_latent_info_df()
latent_df.to_csv(path_or_buf=latent_info_path)
return latent_df
def get_midi_pitch_list(
tonic: str,
octave: int,
mode: str,
arp_dir1: str,
arp_dir2: str,
arp_dir3: str,
arp_dir4: str
) -> list:
"""
Create the sequence of midi pitch values. Refer constants_latent_factors.py for details regarding allowed arg values
Args:
tonic: str, specifies the pitch class of the root note (C, C#, ...., through B)
octave: int, specifies of the octave number (4, 5, 6) of the root note
mode: str, specifies the scale (major, minor, blues etc.)
arp_dir1: str, 'up' or 'down', specifies the arpeggiation direction of Chord 1
arp_dir2: str, 'up' or 'down', specifies the arpeggiation direction of Chord 2
arp_dir3: str, 'up' or 'down', specifies the arpeggiation direction of Chord 3
arp_dir4: str, 'up' or 'down', specifies the arpeggiation direction of Chord 4
Returns:
list of MIDI notes corresponding to the melody defined based on the input arguments
"""
root_pitch = music21.pitch.Pitch(tonic + str(octave)).midi
pitch_seq = []
dir_seq = [arp_dir1, arp_dir2, arp_dir3, arp_dir4]
for index, chord in enumerate(CHORD_DICT.keys()):
seq = CHORD_DICT[chord]
if dir_seq[index] == 'down':
seq = seq[::-1]
for s in seq:
midi_pitch = root_pitch + SCALE_NOTES_DICT[mode][s]
pitch_seq.append(midi_pitch)
return pitch_seq
def create_m21_melody(
tonic: str,
octave: int,
mode: str,
rhythm_bar1: int,
rhythm_bar2: int,
arp_dir1: str,
arp_dir2: str,
arp_dir3: str,
arp_dir4: str
) -> music21.stream.Score:
"""
Creates the 2-bar melody in music21 score format
Args:
tonic: str, specifies the pitch class of the root note (C, C#, ...., through B)
octave: int, specifies of the octave number (4, 5, 6) of the root note
mode: str, specifies the scale (major, minor, blues etc.)
rhythm_bar1: int, specified the rhythm for Bar 1
rhythm_bar2: int, specified the rhythm for Bar 2
arp_dir1: str, 'up' or 'down', specifies the arpergiation direction of Chord 1
arp_dir2: str, 'up' or 'down', specifies the arpeggiation direction of Chord 2
arp_dir3: str, 'up' or 'down', specifies the arpeggiation direction of Chord 3
arp_dir4: str, 'up' or 'down', specifies the arpeggiation direction of Chord 4
Returns:
music21 score object containing the score
"""
score = music21.stream.Score()
part = music21.stream.Part()
dur = 0.0
rhy1 = RHYTHM_DICT[rhythm_bar1]
rhy2 = RHYTHM_DICT[rhythm_bar2]
if sum(rhy1) != 6:
raise(ValueError, f'Invalid rhythm: {rhy1}')
if sum(rhy2) != 6:
raise(ValueError, f'Invalid rhythm: {rhy2}')
midi_pitch_seq = get_midi_pitch_list(tonic, octave, mode, arp_dir1, arp_dir2, arp_dir3, arp_dir4)
curr_note_num = 0
for rhy in [rhy1, rhy2]:
for onset in rhy:
if onset == 1:
f = music21.note.Note()
f.pitch.midi = midi_pitch_seq[curr_note_num]
f.duration = music21.duration.Duration('eighth')
curr_note_num += 1
else:
f = music21.note.Rest()
f.duration = music21.duration.Duration('eighth')
part.insert(dur, f)
dur += music21.duration.Duration('eighth').quarterLength
score.insert(part)
return score
def get_score_for_item(df_row: pd.Series) -> music21.stream.Score:
"""
Returns the score for the index given a data-frame
Args:
df_row: data-frame row containing latent attribute values
Returns:
music21.stream.Score object
"""
return create_m21_melody(
tonic=df_row['tonic'],
octave=df_row['octave'],
mode=df_row['scale'],
rhythm_bar1=df_row['rhythm_bar1'],
rhythm_bar2=df_row['rhythm_bar2'],
arp_dir1=df_row['arp_chord1'],
arp_dir2=df_row['arp_chord2'],
arp_dir3=df_row['arp_chord3'],
arp_dir4=df_row['arp_chord4']
)
def get_file_name_for_item(df_row: pd.Series, index: int) -> str:
"""
Return the file name for index
Args:
df_row: data-frame row containing latent attribute values
index: int, of the item in the dataset
Returns:
str,
"""
tonic = df_row['tonic']
octave = df_row['octave']
mode = df_row['scale']
rhythm_bar1 = df_row['rhythm_bar1']
rhythm_bar2 = df_row['rhythm_bar2']
dir1 = df_row['arp_chord1']
dir2 = df_row['arp_chord2']
dir3 = df_row['arp_chord3']
dir4 = df_row['arp_chord4']
file_name = f'{index}_{tonic}_{octave}_{mode}_{rhythm_bar1}_{rhythm_bar2}_{dir1}_{dir2}_{dir3}_{dir4}'
return file_name
def compute_tick_durations(tick_values: list):
"""
Computes the tick durations
Args:
tick_values: list of allowed tick values
"""
diff = [n - p
for n, p in zip(tick_values[1:], tick_values[:-1])]
diff = diff + [1 - tick_values[-1]]
return diff
def get_notes(score: music21.stream.Score) -> list:
"""
Returns the notes from the music21 score object
Args:
score: music21 score object
Returns:
list, of music21 note objects
"""
notes = score.parts[0].flat.notesAndRests
notes = [n for n in notes if not isinstance(n, music21.harmony.ChordSymbol)]
return notes
def is_score_on_ticks(score: music21.stream.Score, tick_values: list) -> bool:
"""
Checks if the notes in a score are on ticks
Args:
score: music21 score object
tick_values: list of allowed tick values
"""
notes = get_notes(score)
eps = 1e-5
for n in notes:
_, d = divmod(n.offset, 1)
flag = False
for tick_value in tick_values:
if tick_value - eps < d < tick_value + eps:
flag = True
if not flag:
return False
return True
def standard_name(note_or_rest: Union[music21.note.Note, music21.note.Rest]) -> str:
"""
Converts music21 note objects to string
Args:
note_or_rest: music21 note.Note or note.Rest object
Returns:
str,
"""
if isinstance(note_or_rest, music21.note.Note):
return note_or_rest.nameWithOctave
elif isinstance(note_or_rest, music21.note.Rest):
return note_or_rest.name
else:
raise ValueError("Invalid input. Should be a music21.note.Note or music21.note.Rest object ")
def standard_note(note_or_rest_string: str) -> Union[music21.note.Note, music21.note.Rest]:
"""
Converts str to music21 note.Note or note.Rest object
Args:
note_or_rest_string:
Returns:
music21 note.Note or note.Rest object
"""
if note_or_rest_string == 'rest':
return note.Rest()
elif note_or_rest_string == SLUR_SYMBOL:
return note.Rest()
else:
return note.Note(note_or_rest_string)
def concatenate_scores(scores_list):
"""
Each score must 2 bars long
:param scores_list:
:return:
"""
score = music21.stream.Score()
part = music21.stream.Part()
dur = 0.0
for s in scores_list:
notes = get_notes(s)
note_dur = 0
for n in notes:
part.insert(dur + note_dur, n)
note_dur += n.duration.quarterLength
dur += 8.0
score.insert(part)
return score