CoolFace
Apppublic

Clicko777/RVC_HFv2

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
mel_processing.py131 linesDownload Raw Back to train
1import torch2import torch.utils.data3from librosa.filters import mel as librosa_mel_fn4 5 6MAX_WAV_VALUE = 32768.07 8 9def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):10    """11    PARAMS12    ------13    C: compression factor14    """15    return torch.log(torch.clamp(x, min=clip_val) * C)16 17 18def dynamic_range_decompression_torch(x, C=1):19    """20    PARAMS21    ------22    C: compression factor used to compress23    """24    return torch.exp(x) / C25 26 27def spectral_normalize_torch(magnitudes):28    return dynamic_range_compression_torch(magnitudes)29 30 31def spectral_de_normalize_torch(magnitudes):32    return dynamic_range_decompression_torch(magnitudes)33 34 35# Reusable banks36mel_basis = {}37hann_window = {}38 39 40def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):41    """Convert waveform into Linear-frequency Linear-amplitude spectrogram.42 43    Args:44        y             :: (B, T) - Audio waveforms45        n_fft46        sampling_rate47        hop_size48        win_size49        center50    Returns:51        :: (B, Freq, Frame) - Linear-frequency Linear-amplitude spectrogram52    """53    # Validation54    if torch.min(y) < -1.07:55        print("min value is ", torch.min(y))56    if torch.max(y) > 1.07:57        print("max value is ", torch.max(y))58 59    # Window - Cache if needed60    global hann_window61    dtype_device = str(y.dtype) + "_" + str(y.device)62    wnsize_dtype_device = str(win_size) + "_" + dtype_device63    if wnsize_dtype_device not in hann_window:64        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(65            dtype=y.dtype, device=y.device66        )67 68    # Padding69    y = torch.nn.functional.pad(70        y.unsqueeze(1),71        (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),72        mode="reflect",73    )74    y = y.squeeze(1)75 76    # Complex Spectrogram :: (B, T) -> (B, Freq, Frame, RealComplex=2)77    spec = torch.stft(78        y,79        n_fft,80        hop_length=hop_size,81        win_length=win_size,82        window=hann_window[wnsize_dtype_device],83        center=center,84        pad_mode="reflect",85        normalized=False,86        onesided=True,87        return_complex=False,88    )89 90    # Linear-frequency Linear-amplitude spectrogram :: (B, Freq, Frame, RealComplex=2) -> (B, Freq, Frame)91    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)92    return spec93 94 95def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):96    # MelBasis - Cache if needed97    global mel_basis98    dtype_device = str(spec.dtype) + "_" + str(spec.device)99    fmax_dtype_device = str(fmax) + "_" + dtype_device100    if fmax_dtype_device not in mel_basis:101        mel = librosa_mel_fn(102            sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax103        )104        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(105            dtype=spec.dtype, device=spec.device106        )107 108    # Mel-frequency Log-amplitude spectrogram :: (B, Freq=num_mels, Frame)109    melspec = torch.matmul(mel_basis[fmax_dtype_device], spec)110    melspec = spectral_normalize_torch(melspec)111    return melspec112 113 114def mel_spectrogram_torch(115    y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False116):117    """Convert waveform into Mel-frequency Log-amplitude spectrogram.118 119    Args:120        y       :: (B, T)           - Waveforms121    Returns:122        melspec :: (B, Freq, Frame) - Mel-frequency Log-amplitude spectrogram123    """124    # Linear-frequency Linear-amplitude spectrogram :: (B, T) -> (B, Freq, Frame)125    spec = spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center)126 127    # Mel-frequency Log-amplitude spectrogram :: (B, Freq, Frame) -> (B, Freq=num_mels, Frame)128    melspec = spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax)129 130    return melspec131