|  | 
|  | 1 | +import itertools | 
|  | 2 | +from parameterized import parameterized | 
|  | 3 | + | 
|  | 4 | +from torchaudio.backend import sox_io_backend | 
|  | 5 | + | 
|  | 6 | +from ..common_utils import ( | 
|  | 7 | +    TempDirMixin, | 
|  | 8 | +    PytorchTestCase, | 
|  | 9 | +    skipIfNoExec, | 
|  | 10 | +    skipIfNoExtension, | 
|  | 11 | +) | 
|  | 12 | +from .common import ( | 
|  | 13 | +    get_test_name | 
|  | 14 | +) | 
|  | 15 | +from . import sox_utils | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +@skipIfNoExec('sox') | 
|  | 19 | +@skipIfNoExtension | 
|  | 20 | +class TestInfo(TempDirMixin, PytorchTestCase): | 
|  | 21 | +    @parameterized.expand(list(itertools.product( | 
|  | 22 | +        ['float32', 'int32', 'int16', 'uint8'], | 
|  | 23 | +        [8000, 16000], | 
|  | 24 | +        [1, 2], | 
|  | 25 | +    )), name_func=get_test_name) | 
|  | 26 | +    def test_wav(self, dtype, sample_rate, num_channels): | 
|  | 27 | +        """`sox_io_backend.info` can check wav file correctly""" | 
|  | 28 | +        duration = 1 | 
|  | 29 | +        path = self.get_temp_path(f'{dtype}_{sample_rate}_{num_channels}.wav') | 
|  | 30 | +        sox_utils.gen_audio_file( | 
|  | 31 | +            path, sample_rate, num_channels, | 
|  | 32 | +            bit_depth=sox_utils.get_bit_depth(dtype), | 
|  | 33 | +            encoding=sox_utils.get_encoding(dtype), | 
|  | 34 | +            duration=duration, | 
|  | 35 | +        ) | 
|  | 36 | +        info = sox_io_backend.info(path) | 
|  | 37 | +        assert info.get_sample_rate() == sample_rate | 
|  | 38 | +        assert info.get_num_samples() == sample_rate * duration | 
|  | 39 | +        assert info.get_num_channels() == num_channels | 
|  | 40 | + | 
|  | 41 | +    @parameterized.expand(list(itertools.product( | 
|  | 42 | +        ['float32', 'int32', 'int16', 'uint8'], | 
|  | 43 | +        [8000, 16000], | 
|  | 44 | +        [4, 8, 16, 32], | 
|  | 45 | +    )), name_func=get_test_name) | 
|  | 46 | +    def test_wav_multiple_channels(self, dtype, sample_rate, num_channels): | 
|  | 47 | +        """`sox_io_backend.info` can check wav file with channels more than 2 correctly""" | 
|  | 48 | +        duration = 1 | 
|  | 49 | +        path = self.get_temp_path(f'{dtype}_{sample_rate}_{num_channels}.wav') | 
|  | 50 | +        sox_utils.gen_audio_file( | 
|  | 51 | +            path, sample_rate, num_channels, | 
|  | 52 | +            bit_depth=sox_utils.get_bit_depth(dtype), | 
|  | 53 | +            encoding=sox_utils.get_encoding(dtype), | 
|  | 54 | +            duration=duration, | 
|  | 55 | +        ) | 
|  | 56 | +        info = sox_io_backend.info(path) | 
|  | 57 | +        assert info.get_sample_rate() == sample_rate | 
|  | 58 | +        assert info.get_num_samples() == sample_rate * duration | 
|  | 59 | +        assert info.get_num_channels() == num_channels | 
|  | 60 | + | 
|  | 61 | +    @parameterized.expand(list(itertools.product( | 
|  | 62 | +        [8000, 16000], | 
|  | 63 | +        [1, 2], | 
|  | 64 | +        [96, 128, 160, 192, 224, 256, 320], | 
|  | 65 | +    )), name_func=get_test_name) | 
|  | 66 | +    def test_mp3(self, sample_rate, num_channels, bit_rate): | 
|  | 67 | +        """`sox_io_backend.info` can check mp3 file correctly""" | 
|  | 68 | +        duration = 1 | 
|  | 69 | +        path = self.get_temp_path(f'{sample_rate}_{num_channels}_{bit_rate}k.mp3') | 
|  | 70 | +        sox_utils.gen_audio_file( | 
|  | 71 | +            path, sample_rate, num_channels, | 
|  | 72 | +            compression=bit_rate, duration=duration, | 
|  | 73 | +        ) | 
|  | 74 | +        info = sox_io_backend.info(path) | 
|  | 75 | +        assert info.get_sample_rate() == sample_rate | 
|  | 76 | +        # mp3 does not preserve the number of samples | 
|  | 77 | +        # assert info.get_num_samples() == sample_rate * duration | 
|  | 78 | +        assert info.get_num_channels() == num_channels | 
|  | 79 | + | 
|  | 80 | +    @parameterized.expand(list(itertools.product( | 
|  | 81 | +        [8000, 16000], | 
|  | 82 | +        [1, 2], | 
|  | 83 | +        list(range(9)), | 
|  | 84 | +    )), name_func=get_test_name) | 
|  | 85 | +    def test_flac(self, sample_rate, num_channels, compression_level): | 
|  | 86 | +        """`sox_io_backend.info` can check flac file correctly""" | 
|  | 87 | +        duration = 1 | 
|  | 88 | +        path = self.get_temp_path(f'{sample_rate}_{num_channels}_{compression_level}.flac') | 
|  | 89 | +        sox_utils.gen_audio_file( | 
|  | 90 | +            path, sample_rate, num_channels, | 
|  | 91 | +            compression=compression_level, duration=duration, | 
|  | 92 | +        ) | 
|  | 93 | +        info = sox_io_backend.info(path) | 
|  | 94 | +        assert info.get_sample_rate() == sample_rate | 
|  | 95 | +        assert info.get_num_samples() == sample_rate * duration | 
|  | 96 | +        assert info.get_num_channels() == num_channels | 
|  | 97 | + | 
|  | 98 | +    @parameterized.expand(list(itertools.product( | 
|  | 99 | +        [8000, 16000], | 
|  | 100 | +        [1, 2], | 
|  | 101 | +        [-1, 0, 1, 2, 3, 3.6, 5, 10], | 
|  | 102 | +    )), name_func=get_test_name) | 
|  | 103 | +    def test_vorbis(self, sample_rate, num_channels, quality_level): | 
|  | 104 | +        """`sox_io_backend.info` can check vorbis file correctly""" | 
|  | 105 | +        duration = 1 | 
|  | 106 | +        path = self.get_temp_path(f'{sample_rate}_{num_channels}_{quality_level}.vorbis') | 
|  | 107 | +        sox_utils.gen_audio_file( | 
|  | 108 | +            path, sample_rate, num_channels, | 
|  | 109 | +            compression=quality_level, duration=duration, | 
|  | 110 | +        ) | 
|  | 111 | +        info = sox_io_backend.info(path) | 
|  | 112 | +        assert info.get_sample_rate() == sample_rate | 
|  | 113 | +        assert info.get_num_samples() == sample_rate * duration | 
|  | 114 | +        assert info.get_num_channels() == num_channels | 
0 commit comments