-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
94 lines (82 loc) · 2.53 KB
/
utils.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
"""
Created on Fri Oct 29 17:28:18 2022
@author: Shulei Ji
"""
import numpy as np
import torch
def calc_chords_val(chords_bar):
val = []
for c in chords_bar:
if 'maj7' in c: # maj 7th
val.append(0.83) # 2
elif 'm7' in c: # minor 7th
val.append(-0.46) # -1
elif '7' in c: # major 7th
val.append(-0.02) # 0
elif 'm9' in c: # minor 9th
val.append(-0.15) # 0
elif '9' in c: # major 9th
val.append(0.51) # 1
elif 'dim' in c: # diminished
val.append(-0.43) # -1
elif 'rest' in c: # Rest
val.append(0)
elif 'maj' in c: # Major
val.append(0.87) # -2
else: # Minor
val.append(-0.81) # 2
# get the median
med_val = np.mean(val)
# check the range
if med_val > 0.6:
val_idx = 2
elif med_val > 0.2 and med_val <= 0.6:
val_idx = 1
elif med_val > -0.2 and med_val <= 0.2:
val_idx = 0
elif med_val > -0.6 and med_val <= -0.2:
val_idx = -1
else:
val_idx = -2
return val_idx
def calc_piece_val(valenceSeq):
score = sum(valenceSeq) / len(valenceSeq)
if score >= 1.5:
cat = 2
elif score >= 0.5 and score < 1.5:
cat = 1
elif score >= -0.5 and score < 0.5:
cat = 0
elif score >= -1.5 and score < -0.5:
cat = -1
else:
cat = -2
return cat
def bar_padding(melody):
for i in range(len(melody)):
last = melody[i].index(0)
new_data_i = melody[i][:last]
for j in range(last + 1, len(melody[i])):
if melody[i][j] == 0:
new_data_i.extend(melody[i][last:j])
for k in range(42 - (j - last - 1)):
new_data_i.append(1)
last = j
new_data_i.extend(melody[i][last:])
for k in range(42 - (len(melody[i]) - last - 1)):
new_data_i.append(1)
melody[i]=new_data_i
return melody
def list2tensor(melody,chord,valence,device):
new_melody=[]
new_chord=[]
new_valence=[]
for i in range(len(melody)):
if i%12==0:
melody_temp = torch.LongTensor(melody[i:i + 12]).to(device)
chord_temp = torch.LongTensor(chord[i:i + 12]).to(device)
valence_temp = (torch.LongTensor(valence[i:i + 12]) + 2).to(device)
new_melody.append(melody_temp)
new_chord.append(chord_temp)
new_valence.append(valence_temp)
return new_melody,new_chord,new_valence