Yoshitaka16 commited on
Commit
7316673
·
verified ·
1 Parent(s): 9d7d1a0

Upload f0.py

Browse files
Files changed (1) hide show
  1. f0.py +84 -0
f0.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+
4
+ from rvc.lib.predictors.RMVPE import RMVPE0Predictor
5
+ from torchfcpe import spawn_bundled_infer_model
6
+ import torchcrepe
7
+
8
+
9
+ class RMVPE:
10
+ def __init__(self, device, model_name="rmvpe.pt", sample_rate=16000, hop_size=160):
11
+ self.device = device
12
+ self.sample_rate = sample_rate
13
+ self.hop_size = hop_size
14
+ self.model = RMVPE0Predictor(
15
+ os.path.join("rvc", "models", "predictors", model_name),
16
+ device=self.device,
17
+ )
18
+
19
+ def get_f0(self, x, filter_radius=0.03):
20
+ f0 = self.model.infer_from_audio(x, thred=filter_radius)
21
+ return f0
22
+
23
+
24
+ class CREPE:
25
+ def __init__(self, device, sample_rate=16000, hop_size=160):
26
+ self.device = device
27
+ self.sample_rate = sample_rate
28
+ self.hop_size = hop_size
29
+
30
+ def get_f0(self, x, f0_min=50, f0_max=1100, p_len=None, model="full"):
31
+ if p_len is None:
32
+ p_len = x.shape[0] // self.hop_size
33
+
34
+ if not torch.is_tensor(x):
35
+ x = torch.from_numpy(x)
36
+
37
+ batch_size = 512
38
+
39
+ f0, pd = torchcrepe.predict(
40
+ x.float().to(self.device).unsqueeze(dim=0),
41
+ self.sample_rate,
42
+ self.hop_size,
43
+ f0_min,
44
+ f0_max,
45
+ model=model,
46
+ batch_size=batch_size,
47
+ device=self.device,
48
+ return_periodicity=True,
49
+ )
50
+ pd = torchcrepe.filter.median(pd, 3)
51
+ f0 = torchcrepe.filter.mean(f0, 3)
52
+ f0[pd < 0.1] = 0
53
+ f0 = f0[0].cpu().numpy()
54
+
55
+ return f0
56
+
57
+
58
+ class FCPE:
59
+ def __init__(self, device, sample_rate=16000, hop_size=160):
60
+ self.device = device
61
+ self.sample_rate = sample_rate
62
+ self.hop_size = hop_size
63
+ self.model = spawn_bundled_infer_model(self.device)
64
+
65
+ def get_f0(self, x, p_len=None, filter_radius=0.006):
66
+ if p_len is None:
67
+ p_len = x.shape[0] // self.hop_size
68
+
69
+ if not torch.is_tensor(x):
70
+ x = torch.from_numpy(x)
71
+
72
+ f0 = (
73
+ self.model.infer(
74
+ x.float().to(self.device).unsqueeze(0),
75
+ sr=self.sample_rate,
76
+ decoder_mode="local_argmax",
77
+ threshold=filter_radius,
78
+ )
79
+ .squeeze()
80
+ .cpu()
81
+ .numpy()
82
+ )
83
+
84
+ return f0