Commit 8588fba1 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Replace libsox with stub library (#3497)

Summary:
This commit updates the way libsox is integrated to torchaudio

1. We stop statically linking libsox, so torchaudio will not ship libsox.
2. We link libsox dynamically. Users are expected to install libsox by themselves.
3. We use stab library to build torchaudio.

Pull Request resolved: https://github.com/pytorch/audio/pull/3497

Differential Revision: D47803706

Pulled By: mthrok

fbshipit-source-id: 31b05495d81069186fa52d67beea360cc7e817a8
parent 29903c5c
...@@ -75,7 +75,7 @@ fi ...@@ -75,7 +75,7 @@ fi
# Note: installing librosa via pip fail because it will try to compile numba. # Note: installing librosa via pip fail because it will try to compile numba.
( (
set -x set -x
conda install -y -c conda-forge ${NUMBA_DEV_CHANNEL} 'librosa==0.10.0' parameterized 'requests>=2.20' 'ffmpeg>=5,<7' conda install -y -c conda-forge ${NUMBA_DEV_CHANNEL} sox libvorbis 'librosa==0.10.0' parameterized 'requests>=2.20' 'ffmpeg>=5,<7'
pip install kaldi-io SoundFile coverage pytest pytest-cov 'scipy==1.7.3' expecttest unidecode inflect Pillow sentencepiece pytorch-lightning 'protobuf<4.21.0' demucs tinytag pyroomacoustics flashlight-text git+https://github.com/kpu/kenlm pip install kaldi-io SoundFile coverage pytest pytest-cov 'scipy==1.7.3' expecttest unidecode inflect Pillow sentencepiece pytorch-lightning 'protobuf<4.21.0' demucs tinytag pyroomacoustics flashlight-text git+https://github.com/kpu/kenlm
) )
# Install fairseq # Install fairseq
......
...@@ -63,7 +63,7 @@ jobs: ...@@ -63,7 +63,7 @@ jobs:
echo "::endgroup::" echo "::endgroup::"
echo "::group::Install TorchAudio" echo "::group::Install TorchAudio"
conda install --quiet --yes pkg-config cmake>=3.18.0 ninja conda install --quiet --yes cmake>=3.18.0 ninja
pip3 install --progress-bar off -v -e . --no-use-pep517 pip3 install --progress-bar off -v -e . --no-use-pep517
echo "::endgroup::" echo "::endgroup::"
...@@ -75,7 +75,7 @@ jobs: ...@@ -75,7 +75,7 @@ jobs:
conda install \ conda install \
--quiet --yes \ --quiet --yes \
-c conda-forge \ -c conda-forge \
pandoc doxygen pysoundfile sox libvorbis pandoc doxygen pysoundfile
pip install --progress-bar off \ pip install --progress-bar off \
git+https://github.com/kpu/kenlm/ flashlight-text \ git+https://github.com/kpu/kenlm/ flashlight-text \
-r docs/requirements.txt -r docs/requirements-tutorials.txt -r docs/requirements.txt -r docs/requirements-tutorials.txt
......
...@@ -24,7 +24,7 @@ jobs: ...@@ -24,7 +24,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: | run: |
sudo add-apt-repository -y ppa:jonathonf/ffmpeg-4 sudo add-apt-repository -y ppa:jonathonf/ffmpeg-4
sudo apt install -y -qq pkg-config libavfilter-dev libavdevice-dev sudo apt install -y -qq libavfilter-dev libavdevice-dev
- name: Install packages - name: Install packages
run: | run: |
python -m pip install --quiet --upgrade pip python -m pip install --quiet --upgrade pip
......
...@@ -50,6 +50,8 @@ jobs: ...@@ -50,6 +50,8 @@ jobs:
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MACOS=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_DECODER=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_ENCODER=true
echo '::endgroup::' echo '::endgroup::'
set -euxo pipefail set -euxo pipefail
......
...@@ -38,6 +38,8 @@ jobs: ...@@ -38,6 +38,8 @@ jobs:
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_CUDA_SMALL_MEMORY=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_CUDA_SMALL_MEMORY=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_ON_PYTHON_310=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_DECODER=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_ENCODER=true
# Set CHANNEL # Set CHANNEL
if [[(${GITHUB_EVENT_NAME} = 'pull_request' && (${GITHUB_BASE_REF} = 'release'*)) || (${GITHUB_REF} = 'refs/heads/release'*) ]]; then if [[(${GITHUB_EVENT_NAME} = 'pull_request' && (${GITHUB_BASE_REF} = 'release'*)) || (${GITHUB_REF} = 'refs/heads/release'*) ]]; then
...@@ -63,7 +65,7 @@ jobs: ...@@ -63,7 +65,7 @@ jobs:
echo "::endgroup::" echo "::endgroup::"
echo "::group::Install TorchAudio" echo "::group::Install TorchAudio"
conda install --quiet --yes pkg-config 'cmake>=3.18.0' ninja conda install --quiet --yes 'cmake>=3.18.0' ninja
pip3 install --progress-bar off -v -e . --no-use-pep517 pip3 install --progress-bar off -v -e . --no-use-pep517
echo "::endgroup::" echo "::endgroup::"
...@@ -76,7 +78,7 @@ jobs: ...@@ -76,7 +78,7 @@ jobs:
--quiet --yes \ --quiet --yes \
-c conda-forge \ -c conda-forge \
-c numba/label/dev \ -c numba/label/dev \
'librosa==0.10.0' parameterized 'requests>=2.20' sox libvorbis 'librosa==0.10.0' parameterized 'requests>=2.20'
pip3 install --progress-bar off \ pip3 install --progress-bar off \
kaldi-io \ kaldi-io \
SoundFile \ SoundFile \
......
...@@ -49,6 +49,8 @@ jobs: ...@@ -49,6 +49,8 @@ jobs:
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_MOD_sentencepiece=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_AUDIO_OUT_DEVICE=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true export TORCHAUDIO_TEST_ALLOW_SKIP_IF_TEMPORARY_DISABLED=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_DECODER=true
export TORCHAUDIO_TEST_ALLOW_SKIP_IF_NO_SOX_ENCODER=true
echo '::endgroup::' echo '::endgroup::'
set -euxo pipefail set -euxo pipefail
......
...@@ -49,13 +49,11 @@ torchaudio. ...@@ -49,13 +49,11 @@ torchaudio.
conda install pytorch -c pytorch-nightly conda install pytorch -c pytorch-nightly
``` ```
### Install build dependencies ### Install build/runtime dependencies
```bash ```bash
# Install build-time dependencies # Install build-time dependencies
pip install cmake ninja pip install cmake ninja
# [optional for sox]
conda install pkg-config
# [optional for ffmpeg] # [optional for ffmpeg]
conda install ffmpeg conda install ffmpeg
``` ```
......
...@@ -121,7 +121,7 @@ Verify the installation by checking the version and CUDA device accessibility. ...@@ -121,7 +121,7 @@ Verify the installation by checking the version and CUDA device accessibility.
.. code-block:: .. code-block::
pip install cmake ninja pkg-config pip install cmake ninja
2. Install dependencies 2. Install dependencies
~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -22,7 +22,7 @@ Here, we install nightly build. ...@@ -22,7 +22,7 @@ Here, we install nightly build.
.. code-block:: .. code-block::
conda install cmake ninja pkg-config conda install cmake ninja
4. Clone the torchaudio repository 4. Clone the torchaudio repository
---------------------------------- ----------------------------------
......
...@@ -10,7 +10,6 @@ TorchAudio integrates PyTorch for numerical computation and third party librarie ...@@ -10,7 +10,6 @@ TorchAudio integrates PyTorch for numerical computation and third party librarie
- `GCC <https://gcc.gnu.org/>`_ (Linux) - `GCC <https://gcc.gnu.org/>`_ (Linux)
- `Clang <https://clang.llvm.org/>`_ (macOS) - `Clang <https://clang.llvm.org/>`_ (macOS)
- `MSVC <https://visualstudio.microsoft.com>`_ 2019 or newer (Windows) - `MSVC <https://visualstudio.microsoft.com>`_ 2019 or newer (Windows)
- `pkg-config <https://www.freedesktop.org/wiki/Software/pkg-config/>`_ (Linux/macOS, if building sox extension)
- `CUDA toolkit <https://developer.nvidia.com/cudnn>`_ and `cuDNN <https://developer.nvidia.com/cudnn>`_ (if building CUDA extension) - `CUDA toolkit <https://developer.nvidia.com/cudnn>`_ and `cuDNN <https://developer.nvidia.com/cudnn>`_ (if building CUDA extension)
Most of the tools are available in `Conda <https://conda.io/>`_, so we recommend using conda. Most of the tools are available in `Conda <https://conda.io/>`_, so we recommend using conda.
......
...@@ -14,7 +14,6 @@ requirements: ...@@ -14,7 +14,6 @@ requirements:
host: host:
- python - python
- setuptools - setuptools
- pkg-config # [not win]
- cmake - cmake
- ninja - ninja
- pytorch-mutex 1.0 {{ build_variant }} # [not osx ] - pytorch-mutex 1.0 {{ build_variant }} # [not osx ]
......
...@@ -4,7 +4,6 @@ import os ...@@ -4,7 +4,6 @@ import os
import re import re
import shutil import shutil
import subprocess import subprocess
import sys
from pathlib import Path from pathlib import Path
import torch import torch
...@@ -83,18 +82,6 @@ def _get_packages(branch_name, tag): ...@@ -83,18 +82,6 @@ def _get_packages(branch_name, tag):
return find_packages(exclude=exclude) return find_packages(exclude=exclude)
def _init_submodule():
print(" --- Initializing submodules")
try:
subprocess.check_call(["git", "submodule", "init"])
subprocess.check_call(["git", "submodule", "update"])
except Exception:
print(" --- Submodule initalization failed")
print("Please run:\n\tgit submodule update --init --recursive")
sys.exit(1)
print(" --- Initialized submodule")
def _parse_url(path): def _parse_url(path):
with open(path, "r") as file_: with open(path, "r") as file_:
for line in file_: for line in file_:
...@@ -104,18 +91,6 @@ def _parse_url(path): ...@@ -104,18 +91,6 @@ def _parse_url(path):
yield url yield url
def _parse_sources():
third_party_dir = ROOT_DIR / "third_party"
libs = ["sox"]
archive_dir = third_party_dir / "archives"
archive_dir.mkdir(exist_ok=True)
for lib in libs:
cmake_file = third_party_dir / lib / "CMakeLists.txt"
for url in _parse_url(cmake_file):
path = archive_dir / os.path.basename(url)
yield path, url
def _fetch_archives(src): def _fetch_archives(src):
for dest, url in src: for dest, url in src:
if not dest.exists(): if not dest.exists():
...@@ -123,13 +98,6 @@ def _fetch_archives(src): ...@@ -123,13 +98,6 @@ def _fetch_archives(src):
torch.hub.download_url_to_file(url, dest, progress=False) torch.hub.download_url_to_file(url, dest, progress=False)
def _fetch_third_party_libraries():
# Revert this when a submodule is added again
# _init_submodule()
if os.name != "nt":
_fetch_archives(_parse_sources())
def _main(): def _main():
sha = _run_cmd(["git", "rev-parse", "HEAD"]) sha = _run_cmd(["git", "rev-parse", "HEAD"])
branch = _run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"]) branch = _run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"])
...@@ -143,7 +111,6 @@ def _main(): ...@@ -143,7 +111,6 @@ def _main():
print("-- Building version", version) print("-- Building version", version)
_make_version_file(version, sha) _make_version_file(version, sha)
_fetch_third_party_libraries()
with open("README.md") as f: with open("README.md") as f:
long_description = f.read() long_description = f.read()
......
find_package(PkgConfig REQUIRED) include(FetchContent)
include(ExternalProject) FetchContent_Declare(
sox_src
set(INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../install)
set(ARCHIVE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../archives)
set(patch_dir ${PROJECT_SOURCE_DIR}/third_party/patches)
set(COMMON_ARGS --quiet --disable-shared --enable-static --prefix=${INSTALL_DIR} --with-pic --disable-dependency-tracking --disable-debug --disable-examples --disable-doc)
# To pass custom environment variables to ExternalProject_Add command,
# we need to do `${CMAKE_COMMAND} -E env ${envs} <COMMANAD>`.
# https://stackoverflow.com/a/62437353
# We constrcut the custom environment variables here
set(envs
"PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig"
"LDFLAGS=-L${INSTALL_DIR}/lib $ENV{LDFLAGS}"
"CFLAGS=-I${INSTALL_DIR}/include -fvisibility=hidden $ENV{CFLAGS}"
)
ExternalProject_Add(amr
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://sourceforge.net/projects/opencore-amr/files/opencore-amr/opencore-amr-0.1.5.tar.gz
URL_HASH SHA256=2c006cb9d5f651bfb5e60156dbff6af3c9d35c7bbcc9015308c0aff1e14cd341
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/amr/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/amr/configure ${COMMON_ARGS}
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(lame
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
URL_HASH SHA256=24346b4158e4af3bd9f2e194bb23eb473c75fb7377011523353196b19b9a23ff
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/lame/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/lame/configure ${COMMON_ARGS} --enable-nasm
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(ogg
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.3.tar.gz
URL_HASH SHA256=c2e8a485110b97550f453226ec644ebac6cb29d1caef2902c007edab4308d985
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/ogg/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/ogg/configure ${COMMON_ARGS}
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(flac
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ogg
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.3.2.tar.xz
URL_HASH SHA256=91cfc3ed61dc40f47f050a109b08610667d73477af6ef36dcad31c31a4a8d53f
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/flac/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/flac/configure ${COMMON_ARGS} --with-ogg --disable-cpplibs --disable-xmms-plugin
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(vorbis
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ogg
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz
URL_HASH SHA256=6ed40e0241089a42c48604dc00e362beee00036af2d8b3f46338031c9e0351cb
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/vorbis/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/vorbis/configure ${COMMON_ARGS} --with-ogg
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(opus
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ogg
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://ftp.osuosl.org/pub/xiph/releases/opus/opus-1.3.1.tar.gz
URL_HASH SHA256=65b58e1e25b2a114157014736a3d9dfeaad8d41be1c8179866f144a2fb44ff9d
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/opus/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/opus/configure ${COMMON_ARGS} --with-ogg
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
ExternalProject_Add(opusfile
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS opus
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://ftp.osuosl.org/pub/xiph/releases/opus/opusfile-0.12.tar.gz
URL_HASH SHA256=118d8601c12dd6a44f52423e68ca9083cc9f2bfe72da7a8c1acb22a80ae3550b
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/opusfile/
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/opusfile/configure ${COMMON_ARGS} --disable-http
DOWNLOAD_NO_PROGRESS ON
LOG_DOWNLOAD ON
LOG_UPDATE ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON
)
# OpenMP is by default compiled against GNU OpenMP, which conflicts with the version of OpenMP that PyTorch uses.
# See https://github.com/pytorch/audio/pull/1026
# TODO: Add flags like https://github.com/suphoff/pytorch_parallel_extension_cpp/blob/master/setup.py
set(SOX_OPTIONS
--disable-openmp
--with-amrnb
--with-amrwb
--with-flac
--with-lame
--with-oggvorbis
--with-opus
--without-alsa
--without-ao
--without-coreaudio
--without-oss
--without-id3tag
--without-ladspa
--without-mad
--without-magic
--without-png
--without-pulseaudio
--without-sndfile
--without-sndio
--without-sunaudio
--without-waveaudio
--without-wavpack
--without-twolame
)
set(SOX_LIBRARIES
${INSTALL_DIR}/lib/libsox.a
${INSTALL_DIR}/lib/libopencore-amrnb.a
${INSTALL_DIR}/lib/libopencore-amrwb.a
${INSTALL_DIR}/lib/libmp3lame.a
${INSTALL_DIR}/lib/libFLAC.a
${INSTALL_DIR}/lib/libopusfile.a
${INSTALL_DIR}/lib/libopus.a
${INSTALL_DIR}/lib/libvorbisenc.a
${INSTALL_DIR}/lib/libvorbisfile.a
${INSTALL_DIR}/lib/libvorbis.a
${INSTALL_DIR}/lib/libogg.a
)
set(sox_depends
ogg flac vorbis opusfile lame amr
)
ExternalProject_Add(sox
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${sox_depends}
DOWNLOAD_DIR ${ARCHIVE_DIR}
URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2 URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2
URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/sox/ PATCH_COMMAND ""
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env ${envs} ${CMAKE_CURRENT_BINARY_DIR}/src/sox/configure ${COMMON_ARGS} ${SOX_OPTIONS} CONFIGURE_COMMAND ""
BUILD_BYPRODUCTS ${SOX_LIBRARIES} BUILD_COMMAND ""
DOWNLOAD_NO_PROGRESS ON )
LOG_DOWNLOAD ON # FetchContent_MakeAvailable will parse the downloaded content and setup the targets.
LOG_UPDATE ON # We want to only download and not build, so we run Populate manually.
LOG_CONFIGURE ON if(NOT sox_src_POPULATED)
LOG_BUILD ON FetchContent_Populate(sox_src)
LOG_INSTALL ON endif()
LOG_MERGED_STDOUTERR ON
LOG_OUTPUT_ON_FAILURE ON add_library(sox SHARED stub.c)
) if(APPLE)
set_target_properties(sox PROPERTIES SUFFIX .dylib)
add_library(libsox INTERFACE) endif(APPLE)
add_dependencies(libsox sox) target_include_directories(sox PUBLIC ${sox_src_SOURCE_DIR}/src)
target_include_directories(libsox INTERFACE ${INSTALL_DIR}/include)
target_link_libraries(libsox INTERFACE ${SOX_LIBRARIES})
#include <sox.h>
int sox_add_effect(
sox_effects_chain_t* chain,
sox_effect_t* effp,
sox_signalinfo_t* in,
sox_signalinfo_t const* out) {
return -1;
}
int sox_close(sox_format_t* ft) {
return -1;
}
sox_effect_t* sox_create_effect(sox_effect_handler_t const* eh) {
return NULL;
}
sox_effects_chain_t* sox_create_effects_chain(
sox_encodinginfo_t const* in_enc,
sox_encodinginfo_t const* out_enc) {
return NULL;
}
void sox_delete_effect(sox_effect_t* effp) {}
void sox_delete_effects_chain(sox_effects_chain_t* ecp) {}
int sox_effect_options(sox_effect_t* effp, int argc, char* const argv[]) {
return -1;
}
const sox_effect_handler_t* sox_find_effect(char const* name) {
return NULL;
}
int sox_flow_effects(
sox_effects_chain_t* chain,
int callback(sox_bool all_done, void* client_data),
void* client_data) {
return -1;
}
const sox_effect_fn_t* sox_get_effect_fns(void) {
return NULL;
}
const sox_format_tab_t* sox_get_format_fns(void) {
return NULL;
}
sox_globals_t* sox_get_globals(void) {
return NULL;
}
sox_format_t* sox_open_read(
char const* path,
sox_signalinfo_t const* signal,
sox_encodinginfo_t const* encoding,
char const* filetype) {
return NULL;
}
sox_format_t* sox_open_write(
char const* path,
sox_signalinfo_t const* signal,
sox_encodinginfo_t const* encoding,
char const* filetype,
sox_oob_t const* oob,
sox_bool overwrite_permitted(char const* filename)) {
return NULL;
}
const char* sox_strerror(int sox_errno) {
return NULL;
}
size_t sox_write(sox_format_t* ft, const sox_sample_t* buf, size_t len) {
return 0;
}
int sox_init() {
return -1;
};
int sox_quit() {
return -1;
};
...@@ -8,7 +8,7 @@ try: ...@@ -8,7 +8,7 @@ try:
from .fb import _init_ffmpeg from .fb import _init_ffmpeg
except ImportError: except ImportError:
from .utils import _init_ffmpeg from .utils import _init_ffmpeg
from .utils import _check_cuda_version, _fail_since_no_ffmpeg, _init_dll_path, _init_sox, _load_lib from .utils import _check_cuda_version, _fail_since_no_ffmpeg, _fail_since_no_sox, _init_dll_path, _init_sox, _load_lib
_LG = logging.getLogger(__name__) _LG = logging.getLogger(__name__)
...@@ -51,17 +51,21 @@ if _IS_TORCHAUDIO_EXT_AVAILABLE: ...@@ -51,17 +51,21 @@ if _IS_TORCHAUDIO_EXT_AVAILABLE:
_IS_ALIGN_AVAILABLE = torchaudio.lib._torchaudio.is_align_available() _IS_ALIGN_AVAILABLE = torchaudio.lib._torchaudio.is_align_available()
# Similar to libtorchaudio, sox-related features should be importable when present. # Initialize libsox-related features
#
# Note: This will be change in the future when sox is dynamically linked.
# At that point, this initialization should handle the case where
# sox integration is built but libsox is not found.
_SOX_INITIALIZED = False _SOX_INITIALIZED = False
_USE_SOX = False if os.name == "nt" else eval_env("TORCHAUDIO_USE_SOX", True) _USE_SOX = False if os.name == "nt" else eval_env("TORCHAUDIO_USE_SOX", True)
_SOX_MODULE_AVAILABLE = is_module_available("torchaudio.lib._torchaudio_sox") _SOX_MODULE_AVAILABLE = is_module_available("torchaudio.lib._torchaudio_sox")
if _USE_SOX and _SOX_MODULE_AVAILABLE: if _USE_SOX and _SOX_MODULE_AVAILABLE:
_init_sox() try:
_SOX_INITIALIZED = True _init_sox()
_SOX_INITIALIZED = True
except Exception:
# The initialization of sox extension will fail if supported sox
# libraries are not found in the system.
# Since the rest of the torchaudio works without it, we do not report the
# error here.
# The error will be raised when user code attempts to use these features.
_LG.debug("Failed to initialize sox extension", exc_info=True)
if os.name == "nt": if os.name == "nt":
...@@ -74,7 +78,7 @@ elif not _SOX_MODULE_AVAILABLE: ...@@ -74,7 +78,7 @@ elif not _SOX_MODULE_AVAILABLE:
"Please build TorchAudio with libsox support. (BUILD_SOX=1)" "Please build TorchAudio with libsox support. (BUILD_SOX=1)"
) )
else: else:
fail_if_no_sox = no_op fail_if_no_sox = no_op if _SOX_INITIALIZED else _fail_since_no_sox
# Initialize FFmpeg-related features # Initialize FFmpeg-related features
......
...@@ -197,6 +197,25 @@ def _check_cuda_version(): ...@@ -197,6 +197,25 @@ def _check_cuda_version():
return version return version
def _fail_since_no_sox(func):
@wraps(func)
def wrapped(*_args, **_kwargs):
try:
# Note:
# We run _init_sox again just to show users the stacktrace.
# _init_sox would not succeed here.
_init_sox()
except Exception as err:
raise RuntimeError(
f"{func.__name__} requires sox extension which is not available. "
"Please refer to the stacktrace above for how to resolve this."
) from err
# This should not happen in normal execution, but just in case.
return func(*_args, **_kwargs)
return wrapped
def _fail_since_no_ffmpeg(func): def _fail_since_no_ffmpeg(func):
@wraps(func) @wraps(func)
def wrapped(*_args, **_kwargs): def wrapped(*_args, **_kwargs):
......
...@@ -10,7 +10,7 @@ torchaudio_library( ...@@ -10,7 +10,7 @@ torchaudio_library(
libtorchaudio_sox libtorchaudio_sox
"${sources}" "${sources}"
"" ""
"torch;libsox" "torch;sox"
"" ""
) )
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#define TORCHAUDIO_SOX_UTILS_H #define TORCHAUDIO_SOX_UTILS_H
#include <sox.h> #include <sox.h>
#include <torch/script.h> #include <torch/types.h>
namespace torchaudio::sox { namespace torchaudio::sox {
......
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