Skip to content

Commit

Permalink
const
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed Jul 25, 2023
1 parent eb00d0f commit 0c31e33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
67 changes: 34 additions & 33 deletions torchaudio/csrc/sox/stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion torchaudio/csrc/sox/stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0c31e33

Please sign in to comment.