CoolFace
Apppublic

Ikaros521/moe-tts

sourceHugging Facemitupdated 3y agoView on Hugging Face
1likes
mel_processing.py102 linesDownload Raw Back to root
1import torch2import torch.utils.data3from librosa.filters import mel as librosa_mel_fn4 5MAX_WAV_VALUE = 32768.06 7 8def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):9    """10    PARAMS11    ------12    C: compression factor13    """14    return torch.log(torch.clamp(x, min=clip_val) * C)15 16 17def dynamic_range_decompression_torch(x, C=1):18    """19    PARAMS20    ------21    C: compression factor used to compress22    """23    return torch.exp(x) / C24 25 26def spectral_normalize_torch(magnitudes):27    output = dynamic_range_compression_torch(magnitudes)28    return output29 30 31def spectral_de_normalize_torch(magnitudes):32    output = dynamic_range_decompression_torch(magnitudes)33    return output34 35 36mel_basis = {}37hann_window = {}38 39 40def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):41    if torch.min(y) < -1.:42        print('min value is ', torch.min(y))43    if torch.max(y) > 1.:44        print('max value is ', torch.max(y))45 46    global hann_window47    dtype_device = str(y.dtype) + '_' + str(y.device)48    wnsize_dtype_device = str(win_size) + '_' + dtype_device49    if wnsize_dtype_device not in hann_window:50        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)51 52    y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')53    y = y.squeeze(1)54 55    spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],56                      center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=False)57 58    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)59    return spec60 61 62def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):63    global mel_basis64    dtype_device = str(spec.dtype) + '_' + str(spec.device)65    fmax_dtype_device = str(fmax) + '_' + dtype_device66    if fmax_dtype_device not in mel_basis:67        mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)68        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=spec.dtype, device=spec.device)69    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)70    spec = spectral_normalize_torch(spec)71    return spec72 73 74def mel_spectrogram_torch(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):75    if torch.min(y) < -1.:76        print('min value is ', torch.min(y))77    if torch.max(y) > 1.:78        print('max value is ', torch.max(y))79 80    global mel_basis, hann_window81    dtype_device = str(y.dtype) + '_' + str(y.device)82    fmax_dtype_device = str(fmax) + '_' + dtype_device83    wnsize_dtype_device = str(win_size) + '_' + dtype_device84    if fmax_dtype_device not in mel_basis:85        mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)86        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=y.dtype, device=y.device)87    if wnsize_dtype_device not in hann_window:88        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)89 90    y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')91    y = y.squeeze(1)92 93    spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],94                      center=center, pad_mode='reflect', normalized=False, onesided=True)95 96    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)97 98    spec = torch.matmul(mel_basis[fmax_dtype_device], spec)99    spec = spectral_normalize_torch(spec)100 101    return spec102