Skip to content

Commit 75c8b51

Browse files
committed
Add sox_utils module
1 parent c3f8894 commit 75c8b51

File tree

12 files changed

+208
-23
lines changed

12 files changed

+208
-23
lines changed

test/sox_effect/test_sox_effect.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121

2222
@skipIfNoExtension
2323
class TestSoxEffects(PytorchTestCase):
24-
def test_list_effects(self):
25-
"""effect_names returns the list of available effects"""
26-
effects = sox_effects.effect_names()
27-
# We cannot infer what effects are available, so only check some of them.
28-
assert 'highpass' in effects
29-
assert 'phaser' in effects
30-
assert 'gain' in effects
31-
3224
def test_init(self):
3325
"""Calling init_sox_effects multiple times does not crush"""
3426
for _ in range(3):

test/utils/__init__.py

Whitespace-only changes.

test/utils/test_sox_utils.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from torchaudio.utils import sox_utils
2+
3+
from ..common_utils import (
4+
PytorchTestCase,
5+
skipIfNoExtension,
6+
)
7+
8+
9+
@skipIfNoExtension
10+
class TestSoxUtils(PytorchTestCase):
11+
"""Smoke tests for sox_util module"""
12+
def test_set_seed(self):
13+
"""`set_seed` does not crush"""
14+
sox_utils.set_seed(0)
15+
16+
def test_set_verbosity(self):
17+
"""`set_verbosity` does not crush"""
18+
for val in range(6, 0, -1):
19+
sox_utils.set_verbosity(val)
20+
21+
def test_set_buffer_size(self):
22+
"""`set_buffer_size` does not crush"""
23+
sox_utils.set_buffer_size(131072)
24+
# back to default
25+
sox_utils.set_buffer_size(8192)
26+
27+
def test_set_use_threads(self):
28+
"""`set_use_threads` does not crush"""
29+
sox_utils.set_use_threads(True)
30+
# back to default
31+
sox_utils.set_use_threads(False)
32+
33+
def test_list_effects(self):
34+
"""`list_effects` returns the list of available effects"""
35+
effects = sox_utils.list_effects()
36+
# We cannot infer what effects are available, so only check some of them.
37+
assert 'highpass' in effects
38+
assert 'phaser' in effects
39+
assert 'gain' in effects
40+
41+
def test_list_formats(self):
42+
"""`list_formats` returns the list of supported formats"""
43+
formats = sox_utils.list_formats()
44+
assert 'wav' in formats

torchaudio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
compliance,
55
datasets,
66
kaldi_io,
7+
utils,
78
sox_effects,
89
transforms
910
)

torchaudio/csrc/register.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ static auto registerTensorSignal =
1818
.def("get_sample_rate", &sox_utils::TensorSignal::getSampleRate)
1919
.def("get_channels_first", &sox_utils::TensorSignal::getChannelsFirst);
2020

21+
static auto registerSetSoxOptions =
22+
torch::RegisterOperators()
23+
.op("torchaudio::sox_utils_set_seed", &sox_utils::set_seed)
24+
.op("torchaudio::sox_utils_set_verbosity", &sox_utils::set_verbosity)
25+
.op("torchaudio::sox_utils_set_use_threads",
26+
&sox_utils::set_use_threads)
27+
.op("torchaudio::sox_utils_set_buffer_size",
28+
&sox_utils::set_buffer_size)
29+
.op("torchaudio::sox_utils_list_effects", &sox_utils::list_effects)
30+
.op("torchaudio::sox_utils_list_formats", &sox_utils::list_formats);
31+
2132
////////////////////////////////////////////////////////////////////////////////
2233
// sox_io.h
2334
////////////////////////////////////////////////////////////////////////////////
@@ -58,7 +69,6 @@ static auto registerSoxEffects =
5869
&sox_effects::initialize_sox_effects)
5970
.op("torchaudio::sox_effects_shutdown_sox_effects",
6071
&sox_effects::shutdown_sox_effects)
61-
.op("torchaudio::sox_effects_list_effects", &sox_effects::list_effects)
6272
.op(torch::RegisterOperators::options()
6373
.schema(
6474
"torchaudio::sox_effects_apply_effects_tensor(__torch__.torch.classes.torchaudio.TensorSignal input_signal, str[][] effects) -> __torch__.torch.classes.torchaudio.TensorSignal output_signal")

torchaudio/csrc/sox_effects.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ void shutdown_sox_effects() {
5050
}
5151
}
5252

53-
std::vector<std::string> list_effects() {
54-
std::vector<std::string> names;
55-
const sox_effect_fn_t* fns = sox_get_effect_fns();
56-
for (int i = 0; fns[i]; ++i) {
57-
const sox_effect_handler_t* handler = fns[i]();
58-
if (handler && handler->name)
59-
names.push_back(handler->name);
60-
}
61-
return names;
62-
}
63-
6453
c10::intrusive_ptr<TensorSignal> apply_effects_tensor(
6554
const c10::intrusive_ptr<TensorSignal>& input_signal,
6655
std::vector<std::vector<std::string>> effects) {

torchaudio/csrc/sox_effects.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ void initialize_sox_effects();
1111

1212
void shutdown_sox_effects();
1313

14-
std::vector<std::string> list_effects();
15-
1614
c10::intrusive_ptr<torchaudio::sox_utils::TensorSignal> apply_effects_tensor(
1715
const c10::intrusive_ptr<torchaudio::sox_utils::TensorSignal>& input_signal,
1816
std::vector<std::vector<std::string>> effects);

torchaudio/csrc/sox_utils.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,49 @@
55
namespace torchaudio {
66
namespace sox_utils {
77

8+
void set_seed(const int64_t seed) {
9+
sox_get_globals()->ranqd1 = static_cast<sox_int32_t>(seed);
10+
}
11+
12+
void set_verbosity(const int64_t verbosity) {
13+
sox_get_globals()->verbosity = static_cast<unsigned>(verbosity);
14+
}
15+
16+
void set_use_threads(const bool use_threads) {
17+
sox_get_globals()->use_threads = static_cast<sox_bool>(use_threads);
18+
}
19+
20+
void set_buffer_size(const int64_t buffer_size) {
21+
sox_get_globals()->bufsiz = static_cast<size_t>(buffer_size);
22+
}
23+
24+
std::vector<std::vector<std::string>> list_effects() {
25+
std::vector<std::vector<std::string>> effects;
26+
for (const sox_effect_fn_t* fns = sox_get_effect_fns(); *fns; ++fns) {
27+
const sox_effect_handler_t* handler = (*fns)();
28+
if (handler && handler->name) {
29+
if (UNSUPPORTED_EFFECTS.find(handler->name) ==
30+
UNSUPPORTED_EFFECTS.end()) {
31+
effects.emplace_back(std::vector<std::string>{
32+
handler->name,
33+
handler->usage ? std::string(handler->usage) : std::string("")});
34+
}
35+
}
36+
}
37+
return effects;
38+
}
39+
40+
std::vector<std::string> list_formats() {
41+
std::vector<std::string> formats;
42+
for (const sox_format_tab_t* fns = sox_get_format_fns(); fns->fn; ++fns) {
43+
for (const char* const* names = fns->fn()->names; *names; ++names) {
44+
if (!strchr(*names, '/'))
45+
formats.emplace_back(*names);
46+
}
47+
}
48+
return formats;
49+
}
50+
851
TensorSignal::TensorSignal(
952
torch::Tensor tensor_,
1053
int64_t sample_rate_,

torchaudio/csrc/sox_utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ namespace sox_utils {
1111
// APIs for Python interaction
1212
////////////////////////////////////////////////////////////////////////////////
1313

14+
/// Set sox global options
15+
void set_seed(const int64_t seed);
16+
17+
void set_verbosity(const int64_t verbosity);
18+
19+
void set_use_threads(const bool use_threads);
20+
21+
void set_buffer_size(const int64_t buffer_size);
22+
23+
std::vector<std::vector<std::string>> list_effects();
24+
25+
std::vector<std::string> list_formats();
26+
1427
/// Class for exchanging signal infomation (tensor + meta data) between
1528
/// C++ and Python for read/write operation.
1629
struct TensorSignal : torch::CustomClassHolder {

torchaudio/sox_effects/sox_effects.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
module_utils as _mod_utils,
88
misc_ops as _misc_ops,
99
)
10+
from torchaudio.utils.sox_utils import list_effects
11+
1012

1113
if _mod_utils.is_module_available('torchaudio._torchaudio'):
1214
from torchaudio import _torchaudio
@@ -52,7 +54,7 @@ def effect_names() -> List[str]:
5254
Example
5355
>>> EFFECT_NAMES = torchaudio.sox_effects.effect_names()
5456
"""
55-
return torch.ops.torchaudio.sox_effects_list_effects()
57+
return list(list_effects().keys())
5658

5759

5860
@_mod_utils.requires_module('torchaudio._torchaudio')

0 commit comments

Comments
 (0)