|
| 1 | +__all__ = ["LogMelFeatureExtractionV1", "LogMelFeatureExtractionV1Config"] |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Optional, Tuple |
| 5 | + |
| 6 | +from librosa import filters |
| 7 | +import torch |
| 8 | +from torch import nn |
| 9 | + |
| 10 | +from i6_models.config import ModelConfiguration |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class LogMelFeatureExtractionV1Config(ModelConfiguration): |
| 15 | + """ |
| 16 | + Attributes: |
| 17 | + sample_rate: audio sample rate in Hz |
| 18 | + win_size: window size in seconds |
| 19 | + hop_size: window shift in seconds |
| 20 | + f_min: minimum filter frequency in Hz |
| 21 | + f_max: maximum filter frequency in Hz |
| 22 | + min_amp: minimum amplitude for safe log |
| 23 | + num_filters: number of mel windows |
| 24 | + center: centered STFT with automatic padding |
| 25 | + """ |
| 26 | + |
| 27 | + sample_rate: int |
| 28 | + win_size: float |
| 29 | + hop_size: float |
| 30 | + f_min: int |
| 31 | + f_max: int |
| 32 | + min_amp: float |
| 33 | + num_filters: int |
| 34 | + center: bool |
| 35 | + n_fft: Optional[int] = None |
| 36 | + |
| 37 | + def __post_init__(self) -> None: |
| 38 | + super().__post_init__() |
| 39 | + assert self.f_max <= self.sample_rate // 2, "f_max can not be larger than half of the sample rate" |
| 40 | + assert self.f_min > 0 and self.f_max > 0 and self.sample_rate > 0, "frequencies need to be positive" |
| 41 | + assert self.win_size > 0 and self.hop_size > 0, "window settings need to be positive" |
| 42 | + assert self.num_filters > 0, "number of filters needs to be positive" |
| 43 | + assert self.hop_size <= self.win_size, "using a larger hop size than window size does not make sense" |
| 44 | + if self.n_fft is None: |
| 45 | + # if n_fft is not given, set n_fft to the window size (in samples) |
| 46 | + self.n_fft = int(self.win_size * self.sample_rate) |
| 47 | + else: |
| 48 | + assert self.n_fft >= self.win_size * self.sample_rate, "n_fft cannot to be smaller than the window size" |
| 49 | + |
| 50 | + |
| 51 | +class LogMelFeatureExtractionV1(nn.Module): |
| 52 | + """ |
| 53 | + Librosa-compatible log-mel feature extraction using log10. Does not use torchaudio. |
| 54 | +
|
| 55 | + Using it wrapped with torch.no_grad() is recommended if no gradient is needed |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__(self, cfg: LogMelFeatureExtractionV1Config): |
| 59 | + super().__init__() |
| 60 | + self.register_buffer("n_fft", torch.tensor(cfg.n_fft)) |
| 61 | + self.register_buffer("window", torch.hann_window(int(cfg.win_size * cfg.sample_rate))) |
| 62 | + self.register_buffer("hop_length", torch.tensor(int(cfg.hop_size * cfg.sample_rate))) |
| 63 | + self.register_buffer("min_amp", torch.tensor(cfg.min_amp)) |
| 64 | + self.center = cfg.center |
| 65 | + self.register_buffer( |
| 66 | + "mel_basis", |
| 67 | + torch.tensor( |
| 68 | + filters.mel( |
| 69 | + sr=cfg.sample_rate, |
| 70 | + n_fft=int(cfg.sample_rate * cfg.win_size), |
| 71 | + n_mels=cfg.num_filters, |
| 72 | + fmin=cfg.f_min, |
| 73 | + fmax=cfg.f_max, |
| 74 | + ) |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + def forward(self, raw_audio, length) -> Tuple[torch.Tensor, torch.Tensor]: |
| 79 | + """ |
| 80 | + :param raw_audio: [B, T] |
| 81 | + :param length in samples: [B] |
| 82 | + :return features as [B,T,F] and length in frames [B] |
| 83 | + """ |
| 84 | + power_spectrum = ( |
| 85 | + torch.abs( |
| 86 | + torch.stft( |
| 87 | + raw_audio, |
| 88 | + n_fft=self.n_fft, |
| 89 | + hop_length=self.hop_length, |
| 90 | + window=self.window, |
| 91 | + center=self.center, |
| 92 | + pad_mode="constant", |
| 93 | + return_complex=True, |
| 94 | + ) |
| 95 | + ) |
| 96 | + ** 2 |
| 97 | + ) |
| 98 | + if len(power_spectrum.size()) == 2: |
| 99 | + # For some reason torch.stft removes the batch axis for batch sizes of 1, so we need to add it again |
| 100 | + power_spectrum = torch.unsqueeze(power_spectrum, 0) |
| 101 | + melspec = torch.einsum("...ft,mf->...mt", power_spectrum, self.mel_basis) |
| 102 | + log_melspec = torch.log10(torch.max(self.min_amp, melspec)) |
| 103 | + feature_data = torch.transpose(log_melspec, 1, 2) |
| 104 | + |
| 105 | + if self.center: |
| 106 | + length = (length // self.hop_length) + 1 |
| 107 | + else: |
| 108 | + length = ((length - self.n_fft) // self.hop_length) + 1 |
| 109 | + |
| 110 | + return feature_data, length.int() |
0 commit comments