videopython-chatterbox 0.1.7.post1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chatterbox/__init__.py +14 -0
- chatterbox/models/__init__.py +0 -0
- chatterbox/models/s3gen/__init__.py +2 -0
- chatterbox/models/s3gen/configs.py +10 -0
- chatterbox/models/s3gen/const.py +2 -0
- chatterbox/models/s3gen/decoder.py +333 -0
- chatterbox/models/s3gen/f0_predictor.py +55 -0
- chatterbox/models/s3gen/flow.py +198 -0
- chatterbox/models/s3gen/flow_matching.py +246 -0
- chatterbox/models/s3gen/hifigan.py +474 -0
- chatterbox/models/s3gen/matcha/decoder.py +443 -0
- chatterbox/models/s3gen/matcha/flow_matching.py +129 -0
- chatterbox/models/s3gen/matcha/text_encoder.py +413 -0
- chatterbox/models/s3gen/matcha/transformer.py +316 -0
- chatterbox/models/s3gen/s3gen.py +362 -0
- chatterbox/models/s3gen/transformer/__init__.py +0 -0
- chatterbox/models/s3gen/transformer/activation.py +84 -0
- chatterbox/models/s3gen/transformer/attention.py +330 -0
- chatterbox/models/s3gen/transformer/convolution.py +145 -0
- chatterbox/models/s3gen/transformer/embedding.py +294 -0
- chatterbox/models/s3gen/transformer/encoder_layer.py +236 -0
- chatterbox/models/s3gen/transformer/positionwise_feed_forward.py +115 -0
- chatterbox/models/s3gen/transformer/subsampling.py +383 -0
- chatterbox/models/s3gen/transformer/upsample_encoder.py +318 -0
- chatterbox/models/s3gen/utils/class_utils.py +71 -0
- chatterbox/models/s3gen/utils/intmeanflow.py +36 -0
- chatterbox/models/s3gen/utils/mask.py +194 -0
- chatterbox/models/s3gen/utils/mel.py +85 -0
- chatterbox/models/s3gen/xvector.py +428 -0
- chatterbox/models/s3tokenizer/__init__.py +30 -0
- chatterbox/models/s3tokenizer/s3tokenizer.py +168 -0
- chatterbox/models/t3/__init__.py +1 -0
- chatterbox/models/t3/inference/alignment_stream_analyzer.py +181 -0
- chatterbox/models/t3/inference/t3_hf_backend.py +116 -0
- chatterbox/models/t3/llama_configs.py +73 -0
- chatterbox/models/t3/modules/cond_enc.py +97 -0
- chatterbox/models/t3/modules/learned_pos_emb.py +32 -0
- chatterbox/models/t3/modules/perceiver.py +212 -0
- chatterbox/models/t3/modules/t3_config.py +41 -0
- chatterbox/models/t3/t3.py +490 -0
- chatterbox/models/tokenizers/__init__.py +1 -0
- chatterbox/models/tokenizers/tokenizer.py +313 -0
- chatterbox/models/utils.py +4 -0
- chatterbox/models/voice_encoder/__init__.py +1 -0
- chatterbox/models/voice_encoder/config.py +18 -0
- chatterbox/models/voice_encoder/melspec.py +78 -0
- chatterbox/models/voice_encoder/voice_encoder.py +274 -0
- chatterbox/mtl_tts.py +317 -0
- chatterbox/tts.py +272 -0
- chatterbox/tts_turbo.py +296 -0
- chatterbox/vc.py +104 -0
- videopython_chatterbox-0.1.7.post1.dist-info/METADATA +154 -0
- videopython_chatterbox-0.1.7.post1.dist-info/RECORD +56 -0
- videopython_chatterbox-0.1.7.post1.dist-info/WHEEL +4 -0
- videopython_chatterbox-0.1.7.post1.dist-info/licenses/LICENSE +21 -0
- videopython_chatterbox-0.1.7.post1.dist-info/licenses/NOTICE +29 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Copyright [2023-11-28] <sxc19@mails.tsinghua.edu.cn, Xingchen Song>
|
|
2
|
+
# 2024 Alibaba Inc (authors: Xiang Lyu)
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
import torch
|
|
16
|
+
|
|
17
|
+
from ..transformer.activation import Swish
|
|
18
|
+
from ..transformer.subsampling import (
|
|
19
|
+
LinearNoSubsampling,
|
|
20
|
+
EmbedinigNoSubsampling,
|
|
21
|
+
Conv1dSubsampling2,
|
|
22
|
+
Conv2dSubsampling4,
|
|
23
|
+
Conv2dSubsampling6,
|
|
24
|
+
Conv2dSubsampling8,
|
|
25
|
+
)
|
|
26
|
+
from ..transformer.embedding import (
|
|
27
|
+
PositionalEncoding,
|
|
28
|
+
RelPositionalEncoding,
|
|
29
|
+
WhisperPositionalEncoding,
|
|
30
|
+
LearnablePositionalEncoding,
|
|
31
|
+
NoPositionalEncoding)
|
|
32
|
+
from ..transformer.attention import (MultiHeadedAttention,
|
|
33
|
+
RelPositionMultiHeadedAttention)
|
|
34
|
+
from ..transformer.embedding import EspnetRelPositionalEncoding
|
|
35
|
+
from ..transformer.subsampling import LegacyLinearNoSubsampling
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
COSYVOICE_ACTIVATION_CLASSES = {
|
|
39
|
+
"hardtanh": torch.nn.Hardtanh,
|
|
40
|
+
"tanh": torch.nn.Tanh,
|
|
41
|
+
"relu": torch.nn.ReLU,
|
|
42
|
+
"selu": torch.nn.SELU,
|
|
43
|
+
"swish": getattr(torch.nn, "SiLU", Swish),
|
|
44
|
+
"gelu": torch.nn.GELU,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
COSYVOICE_SUBSAMPLE_CLASSES = {
|
|
48
|
+
"linear": LinearNoSubsampling,
|
|
49
|
+
"linear_legacy": LegacyLinearNoSubsampling,
|
|
50
|
+
"embed": EmbedinigNoSubsampling,
|
|
51
|
+
"conv1d2": Conv1dSubsampling2,
|
|
52
|
+
"conv2d": Conv2dSubsampling4,
|
|
53
|
+
"conv2d6": Conv2dSubsampling6,
|
|
54
|
+
"conv2d8": Conv2dSubsampling8,
|
|
55
|
+
'paraformer_dummy': torch.nn.Identity
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
COSYVOICE_EMB_CLASSES = {
|
|
59
|
+
"embed": PositionalEncoding,
|
|
60
|
+
"abs_pos": PositionalEncoding,
|
|
61
|
+
"rel_pos": RelPositionalEncoding,
|
|
62
|
+
"rel_pos_espnet": EspnetRelPositionalEncoding,
|
|
63
|
+
"no_pos": NoPositionalEncoding,
|
|
64
|
+
"abs_pos_whisper": WhisperPositionalEncoding,
|
|
65
|
+
"embed_learnable_pe": LearnablePositionalEncoding,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
COSYVOICE_ATTENTION_CLASSES = {
|
|
69
|
+
"selfattn": MultiHeadedAttention,
|
|
70
|
+
"rel_selfattn": RelPositionMultiHeadedAttention,
|
|
71
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def get_intmeanflow_time_mixer(dims):
|
|
6
|
+
""""
|
|
7
|
+
Diagonal init as described in 3.3 https://arxiv.org/pdf/2510.07979
|
|
8
|
+
"""
|
|
9
|
+
layer = nn.Linear(dims * 2, dims, bias=False)
|
|
10
|
+
|
|
11
|
+
with torch.no_grad():
|
|
12
|
+
target_weight = torch.zeros(dims, 2 * dims)
|
|
13
|
+
target_weight[:, 0:dims] = torch.eye(dims)
|
|
14
|
+
layer.weight.data = target_weight
|
|
15
|
+
|
|
16
|
+
return layer
|
|
17
|
+
|
|
18
|
+
if __name__ == '__main__':
|
|
19
|
+
|
|
20
|
+
D_example = 6
|
|
21
|
+
|
|
22
|
+
W_layer = get_intmeanflow_time_mixer(D_example)
|
|
23
|
+
|
|
24
|
+
print(f"Layer weight (AFTER init):\n{W_layer.weight.data}\n")
|
|
25
|
+
|
|
26
|
+
e_t = torch.tensor([0., 1., 2., 3., 4., 5.])
|
|
27
|
+
e_r = torch.tensor([6., 7., 8., 9., 10., 11.])
|
|
28
|
+
e_concat = torch.cat([e_t, e_r]).unsqueeze(0) # Shape (1, 12)
|
|
29
|
+
|
|
30
|
+
output = W_layer(e_concat)
|
|
31
|
+
|
|
32
|
+
print(f"Test Input e_t: \n{e_t}")
|
|
33
|
+
print(f"Test Input e_r: \n{e_r}")
|
|
34
|
+
print(f"Test Input concat: \n{e_concat}")
|
|
35
|
+
|
|
36
|
+
print(f"Forward Pass Output: \n{output.squeeze(0)}")
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Copyright (c) 2019 Shigeki Karita
|
|
2
|
+
# 2020 Mobvoi Inc (Binbin Zhang)
|
|
3
|
+
# 2024 Alibaba Inc (authors: Xiang Lyu)
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
import torch
|
|
18
|
+
|
|
19
|
+
'''
|
|
20
|
+
def subsequent_mask(
|
|
21
|
+
size: int,
|
|
22
|
+
device: torch.device = torch.device("cpu"),
|
|
23
|
+
) -> torch.Tensor:
|
|
24
|
+
"""Create mask for subsequent steps (size, size).
|
|
25
|
+
|
|
26
|
+
This mask is used only in decoder which works in an auto-regressive mode.
|
|
27
|
+
This means the current step could only do attention with its left steps.
|
|
28
|
+
|
|
29
|
+
In encoder, fully attention is used when streaming is not necessary and
|
|
30
|
+
the sequence is not long. In this case, no attention mask is needed.
|
|
31
|
+
|
|
32
|
+
When streaming is need, chunk-based attention is used in encoder. See
|
|
33
|
+
subsequent_chunk_mask for the chunk-based attention mask.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
size (int): size of mask
|
|
37
|
+
str device (str): "cpu" or "cuda" or torch.Tensor.device
|
|
38
|
+
dtype (torch.device): result dtype
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
torch.Tensor: mask
|
|
42
|
+
|
|
43
|
+
Examples:
|
|
44
|
+
>>> subsequent_mask(3)
|
|
45
|
+
[[1, 0, 0],
|
|
46
|
+
[1, 1, 0],
|
|
47
|
+
[1, 1, 1]]
|
|
48
|
+
"""
|
|
49
|
+
ret = torch.ones(size, size, device=device, dtype=torch.bool)
|
|
50
|
+
return torch.tril(ret)
|
|
51
|
+
'''
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def subsequent_chunk_mask(
|
|
55
|
+
size: int,
|
|
56
|
+
chunk_size: int,
|
|
57
|
+
num_left_chunks: int = -1,
|
|
58
|
+
device: torch.device = torch.device("cpu"),
|
|
59
|
+
) -> torch.Tensor:
|
|
60
|
+
"""Create mask for subsequent steps (size, size) with chunk size,
|
|
61
|
+
this is for streaming encoder
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
size (int): size of mask
|
|
65
|
+
chunk_size (int): size of chunk
|
|
66
|
+
num_left_chunks (int): number of left chunks
|
|
67
|
+
<0: use full chunk
|
|
68
|
+
>=0: use num_left_chunks
|
|
69
|
+
device (torch.device): "cpu" or "cuda" or torch.Tensor.device
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
torch.Tensor: mask
|
|
73
|
+
|
|
74
|
+
Examples:
|
|
75
|
+
>>> subsequent_chunk_mask(4, 2)
|
|
76
|
+
[[1, 1, 0, 0],
|
|
77
|
+
[1, 1, 0, 0],
|
|
78
|
+
[1, 1, 1, 1],
|
|
79
|
+
[1, 1, 1, 1]]
|
|
80
|
+
"""
|
|
81
|
+
# NOTE this modified implementation meets onnx export requirements, but it doesn't support num_left_chunks
|
|
82
|
+
# actually this is not needed after we have inference cache implemented, will remove it later
|
|
83
|
+
pos_idx = torch.arange(size, device=device)
|
|
84
|
+
block_value = (torch.div(pos_idx, chunk_size, rounding_mode='trunc') + 1) * chunk_size
|
|
85
|
+
ret = pos_idx.unsqueeze(0) < block_value.unsqueeze(1)
|
|
86
|
+
return ret
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def add_optional_chunk_mask(xs: torch.Tensor,
|
|
90
|
+
masks: torch.Tensor,
|
|
91
|
+
use_dynamic_chunk: bool,
|
|
92
|
+
use_dynamic_left_chunk: bool,
|
|
93
|
+
decoding_chunk_size: int,
|
|
94
|
+
static_chunk_size: int,
|
|
95
|
+
num_decoding_left_chunks: int,
|
|
96
|
+
enable_full_context: bool = True):
|
|
97
|
+
""" Apply optional mask for encoder.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
xs (torch.Tensor): padded input, (B, L, D), L for max length
|
|
101
|
+
mask (torch.Tensor): mask for xs, (B, 1, L)
|
|
102
|
+
use_dynamic_chunk (bool): whether to use dynamic chunk or not
|
|
103
|
+
use_dynamic_left_chunk (bool): whether to use dynamic left chunk for
|
|
104
|
+
training.
|
|
105
|
+
decoding_chunk_size (int): decoding chunk size for dynamic chunk, it's
|
|
106
|
+
0: default for training, use random dynamic chunk.
|
|
107
|
+
<0: for decoding, use full chunk.
|
|
108
|
+
>0: for decoding, use fixed chunk size as set.
|
|
109
|
+
static_chunk_size (int): chunk size for static chunk training/decoding
|
|
110
|
+
if it's greater than 0, if use_dynamic_chunk is true,
|
|
111
|
+
this parameter will be ignored
|
|
112
|
+
num_decoding_left_chunks: number of left chunks, this is for decoding,
|
|
113
|
+
the chunk size is decoding_chunk_size.
|
|
114
|
+
>=0: use num_decoding_left_chunks
|
|
115
|
+
<0: use all left chunks
|
|
116
|
+
enable_full_context (bool):
|
|
117
|
+
True: chunk size is either [1, 25] or full context(max_len)
|
|
118
|
+
False: chunk size ~ U[1, 25]
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
torch.Tensor: chunk mask of the input xs.
|
|
122
|
+
"""
|
|
123
|
+
# Whether to use chunk mask or not
|
|
124
|
+
if use_dynamic_chunk:
|
|
125
|
+
max_len = xs.size(1)
|
|
126
|
+
if decoding_chunk_size < 0:
|
|
127
|
+
chunk_size = max_len
|
|
128
|
+
num_left_chunks = -1
|
|
129
|
+
elif decoding_chunk_size > 0:
|
|
130
|
+
chunk_size = decoding_chunk_size
|
|
131
|
+
num_left_chunks = num_decoding_left_chunks
|
|
132
|
+
else:
|
|
133
|
+
# chunk size is either [1, 25] or full context(max_len).
|
|
134
|
+
# Since we use 4 times subsampling and allow up to 1s(100 frames)
|
|
135
|
+
# delay, the maximum frame is 100 / 4 = 25.
|
|
136
|
+
chunk_size = torch.randint(1, max_len, (1, )).item()
|
|
137
|
+
num_left_chunks = -1
|
|
138
|
+
if chunk_size > max_len // 2 and enable_full_context:
|
|
139
|
+
chunk_size = max_len
|
|
140
|
+
else:
|
|
141
|
+
chunk_size = chunk_size % 25 + 1
|
|
142
|
+
if use_dynamic_left_chunk:
|
|
143
|
+
max_left_chunks = (max_len - 1) // chunk_size
|
|
144
|
+
num_left_chunks = torch.randint(0, max_left_chunks,
|
|
145
|
+
(1, )).item()
|
|
146
|
+
chunk_masks = subsequent_chunk_mask(xs.size(1), chunk_size,
|
|
147
|
+
num_left_chunks,
|
|
148
|
+
xs.device) # (L, L)
|
|
149
|
+
chunk_masks = chunk_masks.unsqueeze(0) # (1, L, L)
|
|
150
|
+
chunk_masks = masks & chunk_masks # (B, L, L)
|
|
151
|
+
elif static_chunk_size > 0:
|
|
152
|
+
num_left_chunks = num_decoding_left_chunks
|
|
153
|
+
chunk_masks = subsequent_chunk_mask(xs.size(1), static_chunk_size,
|
|
154
|
+
num_left_chunks,
|
|
155
|
+
xs.device) # (L, L)
|
|
156
|
+
chunk_masks = chunk_masks.unsqueeze(0) # (1, L, L)
|
|
157
|
+
chunk_masks = masks & chunk_masks # (B, L, L)
|
|
158
|
+
else:
|
|
159
|
+
chunk_masks = masks
|
|
160
|
+
assert chunk_masks.dtype == torch.bool
|
|
161
|
+
if (chunk_masks.sum(dim=-1) == 0).sum().item() != 0:
|
|
162
|
+
logging.warning('get chunk_masks all false at some timestep, force set to true, make sure they are masked in futuer computation!')
|
|
163
|
+
chunk_masks[chunk_masks.sum(dim=-1)==0] = True
|
|
164
|
+
return chunk_masks
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def make_pad_mask(lengths: torch.Tensor, max_len: int = 0) -> torch.Tensor:
|
|
168
|
+
"""Make mask tensor containing indices of padded part.
|
|
169
|
+
|
|
170
|
+
See description of make_non_pad_mask.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
lengths (torch.Tensor): Batch of lengths (B,).
|
|
174
|
+
Returns:
|
|
175
|
+
torch.Tensor: Mask tensor containing indices of padded part.
|
|
176
|
+
|
|
177
|
+
Examples:
|
|
178
|
+
>>> lengths = [5, 3, 2]
|
|
179
|
+
>>> make_pad_mask(lengths)
|
|
180
|
+
masks = [[0, 0, 0, 0 ,0],
|
|
181
|
+
[0, 0, 0, 1, 1],
|
|
182
|
+
[0, 0, 1, 1, 1]]
|
|
183
|
+
"""
|
|
184
|
+
lengths = lengths.long()
|
|
185
|
+
batch_size = lengths.size(0)
|
|
186
|
+
max_len = max_len if max_len > 0 else lengths.max().item()
|
|
187
|
+
seq_range = torch.arange(0,
|
|
188
|
+
max_len,
|
|
189
|
+
dtype=torch.int64,
|
|
190
|
+
device=lengths.device)
|
|
191
|
+
seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
|
|
192
|
+
seq_length_expand = lengths.unsqueeze(-1)
|
|
193
|
+
mask = seq_range_expand >= seq_length_expand
|
|
194
|
+
return mask
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""mel-spectrogram extraction in Matcha-TTS"""
|
|
2
|
+
import logging
|
|
3
|
+
from librosa.filters import mel as librosa_mel_fn
|
|
4
|
+
import torch
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# NOTE: they decalred these global vars
|
|
11
|
+
mel_basis = {}
|
|
12
|
+
hann_window = {}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
|
|
16
|
+
return torch.log(torch.clamp(x, min=clip_val) * C)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def spectral_normalize_torch(magnitudes):
|
|
20
|
+
output = dynamic_range_compression_torch(magnitudes)
|
|
21
|
+
return output
|
|
22
|
+
|
|
23
|
+
"""
|
|
24
|
+
feat_extractor: !name:matcha.utils.audio.mel_spectrogram
|
|
25
|
+
n_fft: 1920
|
|
26
|
+
num_mels: 80
|
|
27
|
+
sampling_rate: 24000
|
|
28
|
+
hop_size: 480
|
|
29
|
+
win_size: 1920
|
|
30
|
+
fmin: 0
|
|
31
|
+
fmax: 8000
|
|
32
|
+
center: False
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def mel_spectrogram(y, n_fft=1920, num_mels=80, sampling_rate=24000, hop_size=480, win_size=1920,
|
|
37
|
+
fmin=0, fmax=8000, center=False):
|
|
38
|
+
"""Copied from https://github.com/shivammehta25/Matcha-TTS/blob/main/matcha/utils/audio.py
|
|
39
|
+
Set default values according to Cosyvoice's config.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
if isinstance(y, np.ndarray):
|
|
43
|
+
y = torch.tensor(y).float()
|
|
44
|
+
|
|
45
|
+
if len(y.shape) == 1:
|
|
46
|
+
y = y[None, ]
|
|
47
|
+
|
|
48
|
+
# Debug: Check for audio clipping (values outside [-1.0, 1.0] range)
|
|
49
|
+
min_val = torch.min(y)
|
|
50
|
+
max_val = torch.max(y)
|
|
51
|
+
if min_val < -1.0 or max_val > 1.0:
|
|
52
|
+
logger.warning(f"Audio values outside normalized range: min={min_val.item():.4f}, max={max_val.item():.4f}")
|
|
53
|
+
|
|
54
|
+
global mel_basis, hann_window # pylint: disable=global-statement,global-variable-not-assigned
|
|
55
|
+
if f"{str(fmax)}_{str(y.device)}" not in mel_basis:
|
|
56
|
+
mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax)
|
|
57
|
+
mel_basis[str(fmax) + "_" + str(y.device)] = torch.from_numpy(mel).float().to(y.device)
|
|
58
|
+
hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device)
|
|
59
|
+
|
|
60
|
+
y = torch.nn.functional.pad(
|
|
61
|
+
y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), mode="reflect"
|
|
62
|
+
)
|
|
63
|
+
y = y.squeeze(1)
|
|
64
|
+
|
|
65
|
+
spec = torch.view_as_real(
|
|
66
|
+
torch.stft(
|
|
67
|
+
y,
|
|
68
|
+
n_fft,
|
|
69
|
+
hop_length=hop_size,
|
|
70
|
+
win_length=win_size,
|
|
71
|
+
window=hann_window[str(y.device)],
|
|
72
|
+
center=center,
|
|
73
|
+
pad_mode="reflect",
|
|
74
|
+
normalized=False,
|
|
75
|
+
onesided=True,
|
|
76
|
+
return_complex=True,
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
|
|
81
|
+
|
|
82
|
+
spec = torch.matmul(mel_basis[str(fmax) + "_" + str(y.device)], spec)
|
|
83
|
+
spec = spectral_normalize_torch(spec)
|
|
84
|
+
|
|
85
|
+
return spec
|