From 077fd6fe79ba8e06b3cd18b480f212c2657b6704 Mon Sep 17 00:00:00 2001 From: moto <855818+mthrok@users.noreply.github.com> Date: Sun, 28 May 2023 12:38:48 -0400 Subject: [PATCH] move init to C++ and debug stuff --- torchaudio/_extension/utils.py | 1 - torchaudio/csrc/sox/effects.cpp | 13 ++----- torchaudio/csrc/sox/effects_chain.cpp | 6 +-- torchaudio/csrc/sox/io.cpp | 11 ++---- torchaudio/csrc/sox/libsox.cpp | 54 ++++++++++++++++++--------- torchaudio/csrc/sox/libsox.h | 9 ++--- torchaudio/csrc/sox/pybind/pybind.cpp | 17 ++------- torchaudio/csrc/sox/utils.cpp | 8 ++-- 8 files changed, 58 insertions(+), 61 deletions(-) diff --git a/torchaudio/_extension/utils.py b/torchaudio/_extension/utils.py index d7e02886829..b185331ce02 100644 --- a/torchaudio/_extension/utils.py +++ b/torchaudio/_extension/utils.py @@ -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 diff --git a/torchaudio/csrc/sox/effects.cpp b/torchaudio/csrc/sox/effects.cpp index fd0c1d0b8b0..3c3ce0d54ac 100644 --- a/torchaudio/csrc/sox/effects.cpp +++ b/torchaudio/csrc/sox/effects.cpp @@ -130,7 +130,6 @@ auto apply_effects_file( dtype, normalize.value_or(true), channels_first_); - return std::tuple( tensor, chain.getOutputSampleRate()); } @@ -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 diff --git a/torchaudio/csrc/sox/effects_chain.cpp b/torchaudio/csrc/sox/effects_chain.cpp index c35ff181867..c5d6cc27af3 100644 --- a/torchaudio/csrc/sox/effects_chain.cpp +++ b/torchaudio/csrc/sox/effects_chain.cpp @@ -271,7 +271,7 @@ void SoxEffectsChain::addEffect(const std::vector 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 opts; @@ -279,8 +279,8 @@ void SoxEffectsChain::addEffect(const std::vector effect) { 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( diff --git a/torchaudio/csrc/sox/io.cpp b/torchaudio/csrc/sox/io.cpp index 97169875566..197c3c2bee3 100644 --- a/torchaudio/csrc/sox/io.cpp +++ b/torchaudio/csrc/sox/io.cpp @@ -67,8 +67,7 @@ c10::optional> load_audio_file( c10::optional channels_first, const c10::optional& 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( @@ -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 diff --git a/torchaudio/csrc/sox/libsox.cpp b/torchaudio/csrc/sox/libsox.cpp index c9edb0df594..899ca6b3633 100644 --- a/torchaudio/csrc/sox/libsox.cpp +++ b/torchaudio/csrc/sox/libsox.cpp @@ -1,39 +1,59 @@ #include - -#include +#include namespace torchaudio::sox { - -using Library = std::unique_ptr; namespace { -static Library libsox; + +// Handle to the dlopen-ed libsox +static std::unique_ptr 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("libsox.3.dylib", "libsox.dylib"); + auto lsx_ = + std::make_unique("libsox.3.dylib", "libsox.dylib"); #else - auto lsx_ = std::make_unique("libsox.3.so", "libsox.so"); + auto lsx_ = + std::make_unique("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); @@ -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 diff --git a/torchaudio/csrc/sox/libsox.h b/torchaudio/csrc/sox/libsox.h index fa31d7d48a7..2da01a65edb 100644 --- a/torchaudio/csrc/sox/libsox.h +++ b/torchaudio/csrc/sox/libsox.h @@ -1,7 +1,7 @@ #pragma once -#include #include #include +#include namespace torchaudio::sox { @@ -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; @@ -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); @@ -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 diff --git a/torchaudio/csrc/sox/pybind/pybind.cpp b/torchaudio/csrc/sox/pybind/pybind.cpp index 9abbc98ea49..27b32515355 100644 --- a/torchaudio/csrc/sox/pybind/pybind.cpp +++ b/torchaudio/csrc/sox/pybind/pybind.cpp @@ -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, diff --git a/torchaudio/csrc/sox/utils.cpp b/torchaudio/csrc/sox/utils.cpp index d720b3402b7..51de871d15b 100644 --- a/torchaudio/csrc/sox/utils.cpp +++ b/torchaudio/csrc/sox/utils.cpp @@ -1,7 +1,7 @@ #include #include -#include #include +#include #include namespace torchaudio::sox { @@ -44,7 +44,8 @@ std::vector> list_effects() { std::vector list_write_formats() { std::vector 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) @@ -56,7 +57,8 @@ std::vector list_write_formats() { std::vector list_read_formats() { std::vector 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)