test_info.py 3.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
import itertools
from parameterized import parameterized

from torchaudio.backend import sox_io_backend

from ..common_utils import (
    TempDirMixin,
    PytorchTestCase,
    skipIfNoExec,
    skipIfNoExtension,
moto's avatar
moto committed
11
    sox_utils,
moto's avatar
moto committed
12
13
    get_wav_data,
    save_wav,
14
)
moto's avatar
moto committed
15
16
17
from .common import (
    name_func,
)
18
19
20
21
22
23
24
25
26


@skipIfNoExec('sox')
@skipIfNoExtension
class TestInfo(TempDirMixin, PytorchTestCase):
    @parameterized.expand(list(itertools.product(
        ['float32', 'int32', 'int16', 'uint8'],
        [8000, 16000],
        [1, 2],
moto's avatar
moto committed
27
    )), name_func=name_func)
28
29
30
    def test_wav(self, dtype, sample_rate, num_channels):
        """`sox_io_backend.info` can check wav file correctly"""
        duration = 1
31
        path = self.get_temp_path('data.wav')
moto's avatar
moto committed
32
33
        data = get_wav_data(dtype, num_channels, normalize=False, num_frames=duration * sample_rate)
        save_wav(path, data, sample_rate)
34
35
        info = sox_io_backend.info(path)
        assert info.get_sample_rate() == sample_rate
36
        assert info.get_num_frames() == sample_rate * duration
37
38
39
40
41
42
        assert info.get_num_channels() == num_channels

    @parameterized.expand(list(itertools.product(
        ['float32', 'int32', 'int16', 'uint8'],
        [8000, 16000],
        [4, 8, 16, 32],
moto's avatar
moto committed
43
    )), name_func=name_func)
44
45
46
    def test_wav_multiple_channels(self, dtype, sample_rate, num_channels):
        """`sox_io_backend.info` can check wav file with channels more than 2 correctly"""
        duration = 1
47
        path = self.get_temp_path('data.wav')
moto's avatar
moto committed
48
49
        data = get_wav_data(dtype, num_channels, normalize=False, num_frames=duration * sample_rate)
        save_wav(path, data, sample_rate)
50
51
        info = sox_io_backend.info(path)
        assert info.get_sample_rate() == sample_rate
52
        assert info.get_num_frames() == sample_rate * duration
53
54
55
56
57
58
        assert info.get_num_channels() == num_channels

    @parameterized.expand(list(itertools.product(
        [8000, 16000],
        [1, 2],
        [96, 128, 160, 192, 224, 256, 320],
moto's avatar
moto committed
59
    )), name_func=name_func)
60
61
62
    def test_mp3(self, sample_rate, num_channels, bit_rate):
        """`sox_io_backend.info` can check mp3 file correctly"""
        duration = 1
63
        path = self.get_temp_path('data.mp3')
64
65
66
67
68
69
70
        sox_utils.gen_audio_file(
            path, sample_rate, num_channels,
            compression=bit_rate, duration=duration,
        )
        info = sox_io_backend.info(path)
        assert info.get_sample_rate() == sample_rate
        # mp3 does not preserve the number of samples
71
        # assert info.get_num_frames() == sample_rate * duration
72
73
74
75
76
77
        assert info.get_num_channels() == num_channels

    @parameterized.expand(list(itertools.product(
        [8000, 16000],
        [1, 2],
        list(range(9)),
moto's avatar
moto committed
78
    )), name_func=name_func)
79
80
81
    def test_flac(self, sample_rate, num_channels, compression_level):
        """`sox_io_backend.info` can check flac file correctly"""
        duration = 1
82
        path = self.get_temp_path('data.flac')
83
84
85
86
87
88
        sox_utils.gen_audio_file(
            path, sample_rate, num_channels,
            compression=compression_level, duration=duration,
        )
        info = sox_io_backend.info(path)
        assert info.get_sample_rate() == sample_rate
89
        assert info.get_num_frames() == sample_rate * duration
90
91
92
93
94
95
        assert info.get_num_channels() == num_channels

    @parameterized.expand(list(itertools.product(
        [8000, 16000],
        [1, 2],
        [-1, 0, 1, 2, 3, 3.6, 5, 10],
moto's avatar
moto committed
96
    )), name_func=name_func)
97
98
99
    def test_vorbis(self, sample_rate, num_channels, quality_level):
        """`sox_io_backend.info` can check vorbis file correctly"""
        duration = 1
100
        path = self.get_temp_path('data.vorbis')
101
102
103
104
105
106
        sox_utils.gen_audio_file(
            path, sample_rate, num_channels,
            compression=quality_level, duration=duration,
        )
        info = sox_io_backend.info(path)
        assert info.get_sample_rate() == sample_rate
107
        assert info.get_num_frames() == sample_rate * duration
108
        assert info.get_num_channels() == num_channels