voice-changer/server/voice_changer/DiffusionSVC/pitchExtractor/HarvestPitchExtractor.py

41 lines
1.3 KiB
Python
Raw Normal View History

2023-07-14 07:54:08 +03:00
import pyworld
import numpy as np
from const import PitchExtractorType
2023-07-14 22:53:38 +03:00
import torch
2023-07-14 07:54:08 +03:00
from voice_changer.RVC.pitchExtractor.PitchExtractor import PitchExtractor
class HarvestPitchExtractor(PitchExtractor):
def __init__(self):
super().__init__()
self.pitchExtractorType: PitchExtractorType = "harvest"
2023-07-14 23:30:07 +03:00
self.f0_min = 50
self.f0_max = 1100
self.sapmle_rate = 16000
self.uv_interp = True
2023-07-14 07:54:08 +03:00
2023-07-14 22:53:38 +03:00
def extract(self, audio: torch.Tensor, pitchf, f0_up_key, sr, window, silence_front=0):
2023-07-14 07:54:08 +03:00
audio = audio.detach().cpu().numpy()
2023-07-14 23:30:07 +03:00
start_frame = int(silence_front * self.sapmle_rate / window)
real_silence_front = start_frame * window / self.sapmle_rate
audio = audio[int(np.round(real_silence_front * self.sapmle_rate)):]
2023-07-14 07:54:08 +03:00
f0, _ = pyworld.harvest(
audio.astype('double'),
16000,
f0_floor=50,
f0_ceil=1100,
2023-07-14 23:30:07 +03:00
frame_period=(1000 * window / self.sapmle_rate))
pitchf[-f0.shape[0]:] = f0[:pitchf.shape[0]]
f0 = pitchf
2023-07-14 07:54:08 +03:00
2023-07-14 23:30:07 +03:00
if self.uv_interp:
2023-07-14 07:54:08 +03:00
uv = f0 == 0
if len(f0[~uv]) > 0:
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
f0[f0 < 50] = 50
2023-07-14 23:30:07 +03:00
f0 = f0 * 2 ** (float(f0_up_key) / 12)
2023-07-14 07:54:08 +03:00
return f0