Spaces:
Running
Running
kevinwang676
commited on
Commit
•
9c0a6bd
1
Parent(s):
4554aec
Update vc_infer_pipeline.py
Browse files- vc_infer_pipeline.py +425 -363
vc_infer_pipeline.py
CHANGED
@@ -1,363 +1,425 @@
|
|
1 |
-
import numpy as np, parselmouth, torch, pdb
|
2 |
-
from time import time as ttime
|
3 |
-
import torch.nn.functional as F
|
4 |
-
import scipy.signal as signal
|
5 |
-
import pyworld, os, traceback, faiss,librosa
|
6 |
-
from scipy import signal
|
7 |
-
from functools import lru_cache
|
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 |
-
with
|
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 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np, parselmouth, torch, pdb, sys, os
|
2 |
+
from time import time as ttime
|
3 |
+
import torch.nn.functional as F
|
4 |
+
import scipy.signal as signal
|
5 |
+
import pyworld, os, traceback, faiss, librosa, torchcrepe
|
6 |
+
from scipy import signal
|
7 |
+
from functools import lru_cache
|
8 |
+
|
9 |
+
now_dir = os.getcwd()
|
10 |
+
sys.path.append(now_dir)
|
11 |
+
|
12 |
+
bh, ah = signal.butter(N=5, Wn=48, btype="high", fs=16000)
|
13 |
+
|
14 |
+
input_audio_path2wav = {}
|
15 |
+
|
16 |
+
|
17 |
+
@lru_cache
|
18 |
+
def cache_harvest_f0(input_audio_path, fs, f0max, f0min, frame_period):
|
19 |
+
audio = input_audio_path2wav[input_audio_path]
|
20 |
+
f0, t = pyworld.harvest(
|
21 |
+
audio,
|
22 |
+
fs=fs,
|
23 |
+
f0_ceil=f0max,
|
24 |
+
f0_floor=f0min,
|
25 |
+
frame_period=frame_period,
|
26 |
+
)
|
27 |
+
f0 = pyworld.stonemask(audio, f0, t, fs)
|
28 |
+
return f0
|
29 |
+
|
30 |
+
|
31 |
+
def change_rms(data1, sr1, data2, sr2, rate): # 1是输入音频,2是输出音频,rate是2的占比
|
32 |
+
# print(data1.max(),data2.max())
|
33 |
+
rms1 = librosa.feature.rms(
|
34 |
+
y=data1, frame_length=sr1 // 2 * 2, hop_length=sr1 // 2
|
35 |
+
) # 每半秒一个点
|
36 |
+
rms2 = librosa.feature.rms(y=data2, frame_length=sr2 // 2 * 2, hop_length=sr2 // 2)
|
37 |
+
rms1 = torch.from_numpy(rms1)
|
38 |
+
rms1 = F.interpolate(
|
39 |
+
rms1.unsqueeze(0), size=data2.shape[0], mode="linear"
|
40 |
+
).squeeze()
|
41 |
+
rms2 = torch.from_numpy(rms2)
|
42 |
+
rms2 = F.interpolate(
|
43 |
+
rms2.unsqueeze(0), size=data2.shape[0], mode="linear"
|
44 |
+
).squeeze()
|
45 |
+
rms2 = torch.max(rms2, torch.zeros_like(rms2) + 1e-6)
|
46 |
+
data2 *= (
|
47 |
+
torch.pow(rms1, torch.tensor(1 - rate))
|
48 |
+
* torch.pow(rms2, torch.tensor(rate - 1))
|
49 |
+
).numpy()
|
50 |
+
return data2
|
51 |
+
|
52 |
+
|
53 |
+
class VC(object):
|
54 |
+
def __init__(self, tgt_sr, config):
|
55 |
+
self.x_pad, self.x_query, self.x_center, self.x_max, self.is_half = (
|
56 |
+
config.x_pad,
|
57 |
+
config.x_query,
|
58 |
+
config.x_center,
|
59 |
+
config.x_max,
|
60 |
+
config.is_half,
|
61 |
+
)
|
62 |
+
self.sr = 16000 # hubert输入采样率
|
63 |
+
self.window = 160 # 每帧点数
|
64 |
+
self.t_pad = self.sr * self.x_pad # 每条前后pad时间
|
65 |
+
self.t_pad_tgt = tgt_sr * self.x_pad
|
66 |
+
self.t_pad2 = self.t_pad * 2
|
67 |
+
self.t_query = self.sr * self.x_query # 查询切点前后查询时间
|
68 |
+
self.t_center = self.sr * self.x_center # 查询切点位置
|
69 |
+
self.t_max = self.sr * self.x_max # 免查询时长阈值
|
70 |
+
self.device = config.device
|
71 |
+
|
72 |
+
def get_f0(
|
73 |
+
self,
|
74 |
+
input_audio_path,
|
75 |
+
x,
|
76 |
+
p_len,
|
77 |
+
f0_up_key,
|
78 |
+
f0_method,
|
79 |
+
filter_radius,
|
80 |
+
inp_f0=None,
|
81 |
+
):
|
82 |
+
global input_audio_path2wav
|
83 |
+
time_step = self.window / self.sr * 1000
|
84 |
+
f0_min = 50
|
85 |
+
f0_max = 1100
|
86 |
+
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
|
87 |
+
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
|
88 |
+
if f0_method == "pm":
|
89 |
+
f0 = (
|
90 |
+
parselmouth.Sound(x, self.sr)
|
91 |
+
.to_pitch_ac(
|
92 |
+
time_step=time_step / 1000,
|
93 |
+
voicing_threshold=0.6,
|
94 |
+
pitch_floor=f0_min,
|
95 |
+
pitch_ceiling=f0_max,
|
96 |
+
)
|
97 |
+
.selected_array["frequency"]
|
98 |
+
)
|
99 |
+
pad_size = (p_len - len(f0) + 1) // 2
|
100 |
+
if pad_size > 0 or p_len - len(f0) - pad_size > 0:
|
101 |
+
f0 = np.pad(
|
102 |
+
f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant"
|
103 |
+
)
|
104 |
+
elif f0_method == "harvest":
|
105 |
+
input_audio_path2wav[input_audio_path] = x.astype(np.double)
|
106 |
+
f0 = cache_harvest_f0(input_audio_path, self.sr, f0_max, f0_min, 10)
|
107 |
+
if filter_radius > 2:
|
108 |
+
f0 = signal.medfilt(f0, 3)
|
109 |
+
elif f0_method == "crepe":
|
110 |
+
model = "full"
|
111 |
+
# Pick a batch size that doesn't cause memory errors on your gpu
|
112 |
+
batch_size = 512
|
113 |
+
# Compute pitch using first gpu
|
114 |
+
audio = torch.tensor(np.copy(x))[None].float()
|
115 |
+
f0, pd = torchcrepe.predict(
|
116 |
+
audio,
|
117 |
+
self.sr,
|
118 |
+
self.window,
|
119 |
+
f0_min,
|
120 |
+
f0_max,
|
121 |
+
model,
|
122 |
+
batch_size=batch_size,
|
123 |
+
device=self.device,
|
124 |
+
return_periodicity=True,
|
125 |
+
)
|
126 |
+
pd = torchcrepe.filter.median(pd, 3)
|
127 |
+
f0 = torchcrepe.filter.mean(f0, 3)
|
128 |
+
f0[pd < 0.1] = 0
|
129 |
+
f0 = f0[0].cpu().numpy()
|
130 |
+
elif f0_method == "rmvpe":
|
131 |
+
if hasattr(self, "model_rmvpe") == False:
|
132 |
+
from rmvpe import RMVPE
|
133 |
+
|
134 |
+
print("loading rmvpe model")
|
135 |
+
self.model_rmvpe = RMVPE(
|
136 |
+
"rmvpe.pt", is_half=self.is_half, device=self.device
|
137 |
+
)
|
138 |
+
f0 = self.model_rmvpe.infer_from_audio(x, thred=0.03)
|
139 |
+
f0 *= pow(2, f0_up_key / 12)
|
140 |
+
# with open("test.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
141 |
+
tf0 = self.sr // self.window # 每秒f0点数
|
142 |
+
if inp_f0 is not None:
|
143 |
+
delta_t = np.round(
|
144 |
+
(inp_f0[:, 0].max() - inp_f0[:, 0].min()) * tf0 + 1
|
145 |
+
).astype("int16")
|
146 |
+
replace_f0 = np.interp(
|
147 |
+
list(range(delta_t)), inp_f0[:, 0] * 100, inp_f0[:, 1]
|
148 |
+
)
|
149 |
+
shape = f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)].shape[0]
|
150 |
+
f0[self.x_pad * tf0 : self.x_pad * tf0 + len(replace_f0)] = replace_f0[
|
151 |
+
:shape
|
152 |
+
]
|
153 |
+
# with open("test_opt.txt","w")as f:f.write("\n".join([str(i)for i in f0.tolist()]))
|
154 |
+
f0bak = f0.copy()
|
155 |
+
f0_mel = 1127 * np.log(1 + f0 / 700)
|
156 |
+
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
157 |
+
f0_mel_max - f0_mel_min
|
158 |
+
) + 1
|
159 |
+
f0_mel[f0_mel <= 1] = 1
|
160 |
+
f0_mel[f0_mel > 255] = 255
|
161 |
+
f0_coarse = np.rint(f0_mel).astype(np.int)
|
162 |
+
return f0_coarse, f0bak # 1-0
|
163 |
+
|
164 |
+
def vc(
|
165 |
+
self,
|
166 |
+
model,
|
167 |
+
net_g,
|
168 |
+
sid,
|
169 |
+
audio0,
|
170 |
+
pitch,
|
171 |
+
pitchf,
|
172 |
+
times,
|
173 |
+
index,
|
174 |
+
big_npy,
|
175 |
+
index_rate,
|
176 |
+
version,
|
177 |
+
): # ,file_index,file_big_npy
|
178 |
+
feats = torch.from_numpy(audio0)
|
179 |
+
if self.is_half:
|
180 |
+
feats = feats.half()
|
181 |
+
else:
|
182 |
+
feats = feats.float()
|
183 |
+
if feats.dim() == 2: # double channels
|
184 |
+
feats = feats.mean(-1)
|
185 |
+
assert feats.dim() == 1, feats.dim()
|
186 |
+
feats = feats.view(1, -1)
|
187 |
+
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
|
188 |
+
|
189 |
+
inputs = {
|
190 |
+
"source": feats.to(self.device),
|
191 |
+
"padding_mask": padding_mask,
|
192 |
+
"output_layer": 9 if version == "v1" else 12,
|
193 |
+
}
|
194 |
+
t0 = ttime()
|
195 |
+
with torch.no_grad():
|
196 |
+
logits = model.extract_features(**inputs)
|
197 |
+
feats = model.final_proj(logits[0]) if version == "v1" else logits[0]
|
198 |
+
if (
|
199 |
+
isinstance(index, type(None)) == False
|
200 |
+
and isinstance(big_npy, type(None)) == False
|
201 |
+
and index_rate != 0
|
202 |
+
):
|
203 |
+
npy = feats[0].cpu().numpy()
|
204 |
+
if self.is_half:
|
205 |
+
npy = npy.astype("float32")
|
206 |
+
|
207 |
+
# _, I = index.search(npy, 1)
|
208 |
+
# npy = big_npy[I.squeeze()]
|
209 |
+
|
210 |
+
score, ix = index.search(npy, k=8)
|
211 |
+
weight = np.square(1 / score)
|
212 |
+
weight /= weight.sum(axis=1, keepdims=True)
|
213 |
+
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
|
214 |
+
|
215 |
+
if self.is_half:
|
216 |
+
npy = npy.astype("float16")
|
217 |
+
feats = (
|
218 |
+
torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate
|
219 |
+
+ (1 - index_rate) * feats
|
220 |
+
)
|
221 |
+
|
222 |
+
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
|
223 |
+
|
224 |
+
t1 = ttime()
|
225 |
+
p_len = audio0.shape[0] // self.window
|
226 |
+
if feats.shape[1] < p_len:
|
227 |
+
p_len = feats.shape[1]
|
228 |
+
if pitch != None and pitchf != None:
|
229 |
+
pitch = pitch[:, :p_len]
|
230 |
+
pitchf = pitchf[:, :p_len]
|
231 |
+
|
232 |
+
p_len = torch.tensor([p_len], device=self.device).long()
|
233 |
+
with torch.no_grad():
|
234 |
+
if pitch != None and pitchf != None:
|
235 |
+
audio1 = (
|
236 |
+
(net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0])
|
237 |
+
.data.cpu()
|
238 |
+
.float()
|
239 |
+
.numpy()
|
240 |
+
)
|
241 |
+
else:
|
242 |
+
audio1 = (
|
243 |
+
(net_g.infer(feats, p_len, sid)[0][0, 0]).data.cpu().float().numpy()
|
244 |
+
)
|
245 |
+
del feats, p_len, padding_mask
|
246 |
+
if torch.cuda.is_available():
|
247 |
+
torch.cuda.empty_cache()
|
248 |
+
t2 = ttime()
|
249 |
+
times[0] += t1 - t0
|
250 |
+
times[2] += t2 - t1
|
251 |
+
return audio1
|
252 |
+
|
253 |
+
def pipeline(
|
254 |
+
self,
|
255 |
+
model,
|
256 |
+
net_g,
|
257 |
+
sid,
|
258 |
+
audio,
|
259 |
+
input_audio_path,
|
260 |
+
times,
|
261 |
+
f0_up_key,
|
262 |
+
f0_method,
|
263 |
+
file_index,
|
264 |
+
# file_big_npy,
|
265 |
+
index_rate,
|
266 |
+
if_f0,
|
267 |
+
filter_radius,
|
268 |
+
tgt_sr,
|
269 |
+
resample_sr,
|
270 |
+
rms_mix_rate,
|
271 |
+
version,
|
272 |
+
f0_file=None,
|
273 |
+
):
|
274 |
+
if (
|
275 |
+
file_index != ""
|
276 |
+
# and file_big_npy != ""
|
277 |
+
# and os.path.exists(file_big_npy) == True
|
278 |
+
and os.path.exists(file_index) == True
|
279 |
+
and index_rate != 0
|
280 |
+
):
|
281 |
+
try:
|
282 |
+
index = faiss.read_index(file_index)
|
283 |
+
# big_npy = np.load(file_big_npy)
|
284 |
+
big_npy = index.reconstruct_n(0, index.ntotal)
|
285 |
+
except:
|
286 |
+
traceback.print_exc()
|
287 |
+
index = big_npy = None
|
288 |
+
else:
|
289 |
+
index = big_npy = None
|
290 |
+
audio = signal.filtfilt(bh, ah, audio)
|
291 |
+
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
|
292 |
+
opt_ts = []
|
293 |
+
if audio_pad.shape[0] > self.t_max:
|
294 |
+
audio_sum = np.zeros_like(audio)
|
295 |
+
for i in range(self.window):
|
296 |
+
audio_sum += audio_pad[i : i - self.window]
|
297 |
+
for t in range(self.t_center, audio.shape[0], self.t_center):
|
298 |
+
opt_ts.append(
|
299 |
+
t
|
300 |
+
- self.t_query
|
301 |
+
+ np.where(
|
302 |
+
np.abs(audio_sum[t - self.t_query : t + self.t_query])
|
303 |
+
== np.abs(audio_sum[t - self.t_query : t + self.t_query]).min()
|
304 |
+
)[0][0]
|
305 |
+
)
|
306 |
+
s = 0
|
307 |
+
audio_opt = []
|
308 |
+
t = None
|
309 |
+
t1 = ttime()
|
310 |
+
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
|
311 |
+
p_len = audio_pad.shape[0] // self.window
|
312 |
+
inp_f0 = None
|
313 |
+
if hasattr(f0_file, "name") == True:
|
314 |
+
try:
|
315 |
+
with open(f0_file.name, "r") as f:
|
316 |
+
lines = f.read().strip("\n").split("\n")
|
317 |
+
inp_f0 = []
|
318 |
+
for line in lines:
|
319 |
+
inp_f0.append([float(i) for i in line.split(",")])
|
320 |
+
inp_f0 = np.array(inp_f0, dtype="float32")
|
321 |
+
except:
|
322 |
+
traceback.print_exc()
|
323 |
+
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
|
324 |
+
pitch, pitchf = None, None
|
325 |
+
if if_f0 == 1:
|
326 |
+
pitch, pitchf = self.get_f0(
|
327 |
+
input_audio_path,
|
328 |
+
audio_pad,
|
329 |
+
p_len,
|
330 |
+
f0_up_key,
|
331 |
+
f0_method,
|
332 |
+
filter_radius,
|
333 |
+
inp_f0,
|
334 |
+
)
|
335 |
+
pitch = pitch[:p_len]
|
336 |
+
pitchf = pitchf[:p_len]
|
337 |
+
if self.device == "mps":
|
338 |
+
pitchf = pitchf.astype(np.float32)
|
339 |
+
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
|
340 |
+
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
|
341 |
+
t2 = ttime()
|
342 |
+
times[1] += t2 - t1
|
343 |
+
for t in opt_ts:
|
344 |
+
t = t // self.window * self.window
|
345 |
+
if if_f0 == 1:
|
346 |
+
audio_opt.append(
|
347 |
+
self.vc(
|
348 |
+
model,
|
349 |
+
net_g,
|
350 |
+
sid,
|
351 |
+
audio_pad[s : t + self.t_pad2 + self.window],
|
352 |
+
pitch[:, s // self.window : (t + self.t_pad2) // self.window],
|
353 |
+
pitchf[:, s // self.window : (t + self.t_pad2) // self.window],
|
354 |
+
times,
|
355 |
+
index,
|
356 |
+
big_npy,
|
357 |
+
index_rate,
|
358 |
+
version,
|
359 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
360 |
+
)
|
361 |
+
else:
|
362 |
+
audio_opt.append(
|
363 |
+
self.vc(
|
364 |
+
model,
|
365 |
+
net_g,
|
366 |
+
sid,
|
367 |
+
audio_pad[s : t + self.t_pad2 + self.window],
|
368 |
+
None,
|
369 |
+
None,
|
370 |
+
times,
|
371 |
+
index,
|
372 |
+
big_npy,
|
373 |
+
index_rate,
|
374 |
+
version,
|
375 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
376 |
+
)
|
377 |
+
s = t
|
378 |
+
if if_f0 == 1:
|
379 |
+
audio_opt.append(
|
380 |
+
self.vc(
|
381 |
+
model,
|
382 |
+
net_g,
|
383 |
+
sid,
|
384 |
+
audio_pad[t:],
|
385 |
+
pitch[:, t // self.window :] if t is not None else pitch,
|
386 |
+
pitchf[:, t // self.window :] if t is not None else pitchf,
|
387 |
+
times,
|
388 |
+
index,
|
389 |
+
big_npy,
|
390 |
+
index_rate,
|
391 |
+
version,
|
392 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
393 |
+
)
|
394 |
+
else:
|
395 |
+
audio_opt.append(
|
396 |
+
self.vc(
|
397 |
+
model,
|
398 |
+
net_g,
|
399 |
+
sid,
|
400 |
+
audio_pad[t:],
|
401 |
+
None,
|
402 |
+
None,
|
403 |
+
times,
|
404 |
+
index,
|
405 |
+
big_npy,
|
406 |
+
index_rate,
|
407 |
+
version,
|
408 |
+
)[self.t_pad_tgt : -self.t_pad_tgt]
|
409 |
+
)
|
410 |
+
audio_opt = np.concatenate(audio_opt)
|
411 |
+
if rms_mix_rate != 1:
|
412 |
+
audio_opt = change_rms(audio, 16000, audio_opt, tgt_sr, rms_mix_rate)
|
413 |
+
if resample_sr >= 16000 and tgt_sr != resample_sr:
|
414 |
+
audio_opt = librosa.resample(
|
415 |
+
audio_opt, orig_sr=tgt_sr, target_sr=resample_sr
|
416 |
+
)
|
417 |
+
audio_max = np.abs(audio_opt).max() / 0.99
|
418 |
+
max_int16 = 32768
|
419 |
+
if audio_max > 1:
|
420 |
+
max_int16 /= audio_max
|
421 |
+
audio_opt = (audio_opt * max_int16).astype(np.int16)
|
422 |
+
del pitch, pitchf, sid
|
423 |
+
if torch.cuda.is_available():
|
424 |
+
torch.cuda.empty_cache()
|
425 |
+
return audio_opt
|