test.py 6.54 KB
Newer Older
David Pollack's avatar
David Pollack committed
1
import unittest
2
import torch
Soumith Chintala's avatar
Soumith Chintala committed
3
import torchaudio
David Pollack's avatar
David Pollack committed
4
import math
David Pollack's avatar
David Pollack committed
5
6
import os

Soumith Chintala's avatar
Soumith Chintala committed
7

David Pollack's avatar
David Pollack committed
8
9
class Test_LoadSave(unittest.TestCase):
    test_dirpath = os.path.dirname(os.path.realpath(__file__))
10
11
    test_filepath = os.path.join(test_dirpath, "assets",
                                 "steam-train-whistle-daniel_simon.mp3")
Soumith Chintala's avatar
Soumith Chintala committed
12

David Pollack's avatar
David Pollack committed
13
14
15
16
    def test_load(self):
        # check normal loading
        x, sr = torchaudio.load(self.test_filepath)
        self.assertEqual(sr, 44100)
Soumith Chintala's avatar
Soumith Chintala committed
17
        self.assertEqual(x.size(), (278756, 2))
18
        self.assertGreater(x.sum(), 0)
David Pollack's avatar
David Pollack committed
19
20
21

        # check normalizing
        x, sr = torchaudio.load(self.test_filepath, normalization=True)
22
        self.assertEqual(x.dtype, torch.float32)
David Pollack's avatar
David Pollack committed
23
24
25
26
27
28
29
30
        self.assertTrue(x.min() >= -1.0)
        self.assertTrue(x.max() <= 1.0)

        # check raising errors
        with self.assertRaises(OSError):
            torchaudio.load("file-does-not-exist.mp3")

        with self.assertRaises(OSError):
31
32
            tdir = os.path.join(
                os.path.dirname(self.test_dirpath), "torchaudio")
David Pollack's avatar
David Pollack committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
            torchaudio.load(tdir)

    def test_save(self):
        # load signal
        x, sr = torchaudio.load(self.test_filepath)

        # check save
        new_filepath = os.path.join(self.test_dirpath, "test.wav")
        torchaudio.save(new_filepath, x, sr)
        self.assertTrue(os.path.isfile(new_filepath))
        os.unlink(new_filepath)

        # check automatic normalization
        x /= 1 << 31
        torchaudio.save(new_filepath, x, sr)
        self.assertTrue(os.path.isfile(new_filepath))
        os.unlink(new_filepath)

        # test save 1d tensor
Soumith Chintala's avatar
Soumith Chintala committed
52
53
        x = x[:, 0]  # get mono signal
        x.squeeze_()  # remove channel dim
David Pollack's avatar
David Pollack committed
54
55
56
57
58
59
        torchaudio.save(new_filepath, x, sr)
        self.assertTrue(os.path.isfile(new_filepath))
        os.unlink(new_filepath)

        # don't allow invalid sizes as inputs
        with self.assertRaises(ValueError):
Soumith Chintala's avatar
Soumith Chintala committed
60
            x.unsqueeze_(0)  # N x L not L x N
David Pollack's avatar
David Pollack committed
61
62
63
64
65
            torchaudio.save(new_filepath, x, sr)

        with self.assertRaises(ValueError):
            x.squeeze_()
            x.unsqueeze_(1)
Soumith Chintala's avatar
Soumith Chintala committed
66
            x.unsqueeze_(0)  # 1 x L x 1
David Pollack's avatar
David Pollack committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
            torchaudio.save(new_filepath, x, sr)

        # automatically convert sr from floating point to int
        x.squeeze_(0)
        torchaudio.save(new_filepath, x, float(sr))
        self.assertTrue(os.path.isfile(new_filepath))
        os.unlink(new_filepath)

        # don't allow uneven integers
        with self.assertRaises(TypeError):
            torchaudio.save(new_filepath, x, float(sr) + 0.5)
            self.assertTrue(os.path.isfile(new_filepath))
            os.unlink(new_filepath)

        # don't save to folders that don't exist
        with self.assertRaises(OSError):
83
84
            new_filepath = os.path.join(self.test_dirpath, "no-path",
                                        "test.wav")
David Pollack's avatar
David Pollack committed
85
            torchaudio.save(new_filepath, x, sr)
Soumith Chintala's avatar
Soumith Chintala committed
86

87
        # save created file
88
89
        sinewave_filepath = os.path.join(self.test_dirpath, "assets",
                                         "sinewave.wav")
90
91
92
93
        sr = 16000
        freq = 440
        volume = 0.3

94
        y = (torch.cos(
95
            2 * math.pi * torch.arange(0, 4 * sr).float() * freq / sr))
96
97
        y.unsqueeze_(1)
        # y is between -1 and 1, so must scale
Soumith Chintala's avatar
Soumith Chintala committed
98
        y = (y * volume * 2**31).long()
99
100
        torchaudio.save(sinewave_filepath, y, sr)
        self.assertTrue(os.path.isfile(sinewave_filepath))
101

102
103
104
105
106
107
108
109
110
        # test precision
        new_filepath = os.path.join(self.test_dirpath, "test.wav")
        _, _, _, bp = torchaudio.info(sinewave_filepath)
        torchaudio.save(new_filepath, y, sr, precision=16)
        _, _, _, bp16 = torchaudio.info(new_filepath)
        self.assertEqual(bp, 32)
        self.assertEqual(bp16, 16)
        os.unlink(new_filepath)

111
112
113
114
115
116
117
118
119
120
    def test_load_and_save_is_identity(self):
        input_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
        tensor, sample_rate = torchaudio.load(input_path)
        output_path = os.path.join(self.test_dirpath, 'test.wav')
        torchaudio.save(output_path, tensor, sample_rate)
        tensor2, sample_rate2 = torchaudio.load(output_path)
        self.assertTrue(tensor.allclose(tensor2))
        self.assertEqual(sample_rate, sample_rate2)
        os.unlink(output_path)

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    def test_load_partial(self):
        num_frames = 100
        offset = 200
        # load entire mono sinewave wav file, load a partial copy and then compare
        input_sine_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
        x_sine_full, sr_sine = torchaudio.load(input_sine_path)
        x_sine_part, _ = torchaudio.load(input_sine_path, num_frames=num_frames, offset=offset)
        l1_error = x_sine_full[offset:(num_frames+offset)].sub(x_sine_part).abs().sum().item()
        # test for the correct number of samples and that the correct portion was loaded
        self.assertEqual(x_sine_part.size(0), num_frames)
        self.assertEqual(l1_error, 0.)

        # create a two channel version of this wavefile
        x_2ch_sine = x_sine_full.repeat(1, 2)
        out_2ch_sine_path = os.path.join(self.test_dirpath, 'assets', '2ch_sinewave.wav')
        torchaudio.save(out_2ch_sine_path, x_2ch_sine, sr_sine)
        x_2ch_sine_load, _ = torchaudio.load(out_2ch_sine_path, num_frames=num_frames, offset=offset)
        os.unlink(out_2ch_sine_path)
        l1_error = x_2ch_sine_load.sub(x_2ch_sine[offset:(offset + num_frames)]).abs().sum().item()
        self.assertEqual(l1_error, 0.)

        # test with two channel mp3
        x_2ch_full, sr_2ch = torchaudio.load(self.test_filepath, normalization=True)
        x_2ch_part, _ = torchaudio.load(self.test_filepath, normalization=True, num_frames=num_frames, offset=offset)
        l1_error = x_2ch_full[offset:(offset+num_frames)].sub(x_2ch_part).abs().sum().item()
        self.assertEqual(x_2ch_part.size(0), num_frames)
        self.assertEqual(l1_error, 0.)

        # check behavior if number of samples would exceed file length
        offset_ns = 300
        x_ns, _ = torchaudio.load(input_sine_path, num_frames=100000, offset=offset_ns)
        self.assertEqual(x_ns.size(0), x_sine_full.size(0) - offset_ns)

        # check when offset is beyond the end of the file
        with self.assertRaises(RuntimeError):
            torchaudio.load(input_sine_path, offset=100000)

    def test_get_info(self):
        input_path = os.path.join(self.test_dirpath, 'assets', 'sinewave.wav')
        info_expected = (1, 64000, 16000, 32)
        info_load = torchaudio.info(input_path)
        self.assertEqual(info_load, info_expected)
Soumith Chintala's avatar
Soumith Chintala committed
163

David Pollack's avatar
David Pollack committed
164
165
if __name__ == '__main__':
    unittest.main()