Skip to content

Commit

Permalink
Update vc_infer_pipeline.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Bebra777228 authored Aug 4, 2024
1 parent e9c4bdb commit 461452c
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/vc_infer_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

input_audio_path2wav = {}


class AudioProcessor:
@staticmethod
def change_rms(source_audio, source_rate, target_audio, target_rate, rate):
Expand All @@ -37,6 +38,30 @@ def change_rms(source_audio, source_rate, target_audio, target_rate, rate):

return target_audio * (torch.pow(rms1, 1 - rate) * torch.pow(rms2, rate - 1)).numpy()


class Autotune:
def __init__(self, ref_freqs):
self.ref_freqs = ref_freqs
self.note_dict = self.generate_interpolated_frequencies()

def generate_interpolated_frequencies(self):
note_dict = []
for i in range(len(self.ref_freqs) - 1):
freq_low = self.ref_freqs[i]
freq_high = self.ref_freqs[i + 1]
interpolated_freqs = np.linspace(freq_low, freq_high, num=10, endpoint=False)
note_dict.extend(interpolated_freqs)
note_dict.append(self.ref_freqs[-1])
return note_dict

def autotune_f0(self, f0):
autotuned_f0 = np.zeros_like(f0)
for i, freq in enumerate(f0):
closest_note = min(self.note_dict, key=lambda x: abs(x - freq))
autotuned_f0[i] = closest_note
return autotuned_f0


class VC:
def __init__(self, tgt_sr, config):
self.x_pad = config.x_pad
Expand All @@ -55,15 +80,7 @@ def __init__(self, tgt_sr, config):
self.time_step = self.window / self.sample_rate * 1000
self.device = config.device

def get_f0_crepe(
self,
x,
f0_min,
f0_max,
p_len,
hop_length,
model="full"
):
def get_f0_crepe(self, x,f0_min, f0_max, p_len, hop_length, model="full"):
x = x.astype(np.float32)
x /= np.quantile(np.abs(x), 0.999)
audio = torch.from_numpy(x).to(self.device, copy=True).unsqueeze(0)
Expand Down Expand Up @@ -97,6 +114,7 @@ def get_f0(
f0_method,
filter_radius,
hop_length,
f0_autotune,
inp_f0=None,
f0_min=50,
f0_max=1100
Expand Down Expand Up @@ -135,6 +153,12 @@ def get_f0(
del self.model_fcpe
gc.collect()


logging.info(f"f0_autotune = {f0_autotune}")
if f0_autotune == True:
f0 = Autotune.autotune_f0(self, f0)


f0 *= pow(2, pitch / 12)
tf0 = self.sample_rate // self.window
if inp_f0 is not None:
Expand Down Expand Up @@ -244,6 +268,7 @@ def pipeline(
version,
protect,
hop_length,
f0_autotune,
f0_file,
f0_min=50,
f0_max=1100
Expand Down Expand Up @@ -292,6 +317,7 @@ def pipeline(
f0_method,
filter_radius,
hop_length,
f0_autotune,
inp_f0,
f0_min,
f0_max
Expand Down

0 comments on commit 461452c

Please sign in to comment.