mirror of
https://github.com/w-okada/voice-changer.git
synced 2025-01-23 21:45:00 +03:00
WIP: dio
This commit is contained in:
parent
02eadeeaa3
commit
8a2df2dbb4
@ -1,8 +1,9 @@
|
||||
import pyworld
|
||||
import numpy as np
|
||||
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):
|
||||
@ -10,40 +11,35 @@ class DioPitchExtractor(PitchExtractor):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
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()
|
||||
n_frames = int(len(audio) // window) + 1 # NOQA
|
||||
start_frame = int(silence_front * sr / window)
|
||||
real_silence_front = start_frame * window / sr
|
||||
|
||||
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)
|
||||
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)):]
|
||||
|
||||
_f0, t = pyworld.dio(
|
||||
audio.astype(np.double),
|
||||
sr,
|
||||
f0_floor=f0_min,
|
||||
f0_ceil=f0_max,
|
||||
self.sapmle_rate,
|
||||
f0_floor=self.f0_min,
|
||||
f0_ceil=self.f0_max,
|
||||
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)
|
||||
pitchf[-f0.shape[0]:] = f0[:pitchf.shape[0]]
|
||||
f0bak = pitchf.copy()
|
||||
f0_mel = 1127 * np.log(1 + f0bak / 700)
|
||||
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - f0_mel_min) * 254 / (
|
||||
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)
|
||||
if self.uv_interp:
|
||||
uv = f0 == 0
|
||||
if len(f0[~uv]) > 0:
|
||||
f0[uv] = np.interp(np.where(uv)[0], np.where(~uv)[0], f0[~uv])
|
||||
f0[f0 < self.f0_min] = self.f0_min
|
||||
|
||||
return pitch_coarse, pitchf
|
||||
f0 = f0 * 2 ** (float(f0_up_key) / 12)
|
||||
|
||||
return f0
|
||||
|
@ -2,7 +2,8 @@ import pyworld
|
||||
import numpy as np
|
||||
from const import PitchExtractorType
|
||||
import torch
|
||||
from voice_changer.RVC.pitchExtractor.PitchExtractor import PitchExtractor
|
||||
|
||||
from voice_changer.DiffusionSVC.pitchExtractor.PitchExtractor import PitchExtractor
|
||||
|
||||
|
||||
class HarvestPitchExtractor(PitchExtractor):
|
||||
@ -15,25 +16,25 @@ class HarvestPitchExtractor(PitchExtractor):
|
||||
self.sapmle_rate = 16000
|
||||
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()
|
||||
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)):]
|
||||
f0, _ = pyworld.harvest(
|
||||
audio.astype('double'),
|
||||
16000,
|
||||
f0_floor=50,
|
||||
f0_ceil=1100,
|
||||
self.sapmle_rate,
|
||||
f0_floor=self.f0_min,
|
||||
f0_ceil=self.f0_max,
|
||||
frame_period=(1000 * window / self.sapmle_rate))
|
||||
pitchf[-f0.shape[0]:] = f0[:pitchf.shape[0]]
|
||||
f0 = pitchf
|
||||
pitch[-f0.shape[0]:] = f0[:pitch.shape[0]]
|
||||
f0 = pitch
|
||||
|
||||
if self.uv_interp:
|
||||
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
|
||||
f0[f0 < self.f0_min] = self.f0_min
|
||||
|
||||
f0 = f0 * 2 ** (float(f0_up_key) / 12)
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
from typing import Protocol
|
||||
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.PitchExtractor import PitchExtractor
|
||||
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
|
||||
@ -26,8 +27,8 @@ class PitchExtractorManager(Protocol):
|
||||
) -> PitchExtractor:
|
||||
if pitchExtractorType == "harvest":
|
||||
return HarvestPitchExtractor()
|
||||
# elif pitchExtractorType == "dio":
|
||||
# return DioPitchExtractor()
|
||||
elif pitchExtractorType == "dio":
|
||||
return DioPitchExtractor()
|
||||
# elif pitchExtractorType == "crepe":
|
||||
# return CrepePitchExtractor()
|
||||
# elif pitchExtractorType == "crepe_tiny":
|
||||
|
Loading…
Reference in New Issue
Block a user