Skip to content

Commit

Permalink
Use dlopen
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed Jul 26, 2023
1 parent a81a009 commit 88c3f02
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 183 deletions.
13 changes: 5 additions & 8 deletions third_party/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include(FetchContent)

FetchContent_Declare(
sox_src
sox
URL https://downloads.sourceforge.net/project/sox/sox/14.4.2/sox-14.4.2.tar.bz2
URL_HASH SHA256=81a6956d4330e75b5827316e44ae381e6f1e8928003c6aa45896da9041ea149c
PATCH_COMMAND ""
Expand All @@ -10,12 +10,9 @@ FetchContent_Declare(
)
# 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_src_POPULATED)
FetchContent_Populate(sox_src)
if(NOT sox_POPULATED)
FetchContent_Populate(sox)
endif()

add_library(sox SHARED stub.c)
if(APPLE)
set_target_properties(sox PROPERTIES SUFFIX .dylib)
endif(APPLE)
target_include_directories(sox PUBLIC ${sox_src_SOURCE_DIR}/src)
add_library(libsox INTERFACE)
target_include_directories(libsox INTERFACE ${sox_SOURCE_DIR}/src)
85 changes: 0 additions & 85 deletions third_party/sox/stub.c

This file was deleted.

1 change: 1 addition & 0 deletions tools/setup_helpers/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def build_extension(self, ext):
"-DCMAKE_VERBOSE_MAKEFILE=ON",
f"-DPython_INCLUDE_DIR={distutils.sysconfig.get_python_inc()}",
f"-DBUILD_SOX:BOOL={'ON' if _BUILD_SOX else 'OFF'}",
"-DDLOPEN_SOX:BOOL=ON",
f"-DBUILD_RIR:BOOL={'ON' if _BUILD_RIR else 'OFF'}",
f"-DBUILD_RNNT:BOOL={'ON' if _BUILD_RNNT else 'OFF'}",
f"-DBUILD_ALIGN:BOOL={'ON' if _BUILD_ALIGN else 'OFF'}",
Expand Down
7 changes: 0 additions & 7 deletions torchaudio/_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ def _init_sox():
_load_lib("libtorchaudio_sox")
import torchaudio.lib._torchaudio_sox # noqa

torchaudio.lib._torchaudio_sox.set_verbosity(0)

import atexit

torch.ops.torchaudio.sox_effects_initialize_sox_effects()
atexit.register(torch.ops.torchaudio.sox_effects_shutdown_sox_effects)


def _try_access_avutil(ffmpeg_ver):
libname_template = {
Expand Down
12 changes: 9 additions & 3 deletions torchaudio/csrc/sox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
set(
sources
stub.cpp
io.cpp
utils.cpp
effects.cpp
effects_chain.cpp
types.cpp
)
set(
compiler_definitions
DLOPEN_SOX
)

torchaudio_library(
libtorchaudio_sox
"${sources}"
""
"torch;sox"
""
"torch;libsox"
"${compiler_definitions}"
)

if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
Expand All @@ -20,6 +26,6 @@ if (BUILD_TORCHAUDIO_PYTHON_EXTENSION)
"pybind/pybind.cpp;"
""
"libtorchaudio_sox"
""
"${compiler_definitions}"
)
endif()
49 changes: 2 additions & 47 deletions torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
#include <sox.h>
#include <torchaudio/csrc/sox/effects.h>
#include <torchaudio/csrc/sox/effects_chain.h>
#include <torchaudio/csrc/sox/stub.h>
#include <torchaudio/csrc/sox/utils.h>

namespace torchaudio::sox {
namespace {

enum SoxEffectsResourceState { NotInitialized, Initialized, ShutDown };
SoxEffectsResourceState SOX_RESOURCE_STATE = NotInitialized;
std::mutex SOX_RESOUCE_STATE_MUTEX;

} // namespace

void initialize_sox_effects() {
const std::lock_guard<std::mutex> lock(SOX_RESOUCE_STATE_MUTEX);

switch (SOX_RESOURCE_STATE) {
case NotInitialized:
TORCH_CHECK(
sox_init() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = Initialized;
break;
case Initialized:
break;
case ShutDown:
TORCH_CHECK(
false, "SoX Effects has been shut down. Cannot initialize again.");
}
};

void shutdown_sox_effects() {
const std::lock_guard<std::mutex> lock(SOX_RESOUCE_STATE_MUTEX);

switch (SOX_RESOURCE_STATE) {
case NotInitialized:
TORCH_CHECK(false, "SoX Effects is not initialized. Cannot shutdown.");
case Initialized:
TORCH_CHECK(
sox_quit() == SOX_SUCCESS, "Failed to initialize sox effects.");
SOX_RESOURCE_STATE = ShutDown;
break;
case ShutDown:
break;
}
}

auto apply_effects_tensor(
torch::Tensor waveform,
Expand Down Expand Up @@ -91,7 +52,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(SOX sox_open_read(
path.c_str(),
/*signal=*/nullptr,
/*encoding=*/nullptr,
Expand Down Expand Up @@ -129,21 +90,15 @@ auto apply_effects_file(
dtype,
normalize.value_or(true),
channels_first_);

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

namespace {

TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
m.def(
"torchaudio::sox_effects_initialize_sox_effects",
&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
} // namespace torchaudio::sox
42 changes: 21 additions & 21 deletions torchaudio/csrc/sox/effects_chain.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <torchaudio/csrc/sox/effects_chain.h>
#include <torchaudio/csrc/sox/stub.h>
#include <torchaudio/csrc/sox/utils.h>
#include "c10/util/Exception.h"

using namespace torch::indexing;

namespace torchaudio::sox {

namespace {

/// helper classes for passing the location of input tensor and output buffer
Expand Down Expand Up @@ -112,12 +112,12 @@ int file_output_flow(
*osamp = 0;
if (*isamp) {
auto sf = static_cast<FileOutputPriv*>(effp->priv)->sf;
if (sox_write(sf, ibuf, *isamp) != *isamp) {
if (SOX sox_write(sf, ibuf, *isamp) != *isamp) {
TORCH_CHECK(
!sf->sox_errno,
sf->sox_errstr,
" ",
sox_strerror(sf->sox_errno),
SOX sox_strerror(sf->sox_errno),
" ",
sf->filename);
return SOX_EOF;
Expand Down Expand Up @@ -197,18 +197,18 @@ SoxEffectsChain::SoxEffectsChain(
in_sig_(),
interm_sig_(),
out_sig_(),
sec_(sox_create_effects_chain(&in_enc_, &out_enc_)) {
sec_(SOX sox_create_effects_chain(&in_enc_, &out_enc_)) {
TORCH_CHECK(sec_, "Failed to create effect chain.");
}

SoxEffectsChain::~SoxEffectsChain() {
if (sec_ != nullptr) {
sox_delete_effects_chain(sec_);
SOX sox_delete_effects_chain(sec_);
}
}

void SoxEffectsChain::run() {
sox_flow_effects(sec_, NULL, NULL);
SOX sox_flow_effects(sec_, NULL, NULL);
}

void SoxEffectsChain::addInputTensor(
Expand All @@ -217,44 +217,44 @@ void SoxEffectsChain::addInputTensor(
bool channels_first) {
in_sig_ = get_signalinfo(waveform, sample_rate, "wav", channels_first);
interm_sig_ = in_sig_;
SoxEffect e(sox_create_effect(get_tensor_input_handler()));
SoxEffect e(SOX sox_create_effect(get_tensor_input_handler()));
auto priv = static_cast<TensorInputPriv*>(e->priv);
priv->index = 0;
priv->waveform = waveform;
priv->sample_rate = sample_rate;
priv->channels_first = channels_first;
TORCH_CHECK(
sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
SOX sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
"Internal Error: Failed to add effect: input_tensor");
}

void SoxEffectsChain::addOutputBuffer(
std::vector<sox_sample_t>* output_buffer) {
SoxEffect e(sox_create_effect(get_tensor_output_handler()));
SoxEffect e(SOX sox_create_effect(get_tensor_output_handler()));
static_cast<TensorOutputPriv*>(e->priv)->buffer = output_buffer;
TORCH_CHECK(
sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
SOX sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
"Internal Error: Failed to add effect: output_tensor");
}

void SoxEffectsChain::addInputFile(sox_format_t* sf) {
in_sig_ = sf->signal;
interm_sig_ = in_sig_;
SoxEffect e(sox_create_effect(sox_find_effect("input")));
SoxEffect e(SOX sox_create_effect(SOX sox_find_effect("input")));
char* opts[] = {(char*)sf};
sox_effect_options(e, 1, opts);
SOX sox_effect_options(e, 1, opts);
TORCH_CHECK(
sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
SOX sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
"Internal Error: Failed to add effect: input ",
sf->filename);
}

void SoxEffectsChain::addOutputFile(sox_format_t* sf) {
out_sig_ = sf->signal;
SoxEffect e(sox_create_effect(get_file_output_handler()));
SoxEffect e(SOX sox_create_effect(get_file_output_handler()));
static_cast<FileOutputPriv*>(e->priv)->sf = sf;
TORCH_CHECK(
sox_add_effect(sec_, e, &interm_sig_, &out_sig_) == SOX_SUCCESS,
SOX sox_add_effect(sec_, e, &interm_sig_, &out_sig_) == SOX_SUCCESS,
"Internal Error: Failed to add effect: output ",
sf->filename);
}
Expand All @@ -266,25 +266,25 @@ void SoxEffectsChain::addEffect(const std::vector<std::string> effect) {
TORCH_CHECK(
UNSUPPORTED_EFFECTS.find(name) == UNSUPPORTED_EFFECTS.end(),
"Unsupported effect: ",
name)
name);

auto returned_effect = sox_find_effect(name.c_str());
auto returned_effect = SOX sox_find_effect(name.c_str());
TORCH_CHECK(returned_effect, "Unsupported effect: ", name)

SoxEffect e(sox_create_effect(returned_effect));
SoxEffect e(SOX sox_create_effect(returned_effect));
const auto num_options = num_args - 1;

std::vector<char*> opts;
for (size_t i = 1; i < num_args; ++i) {
opts.push_back((char*)effect[i].c_str());
}
TORCH_CHECK(
sox_effect_options(e, num_options, num_options ? opts.data() : nullptr) ==
SOX_SUCCESS,
SOX sox_effect_options(
e, num_options, num_options ? opts.data() : nullptr) == SOX_SUCCESS,
"Invalid effect option: ",
c10::Join(" ", effect))
TORCH_CHECK(
sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
SOX sox_add_effect(sec_, e, &interm_sig_, &in_sig_) == SOX_SUCCESS,
"Internal Error: Failed to add effect: \"",
c10::Join(" ", effect),
"\"");
Expand Down
Loading

0 comments on commit 88c3f02

Please sign in to comment.