|
from typing import Any |
|
from typing import Dict |
|
from typing import Optional |
|
from typing import Tuple |
|
|
|
import torch |
|
from typeguard import check_argument_types |
|
|
|
from espnet2.layers.stft import Stft |
|
from espnet2.tts.feats_extract.abs_feats_extract import AbsFeatsExtract |
|
|
|
|
|
class LogSpectrogram(AbsFeatsExtract): |
|
"""Conventional frontend structure for ASR |
|
|
|
Stft -> log-amplitude-spec |
|
""" |
|
|
|
def __init__( |
|
self, |
|
n_fft: int = 1024, |
|
win_length: int = None, |
|
hop_length: int = 256, |
|
window: Optional[str] = "hann", |
|
center: bool = True, |
|
normalized: bool = False, |
|
onesided: bool = True, |
|
): |
|
assert check_argument_types() |
|
super().__init__() |
|
self.n_fft = n_fft |
|
self.hop_length = hop_length |
|
self.win_length = win_length |
|
self.window = window |
|
self.stft = Stft( |
|
n_fft=n_fft, |
|
win_length=win_length, |
|
hop_length=hop_length, |
|
window=window, |
|
center=center, |
|
normalized=normalized, |
|
onesided=onesided, |
|
) |
|
self.n_fft = n_fft |
|
|
|
def output_size(self) -> int: |
|
return self.n_fft // 2 + 1 |
|
|
|
def get_parameters(self) -> Dict[str, Any]: |
|
"""Return the parameters required by Vocoder""" |
|
return dict( |
|
n_fft=self.n_fft, |
|
n_shift=self.hop_length, |
|
win_length=self.win_length, |
|
window=self.window, |
|
) |
|
|
|
def forward( |
|
self, input: torch.Tensor, input_lengths: torch.Tensor = None |
|
) -> Tuple[torch.Tensor, torch.Tensor]: |
|
|
|
input_stft, feats_lens = self.stft(input, input_lengths) |
|
|
|
assert input_stft.dim() >= 4, input_stft.shape |
|
|
|
assert input_stft.shape[-1] == 2, input_stft.shape |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input_power = input_stft[..., 0] ** 2 + input_stft[..., 1] ** 2 |
|
log_amp = 0.5 * torch.log10(torch.clamp(input_power, min=1.0e-10)) |
|
return log_amp, feats_lens |
|
|