Plana-Archive commited on
Commit
e601cba
·
verified ·
1 Parent(s): 076cd6b

Clean up previous raw files

Browse files
Files changed (1) hide show
  1. Zero-CPU/mel_processing.py +0 -146
Zero-CPU/mel_processing.py DELETED
@@ -1,146 +0,0 @@
1
- import torch
2
- import torch.utils.data
3
- from librosa.filters import mel as librosa_mel_fn
4
- import warnings
5
-
6
- # warnings.simplefilter(action='ignore', category=FutureWarning)
7
- warnings.filterwarnings(action="ignore")
8
- MAX_WAV_VALUE = 32768.0
9
-
10
-
11
- def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
12
- """
13
- PARAMS
14
- ------
15
- C: compression factor
16
- """
17
- return torch.log(torch.clamp(x, min=clip_val) * C)
18
-
19
-
20
- def dynamic_range_decompression_torch(x, C=1):
21
- """
22
- PARAMS
23
- ------
24
- C: compression factor used to compress
25
- """
26
- return torch.exp(x) / C
27
-
28
-
29
- def spectral_normalize_torch(magnitudes):
30
- output = dynamic_range_compression_torch(magnitudes)
31
- return output
32
-
33
-
34
- def spectral_de_normalize_torch(magnitudes):
35
- output = dynamic_range_decompression_torch(magnitudes)
36
- return output
37
-
38
-
39
- mel_basis = {}
40
- hann_window = {}
41
-
42
-
43
- def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):
44
- if torch.min(y) < -1.0:
45
- print("min value is ", torch.min(y))
46
- if torch.max(y) > 1.0:
47
- print("max value is ", torch.max(y))
48
-
49
- global hann_window
50
- dtype_device = str(y.dtype) + "_" + str(y.device)
51
- wnsize_dtype_device = str(win_size) + "_" + dtype_device
52
- if wnsize_dtype_device not in hann_window:
53
- hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
54
- dtype=y.dtype, device=y.device
55
- )
56
-
57
- y = torch.nn.functional.pad(
58
- y.unsqueeze(1),
59
- (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
60
- mode="reflect",
61
- )
62
- y = y.squeeze(1)
63
-
64
- spec = torch.stft(
65
- y,
66
- n_fft,
67
- hop_length=hop_size,
68
- win_length=win_size,
69
- window=hann_window[wnsize_dtype_device],
70
- center=center,
71
- pad_mode="reflect",
72
- normalized=False,
73
- onesided=True,
74
- return_complex=False,
75
- )
76
-
77
- spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
78
- return spec
79
-
80
-
81
- def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):
82
- global mel_basis
83
- dtype_device = str(spec.dtype) + "_" + str(spec.device)
84
- fmax_dtype_device = str(fmax) + "_" + dtype_device
85
- if fmax_dtype_device not in mel_basis:
86
- mel = librosa_mel_fn(
87
- sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
88
- )
89
- mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
90
- dtype=spec.dtype, device=spec.device
91
- )
92
- spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
93
- spec = spectral_normalize_torch(spec)
94
- return spec
95
-
96
-
97
- def mel_spectrogram_torch(
98
- y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False
99
- ):
100
- if torch.min(y) < -1.0:
101
- print("min value is ", torch.min(y))
102
- if torch.max(y) > 1.0:
103
- print("max value is ", torch.max(y))
104
-
105
- global mel_basis, hann_window
106
- dtype_device = str(y.dtype) + "_" + str(y.device)
107
- fmax_dtype_device = str(fmax) + "_" + dtype_device
108
- wnsize_dtype_device = str(win_size) + "_" + dtype_device
109
- if fmax_dtype_device not in mel_basis:
110
- mel = librosa_mel_fn(
111
- sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax
112
- )
113
- mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(
114
- dtype=y.dtype, device=y.device
115
- )
116
- if wnsize_dtype_device not in hann_window:
117
- hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(
118
- dtype=y.dtype, device=y.device
119
- )
120
-
121
- y = torch.nn.functional.pad(
122
- y.unsqueeze(1),
123
- (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),
124
- mode="reflect",
125
- )
126
- y = y.squeeze(1)
127
-
128
- spec = torch.stft(
129
- y,
130
- n_fft,
131
- hop_length=hop_size,
132
- win_length=win_size,
133
- window=hann_window[wnsize_dtype_device],
134
- center=center,
135
- pad_mode="reflect",
136
- normalized=False,
137
- onesided=True,
138
- return_complex=False,
139
- )
140
-
141
- spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
142
-
143
- spec = torch.matmul(mel_basis[fmax_dtype_device], spec)
144
- spec = spectral_normalize_torch(spec)
145
-
146
- return spec