-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move utility Tensor functions to misc_ops module (#694)
* also deletes duplicated func
- Loading branch information
Showing
6 changed files
with
53 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Union, Callable | ||
|
||
import torch | ||
from torch import Tensor | ||
|
||
|
||
def normalize_audio(signal: Tensor, normalization: Union[bool, float, Callable]) -> None: | ||
"""Audio normalization of a tensor in-place. The normalization can be a bool, | ||
a number, or a callable that takes the audio tensor as an input. SoX uses | ||
32-bit signed integers internally, thus bool normalizes based on that assumption. | ||
""" | ||
|
||
if not normalization: | ||
return | ||
|
||
if isinstance(normalization, bool): | ||
normalization = 1 << 31 | ||
|
||
if isinstance(normalization, (float, int)): | ||
# normalize with custom value | ||
signal /= normalization | ||
elif callable(normalization): | ||
signal /= normalization(signal) | ||
|
||
|
||
def check_input(src: Tensor) -> None: | ||
if not torch.is_tensor(src): | ||
raise TypeError('Expected a tensor, got %s' % type(src)) | ||
if src.is_cuda: | ||
raise TypeError('Expected a CPU based tensor, got %s' % type(src)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters