2023-05-13 07:33:40 +00:00
|
|
|
import numpy as np
|
2023-06-26 06:57:53 +00:00
|
|
|
import parselmouth
|
|
|
|
|
|
|
|
from modules.F0Predictor.F0Predictor import F0Predictor
|
|
|
|
|
2023-05-13 07:33:40 +00:00
|
|
|
|
|
|
|
class PMF0Predictor(F0Predictor):
|
|
|
|
def __init__(self,hop_length=512,f0_min=50,f0_max=1100,sampling_rate=44100):
|
|
|
|
self.hop_length = hop_length
|
|
|
|
self.f0_min = f0_min
|
|
|
|
self.f0_max = f0_max
|
|
|
|
self.sampling_rate = sampling_rate
|
|
|
|
|
|
|
|
|
|
|
|
def interpolate_f0(self,f0):
|
|
|
|
'''
|
|
|
|
对F0进行插值处理
|
|
|
|
'''
|
2023-06-05 12:52:57 +00:00
|
|
|
vuv_vector = np.zeros_like(f0, dtype=np.float32)
|
|
|
|
vuv_vector[f0 > 0.0] = 1.0
|
|
|
|
vuv_vector[f0 <= 0.0] = 0.0
|
2023-05-13 07:33:40 +00:00
|
|
|
|
2023-06-05 12:52:57 +00:00
|
|
|
nzindex = np.nonzero(f0)[0]
|
|
|
|
data = f0[nzindex]
|
|
|
|
nzindex = nzindex.astype(np.float32)
|
|
|
|
time_org = self.hop_length / self.sampling_rate * nzindex
|
|
|
|
time_frame = np.arange(f0.shape[0]) * self.hop_length / self.sampling_rate
|
2023-06-07 17:47:42 +00:00
|
|
|
|
|
|
|
if data.shape[0] <= 0:
|
2023-06-08 04:37:01 +00:00
|
|
|
return np.zeros(f0.shape[0], dtype=np.float32),vuv_vector
|
2023-06-07 17:47:42 +00:00
|
|
|
|
|
|
|
if data.shape[0] == 1:
|
2023-06-08 04:37:01 +00:00
|
|
|
return np.ones(f0.shape[0], dtype=np.float32) * f0[0],vuv_vector
|
2023-06-07 17:47:42 +00:00
|
|
|
|
2023-06-05 12:52:57 +00:00
|
|
|
f0 = np.interp(time_frame, time_org, data, left=data[0], right=data[-1])
|
|
|
|
|
|
|
|
return f0,vuv_vector
|
2023-06-07 17:47:42 +00:00
|
|
|
|
2023-05-13 07:33:40 +00:00
|
|
|
|
|
|
|
def compute_f0(self,wav,p_len=None):
|
|
|
|
x = wav
|
|
|
|
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"
|
|
|
|
time_step = self.hop_length / self.sampling_rate * 1000
|
|
|
|
f0 = parselmouth.Sound(x, self.sampling_rate).to_pitch_ac(
|
|
|
|
time_step=time_step / 1000, voicing_threshold=0.6,
|
|
|
|
pitch_floor=self.f0_min, pitch_ceiling=self.f0_max).selected_array['frequency']
|
|
|
|
|
|
|
|
pad_size=(p_len - len(f0) + 1) // 2
|
|
|
|
if(pad_size>0 or p_len - len(f0) - pad_size>0):
|
|
|
|
f0 = np.pad(f0,[[pad_size,p_len - len(f0) - pad_size]], mode='constant')
|
|
|
|
f0,uv = self.interpolate_f0(f0)
|
|
|
|
return f0
|
|
|
|
|
|
|
|
def compute_f0_uv(self,wav,p_len=None):
|
|
|
|
x = wav
|
|
|
|
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"
|
|
|
|
time_step = self.hop_length / self.sampling_rate * 1000
|
|
|
|
f0 = parselmouth.Sound(x, self.sampling_rate).to_pitch_ac(
|
|
|
|
time_step=time_step / 1000, voicing_threshold=0.6,
|
|
|
|
pitch_floor=self.f0_min, pitch_ceiling=self.f0_max).selected_array['frequency']
|
|
|
|
|
|
|
|
pad_size=(p_len - len(f0) + 1) // 2
|
|
|
|
if(pad_size>0 or p_len - len(f0) - pad_size>0):
|
|
|
|
f0 = np.pad(f0,[[pad_size,p_len - len(f0) - pad_size]], mode='constant')
|
|
|
|
f0,uv = self.interpolate_f0(f0)
|
|
|
|
return f0,uv
|