-
Notifications
You must be signed in to change notification settings - Fork 31
/
svs_song_pitch.py
142 lines (120 loc) · 4.62 KB
/
svs_song_pitch.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
import os
import numpy as np
from scipy.io import wavfile
from time import *
import torch
import argparse
from vits.models import SynthesizerTrn
from util import SingInput
from util import FeatureInput
from omegaconf import OmegaConf
from pitch.models import PitchDiffusion
from pitch.utils import fix_len_compatibility
def save_wav(wav, path, rate):
wav *= 32767 / max(0.01, np.max(np.abs(wav))) * 0.6
wavfile.write(path, rate, wav.astype(np.int16))
def load_svs_model(checkpoint_path, model):
assert os.path.isfile(checkpoint_path)
checkpoint_dict = torch.load(checkpoint_path, map_location="cpu")
saved_state_dict = checkpoint_dict["model_g"]
state_dict = model.state_dict()
new_state_dict = {}
for k, v in state_dict.items():
try:
new_state_dict[k] = saved_state_dict[k]
except:
print("%s is not in the checkpoint" % k)
new_state_dict[k] = v
model.load_state_dict(new_state_dict)
return model
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, required=True,
help="yaml file for configuration")
parser.add_argument('-m', '--model', type=str, required=True,
help="path of checkpoint pt file")
parser.add_argument('-p', '--pitch', type=str, required=True,
help="path of checkpoint pt file")
args = parser.parse_args()
# define model and load checkpoint
hps = OmegaConf.load(args.config)
singInput = SingInput(hps.data.sampling_rate, hps.data.hop_length)
featureInput = FeatureInput(hps.data.sampling_rate, hps.data.hop_length)
net_g = SynthesizerTrn(
hps.data.filter_length // 2 + 1,
hps.data.segment_size // hps.data.hop_length,
hps).cuda()
net_g.eval()
load_svs_model(args.model, net_g)
net_p = PitchDiffusion().cuda()
net_p.eval()
load_svs_model(args.pitch, net_p)
# check directory existence
os.makedirs("./svs_out", exist_ok=True)
fo = open("./svs_song.txt", "r+")
song_rate = hps.data.sampling_rate
song_time = fo.readline().strip().split("|")[1]
song_length = int(song_rate * (float(song_time) + 30))
song_data = np.zeros(song_length, dtype="float32")
while True:
try:
message = fo.readline().strip()
except Exception as e:
print("nothing of except:", e)
break
if message == None:
break
if message == "":
break
(
item_indx,
item_time,
labels_ids,
labels_frames,
scores_ids,
scores_dur,
labels_slr,
labels_uvs,
) = singInput.parseSong(message)
labels_ids = singInput.expandInput(labels_ids, labels_frames)
labels_uvs = singInput.expandInput(labels_uvs, labels_frames)
labels_slr = singInput.expandInput(labels_slr, labels_frames)
scores_ids = singInput.expandInput(scores_ids, labels_frames)
phone = torch.LongTensor(labels_ids)
score = torch.LongTensor(scores_ids)
slurs = torch.LongTensor(labels_slr)
lengths = phone.size()[0]
lengths_fix = fix_len_compatibility(lengths)
phone_fix = torch.zeros((1, lengths_fix), dtype=torch.long).cuda()
score_fix = torch.zeros((1, lengths_fix), dtype=torch.long).cuda()
slurs_fix = torch.zeros((1, lengths_fix), dtype=torch.long).cuda()
phone_fix[0, :lengths] = phone
score_fix[0, :lengths] = score
slurs_fix[0, :lengths] = slurs
begin_time = time()
with torch.no_grad():
n_timesteps = 50
temperature = 1
# PIT
phone_lengths = torch.LongTensor([lengths_fix]).cuda()
pit_pri, pit_pre = net_p(phone_fix, phone_lengths, score_fix, slurs_fix, n_timesteps, temperature)
pitch = pit_pre[:, 0, :]
pitch = 2**pitch
print('~~~~~~~')
audio = (
net_g.infer(phone_fix, phone_lengths, score_fix, pitch, slurs_fix)[0, 0]
.data.cpu()
.float()
.numpy()
)
save_wav(audio, f"./svs_out/{item_indx}.wav", hps.data.sampling_rate)
# wav
item_start = int(song_rate * float(item_time))
item_end = item_start + len(audio)
song_data[item_start:item_end] = audio
# out of for
song_data = np.array(song_data, dtype="float32")
save_wav(song_data, f"./svs_out/_song.wav", hps.data.sampling_rate)
fo.close()
# can be deleted
os.system("chmod 777 ./svs_out -R")