Skip to content

Commit

Permalink
format files with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
migperfer committed Nov 1, 2021
1 parent 6250801 commit 0631210
Show file tree
Hide file tree
Showing 19 changed files with 1,752 additions and 1,067 deletions.
7 changes: 5 additions & 2 deletions Installation/nnAudio/Spectrogram.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from .features import *
import warnings

warnings.warn("importing Spectrogram subpackage will be deprecated soon. You should import the feature extractor "
"from the feature subpackage. See actual documentation.", category=Warning)
warnings.warn(
"importing Spectrogram subpackage will be deprecated soon. You should import the feature extractor "
"from the feature subpackage. See actual documentation.",
category=Warning,
)
2 changes: 1 addition & 1 deletion Installation/nnAudio/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.6"
__version__ = "0.2.6"
2 changes: 1 addition & 1 deletion Installation/nnAudio/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
from .gammatone import *
from .griffin_lim import *
from .mel import *
from .stft import *
from .stft import *
228 changes: 157 additions & 71 deletions Installation/nnAudio/features/cfp.py

Large diffs are not rendered by default.

563 changes: 358 additions & 205 deletions Installation/nnAudio/features/cqt.py

Large diffs are not rendered by default.

67 changes: 48 additions & 19 deletions Installation/nnAudio/features/gammatone.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,24 @@ class Gammatonegram(nn.Module):
>>> specs = spec_layer(x)
"""

def __init__(self, sr=44100, n_fft=2048, n_bins=64, hop_length=512, window='hann', center=True, pad_mode='reflect',
power=2.0, htk=False, fmin=20.0, fmax=None, norm=1, trainable_bins=False, trainable_STFT=False,
verbose=True):
def __init__(
self,
sr=44100,
n_fft=2048,
n_bins=64,
hop_length=512,
window="hann",
center=True,
pad_mode="reflect",
power=2.0,
htk=False,
fmin=20.0,
fmax=None,
norm=1,
trainable_bins=False,
trainable_STFT=False,
verbose=True,
):
super(Gammatonegram, self).__init__()
self.stride = hop_length
self.center = center
Expand All @@ -64,38 +79,47 @@ def __init__(self, sr=44100, n_fft=2048, n_bins=64, hop_length=512, window='hann

# Create filter windows for stft
start = time()
wsin, wcos, self.bins2freq, _, _ = create_fourier_kernels(n_fft, freq_bins=None, window=window, freq_scale='no',
sr=sr)
wsin, wcos, self.bins2freq, _, _ = create_fourier_kernels(
n_fft, freq_bins=None, window=window, freq_scale="no", sr=sr
)

wsin = torch.tensor(wsin, dtype=torch.float)
wcos = torch.tensor(wcos, dtype=torch.float)

if trainable_STFT:
wsin = nn.Parameter(wsin, requires_grad=trainable_STFT)
wcos = nn.Parameter(wcos, requires_grad=trainable_STFT)
self.register_parameter('wsin', wsin)
self.register_parameter('wcos', wcos)
self.register_parameter("wsin", wsin)
self.register_parameter("wcos", wcos)
else:
self.register_buffer('wsin', wsin)
self.register_buffer('wcos', wcos)
self.register_buffer("wsin", wsin)
self.register_buffer("wcos", wcos)

# Creating kenral for Gammatone spectrogram
start = time()
gammatone_basis = gammatone(sr, n_fft, n_bins, fmin, fmax)
gammatone_basis = torch.tensor(gammatone_basis)

if verbose == True:
print("STFT filter created, time used = {:.4f} seconds".format(time() - start))
print("Gammatone filter created, time used = {:.4f} seconds".format(time() - start))
print(
"STFT filter created, time used = {:.4f} seconds".format(time() - start)
)
print(
"Gammatone filter created, time used = {:.4f} seconds".format(
time() - start
)
)
else:
pass
# Making everything nn.Prarmeter, so that this model can support nn.DataParallel

if trainable_bins:
gammatone_basis = nn.Parameter(gammatone_basis, requires_grad=trainable_bins)
self.register_parameter('gammatone_basis', gammatone_basis)
gammatone_basis = nn.Parameter(
gammatone_basis, requires_grad=trainable_bins
)
self.register_parameter("gammatone_basis", gammatone_basis)
else:
self.register_buffer('gammatone_basis', gammatone_basis)
self.register_buffer("gammatone_basis", gammatone_basis)

# if trainable_mel==True:
# self.mel_basis = nn.Parameter(self.mel_basis)
Expand All @@ -106,15 +130,20 @@ def __init__(self, sr=44100, n_fft=2048, n_bins=64, hop_length=512, window='hann
def forward(self, x):
x = broadcast_dim(x)
if self.center:
if self.pad_mode == 'constant':
if self.pad_mode == "constant":
padding = nn.ConstantPad1d(self.n_fft // 2, 0)
elif self.pad_mode == 'reflect':
elif self.pad_mode == "reflect":
padding = nn.ReflectionPad1d(self.n_fft // 2)

x = padding(x)

spec = torch.sqrt(conv1d(x, self.wsin, stride=self.stride).pow(2) \
+ conv1d(x, self.wcos, stride=self.stride).pow(2)) ** self.power # Doing STFT by using conv1d
spec = (
torch.sqrt(
conv1d(x, self.wsin, stride=self.stride).pow(2)
+ conv1d(x, self.wcos, stride=self.stride).pow(2)
)
** self.power
) # Doing STFT by using conv1d

gammatonespec = torch.matmul(self.gammatone_basis, spec)
return gammatonespec
return gammatonespec
86 changes: 50 additions & 36 deletions Installation/nnAudio/features/griffin_lim.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ class Griffin_Lim(nn.Module):
"""

def __init__(self,
n_fft,
n_iter=32,
hop_length=None,
win_length=None,
window='hann',
center=True,
pad_mode='reflect',
momentum=0.99,
device='cpu'):
def __init__(
self,
n_fft,
n_iter=32,
hop_length=None,
win_length=None,
window="hann",
center=True,
pad_mode="reflect",
momentum=0.99,
device="cpu",
):
super().__init__()

self.n_fft = n_fft
Expand All @@ -73,10 +75,9 @@ def __init__(self,
self.hop_length = hop_length

# Creating window function for stft and istft later
self.w = torch.tensor(get_window(window,
int(self.win_length),
fftbins=True),
device=device).float()
self.w = torch.tensor(
get_window(window, int(self.win_length), fftbins=True), device=device
).float()

def forward(self, S):
"""
Expand All @@ -88,7 +89,9 @@ def forward(self, S):
Spectrogram of the shape ``(batch, n_fft//2+1, timesteps)``
"""

assert S.dim() == 3, "Please make sure your input is in the shape of (batch, freq_bins, timesteps)"
assert (
S.dim() == 3
), "Please make sure your input is in the shape of (batch, freq_bins, timesteps)"

# Initializing Random Phase
rand_phase = torch.randn(*S.shape, device=self.device)
Expand All @@ -104,31 +107,42 @@ def forward(self, S):

# spec2wav conversion
# print(f'win_length={self.win_length}\tw={self.w.shape}')
inverse = torch.istft(S.unsqueeze(-1) * angles,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
center=self.center)
inverse = torch.istft(
S.unsqueeze(-1) * angles,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
center=self.center,
)
# wav2spec conversion
rebuilt = torch.stft(inverse,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
pad_mode=self.pad_mode)
rebuilt = torch.stft(
inverse,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
pad_mode=self.pad_mode,
)

# Phase update rule
angles[:, :, :] = rebuilt[:, :, :] - (self.momentum / (1 + self.momentum)) * tprev[:, :, :]
angles[:, :, :] = (
rebuilt[:, :, :]
- (self.momentum / (1 + self.momentum)) * tprev[:, :, :]
)

# Phase normalization
angles = angles.div(torch.sqrt(angles.pow(2).sum(-1)).unsqueeze(-1) + 1e-16) # normalizing the phase
angles = angles.div(
torch.sqrt(angles.pow(2).sum(-1)).unsqueeze(-1) + 1e-16
) # normalizing the phase

# Using the final phase to reconstruct the waveforms
inverse = torch.istft(S.unsqueeze(-1) * angles,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
center=self.center)
return inverse
inverse = torch.istft(
S.unsqueeze(-1) * angles,
self.n_fft,
self.hop_length,
win_length=self.win_length,
window=self.w,
center=self.center,
)
return inverse
Loading

0 comments on commit 0631210

Please sign in to comment.