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

[BC Breaking] Reorganize C++ resources (#630)

This PR
 - Changes the location of C++ codes from
     - from `torchaudio/torch_sox.cpp` to `torchaudio/csrc/sox.cpp`
     - from `torchaudio/torch_sox.h` to `torchaudio/csrc/sox.h`
 - Changes the location where the resulting library is placed
     - from `_torch_sox.so` (outside of `torchaudio` module)
        to `torchaudio/_torchaudio.so`.
parent 2c28b743
#!/usr/bin/env python #!/usr/bin/env python
import os import os
import platform import platform
import sys
import subprocess import subprocess
from setuptools import setup, find_packages from setuptools import setup, find_packages
...@@ -85,8 +84,8 @@ if platform.system() == 'Windows': ...@@ -85,8 +84,8 @@ if platform.system() == 'Windows':
else: else:
ext_modules = [ ext_modules = [
CppExtension( CppExtension(
'_torch_sox', 'torchaudio._torchaudio',
['torchaudio/torch_sox.cpp'], ['torchaudio/csrc/sox.cpp'],
libraries=libraries, libraries=libraries,
include_dirs=include_dirs + [cwd], include_dirs=include_dirs + [cwd],
extra_compile_args=eca, extra_compile_args=eca,
...@@ -94,6 +93,7 @@ else: ...@@ -94,6 +93,7 @@ else:
extra_link_args=ela), extra_link_args=ela),
] ]
setup( setup(
name="torchaudio", name="torchaudio",
version=version, version=version,
...@@ -116,8 +116,7 @@ setup( ...@@ -116,8 +116,7 @@ setup(
"Topic :: Multimedia :: Sound/Audio", "Topic :: Multimedia :: Sound/Audio",
"Topic :: Scientific/Engineering :: Artificial Intelligence" "Topic :: Scientific/Engineering :: Artificial Intelligence"
], ],
# Exclude the build files. packages=find_packages(exclude=["build*", "test*", "torchaudio.csrc*"]),
packages=find_packages(exclude=["build"]),
ext_modules=ext_modules, ext_modules=ext_modules,
cmdclass={'build_ext': BuildExtension}, cmdclass={'build_ext': BuildExtension},
install_requires=[pytorch_package_dep] install_requires=[pytorch_package_dep]
......
...@@ -193,8 +193,8 @@ def save_encinfo(filepath: str, ...@@ -193,8 +193,8 @@ def save_encinfo(filepath: str,
# save data to file # save data to file
src = src.contiguous() src = src.contiguous()
import _torch_sox from . import _torchaudio
_torch_sox.write_audio_file(filepath, src, signalinfo, encodinginfo, filetype) _torchaudio.write_audio_file(filepath, src, signalinfo, encodinginfo, filetype)
def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]: def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]:
...@@ -236,8 +236,8 @@ def sox_signalinfo_t() -> SignalInfo: ...@@ -236,8 +236,8 @@ def sox_signalinfo_t() -> SignalInfo:
>>> si.length = 0 >>> si.length = 0
""" """
import _torch_sox from . import _torchaudio
return _torch_sox.sox_signalinfo_t() return _torchaudio.sox_signalinfo_t()
@_audio_backend_guard("sox") @_audio_backend_guard("sox")
...@@ -271,8 +271,8 @@ def sox_encodinginfo_t() -> EncodingInfo: ...@@ -271,8 +271,8 @@ def sox_encodinginfo_t() -> EncodingInfo:
""" """
import _torch_sox from . import _torchaudio
ei = _torch_sox.sox_encodinginfo_t() ei = _torchaudio.sox_encodinginfo_t()
sdo = get_sox_option_t(2) # sox_default_option sdo = get_sox_option_t(2) # sox_default_option
ei.reverse_bytes = sdo ei.reverse_bytes = sdo
ei.reverse_nibbles = sdo ei.reverse_nibbles = sdo
...@@ -292,12 +292,12 @@ def get_sox_encoding_t(i: int = None) -> EncodingInfo: ...@@ -292,12 +292,12 @@ def get_sox_encoding_t(i: int = None) -> EncodingInfo:
sox_encoding_t: A sox_encoding_t type for output encoding sox_encoding_t: A sox_encoding_t type for output encoding
""" """
import _torch_sox from . import _torchaudio
if i is None: if i is None:
# one can see all possible values using the .__members__ attribute # one can see all possible values using the .__members__ attribute
return _torch_sox.sox_encoding_t return _torchaudio.sox_encoding_t
else: else:
return _torch_sox.sox_encoding_t(i) return _torchaudio.sox_encoding_t(i)
@_audio_backend_guard("sox") @_audio_backend_guard("sox")
...@@ -312,11 +312,11 @@ def get_sox_option_t(i: int = 2) -> Any: ...@@ -312,11 +312,11 @@ def get_sox_option_t(i: int = 2) -> Any:
sox_option_t: A sox_option_t type sox_option_t: A sox_option_t type
""" """
import _torch_sox from . import _torchaudio
if i is None: if i is None:
return _torch_sox.sox_option_t return _torchaudio.sox_option_t
else: else:
return _torch_sox.sox_option_t(i) return _torchaudio.sox_option_t(i)
@_audio_backend_guard("sox") @_audio_backend_guard("sox")
...@@ -332,11 +332,11 @@ def get_sox_bool(i: int = 0) -> Any: ...@@ -332,11 +332,11 @@ def get_sox_bool(i: int = 0) -> Any:
sox_bool: A sox_bool type sox_bool: A sox_bool type
""" """
import _torch_sox from . import _torchaudio
if i is None: if i is None:
return _torch_sox.sox_bool return _torchaudio.sox_bool
else: else:
return _torch_sox.sox_bool(i) return _torchaudio.sox_bool(i)
_SOX_INITIALIZED: Optional[bool] = False _SOX_INITIALIZED: Optional[bool] = False
...@@ -370,8 +370,8 @@ def initialize_sox() -> int: ...@@ -370,8 +370,8 @@ def initialize_sox() -> int:
if _SOX_INITIALIZED is None: if _SOX_INITIALIZED is None:
raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.') raise RuntimeError('SoX effects chain has been already shut down. Can not initialize again.')
if not _SOX_INITIALIZED: if not _SOX_INITIALIZED:
import _torch_sox from . import _torchaudio
code = _torch_sox.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)
...@@ -394,8 +394,8 @@ def shutdown_sox() -> int: ...@@ -394,8 +394,8 @@ def shutdown_sox() -> int:
""" """
global _SOX_INITIALIZED global _SOX_INITIALIZED
if _SOX_INITIALIZED: if _SOX_INITIALIZED:
import _torch_sox from . import _torchaudio
code = _torch_sox.shutdown_sox() code = _torchaudio.shutdown_sox()
if code == _SOX_INITIALIZED: if code == _SOX_INITIALIZED:
_SOX_INITIALIZED = None _SOX_INITIALIZED = None
return code return code
......
...@@ -35,8 +35,8 @@ def load(filepath: str, ...@@ -35,8 +35,8 @@ def load(filepath: str,
if offset < 0: if offset < 0:
raise ValueError("Expected positive offset value") raise ValueError("Expected positive offset value")
import _torch_sox from . import _torchaudio
sample_rate = _torch_sox.read_audio_file( sample_rate = _torchaudio.read_audio_file(
filepath, filepath,
out, out,
channels_first, channels_first,
...@@ -68,5 +68,5 @@ def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, chan ...@@ -68,5 +68,5 @@ def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, chan
def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]: def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]:
r"""See torchaudio.info""" r"""See torchaudio.info"""
import _torch_sox from . import _torchaudio
return _torch_sox.get_info(filepath) return _torchaudio.get_info(filepath)
#include <torchaudio/torch_sox.h> #include <torchaudio/csrc/sox.h>
#include <torch/extension.h>
#include <sox.h>
#include <algorithm> #include <algorithm>
#include <cstdint> #include <cstdint>
...@@ -385,7 +381,7 @@ int build_flow_effects(const std::string& file_name, ...@@ -385,7 +381,7 @@ int build_flow_effects(const std::string& file_name,
} // namespace audio } // namespace audio
} // namespace torch } // namespace torch
PYBIND11_MODULE(_torch_sox, m) { PYBIND11_MODULE(_torchaudio, m) {
py::class_<torch::audio::SoxEffect>(m, "SoxEffect") py::class_<torch::audio::SoxEffect>(m, "SoxEffect")
.def(py::init<>()) .def(py::init<>())
.def("__repr__", [](const torch::audio::SoxEffect &self) { .def("__repr__", [](const torch::audio::SoxEffect &self) {
......
...@@ -16,8 +16,8 @@ def effect_names() -> List[str]: ...@@ -16,8 +16,8 @@ def effect_names() -> List[str]:
>>> EFFECT_NAMES = torchaudio.sox_effects.effect_names() >>> EFFECT_NAMES = torchaudio.sox_effects.effect_names()
""" """
import _torch_sox from . import _torchaudio
return _torch_sox.get_effect_names() return _torchaudio.get_effect_names()
@_audio_backend_guard("sox") @_audio_backend_guard("sox")
...@@ -29,8 +29,8 @@ def SoxEffect(): ...@@ -29,8 +29,8 @@ def SoxEffect():
name of effect, and eopts (List[str]) which is a list of effect options. name of effect, and eopts (List[str]) which is a list of effect options.
""" """
import _torch_sox from . import _torchaudio
return _torch_sox.SoxEffect() return _torchaudio.SoxEffect()
class SoxEffectsChain(object): class SoxEffectsChain(object):
...@@ -150,15 +150,15 @@ class SoxEffectsChain(object): ...@@ -150,15 +150,15 @@ class SoxEffectsChain(object):
# print("effect options:", [x.eopts for x in self.chain]) # print("effect options:", [x.eopts for x in self.chain])
torchaudio.initialize_sox() torchaudio.initialize_sox()
import _torch_sox from . import _torchaudio
sr = _torch_sox.build_flow_effects(self.input_file, sr = _torchaudio.build_flow_effects(self.input_file,
out, out,
self.channels_first, self.channels_first,
self.out_siginfo, self.out_siginfo,
self.out_encinfo, self.out_encinfo,
self.filetype, self.filetype,
self.chain, self.chain,
self.MAX_EFFECT_OPTS) self.MAX_EFFECT_OPTS)
torchaudio._audio_normalization(out, self.normalization) torchaudio._audio_normalization(out, self.normalization)
......
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