Skip to content

Commit

Permalink
move init to C++ and debug stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed May 28, 2023
1 parent 87e6a47 commit 077fd6f
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 61 deletions.
1 change: 0 additions & 1 deletion torchaudio/_extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def _init_sox():
_load_lib("libtorchaudio_sox")
import torchaudio.lib._torchaudio_sox # noqa

torchaudio.lib._torchaudio_sox.init_libsox()
torchaudio.lib._torchaudio_sox.set_verbosity(0)

import atexit
Expand Down
13 changes: 3 additions & 10 deletions torchaudio/csrc/sox/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ auto apply_effects_file(
dtype,
normalize.value_or(true),
channels_first_);

return std::tuple<torch::Tensor, int64_t>(
tensor, chain.getOutputSampleRate());
}
Expand All @@ -139,14 +138,8 @@ 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);
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 torchaudio::sox
6 changes: 3 additions & 3 deletions torchaudio/csrc/sox/effects_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,16 @@ void SoxEffectsChain::addEffect(const std::vector<std::string> effect) {
auto returned_effect = lsx().sox_find_effect(name.c_str());
TORCH_CHECK(returned_effect, "Unsupported effect: ", name)

SoxEffect e(lsx().sox_create_effect(returned_effect));
SoxEffect e(lsx().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(
lsx().sox_effect_options(e, num_options, num_options ? opts.data() : nullptr) ==
SOX_SUCCESS,
lsx().sox_effect_options(
e, num_options, num_options ? opts.data() : nullptr) == SOX_SUCCESS,
"Invalid effect option: ",
c10::Join(" ", effect))
TORCH_CHECK(
Expand Down
11 changes: 3 additions & 8 deletions torchaudio/csrc/sox/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ c10::optional<std::tuple<torch::Tensor, int64_t>> load_audio_file(
c10::optional<bool> channels_first,
const c10::optional<std::string>& format) {
auto effects = get_effects(frame_offset, num_frames);
return apply_effects_file(
path, effects, normalize, channels_first, format);
return apply_effects_file(path, effects, normalize, channels_first, format);
}

void save_audio_file(
Expand Down Expand Up @@ -132,12 +131,8 @@ void save_audio_file(

TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
m.def("torchaudio::sox_io_get_info", &get_info_file);
m.def(
"torchaudio::sox_io_load_audio_file",
&load_audio_file);
m.def(
"torchaudio::sox_io_save_audio_file",
&save_audio_file);
m.def("torchaudio::sox_io_load_audio_file", &load_audio_file);
m.def("torchaudio::sox_io_save_audio_file", &save_audio_file);
}

} // namespace torchaudio::sox
54 changes: 36 additions & 18 deletions torchaudio/csrc/sox/libsox.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
#include <torchaudio/csrc/sox/libsox.h>

#include <iostream>
#include <c10/util/CallOnce.h>

namespace torchaudio::sox {

using Library = std::unique_ptr<at::DynamicLibrary>;
namespace {
static Library libsox;

// Handle to the dlopen-ed libsox
static std::unique_ptr<at::DynamicLibrary> libsox;
// LSX class which torchaudio will be using
static LSX _lsx;

// dlopen libsox and populate mehotds on _lsx.
void _init_lsx();

} // namespace
void init_libsox() {

// Fetch lsx
LSX& lsx() {
static c10::once_flag init_flag;
c10::call_once(init_flag, _init_lsx);
return _lsx;
}

namespace {

// dlopen libsox and populate mehotds on _lsx.
void _init_lsx() {
libsox = []() {
#if defined(_WIN32)
#error Windows is not supported.
#elif defined(__APPLE__)
auto lsx_ = std::make_unique<at::DynamicLibrary>("libsox.3.dylib", "libsox.dylib");
auto lsx_ =
std::make_unique<at::DynamicLibrary>("libsox.3.dylib", "libsox.dylib");
#else
auto lsx_ = std::make_unique<at::DynamicLibrary>("libsox.3.so", "libsox.so");
auto lsx_ =
std::make_unique<at::DynamicLibrary>("libsox.3.so", "libsox.so");
#endif

// check version: we only support 14.4.2
auto fn = (const char* (*)(void))lsx_->sym("sox_version");
std::cerr << fn() << std::endl;
TORCH_CHECK(strcmp(fn(), "14.4.2") == 0, "libsox library was found but not 14.4.2.");
TORCH_CHECK(
strcmp(fn(), "14.4.2") == 0,
"Need libsox 14.4.2, but found", fn());
return lsx_;
}();

#define set_func(NAME) \
_lsx.NAME = (decltype(LSX::NAME))libsox->sym(#NAME)
#define set_func(NAME) _lsx.NAME = (decltype(LSX::NAME))libsox->sym(#NAME)

// Note
// If any of the following fails, it will leave _lsx in invalid state.
// But _lsx cannot be accessed without this fuction succeful, so it'okay.
set_func(sox_add_effect);
set_func(sox_close);
set_func(sox_create_effect);
set_func(sox_create_effects_chain);
set_func(sox_delete_effect);
set_func(sox_delete_effects_chain);
set_func(sox_effect_options);
set_func(sox_find_effect);
set_func(sox_flow_effects);
Expand All @@ -46,10 +66,8 @@ void init_libsox() {
set_func(sox_quit);
set_func(sox_strerror);
set_func(sox_write);

#undef set_func
#undef set_func
}

LSX& lsx() { return _lsx; }

} // namespace torchaudio::sox
} // namespace
} // namespace torchaudio::sox
9 changes: 4 additions & 5 deletions torchaudio/csrc/sox/libsox.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <sox.h>
#include <ATen/DynamicLibrary.h>
#include <memory.h>
#include <sox.h>

namespace torchaudio::sox {

Expand All @@ -11,7 +11,7 @@ struct LSX {
sox_effect_t* effp,
sox_signalinfo_t* in,
sox_signalinfo_t const* out) = nullptr;
int (*sox_close) (sox_format_t* ft) = nullptr;
int (*sox_close)(sox_format_t* ft) = nullptr;

sox_effect_t* (*sox_create_effect)(sox_effect_handler_t const* eh) = nullptr;

Expand All @@ -20,8 +20,8 @@ struct LSX {
sox_encodinginfo_t const* out_enc) = nullptr;

void (*sox_delete_effect)(sox_effect_t* effp);
void (*sox_delete_effects_chain)(sox_effects_chain_t *ecp);
void (*sox_delete_effects_chain)(sox_effects_chain_t* ecp);

int (*sox_effect_options)(sox_effect_t* effp, int argc, char* const argv[]);

const sox_effect_handler_t* (*sox_find_effect)(char const* name);
Expand Down Expand Up @@ -59,7 +59,6 @@ struct LSX {
size_t (*sox_write)(sox_format_t* ft, const sox_sample_t* buf, size_t len);
};

void init_libsox();
LSX& lsx();

} // namespace torchaudio::sox
17 changes: 4 additions & 13 deletions torchaudio/csrc/sox/pybind/pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ namespace sox {
namespace {

PYBIND11_MODULE(_torchaudio_sox, m) {
m.def("init_libsox", &torchaudio::sox::init_libsox, "Initialize libsox.");
m.def("set_seed", &torchaudio::sox::set_seed, "Set random seed.");
m.def("set_verbosity", &torchaudio::sox::set_verbosity, "Set verbosity.");
m.def("set_use_threads", &torchaudio::sox::set_use_threads, "Set threading.");
m.def(
"set_verbosity", &torchaudio::sox::set_verbosity, "Set verbosity.");
"set_buffer_size", &torchaudio::sox::set_buffer_size, "Set buffer size.");
m.def(
"set_use_threads",
&torchaudio::sox::set_use_threads,
"Set threading.");
m.def(
"set_buffer_size",
&torchaudio::sox::set_buffer_size,
"Set buffer size.");
m.def(
"get_buffer_size",
&torchaudio::sox::get_buffer_size,
"Get buffer size.");
"get_buffer_size", &torchaudio::sox::get_buffer_size, "Get buffer size.");
m.def(
"list_effects",
&torchaudio::sox::list_effects,
Expand Down
8 changes: 5 additions & 3 deletions torchaudio/csrc/sox/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <c10/core/ScalarType.h>
#include <sox.h>
#include <torchaudio/csrc/sox/types.h>
#include <torchaudio/csrc/sox/libsox.h>
#include <torchaudio/csrc/sox/types.h>
#include <torchaudio/csrc/sox/utils.h>

namespace torchaudio::sox {
Expand Down Expand Up @@ -44,7 +44,8 @@ std::vector<std::vector<std::string>> list_effects() {

std::vector<std::string> list_write_formats() {
std::vector<std::string> formats;
for (const sox_format_tab_t* fns = lsx().sox_get_format_fns(); fns->fn; ++fns) {
for (const sox_format_tab_t* fns = lsx().sox_get_format_fns(); fns->fn;
++fns) {
const sox_format_handler_t* handler = fns->fn();
for (const char* const* names = handler->names; *names; ++names) {
if (!strchr(*names, '/') && handler->write)
Expand All @@ -56,7 +57,8 @@ std::vector<std::string> list_write_formats() {

std::vector<std::string> list_read_formats() {
std::vector<std::string> formats;
for (const sox_format_tab_t* fns = lsx().sox_get_format_fns(); fns->fn; ++fns) {
for (const sox_format_tab_t* fns = lsx().sox_get_format_fns(); fns->fn;
++fns) {
const sox_format_handler_t* handler = fns->fn();
for (const char* const* names = handler->names; *names; ++names) {
if (!strchr(*names, '/') && handler->read)
Expand Down

0 comments on commit 077fd6f

Please sign in to comment.