Unverified Commit 9f3075c1 authored by moto's avatar moto Committed by GitHub
Browse files

Move sox effect init/shut down to sox_effects module

For better cohesion and coupling, move sox_effects related functions to sox_effects module.
Then perform initialization in __init__.py.

See the following for cohesion and coupling.
https://en.wikipedia.org/wiki/Coupling_(computer_programming)
parent e61b77dc
import atexit
import os.path
from pathlib import Path
from typing import Any, Callable, Optional, Tuple, Union
......@@ -20,6 +19,7 @@ from torchaudio._backend import (
)
from torchaudio._soundfile_backend import SignalInfo, EncodingInfo
from torchaudio._internal import module_utils as _mod_utils
from torchaudio.sox_effects import initialize_sox, shutdown_sox
try:
from .version import __version__, git_version # noqa: F401
......@@ -29,6 +29,7 @@ except ImportError:
if _mod_utils.is_module_available('torchaudio._torchaudio'):
from . import _torchaudio
initialize_sox()
def load(filepath: Union[str, Path],
......@@ -329,67 +330,6 @@ def get_sox_bool(i: int = 0) -> Any:
return _torchaudio.sox_bool(i)
_SOX_INITIALIZED: Optional[bool] = False
# This variable has a micro lifecycle. (False -> True -> None)
# False: Not initialized
# True: Initialized
# None: Already shut down (should not be initialized again.)
_SOX_SUCCESS_CODE = 0
# defined at
# https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93a9ef2b87ec303edfe40751d9a85fadeeb
@_mod_utils.requires_module("torchaudio._torchaudio")
def initialize_sox() -> int:
"""Initialize sox for use with effects chains.
You only need to call this function once to use SoX effects chains multiple times.
It is safe to call this function multiple times as long as ``shutdown_sox`` is not yet called.
Once ``shutdown_sox`` is called, you can no longer use SoX effects and calling this function
results in `RuntimeError`.
Note:
This function is not required for simple loading.
Returns:
int: Code corresponding to sox_error_t enum. See
https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93
"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED is None:
raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.')
if not _SOX_INITIALIZED:
code = _torchaudio.initialize_sox()
if code == _SOX_SUCCESS_CODE:
_SOX_INITIALIZED = True
atexit.register(shutdown_sox)
return code
return _SOX_SUCCESS_CODE
@_mod_utils.requires_module("torchaudio._torchaudio")
def shutdown_sox() -> int:
"""Showdown sox for effects chain.
You do not need to call this function as it will be called automatically
at the end of program execution, if ``initialize_sox`` was called.
It is safe to call this function multiple times.
Returns:
int: Code corresponding to sox_error_t enum. See
https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93
"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED:
code = _torchaudio.shutdown_sox()
if code == _SOX_INITIALIZED:
_SOX_INITIALIZED = None
return code
return _SOX_SUCCESS_CODE
def _audio_normalization(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
......
import atexit
from typing import Any, Callable, List, Optional, Tuple, Union
import torch
......@@ -10,6 +11,67 @@ if _mod_utils.is_module_available('torchaudio._torchaudio'):
from . import _torchaudio
_SOX_INITIALIZED: Optional[bool] = False
# This variable has a micro lifecycle. (False -> True -> None)
# False: Not initialized
# True: Initialized
# None: Already shut down (should not be initialized again.)
_SOX_SUCCESS_CODE = 0
# defined at
# https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93a9ef2b87ec303edfe40751d9a85fadeeb
@_mod_utils.requires_module('torchaudio._torchaudio')
def initialize_sox() -> int:
"""Initialize sox for use with effects chains.
You only need to call this function once to use SoX effects chains multiple times.
It is safe to call this function multiple times as long as ``shutdown_sox`` is not yet called.
Once ``shutdown_sox`` is called, you can no longer use SoX effects and calling this function
results in `RuntimeError`.
Note:
This function is not required for simple loading.
Returns:
int: Code corresponding to sox_error_t enum. See
https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93
"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED is None:
raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.')
if not _SOX_INITIALIZED:
code = _torchaudio.initialize_sox()
if code == _SOX_SUCCESS_CODE:
_SOX_INITIALIZED = True
atexit.register(shutdown_sox)
return code
return _SOX_SUCCESS_CODE
@_mod_utils.requires_module("torchaudio._torchaudio")
def shutdown_sox() -> int:
"""Showdown sox for effects chain.
You do not need to call this function as it will be called automatically
at the end of program execution, if ``initialize_sox`` was called.
It is safe to call this function multiple times.
Returns:
int: Code corresponding to sox_error_t enum. See
https://fossies.org/dox/sox-14.4.2/sox_8h.html#a8e07e80cebeff3339265d89c387cea93
"""
global _SOX_INITIALIZED
if _SOX_INITIALIZED:
code = _torchaudio.shutdown_sox()
if code == _SOX_INITIALIZED:
_SOX_INITIALIZED = None
return code
return _SOX_SUCCESS_CODE
@_mod_utils.requires_module('torchaudio._torchaudio')
def effect_names() -> List[str]:
"""Gets list of valid sox effect names
......@@ -149,7 +211,6 @@ class SoxEffectsChain(object):
# print("effect options:", [x.eopts for x in self.chain])
torchaudio.initialize_sox()
sr = _torchaudio.build_flow_effects(self.input_file,
out,
self.channels_first,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment