File size: 1,004 Bytes
8d6ad36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations

import torch
import torchaudio

class TorchMel(torch.nn.Module):
    def __init__(self, 
                n_fft=640,
                n_mel_channels=64,
                target_sample_rate=16000,
                hop_length=160,
                win_length=640):
        super().__init__()

        self.mel_stft = torchaudio.transforms.MelSpectrogram(
                sample_rate=target_sample_rate,
                n_fft=n_fft,
                win_length=win_length,
                hop_length=hop_length,
                n_mels=n_mel_channels,
                power=1,
                center=True,
                normalized=False,
                norm=None,
            )
 
    def forward(self, waveform):
        if len(waveform.shape) == 3:
            waveform = waveform.squeeze(1)  # 'b 1 nw -> b nw'
        assert len(waveform.shape) == 2
        mel = self.mel_stft(waveform)
        mel = mel.clamp(min=1e-5).log()

        return mel[:,:,:-1].transpose(1,2)