CoolFace
Apppublic

kkvc-hf/Style-Bert-VITS2-AS2

sourceHugging Faceapache-2.0updated 11mo agoView on Hugging Face
1likes
mel_processing.py149 linesDownload Raw Back to root
1import warnings2 3import torch4import torch.utils.data5from librosa.filters import mel as librosa_mel_fn6 7 8# warnings.simplefilter(action='ignore', category=FutureWarning)9warnings.filterwarnings(action="ignore")10MAX_WAV_VALUE = 32768.011 12 13def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):14    """15    PARAMS16    ------17    C: compression factor18    """19    return torch.log(torch.clamp(x, min=clip_val) * C)20 21 22def dynamic_range_decompression_torch(x, C=1):23    """24    PARAMS25    ------26    C: compression factor used to compress27    """28    return torch.exp(x) / C29 30 31def spectral_normalize_torch(magnitudes):32    output = dynamic_range_compression_torch(magnitudes)33    return output34 35 36def spectral_de_normalize_torch(magnitudes):37    output = dynamic_range_decompression_torch(magnitudes)38    return output39 40 41mel_basis = {}42hann_window = {}43 44 45def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):46    if torch.min(y) < -1.0:47        print("min value is ", torch.min(y))48    if torch.max(y) > 1.0:49        print("max value is ", torch.max(y))50 51    global hann_window52    dtype_device = str(y.dtype) + "_" + str(y.device)53    wnsize_dtype_device = str(win_size) + "_" + dtype_device54    if wnsize_dtype_device not in hann_window:55        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(56            dtype=y.dtype, device=y.device57        )58 59    y = torch.nn.functional.pad(60        y.unsqueeze(1),61        (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),62        mode="reflect",63    )64    y = y.squeeze(1)65 66    spec = torch.stft(67        y,68        n_fft,69        hop_length=hop_size,70        win_length=win_size,71        window=hann_window[wnsize_dtype_device],72        center=center,73        pad_mode="reflect",74        normalized=False,75        onesided=True,76        return_complex=False,77    )78 79    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)80    return spec81 82 83def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):84    global mel_basis85    dtype_device = str(spec.dtype) + "_" + str(spec.device)86    fmax_dtype_device = str(fmax) + "_" + dtype_device87    if fmax_dtype_device not in mel_basis:88        mel = librosa_mel_fn(89            sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax90        )91        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(92            dtype=spec.dtype, device=spec.device93        )94    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)95    spec = spectral_normalize_torch(spec)96    return spec97 98 99def mel_spectrogram_torch(100    y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False101):102    if torch.min(y) < -1.0:103        print("min value is ", torch.min(y))104    if torch.max(y) > 1.0:105        print("max value is ", torch.max(y))106 107    global mel_basis, hann_window108    dtype_device = str(y.dtype) + "_" + str(y.device)109    fmax_dtype_device = str(fmax) + "_" + dtype_device110    wnsize_dtype_device = str(win_size) + "_" + dtype_device111    if fmax_dtype_device not in mel_basis:112        mel = librosa_mel_fn(113            sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax114        )115        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(116            dtype=y.dtype, device=y.device117        )118    if wnsize_dtype_device not in hann_window:119        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(120            dtype=y.dtype, device=y.device121        )122 123    y = torch.nn.functional.pad(124        y.unsqueeze(1),125        (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),126        mode="reflect",127    )128    y = y.squeeze(1)129 130    spec = torch.stft(131        y,132        n_fft,133        hop_length=hop_size,134        win_length=win_size,135        window=hann_window[wnsize_dtype_device],136        center=center,137        pad_mode="reflect",138        normalized=False,139        onesided=True,140        return_complex=False,141    )142 143    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)144 145    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)146    spec = spectral_normalize_torch(spec)147 148    return spec149