Skip to content

Commit c82a7f9

Browse files
authored
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.
1 parent 8296568 commit c82a7f9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

torchaudio/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,17 @@
2323
SignalInfo,
2424
EncodingInfo,
2525
)
26-
from torchaudio._internal import (
27-
module_utils as _mod_utils,
28-
misc_ops as _misc_ops,
26+
from torchaudio.sox_effects import (
27+
init_sox_effects as initialize_sox,
28+
shutdown_sox_effects as shutdown_sox,
2929
)
30-
from torchaudio.sox_effects import initialize_sox, shutdown_sox
3130

3231
try:
3332
from .version import __version__, git_version # noqa: F401
3433
except ImportError:
3534
pass
3635

3736

38-
if _mod_utils.is_module_available('torchaudio._torchaudio'):
39-
from . import _torchaudio
40-
initialize_sox()
41-
42-
4337
def load(filepath: Union[str, Path],
4438
out: Optional[Tensor] = None,
4539
normalization: Union[bool, float, Callable] = True,

torchaudio/sox_effects/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from torchaudio._internal import module_utils as _mod_utils
2+
from .sox_effects import (
3+
init_sox_effects,
4+
shutdown_sox_effects,
5+
effect_names,
6+
SoxEffect,
7+
SoxEffectsChain,
8+
)
9+
10+
11+
if _mod_utils.is_module_available('torchaudio._torchaudio'):
12+
init_sox_effects()

torchaudio/sox_effects.py renamed to torchaudio/sox_effects/sox_effects.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from typing import Any, Callable, List, Optional, Tuple, Union
33

44
import torch
5-
import torchaudio
65
from torch import Tensor
76

87
from torchaudio._internal import (
@@ -11,7 +10,7 @@
1110
)
1211

1312
if _mod_utils.is_module_available('torchaudio._torchaudio'):
14-
from . import _torchaudio
13+
from torchaudio import _torchaudio
1514

1615

1716
_SOX_INITIALIZED: Optional[bool] = False
@@ -26,7 +25,7 @@
2625

2726

2827
@_mod_utils.requires_module('torchaudio._torchaudio')
29-
def initialize_sox() -> int:
28+
def init_sox_effects() -> int:
3029
"""Initialize sox for use with effects chains.
3130
3231
You only need to call this function once to use SoX effects chains multiple times.
@@ -48,13 +47,13 @@ def initialize_sox() -> int:
4847
code = _torchaudio.initialize_sox()
4948
if code == _SOX_SUCCESS_CODE:
5049
_SOX_INITIALIZED = True
51-
atexit.register(shutdown_sox)
50+
atexit.register(shutdown_sox_effects)
5251
return code
5352
return _SOX_SUCCESS_CODE
5453

5554

5655
@_mod_utils.requires_module("torchaudio._torchaudio")
57-
def shutdown_sox() -> int:
56+
def shutdown_sox_effects() -> int:
5857
"""Showdown sox for effects chain.
5958
6059
You do not need to call this function as it will be called automatically

0 commit comments

Comments
 (0)