This commit is contained in:
w-okada 2023-07-15 05:40:40 +09:00
parent 02eadeeaa3
commit 8a2df2dbb4
3 changed files with 37 additions and 39 deletions

View File

@ -1,8 +1,9 @@
import pyworld import pyworld
import numpy as np import numpy as np
from const import PitchExtractorType from const import PitchExtractorType
import torch
from voice_changer.RVC.pitchExtractor.PitchExtractor import PitchExtractor from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor
class DioPitchExtractor(PitchExtractor): class DioPitchExtractor(PitchExtractor):
@ -10,40 +11,35 @@ class DioPitchExtractor(PitchExtractor):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.pitchExtractorType: PitchExtractorType = "dio" self.pitchExtractorType: PitchExtractorType = "dio"
self.f0_min = 50
self.f0_max = 1100
self.sapmle_rate = 16000
self.uv_interp = True
def extract(self, audio, pitchf, f0_up_key, sr, window, silence_front=0): def extract(self, audio: torch.Tensor, pitch, f0_up_key, sr, window, silence_front=0):
audio = audio.detach().cpu().numpy() audio = audio.detach().cpu().numpy()
n_frames = int(len(audio) // window) + 1 # NOQA start_frame = int(silence_front * self.sapmle_rate / window)
start_frame = int(silence_front * sr / window) real_silence_front = start_frame * window / self.sapmle_rate
real_silence_front = start_frame * window / sr audio = audio[int(np.round(real_silence_front * self.sapmle_rate)):]
silence_front_offset = max(min(int(np.round(real_silence_front * sr)), len(audio) - 3000), 0)
audio = audio[silence_front_offset:]
f0_min = 50
f0_max = 1100
f0_mel_min = 1127 * np.log(1 + f0_min / 700)
f0_mel_max = 1127 * np.log(1 + f0_max / 700)
_f0, t = pyworld.dio( _f0, t = pyworld.dio(
audio.astype(np.double), audio.astype(np.double),
sr, self.sapmle_rate,
f0_floor=f0_min, f0_floor=self.f0_min,
f0_ceil=f0_max, f0_ceil=self.f0_max,
channels_in_octave=2, channels_in_octave=2,
frame_period=10, frame_period=(1000 * window / self.sapmle_rate)
) )
f0 = pyworld.stonemask(audio.astype(np.double), _f0, t, sr) f0 = pyworld.stonemask(audio.astype(np.double), _f0, t, self.sapmle_rate)
pitch[-f0.shape[0]:] = f0[:pitch.shape[0]]
f0 = pitch
f0 *= pow(2, f0_up_key / 12) if self.uv_interp:
pitchf[-f0.shape[0]:] = f0[:pitchf.shape[0]] uv = f0 == 0
f0bak = pitchf.copy() if len(f0[~uv]) > 0:
f0_mel = 1127 * np.log(1 + f0bak / 700) f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / ( f0[f0 < self.f0_min] = self.f0_min
f0_mel_max - f0_mel_min
) + 1
f0_mel[f0_mel <= 1] = 1
f0_mel[f0_mel > 255] = 255
pitch_coarse = np.rint(f0_mel).astype(int)
return pitch_coarse, pitchf f0 = f0 * 2 ** (float(f0_up_key) / 12)
return f0

View File

@ -2,7 +2,8 @@ import pyworld
import numpy as np import numpy as np
from const import PitchExtractorType from const import PitchExtractorType
import torch import torch
from voice_changer.RVC.pitchExtractor.PitchExtractor import PitchExtractor
from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor
class HarvestPitchExtractor(PitchExtractor): class HarvestPitchExtractor(PitchExtractor):
@ -15,25 +16,25 @@ class HarvestPitchExtractor(PitchExtractor):
self.sapmle_rate = 16000 self.sapmle_rate = 16000
self.uv_interp = True self.uv_interp = True
def extract(self, audio: torch.Tensor, pitchf, f0_up_key, sr, window, silence_front=0): def extract(self, audio: torch.Tensor, pitch, f0_up_key, sr, window, silence_front=0):
audio = audio.detach().cpu().numpy() audio = audio.detach().cpu().numpy()
start_frame = int(silence_front * self.sapmle_rate / window) start_frame = int(silence_front * self.sapmle_rate / window)
real_silence_front = start_frame * window / self.sapmle_rate real_silence_front = start_frame * window / self.sapmle_rate
audio = audio[int(np.round(real_silence_front * self.sapmle_rate)):] audio = audio[int(np.round(real_silence_front * self.sapmle_rate)):]
f0, _ = pyworld.harvest( f0, _ = pyworld.harvest(
audio.astype('double'), audio.astype('double'),
16000, self.sapmle_rate,
f0_floor=50, f0_floor=self.f0_min,
f0_ceil=1100, f0_ceil=self.f0_max,
frame_period=(1000 * window / self.sapmle_rate)) frame_period=(1000 * window / self.sapmle_rate))
pitchf[-f0.shape[0]:] = f0[:pitchf.shape[0]] pitch[-f0.shape[0]:] = f0[:pitch.shape[0]]
f0 = pitchf f0 = pitch
if self.uv_interp: if self.uv_interp:
uv = f0 == 0 uv = f0 == 0
if len(f0[~uv]) > 0: if len(f0[~uv]) > 0:
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv]) f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
f0[f0 < 50] = 50 f0[f0 < self.f0_min] = self.f0_min
f0 = f0 * 2 ** (float(f0_up_key) / 12) f0 = f0 * 2 ** (float(f0_up_key) / 12)

View File

@ -1,5 +1,6 @@
from typing import Protocol from typing import Protocol
from const import PitchExtractorType from const import PitchExtractorType
from voice_changer.DiffusionSVC.pitchExtractor.DioPitchExtractor import DioPitchExtractor
from voice_changer.DiffusionSVC.pitchExtractor.HarvestPitchExtractor import HarvestPitchExtractor from voice_changer.DiffusionSVC.pitchExtractor.HarvestPitchExtractor import HarvestPitchExtractor
from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
@ -26,8 +27,8 @@ class PitchExtractorManager(Protocol):
) -> PitchExtractor: ) -> PitchExtractor:
if pitchExtractorType == "harvest": if pitchExtractorType == "harvest":
return HarvestPitchExtractor() return HarvestPitchExtractor()
# elif pitchExtractorType == "dio": elif pitchExtractorType == "dio":
# return DioPitchExtractor() return DioPitchExtractor()
# elif pitchExtractorType == "crepe": # elif pitchExtractorType == "crepe":
# return CrepePitchExtractor() # return CrepePitchExtractor()
# elif pitchExtractorType == "crepe_tiny": # elif pitchExtractorType == "crepe_tiny":