2023-05-29 17:52:23 +02:00
|
|
|
from infer_pack.modules.F0Predictor.F0Predictor import F0Predictor
|
|
|
|
import pyworld
|
|
|
|
import numpy as np
|
|
|
|
|
2023-05-30 09:22:53 +02:00
|
|
|
|
2023-05-29 17:52:23 +02:00
|
|
|
class DioF0Predictor(F0Predictor):
|
2023-05-30 09:22:53 +02:00
|
|
|
def __init__(self, hop_length=512, f0_min=50, f0_max=1100, sampling_rate=44100):
|
2023-05-29 17:52:23 +02:00
|
|
|
self.hop_length = hop_length
|
|
|
|
self.f0_min = f0_min
|
|
|
|
self.f0_max = f0_max
|
|
|
|
self.sampling_rate = sampling_rate
|
|
|
|
|
2023-05-30 09:22:53 +02:00
|
|
|
def interpolate_f0(self, f0):
|
|
|
|
"""
|
2023-05-29 17:52:23 +02:00
|
|
|
对F0进行插值处理
|
2023-05-30 09:22:53 +02:00
|
|
|
"""
|
|
|
|
|
2023-05-29 17:52:23 +02:00
|
|
|
data = np.reshape(f0, (f0.size, 1))
|
2023-05-30 09:22:53 +02:00
|
|
|
|
2023-05-29 17:52:23 +02:00
|
|
|
vuv_vector = np.zeros((data.size, 1), dtype=np.float32)
|
|
|
|
vuv_vector[data > 0.0] = 1.0
|
|
|
|
vuv_vector[data <= 0.0] = 0.0
|
2023-05-30 09:22:53 +02:00
|
|
|
|
2023-05-29 17:52:23 +02:00
|
|
|
ip_data = data
|
2023-05-30 09:22:53 +02:00
|
|
|
|
2023-05-29 17:52:23 +02:00
|
|
|
frame_number = data.size
|
|
|
|
last_value = 0.0
|
|
|
|
for i in range(frame_number):
|
|
|
|
if data[i] <= 0.0:
|
|
|
|
j = i + 1
|
|
|
|
for j in range(i + 1, frame_number):
|
|
|
|
if data[j] > 0.0:
|
|
|
|
break
|
|
|
|
if j < frame_number - 1:
|
|
|
|
if last_value > 0.0:
|
|
|
|
step = (data[j] - data[i - 1]) / float(j - i)
|
|
|
|
for k in range(i, j):
|
|
|
|
ip_data[k] = data[i - 1] + step * (k - i + 1)
|
|
|
|
else:
|
|
|
|
for k in range(i, j):
|
|
|
|
ip_data[k] = data[j]
|
|
|
|
else:
|
|
|
|
for k in range(i, frame_number):
|
|
|
|
ip_data[k] = last_value
|
|
|
|
else:
|
2023-05-30 09:22:53 +02:00
|
|
|
ip_data[i] = data[i] # 这里可能存在一个没有必要的拷贝
|
2023-05-29 17:52:23 +02:00
|
|
|
last_value = data[i]
|
|
|
|
|
2023-05-30 09:22:53 +02:00
|
|
|
return ip_data[:, 0], vuv_vector[:, 0]
|
|
|
|
|
|
|
|
def resize_f0(self, x, target_len):
|
2023-05-29 17:52:23 +02:00
|
|
|
source = np.array(x)
|
2023-05-30 09:22:53 +02:00
|
|
|
source[source < 0.001] = np.nan
|
|
|
|
target = np.interp(
|
|
|
|
np.arange(0, len(source) * target_len, len(source)) / target_len,
|
|
|
|
np.arange(0, len(source)),
|
|
|
|
source,
|
|
|
|
)
|
2023-05-29 17:52:23 +02:00
|
|
|
res = np.nan_to_num(target)
|
|
|
|
return res
|
2023-05-30 09:22:53 +02:00
|
|
|
|
|
|
|
def compute_f0(self, wav, p_len=None):
|
2023-05-29 17:52:23 +02:00
|
|
|
if p_len is None:
|
2023-05-30 09:22:53 +02:00
|
|
|
p_len = wav.shape[0] // self.hop_length
|
2023-05-29 17:52:23 +02:00
|
|
|
f0, t = pyworld.dio(
|
|
|
|
wav.astype(np.double),
|
|
|
|
fs=self.sampling_rate,
|
|
|
|
f0_floor=self.f0_min,
|
|
|
|
f0_ceil=self.f0_max,
|
|
|
|
frame_period=1000 * self.hop_length / self.sampling_rate,
|
|
|
|
)
|
|
|
|
f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.sampling_rate)
|
|
|
|
for index, pitch in enumerate(f0):
|
|
|
|
f0[index] = round(pitch, 1)
|
|
|
|
return self.interpolate_f0(self.resize_f0(f0, p_len))[0]
|
|
|
|
|
2023-05-30 09:22:53 +02:00
|
|
|
def compute_f0_uv(self, wav, p_len=None):
|
2023-05-29 17:52:23 +02:00
|
|
|
if p_len is None:
|
2023-05-30 09:22:53 +02:00
|
|
|
p_len = wav.shape[0] // self.hop_length
|
2023-05-29 17:52:23 +02:00
|
|
|
f0, t = pyworld.dio(
|
|
|
|
wav.astype(np.double),
|
|
|
|
fs=self.sampling_rate,
|
|
|
|
f0_floor=self.f0_min,
|
|
|
|
f0_ceil=self.f0_max,
|
|
|
|
frame_period=1000 * self.hop_length / self.sampling_rate,
|
|
|
|
)
|
|
|
|
f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.sampling_rate)
|
|
|
|
for index, pitch in enumerate(f0):
|
|
|
|
f0[index] = round(pitch, 1)
|
|
|
|
return self.interpolate_f0(self.resize_f0(f0, p_len))
|