Spaces:
Runtime error
Runtime error
import numpy as np | |
import pyworld | |
from scipy.io import wavfile | |
class FeatureInput(object): | |
def __init__(self, samplerate=16000, hop_size=160): | |
self.fs = samplerate | |
self.hop = hop_size | |
self.f0_bin = 256 | |
self.f0_max = 1100.0 | |
self.f0_min = 50.0 | |
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700) | |
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700) | |
def compute_f0(self, audio, sr): | |
x, sr = audio, self.fs | |
assert sr == self.fs | |
f0, t = pyworld.dio( | |
x.astype(np.double), | |
fs=sr, | |
f0_ceil=800, | |
frame_period=1000 * self.hop / sr, | |
) | |
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs) | |
for index, pitch in enumerate(f0): | |
f0[index] = round(pitch, 1) | |
return f0 | |
# for numpy # code from diffsinger | |
def coarse_f0(self, f0): | |
f0_mel = 1127 * np.log(1 + f0 / 700) | |
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * ( | |
self.f0_bin - 2 | |
) / (self.f0_mel_max - self.f0_mel_min) + 1 | |
# use 0 or 1 | |
f0_mel[f0_mel <= 1] = 1 | |
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1 | |
f0_coarse = np.rint(f0_mel).astype(np.int) | |
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, ( | |
f0_coarse.max(), | |
f0_coarse.min(), | |
) | |
return f0_coarse | |
# for tensor # code from diffsinger | |
def coarse_f0_ts(self, f0): | |
f0_mel = 1127 * (1 + f0 / 700).log() | |
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * ( | |
self.f0_bin - 2 | |
) / (self.f0_mel_max - self.f0_mel_min) + 1 | |
# use 0 or 1 | |
f0_mel[f0_mel <= 1] = 1 | |
f0_mel[f0_mel > self.f0_bin - 1] = self.f0_bin - 1 | |
f0_coarse = (f0_mel + 0.5).long() | |
assert f0_coarse.max() <= 255 and f0_coarse.min() >= 1, ( | |
f0_coarse.max(), | |
f0_coarse.min(), | |
) | |
return f0_coarse | |
def save_wav(self, wav, path): | |
wav *= 32767 / max(0.01, np.max(np.abs(wav))) * 0.6 | |
wavfile.write(path, self.fs, wav.astype(np.int16)) | |