Skip to content

Commit

Permalink
Use dlopen to open sox
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed May 29, 2023
1 parent dacb54b commit f46b7c5
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 226 deletions.
20 changes: 1 addition & 19 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,31 +104,13 @@ def _parse_url(path):
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):
for dest, url in src:
if not dest.exists():
print(f" --- Fetching {os.path.basename(dest)}")
torch.hub.download_url_to_file(url, dest, progress=False)


def _fetch_third_party_libraries():
_init_submodule()
if os.name != "nt":
_fetch_archives(_parse_sources())


def _main():
sha = _run_cmd(["git", "rev-parse", "HEAD"])
branch = _run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"])
Expand All @@ -142,7 +124,7 @@ def _main():
print("-- Building version", version)

_make_version_file(version, sha)
_fetch_third_party_libraries()
_init_submodule()

with open("README.md") as f:
long_description = f.read()
Expand Down
91 changes: 12 additions & 79 deletions third_party/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,85 +1,18 @@
find_package(PkgConfig REQUIRED)
include(FetchContent)

include(ExternalProject)

# set(INSTALL_DIR ${CMAKE_BINARY_DIR}/sox)
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(COMPILE_ARGS
--quiet
--enable-shared
--disable-static
--prefix=${INSTALL_DIR}
--with-pic
--disable-dependency-tracking
--disable-debug
--disable-examples
--disable-doc
--disable-openmp
--without-amrnb
--without-amrwb
--without-flac
--without-lame
--without-oggvorbis
--without-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
)

if (APPLE)
set(byproduct ${INSTALL_DIR}/lib/libsox.3.dylib)
set(sox_library ${INSTALL_DIR}/lib/libsox.dylib)
else()
set(byproduct ${INSTALL_DIR}/lib/libsox.so)
set(sox_library ${INSTALL_DIR}/lib/libsox.so)
endif()

ExternalProject_Add(sox
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
DOWNLOAD_DIR ${ARCHIVE_DIR}
FetchContent_Declare(
sox
URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2
URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c
PATCH_COMMAND cp ${patch_dir}/config.guess ${patch_dir}/config.sub ${CMAKE_CURRENT_BINARY_DIR}/src/sox/
CONFIGURE_COMMAND ${CMAKE_CURRENT_BINARY_DIR}/src/sox/configure ${COMPILE_ARGS}
BUILD_BYPRODUCTS ${byproduct}
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
)

if (APPLE)
# Modify RPATH, so that they won't be hardcoded
add_custom_command(
OUTPUT ${sox_library}
COMMAND install_name_tool -change ${byproduct} @rpath/libsox.dylib -id @rpath/libsox.dylib ${byproduct}
DEPENDS sox
)
add_custom_target(_libsox DEPENDS ${sox_library})
else()
add_custom_target(_libsox DEPENDS sox)
PATCH_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)
# FetchContent_MakeAvailable will parse the downloaded content and setup the targets.
# We want to only download and not build, so we run Populate manually.
if(NOT sox_POPULATED)
FetchContent_Populate(sox)
endif()

add_library(libsox INTERFACE)
add_dependencies(libsox _libsox)
target_include_directories(libsox INTERFACE ${INSTALL_DIR}/include)
target_link_libraries(libsox INTERFACE ${sox_library})
target_include_directories(libsox INTERFACE ${sox_SOURCE_DIR}/src)
1 change: 1 addition & 0 deletions torchaudio/csrc/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set(
sources
libsox.cpp
io.cpp
utils.cpp
effects.cpp
Expand Down
36 changes: 12 additions & 24 deletions torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#include <sox.h>
#include <torchaudio/csrc/sox/effects.h>
#include <torchaudio/csrc/sox/effects_chain.h>
#include <torchaudio/csrc/sox/libsox.h>
#include <torchaudio/csrc/sox/utils.h>

using namespace torchaudio::sox_utils;

namespace torchaudio {
namespace sox_effects {

namespace torchaudio::sox {
namespace {

enum SoxEffectsResourceState { NotInitialized, Initialized, ShutDown };
Expand All @@ -22,7 +19,7 @@ void initialize_sox_effects() {
switch (SOX_RESOURCE_STATE) {
case NotInitialized:
TORCH_CHECK(
sox_init() == SOX_SUCCESS, "Failed to initialize sox effects.");
lsx().sox_init() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = Initialized;
break;
case Initialized:
Expand All @@ -41,7 +38,7 @@ void shutdown_sox_effects() {
TORCH_CHECK(false, "SoX Effects is not initialized. Cannot shutdown.");
case Initialized:
TORCH_CHECK(
sox_quit() == SOX_SUCCESS, "Failed to initialize sox effects.");
lsx().sox_quit() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = ShutDown;
break;
case ShutDown:
Expand All @@ -58,7 +55,7 @@ auto apply_effects_tensor(

// Create SoxEffectsChain
const auto dtype = waveform.dtype();
torchaudio::sox_effects_chain::SoxEffectsChain chain(
SoxEffectsChain chain(
/*input_encoding=*/get_tensor_encodinginfo(dtype),
/*output_encoding=*/get_tensor_encodinginfo(dtype));

Expand Down Expand Up @@ -95,7 +92,7 @@ auto apply_effects_file(
const c10::optional<std::string>& format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>> {
// Open input file
SoxFormat sf(sox_open_read(
SoxFormat sf(lsx().sox_open_read(
path.c_str(),
/*signal=*/nullptr,
/*encoding=*/nullptr,
Expand All @@ -113,7 +110,7 @@ auto apply_effects_file(
out_buffer.reserve(sf->signal.length);

// Create and run SoxEffectsChain
torchaudio::sox_effects_chain::SoxEffectsChain chain(
SoxEffectsChain chain(
/*input_encoding=*/sf->encoding,
/*output_encoding=*/get_tensor_encodinginfo(dtype));

Expand All @@ -133,25 +130,16 @@ auto apply_effects_file(
dtype,
normalize.value_or(true),
channels_first_);

return std::tuple<torch::Tensor, int64_t>(
tensor, chain.getOutputSampleRate());
}

TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
m.def(
"torchaudio::sox_effects_initialize_sox_effects",
&torchaudio::sox_effects::initialize_sox_effects);
m.def(
"torchaudio::sox_effects_shutdown_sox_effects",
&torchaudio::sox_effects::shutdown_sox_effects);
m.def(
"torchaudio::sox_effects_apply_effects_tensor",
&torchaudio::sox_effects::apply_effects_tensor);
m.def(
"torchaudio::sox_effects_apply_effects_file",
&torchaudio::sox_effects::apply_effects_file);
&initialize_sox_effects);
m.def("torchaudio::sox_effects_shutdown_sox_effects", &shutdown_sox_effects);
m.def("torchaudio::sox_effects_apply_effects_tensor", &apply_effects_tensor);
m.def("torchaudio::sox_effects_apply_effects_file", &apply_effects_file);
}

} // namespace sox_effects
} // namespace torchaudio
} // namespace torchaudio::sox
6 changes: 2 additions & 4 deletions torchaudio/csrc/sox/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include <torch/script.h>
#include <torchaudio/csrc/sox/utils.h>

namespace torchaudio {
namespace sox_effects {
namespace torchaudio::sox {

void initialize_sox_effects();

Expand All @@ -25,7 +24,6 @@ auto apply_effects_file(
const c10::optional<std::string>& format)
-> c10::optional<std::tuple<torch::Tensor, int64_t>>;

} // namespace sox_effects
} // namespace torchaudio
} // namespace torchaudio::sox

#endif
Loading

0 comments on commit f46b7c5

Please sign in to comment.