CoolFace
Apppublic

DD0101/VITS

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
mel_processing.py113 linesDownload Raw Back to root
1import math2import os3import random4import torch5from torch import nn6import torch.nn.functional as F7import torch.utils.data8import numpy as np9import librosa10import librosa.util as librosa_util11from librosa.util import normalize, pad_center, tiny12from scipy.signal import get_window13from scipy.io.wavfile import read14from librosa.filters import mel as librosa_mel_fn15 16MAX_WAV_VALUE = 32768.017 18 19def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):20    """21    PARAMS22    ------23    C: compression factor24    """25    return torch.log(torch.clamp(x, min=clip_val) * C)26 27 28def dynamic_range_decompression_torch(x, C=1):29    """30    PARAMS31    ------32    C: compression factor used to compress33    """34    return torch.exp(x) / C35 36 37def spectral_normalize_torch(magnitudes):38    output = dynamic_range_compression_torch(magnitudes)39    return output40 41 42def spectral_de_normalize_torch(magnitudes):43    output = dynamic_range_decompression_torch(magnitudes)44    return output45 46 47mel_basis = {}48hann_window = {}49 50 51def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):52    if torch.min(y) < -1.:53        print('min value is ', torch.min(y))54    if torch.max(y) > 1.:55        print('max value is ', torch.max(y))56 57    global hann_window58    dtype_device = str(y.dtype) + '_' + str(y.device)59    wnsize_dtype_device = str(win_size) + '_' + dtype_device60    if wnsize_dtype_device not in hann_window:61        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)62 63    y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')64    y = y.squeeze(1)65 66    spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],67                      center=center, pad_mode='reflect', normalized=False, onesided=True)68 69    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)70    return spec71 72 73def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):74    global mel_basis75    dtype_device = str(spec.dtype) + '_' + str(spec.device)76    fmax_dtype_device = str(fmax) + '_' + dtype_device77    if fmax_dtype_device not in mel_basis:78        mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)79        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=spec.dtype, device=spec.device)80    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)81    spec = spectral_normalize_torch(spec)82    return spec83 84 85def mel_spectrogram_torch(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):86    if torch.min(y) < -1.:87        print('min value is ', torch.min(y))88    if torch.max(y) > 1.:89        print('max value is ', torch.max(y))90 91    global mel_basis, hann_window92    dtype_device = str(y.dtype) + '_' + str(y.device)93    fmax_dtype_device = str(fmax) + '_' + dtype_device94    wnsize_dtype_device = str(win_size) + '_' + dtype_device95    if fmax_dtype_device not in mel_basis:96        mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)97        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=y.dtype, device=y.device)98    if wnsize_dtype_device not in hann_window:99        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)100 101    y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')102    y = y.squeeze(1)103 104    spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],105                      center=center, pad_mode='reflect', normalized=False, onesided=True)106 107    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)108 109    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)110    spec = spectral_normalize_torch(spec)111 112    return spec113