forked from sigsep/open-unmix-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transforms.py
216 lines (178 loc) · 6.47 KB
/
transforms.py
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
from typing import Optional
import torch
import torchaudio
from torch import Tensor
import torch.nn as nn
try:
from asteroid_filterbanks.enc_dec import Encoder, Decoder
from asteroid_filterbanks.transforms import to_torchaudio, from_torchaudio
from asteroid_filterbanks import torch_stft_fb
except ImportError:
pass
def make_filterbanks(n_fft=4096, n_hop=1024, center=False, sample_rate=44100.0, method="torch"):
window = nn.Parameter(torch.hann_window(n_fft), requires_grad=False)
if method == "torch":
encoder = TorchSTFT(n_fft=n_fft, n_hop=n_hop, window=window, center=center)
decoder = TorchISTFT(n_fft=n_fft, n_hop=n_hop, window=window, center=center)
elif method == "asteroid":
fb = torch_stft_fb.TorchSTFTFB.from_torch_args(
n_fft=n_fft,
hop_length=n_hop,
win_length=n_fft,
window=window,
center=center,
sample_rate=sample_rate,
)
encoder = AsteroidSTFT(fb)
decoder = AsteroidISTFT(fb)
else:
raise NotImplementedError
return encoder, decoder
class AsteroidSTFT(nn.Module):
def __init__(self, fb):
super(AsteroidSTFT, self).__init__()
self.enc = Encoder(fb)
def forward(self, x):
aux = self.enc(x)
return to_torchaudio(aux)
class AsteroidISTFT(nn.Module):
def __init__(self, fb):
super(AsteroidISTFT, self).__init__()
self.dec = Decoder(fb)
def forward(self, X: Tensor, length: Optional[int] = None) -> Tensor:
aux = from_torchaudio(X)
return self.dec(aux, length=length)
class TorchSTFT(nn.Module):
"""Multichannel Short-Time-Fourier Forward transform
uses hard coded hann_window.
Args:
n_fft (int, optional): transform FFT size. Defaults to 4096.
n_hop (int, optional): transform hop size. Defaults to 1024.
center (bool, optional): If True, the signals first window is
zero padded. Centering is required for a perfect
reconstruction of the signal. However, during training
of spectrogram models, it can safely turned off.
Defaults to `true`
window (nn.Parameter, optional): window function
"""
def __init__(
self,
n_fft: int = 4096,
n_hop: int = 1024,
center: bool = False,
window: Optional[nn.Parameter] = None,
):
super(TorchSTFT, self).__init__()
if window is None:
self.window = nn.Parameter(torch.hann_window(n_fft), requires_grad=False)
else:
self.window = window
self.n_fft = n_fft
self.n_hop = n_hop
self.center = center
def forward(self, x: Tensor) -> Tensor:
"""STFT forward path
Args:
x (Tensor): audio waveform of
shape (nb_samples, nb_channels, nb_timesteps)
Returns:
STFT (Tensor): complex stft of
shape (nb_samples, nb_channels, nb_bins, nb_frames, complex=2)
last axis is stacked real and imaginary
"""
shape = x.size()
nb_samples, nb_channels, nb_timesteps = shape
# pack batch
x = x.view(-1, shape[-1])
complex_stft = torch.stft(
x,
n_fft=self.n_fft,
hop_length=self.n_hop,
window=self.window,
center=self.center,
normalized=False,
onesided=True,
pad_mode="reflect",
return_complex=True,
)
stft_f = torch.view_as_real(complex_stft)
# unpack batch
stft_f = stft_f.view(shape[:-1] + stft_f.shape[-3:])
return stft_f
class TorchISTFT(nn.Module):
"""Multichannel Inverse-Short-Time-Fourier functional
wrapper for torch.istft to support batches
Args:
STFT (Tensor): complex stft of
shape (nb_samples, nb_channels, nb_bins, nb_frames, complex=2)
last axis is stacked real and imaginary
n_fft (int, optional): transform FFT size. Defaults to 4096.
n_hop (int, optional): transform hop size. Defaults to 1024.
window (callable, optional): window function
center (bool, optional): If True, the signals first window is
zero padded. Centering is required for a perfect
reconstruction of the signal. However, during training
of spectrogram models, it can safely turned off.
Defaults to `true`
length (int, optional): audio signal length to crop the signal
Returns:
x (Tensor): audio waveform of
shape (nb_samples, nb_channels, nb_timesteps)
"""
def __init__(
self,
n_fft: int = 4096,
n_hop: int = 1024,
center: bool = False,
sample_rate: float = 44100.0,
window: Optional[nn.Parameter] = None,
) -> None:
super(TorchISTFT, self).__init__()
self.n_fft = n_fft
self.n_hop = n_hop
self.center = center
self.sample_rate = sample_rate
if window is None:
self.window = nn.Parameter(torch.hann_window(n_fft), requires_grad=False)
else:
self.window = window
def forward(self, X: Tensor, length: Optional[int] = None) -> Tensor:
shape = X.size()
X = X.reshape(-1, shape[-3], shape[-2], shape[-1])
y = torch.istft(
torch.view_as_complex(X),
n_fft=self.n_fft,
hop_length=self.n_hop,
window=self.window,
center=self.center,
normalized=False,
onesided=True,
length=length,
)
y = y.reshape(shape[:-3] + y.shape[-1:])
return y
class ComplexNorm(nn.Module):
r"""Compute the norm of complex tensor input.
Extension of `torchaudio.functional.complex_norm` with mono
Args:
mono (bool): Downmix to single channel after applying power norm
to maximize
"""
def __init__(self, mono: bool = False):
super(ComplexNorm, self).__init__()
self.mono = mono
def forward(self, spec: Tensor) -> Tensor:
"""
Args:
spec: complex_tensor (Tensor): Tensor shape of
`(..., complex=2)`
Returns:
Tensor: Power/Mag of input
`(...,)`
"""
# take the magnitude
spec = torch.abs(torch.view_as_complex(spec))
# downmix in the mag domain to preserve energy
if self.mono:
spec = torch.mean(spec, 1, keepdim=True)
return spec