Skip to content

Commit 4b583ea

Browse files
authored
Add sox_io_backend (#726)
1 parent 894959a commit 4b583ea

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

test/sox_io_backend/test_torchscript.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Optional
33

44
import torch
5-
from torchaudio.backend import sox_io_backend
5+
import torchaudio
66
from parameterized import parameterized
77

88
from ..common_utils import (
@@ -21,11 +21,11 @@
2121

2222

2323
def py_info_func(filepath: str) -> torch.classes.torchaudio.SignalInfo:
24-
return sox_io_backend.info(filepath)
24+
return torchaudio.info(filepath)
2525

2626

2727
def py_load_func(filepath: str, normalize: bool, channels_first: bool):
28-
return sox_io_backend.load(
28+
return torchaudio.load(
2929
filepath, normalize=normalize, channels_first=channels_first)
3030

3131

@@ -36,13 +36,15 @@ def py_save_func(
3636
channels_first: bool = True,
3737
compression: Optional[float] = None,
3838
):
39-
sox_io_backend.save(filepath, tensor, sample_rate, channels_first, compression)
39+
torchaudio.save(filepath, tensor, sample_rate, channels_first, compression)
4040

4141

4242
@skipIfNoExec('sox')
4343
@skipIfNoExtension
4444
class SoxIO(TempDirMixin, TorchaudioTestCase):
4545
"""TorchScript-ability Test suite for `sox_io_backend`"""
46+
backend = 'sox_io'
47+
4648
@parameterized.expand(list(itertools.product(
4749
['float32', 'int32', 'int16', 'uint8'],
4850
[8000, 16000],

test/test_backend.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
31
import torchaudio
42

53
from . import common_utils
@@ -33,6 +31,12 @@ class TestBackendSwitch_SoX(BackendSwitchMixin, common_utils.TorchaudioTestCase)
3331
backend_module = torchaudio.backend.sox_backend
3432

3533

34+
@common_utils.skipIfNoExtension
35+
class TestBackendSwitch_SoXIO(BackendSwitchMixin, common_utils.TorchaudioTestCase):
36+
backend = 'sox_io'
37+
backend_module = torchaudio.backend.sox_io_backend
38+
39+
3640
@common_utils.skipIfNoModule('soundfile')
3741
class TestBackendSwitch_soundfile(BackendSwitchMixin, common_utils.TorchaudioTestCase):
3842
backend = 'soundfile'

test/test_io.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@ class Test_LoadSave(unittest.TestCase):
3030

3131
def test_1_save(self):
3232
for backend in BACKENDS_MP3:
33+
if backend == 'sox_io':
34+
continue
3335
with self.subTest():
3436
torchaudio.set_audio_backend(backend)
3537
self._test_1_save(self.test_filepath, False)
3638

3739
for backend in BACKENDS:
40+
if backend == 'sox_io':
41+
continue
3842
with self.subTest():
3943
torchaudio.set_audio_backend(backend)
4044
self._test_1_save(self.test_filepath_wav, True)
@@ -81,6 +85,8 @@ def _test_1_save(self, test_filepath, normalization):
8185

8286
def test_1_save_sine(self):
8387
for backend in BACKENDS:
88+
if backend == 'sox_io':
89+
continue
8490
with self.subTest():
8591
torchaudio.set_audio_backend(backend)
8692
self._test_1_save_sine()
@@ -114,11 +120,15 @@ def _test_1_save_sine(self):
114120

115121
def test_2_load(self):
116122
for backend in BACKENDS_MP3:
123+
if backend == 'sox_io':
124+
continue
117125
with self.subTest():
118126
torchaudio.set_audio_backend(backend)
119127
self._test_2_load(self.test_filepath, 278756)
120128

121129
for backend in BACKENDS:
130+
if backend == 'sox_io':
131+
continue
122132
with self.subTest():
123133
torchaudio.set_audio_backend(backend)
124134
self._test_2_load(self.test_filepath_wav, 276858)
@@ -155,6 +165,8 @@ def _test_2_load(self, test_filepath, length):
155165

156166
def test_2_load_nonormalization(self):
157167
for backend in BACKENDS_MP3:
168+
if backend == 'sox_io':
169+
continue
158170
with self.subTest():
159171
torchaudio.set_audio_backend(backend)
160172
self._test_2_load_nonormalization(self.test_filepath, 278756)
@@ -172,6 +184,8 @@ def _test_2_load_nonormalization(self, test_filepath, length):
172184

173185
def test_3_load_and_save_is_identity(self):
174186
for backend in BACKENDS:
187+
if backend == 'sox_io':
188+
continue
175189
with self.subTest():
176190
torchaudio.set_audio_backend(backend)
177191
self._test_3_load_and_save_is_identity()
@@ -210,6 +224,8 @@ def _test_3_load_and_save_is_identity_across_backend(self, backend1, backend2):
210224

211225
def test_4_load_partial(self):
212226
for backend in BACKENDS_MP3:
227+
if backend == 'sox_io':
228+
continue
213229
with self.subTest():
214230
torchaudio.set_audio_backend(backend)
215231
self._test_4_load_partial()
@@ -252,6 +268,8 @@ def _test_4_load_partial(self):
252268

253269
def test_5_get_info(self):
254270
for backend in BACKENDS:
271+
if backend == 'sox_io':
272+
continue
255273
with self.subTest():
256274
torchaudio.set_audio_backend(backend)
257275
self._test_5_get_info()

torchaudio/backend/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import (
88
no_backend,
99
sox_backend,
10+
sox_io_backend,
1011
soundfile_backend,
1112
)
1213

@@ -24,6 +25,7 @@ def list_audio_backends() -> List[str]:
2425
backends.append('soundfile')
2526
if is_module_available('torchaudio._torchaudio'):
2627
backends.append('sox')
28+
backends.append('sox_io')
2729
return backends
2830

2931

@@ -43,6 +45,8 @@ def set_audio_backend(backend: Optional[str]) -> None:
4345
module = no_backend
4446
elif backend == 'sox':
4547
module = sox_backend
48+
elif backend == 'sox_io':
49+
module = sox_io_backend
4650
elif backend == 'soundfile':
4751
module = soundfile_backend
4852
else:
@@ -69,6 +73,8 @@ def get_audio_backend() -> Optional[str]:
6973
return None
7074
if torchaudio.load == sox_backend.load:
7175
return 'sox'
76+
if torchaudio.load == sox_io_backend.load:
77+
return 'sox_io'
7278
if torchaudio.load == soundfile_backend.load:
7379
return 'soundfile'
7480
raise ValueError('Unknown backend.')

0 commit comments

Comments
 (0)