File size: 8,182 Bytes
a03c9b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright 2024 The YourMT3 Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Please see the details in the LICENSE file.
"""spectrogram.py"""
import importlib
from typing import Optional, Literal, Dict, Tuple
from packaging.version import parse as VersionParse

import torch
import torch.nn as nn
from einops import rearrange
from model.ops import minmax_normalize
from config.config import audio_cfg as default_audio_cfg
"""
Example usage:

# MT3 setup
>>> hop = 8 ms or 128 samples 
>>> melspec = Melspectrogram(sample_rate=16000, n_fft=2048, hop_length=128,
                            f_min=50, f_max=8000, n_mels=512)
>>> x = torch.randn(2, 1, 32767) # (B, C=1, T): 2.048 s
>>> y = melspec(x) # (2, 256, 512) (B, T, F)

# PerceiverTF-like setup
>>> hop = 18.75 ms or 300 samples
>>> spec = Spectrogram(n_fft=2048, hop_length=300)
                            )
>>> x = torch.randn(2, 1, 95999) # (B, C=1, T): 6.000 s
>>> y = spec(x) # (2, 320, 1024) (B, T, F)

# Hybrid setup (2.048 seconds segment and spectrogram with hop=300)
>>> hop = 18.75 ms or 300 samples
>>> spec = Spectrogram(n_fft=2048, hop_length=300)
>>> x = torch.randn(2, 1, 32767) # (B, C=1, T): 2.048 s
>>> y = spec(x) # (2, 110, 1024) (B, T, F)

# PerceiverTF-like setup, hop=256
>>> hop = 16 ms or 256 samples
>>> spec256 = Spectrogram(sample_rate=16000, n_fft=2048, hop_length=256,
                                f_min=20, f_max=8000, n_mels=256)
>>> x = torch.randn(2, 1, 32767) # (B, C=1, T): 2.048 s
>>> y = spec256(x) # (2, 128, 1024) (B, T, F)
"""


def optional_compiler_disable(func):
    if VersionParse(torch.__version__) >= VersionParse("2.1"):
        # If the version is 2.1 or higher, apply the torch.compiler.disable decorator.
        return torch.compiler.disable(func)
    else:
        # If the version is below 2.1, return the original function.
        return func


# -------------------------------------------------------------------------------------
# Log-Mel spectrogram
# -------------------------------------------------------------------------------------
class Melspectrogram(nn.Module):

    def __init__(
        self,
        audio_backend: Literal['torchaudio', 'nnaudio'] = 'torchaudio',
        sample_rate: int = 16000,
        n_fft: int = 2048,
        hop_length: int = 128,
        f_min: int = 50,  # 20 Hz in the MT3 paper, but we can only use 20 Hz with nnAudio
        f_max: Optional[int] = 8000,
        n_mels: int = 512,
        eps: float = 1e-5,
        **kwargs,
    ):
        """
        Log-Melspectrogram

        Args:
            audio_backend (str): 'torchaudio' or 'nnaudio'
            sample_rate (int): sample rate in Hz
            n_fft (int): FFT window size
            hop_length (int): hop length in samples
            f_min (int): minimum frequency in Hz
            f_max (int): maximum frequency in Hz
            n_mels (int): number of mel frequency bins
            eps (float): epsilon for numerical stability

        """
        super(Melspectrogram, self).__init__()
        self.audio_backend = audio_backend.lower()

        if audio_backend.lower() == 'torchaudio':
            torchaudio = importlib.import_module('torchaudio')
            self.mel_stft = torchaudio.transforms.MelSpectrogram(
                sample_rate=sample_rate,
                n_fft=n_fft,
                hop_length=hop_length,
                f_min=f_min,
                f_max=f_max,
                n_mels=n_mels,
            )
        elif audio_backend.lower() == 'nnaudio':
            nnaudio = importlib.import_module('nnAudio.features')
            self.mel_stft_nnaudio = nnaudio.mel.MelSpectrogram(
                sr=sample_rate,
                win_length=n_fft,
                n_mels=n_mels,
                hop_length=hop_length,
                fmin=20,  #f_min,
                fmax=f_max)
        else:
            raise NotImplementedError(audio_backend)
        self.eps = eps

    @optional_compiler_disable
    def forward(self, x: torch.Tensor) -> torch.Tensor:  # (B, 1, T)
        """
        Args:
            x (torch.Tensor): (B, 1, T)
            
        Returns:
            torch.Tensor: (B, T, F)
        
        """
        if self.audio_backend == 'torchaudio':
            x = self.mel_stft(x)  # (B, 1, F, T)
            x = rearrange(x, 'b 1 f t -> b t f')
            x = minmax_normalize(torch.log(x + self.eps))
            # some versions of torchaudio returns nan when input is all-zeros
            return torch.nan_to_num(x)

        elif self.audio_backend == 'nnaudio':
            x = self.mel_stft_nnaudio(x)  # (B, F, T)
            x = rearrange(x, 'b f t -> b t f')
            x = minmax_normalize(torch.log(x + self.eps))
            return x


# -------------------------------------------------------------------------------------
# Log-spectrogram
# -------------------------------------------------------------------------------------
class Spectrogram(nn.Module):

    def __init__(
        self,
        audio_backend: Literal['torchaudio', 'nnaudio'] = 'torchaudio',
        n_fft: int = 2048,
        hop_length: int = 128,
        eps: float = 1e-5,
        **kwargs,
    ):
        """
        Log-Magnitude Spectrogram 

        Args:
            audio_backend (str): 'torchaudio' or 'nnaudio'
            n_fft (int): FFT window size, creates n_fft // 2 + 1 freq-bins
            hop_length (int): hop length in samples
            eps (float): epsilon for numerical stability

        """
        super(Spectrogram, self).__init__()
        self.audio_backend = audio_backend.lower()

        if audio_backend.lower() == 'torchaudio':
            torchaudio = importlib.import_module('torchaudio')
            self.stft = torchaudio.transforms.Spectrogram(n_fft=n_fft,
                                                          hop_length=hop_length,
                                                          window_fn=torch.hann_window,
                                                          power=1.)  # (B, 1, F, T), remove DC component
        elif audio_backend.lower() == 'nnaudio':
            # TODO: nnAudio spectrogram
            raise NotImplementedError(audio_backend)
        else:
            raise NotImplementedError(audio_backend)
        self.eps = eps

    @optional_compiler_disable
    def forward(self, x: torch.Tensor) -> torch.Tensor:  # (B, 1, T)
        """
        Args:
            x (torch.Tensor): (B, 1, T)
            
        Returns:
            torch.Tensor: (B, T, F)
        
        """
        if self.audio_backend == 'torchaudio':
            x = self.stft(x)[:, :, 1:, :]  # (B, 1, F, T) remove DC component
            x = rearrange(x, 'b 1 f t -> b t f')
            x = minmax_normalize(torch.log(x + self.eps))
            return torch.nan_to_num(x)  # some versions of torchaudio returns nan when input is all-zeros
        elif self.audio_backend == 'nnaudio':
            raise NotImplementedError(self.audio_backend)


def get_spectrogram_layer_from_audio_cfg(audio_cfg: Optional[Dict] = None) -> Tuple[nn.Module, Tuple[int]]:
    """Get mel-/spectrogram layer from config.
    - Used by 'ymt3' to create a spectrogram layer.
    - Returns output shape of spectrogram layer, which is used to determine input shape of model.
    
    Args:
        audio_cfg (dict): see config/config.py

    Returns:
        layer (nn.Module): mel-/spectrogram layer
        output_shape (tuple): inferred output shape of layer excluding batch dim. (T, F)
    """
    if audio_cfg is None:
        audio_cfg = default_audio_cfg

    if audio_cfg['codec'] == 'melspec':
        layer = Melspectrogram(**audio_cfg)
    elif audio_cfg['codec'] == 'spec':
        layer = Spectrogram(**audio_cfg)
    else:
        raise NotImplementedError(audio_cfg['codec'])

    # Infer output shape of the spectrogram layer
    with torch.no_grad():
        output_shape = layer(torch.randn(1, 1, audio_cfg['input_frames'])).shape[1:]
    return layer, output_shape