test.py 3.25 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))
David Pollack's avatar
David Pollack committed
18
19
20
21
22
23
24
25
26
27
28

        # check normalizing
        x, sr = torchaudio.load(self.test_filepath, normalization=True)
        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):
29
30
            tdir = os.path.join(os.path.dirname(
                self.test_dirpath), "torchaudio")
David Pollack's avatar
David Pollack committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
            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
50
51
        x = x[:, 0]  # get mono signal
        x.squeeze_()  # remove channel dim
David Pollack's avatar
David Pollack committed
52
53
54
55
56
57
        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
58
            x.unsqueeze_(0)  # N x L not L x N
David Pollack's avatar
David Pollack committed
59
60
61
62
63
            torchaudio.save(new_filepath, x, sr)

        with self.assertRaises(ValueError):
            x.squeeze_()
            x.unsqueeze_(1)
Soumith Chintala's avatar
Soumith Chintala committed
64
            x.unsqueeze_(0)  # 1 x L x 1
David Pollack's avatar
David Pollack committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
            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):
81
82
            new_filepath = os.path.join(
                self.test_dirpath, "no-path", "test.wav")
David Pollack's avatar
David Pollack committed
83
            torchaudio.save(new_filepath, x, sr)
Soumith Chintala's avatar
Soumith Chintala committed
84

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

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

Soumith Chintala's avatar
Soumith Chintala committed
99

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