cymic/VITS-Tokaiteio
3
1import math2import os3import random4import torch5from torch import nn6import torch.nn.functional as F7import torch.utils.data8import numpy as np9 10import logging11 12numba_logger = logging.getLogger('numba')13numba_logger.setLevel(logging.WARNING)14import warnings15warnings.filterwarnings('ignore')16import librosa17import librosa.util as librosa_util18from librosa.util import normalize, pad_center, tiny19from scipy.signal import get_window20from scipy.io.wavfile import read21from librosa.filters import mel as librosa_mel_fn22 23MAX_WAV_VALUE = 32768.024 25 26def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):27 """28 PARAMS29 ------30 C: compression factor31 """32 return torch.log(torch.clamp(x, min=clip_val) * C)33 34 35def dynamic_range_decompression_torch(x, C=1):36 """37 PARAMS38 ------39 C: compression factor used to compress40 """41 return torch.exp(x) / C42 43 44def spectral_normalize_torch(magnitudes):45 output = dynamic_range_compression_torch(magnitudes)46 return output47 48 49def spectral_de_normalize_torch(magnitudes):50 output = dynamic_range_decompression_torch(magnitudes)51 return output52 53 54mel_basis = {}55hann_window = {}56 57 58def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):59 if torch.min(y) < -1.:60 print('min value is ', torch.min(y))61 if torch.max(y) > 1.:62 print('max value is ', torch.max(y))63 64 global hann_window65 dtype_device = str(y.dtype) + '_' + str(y.device)66 wnsize_dtype_device = str(win_size) + '_' + dtype_device67 if wnsize_dtype_device not in hann_window:68 hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)69 70 y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')71 y = y.squeeze(1)72 73 spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],74 center=center, pad_mode='reflect', normalized=False, onesided=True)75 76 spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)77 return spec78 79 80def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):81 global mel_basis82 dtype_device = str(spec.dtype) + '_' + str(spec.device)83 fmax_dtype_device = str(fmax) + '_' + 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=spec.dtype, device=spec.device)87 spec = torch.matmul(mel_basis[fmax_dtype_device], spec)88 spec = spectral_normalize_torch(spec)89 return spec90 91 92def mel_spectrogram_torch(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False):93 if torch.min(y) < -1.:94 print('min value is ', torch.min(y))95 if torch.max(y) > 1.:96 print('max value is ', torch.max(y))97 98 global mel_basis, hann_window99 dtype_device = str(y.dtype) + '_' + str(y.device)100 fmax_dtype_device = str(fmax) + '_' + dtype_device101 wnsize_dtype_device = str(win_size) + '_' + dtype_device102 if fmax_dtype_device not in mel_basis:103 mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)104 mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=y.dtype, device=y.device)105 if wnsize_dtype_device not in hann_window:106 hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)107 108 y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect')109 y = y.squeeze(1)110 111 spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device],112 center=center, pad_mode='reflect', normalized=False, onesided=True)113 114 spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)115 116 spec = torch.matmul(mel_basis[fmax_dtype_device], spec)117 spec = spectral_normalize_torch(spec)118 119 return spec120 