diff --git a/torchaudio/csrc/sox/stub.cpp b/torchaudio/csrc/sox/stub.cpp index c83b6d3c440..08fb12a16cc 100644 --- a/torchaudio/csrc/sox/stub.cpp +++ b/torchaudio/csrc/sox/stub.cpp @@ -6,43 +6,44 @@ namespace torchaudio::sox::detail { namespace { +LibSoxStub get_stub(at::DynamicLibrary& handle) { + // Validate version: we only support 14.4.2 + auto fn = (const sox_version_info_t* (*)())handle.sym("sox_version_info"); + if (SOX_LIB_VERSION_CODE != fn()->version_code) { + auto ver = ((const char* (*)())handle.sym("sox_version"))(); + TORCH_CHECK(false, "Need libsox 14.4.2, but found ", ver); + } + +#define get_symbol(X) (decltype(LibSoxStub::X))handle.sym(#X) + return LibSoxStub { + get_symbol(sox_add_effect), + get_symbol(sox_close), + get_symbol(sox_create_effect), + get_symbol(sox_create_effects_chain), + get_symbol(sox_delete_effect), + get_symbol(sox_delete_effects_chain), + get_symbol(sox_effect_options), + get_symbol(sox_find_effect), + get_symbol(sox_flow_effects), + get_symbol(sox_get_effect_fns), + get_symbol(sox_get_format_fns), + get_symbol(sox_get_globals), + get_symbol(sox_open_read), + get_symbol(sox_open_write), + get_symbol(sox_strerror), + get_symbol(sox_write) + }; +#undef get_symbol +} + // Handle to the dlopen-ed libsox class StubImpl { at::DynamicLibrary handle; public: - LibSoxStub stub{}; - - StubImpl(const char* name) : handle(name) { - // check version: we only support 14.4.2 - { - auto f = (const sox_version_info_t* (*)())handle.sym("sox_version_info"); - if (SOX_LIB_VERSION_CODE != f()->version_code) { - auto ver = ((const char* (*)())handle.sym("sox_version"))(); - TORCH_CHECK(false, "Need libsox 14.4.2, but found ", ver); - } - } - - // Register fanction pointers on public-facing interface -#define set(X) stub.X = (decltype(LibSoxStub::X))handle.sym(#X) - set(sox_add_effect); - set(sox_close); - set(sox_create_effect); - set(sox_create_effects_chain); - set(sox_delete_effect); - set(sox_delete_effects_chain); - set(sox_effect_options); - set(sox_find_effect); - set(sox_flow_effects); - set(sox_get_effect_fns); - set(sox_get_format_fns); - set(sox_get_globals); - set(sox_open_read); - set(sox_open_write); - set(sox_strerror); - set(sox_write); -#undef set + const LibSoxStub stub; + StubImpl(const char* name) : handle(name), stub(get_stub(handle)) { // Global config auto config = stub.sox_get_globals(); config->verbosity = 0; @@ -69,8 +70,8 @@ class StubImpl { #else #define EXT "so" #endif -LibSoxStub& libsox_stub() { - static StubImpl s{"libsox." EXT}; +const LibSoxStub& libsox_stub() { + static const StubImpl s{"libsox." EXT}; return s.stub; } #undef EXT diff --git a/torchaudio/csrc/sox/stub.h b/torchaudio/csrc/sox/stub.h index 9e1517d1348..d2b3f70e64a 100644 --- a/torchaudio/csrc/sox/stub.h +++ b/torchaudio/csrc/sox/stub.h @@ -60,7 +60,7 @@ struct LibSoxStub { size_t (*sox_write)(sox_format_t* ft, const sox_sample_t* buf, size_t len); }; -LibSoxStub& libsox_stub(); +const LibSoxStub& libsox_stub(); } // namespace torchaudio::sox