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

Add sox_effects submodule and delegate sox_effects init/shutdown (#708)

There are couple of aspects of this PR that overall improves the maintainability of the code base, based on "decoupling" and "separation of concerns".

First, `sox_effects` functionalities can be either available or unavailable. From the viewpoint of `torchaudio` main module, the looser the connection between the `torchaudio` module and `torchaudio.sox_effects`, the more manageable the code base become because you can change the two modules independently. This is mostly accomplished when the definitions of `initialize_sox` and `shutdown_sox` were moved from `torchaudio.__init__` to `torchaudio.sox_effects`, but the initialization of sox effects are still happening in `torchaudio.__init__`. If we move the initialization to `sox_effects` module, the responsibility of sox initialization is moved to `sox_effects` module, along with the required module availability check etc. The main `torchaudio` module can be carefree about how the `sox_effects` module should work.

In addition to that, I found that `initialize_sox` and `shutdown_sox` are confusing because it sound like they are required for `libsox` based I/O. To make it clear, I renamed them to include `sox_effect` in function name.

Also moving functions from the original places are BC breaking itself, therefore, these functions are re-imported in `torchaudio.__init__` and renamed to match the original names. Therefore the PR is not BC breaking.
parent 82965683
...@@ -23,11 +23,10 @@ from torchaudio.backend import ( ...@@ -23,11 +23,10 @@ from torchaudio.backend import (
SignalInfo, SignalInfo,
EncodingInfo, EncodingInfo,
) )
from torchaudio._internal import ( from torchaudio.sox_effects import (
module_utils as _mod_utils, init_sox_effects as initialize_sox,
misc_ops as _misc_ops, shutdown_sox_effects as shutdown_sox,
) )
from torchaudio.sox_effects import initialize_sox, shutdown_sox
try: try:
from .version import __version__, git_version # noqa: F401 from .version import __version__, git_version # noqa: F401
...@@ -35,11 +34,6 @@ except ImportError: ...@@ -35,11 +34,6 @@ except ImportError:
pass pass
if _mod_utils.is_module_available('torchaudio._torchaudio'):
from . import _torchaudio
initialize_sox()
def load(filepath: Union[str, Path], def load(filepath: Union[str, Path],
out: Optional[Tensor] = None, out: Optional[Tensor] = None,
normalization: Union[bool, float, Callable] = True, normalization: Union[bool, float, Callable] = True,
......
from torchaudio._internal import module_utils as _mod_utils
from .sox_effects import (
init_sox_effects,
shutdown_sox_effects,
effect_names,
SoxEffect,
SoxEffectsChain,
)
if _mod_utils.is_module_available('torchaudio._torchaudio'):
init_sox_effects()
...@@ -2,7 +2,6 @@ import atexit ...@@ -2,7 +2,6 @@ import atexit
from typing import Any, Callable, List, Optional, Tuple, Union from typing import Any, Callable, List, Optional, Tuple, Union
import torch import torch
import torchaudio
from torch import Tensor from torch import Tensor
from torchaudio._internal import ( from torchaudio._internal import (
...@@ -11,7 +10,7 @@ from torchaudio._internal import ( ...@@ -11,7 +10,7 @@ from torchaudio._internal import (
) )
if _mod_utils.is_module_available('torchaudio._torchaudio'): if _mod_utils.is_module_available('torchaudio._torchaudio'):
from . import _torchaudio from torchaudio import _torchaudio
_SOX_INITIALIZED: Optional[bool] = False _SOX_INITIALIZED: Optional[bool] = False
...@@ -26,7 +25,7 @@ _SOX_SUCCESS_CODE = 0 ...@@ -26,7 +25,7 @@ _SOX_SUCCESS_CODE = 0
@_mod_utils.requires_module('torchaudio._torchaudio') @_mod_utils.requires_module('torchaudio._torchaudio')
def initialize_sox() -> int: def init_sox_effects() -> int:
"""Initialize sox for use with effects chains. """Initialize sox for use with effects chains.
You only need to call this function once to use SoX effects chains multiple times. You only need to call this function once to use SoX effects chains multiple times.
...@@ -48,13 +47,13 @@ def initialize_sox() -> int: ...@@ -48,13 +47,13 @@ def initialize_sox() -> int:
code = _torchaudio.initialize_sox() code = _torchaudio.initialize_sox()
if code == _SOX_SUCCESS_CODE: if code == _SOX_SUCCESS_CODE:
_SOX_INITIALIZED = True _SOX_INITIALIZED = True
atexit.register(shutdown_sox) atexit.register(shutdown_sox_effects)
return code return code
return _SOX_SUCCESS_CODE return _SOX_SUCCESS_CODE
@_mod_utils.requires_module("torchaudio._torchaudio") @_mod_utils.requires_module("torchaudio._torchaudio")
def shutdown_sox() -> int: def shutdown_sox_effects() -> int:
"""Showdown sox for effects chain. """Showdown sox for effects chain.
You do not need to call this function as it will be called automatically You do not need to call this function as it will be called automatically
......
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