diff --git a/test/assets/whitenoise.wav b/test/assets/whitenoise.wav new file mode 100644 index 0000000000..b95ce95776 Binary files /dev/null and b/test/assets/whitenoise.wav differ diff --git a/test/common_utils.py b/test/common_utils.py index 00985f73e6..8c14fe25b6 100644 --- a/test/common_utils.py +++ b/test/common_utils.py @@ -1,10 +1,13 @@ import os -from shutil import copytree import tempfile +from contextlib import contextmanager +from shutil import copytree import torch +import torchaudio TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__)) +BACKENDS = torchaudio._backend._audio_backends def create_temp_assets_dir(): @@ -48,3 +51,35 @@ def random_int_tensor(seed, size, low=0, high=2 ** 32, a=22695477, c=1, m=2 ** 3 """ Same as random_float_tensor but integers between [low, high) """ return torch.floor(random_float_tensor(seed, size, a, c, m) * (high - low)) + low + + +@contextmanager +def AudioBackendScope(new_backend): + previous_backend = torchaudio.get_audio_backend() + try: + torchaudio.set_audio_backend(new_backend) + yield + finally: + torchaudio.set_audio_backend(previous_backend) + + +def filter_backends_with_mp3(backends): + # Filter out backends that do not support mp3 + + test_dirpath, _ = create_temp_assets_dir() + test_filepath = os.path.join( + test_dirpath, "assets", "steam-train-whistle-daniel_simon.mp3" + ) + + def supports_mp3(backend): + try: + with AudioBackendScope(backend): + torchaudio.load(test_filepath) + return True + except RuntimeError: + return False + + return [backend for backend in backends if supports_mp3(backend)] + + +BACKENDS_MP3 = filter_backends_with_mp3(BACKENDS) diff --git a/test/test.py b/test/test.py index 81338a1154..80968e15ad 100644 --- a/test/test.py +++ b/test/test.py @@ -1,39 +1,25 @@ import unittest -import common_utils import torch import torchaudio import math import os - - -class AudioBackendScope: - def __init__(self, backend): - self.new_backend = backend - self.previous_backend = torchaudio.get_audio_backend() - - def __enter__(self): - torchaudio.set_audio_backend(self.new_backend) - return self.new_backend - - def __exit__(self, type, value, traceback): - backend = self.previous_backend - torchaudio.set_audio_backend(backend) +from common_utils import AudioBackendScope, BACKENDS, BACKENDS_MP3, create_temp_assets_dir class Test_LoadSave(unittest.TestCase): - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() test_filepath = os.path.join(test_dirpath, "assets", "steam-train-whistle-daniel_simon.mp3") test_filepath_wav = os.path.join(test_dirpath, "assets", "steam-train-whistle-daniel_simon.wav") def test_1_save(self): - for backend in ["sox"]: + for backend in BACKENDS_MP3: with self.subTest(): with AudioBackendScope(backend): self._test_1_save(self.test_filepath, False) - for backend in ["sox", "soundfile"]: + for backend in BACKENDS: with self.subTest(): with AudioBackendScope(backend): self._test_1_save(self.test_filepath_wav, True) @@ -79,7 +65,7 @@ def _test_1_save(self, test_filepath, normalization): torchaudio.save(new_filepath, x, sr) def test_1_save_sine(self): - for backend in ["sox", "soundfile"]: + for backend in BACKENDS: with self.subTest(): with AudioBackendScope(backend): self._test_1_save_sine() @@ -112,12 +98,12 @@ def _test_1_save_sine(self): os.unlink(new_filepath) def test_2_load(self): - for backend in ["sox"]: + for backend in BACKENDS_MP3: with self.subTest(): with AudioBackendScope(backend): self._test_2_load(self.test_filepath, 278756) - for backend in ["sox", "soundfile"]: + for backend in BACKENDS: with self.subTest(): with AudioBackendScope(backend): self._test_2_load(self.test_filepath_wav, 276858) @@ -153,7 +139,7 @@ def _test_2_load(self, test_filepath, length): torchaudio.load(tdir) def test_2_load_nonormalization(self): - for backend in ["sox"]: + for backend in BACKENDS_MP3: with self.subTest(): with AudioBackendScope(backend): self._test_2_load_nonormalization(self.test_filepath, 278756) @@ -170,7 +156,7 @@ def _test_2_load_nonormalization(self, test_filepath, length): self.assertTrue(isinstance(x, torch.LongTensor)) def test_3_load_and_save_is_identity(self): - for backend in ["sox", "soundfile"]: + for backend in BACKENDS: with self.subTest(): with AudioBackendScope(backend): self._test_3_load_and_save_is_identity() @@ -185,6 +171,7 @@ def _test_3_load_and_save_is_identity(self): self.assertEqual(sample_rate, sample_rate2) os.unlink(output_path) + @unittest.skipIf(set(["sox", "soundfile"]) not in set(BACKENDS), "sox and soundfile are not available") def test_3_load_and_save_is_identity_across_backend(self): with self.subTest(): self._test_3_load_and_save_is_identity_across_backend("sox", "soundfile") @@ -208,7 +195,7 @@ def _test_3_load_and_save_is_identity_across_backend(self, backend1, backend2): os.unlink(output_path) def test_4_load_partial(self): - for backend in ["sox"]: + for backend in BACKENDS_MP3: with self.subTest(): with AudioBackendScope(backend): self._test_4_load_partial() @@ -250,7 +237,7 @@ def _test_4_load_partial(self): torchaudio.load(input_sine_path, offset=100000) def test_5_get_info(self): - for backend in ["sox", "soundfile"]: + for backend in BACKENDS: with self.subTest(): with AudioBackendScope(backend): self._test_5_get_info() diff --git a/test/test_compliance_kaldi.py b/test/test_compliance_kaldi.py index 3ba79b92f2..83751e3e0e 100644 --- a/test/test_compliance_kaldi.py +++ b/test/test_compliance_kaldi.py @@ -1,11 +1,11 @@ import math import os -import common_utils import compliance.utils import torch import torchaudio import torchaudio.compliance.kaldi as kaldi import unittest +from common_utils import AudioBackendScope, BACKENDS, create_temp_assets_dir def extract_window(window, wave, f, frame_length, frame_shift, snip_edges): @@ -45,7 +45,7 @@ def first_sample_of_frame(frame, window_size, window_shift, snip_edges): class Test_Kaldi(unittest.TestCase): - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() test_filepath = os.path.join(test_dirpath, 'assets', 'kaldi_file.wav') test_8000_filepath = os.path.join(test_dirpath, 'assets', 'kaldi_file_8000.wav') kaldi_output_dir = os.path.join(test_dirpath, 'assets', 'kaldi') @@ -159,6 +159,8 @@ def _compliance_test_helper(self, sound_filepath, filepath_key, expected_num_fil self.assertTrue(output.shape, kaldi_output.shape) self.assertTrue(torch.allclose(output, kaldi_output, atol=atol, rtol=rtol)) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_spectrogram(self): def get_output_fn(sound, args): output = kaldi.spectrogram( @@ -179,6 +181,8 @@ def get_output_fn(sound, args): self._compliance_test_helper(self.test_filepath, 'spec', 131, 13, get_output_fn, atol=1e-3, rtol=0) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_fbank(self): def get_output_fn(sound, args): output = kaldi.fbank( @@ -209,6 +213,8 @@ def get_output_fn(sound, args): self._compliance_test_helper(self.test_filepath, 'fbank', 97, 22, get_output_fn, atol=1e-3, rtol=1e-1) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_mfcc(self): def get_output_fn(sound, args): output = kaldi.mfcc( @@ -243,6 +249,8 @@ def test_mfcc_empty(self): # Passing in an empty tensor should result in an error self.assertRaises(AssertionError, kaldi.mfcc, torch.empty(0)) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_resample_waveform(self): def get_output_fn(sound, args): output = kaldi.resample_waveform(sound, args[1], args[2]) diff --git a/test/test_dataloader.py b/test/test_dataloader.py index f0c2f33442..10f861c8b8 100644 --- a/test/test_dataloader.py +++ b/test/test_dataloader.py @@ -1,16 +1,17 @@ import unittest -import common_utils import torch import torch.nn as nn from torch.utils.data import Dataset, DataLoader import torchaudio import math import os +from common_utils import AudioBackendScope, BACKENDS, create_temp_assets_dir +@unittest.skipIf("sox" not in BACKENDS, "sox not available") class TORCHAUDIODS(Dataset): - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() def __init__(self): self.asset_dirpath = os.path.join(self.test_dirpath, "assets") @@ -33,6 +34,7 @@ def __len__(self): return len(self.data) +@unittest.skipIf("sox" not in BACKENDS, "sox not available") class Test_DataLoader(unittest.TestCase): @classmethod def setUpClass(cls): @@ -50,4 +52,5 @@ def test_1(self): self.assertTrue(x.size() == expected_size) if __name__ == '__main__': - unittest.main() + with AudioBackendScope("sox"): + unittest.main() diff --git a/test/test_functional.py b/test/test_functional.py index e9c9e290fd..93382686e3 100644 --- a/test/test_functional.py +++ b/test/test_functional.py @@ -9,6 +9,7 @@ import unittest import common_utils +from common_utils import AudioBackendScope, BACKENDS from torchaudio.common_utils import IMPORT_LIBROSA if IMPORT_LIBROSA: @@ -39,7 +40,7 @@ class TestFunctional(unittest.TestCase): test_dirpath, test_dir = common_utils.create_temp_assets_dir() test_filepath = os.path.join(test_dirpath, 'assets', - 'steam-train-whistle-daniel_simon.mp3') + 'steam-train-whistle-daniel_simon.wav') waveform_train, sr_train = torchaudio.load(test_filepath) def test_torchscript_spectrogram(self): @@ -433,6 +434,8 @@ def test_create_fb(self): self._test_create_fb(n_mels=56, fmin=1900.0, fmax=900.0) self._test_create_fb(n_mels=10, fmin=1900.0, fmax=900.0) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_gain(self): waveform_gain = F.gain(self.waveform_train, 3) self.assertTrue(waveform_gain.abs().max().item(), 1.) @@ -444,6 +447,8 @@ def test_gain(self): self.assertTrue(torch.allclose(waveform_gain, sox_gain_waveform, atol=1e-04)) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_dither(self): waveform_dithered = F.dither(self.waveform_train) waveform_dithered_noiseshaped = F.dither(self.waveform_train, noise_shaping=True) @@ -461,6 +466,8 @@ def test_dither(self): self.assertTrue(torch.allclose(waveform_dithered_noiseshaped, sox_dither_waveform_ns, atol=1e-02)) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_vctk_transform_pipeline(self): test_filepath_vctk = os.path.join(self.test_dirpath, "assets/VCTK-Corpus/wav48/p224/", "p224_002.wav") wf_vctk, sr_vctk = torchaudio.load(test_filepath_vctk) diff --git a/test/test_functional_filtering.py b/test/test_functional_filtering.py index edc1f3859f..7527a30f39 100644 --- a/test/test_functional_filtering.py +++ b/test/test_functional_filtering.py @@ -4,8 +4,8 @@ import torchaudio import torchaudio.functional as F import unittest -import common_utils import time +from common_utils import AudioBackendScope, BACKENDS, create_temp_assets_dir def _test_torchscript_functional(py_method, *args, **kwargs): @@ -18,7 +18,7 @@ def _test_torchscript_functional(py_method, *args, **kwargs): class TestFunctionalFiltering(unittest.TestCase): - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() def _test_lfilter_basic(self, dtype, device): """ @@ -91,14 +91,14 @@ def _test_lfilter(self, waveform, device): def test_lfilter(self): - filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") waveform, _ = torchaudio.load(filepath, normalization=True) self._test_lfilter(waveform, torch.device("cpu")) def test_lfilter_gpu(self): if torch.cuda.is_available(): - filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") waveform, _ = torchaudio.load(filepath, normalization=True) cuda0 = torch.device("cuda:0") cuda_waveform = waveform.cuda(device=cuda0) @@ -107,6 +107,8 @@ def test_lfilter_gpu(self): print("skipping GPU test for lfilter because device not available") pass + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_lowpass(self): """ @@ -115,7 +117,7 @@ def test_lowpass(self): CUTOFF_FREQ = 3000 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("lowpass", [CUTOFF_FREQ]) @@ -127,6 +129,8 @@ def test_lowpass(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.lowpass_biquad, waveform, sample_rate, CUTOFF_FREQ) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_highpass(self): """ Test biquad highpass filter, compare to SoX implementation @@ -134,7 +138,7 @@ def test_highpass(self): CUTOFF_FREQ = 2000 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("highpass", [CUTOFF_FREQ]) @@ -147,6 +151,8 @@ def test_highpass(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-3) _test_torchscript_functional(F.highpass_biquad, waveform, sample_rate, CUTOFF_FREQ) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_allpass(self): """ Test biquad allpass filter, compare to SoX implementation @@ -155,7 +161,7 @@ def test_allpass(self): CENTRAL_FREQ = 1000 Q = 0.707 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("allpass", [CENTRAL_FREQ, str(Q) + 'q']) @@ -167,6 +173,8 @@ def test_allpass(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.allpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_bandpass_with_csg(self): """ Test biquad bandpass filter, compare to SoX implementation @@ -176,7 +184,7 @@ def test_bandpass_with_csg(self): Q = 0.707 CONST_SKIRT_GAIN = True - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("bandpass", ["-c", CENTRAL_FREQ, str(Q) + 'q']) @@ -188,6 +196,8 @@ def test_bandpass_with_csg(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.bandpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_bandpass_without_csg(self): """ Test biquad bandpass filter, compare to SoX implementation @@ -197,7 +207,7 @@ def test_bandpass_without_csg(self): Q = 0.707 CONST_SKIRT_GAIN = False - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("bandpass", [CENTRAL_FREQ, str(Q) + 'q']) @@ -209,6 +219,8 @@ def test_bandpass_without_csg(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.bandpass_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, CONST_SKIRT_GAIN) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_bandreject(self): """ Test biquad bandreject filter, compare to SoX implementation @@ -217,7 +229,7 @@ def test_bandreject(self): CENTRAL_FREQ = 1000 Q = 0.707 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("bandreject", [CENTRAL_FREQ, str(Q) + 'q']) @@ -229,6 +241,8 @@ def test_bandreject(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.bandreject_biquad, waveform, sample_rate, CENTRAL_FREQ, Q) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_band_with_noise(self): """ Test biquad band filter with noise mode, compare to SoX implementation @@ -238,7 +252,7 @@ def test_band_with_noise(self): Q = 0.707 NOISE = True - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("band", ["-n", CENTRAL_FREQ, str(Q) + 'q']) @@ -250,6 +264,8 @@ def test_band_with_noise(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.band_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, NOISE) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_band_without_noise(self): """ Test biquad band filter without noise mode, compare to SoX implementation @@ -259,7 +275,7 @@ def test_band_without_noise(self): Q = 0.707 NOISE = False - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("band", [CENTRAL_FREQ, str(Q) + 'q']) @@ -271,6 +287,8 @@ def test_band_without_noise(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.band_biquad, waveform, sample_rate, CENTRAL_FREQ, Q, NOISE) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_treble(self): """ Test biquad treble filter, compare to SoX implementation @@ -280,7 +298,7 @@ def test_treble(self): Q = 0.707 GAIN = 40 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("treble", [GAIN, CENTRAL_FREQ, str(Q) + 'q']) @@ -292,12 +310,14 @@ def test_treble(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.treble_biquad, waveform, sample_rate, GAIN, CENTRAL_FREQ, Q) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_deemph(self): """ Test biquad deemph filter, compare to SoX implementation """ - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("deemph") @@ -309,12 +329,14 @@ def test_deemph(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.deemph_biquad, waveform, sample_rate) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_riaa(self): """ Test biquad riaa filter, compare to SoX implementation """ - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("riaa") @@ -326,6 +348,8 @@ def test_riaa(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.riaa_biquad, waveform, sample_rate) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_equalizer(self): """ Test biquad peaking equalizer filter, compare to SoX implementation @@ -335,7 +359,7 @@ def test_equalizer(self): Q = 0.707 GAIN = 1 - noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + noise_filepath = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") E = torchaudio.sox_effects.SoxEffectsChain() E.set_input_file(noise_filepath) E.append_effect_to_chain("equalizer", [CENTER_FREQ, Q, GAIN]) @@ -347,9 +371,11 @@ def test_equalizer(self): assert torch.allclose(sox_output_waveform, output_waveform, atol=1e-4) _test_torchscript_functional(F.equalizer_biquad, waveform, sample_rate, CENTER_FREQ, GAIN, Q) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_perf_biquad_filtering(self): - fn_sine = os.path.join(self.test_dirpath, "assets", "whitenoise.mp3") + fn_sine = os.path.join(self.test_dirpath, "assets", "whitenoise.wav") b0 = 0.4 b1 = 0.2 diff --git a/test/test_sox_effects.py b/test/test_sox_effects.py index c93533c38c..1acb39a5ec 100644 --- a/test/test_sox_effects.py +++ b/test/test_sox_effects.py @@ -1,13 +1,15 @@ import unittest -import common_utils import torch import torchaudio import math import os +from common_utils import AudioBackendScope, BACKENDS, create_temp_assets_dir + +@unittest.skipIf("sox" not in BACKENDS, "sox not available") class Test_SoxEffectsChain(unittest.TestCase): - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() test_filepath = os.path.join(test_dirpath, "assets", "steam-train-whistle-daniel_simon.mp3") @@ -259,4 +261,5 @@ def test_vol(self): if __name__ == '__main__': - unittest.main() + with AudioBackendScope("sox"): + unittest.main() diff --git a/test/test_transforms.py b/test/test_transforms.py index b0552001f4..0d671c21ce 100644 --- a/test/test_transforms.py +++ b/test/test_transforms.py @@ -7,7 +7,7 @@ import torchaudio.functional as F from torchaudio.common_utils import IMPORT_LIBROSA, IMPORT_SCIPY import unittest -import common_utils +from common_utils import AudioBackendScope, BACKENDS, create_temp_assets_dir if IMPORT_LIBROSA: import librosa @@ -52,9 +52,9 @@ class Tester(unittest.TestCase): waveform.unsqueeze_(0) # (1, 64000) waveform = (waveform * volume * 2**31).long() # file for stereo stft test - test_dirpath, test_dir = common_utils.create_temp_assets_dir() + test_dirpath, test_dir = create_temp_assets_dir() test_filepath = os.path.join(test_dirpath, 'assets', - 'steam-train-whistle-daniel_simon.mp3') + 'steam-train-whistle-daniel_simon.wav') def scale(self, waveform, factor=float(2**31)): # scales a waveform by a factor @@ -530,8 +530,13 @@ def test_batch_melspectrogram(self): self.assertTrue(computed.shape == expected.shape, (computed.shape, expected.shape)) self.assertTrue(torch.allclose(computed, expected)) + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_batch_mfcc(self): - waveform, sample_rate = torchaudio.load(self.test_filepath) + test_filepath = os.path.join( + self.test_dirpath, 'assets', 'steam-train-whistle-daniel_simon.mp3' + ) + waveform, sample_rate = torchaudio.load(test_filepath) # Single then transform then batch expected = transforms.MFCC()(waveform).repeat(3, 1, 1, 1) @@ -632,7 +637,7 @@ class TestLibrosaConsistency(unittest.TestCase): @classmethod def setUpClass(cls): - cls.test_dirpath, cls.test_dir = common_utils.create_temp_assets_dir() + cls.test_dirpath, cls.test_dir = create_temp_assets_dir() def _to_librosa(self, sound): return sound.cpu().numpy().squeeze() @@ -644,6 +649,8 @@ def _get_sample_data(self, *asset_paths, **kwargs): return sound.mean(dim=0, keepdim=True), sample_rate @unittest.skipIf(not IMPORT_LIBROSA, 'Librosa is not available') + @unittest.skipIf("sox" not in BACKENDS, "sox not available") + @AudioBackendScope("sox") def test_MelScale(self): """MelScale transform is comparable to that of librosa""" n_fft = 2048