CoolFace
Apppublic

Florii/Aesthetic_RVC

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
mel_processing.py130 linesDownload Raw Back to train
1import torch2from librosa.filters import mel as librosa_mel_fn3 4 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    return dynamic_range_compression_torch(magnitudes)28 29 30def spectral_de_normalize_torch(magnitudes):31    return dynamic_range_decompression_torch(magnitudes)32 33 34# Reusable banks35mel_basis = {}36hann_window = {}37 38 39def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):40    """Convert waveform into Linear-frequency Linear-amplitude spectrogram.41 42    Args:43        y             :: (B, T) - Audio waveforms44        n_fft45        sampling_rate46        hop_size47        win_size48        center49    Returns:50        :: (B, Freq, Frame) - Linear-frequency Linear-amplitude spectrogram51    """52    # Validation53    if torch.min(y) < -1.07:54        print("min value is ", torch.min(y))55    if torch.max(y) > 1.07:56        print("max value is ", torch.max(y))57 58    # Window - Cache if needed59    global hann_window60    dtype_device = str(y.dtype) + "_" + str(y.device)61    wnsize_dtype_device = str(win_size) + "_" + dtype_device62    if wnsize_dtype_device not in hann_window:63        hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(64            dtype=y.dtype, device=y.device65        )66 67    # Padding68    y = torch.nn.functional.pad(69        y.unsqueeze(1),70        (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),71        mode="reflect",72    )73    y = y.squeeze(1)74 75    # Complex Spectrogram :: (B, T) -> (B, Freq, Frame, RealComplex=2)76    spec = torch.stft(77        y,78        n_fft,79        hop_length=hop_size,80        win_length=win_size,81        window=hann_window[wnsize_dtype_device],82        center=center,83        pad_mode="reflect",84        normalized=False,85        onesided=True,86        return_complex=False,87    )88 89    # Linear-frequency Linear-amplitude spectrogram :: (B, Freq, Frame, RealComplex=2) -> (B, Freq, Frame)90    spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)91    return spec92 93 94def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):95    # MelBasis - Cache if needed96    global mel_basis97    dtype_device = str(spec.dtype) + "_" + str(spec.device)98    fmax_dtype_device = str(fmax) + "_" + dtype_device99    if fmax_dtype_device not in mel_basis:100        mel = librosa_mel_fn(101            sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax102        )103        mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(104            dtype=spec.dtype, device=spec.device105        )106 107    # Mel-frequency Log-amplitude spectrogram :: (B, Freq=num_mels, Frame)108    melspec = torch.matmul(mel_basis[fmax_dtype_device], spec)109    melspec = spectral_normalize_torch(melspec)110    return melspec111 112 113def mel_spectrogram_torch(114    y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False115):116    """Convert waveform into Mel-frequency Log-amplitude spectrogram.117 118    Args:119        y       :: (B, T)           - Waveforms120    Returns:121        melspec :: (B, Freq, Frame) - Mel-frequency Log-amplitude spectrogram122    """123    # Linear-frequency Linear-amplitude spectrogram :: (B, T) -> (B, Freq, Frame)124    spec = spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center)125 126    # Mel-frequency Log-amplitude spectrogram :: (B, Freq, Frame) -> (B, Freq=num_mels, Frame)127    melspec = spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax)128 129    return melspec130