Eddycrack864 commited on
Commit
8573118
1 Parent(s): 1af1682

Upload 38 files

Browse files
Files changed (38) hide show
  1. lib_v5/mdxnet.py +136 -0
  2. lib_v5/mixer.ckpt +3 -0
  3. lib_v5/modules.py +74 -0
  4. lib_v5/pyrb.py +92 -0
  5. lib_v5/results.py +48 -0
  6. lib_v5/spec_utils.py +1241 -0
  7. lib_v5/tfc_tdf_v3.py +253 -0
  8. lib_v5/vr_network/__init__.py +1 -0
  9. lib_v5/vr_network/layers.py +143 -0
  10. lib_v5/vr_network/layers_new.py +126 -0
  11. lib_v5/vr_network/model_param_init.py +32 -0
  12. lib_v5/vr_network/modelparams/1band_sr16000_hl512.json +19 -0
  13. lib_v5/vr_network/modelparams/1band_sr32000_hl512.json +19 -0
  14. lib_v5/vr_network/modelparams/1band_sr33075_hl384.json +19 -0
  15. lib_v5/vr_network/modelparams/1band_sr44100_hl1024.json +19 -0
  16. lib_v5/vr_network/modelparams/1band_sr44100_hl256.json +19 -0
  17. lib_v5/vr_network/modelparams/1band_sr44100_hl512.json +19 -0
  18. lib_v5/vr_network/modelparams/1band_sr44100_hl512_cut.json +19 -0
  19. lib_v5/vr_network/modelparams/1band_sr44100_hl512_nf1024.json +19 -0
  20. lib_v5/vr_network/modelparams/2band_32000.json +30 -0
  21. lib_v5/vr_network/modelparams/2band_44100_lofi.json +30 -0
  22. lib_v5/vr_network/modelparams/2band_48000.json +30 -0
  23. lib_v5/vr_network/modelparams/3band_44100.json +42 -0
  24. lib_v5/vr_network/modelparams/3band_44100_mid.json +43 -0
  25. lib_v5/vr_network/modelparams/3band_44100_msb2.json +43 -0
  26. lib_v5/vr_network/modelparams/4band_44100.json +54 -0
  27. lib_v5/vr_network/modelparams/4band_44100_mid.json +55 -0
  28. lib_v5/vr_network/modelparams/4band_44100_msb.json +55 -0
  29. lib_v5/vr_network/modelparams/4band_44100_msb2.json +55 -0
  30. lib_v5/vr_network/modelparams/4band_44100_reverse.json +55 -0
  31. lib_v5/vr_network/modelparams/4band_44100_sw.json +55 -0
  32. lib_v5/vr_network/modelparams/4band_v2.json +54 -0
  33. lib_v5/vr_network/modelparams/4band_v2_sn.json +55 -0
  34. lib_v5/vr_network/modelparams/4band_v3.json +54 -0
  35. lib_v5/vr_network/modelparams/4band_v3_sn.json +55 -0
  36. lib_v5/vr_network/modelparams/ensemble.json +43 -0
  37. lib_v5/vr_network/nets.py +166 -0
  38. lib_v5/vr_network/nets_new.py +125 -0
lib_v5/mdxnet.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from .modules import TFC_TDF
4
+ from pytorch_lightning import LightningModule
5
+
6
+ dim_s = 4
7
+
8
+ class AbstractMDXNet(LightningModule):
9
+ def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap):
10
+ super().__init__()
11
+ self.target_name = target_name
12
+ self.lr = lr
13
+ self.optimizer = optimizer
14
+ self.dim_c = dim_c
15
+ self.dim_f = dim_f
16
+ self.dim_t = dim_t
17
+ self.n_fft = n_fft
18
+ self.n_bins = n_fft // 2 + 1
19
+ self.hop_length = hop_length
20
+ self.window = nn.Parameter(torch.hann_window(window_length=self.n_fft, periodic=True), requires_grad=False)
21
+ self.freq_pad = nn.Parameter(torch.zeros([1, dim_c, self.n_bins - self.dim_f, self.dim_t]), requires_grad=False)
22
+
23
+ def get_optimizer(self):
24
+ if self.optimizer == 'rmsprop':
25
+ return torch.optim.RMSprop(self.parameters(), self.lr)
26
+
27
+ if self.optimizer == 'adamw':
28
+ return torch.optim.AdamW(self.parameters(), self.lr)
29
+
30
+ class ConvTDFNet(AbstractMDXNet):
31
+ def __init__(self, target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length,
32
+ num_blocks, l, g, k, bn, bias, overlap):
33
+
34
+ super(ConvTDFNet, self).__init__(
35
+ target_name, lr, optimizer, dim_c, dim_f, dim_t, n_fft, hop_length, overlap)
36
+ #self.save_hyperparameters()
37
+
38
+ self.num_blocks = num_blocks
39
+ self.l = l
40
+ self.g = g
41
+ self.k = k
42
+ self.bn = bn
43
+ self.bias = bias
44
+
45
+ if optimizer == 'rmsprop':
46
+ norm = nn.BatchNorm2d
47
+
48
+ if optimizer == 'adamw':
49
+ norm = lambda input:nn.GroupNorm(2, input)
50
+
51
+ self.n = num_blocks // 2
52
+ scale = (2, 2)
53
+
54
+ self.first_conv = nn.Sequential(
55
+ nn.Conv2d(in_channels=self.dim_c, out_channels=g, kernel_size=(1, 1)),
56
+ norm(g),
57
+ nn.ReLU(),
58
+ )
59
+
60
+ f = self.dim_f
61
+ c = g
62
+ self.encoding_blocks = nn.ModuleList()
63
+ self.ds = nn.ModuleList()
64
+ for i in range(self.n):
65
+ self.encoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm))
66
+ self.ds.append(
67
+ nn.Sequential(
68
+ nn.Conv2d(in_channels=c, out_channels=c + g, kernel_size=scale, stride=scale),
69
+ norm(c + g),
70
+ nn.ReLU()
71
+ )
72
+ )
73
+ f = f // 2
74
+ c += g
75
+
76
+ self.bottleneck_block = TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm)
77
+
78
+ self.decoding_blocks = nn.ModuleList()
79
+ self.us = nn.ModuleList()
80
+ for i in range(self.n):
81
+ self.us.append(
82
+ nn.Sequential(
83
+ nn.ConvTranspose2d(in_channels=c, out_channels=c - g, kernel_size=scale, stride=scale),
84
+ norm(c - g),
85
+ nn.ReLU()
86
+ )
87
+ )
88
+ f = f * 2
89
+ c -= g
90
+
91
+ self.decoding_blocks.append(TFC_TDF(c, l, f, k, bn, bias=bias, norm=norm))
92
+
93
+ self.final_conv = nn.Sequential(
94
+ nn.Conv2d(in_channels=c, out_channels=self.dim_c, kernel_size=(1, 1)),
95
+ )
96
+
97
+ def forward(self, x):
98
+
99
+ x = self.first_conv(x)
100
+
101
+ x = x.transpose(-1, -2)
102
+
103
+ ds_outputs = []
104
+ for i in range(self.n):
105
+ x = self.encoding_blocks[i](x)
106
+ ds_outputs.append(x)
107
+ x = self.ds[i](x)
108
+
109
+ x = self.bottleneck_block(x)
110
+
111
+ for i in range(self.n):
112
+ x = self.us[i](x)
113
+ x *= ds_outputs[-i - 1]
114
+ x = self.decoding_blocks[i](x)
115
+
116
+ x = x.transpose(-1, -2)
117
+
118
+ x = self.final_conv(x)
119
+
120
+ return x
121
+
122
+ class Mixer(nn.Module):
123
+ def __init__(self, device, mixer_path):
124
+
125
+ super(Mixer, self).__init__()
126
+
127
+ self.linear = nn.Linear((dim_s+1)*2, dim_s*2, bias=False)
128
+
129
+ self.load_state_dict(
130
+ torch.load(mixer_path, map_location=device)
131
+ )
132
+
133
+ def forward(self, x):
134
+ x = x.reshape(1,(dim_s+1)*2,-1).transpose(-1,-2)
135
+ x = self.linear(x)
136
+ return x.transpose(-1,-2).reshape(dim_s,2,-1)
lib_v5/mixer.ckpt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ea781bd52c6a523b825fa6cdbb6189f52e318edd8b17e6fe404f76f7af8caa9c
3
+ size 1208
lib_v5/modules.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+
5
+ class TFC(nn.Module):
6
+ def __init__(self, c, l, k, norm):
7
+ super(TFC, self).__init__()
8
+
9
+ self.H = nn.ModuleList()
10
+ for i in range(l):
11
+ self.H.append(
12
+ nn.Sequential(
13
+ nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
14
+ norm(c),
15
+ nn.ReLU(),
16
+ )
17
+ )
18
+
19
+ def forward(self, x):
20
+ for h in self.H:
21
+ x = h(x)
22
+ return x
23
+
24
+
25
+ class DenseTFC(nn.Module):
26
+ def __init__(self, c, l, k, norm):
27
+ super(DenseTFC, self).__init__()
28
+
29
+ self.conv = nn.ModuleList()
30
+ for i in range(l):
31
+ self.conv.append(
32
+ nn.Sequential(
33
+ nn.Conv2d(in_channels=c, out_channels=c, kernel_size=k, stride=1, padding=k // 2),
34
+ norm(c),
35
+ nn.ReLU(),
36
+ )
37
+ )
38
+
39
+ def forward(self, x):
40
+ for layer in self.conv[:-1]:
41
+ x = torch.cat([layer(x), x], 1)
42
+ return self.conv[-1](x)
43
+
44
+
45
+ class TFC_TDF(nn.Module):
46
+ def __init__(self, c, l, f, k, bn, dense=False, bias=True, norm=nn.BatchNorm2d):
47
+
48
+ super(TFC_TDF, self).__init__()
49
+
50
+ self.use_tdf = bn is not None
51
+
52
+ self.tfc = DenseTFC(c, l, k, norm) if dense else TFC(c, l, k, norm)
53
+
54
+ if self.use_tdf:
55
+ if bn == 0:
56
+ self.tdf = nn.Sequential(
57
+ nn.Linear(f, f, bias=bias),
58
+ norm(c),
59
+ nn.ReLU()
60
+ )
61
+ else:
62
+ self.tdf = nn.Sequential(
63
+ nn.Linear(f, f // bn, bias=bias),
64
+ norm(c),
65
+ nn.ReLU(),
66
+ nn.Linear(f // bn, f, bias=bias),
67
+ norm(c),
68
+ nn.ReLU()
69
+ )
70
+
71
+ def forward(self, x):
72
+ x = self.tfc(x)
73
+ return x + self.tdf(x) if self.use_tdf else x
74
+
lib_v5/pyrb.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+ import six
5
+ import numpy as np
6
+ import soundfile as sf
7
+ import sys
8
+
9
+ if getattr(sys, 'frozen', False):
10
+ BASE_PATH_RUB = sys._MEIPASS
11
+ else:
12
+ BASE_PATH_RUB = os.path.dirname(os.path.abspath(__file__))
13
+
14
+ __all__ = ['time_stretch', 'pitch_shift']
15
+
16
+ __RUBBERBAND_UTIL = os.path.join(BASE_PATH_RUB, 'rubberband')
17
+
18
+ if six.PY2:
19
+ DEVNULL = open(os.devnull, 'w')
20
+ else:
21
+ DEVNULL = subprocess.DEVNULL
22
+
23
+ def __rubberband(y, sr, **kwargs):
24
+
25
+ assert sr > 0
26
+
27
+ # Get the input and output tempfile
28
+ fd, infile = tempfile.mkstemp(suffix='.wav')
29
+ os.close(fd)
30
+ fd, outfile = tempfile.mkstemp(suffix='.wav')
31
+ os.close(fd)
32
+
33
+ # dump the audio
34
+ sf.write(infile, y, sr)
35
+
36
+ try:
37
+ # Execute rubberband
38
+ arguments = [__RUBBERBAND_UTIL, '-q']
39
+
40
+ for key, value in six.iteritems(kwargs):
41
+ arguments.append(str(key))
42
+ arguments.append(str(value))
43
+
44
+ arguments.extend([infile, outfile])
45
+
46
+ subprocess.check_call(arguments, stdout=DEVNULL, stderr=DEVNULL)
47
+
48
+ # Load the processed audio.
49
+ y_out, _ = sf.read(outfile, always_2d=True)
50
+
51
+ # make sure that output dimensions matches input
52
+ if y.ndim == 1:
53
+ y_out = np.squeeze(y_out)
54
+
55
+ except OSError as exc:
56
+ six.raise_from(RuntimeError('Failed to execute rubberband. '
57
+ 'Please verify that rubberband-cli '
58
+ 'is installed.'),
59
+ exc)
60
+
61
+ finally:
62
+ # Remove temp files
63
+ os.unlink(infile)
64
+ os.unlink(outfile)
65
+
66
+ return y_out
67
+
68
+ def time_stretch(y, sr, rate, rbargs=None):
69
+ if rate <= 0:
70
+ raise ValueError('rate must be strictly positive')
71
+
72
+ if rate == 1.0:
73
+ return y
74
+
75
+ if rbargs is None:
76
+ rbargs = dict()
77
+
78
+ rbargs.setdefault('--tempo', rate)
79
+
80
+ return __rubberband(y, sr, **rbargs)
81
+
82
+ def pitch_shift(y, sr, n_steps, rbargs=None):
83
+
84
+ if n_steps == 0:
85
+ return y
86
+
87
+ if rbargs is None:
88
+ rbargs = dict()
89
+
90
+ rbargs.setdefault('--pitch', n_steps)
91
+
92
+ return __rubberband(y, sr, **rbargs)
lib_v5/results.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+
3
+ """
4
+ Matchering - Audio Matching and Mastering Python Library
5
+ Copyright (C) 2016-2022 Sergree
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ """
20
+
21
+ import os
22
+ import soundfile as sf
23
+
24
+
25
+ class Result:
26
+ def __init__(
27
+ self, file: str, subtype: str, use_limiter: bool = True, normalize: bool = True
28
+ ):
29
+ _, file_ext = os.path.splitext(file)
30
+ file_ext = file_ext[1:].upper()
31
+ if not sf.check_format(file_ext):
32
+ raise TypeError(f"{file_ext} format is not supported")
33
+ if not sf.check_format(file_ext, subtype):
34
+ raise TypeError(f"{file_ext} format does not have {subtype} subtype")
35
+ self.file = file
36
+ self.subtype = subtype
37
+ self.use_limiter = use_limiter
38
+ self.normalize = normalize
39
+
40
+
41
+ def pcm16(file: str) -> Result:
42
+ return Result(file, "PCM_16")
43
+
44
+ def pcm24(file: str) -> Result:
45
+ return Result(file, "FLOAT")
46
+
47
+ def save_audiofile(file: str, wav_set="PCM_16") -> Result:
48
+ return Result(file, wav_set)
lib_v5/spec_utils.py ADDED
@@ -0,0 +1,1241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import audioread
2
+ import librosa
3
+ import numpy as np
4
+ import soundfile as sf
5
+ import math
6
+ import platform
7
+ import traceback
8
+ from . import pyrb
9
+ from scipy.signal import correlate, hilbert
10
+ import io
11
+
12
+ OPERATING_SYSTEM = platform.system()
13
+ SYSTEM_ARCH = platform.platform()
14
+ SYSTEM_PROC = platform.processor()
15
+ ARM = 'arm'
16
+
17
+ AUTO_PHASE = "Automatic"
18
+ POSITIVE_PHASE = "Positive Phase"
19
+ NEGATIVE_PHASE = "Negative Phase"
20
+ NONE_P = "None",
21
+ LOW_P = "Shifts: Low",
22
+ MED_P = "Shifts: Medium",
23
+ HIGH_P = "Shifts: High",
24
+ VHIGH_P = "Shifts: Very High"
25
+ MAXIMUM_P = "Shifts: Maximum"
26
+
27
+ progress_value = 0
28
+ last_update_time = 0
29
+ is_macos = False
30
+
31
+ if OPERATING_SYSTEM == 'Windows':
32
+ from pyrubberband import pyrb
33
+ else:
34
+ from . import pyrb
35
+
36
+ if OPERATING_SYSTEM == 'Darwin':
37
+ wav_resolution = "polyphase" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else "sinc_fastest"
38
+ wav_resolution_float_resampling = "kaiser_best" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else wav_resolution
39
+ is_macos = True
40
+ else:
41
+ wav_resolution = "sinc_fastest"
42
+ wav_resolution_float_resampling = wav_resolution
43
+
44
+ MAX_SPEC = 'Max Spec'
45
+ MIN_SPEC = 'Min Spec'
46
+ LIN_ENSE = 'Linear Ensemble'
47
+
48
+ MAX_WAV = MAX_SPEC
49
+ MIN_WAV = MIN_SPEC
50
+
51
+ AVERAGE = 'Average'
52
+
53
+ def crop_center(h1, h2):
54
+ h1_shape = h1.size()
55
+ h2_shape = h2.size()
56
+
57
+ if h1_shape[3] == h2_shape[3]:
58
+ return h1
59
+ elif h1_shape[3] < h2_shape[3]:
60
+ raise ValueError('h1_shape[3] must be greater than h2_shape[3]')
61
+
62
+ s_time = (h1_shape[3] - h2_shape[3]) // 2
63
+ e_time = s_time + h2_shape[3]
64
+ h1 = h1[:, :, :, s_time:e_time]
65
+
66
+ return h1
67
+
68
+ def preprocess(X_spec):
69
+ X_mag = np.abs(X_spec)
70
+ X_phase = np.angle(X_spec)
71
+
72
+ return X_mag, X_phase
73
+
74
+ def make_padding(width, cropsize, offset):
75
+ left = offset
76
+ roi_size = cropsize - offset * 2
77
+ if roi_size == 0:
78
+ roi_size = cropsize
79
+ right = roi_size - (width % roi_size) + left
80
+
81
+ return left, right, roi_size
82
+
83
+ def normalize(wave, is_normalize=False):
84
+ """Normalize audio"""
85
+
86
+ maxv = np.abs(wave).max()
87
+ if maxv > 1.0:
88
+ if is_normalize:
89
+ print("Above clipping threshold.")
90
+ wave /= maxv
91
+
92
+ return wave
93
+
94
+ def auto_transpose(audio_array:np.ndarray):
95
+ """
96
+ Ensure that the audio array is in the (channels, samples) format.
97
+
98
+ Parameters:
99
+ audio_array (ndarray): Input audio array.
100
+
101
+ Returns:
102
+ ndarray: Transposed audio array if necessary.
103
+ """
104
+
105
+ # If the second dimension is 2 (indicating stereo channels), transpose the array
106
+ if audio_array.shape[1] == 2:
107
+ return audio_array.T
108
+ return audio_array
109
+
110
+ def write_array_to_mem(audio_data, subtype):
111
+ if isinstance(audio_data, np.ndarray):
112
+ audio_buffer = io.BytesIO()
113
+ sf.write(audio_buffer, audio_data, 44100, subtype=subtype, format='WAV')
114
+ audio_buffer.seek(0)
115
+ return audio_buffer
116
+ else:
117
+ return audio_data
118
+
119
+ def spectrogram_to_image(spec, mode='magnitude'):
120
+ if mode == 'magnitude':
121
+ if np.iscomplexobj(spec):
122
+ y = np.abs(spec)
123
+ else:
124
+ y = spec
125
+ y = np.log10(y ** 2 + 1e-8)
126
+ elif mode == 'phase':
127
+ if np.iscomplexobj(spec):
128
+ y = np.angle(spec)
129
+ else:
130
+ y = spec
131
+
132
+ y -= y.min()
133
+ y *= 255 / y.max()
134
+ img = np.uint8(y)
135
+
136
+ if y.ndim == 3:
137
+ img = img.transpose(1, 2, 0)
138
+ img = np.concatenate([
139
+ np.max(img, axis=2, keepdims=True), img
140
+ ], axis=2)
141
+
142
+ return img
143
+
144
+ def reduce_vocal_aggressively(X, y, softmask):
145
+ v = X - y
146
+ y_mag_tmp = np.abs(y)
147
+ v_mag_tmp = np.abs(v)
148
+
149
+ v_mask = v_mag_tmp > y_mag_tmp
150
+ y_mag = np.clip(y_mag_tmp - v_mag_tmp * v_mask * softmask, 0, np.inf)
151
+
152
+ return y_mag * np.exp(1.j * np.angle(y))
153
+
154
+ def merge_artifacts(y_mask, thres=0.01, min_range=64, fade_size=32):
155
+ mask = y_mask
156
+
157
+ try:
158
+ if min_range < fade_size * 2:
159
+ raise ValueError('min_range must be >= fade_size * 2')
160
+
161
+ idx = np.where(y_mask.min(axis=(0, 1)) > thres)[0]
162
+ start_idx = np.insert(idx[np.where(np.diff(idx) != 1)[0] + 1], 0, idx[0])
163
+ end_idx = np.append(idx[np.where(np.diff(idx) != 1)[0]], idx[-1])
164
+ artifact_idx = np.where(end_idx - start_idx > min_range)[0]
165
+ weight = np.zeros_like(y_mask)
166
+ if len(artifact_idx) > 0:
167
+ start_idx = start_idx[artifact_idx]
168
+ end_idx = end_idx[artifact_idx]
169
+ old_e = None
170
+ for s, e in zip(start_idx, end_idx):
171
+ if old_e is not None and s - old_e < fade_size:
172
+ s = old_e - fade_size * 2
173
+
174
+ if s != 0:
175
+ weight[:, :, s:s + fade_size] = np.linspace(0, 1, fade_size)
176
+ else:
177
+ s -= fade_size
178
+
179
+ if e != y_mask.shape[2]:
180
+ weight[:, :, e - fade_size:e] = np.linspace(1, 0, fade_size)
181
+ else:
182
+ e += fade_size
183
+
184
+ weight[:, :, s + fade_size:e - fade_size] = 1
185
+ old_e = e
186
+
187
+ v_mask = 1 - y_mask
188
+ y_mask += weight * v_mask
189
+
190
+ mask = y_mask
191
+ except Exception as e:
192
+ error_name = f'{type(e).__name__}'
193
+ traceback_text = ''.join(traceback.format_tb(e.__traceback__))
194
+ message = f'{error_name}: "{e}"\n{traceback_text}"'
195
+ print('Post Process Failed: ', message)
196
+
197
+ return mask
198
+
199
+ def align_wave_head_and_tail(a, b):
200
+ l = min([a[0].size, b[0].size])
201
+
202
+ return a[:l,:l], b[:l,:l]
203
+
204
+ def convert_channels(spec, mp, band):
205
+ cc = mp.param['band'][band].get('convert_channels')
206
+
207
+ if 'mid_side_c' == cc:
208
+ spec_left = np.add(spec[0], spec[1] * .25)
209
+ spec_right = np.subtract(spec[1], spec[0] * .25)
210
+ elif 'mid_side' == cc:
211
+ spec_left = np.add(spec[0], spec[1]) / 2
212
+ spec_right = np.subtract(spec[0], spec[1])
213
+ elif 'stereo_n' == cc:
214
+ spec_left = np.add(spec[0], spec[1] * .25) / 0.9375
215
+ spec_right = np.add(spec[1], spec[0] * .25) / 0.9375
216
+ else:
217
+ return spec
218
+
219
+ return np.asfortranarray([spec_left, spec_right])
220
+
221
+ def combine_spectrograms(specs, mp, is_v51_model=False):
222
+ l = min([specs[i].shape[2] for i in specs])
223
+ spec_c = np.zeros(shape=(2, mp.param['bins'] + 1, l), dtype=np.complex64)
224
+ offset = 0
225
+ bands_n = len(mp.param['band'])
226
+
227
+ for d in range(1, bands_n + 1):
228
+ h = mp.param['band'][d]['crop_stop'] - mp.param['band'][d]['crop_start']
229
+ spec_c[:, offset:offset+h, :l] = specs[d][:, mp.param['band'][d]['crop_start']:mp.param['band'][d]['crop_stop'], :l]
230
+ offset += h
231
+
232
+ if offset > mp.param['bins']:
233
+ raise ValueError('Too much bins')
234
+
235
+ # lowpass fiter
236
+
237
+ if mp.param['pre_filter_start'] > 0:
238
+ if is_v51_model:
239
+ spec_c *= get_lp_filter_mask(spec_c.shape[1], mp.param['pre_filter_start'], mp.param['pre_filter_stop'])
240
+ else:
241
+ if bands_n == 1:
242
+ spec_c = fft_lp_filter(spec_c, mp.param['pre_filter_start'], mp.param['pre_filter_stop'])
243
+ else:
244
+ gp = 1
245
+ for b in range(mp.param['pre_filter_start'] + 1, mp.param['pre_filter_stop']):
246
+ g = math.pow(10, -(b - mp.param['pre_filter_start']) * (3.5 - gp) / 20.0)
247
+ gp = g
248
+ spec_c[:, b, :] *= g
249
+
250
+ return np.asfortranarray(spec_c)
251
+
252
+ def wave_to_spectrogram(wave, hop_length, n_fft, mp, band, is_v51_model=False):
253
+
254
+ if wave.ndim == 1:
255
+ wave = np.asfortranarray([wave,wave])
256
+
257
+ if not is_v51_model:
258
+ if mp.param['reverse']:
259
+ wave_left = np.flip(np.asfortranarray(wave[0]))
260
+ wave_right = np.flip(np.asfortranarray(wave[1]))
261
+ elif mp.param['mid_side']:
262
+ wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2)
263
+ wave_right = np.asfortranarray(np.subtract(wave[0], wave[1]))
264
+ elif mp.param['mid_side_b2']:
265
+ wave_left = np.asfortranarray(np.add(wave[1], wave[0] * .5))
266
+ wave_right = np.asfortranarray(np.subtract(wave[0], wave[1] * .5))
267
+ else:
268
+ wave_left = np.asfortranarray(wave[0])
269
+ wave_right = np.asfortranarray(wave[1])
270
+ else:
271
+ wave_left = np.asfortranarray(wave[0])
272
+ wave_right = np.asfortranarray(wave[1])
273
+
274
+ spec_left = librosa.stft(wave_left, n_fft, hop_length=hop_length)
275
+ spec_right = librosa.stft(wave_right, n_fft, hop_length=hop_length)
276
+
277
+ spec = np.asfortranarray([spec_left, spec_right])
278
+
279
+ if is_v51_model:
280
+ spec = convert_channels(spec, mp, band)
281
+
282
+ return spec
283
+
284
+ def spectrogram_to_wave(spec, hop_length=1024, mp={}, band=0, is_v51_model=True):
285
+ spec_left = np.asfortranarray(spec[0])
286
+ spec_right = np.asfortranarray(spec[1])
287
+
288
+ wave_left = librosa.istft(spec_left, hop_length=hop_length)
289
+ wave_right = librosa.istft(spec_right, hop_length=hop_length)
290
+
291
+ if is_v51_model:
292
+ cc = mp.param['band'][band].get('convert_channels')
293
+ if 'mid_side_c' == cc:
294
+ return np.asfortranarray([np.subtract(wave_left / 1.0625, wave_right / 4.25), np.add(wave_right / 1.0625, wave_left / 4.25)])
295
+ elif 'mid_side' == cc:
296
+ return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
297
+ elif 'stereo_n' == cc:
298
+ return np.asfortranarray([np.subtract(wave_left, wave_right * .25), np.subtract(wave_right, wave_left * .25)])
299
+ else:
300
+ if mp.param['reverse']:
301
+ return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)])
302
+ elif mp.param['mid_side']:
303
+ return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
304
+ elif mp.param['mid_side_b2']:
305
+ return np.asfortranarray([np.add(wave_right / 1.25, .4 * wave_left), np.subtract(wave_left / 1.25, .4 * wave_right)])
306
+
307
+ return np.asfortranarray([wave_left, wave_right])
308
+
309
+ def cmb_spectrogram_to_wave(spec_m, mp, extra_bins_h=None, extra_bins=None, is_v51_model=False):
310
+ bands_n = len(mp.param['band'])
311
+ offset = 0
312
+
313
+ for d in range(1, bands_n + 1):
314
+ bp = mp.param['band'][d]
315
+ spec_s = np.ndarray(shape=(2, bp['n_fft'] // 2 + 1, spec_m.shape[2]), dtype=complex)
316
+ h = bp['crop_stop'] - bp['crop_start']
317
+ spec_s[:, bp['crop_start']:bp['crop_stop'], :] = spec_m[:, offset:offset+h, :]
318
+
319
+ offset += h
320
+ if d == bands_n: # higher
321
+ if extra_bins_h: # if --high_end_process bypass
322
+ max_bin = bp['n_fft'] // 2
323
+ spec_s[:, max_bin-extra_bins_h:max_bin, :] = extra_bins[:, :extra_bins_h, :]
324
+ if bp['hpf_start'] > 0:
325
+ if is_v51_model:
326
+ spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
327
+ else:
328
+ spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
329
+ if bands_n == 1:
330
+ wave = spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model)
331
+ else:
332
+ wave = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model))
333
+ else:
334
+ sr = mp.param['band'][d+1]['sr']
335
+ if d == 1: # lower
336
+ if is_v51_model:
337
+ spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
338
+ else:
339
+ spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
340
+ wave = librosa.resample(spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model), bp['sr'], sr, res_type=wav_resolution)
341
+ else: # mid
342
+ if is_v51_model:
343
+ spec_s *= get_hp_filter_mask(spec_s.shape[1], bp['hpf_start'], bp['hpf_stop'] - 1)
344
+ spec_s *= get_lp_filter_mask(spec_s.shape[1], bp['lpf_start'], bp['lpf_stop'])
345
+ else:
346
+ spec_s = fft_hp_filter(spec_s, bp['hpf_start'], bp['hpf_stop'] - 1)
347
+ spec_s = fft_lp_filter(spec_s, bp['lpf_start'], bp['lpf_stop'])
348
+
349
+ wave2 = np.add(wave, spectrogram_to_wave(spec_s, bp['hl'], mp, d, is_v51_model))
350
+ wave = librosa.resample(wave2, bp['sr'], sr, res_type=wav_resolution)
351
+
352
+ return wave
353
+
354
+ def get_lp_filter_mask(n_bins, bin_start, bin_stop):
355
+ mask = np.concatenate([
356
+ np.ones((bin_start - 1, 1)),
357
+ np.linspace(1, 0, bin_stop - bin_start + 1)[:, None],
358
+ np.zeros((n_bins - bin_stop, 1))
359
+ ], axis=0)
360
+
361
+ return mask
362
+
363
+ def get_hp_filter_mask(n_bins, bin_start, bin_stop):
364
+ mask = np.concatenate([
365
+ np.zeros((bin_stop + 1, 1)),
366
+ np.linspace(0, 1, 1 + bin_start - bin_stop)[:, None],
367
+ np.ones((n_bins - bin_start - 2, 1))
368
+ ], axis=0)
369
+
370
+ return mask
371
+
372
+ def fft_lp_filter(spec, bin_start, bin_stop):
373
+ g = 1.0
374
+ for b in range(bin_start, bin_stop):
375
+ g -= 1 / (bin_stop - bin_start)
376
+ spec[:, b, :] = g * spec[:, b, :]
377
+
378
+ spec[:, bin_stop:, :] *= 0
379
+
380
+ return spec
381
+
382
+ def fft_hp_filter(spec, bin_start, bin_stop):
383
+ g = 1.0
384
+ for b in range(bin_start, bin_stop, -1):
385
+ g -= 1 / (bin_start - bin_stop)
386
+ spec[:, b, :] = g * spec[:, b, :]
387
+
388
+ spec[:, 0:bin_stop+1, :] *= 0
389
+
390
+ return spec
391
+
392
+ def spectrogram_to_wave_old(spec, hop_length=1024):
393
+ if spec.ndim == 2:
394
+ wave = librosa.istft(spec, hop_length=hop_length)
395
+ elif spec.ndim == 3:
396
+ spec_left = np.asfortranarray(spec[0])
397
+ spec_right = np.asfortranarray(spec[1])
398
+
399
+ wave_left = librosa.istft(spec_left, hop_length=hop_length)
400
+ wave_right = librosa.istft(spec_right, hop_length=hop_length)
401
+ wave = np.asfortranarray([wave_left, wave_right])
402
+
403
+ return wave
404
+
405
+ def wave_to_spectrogram_old(wave, hop_length, n_fft):
406
+ wave_left = np.asfortranarray(wave[0])
407
+ wave_right = np.asfortranarray(wave[1])
408
+
409
+ spec_left = librosa.stft(wave_left, n_fft, hop_length=hop_length)
410
+ spec_right = librosa.stft(wave_right, n_fft, hop_length=hop_length)
411
+
412
+ spec = np.asfortranarray([spec_left, spec_right])
413
+
414
+ return spec
415
+
416
+ def mirroring(a, spec_m, input_high_end, mp):
417
+ if 'mirroring' == a:
418
+ mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1)
419
+ mirror = mirror * np.exp(1.j * np.angle(input_high_end))
420
+
421
+ return np.where(np.abs(input_high_end) <= np.abs(mirror), input_high_end, mirror)
422
+
423
+ if 'mirroring2' == a:
424
+ mirror = np.flip(np.abs(spec_m[:, mp.param['pre_filter_start']-10-input_high_end.shape[1]:mp.param['pre_filter_start']-10, :]), 1)
425
+ mi = np.multiply(mirror, input_high_end * 1.7)
426
+
427
+ return np.where(np.abs(input_high_end) <= np.abs(mi), input_high_end, mi)
428
+
429
+ def adjust_aggr(mask, is_non_accom_stem, aggressiveness):
430
+ aggr = aggressiveness['value'] * 2
431
+
432
+ if aggr != 0:
433
+ if is_non_accom_stem:
434
+ aggr = 1 - aggr
435
+
436
+ aggr = [aggr, aggr]
437
+
438
+ if aggressiveness['aggr_correction'] is not None:
439
+ aggr[0] += aggressiveness['aggr_correction']['left']
440
+ aggr[1] += aggressiveness['aggr_correction']['right']
441
+
442
+ for ch in range(2):
443
+ mask[ch, :aggressiveness['split_bin']] = np.power(mask[ch, :aggressiveness['split_bin']], 1 + aggr[ch] / 3)
444
+ mask[ch, aggressiveness['split_bin']:] = np.power(mask[ch, aggressiveness['split_bin']:], 1 + aggr[ch])
445
+
446
+ return mask
447
+
448
+ def stft(wave, nfft, hl):
449
+ wave_left = np.asfortranarray(wave[0])
450
+ wave_right = np.asfortranarray(wave[1])
451
+ spec_left = librosa.stft(wave_left, nfft, hop_length=hl)
452
+ spec_right = librosa.stft(wave_right, nfft, hop_length=hl)
453
+ spec = np.asfortranarray([spec_left, spec_right])
454
+
455
+ return spec
456
+
457
+ def istft(spec, hl):
458
+ spec_left = np.asfortranarray(spec[0])
459
+ spec_right = np.asfortranarray(spec[1])
460
+ wave_left = librosa.istft(spec_left, hop_length=hl)
461
+ wave_right = librosa.istft(spec_right, hop_length=hl)
462
+ wave = np.asfortranarray([wave_left, wave_right])
463
+
464
+ return wave
465
+
466
+ def spec_effects(wave, algorithm='Default', value=None):
467
+ spec = [stft(wave[0],2048,1024), stft(wave[1],2048,1024)]
468
+ if algorithm == 'Min_Mag':
469
+ v_spec_m = np.where(np.abs(spec[1]) <= np.abs(spec[0]), spec[1], spec[0])
470
+ wave = istft(v_spec_m,1024)
471
+ elif algorithm == 'Max_Mag':
472
+ v_spec_m = np.where(np.abs(spec[1]) >= np.abs(spec[0]), spec[1], spec[0])
473
+ wave = istft(v_spec_m,1024)
474
+ elif algorithm == 'Default':
475
+ wave = (wave[1] * value) + (wave[0] * (1-value))
476
+ elif algorithm == 'Invert_p':
477
+ X_mag = np.abs(spec[0])
478
+ y_mag = np.abs(spec[1])
479
+ max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
480
+ v_spec = spec[1] - max_mag * np.exp(1.j * np.angle(spec[0]))
481
+ wave = istft(v_spec,1024)
482
+
483
+ return wave
484
+
485
+ def spectrogram_to_wave_no_mp(spec, n_fft=2048, hop_length=1024):
486
+ wave = librosa.istft(spec, n_fft=n_fft, hop_length=hop_length)
487
+
488
+ if wave.ndim == 1:
489
+ wave = np.asfortranarray([wave,wave])
490
+
491
+ return wave
492
+
493
+ def wave_to_spectrogram_no_mp(wave):
494
+
495
+ spec = librosa.stft(wave, n_fft=2048, hop_length=1024)
496
+
497
+ if spec.ndim == 1:
498
+ spec = np.asfortranarray([spec,spec])
499
+
500
+ return spec
501
+
502
+ def invert_audio(specs, invert_p=True):
503
+
504
+ ln = min([specs[0].shape[2], specs[1].shape[2]])
505
+ specs[0] = specs[0][:,:,:ln]
506
+ specs[1] = specs[1][:,:,:ln]
507
+
508
+ if invert_p:
509
+ X_mag = np.abs(specs[0])
510
+ y_mag = np.abs(specs[1])
511
+ max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
512
+ v_spec = specs[1] - max_mag * np.exp(1.j * np.angle(specs[0]))
513
+ else:
514
+ specs[1] = reduce_vocal_aggressively(specs[0], specs[1], 0.2)
515
+ v_spec = specs[0] - specs[1]
516
+
517
+ return v_spec
518
+
519
+ def invert_stem(mixture, stem):
520
+ mixture = wave_to_spectrogram_no_mp(mixture)
521
+ stem = wave_to_spectrogram_no_mp(stem)
522
+ output = spectrogram_to_wave_no_mp(invert_audio([mixture, stem]))
523
+
524
+ return -output.T
525
+
526
+ def ensembling(a, inputs, is_wavs=False):
527
+
528
+ for i in range(1, len(inputs)):
529
+ if i == 1:
530
+ input = inputs[0]
531
+
532
+ if is_wavs:
533
+ ln = min([input.shape[1], inputs[i].shape[1]])
534
+ input = input[:,:ln]
535
+ inputs[i] = inputs[i][:,:ln]
536
+ else:
537
+ ln = min([input.shape[2], inputs[i].shape[2]])
538
+ input = input[:,:,:ln]
539
+ inputs[i] = inputs[i][:,:,:ln]
540
+
541
+ if MIN_SPEC == a:
542
+ input = np.where(np.abs(inputs[i]) <= np.abs(input), inputs[i], input)
543
+ if MAX_SPEC == a:
544
+ input = np.where(np.abs(inputs[i]) >= np.abs(input), inputs[i], input)
545
+
546
+ #linear_ensemble
547
+ #input = ensemble_wav(inputs, split_size=1)
548
+
549
+ return input
550
+
551
+ def ensemble_for_align(waves):
552
+
553
+ specs = []
554
+
555
+ for wav in waves:
556
+ spec = wave_to_spectrogram_no_mp(wav.T)
557
+ specs.append(spec)
558
+
559
+ wav_aligned = spectrogram_to_wave_no_mp(ensembling(MIN_SPEC, specs)).T
560
+ wav_aligned = match_array_shapes(wav_aligned, waves[1], is_swap=True)
561
+
562
+ return wav_aligned
563
+
564
+ def ensemble_inputs(audio_input, algorithm, is_normalization, wav_type_set, save_path, is_wave=False, is_array=False):
565
+
566
+ wavs_ = []
567
+
568
+ if algorithm == AVERAGE:
569
+ output = average_audio(audio_input)
570
+ samplerate = 44100
571
+ else:
572
+ specs = []
573
+
574
+ for i in range(len(audio_input)):
575
+ wave, samplerate = librosa.load(audio_input[i], mono=False, sr=44100)
576
+ wavs_.append(wave)
577
+ spec = wave if is_wave else wave_to_spectrogram_no_mp(wave)
578
+ specs.append(spec)
579
+
580
+ wave_shapes = [w.shape[1] for w in wavs_]
581
+ target_shape = wavs_[wave_shapes.index(max(wave_shapes))]
582
+
583
+ if is_wave:
584
+ output = ensembling(algorithm, specs, is_wavs=True)
585
+ else:
586
+ output = spectrogram_to_wave_no_mp(ensembling(algorithm, specs))
587
+
588
+ output = to_shape(output, target_shape.shape)
589
+
590
+ sf.write(save_path, normalize(output.T, is_normalization), samplerate, subtype=wav_type_set)
591
+
592
+ def to_shape(x, target_shape):
593
+ padding_list = []
594
+ for x_dim, target_dim in zip(x.shape, target_shape):
595
+ pad_value = (target_dim - x_dim)
596
+ pad_tuple = ((0, pad_value))
597
+ padding_list.append(pad_tuple)
598
+
599
+ return np.pad(x, tuple(padding_list), mode='constant')
600
+
601
+ def to_shape_minimize(x: np.ndarray, target_shape):
602
+
603
+ padding_list = []
604
+ for x_dim, target_dim in zip(x.shape, target_shape):
605
+ pad_value = (target_dim - x_dim)
606
+ pad_tuple = ((0, pad_value))
607
+ padding_list.append(pad_tuple)
608
+
609
+ return np.pad(x, tuple(padding_list), mode='constant')
610
+
611
+ def detect_leading_silence(audio, sr, silence_threshold=0.007, frame_length=1024):
612
+ """
613
+ Detect silence at the beginning of an audio signal.
614
+
615
+ :param audio: np.array, audio signal
616
+ :param sr: int, sample rate
617
+ :param silence_threshold: float, magnitude threshold below which is considered silence
618
+ :param frame_length: int, the number of samples to consider for each check
619
+
620
+ :return: float, duration of the leading silence in milliseconds
621
+ """
622
+
623
+ if len(audio.shape) == 2:
624
+ # If stereo, pick the channel with more energy to determine the silence
625
+ channel = np.argmax(np.sum(np.abs(audio), axis=1))
626
+ audio = audio[channel]
627
+
628
+ for i in range(0, len(audio), frame_length):
629
+ if np.max(np.abs(audio[i:i+frame_length])) > silence_threshold:
630
+ return (i / sr) * 1000
631
+
632
+ return (len(audio) / sr) * 1000
633
+
634
+ def adjust_leading_silence(target_audio, reference_audio, silence_threshold=0.01, frame_length=1024):
635
+ """
636
+ Adjust the leading silence of the target_audio to match the leading silence of the reference_audio.
637
+
638
+ :param target_audio: np.array, audio signal that will have its silence adjusted
639
+ :param reference_audio: np.array, audio signal used as a reference
640
+ :param sr: int, sample rate
641
+ :param silence_threshold: float, magnitude threshold below which is considered silence
642
+ :param frame_length: int, the number of samples to consider for each check
643
+
644
+ :return: np.array, target_audio adjusted to have the same leading silence as reference_audio
645
+ """
646
+
647
+ def find_silence_end(audio):
648
+ if len(audio.shape) == 2:
649
+ # If stereo, pick the channel with more energy to determine the silence
650
+ channel = np.argmax(np.sum(np.abs(audio), axis=1))
651
+ audio_mono = audio[channel]
652
+ else:
653
+ audio_mono = audio
654
+
655
+ for i in range(0, len(audio_mono), frame_length):
656
+ if np.max(np.abs(audio_mono[i:i+frame_length])) > silence_threshold:
657
+ return i
658
+ return len(audio_mono)
659
+
660
+ ref_silence_end = find_silence_end(reference_audio)
661
+ target_silence_end = find_silence_end(target_audio)
662
+ silence_difference = ref_silence_end - target_silence_end
663
+
664
+ try:
665
+ ref_silence_end_p = (ref_silence_end / 44100) * 1000
666
+ target_silence_end_p = (target_silence_end / 44100) * 1000
667
+ silence_difference_p = ref_silence_end_p - target_silence_end_p
668
+ print("silence_difference: ", silence_difference_p)
669
+ except Exception as e:
670
+ pass
671
+
672
+ if silence_difference > 0: # Add silence to target_audio
673
+ if len(target_audio.shape) == 2: # stereo
674
+ silence_to_add = np.zeros((target_audio.shape[0], silence_difference))
675
+ else: # mono
676
+ silence_to_add = np.zeros(silence_difference)
677
+ return np.hstack((silence_to_add, target_audio))
678
+ elif silence_difference < 0: # Remove silence from target_audio
679
+ if len(target_audio.shape) == 2: # stereo
680
+ return target_audio[:, -silence_difference:]
681
+ else: # mono
682
+ return target_audio[-silence_difference:]
683
+ else: # No adjustment needed
684
+ return target_audio
685
+
686
+ def match_array_shapes(array_1:np.ndarray, array_2:np.ndarray, is_swap=False):
687
+
688
+ if is_swap:
689
+ array_1, array_2 = array_1.T, array_2.T
690
+
691
+ #print("before", array_1.shape, array_2.shape)
692
+ if array_1.shape[1] > array_2.shape[1]:
693
+ array_1 = array_1[:,:array_2.shape[1]]
694
+ elif array_1.shape[1] < array_2.shape[1]:
695
+ padding = array_2.shape[1] - array_1.shape[1]
696
+ array_1 = np.pad(array_1, ((0,0), (0,padding)), 'constant', constant_values=0)
697
+
698
+ #print("after", array_1.shape, array_2.shape)
699
+
700
+ if is_swap:
701
+ array_1, array_2 = array_1.T, array_2.T
702
+
703
+ return array_1
704
+
705
+ def match_mono_array_shapes(array_1: np.ndarray, array_2: np.ndarray):
706
+
707
+ if len(array_1) > len(array_2):
708
+ array_1 = array_1[:len(array_2)]
709
+ elif len(array_1) < len(array_2):
710
+ padding = len(array_2) - len(array_1)
711
+ array_1 = np.pad(array_1, (0, padding), 'constant', constant_values=0)
712
+
713
+ return array_1
714
+
715
+ def change_pitch_semitones(y, sr, semitone_shift):
716
+ factor = 2 ** (semitone_shift / 12) # Convert semitone shift to factor for resampling
717
+ y_pitch_tuned = []
718
+ for y_channel in y:
719
+ y_pitch_tuned.append(librosa.resample(y_channel, sr, sr*factor, res_type=wav_resolution_float_resampling))
720
+ y_pitch_tuned = np.array(y_pitch_tuned)
721
+ new_sr = sr * factor
722
+ return y_pitch_tuned, new_sr
723
+
724
+ def augment_audio(export_path, audio_file, rate, is_normalization, wav_type_set, save_format=None, is_pitch=False, is_time_correction=True):
725
+
726
+ wav, sr = librosa.load(audio_file, sr=44100, mono=False)
727
+
728
+ if wav.ndim == 1:
729
+ wav = np.asfortranarray([wav,wav])
730
+
731
+ if not is_time_correction:
732
+ wav_mix = change_pitch_semitones(wav, 44100, semitone_shift=-rate)[0]
733
+ else:
734
+ if is_pitch:
735
+ wav_1 = pyrb.pitch_shift(wav[0], sr, rate, rbargs=None)
736
+ wav_2 = pyrb.pitch_shift(wav[1], sr, rate, rbargs=None)
737
+ else:
738
+ wav_1 = pyrb.time_stretch(wav[0], sr, rate, rbargs=None)
739
+ wav_2 = pyrb.time_stretch(wav[1], sr, rate, rbargs=None)
740
+
741
+ if wav_1.shape > wav_2.shape:
742
+ wav_2 = to_shape(wav_2, wav_1.shape)
743
+ if wav_1.shape < wav_2.shape:
744
+ wav_1 = to_shape(wav_1, wav_2.shape)
745
+
746
+ wav_mix = np.asfortranarray([wav_1, wav_2])
747
+
748
+ sf.write(export_path, normalize(wav_mix.T, is_normalization), sr, subtype=wav_type_set)
749
+ save_format(export_path)
750
+
751
+ def average_audio(audio):
752
+
753
+ waves = []
754
+ wave_shapes = []
755
+ final_waves = []
756
+
757
+ for i in range(len(audio)):
758
+ wave = librosa.load(audio[i], sr=44100, mono=False)
759
+ waves.append(wave[0])
760
+ wave_shapes.append(wave[0].shape[1])
761
+
762
+ wave_shapes_index = wave_shapes.index(max(wave_shapes))
763
+ target_shape = waves[wave_shapes_index]
764
+ waves.pop(wave_shapes_index)
765
+ final_waves.append(target_shape)
766
+
767
+ for n_array in waves:
768
+ wav_target = to_shape(n_array, target_shape.shape)
769
+ final_waves.append(wav_target)
770
+
771
+ waves = sum(final_waves)
772
+ waves = waves/len(audio)
773
+
774
+ return waves
775
+
776
+ def average_dual_sources(wav_1, wav_2, value):
777
+
778
+ if wav_1.shape > wav_2.shape:
779
+ wav_2 = to_shape(wav_2, wav_1.shape)
780
+ if wav_1.shape < wav_2.shape:
781
+ wav_1 = to_shape(wav_1, wav_2.shape)
782
+
783
+ wave = (wav_1 * value) + (wav_2 * (1-value))
784
+
785
+ return wave
786
+
787
+ def reshape_sources(wav_1: np.ndarray, wav_2: np.ndarray):
788
+
789
+ if wav_1.shape > wav_2.shape:
790
+ wav_2 = to_shape(wav_2, wav_1.shape)
791
+ if wav_1.shape < wav_2.shape:
792
+ ln = min([wav_1.shape[1], wav_2.shape[1]])
793
+ wav_2 = wav_2[:,:ln]
794
+
795
+ ln = min([wav_1.shape[1], wav_2.shape[1]])
796
+ wav_1 = wav_1[:,:ln]
797
+ wav_2 = wav_2[:,:ln]
798
+
799
+ return wav_2
800
+
801
+ def reshape_sources_ref(wav_1_shape, wav_2: np.ndarray):
802
+
803
+ if wav_1_shape > wav_2.shape:
804
+ wav_2 = to_shape(wav_2, wav_1_shape)
805
+
806
+ return wav_2
807
+
808
+ def combine_arrarys(audio_sources, is_swap=False):
809
+ source = np.zeros_like(max(audio_sources, key=np.size))
810
+
811
+ for v in audio_sources:
812
+ v = match_array_shapes(v, source, is_swap=is_swap)
813
+ source += v
814
+
815
+ return source
816
+
817
+ def combine_audio(paths: list, audio_file_base=None, wav_type_set='FLOAT', save_format=None):
818
+
819
+ source = combine_arrarys([load_audio(i) for i in paths])
820
+ save_path = f"{audio_file_base}_combined.wav"
821
+ sf.write(save_path, source.T, 44100, subtype=wav_type_set)
822
+ save_format(save_path)
823
+
824
+ def reduce_mix_bv(inst_source, voc_source, reduction_rate=0.9):
825
+ # Reduce the volume
826
+ inst_source = inst_source * (1 - reduction_rate)
827
+
828
+ mix_reduced = combine_arrarys([inst_source, voc_source], is_swap=True)
829
+
830
+ return mix_reduced
831
+
832
+ def organize_inputs(inputs):
833
+ input_list = {
834
+ "target":None,
835
+ "reference":None,
836
+ "reverb":None,
837
+ "inst":None
838
+ }
839
+
840
+ for i in inputs:
841
+ if i.endswith("_(Vocals).wav"):
842
+ input_list["reference"] = i
843
+ elif "_RVC_" in i:
844
+ input_list["target"] = i
845
+ elif i.endswith("reverbed_stem.wav"):
846
+ input_list["reverb"] = i
847
+ elif i.endswith("_(Instrumental).wav"):
848
+ input_list["inst"] = i
849
+
850
+ return input_list
851
+
852
+ def check_if_phase_inverted(wav1, wav2, is_mono=False):
853
+ # Load the audio files
854
+ if not is_mono:
855
+ wav1 = np.mean(wav1, axis=0)
856
+ wav2 = np.mean(wav2, axis=0)
857
+
858
+ # Compute the correlation
859
+ correlation = np.corrcoef(wav1[:1000], wav2[:1000])
860
+
861
+ return correlation[0,1] < 0
862
+
863
+ def align_audio(file1,
864
+ file2,
865
+ file2_aligned,
866
+ file_subtracted,
867
+ wav_type_set,
868
+ is_save_aligned,
869
+ command_Text,
870
+ save_format,
871
+ align_window:list,
872
+ align_intro_val:list,
873
+ db_analysis:tuple,
874
+ set_progress_bar,
875
+ phase_option,
876
+ phase_shifts,
877
+ is_match_silence,
878
+ is_spec_match):
879
+
880
+ global progress_value
881
+ progress_value = 0
882
+ is_mono = False
883
+
884
+ def get_diff(a, b):
885
+ corr = np.correlate(a, b, "full")
886
+ diff = corr.argmax() - (b.shape[0] - 1)
887
+
888
+ return diff
889
+
890
+ def progress_bar(length):
891
+ global progress_value
892
+ progress_value += 1
893
+
894
+ if (0.90/length*progress_value) >= 0.9:
895
+ length = progress_value + 1
896
+
897
+ set_progress_bar(0.1, (0.9/length*progress_value))
898
+
899
+ # read tracks
900
+
901
+ if file1.endswith(".mp3") and is_macos:
902
+ length1 = rerun_mp3(file1)
903
+ wav1, sr1 = librosa.load(file1, duration=length1, sr=44100, mono=False)
904
+ else:
905
+ wav1, sr1 = librosa.load(file1, sr=44100, mono=False)
906
+
907
+ if file2.endswith(".mp3") and is_macos:
908
+ length2 = rerun_mp3(file2)
909
+ wav2, sr2 = librosa.load(file2, duration=length2, sr=44100, mono=False)
910
+ else:
911
+ wav2, sr2 = librosa.load(file2, sr=44100, mono=False)
912
+
913
+ if wav1.ndim == 1 and wav2.ndim == 1:
914
+ is_mono = True
915
+ elif wav1.ndim == 1:
916
+ wav1 = np.asfortranarray([wav1,wav1])
917
+ elif wav2.ndim == 1:
918
+ wav2 = np.asfortranarray([wav2,wav2])
919
+
920
+ # Check if phase is inverted
921
+ if phase_option == AUTO_PHASE:
922
+ if check_if_phase_inverted(wav1, wav2, is_mono=is_mono):
923
+ wav2 = -wav2
924
+ elif phase_option == POSITIVE_PHASE:
925
+ wav2 = +wav2
926
+ elif phase_option == NEGATIVE_PHASE:
927
+ wav2 = -wav2
928
+
929
+ if is_match_silence:
930
+ wav2 = adjust_leading_silence(wav2, wav1)
931
+
932
+ wav1_length = int(librosa.get_duration(y=wav1, sr=44100))
933
+ wav2_length = int(librosa.get_duration(y=wav2, sr=44100))
934
+
935
+ if not is_mono:
936
+ wav1 = wav1.transpose()
937
+ wav2 = wav2.transpose()
938
+
939
+ wav2_org = wav2.copy()
940
+
941
+ command_Text("Processing files... \n")
942
+ seconds_length = min(wav1_length, wav2_length)
943
+
944
+ wav2_aligned_sources = []
945
+
946
+ for sec_len in align_intro_val:
947
+ # pick a position at 1 second in and get diff
948
+ sec_seg = 1 if sec_len == 1 else int(seconds_length // sec_len)
949
+ index = sr1*sec_seg # 1 second in, assuming sr1 = sr2 = 44100
950
+
951
+ if is_mono:
952
+ samp1, samp2 = wav1[index : index + sr1], wav2[index : index + sr1]
953
+ diff = get_diff(samp1, samp2)
954
+ #print(f"Estimated difference: {diff}\n")
955
+ else:
956
+ index = sr1*sec_seg # 1 second in, assuming sr1 = sr2 = 44100
957
+ samp1, samp2 = wav1[index : index + sr1, 0], wav2[index : index + sr1, 0]
958
+ samp1_r, samp2_r = wav1[index : index + sr1, 1], wav2[index : index + sr1, 1]
959
+ diff, diff_r = get_diff(samp1, samp2), get_diff(samp1_r, samp2_r)
960
+ #print(f"Estimated difference Left Channel: {diff}\nEstimated difference Right Channel: {diff_r}\n")
961
+
962
+ # make aligned track 2
963
+ if diff > 0:
964
+ zeros_to_append = np.zeros(diff) if is_mono else np.zeros((diff, 2))
965
+ wav2_aligned = np.append(zeros_to_append, wav2_org, axis=0)
966
+ elif diff < 0:
967
+ wav2_aligned = wav2_org[-diff:]
968
+ else:
969
+ wav2_aligned = wav2_org
970
+ #command_Text(f"Audio files already aligned.\n")
971
+
972
+ if not any(np.array_equal(wav2_aligned, source) for source in wav2_aligned_sources):
973
+ wav2_aligned_sources.append(wav2_aligned)
974
+
975
+ #print("Unique Sources: ", len(wav2_aligned_sources))
976
+
977
+ unique_sources = len(wav2_aligned_sources)
978
+
979
+ sub_mapper_big_mapper = {}
980
+
981
+ for s in wav2_aligned_sources:
982
+ wav2_aligned = match_mono_array_shapes(s, wav1) if is_mono else match_array_shapes(s, wav1, is_swap=True)
983
+
984
+ if align_window:
985
+ wav_sub = time_correction(wav1, wav2_aligned, seconds_length, align_window=align_window, db_analysis=db_analysis, progress_bar=progress_bar, unique_sources=unique_sources, phase_shifts=phase_shifts)
986
+ wav_sub_size = np.abs(wav_sub).mean()
987
+ sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size:wav_sub}}
988
+ else:
989
+ wav2_aligned = wav2_aligned * np.power(10, db_analysis[0] / 20)
990
+ db_range = db_analysis[1]
991
+
992
+ for db_adjustment in db_range:
993
+ # Adjust the dB of track2
994
+ s_adjusted = wav2_aligned * (10 ** (db_adjustment / 20))
995
+ wav_sub = wav1 - s_adjusted
996
+ wav_sub_size = np.abs(wav_sub).mean()
997
+ sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size:wav_sub}}
998
+
999
+ #print(sub_mapper_big_mapper.keys(), min(sub_mapper_big_mapper.keys()))
1000
+
1001
+ sub_mapper_value_list = list(sub_mapper_big_mapper.values())
1002
+
1003
+ if is_spec_match and len(sub_mapper_value_list) >= 2:
1004
+ #print("using spec ensemble with align")
1005
+ wav_sub = ensemble_for_align(list(sub_mapper_big_mapper.values()))
1006
+ else:
1007
+ #print("using linear ensemble with align")
1008
+ wav_sub = ensemble_wav(list(sub_mapper_big_mapper.values()))
1009
+
1010
+ #print(f"Mix Mean: {np.abs(wav1).mean()}\nInst Mean: {np.abs(wav2).mean()}")
1011
+ #print('Final: ', np.abs(wav_sub).mean())
1012
+ wav_sub = np.clip(wav_sub, -1, +1)
1013
+
1014
+ command_Text(f"Saving inverted track... ")
1015
+
1016
+ if is_save_aligned or is_spec_match:
1017
+ wav1 = match_mono_array_shapes(wav1, wav_sub) if is_mono else match_array_shapes(wav1, wav_sub, is_swap=True)
1018
+ wav2_aligned = wav1 - wav_sub
1019
+
1020
+ if is_spec_match:
1021
+ if wav1.ndim == 1 and wav2.ndim == 1:
1022
+ wav2_aligned = np.asfortranarray([wav2_aligned, wav2_aligned]).T
1023
+ wav1 = np.asfortranarray([wav1, wav1]).T
1024
+
1025
+ wav2_aligned = ensemble_for_align([wav2_aligned, wav1])
1026
+ wav_sub = wav1 - wav2_aligned
1027
+
1028
+ if is_save_aligned:
1029
+ sf.write(file2_aligned, wav2_aligned, sr1, subtype=wav_type_set)
1030
+ save_format(file2_aligned)
1031
+
1032
+ sf.write(file_subtracted, wav_sub, sr1, subtype=wav_type_set)
1033
+ save_format(file_subtracted)
1034
+
1035
+ def phase_shift_hilbert(signal, degree):
1036
+ analytic_signal = hilbert(signal)
1037
+ return np.cos(np.radians(degree)) * analytic_signal.real - np.sin(np.radians(degree)) * analytic_signal.imag
1038
+
1039
+ def get_phase_shifted_tracks(track, phase_shift):
1040
+ if phase_shift == 180:
1041
+ return [track, -track]
1042
+
1043
+ step = phase_shift
1044
+ end = 180 - (180 % step) if 180 % step == 0 else 181
1045
+ phase_range = range(step, end, step)
1046
+
1047
+ flipped_list = [track, -track]
1048
+ for i in phase_range:
1049
+ flipped_list.extend([phase_shift_hilbert(track, i), phase_shift_hilbert(track, -i)])
1050
+
1051
+ return flipped_list
1052
+
1053
+ def time_correction(mix:np.ndarray, instrumental:np.ndarray, seconds_length, align_window, db_analysis, sr=44100, progress_bar=None, unique_sources=None, phase_shifts=NONE_P):
1054
+ # Function to align two tracks using cross-correlation
1055
+
1056
+ def align_tracks(track1, track2):
1057
+ # A dictionary to store each version of track2_shifted and its mean absolute value
1058
+ shifted_tracks = {}
1059
+
1060
+ # Loop to adjust dB of track2
1061
+ track2 = track2 * np.power(10, db_analysis[0] / 20)
1062
+ db_range = db_analysis[1]
1063
+
1064
+ if phase_shifts == 190:
1065
+ track2_flipped = [track2]
1066
+ else:
1067
+ track2_flipped = get_phase_shifted_tracks(track2, phase_shifts)
1068
+
1069
+ for db_adjustment in db_range:
1070
+ for t in track2_flipped:
1071
+ # Adjust the dB of track2
1072
+ track2_adjusted = t * (10 ** (db_adjustment / 20))
1073
+ corr = correlate(track1, track2_adjusted)
1074
+ delay = np.argmax(np.abs(corr)) - (len(track1) - 1)
1075
+ track2_shifted = np.roll(track2_adjusted, shift=delay)
1076
+
1077
+ # Compute the mean absolute value of track2_shifted
1078
+ track2_shifted_sub = track1 - track2_shifted
1079
+ mean_abs_value = np.abs(track2_shifted_sub).mean()
1080
+
1081
+ # Store track2_shifted and its mean absolute value in the dictionary
1082
+ shifted_tracks[mean_abs_value] = track2_shifted
1083
+
1084
+ # Return the version of track2_shifted with the smallest mean absolute value
1085
+
1086
+ return shifted_tracks[min(shifted_tracks.keys())]
1087
+
1088
+ # Make sure the audio files have the same shape
1089
+
1090
+ assert mix.shape == instrumental.shape, f"Audio files must have the same shape - Mix: {mix.shape}, Inst: {instrumental.shape}"
1091
+
1092
+ seconds_length = seconds_length // 2
1093
+
1094
+ sub_mapper = {}
1095
+
1096
+ progress_update_interval = 120
1097
+ total_iterations = 0
1098
+
1099
+ if len(align_window) > 2:
1100
+ progress_update_interval = 320
1101
+
1102
+ for secs in align_window:
1103
+ step = secs / 2
1104
+ window_size = int(sr * secs)
1105
+ step_size = int(sr * step)
1106
+
1107
+ if len(mix.shape) == 1:
1108
+ total_mono = (len(range(0, len(mix) - window_size, step_size))//progress_update_interval)*unique_sources
1109
+ total_iterations += total_mono
1110
+ else:
1111
+ total_stereo_ = len(range(0, len(mix[:, 0]) - window_size, step_size))*2
1112
+ total_stereo = (total_stereo_//progress_update_interval) * unique_sources
1113
+ total_iterations += total_stereo
1114
+
1115
+ #print(total_iterations)
1116
+
1117
+ for secs in align_window:
1118
+ sub = np.zeros_like(mix)
1119
+ divider = np.zeros_like(mix)
1120
+ step = secs / 2
1121
+ window_size = int(sr * secs)
1122
+ step_size = int(sr * step)
1123
+ window = np.hanning(window_size)
1124
+
1125
+ # For the mono case:
1126
+ if len(mix.shape) == 1:
1127
+ # The files are mono
1128
+ counter = 0
1129
+ for i in range(0, len(mix) - window_size, step_size):
1130
+ counter += 1
1131
+ if counter % progress_update_interval == 0:
1132
+ progress_bar(total_iterations)
1133
+ window_mix = mix[i:i+window_size] * window
1134
+ window_instrumental = instrumental[i:i+window_size] * window
1135
+ window_instrumental_aligned = align_tracks(window_mix, window_instrumental)
1136
+ sub[i:i+window_size] += window_mix - window_instrumental_aligned
1137
+ divider[i:i+window_size] += window
1138
+ else:
1139
+ # The files are stereo
1140
+ counter = 0
1141
+ for ch in range(mix.shape[1]):
1142
+ for i in range(0, len(mix[:, ch]) - window_size, step_size):
1143
+ counter += 1
1144
+ if counter % progress_update_interval == 0:
1145
+ progress_bar(total_iterations)
1146
+ window_mix = mix[i:i+window_size, ch] * window
1147
+ window_instrumental = instrumental[i:i+window_size, ch] * window
1148
+ window_instrumental_aligned = align_tracks(window_mix, window_instrumental)
1149
+ sub[i:i+window_size, ch] += window_mix - window_instrumental_aligned
1150
+ divider[i:i+window_size, ch] += window
1151
+
1152
+ # Normalize the result by the overlap count
1153
+ sub = np.where(divider > 1e-6, sub / divider, sub)
1154
+ sub_size = np.abs(sub).mean()
1155
+ sub_mapper = {**sub_mapper, **{sub_size: sub}}
1156
+
1157
+ #print("SUB_LEN", len(list(sub_mapper.values())))
1158
+
1159
+ sub = ensemble_wav(list(sub_mapper.values()), split_size=12)
1160
+
1161
+ return sub
1162
+
1163
+ def ensemble_wav(waveforms, split_size=240):
1164
+ # Create a dictionary to hold the thirds of each waveform and their mean absolute values
1165
+ waveform_thirds = {i: np.array_split(waveform, split_size) for i, waveform in enumerate(waveforms)}
1166
+
1167
+ # Initialize the final waveform
1168
+ final_waveform = []
1169
+
1170
+ # For chunk
1171
+ for third_idx in range(split_size):
1172
+ # Compute the mean absolute value of each third from each waveform
1173
+ means = [np.abs(waveform_thirds[i][third_idx]).mean() for i in range(len(waveforms))]
1174
+
1175
+ # Find the index of the waveform with the lowest mean absolute value for this third
1176
+ min_index = np.argmin(means)
1177
+
1178
+ # Add the least noisy third to the final waveform
1179
+ final_waveform.append(waveform_thirds[min_index][third_idx])
1180
+
1181
+ # Concatenate all the thirds to create the final waveform
1182
+ final_waveform = np.concatenate(final_waveform)
1183
+
1184
+ return final_waveform
1185
+
1186
+ def ensemble_wav_min(waveforms):
1187
+ for i in range(1, len(waveforms)):
1188
+ if i == 1:
1189
+ wave = waveforms[0]
1190
+
1191
+ ln = min(len(wave), len(waveforms[i]))
1192
+ wave = wave[:ln]
1193
+ waveforms[i] = waveforms[i][:ln]
1194
+
1195
+ wave = np.where(np.abs(waveforms[i]) <= np.abs(wave), waveforms[i], wave)
1196
+
1197
+ return wave
1198
+
1199
+ def align_audio_test(wav1, wav2, sr1=44100):
1200
+ def get_diff(a, b):
1201
+ corr = np.correlate(a, b, "full")
1202
+ diff = corr.argmax() - (b.shape[0] - 1)
1203
+ return diff
1204
+
1205
+ # read tracks
1206
+ wav1 = wav1.transpose()
1207
+ wav2 = wav2.transpose()
1208
+
1209
+ #print(f"Audio file shapes: {wav1.shape} / {wav2.shape}\n")
1210
+
1211
+ wav2_org = wav2.copy()
1212
+
1213
+ # pick a position at 1 second in and get diff
1214
+ index = sr1#*seconds_length # 1 second in, assuming sr1 = sr2 = 44100
1215
+ samp1 = wav1[index : index + sr1, 0] # currently use left channel
1216
+ samp2 = wav2[index : index + sr1, 0]
1217
+ diff = get_diff(samp1, samp2)
1218
+
1219
+ # make aligned track 2
1220
+ if diff > 0:
1221
+ wav2_aligned = np.append(np.zeros((diff, 1)), wav2_org, axis=0)
1222
+ elif diff < 0:
1223
+ wav2_aligned = wav2_org[-diff:]
1224
+ else:
1225
+ wav2_aligned = wav2_org
1226
+
1227
+ return wav2_aligned
1228
+
1229
+ def load_audio(audio_file):
1230
+ wav, sr = librosa.load(audio_file, sr=44100, mono=False)
1231
+
1232
+ if wav.ndim == 1:
1233
+ wav = np.asfortranarray([wav,wav])
1234
+
1235
+ return wav
1236
+
1237
+ def rerun_mp3(audio_file):
1238
+ with audioread.audio_open(audio_file) as f:
1239
+ track_length = int(f.duration)
1240
+
1241
+ return track_length
lib_v5/tfc_tdf_v3.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from functools import partial
4
+
5
+ class STFT:
6
+ def __init__(self, n_fft, hop_length, dim_f, device):
7
+ self.n_fft = n_fft
8
+ self.hop_length = hop_length
9
+ self.window = torch.hann_window(window_length=self.n_fft, periodic=True)
10
+ self.dim_f = dim_f
11
+ self.device = device
12
+
13
+ def __call__(self, x):
14
+
15
+ x_is_mps = not x.device.type in ["cuda", "cpu"]
16
+ if x_is_mps:
17
+ x = x.cpu()
18
+
19
+ window = self.window.to(x.device)
20
+ batch_dims = x.shape[:-2]
21
+ c, t = x.shape[-2:]
22
+ x = x.reshape([-1, t])
23
+ x = torch.stft(x, n_fft=self.n_fft, hop_length=self.hop_length, window=window, center=True,return_complex=False)
24
+ x = x.permute([0, 3, 1, 2])
25
+ x = x.reshape([*batch_dims, c, 2, -1, x.shape[-1]]).reshape([*batch_dims, c * 2, -1, x.shape[-1]])
26
+
27
+ if x_is_mps:
28
+ x = x.to(self.device)
29
+
30
+ return x[..., :self.dim_f, :]
31
+
32
+ def inverse(self, x):
33
+
34
+ x_is_mps = not x.device.type in ["cuda", "cpu"]
35
+ if x_is_mps:
36
+ x = x.cpu()
37
+
38
+ window = self.window.to(x.device)
39
+ batch_dims = x.shape[:-3]
40
+ c, f, t = x.shape[-3:]
41
+ n = self.n_fft // 2 + 1
42
+ f_pad = torch.zeros([*batch_dims, c, n - f, t]).to(x.device)
43
+ x = torch.cat([x, f_pad], -2)
44
+ x = x.reshape([*batch_dims, c // 2, 2, n, t]).reshape([-1, 2, n, t])
45
+ x = x.permute([0, 2, 3, 1])
46
+ x = x[..., 0] + x[..., 1] * 1.j
47
+ x = torch.istft(x, n_fft=self.n_fft, hop_length=self.hop_length, window=window, center=True)
48
+ x = x.reshape([*batch_dims, 2, -1])
49
+
50
+ if x_is_mps:
51
+ x = x.to(self.device)
52
+
53
+ return x
54
+
55
+ def get_norm(norm_type):
56
+ def norm(c, norm_type):
57
+ if norm_type == 'BatchNorm':
58
+ return nn.BatchNorm2d(c)
59
+ elif norm_type == 'InstanceNorm':
60
+ return nn.InstanceNorm2d(c, affine=True)
61
+ elif 'GroupNorm' in norm_type:
62
+ g = int(norm_type.replace('GroupNorm', ''))
63
+ return nn.GroupNorm(num_groups=g, num_channels=c)
64
+ else:
65
+ return nn.Identity()
66
+
67
+ return partial(norm, norm_type=norm_type)
68
+
69
+
70
+ def get_act(act_type):
71
+ if act_type == 'gelu':
72
+ return nn.GELU()
73
+ elif act_type == 'relu':
74
+ return nn.ReLU()
75
+ elif act_type[:3] == 'elu':
76
+ alpha = float(act_type.replace('elu', ''))
77
+ return nn.ELU(alpha)
78
+ else:
79
+ raise Exception
80
+
81
+
82
+ class Upscale(nn.Module):
83
+ def __init__(self, in_c, out_c, scale, norm, act):
84
+ super().__init__()
85
+ self.conv = nn.Sequential(
86
+ norm(in_c),
87
+ act,
88
+ nn.ConvTranspose2d(in_channels=in_c, out_channels=out_c, kernel_size=scale, stride=scale, bias=False)
89
+ )
90
+
91
+ def forward(self, x):
92
+ return self.conv(x)
93
+
94
+
95
+ class Downscale(nn.Module):
96
+ def __init__(self, in_c, out_c, scale, norm, act):
97
+ super().__init__()
98
+ self.conv = nn.Sequential(
99
+ norm(in_c),
100
+ act,
101
+ nn.Conv2d(in_channels=in_c, out_channels=out_c, kernel_size=scale, stride=scale, bias=False)
102
+ )
103
+
104
+ def forward(self, x):
105
+ return self.conv(x)
106
+
107
+
108
+ class TFC_TDF(nn.Module):
109
+ def __init__(self, in_c, c, l, f, bn, norm, act):
110
+ super().__init__()
111
+
112
+ self.blocks = nn.ModuleList()
113
+ for i in range(l):
114
+ block = nn.Module()
115
+
116
+ block.tfc1 = nn.Sequential(
117
+ norm(in_c),
118
+ act,
119
+ nn.Conv2d(in_c, c, 3, 1, 1, bias=False),
120
+ )
121
+ block.tdf = nn.Sequential(
122
+ norm(c),
123
+ act,
124
+ nn.Linear(f, f // bn, bias=False),
125
+ norm(c),
126
+ act,
127
+ nn.Linear(f // bn, f, bias=False),
128
+ )
129
+ block.tfc2 = nn.Sequential(
130
+ norm(c),
131
+ act,
132
+ nn.Conv2d(c, c, 3, 1, 1, bias=False),
133
+ )
134
+ block.shortcut = nn.Conv2d(in_c, c, 1, 1, 0, bias=False)
135
+
136
+ self.blocks.append(block)
137
+ in_c = c
138
+
139
+ def forward(self, x):
140
+ for block in self.blocks:
141
+ s = block.shortcut(x)
142
+ x = block.tfc1(x)
143
+ x = x + block.tdf(x)
144
+ x = block.tfc2(x)
145
+ x = x + s
146
+ return x
147
+
148
+
149
+ class TFC_TDF_net(nn.Module):
150
+ def __init__(self, config, device):
151
+ super().__init__()
152
+ self.config = config
153
+ self.device = device
154
+
155
+ norm = get_norm(norm_type=config.model.norm)
156
+ act = get_act(act_type=config.model.act)
157
+
158
+ self.num_target_instruments = 1 if config.training.target_instrument else len(config.training.instruments)
159
+ self.num_subbands = config.model.num_subbands
160
+
161
+ dim_c = self.num_subbands * config.audio.num_channels * 2
162
+ n = config.model.num_scales
163
+ scale = config.model.scale
164
+ l = config.model.num_blocks_per_scale
165
+ c = config.model.num_channels
166
+ g = config.model.growth
167
+ bn = config.model.bottleneck_factor
168
+ f = config.audio.dim_f // self.num_subbands
169
+
170
+ self.first_conv = nn.Conv2d(dim_c, c, 1, 1, 0, bias=False)
171
+
172
+ self.encoder_blocks = nn.ModuleList()
173
+ for i in range(n):
174
+ block = nn.Module()
175
+ block.tfc_tdf = TFC_TDF(c, c, l, f, bn, norm, act)
176
+ block.downscale = Downscale(c, c + g, scale, norm, act)
177
+ f = f // scale[1]
178
+ c += g
179
+ self.encoder_blocks.append(block)
180
+
181
+ self.bottleneck_block = TFC_TDF(c, c, l, f, bn, norm, act)
182
+
183
+ self.decoder_blocks = nn.ModuleList()
184
+ for i in range(n):
185
+ block = nn.Module()
186
+ block.upscale = Upscale(c, c - g, scale, norm, act)
187
+ f = f * scale[1]
188
+ c -= g
189
+ block.tfc_tdf = TFC_TDF(2 * c, c, l, f, bn, norm, act)
190
+ self.decoder_blocks.append(block)
191
+
192
+ self.final_conv = nn.Sequential(
193
+ nn.Conv2d(c + dim_c, c, 1, 1, 0, bias=False),
194
+ act,
195
+ nn.Conv2d(c, self.num_target_instruments * dim_c, 1, 1, 0, bias=False)
196
+ )
197
+
198
+ self.stft = STFT(config.audio.n_fft, config.audio.hop_length, config.audio.dim_f, self.device)
199
+
200
+ def cac2cws(self, x):
201
+ k = self.num_subbands
202
+ b, c, f, t = x.shape
203
+ x = x.reshape(b, c, k, f // k, t)
204
+ x = x.reshape(b, c * k, f // k, t)
205
+ return x
206
+
207
+ def cws2cac(self, x):
208
+ k = self.num_subbands
209
+ b, c, f, t = x.shape
210
+ x = x.reshape(b, c // k, k, f, t)
211
+ x = x.reshape(b, c // k, f * k, t)
212
+ return x
213
+
214
+ def forward(self, x):
215
+
216
+ x = self.stft(x)
217
+
218
+ mix = x = self.cac2cws(x)
219
+
220
+ first_conv_out = x = self.first_conv(x)
221
+
222
+ x = x.transpose(-1, -2)
223
+
224
+ encoder_outputs = []
225
+ for block in self.encoder_blocks:
226
+ x = block.tfc_tdf(x)
227
+ encoder_outputs.append(x)
228
+ x = block.downscale(x)
229
+
230
+ x = self.bottleneck_block(x)
231
+
232
+ for block in self.decoder_blocks:
233
+ x = block.upscale(x)
234
+ x = torch.cat([x, encoder_outputs.pop()], 1)
235
+ x = block.tfc_tdf(x)
236
+
237
+ x = x.transpose(-1, -2)
238
+
239
+ x = x * first_conv_out # reduce artifacts
240
+
241
+ x = self.final_conv(torch.cat([mix, x], 1))
242
+
243
+ x = self.cws2cac(x)
244
+
245
+ if self.num_target_instruments > 1:
246
+ b, c, f, t = x.shape
247
+ x = x.reshape(b, self.num_target_instruments, -1, f, t)
248
+
249
+ x = self.stft.inverse(x)
250
+
251
+ return x
252
+
253
+
lib_v5/vr_network/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ # VR init.
lib_v5/vr_network/layers.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+ from lib_v5 import spec_utils
6
+
7
+ class Conv2DBNActiv(nn.Module):
8
+
9
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
10
+ super(Conv2DBNActiv, self).__init__()
11
+ self.conv = nn.Sequential(
12
+ nn.Conv2d(
13
+ nin, nout,
14
+ kernel_size=ksize,
15
+ stride=stride,
16
+ padding=pad,
17
+ dilation=dilation,
18
+ bias=False),
19
+ nn.BatchNorm2d(nout),
20
+ activ()
21
+ )
22
+
23
+ def __call__(self, x):
24
+ return self.conv(x)
25
+
26
+ class SeperableConv2DBNActiv(nn.Module):
27
+
28
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
29
+ super(SeperableConv2DBNActiv, self).__init__()
30
+ self.conv = nn.Sequential(
31
+ nn.Conv2d(
32
+ nin, nin,
33
+ kernel_size=ksize,
34
+ stride=stride,
35
+ padding=pad,
36
+ dilation=dilation,
37
+ groups=nin,
38
+ bias=False),
39
+ nn.Conv2d(
40
+ nin, nout,
41
+ kernel_size=1,
42
+ bias=False),
43
+ nn.BatchNorm2d(nout),
44
+ activ()
45
+ )
46
+
47
+ def __call__(self, x):
48
+ return self.conv(x)
49
+
50
+
51
+ class Encoder(nn.Module):
52
+
53
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
54
+ super(Encoder, self).__init__()
55
+ self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
56
+ self.conv2 = Conv2DBNActiv(nout, nout, ksize, stride, pad, activ=activ)
57
+
58
+ def __call__(self, x):
59
+ skip = self.conv1(x)
60
+ h = self.conv2(skip)
61
+
62
+ return h, skip
63
+
64
+
65
+ class Decoder(nn.Module):
66
+
67
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
68
+ super(Decoder, self).__init__()
69
+ self.conv = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
70
+ self.dropout = nn.Dropout2d(0.1) if dropout else None
71
+
72
+ def __call__(self, x, skip=None):
73
+ x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
74
+ if skip is not None:
75
+ skip = spec_utils.crop_center(skip, x)
76
+ x = torch.cat([x, skip], dim=1)
77
+ h = self.conv(x)
78
+
79
+ if self.dropout is not None:
80
+ h = self.dropout(h)
81
+
82
+ return h
83
+
84
+
85
+ class ASPPModule(nn.Module):
86
+
87
+ def __init__(self, nn_architecture, nin, nout, dilations=(4, 8, 16), activ=nn.ReLU):
88
+ super(ASPPModule, self).__init__()
89
+ self.conv1 = nn.Sequential(
90
+ nn.AdaptiveAvgPool2d((1, None)),
91
+ Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
92
+ )
93
+
94
+ self.nn_architecture = nn_architecture
95
+ self.six_layer = [129605]
96
+ self.seven_layer = [537238, 537227, 33966]
97
+
98
+ extra_conv = SeperableConv2DBNActiv(
99
+ nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
100
+
101
+ self.conv2 = Conv2DBNActiv(nin, nin, 1, 1, 0, activ=activ)
102
+ self.conv3 = SeperableConv2DBNActiv(
103
+ nin, nin, 3, 1, dilations[0], dilations[0], activ=activ)
104
+ self.conv4 = SeperableConv2DBNActiv(
105
+ nin, nin, 3, 1, dilations[1], dilations[1], activ=activ)
106
+ self.conv5 = SeperableConv2DBNActiv(
107
+ nin, nin, 3, 1, dilations[2], dilations[2], activ=activ)
108
+
109
+ if self.nn_architecture in self.six_layer:
110
+ self.conv6 = extra_conv
111
+ nin_x = 6
112
+ elif self.nn_architecture in self.seven_layer:
113
+ self.conv6 = extra_conv
114
+ self.conv7 = extra_conv
115
+ nin_x = 7
116
+ else:
117
+ nin_x = 5
118
+
119
+ self.bottleneck = nn.Sequential(
120
+ Conv2DBNActiv(nin * nin_x, nout, 1, 1, 0, activ=activ),
121
+ nn.Dropout2d(0.1)
122
+ )
123
+
124
+ def forward(self, x):
125
+ _, _, h, w = x.size()
126
+ feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
127
+ feat2 = self.conv2(x)
128
+ feat3 = self.conv3(x)
129
+ feat4 = self.conv4(x)
130
+ feat5 = self.conv5(x)
131
+
132
+ if self.nn_architecture in self.six_layer:
133
+ feat6 = self.conv6(x)
134
+ out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6), dim=1)
135
+ elif self.nn_architecture in self.seven_layer:
136
+ feat6 = self.conv6(x)
137
+ feat7 = self.conv7(x)
138
+ out = torch.cat((feat1, feat2, feat3, feat4, feat5, feat6, feat7), dim=1)
139
+ else:
140
+ out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
141
+
142
+ bottle = self.bottleneck(out)
143
+ return bottle
lib_v5/vr_network/layers_new.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+ from lib_v5 import spec_utils
6
+
7
+ class Conv2DBNActiv(nn.Module):
8
+
9
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, dilation=1, activ=nn.ReLU):
10
+ super(Conv2DBNActiv, self).__init__()
11
+ self.conv = nn.Sequential(
12
+ nn.Conv2d(
13
+ nin, nout,
14
+ kernel_size=ksize,
15
+ stride=stride,
16
+ padding=pad,
17
+ dilation=dilation,
18
+ bias=False),
19
+ nn.BatchNorm2d(nout),
20
+ activ()
21
+ )
22
+
23
+ def __call__(self, x):
24
+ return self.conv(x)
25
+
26
+ class Encoder(nn.Module):
27
+
28
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.LeakyReLU):
29
+ super(Encoder, self).__init__()
30
+ self.conv1 = Conv2DBNActiv(nin, nout, ksize, stride, pad, activ=activ)
31
+ self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ)
32
+
33
+ def __call__(self, x):
34
+ h = self.conv1(x)
35
+ h = self.conv2(h)
36
+
37
+ return h
38
+
39
+
40
+ class Decoder(nn.Module):
41
+
42
+ def __init__(self, nin, nout, ksize=3, stride=1, pad=1, activ=nn.ReLU, dropout=False):
43
+ super(Decoder, self).__init__()
44
+ self.conv1 = Conv2DBNActiv(nin, nout, ksize, 1, pad, activ=activ)
45
+ # self.conv2 = Conv2DBNActiv(nout, nout, ksize, 1, pad, activ=activ)
46
+ self.dropout = nn.Dropout2d(0.1) if dropout else None
47
+
48
+ def __call__(self, x, skip=None):
49
+ x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
50
+
51
+ if skip is not None:
52
+ skip = spec_utils.crop_center(skip, x)
53
+ x = torch.cat([x, skip], dim=1)
54
+
55
+ h = self.conv1(x)
56
+ # h = self.conv2(h)
57
+
58
+ if self.dropout is not None:
59
+ h = self.dropout(h)
60
+
61
+ return h
62
+
63
+
64
+ class ASPPModule(nn.Module):
65
+
66
+ def __init__(self, nin, nout, dilations=(4, 8, 12), activ=nn.ReLU, dropout=False):
67
+ super(ASPPModule, self).__init__()
68
+ self.conv1 = nn.Sequential(
69
+ nn.AdaptiveAvgPool2d((1, None)),
70
+ Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ)
71
+ )
72
+ self.conv2 = Conv2DBNActiv(nin, nout, 1, 1, 0, activ=activ)
73
+ self.conv3 = Conv2DBNActiv(
74
+ nin, nout, 3, 1, dilations[0], dilations[0], activ=activ
75
+ )
76
+ self.conv4 = Conv2DBNActiv(
77
+ nin, nout, 3, 1, dilations[1], dilations[1], activ=activ
78
+ )
79
+ self.conv5 = Conv2DBNActiv(
80
+ nin, nout, 3, 1, dilations[2], dilations[2], activ=activ
81
+ )
82
+ self.bottleneck = Conv2DBNActiv(nout * 5, nout, 1, 1, 0, activ=activ)
83
+ self.dropout = nn.Dropout2d(0.1) if dropout else None
84
+
85
+ def forward(self, x):
86
+ _, _, h, w = x.size()
87
+ feat1 = F.interpolate(self.conv1(x), size=(h, w), mode='bilinear', align_corners=True)
88
+ feat2 = self.conv2(x)
89
+ feat3 = self.conv3(x)
90
+ feat4 = self.conv4(x)
91
+ feat5 = self.conv5(x)
92
+ out = torch.cat((feat1, feat2, feat3, feat4, feat5), dim=1)
93
+ out = self.bottleneck(out)
94
+
95
+ if self.dropout is not None:
96
+ out = self.dropout(out)
97
+
98
+ return out
99
+
100
+
101
+ class LSTMModule(nn.Module):
102
+
103
+ def __init__(self, nin_conv, nin_lstm, nout_lstm):
104
+ super(LSTMModule, self).__init__()
105
+ self.conv = Conv2DBNActiv(nin_conv, 1, 1, 1, 0)
106
+ self.lstm = nn.LSTM(
107
+ input_size=nin_lstm,
108
+ hidden_size=nout_lstm // 2,
109
+ bidirectional=True
110
+ )
111
+ self.dense = nn.Sequential(
112
+ nn.Linear(nout_lstm, nin_lstm),
113
+ nn.BatchNorm1d(nin_lstm),
114
+ nn.ReLU()
115
+ )
116
+
117
+ def forward(self, x):
118
+ N, _, nbins, nframes = x.size()
119
+ h = self.conv(x)[:, 0] # N, nbins, nframes
120
+ h = h.permute(2, 0, 1) # nframes, N, nbins
121
+ h, _ = self.lstm(h)
122
+ h = self.dense(h.reshape(-1, h.size()[-1])) # nframes * N, nbins
123
+ h = h.reshape(nframes, N, 1, nbins)
124
+ h = h.permute(1, 2, 3, 0)
125
+
126
+ return h
lib_v5/vr_network/model_param_init.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ default_param = {}
4
+ default_param['bins'] = -1
5
+ default_param['unstable_bins'] = -1 # training only
6
+ default_param['stable_bins'] = -1 # training only
7
+ default_param['sr'] = 44100
8
+ default_param['pre_filter_start'] = -1
9
+ default_param['pre_filter_stop'] = -1
10
+ default_param['band'] = {}
11
+
12
+ N_BINS = 'n_bins'
13
+
14
+ def int_keys(d):
15
+ r = {}
16
+ for k, v in d:
17
+ if k.isdigit():
18
+ k = int(k)
19
+ r[k] = v
20
+ return r
21
+
22
+ class ModelParameters(object):
23
+ def __init__(self, config_path=''):
24
+ with open(config_path, 'r') as f:
25
+ self.param = json.loads(f.read(), object_pairs_hook=int_keys)
26
+
27
+ for k in ['mid_side', 'mid_side_b', 'mid_side_b2', 'stereo_w', 'stereo_n', 'reverse']:
28
+ if not k in self.param:
29
+ self.param[k] = False
30
+
31
+ if N_BINS in self.param:
32
+ self.param['bins'] = self.param[N_BINS]
lib_v5/vr_network/modelparams/1band_sr16000_hl512.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 16000,
8
+ "hl": 512,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 1024,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 16000,
17
+ "pre_filter_start": 1023,
18
+ "pre_filter_stop": 1024
19
+ }
lib_v5/vr_network/modelparams/1band_sr32000_hl512.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 32000,
8
+ "hl": 512,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 1024,
12
+ "hpf_start": -1,
13
+ "res_type": "kaiser_fast"
14
+ }
15
+ },
16
+ "sr": 32000,
17
+ "pre_filter_start": 1000,
18
+ "pre_filter_stop": 1021
19
+ }
lib_v5/vr_network/modelparams/1band_sr33075_hl384.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 33075,
8
+ "hl": 384,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 1024,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 33075,
17
+ "pre_filter_start": 1000,
18
+ "pre_filter_stop": 1021
19
+ }
lib_v5/vr_network/modelparams/1band_sr44100_hl1024.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 44100,
8
+ "hl": 1024,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 1024,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 44100,
17
+ "pre_filter_start": 1023,
18
+ "pre_filter_stop": 1024
19
+ }
lib_v5/vr_network/modelparams/1band_sr44100_hl256.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 256,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 44100,
8
+ "hl": 256,
9
+ "n_fft": 512,
10
+ "crop_start": 0,
11
+ "crop_stop": 256,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 44100,
17
+ "pre_filter_start": 256,
18
+ "pre_filter_stop": 256
19
+ }
lib_v5/vr_network/modelparams/1band_sr44100_hl512.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 44100,
8
+ "hl": 512,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 1024,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 44100,
17
+ "pre_filter_start": 1023,
18
+ "pre_filter_stop": 1024
19
+ }
lib_v5/vr_network/modelparams/1band_sr44100_hl512_cut.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 1024,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 44100,
8
+ "hl": 512,
9
+ "n_fft": 2048,
10
+ "crop_start": 0,
11
+ "crop_stop": 700,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 44100,
17
+ "pre_filter_start": 1023,
18
+ "pre_filter_stop": 700
19
+ }
lib_v5/vr_network/modelparams/1band_sr44100_hl512_nf1024.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 512,
3
+ "unstable_bins": 0,
4
+ "reduction_bins": 0,
5
+ "band": {
6
+ "1": {
7
+ "sr": 44100,
8
+ "hl": 512,
9
+ "n_fft": 1024,
10
+ "crop_start": 0,
11
+ "crop_stop": 512,
12
+ "hpf_start": -1,
13
+ "res_type": "sinc_best"
14
+ }
15
+ },
16
+ "sr": 44100,
17
+ "pre_filter_start": 511,
18
+ "pre_filter_stop": 512
19
+ }
lib_v5/vr_network/modelparams/2band_32000.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 768,
3
+ "unstable_bins": 7,
4
+ "reduction_bins": 705,
5
+ "band": {
6
+ "1": {
7
+ "sr": 6000,
8
+ "hl": 66,
9
+ "n_fft": 512,
10
+ "crop_start": 0,
11
+ "crop_stop": 240,
12
+ "lpf_start": 60,
13
+ "lpf_stop": 118,
14
+ "res_type": "sinc_fastest"
15
+ },
16
+ "2": {
17
+ "sr": 32000,
18
+ "hl": 352,
19
+ "n_fft": 1024,
20
+ "crop_start": 22,
21
+ "crop_stop": 505,
22
+ "hpf_start": 44,
23
+ "hpf_stop": 23,
24
+ "res_type": "sinc_medium"
25
+ }
26
+ },
27
+ "sr": 32000,
28
+ "pre_filter_start": 710,
29
+ "pre_filter_stop": 731
30
+ }
lib_v5/vr_network/modelparams/2band_44100_lofi.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 512,
3
+ "unstable_bins": 7,
4
+ "reduction_bins": 510,
5
+ "band": {
6
+ "1": {
7
+ "sr": 11025,
8
+ "hl": 160,
9
+ "n_fft": 768,
10
+ "crop_start": 0,
11
+ "crop_stop": 192,
12
+ "lpf_start": 41,
13
+ "lpf_stop": 139,
14
+ "res_type": "sinc_fastest"
15
+ },
16
+ "2": {
17
+ "sr": 44100,
18
+ "hl": 640,
19
+ "n_fft": 1024,
20
+ "crop_start": 10,
21
+ "crop_stop": 320,
22
+ "hpf_start": 47,
23
+ "hpf_stop": 15,
24
+ "res_type": "sinc_medium"
25
+ }
26
+ },
27
+ "sr": 44100,
28
+ "pre_filter_start": 510,
29
+ "pre_filter_stop": 512
30
+ }
lib_v5/vr_network/modelparams/2band_48000.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 768,
3
+ "unstable_bins": 7,
4
+ "reduction_bins": 705,
5
+ "band": {
6
+ "1": {
7
+ "sr": 6000,
8
+ "hl": 66,
9
+ "n_fft": 512,
10
+ "crop_start": 0,
11
+ "crop_stop": 240,
12
+ "lpf_start": 60,
13
+ "lpf_stop": 240,
14
+ "res_type": "sinc_fastest"
15
+ },
16
+ "2": {
17
+ "sr": 48000,
18
+ "hl": 528,
19
+ "n_fft": 1536,
20
+ "crop_start": 22,
21
+ "crop_stop": 505,
22
+ "hpf_start": 82,
23
+ "hpf_stop": 22,
24
+ "res_type": "sinc_medium"
25
+ }
26
+ },
27
+ "sr": 48000,
28
+ "pre_filter_start": 710,
29
+ "pre_filter_stop": 731
30
+ }
lib_v5/vr_network/modelparams/3band_44100.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 768,
3
+ "unstable_bins": 5,
4
+ "reduction_bins": 733,
5
+ "band": {
6
+ "1": {
7
+ "sr": 11025,
8
+ "hl": 128,
9
+ "n_fft": 768,
10
+ "crop_start": 0,
11
+ "crop_stop": 278,
12
+ "lpf_start": 28,
13
+ "lpf_stop": 140,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 22050,
18
+ "hl": 256,
19
+ "n_fft": 768,
20
+ "crop_start": 14,
21
+ "crop_stop": 322,
22
+ "hpf_start": 70,
23
+ "hpf_stop": 14,
24
+ "lpf_start": 283,
25
+ "lpf_stop": 314,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 44100,
30
+ "hl": 512,
31
+ "n_fft": 768,
32
+ "crop_start": 131,
33
+ "crop_stop": 313,
34
+ "hpf_start": 154,
35
+ "hpf_stop": 141,
36
+ "res_type": "sinc_medium"
37
+ }
38
+ },
39
+ "sr": 44100,
40
+ "pre_filter_start": 757,
41
+ "pre_filter_stop": 768
42
+ }
lib_v5/vr_network/modelparams/3band_44100_mid.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mid_side": true,
3
+ "bins": 768,
4
+ "unstable_bins": 5,
5
+ "reduction_bins": 733,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 768,
11
+ "crop_start": 0,
12
+ "crop_stop": 278,
13
+ "lpf_start": 28,
14
+ "lpf_stop": 140,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 22050,
19
+ "hl": 256,
20
+ "n_fft": 768,
21
+ "crop_start": 14,
22
+ "crop_stop": 322,
23
+ "hpf_start": 70,
24
+ "hpf_stop": 14,
25
+ "lpf_start": 283,
26
+ "lpf_stop": 314,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 44100,
31
+ "hl": 512,
32
+ "n_fft": 768,
33
+ "crop_start": 131,
34
+ "crop_stop": 313,
35
+ "hpf_start": 154,
36
+ "hpf_stop": 141,
37
+ "res_type": "sinc_medium"
38
+ }
39
+ },
40
+ "sr": 44100,
41
+ "pre_filter_start": 757,
42
+ "pre_filter_stop": 768
43
+ }
lib_v5/vr_network/modelparams/3band_44100_msb2.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mid_side_b2": true,
3
+ "bins": 640,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 565,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 108,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 187,
13
+ "lpf_start": 92,
14
+ "lpf_stop": 186,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 22050,
19
+ "hl": 216,
20
+ "n_fft": 768,
21
+ "crop_start": 0,
22
+ "crop_stop": 212,
23
+ "hpf_start": 68,
24
+ "hpf_stop": 34,
25
+ "lpf_start": 174,
26
+ "lpf_stop": 209,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 44100,
31
+ "hl": 432,
32
+ "n_fft": 640,
33
+ "crop_start": 66,
34
+ "crop_stop": 307,
35
+ "hpf_start": 86,
36
+ "hpf_stop": 72,
37
+ "res_type": "kaiser_fast"
38
+ }
39
+ },
40
+ "sr": 44100,
41
+ "pre_filter_start": 639,
42
+ "pre_filter_stop": 640
43
+ }
lib_v5/vr_network/modelparams/4band_44100.json ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 768,
3
+ "unstable_bins": 7,
4
+ "reduction_bins": 668,
5
+ "band": {
6
+ "1": {
7
+ "sr": 11025,
8
+ "hl": 128,
9
+ "n_fft": 1024,
10
+ "crop_start": 0,
11
+ "crop_stop": 186,
12
+ "lpf_start": 37,
13
+ "lpf_stop": 73,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 11025,
18
+ "hl": 128,
19
+ "n_fft": 512,
20
+ "crop_start": 4,
21
+ "crop_stop": 185,
22
+ "hpf_start": 36,
23
+ "hpf_stop": 18,
24
+ "lpf_start": 93,
25
+ "lpf_stop": 185,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 22050,
30
+ "hl": 256,
31
+ "n_fft": 512,
32
+ "crop_start": 46,
33
+ "crop_stop": 186,
34
+ "hpf_start": 93,
35
+ "hpf_stop": 46,
36
+ "lpf_start": 164,
37
+ "lpf_stop": 186,
38
+ "res_type": "polyphase"
39
+ },
40
+ "4": {
41
+ "sr": 44100,
42
+ "hl": 512,
43
+ "n_fft": 768,
44
+ "crop_start": 121,
45
+ "crop_stop": 382,
46
+ "hpf_start": 138,
47
+ "hpf_stop": 123,
48
+ "res_type": "sinc_medium"
49
+ }
50
+ },
51
+ "sr": 44100,
52
+ "pre_filter_start": 740,
53
+ "pre_filter_stop": 768
54
+ }
lib_v5/vr_network/modelparams/4band_44100_mid.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 768,
3
+ "unstable_bins": 7,
4
+ "mid_side": true,
5
+ "reduction_bins": 668,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 186,
13
+ "lpf_start": 37,
14
+ "lpf_stop": 73,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 11025,
19
+ "hl": 128,
20
+ "n_fft": 512,
21
+ "crop_start": 4,
22
+ "crop_stop": 185,
23
+ "hpf_start": 36,
24
+ "hpf_stop": 18,
25
+ "lpf_start": 93,
26
+ "lpf_stop": 185,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 22050,
31
+ "hl": 256,
32
+ "n_fft": 512,
33
+ "crop_start": 46,
34
+ "crop_stop": 186,
35
+ "hpf_start": 93,
36
+ "hpf_stop": 46,
37
+ "lpf_start": 164,
38
+ "lpf_stop": 186,
39
+ "res_type": "polyphase"
40
+ },
41
+ "4": {
42
+ "sr": 44100,
43
+ "hl": 512,
44
+ "n_fft": 768,
45
+ "crop_start": 121,
46
+ "crop_stop": 382,
47
+ "hpf_start": 138,
48
+ "hpf_stop": 123,
49
+ "res_type": "sinc_medium"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 740,
54
+ "pre_filter_stop": 768
55
+ }
lib_v5/vr_network/modelparams/4band_44100_msb.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mid_side_b": true,
3
+ "bins": 768,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 668,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 186,
13
+ "lpf_start": 37,
14
+ "lpf_stop": 73,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 11025,
19
+ "hl": 128,
20
+ "n_fft": 512,
21
+ "crop_start": 4,
22
+ "crop_stop": 185,
23
+ "hpf_start": 36,
24
+ "hpf_stop": 18,
25
+ "lpf_start": 93,
26
+ "lpf_stop": 185,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 22050,
31
+ "hl": 256,
32
+ "n_fft": 512,
33
+ "crop_start": 46,
34
+ "crop_stop": 186,
35
+ "hpf_start": 93,
36
+ "hpf_stop": 46,
37
+ "lpf_start": 164,
38
+ "lpf_stop": 186,
39
+ "res_type": "polyphase"
40
+ },
41
+ "4": {
42
+ "sr": 44100,
43
+ "hl": 512,
44
+ "n_fft": 768,
45
+ "crop_start": 121,
46
+ "crop_stop": 382,
47
+ "hpf_start": 138,
48
+ "hpf_stop": 123,
49
+ "res_type": "sinc_medium"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 740,
54
+ "pre_filter_stop": 768
55
+ }
lib_v5/vr_network/modelparams/4band_44100_msb2.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mid_side_b": true,
3
+ "bins": 768,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 668,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 186,
13
+ "lpf_start": 37,
14
+ "lpf_stop": 73,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 11025,
19
+ "hl": 128,
20
+ "n_fft": 512,
21
+ "crop_start": 4,
22
+ "crop_stop": 185,
23
+ "hpf_start": 36,
24
+ "hpf_stop": 18,
25
+ "lpf_start": 93,
26
+ "lpf_stop": 185,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 22050,
31
+ "hl": 256,
32
+ "n_fft": 512,
33
+ "crop_start": 46,
34
+ "crop_stop": 186,
35
+ "hpf_start": 93,
36
+ "hpf_stop": 46,
37
+ "lpf_start": 164,
38
+ "lpf_stop": 186,
39
+ "res_type": "polyphase"
40
+ },
41
+ "4": {
42
+ "sr": 44100,
43
+ "hl": 512,
44
+ "n_fft": 768,
45
+ "crop_start": 121,
46
+ "crop_stop": 382,
47
+ "hpf_start": 138,
48
+ "hpf_stop": 123,
49
+ "res_type": "sinc_medium"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 740,
54
+ "pre_filter_stop": 768
55
+ }
lib_v5/vr_network/modelparams/4band_44100_reverse.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "reverse": true,
3
+ "bins": 768,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 668,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 186,
13
+ "lpf_start": 37,
14
+ "lpf_stop": 73,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 11025,
19
+ "hl": 128,
20
+ "n_fft": 512,
21
+ "crop_start": 4,
22
+ "crop_stop": 185,
23
+ "hpf_start": 36,
24
+ "hpf_stop": 18,
25
+ "lpf_start": 93,
26
+ "lpf_stop": 185,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 22050,
31
+ "hl": 256,
32
+ "n_fft": 512,
33
+ "crop_start": 46,
34
+ "crop_stop": 186,
35
+ "hpf_start": 93,
36
+ "hpf_stop": 46,
37
+ "lpf_start": 164,
38
+ "lpf_stop": 186,
39
+ "res_type": "polyphase"
40
+ },
41
+ "4": {
42
+ "sr": 44100,
43
+ "hl": 512,
44
+ "n_fft": 768,
45
+ "crop_start": 121,
46
+ "crop_stop": 382,
47
+ "hpf_start": 138,
48
+ "hpf_stop": 123,
49
+ "res_type": "sinc_medium"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 740,
54
+ "pre_filter_stop": 768
55
+ }
lib_v5/vr_network/modelparams/4band_44100_sw.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "stereo_w": true,
3
+ "bins": 768,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 668,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 128,
10
+ "n_fft": 1024,
11
+ "crop_start": 0,
12
+ "crop_stop": 186,
13
+ "lpf_start": 37,
14
+ "lpf_stop": 73,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 11025,
19
+ "hl": 128,
20
+ "n_fft": 512,
21
+ "crop_start": 4,
22
+ "crop_stop": 185,
23
+ "hpf_start": 36,
24
+ "hpf_stop": 18,
25
+ "lpf_start": 93,
26
+ "lpf_stop": 185,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 22050,
31
+ "hl": 256,
32
+ "n_fft": 512,
33
+ "crop_start": 46,
34
+ "crop_stop": 186,
35
+ "hpf_start": 93,
36
+ "hpf_stop": 46,
37
+ "lpf_start": 164,
38
+ "lpf_stop": 186,
39
+ "res_type": "polyphase"
40
+ },
41
+ "4": {
42
+ "sr": 44100,
43
+ "hl": 512,
44
+ "n_fft": 768,
45
+ "crop_start": 121,
46
+ "crop_stop": 382,
47
+ "hpf_start": 138,
48
+ "hpf_stop": 123,
49
+ "res_type": "sinc_medium"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 740,
54
+ "pre_filter_stop": 768
55
+ }
lib_v5/vr_network/modelparams/4band_v2.json ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 672,
3
+ "unstable_bins": 8,
4
+ "reduction_bins": 637,
5
+ "band": {
6
+ "1": {
7
+ "sr": 7350,
8
+ "hl": 80,
9
+ "n_fft": 640,
10
+ "crop_start": 0,
11
+ "crop_stop": 85,
12
+ "lpf_start": 25,
13
+ "lpf_stop": 53,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 7350,
18
+ "hl": 80,
19
+ "n_fft": 320,
20
+ "crop_start": 4,
21
+ "crop_stop": 87,
22
+ "hpf_start": 25,
23
+ "hpf_stop": 12,
24
+ "lpf_start": 31,
25
+ "lpf_stop": 62,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 14700,
30
+ "hl": 160,
31
+ "n_fft": 512,
32
+ "crop_start": 17,
33
+ "crop_stop": 216,
34
+ "hpf_start": 48,
35
+ "hpf_stop": 24,
36
+ "lpf_start": 139,
37
+ "lpf_stop": 210,
38
+ "res_type": "polyphase"
39
+ },
40
+ "4": {
41
+ "sr": 44100,
42
+ "hl": 480,
43
+ "n_fft": 960,
44
+ "crop_start": 78,
45
+ "crop_stop": 383,
46
+ "hpf_start": 130,
47
+ "hpf_stop": 86,
48
+ "res_type": "kaiser_fast"
49
+ }
50
+ },
51
+ "sr": 44100,
52
+ "pre_filter_start": 668,
53
+ "pre_filter_stop": 672
54
+ }
lib_v5/vr_network/modelparams/4band_v2_sn.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 672,
3
+ "unstable_bins": 8,
4
+ "reduction_bins": 637,
5
+ "band": {
6
+ "1": {
7
+ "sr": 7350,
8
+ "hl": 80,
9
+ "n_fft": 640,
10
+ "crop_start": 0,
11
+ "crop_stop": 85,
12
+ "lpf_start": 25,
13
+ "lpf_stop": 53,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 7350,
18
+ "hl": 80,
19
+ "n_fft": 320,
20
+ "crop_start": 4,
21
+ "crop_stop": 87,
22
+ "hpf_start": 25,
23
+ "hpf_stop": 12,
24
+ "lpf_start": 31,
25
+ "lpf_stop": 62,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 14700,
30
+ "hl": 160,
31
+ "n_fft": 512,
32
+ "crop_start": 17,
33
+ "crop_stop": 216,
34
+ "hpf_start": 48,
35
+ "hpf_stop": 24,
36
+ "lpf_start": 139,
37
+ "lpf_stop": 210,
38
+ "res_type": "polyphase"
39
+ },
40
+ "4": {
41
+ "sr": 44100,
42
+ "hl": 480,
43
+ "n_fft": 960,
44
+ "crop_start": 78,
45
+ "crop_stop": 383,
46
+ "hpf_start": 130,
47
+ "hpf_stop": 86,
48
+ "convert_channels": "stereo_n",
49
+ "res_type": "kaiser_fast"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 668,
54
+ "pre_filter_stop": 672
55
+ }
lib_v5/vr_network/modelparams/4band_v3.json ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bins": 672,
3
+ "unstable_bins": 8,
4
+ "reduction_bins": 530,
5
+ "band": {
6
+ "1": {
7
+ "sr": 7350,
8
+ "hl": 80,
9
+ "n_fft": 640,
10
+ "crop_start": 0,
11
+ "crop_stop": 85,
12
+ "lpf_start": 25,
13
+ "lpf_stop": 53,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 7350,
18
+ "hl": 80,
19
+ "n_fft": 320,
20
+ "crop_start": 4,
21
+ "crop_stop": 87,
22
+ "hpf_start": 25,
23
+ "hpf_stop": 12,
24
+ "lpf_start": 31,
25
+ "lpf_stop": 62,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 14700,
30
+ "hl": 160,
31
+ "n_fft": 512,
32
+ "crop_start": 17,
33
+ "crop_stop": 216,
34
+ "hpf_start": 48,
35
+ "hpf_stop": 24,
36
+ "lpf_start": 139,
37
+ "lpf_stop": 210,
38
+ "res_type": "polyphase"
39
+ },
40
+ "4": {
41
+ "sr": 44100,
42
+ "hl": 480,
43
+ "n_fft": 960,
44
+ "crop_start": 78,
45
+ "crop_stop": 383,
46
+ "hpf_start": 130,
47
+ "hpf_stop": 86,
48
+ "res_type": "kaiser_fast"
49
+ }
50
+ },
51
+ "sr": 44100,
52
+ "pre_filter_start": 668,
53
+ "pre_filter_stop": 672
54
+ }
lib_v5/vr_network/modelparams/4band_v3_sn.json ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "n_bins": 672,
3
+ "unstable_bins": 8,
4
+ "stable_bins": 530,
5
+ "band": {
6
+ "1": {
7
+ "sr": 7350,
8
+ "hl": 80,
9
+ "n_fft": 640,
10
+ "crop_start": 0,
11
+ "crop_stop": 85,
12
+ "lpf_start": 25,
13
+ "lpf_stop": 53,
14
+ "res_type": "polyphase"
15
+ },
16
+ "2": {
17
+ "sr": 7350,
18
+ "hl": 80,
19
+ "n_fft": 320,
20
+ "crop_start": 4,
21
+ "crop_stop": 87,
22
+ "hpf_start": 25,
23
+ "hpf_stop": 12,
24
+ "lpf_start": 31,
25
+ "lpf_stop": 62,
26
+ "res_type": "polyphase"
27
+ },
28
+ "3": {
29
+ "sr": 14700,
30
+ "hl": 160,
31
+ "n_fft": 512,
32
+ "crop_start": 17,
33
+ "crop_stop": 216,
34
+ "hpf_start": 48,
35
+ "hpf_stop": 24,
36
+ "lpf_start": 139,
37
+ "lpf_stop": 210,
38
+ "res_type": "polyphase"
39
+ },
40
+ "4": {
41
+ "sr": 44100,
42
+ "hl": 480,
43
+ "n_fft": 960,
44
+ "crop_start": 78,
45
+ "crop_stop": 383,
46
+ "hpf_start": 130,
47
+ "hpf_stop": 86,
48
+ "convert_channels": "stereo_n",
49
+ "res_type": "kaiser_fast"
50
+ }
51
+ },
52
+ "sr": 44100,
53
+ "pre_filter_start": 668,
54
+ "pre_filter_stop": 672
55
+ }
lib_v5/vr_network/modelparams/ensemble.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "mid_side_b2": true,
3
+ "bins": 1280,
4
+ "unstable_bins": 7,
5
+ "reduction_bins": 565,
6
+ "band": {
7
+ "1": {
8
+ "sr": 11025,
9
+ "hl": 108,
10
+ "n_fft": 2048,
11
+ "crop_start": 0,
12
+ "crop_stop": 374,
13
+ "lpf_start": 92,
14
+ "lpf_stop": 186,
15
+ "res_type": "polyphase"
16
+ },
17
+ "2": {
18
+ "sr": 22050,
19
+ "hl": 216,
20
+ "n_fft": 1536,
21
+ "crop_start": 0,
22
+ "crop_stop": 424,
23
+ "hpf_start": 68,
24
+ "hpf_stop": 34,
25
+ "lpf_start": 348,
26
+ "lpf_stop": 418,
27
+ "res_type": "polyphase"
28
+ },
29
+ "3": {
30
+ "sr": 44100,
31
+ "hl": 432,
32
+ "n_fft": 1280,
33
+ "crop_start": 132,
34
+ "crop_stop": 614,
35
+ "hpf_start": 172,
36
+ "hpf_stop": 144,
37
+ "res_type": "polyphase"
38
+ }
39
+ },
40
+ "sr": 44100,
41
+ "pre_filter_start": 1280,
42
+ "pre_filter_stop": 1280
43
+ }
lib_v5/vr_network/nets.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+
5
+ from . import layers
6
+
7
+ class BaseASPPNet(nn.Module):
8
+
9
+ def __init__(self, nn_architecture, nin, ch, dilations=(4, 8, 16)):
10
+ super(BaseASPPNet, self).__init__()
11
+ self.nn_architecture = nn_architecture
12
+ self.enc1 = layers.Encoder(nin, ch, 3, 2, 1)
13
+ self.enc2 = layers.Encoder(ch, ch * 2, 3, 2, 1)
14
+ self.enc3 = layers.Encoder(ch * 2, ch * 4, 3, 2, 1)
15
+ self.enc4 = layers.Encoder(ch * 4, ch * 8, 3, 2, 1)
16
+
17
+ if self.nn_architecture == 129605:
18
+ self.enc5 = layers.Encoder(ch * 8, ch * 16, 3, 2, 1)
19
+ self.aspp = layers.ASPPModule(nn_architecture, ch * 16, ch * 32, dilations)
20
+ self.dec5 = layers.Decoder(ch * (16 + 32), ch * 16, 3, 1, 1)
21
+ else:
22
+ self.aspp = layers.ASPPModule(nn_architecture, ch * 8, ch * 16, dilations)
23
+
24
+ self.dec4 = layers.Decoder(ch * (8 + 16), ch * 8, 3, 1, 1)
25
+ self.dec3 = layers.Decoder(ch * (4 + 8), ch * 4, 3, 1, 1)
26
+ self.dec2 = layers.Decoder(ch * (2 + 4), ch * 2, 3, 1, 1)
27
+ self.dec1 = layers.Decoder(ch * (1 + 2), ch, 3, 1, 1)
28
+
29
+ def __call__(self, x):
30
+ h, e1 = self.enc1(x)
31
+ h, e2 = self.enc2(h)
32
+ h, e3 = self.enc3(h)
33
+ h, e4 = self.enc4(h)
34
+
35
+ if self.nn_architecture == 129605:
36
+ h, e5 = self.enc5(h)
37
+ h = self.aspp(h)
38
+ h = self.dec5(h, e5)
39
+ else:
40
+ h = self.aspp(h)
41
+
42
+ h = self.dec4(h, e4)
43
+ h = self.dec3(h, e3)
44
+ h = self.dec2(h, e2)
45
+ h = self.dec1(h, e1)
46
+
47
+ return h
48
+
49
+ def determine_model_capacity(n_fft_bins, nn_architecture):
50
+
51
+ sp_model_arch = [31191, 33966, 129605]
52
+ hp_model_arch = [123821, 123812]
53
+ hp2_model_arch = [537238, 537227]
54
+
55
+ if nn_architecture in sp_model_arch:
56
+ model_capacity_data = [
57
+ (2, 16),
58
+ (2, 16),
59
+ (18, 8, 1, 1, 0),
60
+ (8, 16),
61
+ (34, 16, 1, 1, 0),
62
+ (16, 32),
63
+ (32, 2, 1),
64
+ (16, 2, 1),
65
+ (16, 2, 1),
66
+ ]
67
+
68
+ if nn_architecture in hp_model_arch:
69
+ model_capacity_data = [
70
+ (2, 32),
71
+ (2, 32),
72
+ (34, 16, 1, 1, 0),
73
+ (16, 32),
74
+ (66, 32, 1, 1, 0),
75
+ (32, 64),
76
+ (64, 2, 1),
77
+ (32, 2, 1),
78
+ (32, 2, 1),
79
+ ]
80
+
81
+ if nn_architecture in hp2_model_arch:
82
+ model_capacity_data = [
83
+ (2, 64),
84
+ (2, 64),
85
+ (66, 32, 1, 1, 0),
86
+ (32, 64),
87
+ (130, 64, 1, 1, 0),
88
+ (64, 128),
89
+ (128, 2, 1),
90
+ (64, 2, 1),
91
+ (64, 2, 1),
92
+ ]
93
+
94
+ cascaded = CascadedASPPNet
95
+ model = cascaded(n_fft_bins, model_capacity_data, nn_architecture)
96
+
97
+ return model
98
+
99
+ class CascadedASPPNet(nn.Module):
100
+
101
+ def __init__(self, n_fft, model_capacity_data, nn_architecture):
102
+ super(CascadedASPPNet, self).__init__()
103
+ self.stg1_low_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[0])
104
+ self.stg1_high_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[1])
105
+
106
+ self.stg2_bridge = layers.Conv2DBNActiv(*model_capacity_data[2])
107
+ self.stg2_full_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[3])
108
+
109
+ self.stg3_bridge = layers.Conv2DBNActiv(*model_capacity_data[4])
110
+ self.stg3_full_band_net = BaseASPPNet(nn_architecture, *model_capacity_data[5])
111
+
112
+ self.out = nn.Conv2d(*model_capacity_data[6], bias=False)
113
+ self.aux1_out = nn.Conv2d(*model_capacity_data[7], bias=False)
114
+ self.aux2_out = nn.Conv2d(*model_capacity_data[8], bias=False)
115
+
116
+ self.max_bin = n_fft // 2
117
+ self.output_bin = n_fft // 2 + 1
118
+
119
+ self.offset = 128
120
+
121
+ def forward(self, x):
122
+ mix = x.detach()
123
+ x = x.clone()
124
+
125
+ x = x[:, :, :self.max_bin]
126
+
127
+ bandw = x.size()[2] // 2
128
+ aux1 = torch.cat([
129
+ self.stg1_low_band_net(x[:, :, :bandw]),
130
+ self.stg1_high_band_net(x[:, :, bandw:])
131
+ ], dim=2)
132
+
133
+ h = torch.cat([x, aux1], dim=1)
134
+ aux2 = self.stg2_full_band_net(self.stg2_bridge(h))
135
+
136
+ h = torch.cat([x, aux1, aux2], dim=1)
137
+ h = self.stg3_full_band_net(self.stg3_bridge(h))
138
+
139
+ mask = torch.sigmoid(self.out(h))
140
+ mask = F.pad(
141
+ input=mask,
142
+ pad=(0, 0, 0, self.output_bin - mask.size()[2]),
143
+ mode='replicate')
144
+
145
+ if self.training:
146
+ aux1 = torch.sigmoid(self.aux1_out(aux1))
147
+ aux1 = F.pad(
148
+ input=aux1,
149
+ pad=(0, 0, 0, self.output_bin - aux1.size()[2]),
150
+ mode='replicate')
151
+ aux2 = torch.sigmoid(self.aux2_out(aux2))
152
+ aux2 = F.pad(
153
+ input=aux2,
154
+ pad=(0, 0, 0, self.output_bin - aux2.size()[2]),
155
+ mode='replicate')
156
+ return mask * mix, aux1 * mix, aux2 * mix
157
+ else:
158
+ return mask# * mix
159
+
160
+ def predict_mask(self, x):
161
+ mask = self.forward(x)
162
+
163
+ if self.offset > 0:
164
+ mask = mask[:, :, :, self.offset:-self.offset]
165
+
166
+ return mask
lib_v5/vr_network/nets_new.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ import torch.nn.functional as F
4
+ from . import layers_new as layers
5
+
6
+ class BaseNet(nn.Module):
7
+
8
+ def __init__(self, nin, nout, nin_lstm, nout_lstm, dilations=((4, 2), (8, 4), (12, 6))):
9
+ super(BaseNet, self).__init__()
10
+ self.enc1 = layers.Conv2DBNActiv(nin, nout, 3, 1, 1)
11
+ self.enc2 = layers.Encoder(nout, nout * 2, 3, 2, 1)
12
+ self.enc3 = layers.Encoder(nout * 2, nout * 4, 3, 2, 1)
13
+ self.enc4 = layers.Encoder(nout * 4, nout * 6, 3, 2, 1)
14
+ self.enc5 = layers.Encoder(nout * 6, nout * 8, 3, 2, 1)
15
+
16
+ self.aspp = layers.ASPPModule(nout * 8, nout * 8, dilations, dropout=True)
17
+
18
+ self.dec4 = layers.Decoder(nout * (6 + 8), nout * 6, 3, 1, 1)
19
+ self.dec3 = layers.Decoder(nout * (4 + 6), nout * 4, 3, 1, 1)
20
+ self.dec2 = layers.Decoder(nout * (2 + 4), nout * 2, 3, 1, 1)
21
+ self.lstm_dec2 = layers.LSTMModule(nout * 2, nin_lstm, nout_lstm)
22
+ self.dec1 = layers.Decoder(nout * (1 + 2) + 1, nout * 1, 3, 1, 1)
23
+
24
+ def __call__(self, x):
25
+ e1 = self.enc1(x)
26
+ e2 = self.enc2(e1)
27
+ e3 = self.enc3(e2)
28
+ e4 = self.enc4(e3)
29
+ e5 = self.enc5(e4)
30
+
31
+ h = self.aspp(e5)
32
+
33
+ h = self.dec4(h, e4)
34
+ h = self.dec3(h, e3)
35
+ h = self.dec2(h, e2)
36
+ h = torch.cat([h, self.lstm_dec2(h)], dim=1)
37
+ h = self.dec1(h, e1)
38
+
39
+ return h
40
+
41
+ class CascadedNet(nn.Module):
42
+
43
+ def __init__(self, n_fft, nn_arch_size=51000, nout=32, nout_lstm=128):
44
+ super(CascadedNet, self).__init__()
45
+ self.max_bin = n_fft // 2
46
+ self.output_bin = n_fft // 2 + 1
47
+ self.nin_lstm = self.max_bin // 2
48
+ self.offset = 64
49
+ nout = 64 if nn_arch_size == 218409 else nout
50
+
51
+ #print(nout, nout_lstm, n_fft)
52
+
53
+ self.stg1_low_band_net = nn.Sequential(
54
+ BaseNet(2, nout // 2, self.nin_lstm // 2, nout_lstm),
55
+ layers.Conv2DBNActiv(nout // 2, nout // 4, 1, 1, 0)
56
+ )
57
+ self.stg1_high_band_net = BaseNet(2, nout // 4, self.nin_lstm // 2, nout_lstm // 2)
58
+
59
+ self.stg2_low_band_net = nn.Sequential(
60
+ BaseNet(nout // 4 + 2, nout, self.nin_lstm // 2, nout_lstm),
61
+ layers.Conv2DBNActiv(nout, nout // 2, 1, 1, 0)
62
+ )
63
+ self.stg2_high_band_net = BaseNet(nout // 4 + 2, nout // 2, self.nin_lstm // 2, nout_lstm // 2)
64
+
65
+ self.stg3_full_band_net = BaseNet(3 * nout // 4 + 2, nout, self.nin_lstm, nout_lstm)
66
+
67
+ self.out = nn.Conv2d(nout, 2, 1, bias=False)
68
+ self.aux_out = nn.Conv2d(3 * nout // 4, 2, 1, bias=False)
69
+
70
+ def forward(self, x):
71
+ x = x[:, :, :self.max_bin]
72
+
73
+ bandw = x.size()[2] // 2
74
+ l1_in = x[:, :, :bandw]
75
+ h1_in = x[:, :, bandw:]
76
+ l1 = self.stg1_low_band_net(l1_in)
77
+ h1 = self.stg1_high_band_net(h1_in)
78
+ aux1 = torch.cat([l1, h1], dim=2)
79
+
80
+ l2_in = torch.cat([l1_in, l1], dim=1)
81
+ h2_in = torch.cat([h1_in, h1], dim=1)
82
+ l2 = self.stg2_low_band_net(l2_in)
83
+ h2 = self.stg2_high_band_net(h2_in)
84
+ aux2 = torch.cat([l2, h2], dim=2)
85
+
86
+ f3_in = torch.cat([x, aux1, aux2], dim=1)
87
+ f3 = self.stg3_full_band_net(f3_in)
88
+
89
+ mask = torch.sigmoid(self.out(f3))
90
+ mask = F.pad(
91
+ input=mask,
92
+ pad=(0, 0, 0, self.output_bin - mask.size()[2]),
93
+ mode='replicate'
94
+ )
95
+
96
+ if self.training:
97
+ aux = torch.cat([aux1, aux2], dim=1)
98
+ aux = torch.sigmoid(self.aux_out(aux))
99
+ aux = F.pad(
100
+ input=aux,
101
+ pad=(0, 0, 0, self.output_bin - aux.size()[2]),
102
+ mode='replicate'
103
+ )
104
+ return mask, aux
105
+ else:
106
+ return mask
107
+
108
+ def predict_mask(self, x):
109
+ mask = self.forward(x)
110
+
111
+ if self.offset > 0:
112
+ mask = mask[:, :, :, self.offset:-self.offset]
113
+ assert mask.size()[3] > 0
114
+
115
+ return mask
116
+
117
+ def predict(self, x):
118
+ mask = self.forward(x)
119
+ pred_mag = x * mask
120
+
121
+ if self.offset > 0:
122
+ pred_mag = pred_mag[:, :, :, self.offset:-self.offset]
123
+ assert pred_mag.size()[3] > 0
124
+
125
+ return pred_mag