Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
prophesier
GitHub Repository: prophesier/diff-svc
Path: blob/main/trans_key.py
446 views
1
head_list = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
2
3
4
def trans_f0_seq(feature_pit, transform):
5
feature_pit = feature_pit * 2 ** (transform / 12)
6
return round(feature_pit, 1)
7
8
9
def move_key(raw_data, mv_key):
10
head = raw_data[:-1]
11
body = int(raw_data[-1])
12
new_head_index = head_list.index(head) + mv_key
13
while new_head_index < 0:
14
body -= 1
15
new_head_index += 12
16
while new_head_index > 11:
17
body += 1
18
new_head_index -= 12
19
result_data = head_list[new_head_index] + str(body)
20
return result_data
21
22
23
def trans_key(raw_data, key):
24
for i in raw_data:
25
note_seq_list = i["note_seq"].split(" ")
26
new_note_seq_list = []
27
for note_seq in note_seq_list:
28
if note_seq != "rest":
29
new_note_seq = move_key(note_seq, key)
30
new_note_seq_list.append(new_note_seq)
31
else:
32
new_note_seq_list.append(note_seq)
33
i["note_seq"] = " ".join(new_note_seq_list)
34
35
f0_seq_list = i["f0_seq"].split(" ")
36
f0_seq_list = [float(x) for x in f0_seq_list]
37
new_f0_seq_list = []
38
for f0_seq in f0_seq_list:
39
new_f0_seq = trans_f0_seq(f0_seq, key)
40
new_f0_seq_list.append(str(new_f0_seq))
41
i["f0_seq"] = " ".join(new_f0_seq_list)
42
return raw_data
43
44
45
key = -6
46
f_w = open("raw.txt", "w", encoding='utf-8')
47
with open("result.txt", "r", encoding='utf-8') as f:
48
raw_data = f.readlines()
49
for raw in raw_data:
50
raw_list = raw.split("|")
51
new_note_seq_list = []
52
for note_seq in raw_list[3].split(" "):
53
if note_seq != "rest":
54
note_seq = note_seq.split("/")[0] if "/" in note_seq else note_seq
55
new_note_seq = move_key(note_seq, key)
56
new_note_seq_list.append(new_note_seq)
57
else:
58
new_note_seq_list.append(note_seq)
59
raw_list[3] = " ".join(new_note_seq_list)
60
f_w.write("|".join(raw_list))
61
f_w.close()
62
63