Skip to content

Commit 26c419c

Browse files
authored
[audio]fix audio get_window security error (#47386)
* fix window security error * format
1 parent 0f649b3 commit 26c419c

1 file changed

Lines changed: 41 additions & 3 deletions

File tree

python/paddle/audio/functional/window.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,39 @@
1919
from paddle import Tensor
2020

2121

22+
class WindowFunctionRegister(object):
23+
def __init__(self):
24+
self._functions_dict = dict()
25+
26+
def register(self, func=None):
27+
def add_subfunction(func):
28+
name = func.__name__
29+
self._functions_dict[name] = func
30+
return func
31+
32+
return add_subfunction
33+
34+
def get(self, name):
35+
return self._functions_dict[name]
36+
37+
38+
window_function_register = WindowFunctionRegister()
39+
40+
41+
@window_function_register.register()
2242
def _cat(x: List[Tensor], data_type: str) -> Tensor:
2343
l = [paddle.to_tensor(_, data_type) for _ in x]
2444
return paddle.concat(l)
2545

2646

47+
@window_function_register.register()
2748
def _acosh(x: Union[Tensor, float]) -> Tensor:
2849
if isinstance(x, float):
2950
return math.log(x + math.sqrt(x**2 - 1))
3051
return paddle.log(x + paddle.sqrt(paddle.square(x) - 1))
3152

3253

54+
@window_function_register.register()
3355
def _extend(M: int, sym: bool) -> bool:
3456
"""Extend window by 1 sample if needed for DFT-even symmetry."""
3557
if not sym:
@@ -38,6 +60,7 @@ def _extend(M: int, sym: bool) -> bool:
3860
return M, False
3961

4062

63+
@window_function_register.register()
4164
def _len_guards(M: int) -> bool:
4265
"""Handle small or incorrect window lengths."""
4366
if int(M) != M or M < 0:
@@ -46,6 +69,7 @@ def _len_guards(M: int) -> bool:
4669
return M <= 1
4770

4871

72+
@window_function_register.register()
4973
def _truncate(w: Tensor, needed: bool) -> Tensor:
5074
"""Truncate window by 1 sample if needed for DFT-even symmetry."""
5175
if needed:
@@ -54,6 +78,7 @@ def _truncate(w: Tensor, needed: bool) -> Tensor:
5478
return w
5579

5680

81+
@window_function_register.register()
5782
def _general_gaussian(
5883
M: int, p, sig, sym: bool = True, dtype: str = 'float64'
5984
) -> Tensor:
@@ -70,6 +95,7 @@ def _general_gaussian(
7095
return _truncate(w, needs_trunc)
7196

7297

98+
@window_function_register.register()
7399
def _general_cosine(
74100
M: int, a: float, sym: bool = True, dtype: str = 'float64'
75101
) -> Tensor:
@@ -86,6 +112,7 @@ def _general_cosine(
86112
return _truncate(w, needs_trunc)
87113

88114

115+
@window_function_register.register()
89116
def _general_hamming(
90117
M: int, alpha: float, sym: bool = True, dtype: str = 'float64'
91118
) -> Tensor:
@@ -95,6 +122,7 @@ def _general_hamming(
95122
return _general_cosine(M, [alpha, 1.0 - alpha], sym, dtype=dtype)
96123

97124

125+
@window_function_register.register()
98126
def _taylor(
99127
M: int, nbar=4, sll=30, norm=True, sym: bool = True, dtype: str = 'float64'
100128
) -> Tensor:
@@ -151,6 +179,7 @@ def W(n):
151179
return _truncate(w, needs_trunc)
152180

153181

182+
@window_function_register.register()
154183
def _hamming(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
155184
"""Compute a Hamming window.
156185
The Hamming window is a taper formed by using a raised cosine with
@@ -159,6 +188,7 @@ def _hamming(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
159188
return _general_hamming(M, 0.54, sym, dtype=dtype)
160189

161190

191+
@window_function_register.register()
162192
def _hann(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
163193
"""Compute a Hann window.
164194
The Hann window is a taper formed by using a raised cosine or sine-squared
@@ -167,6 +197,7 @@ def _hann(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
167197
return _general_hamming(M, 0.5, sym, dtype=dtype)
168198

169199

200+
@window_function_register.register()
170201
def _tukey(
171202
M: int, alpha=0.5, sym: bool = True, dtype: str = 'float64'
172203
) -> Tensor:
@@ -200,6 +231,7 @@ def _tukey(
200231
return _truncate(w, needs_trunc)
201232

202233

234+
@window_function_register.register()
203235
def _kaiser(
204236
M: int, beta: float, sym: bool = True, dtype: str = 'float64'
205237
) -> Tensor:
@@ -209,6 +241,7 @@ def _kaiser(
209241
raise NotImplementedError()
210242

211243

244+
@window_function_register.register()
212245
def _gaussian(
213246
M: int, std: float, sym: bool = True, dtype: str = 'float64'
214247
) -> Tensor:
@@ -226,6 +259,7 @@ def _gaussian(
226259
return _truncate(w, needs_trunc)
227260

228261

262+
@window_function_register.register()
229263
def _exponential(
230264
M: int, center=None, tau=1.0, sym: bool = True, dtype: str = 'float64'
231265
) -> Tensor:
@@ -245,6 +279,7 @@ def _exponential(
245279
return _truncate(w, needs_trunc)
246280

247281

282+
@window_function_register.register()
248283
def _triang(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
249284
"""Compute a triangular window."""
250285
if _len_guards(M):
@@ -262,6 +297,7 @@ def _triang(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
262297
return _truncate(w, needs_trunc)
263298

264299

300+
@window_function_register.register()
265301
def _bohman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
266302
"""Compute a Bohman window.
267303
The Bohman window is the autocorrelation of a cosine window.
@@ -279,6 +315,7 @@ def _bohman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
279315
return _truncate(w, needs_trunc)
280316

281317

318+
@window_function_register.register()
282319
def _blackman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
283320
"""Compute a Blackman window.
284321
The Blackman window is a taper formed by using the first three terms of
@@ -289,6 +326,7 @@ def _blackman(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
289326
return _general_cosine(M, [0.42, 0.50, 0.08], sym, dtype=dtype)
290327

291328

329+
@window_function_register.register()
292330
def _cosine(M: int, sym: bool = True, dtype: str = 'float64') -> Tensor:
293331
"""Compute a window with a simple cosine shape."""
294332
if _len_guards(M):
@@ -308,7 +346,7 @@ def get_window(
308346
"""Return a window of a given length and type.
309347
310348
Args:
311-
window (Union[str, Tuple[str, float]]): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'.
349+
window (Union[str, Tuple[str, float]]): The window function applied to the signal before the Fourier transform. Supported window functions: 'hamming', 'hann', 'kaiser', 'gaussian', 'general_gaussian', 'exponential', 'triang', 'bohman', 'blackman', 'cosine', 'tukey', 'taylor'.
312350
win_length (int): Number of samples.
313351
fftbins (bool, optional): If True, create a "periodic" window. Otherwise, create a "symmetric" window, for use in filter design. Defaults to True.
314352
dtype (str, optional): The data type of the return window. Defaults to 'float64'.
@@ -348,8 +386,8 @@ def get_window(
348386
)
349387

350388
try:
351-
winfunc = eval('_' + winstr)
352-
except NameError as e:
389+
winfunc = window_function_register.get('_' + winstr)
390+
except KeyError as e:
353391
raise ValueError("Unknown window type.") from e
354392

355393
params = (win_length,) + args

0 commit comments

Comments
 (0)