forked from svc-develop-team/so-vits-svc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request svc-develop-team#292 from svc-develop-team/4.1-Latest
4.1 latest
- Loading branch information
Showing
18 changed files
with
681 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from typing import Union | ||
|
||
import numpy as np | ||
import torch | ||
import torch.nn.functional as F | ||
|
||
from modules.F0Predictor.F0Predictor import F0Predictor | ||
|
||
from .rmvpe import RMVPE | ||
|
||
|
||
class RMVPEF0Predictor(F0Predictor): | ||
def __init__(self,hop_length=512,f0_min=50,f0_max=1100, dtype=torch.float32, device=None,sampling_rate=44100,threshold=0.05): | ||
self.rmvpe = RMVPE(model_path="pretrain/rmvpe.pt",dtype=dtype,device=device) | ||
self.hop_length = hop_length | ||
self.f0_min = f0_min | ||
self.f0_max = f0_max | ||
if device is None: | ||
self.device = 'cuda' if torch.cuda.is_available() else 'cpu' | ||
else: | ||
self.device = device | ||
self.threshold = threshold | ||
self.sampling_rate = sampling_rate | ||
self.dtype = dtype | ||
|
||
def repeat_expand( | ||
self, content: Union[torch.Tensor, np.ndarray], target_len: int, mode: str = "nearest" | ||
): | ||
ndim = content.ndim | ||
|
||
if content.ndim == 1: | ||
content = content[None, None] | ||
elif content.ndim == 2: | ||
content = content[None] | ||
|
||
assert content.ndim == 3 | ||
|
||
is_np = isinstance(content, np.ndarray) | ||
if is_np: | ||
content = torch.from_numpy(content) | ||
|
||
results = torch.nn.functional.interpolate(content, size=target_len, mode=mode) | ||
|
||
if is_np: | ||
results = results.numpy() | ||
|
||
if ndim == 1: | ||
return results[0, 0] | ||
elif ndim == 2: | ||
return results[0] | ||
|
||
def post_process(self, x, sampling_rate, f0, pad_to): | ||
if isinstance(f0, np.ndarray): | ||
f0 = torch.from_numpy(f0).float().to(x.device) | ||
|
||
if pad_to is None: | ||
return f0 | ||
|
||
f0 = self.repeat_expand(f0, pad_to) | ||
|
||
vuv_vector = torch.zeros_like(f0) | ||
vuv_vector[f0 > 0.0] = 1.0 | ||
vuv_vector[f0 <= 0.0] = 0.0 | ||
|
||
# 去掉0频率, 并线性插值 | ||
nzindex = torch.nonzero(f0).squeeze() | ||
f0 = torch.index_select(f0, dim=0, index=nzindex).cpu().numpy() | ||
time_org = self.hop_length / sampling_rate * nzindex.cpu().numpy() | ||
time_frame = np.arange(pad_to) * self.hop_length / sampling_rate | ||
|
||
vuv_vector = F.interpolate(vuv_vector[None,None,:],size=pad_to)[0][0] | ||
|
||
if f0.shape[0] <= 0: | ||
return torch.zeros(pad_to, dtype=torch.float, device=x.device),vuv_vector.cpu().numpy() | ||
if f0.shape[0] == 1: | ||
return torch.ones(pad_to, dtype=torch.float, device=x.device) * f0[0],vuv_vector.cpu().numpy() | ||
|
||
# 大概可以用 torch 重写? | ||
f0 = np.interp(time_frame, time_org, f0, left=f0[0], right=f0[-1]) | ||
#vuv_vector = np.ceil(scipy.ndimage.zoom(vuv_vector,pad_to/len(vuv_vector),order = 0)) | ||
|
||
return f0,vuv_vector.cpu().numpy() | ||
|
||
def compute_f0(self,wav,p_len=None): | ||
x = torch.FloatTensor(wav).to(self.dtype).to(self.device) | ||
if p_len is None: | ||
p_len = x.shape[0]//self.hop_length | ||
else: | ||
assert abs(p_len-x.shape[0]//self.hop_length) < 4, "pad length error" | ||
f0 = self.rmvpe.infer_from_audio(x,self.sampling_rate,self.threshold) | ||
if torch.all(f0 == 0): | ||
rtn = f0.cpu().numpy() if p_len is None else np.zeros(p_len) | ||
return rtn,rtn | ||
return self.post_process(x,self.sampling_rate,f0,p_len)[0] | ||
|
||
def compute_f0_uv(self,wav,p_len=None): | ||
x = torch.FloatTensor(wav).to(self.dtype).to(self.device) | ||
if p_len is None: | ||
p_len = x.shape[0]//self.hop_length | ||
else: | ||
assert abs(p_len-x.shape[0]//self.hop_length) < 4, "pad length error" | ||
f0 = self.rmvpe.infer_from_audio(x,self.sampling_rate,self.threshold) | ||
if torch.all(f0 == 0): | ||
rtn = f0.cpu().numpy() if p_len is None else np.zeros(p_len) | ||
return rtn,rtn | ||
return self.post_process(x,self.sampling_rate,f0,p_len) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from .constants import * # noqa: F403 | ||
from .inference import RMVPE # noqa: F401 | ||
from .model import E2E, E2E0 # noqa: F401 | ||
from .spec import MelSpectrogram # noqa: F401 | ||
from .utils import ( # noqa: F401 | ||
cycle, | ||
summary, | ||
to_local_average_cents, | ||
to_viterbi_cents, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SAMPLE_RATE = 16000 | ||
|
||
N_CLASS = 360 | ||
|
||
N_MELS = 128 | ||
MEL_FMIN = 30 | ||
MEL_FMAX = SAMPLE_RATE // 2 | ||
WINDOW_LENGTH = 1024 | ||
CONST = 1997.3794084376191 |
Oops, something went wrong.