Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
prophesier
GitHub Repository: prophesier/diff-svc
Path: blob/main/modules/hifigan/mel_utils.py
694 views
1
import numpy as np
2
import torch
3
import torch.utils.data
4
from librosa.filters import mel as librosa_mel_fn
5
from scipy.io.wavfile import read
6
7
MAX_WAV_VALUE = 32768.0
8
9
10
def load_wav(full_path):
11
sampling_rate, data = read(full_path)
12
return data, sampling_rate
13
14
15
def dynamic_range_compression(x, C=1, clip_val=1e-5):
16
return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)
17
18
19
def dynamic_range_decompression(x, C=1):
20
return np.exp(x) / C
21
22
23
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
24
return torch.log(torch.clamp(x, min=clip_val) * C)
25
26
27
def dynamic_range_decompression_torch(x, C=1):
28
return torch.exp(x) / C
29
30
31
def spectral_normalize_torch(magnitudes):
32
output = dynamic_range_compression_torch(magnitudes)
33
return output
34
35
36
def spectral_de_normalize_torch(magnitudes):
37
output = dynamic_range_decompression_torch(magnitudes)
38
return output
39
40
41
mel_basis = {}
42
hann_window = {}
43
44
45
def mel_spectrogram(y, hparams, center=False, complex=False):
46
# hop_size: 512 # For 22050Hz, 275 ~= 12.5 ms (0.0125 * sample_rate)
47
# win_size: 2048 # For 22050Hz, 1100 ~= 50 ms (If None, win_size: fft_size) (0.05 * sample_rate)
48
# fmin: 55 # Set this to 55 if your speaker is male! if female, 95 should help taking off noise. (To test depending on dataset. Pitch info: male~[65, 260], female~[100, 525])
49
# fmax: 10000 # To be increased/reduced depending on data.
50
# fft_size: 2048 # Extra window size is filled with 0 paddings to match this parameter
51
# n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax,
52
n_fft = hparams['fft_size']
53
num_mels = hparams['audio_num_mel_bins']
54
sampling_rate = hparams['audio_sample_rate']
55
hop_size = hparams['hop_size']
56
win_size = hparams['win_size']
57
fmin = hparams['fmin']
58
fmax = hparams['fmax']
59
y = y.clamp(min=-1., max=1.)
60
global mel_basis, hann_window
61
if fmax not in mel_basis:
62
mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)
63
mel_basis[str(fmax) + '_' + str(y.device)] = torch.from_numpy(mel).float().to(y.device)
64
hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
65
66
y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
67
mode='reflect')
68
y = y.squeeze(1)
69
70
spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)],
71
center=center, pad_mode='reflect', normalized=False, onesided=True)
72
73
if not complex:
74
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
75
spec = torch.matmul(mel_basis[str(fmax) + '_' + str(y.device)], spec)
76
spec = spectral_normalize_torch(spec)
77
else:
78
B, C, T, _ = spec.shape
79
spec = spec.transpose(1, 2) # [B, T, n_fft, 2]
80
return spec
81
82