Delete dreamvoice/src/modules/.ipynb_checkpoints
Browse files
dreamvoice/src/modules/.ipynb_checkpoints/mel-checkpoint.py
DELETED
@@ -1,37 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
import torch.nn.functional as F
|
3 |
-
import torchaudio
|
4 |
-
import torchaudio.transforms as transforms
|
5 |
-
|
6 |
-
|
7 |
-
class LogMelSpectrogram(torch.nn.Module):
|
8 |
-
def __init__(self, sr=24000, frame_length=1920, hop_length=480, n_mel=128, f_min=0, f_max=12000,):
|
9 |
-
super().__init__()
|
10 |
-
self.frame_length = frame_length
|
11 |
-
self.hop_length = hop_length
|
12 |
-
self.mel = transforms.MelSpectrogram(
|
13 |
-
sample_rate=sr,
|
14 |
-
n_fft=frame_length,
|
15 |
-
win_length=frame_length,
|
16 |
-
hop_length=hop_length,
|
17 |
-
center=False,
|
18 |
-
power=1.0,
|
19 |
-
norm="slaney",
|
20 |
-
n_mels=n_mel,
|
21 |
-
mel_scale="slaney",
|
22 |
-
f_min=f_min,
|
23 |
-
f_max=f_max
|
24 |
-
)
|
25 |
-
|
26 |
-
@torch.no_grad()
|
27 |
-
def forward(self, x, target_length=None):
|
28 |
-
x = F.pad(x, ((self.frame_length - self.hop_length) // 2,
|
29 |
-
(self.frame_length - self.hop_length) // 2), "reflect")
|
30 |
-
mel = self.mel(x)
|
31 |
-
|
32 |
-
target_length = mel.shape[-1] if target_length is None else target_length
|
33 |
-
logmel = torch.zeros(mel.shape[0], mel.shape[1], target_length).to(mel.device)
|
34 |
-
logmel[:, :, :mel.shape[2]] = mel
|
35 |
-
|
36 |
-
logmel = torch.log(torch.clamp(logmel, min=1e-5))
|
37 |
-
return logmel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|