test_transforms.py 4.86 KB
Newer Older
1
from __future__ import print_function
David Pollack's avatar
David Pollack committed
2
3
4
5
6
7
import torch
import torchaudio
import torchaudio.transforms as transforms
import numpy as np
import unittest

Soumith Chintala's avatar
Soumith Chintala committed
8

David Pollack's avatar
David Pollack committed
9
10
11
12
class Tester(unittest.TestCase):

    sr = 16000
    freq = 440
13
    volume = 0.3
Soumith Chintala's avatar
Soumith Chintala committed
14
    sig = (torch.cos(2 * np.pi * torch.arange(0, 4 * sr) * freq / sr)).float()
David Pollack's avatar
David Pollack committed
15
    sig.unsqueeze_(1)
Soumith Chintala's avatar
Soumith Chintala committed
16
    sig = (sig * volume * 2**31).long()
David Pollack's avatar
David Pollack committed
17
18
19
20
21
22

    def test_scale(self):

        audio_orig = self.sig.clone()
        result = transforms.Scale()(audio_orig)
        self.assertTrue(result.min() >= -1. and result.max() <= 1.,
23
                        print("min: {}, max: {}".format(result.min(), result.max())))
David Pollack's avatar
David Pollack committed
24

Soumith Chintala's avatar
Soumith Chintala committed
25
26
        maxminmax = np.abs(
            [audio_orig.min(), audio_orig.max()]).max().astype(np.float)
David Pollack's avatar
David Pollack committed
27
28
29
        result = transforms.Scale(factor=maxminmax)(audio_orig)
        self.assertTrue((result.min() == -1. or result.max() == 1.) and
                        result.min() >= -1. and result.max() <= 1.,
30
                        print("min: {}, max: {}".format(result.min(), result.max())))
David Pollack's avatar
David Pollack committed
31

32
33
34
        repr_test = transforms.Scale()
        repr_test.__repr__()

David Pollack's avatar
David Pollack committed
35
36
37
38
39
40
41
42
43
    def test_pad_trim(self):

        audio_orig = self.sig.clone()
        length_orig = audio_orig.size(0)
        length_new = int(length_orig * 1.2)

        result = transforms.PadTrim(max_len=length_new)(audio_orig)

        self.assertTrue(result.size(0) == length_new,
44
                        print("old size: {}, new size: {}".format(audio_orig.size(0), result.size(0))))
David Pollack's avatar
David Pollack committed
45
46
47
48
49
50
51
52

        audio_orig = self.sig.clone()
        length_orig = audio_orig.size(0)
        length_new = int(length_orig * 0.8)

        result = transforms.PadTrim(max_len=length_new)(audio_orig)

        self.assertTrue(result.size(0) == length_new,
53
                        print("old size: {}, new size: {}".format(audio_orig.size(0), result.size(0))))
David Pollack's avatar
David Pollack committed
54

55
56
57
        repr_test = transforms.PadTrim(max_len=length_new)
        repr_test.__repr__()

David Pollack's avatar
David Pollack committed
58
    def test_downmix_mono(self):
David Pollack's avatar
David Pollack committed
59

David Pollack's avatar
David Pollack committed
60
61
62
63
64
65
66
67
68
69
70
71
72
        audio_L = self.sig.clone()
        audio_R = self.sig.clone()
        R_idx = int(audio_R.size(0) * 0.1)
        audio_R = torch.cat((audio_R[R_idx:], audio_R[:R_idx]))

        audio_Stereo = torch.cat((audio_L, audio_R), dim=1)

        self.assertTrue(audio_Stereo.size(1) == 2)

        result = transforms.DownmixMono()(audio_Stereo)

        self.assertTrue(result.size(1) == 1)

73
74
75
        repr_test = transforms.DownmixMono()
        repr_test.__repr__()

76
77
78
79
80
81
    def test_lc2cl(self):

        audio = self.sig.clone()
        result = transforms.LC2CL()(audio)
        self.assertTrue(result.size()[::-1] == audio.size())

82
83
84
        repr_test = transforms.LC2CL()
        repr_test.__repr__()

85
86
87
88
    def test_mel(self):

        audio = self.sig.clone()
        audio = transforms.Scale()(audio)
89
        self.assertTrue(len(audio.size()) == 2)
90
        result = transforms.MEL()(audio)
91
        self.assertTrue(len(result.size()) == 3)
92
        result = transforms.BLC2CBL()(result)
93
        self.assertTrue(len(result.size()) == 3)
94

95
96
97
98
99
        repr_test = transforms.MEL()
        repr_test.__repr__()
        repr_test = transforms.BLC2CBL()
        repr_test.__repr__()

David Pollack's avatar
David Pollack committed
100
101
102
103
104
    def test_compose(self):

        audio_orig = self.sig.clone()
        length_orig = audio_orig.size(0)
        length_new = int(length_orig * 1.2)
Soumith Chintala's avatar
Soumith Chintala committed
105
106
        maxminmax = np.abs(
            [audio_orig.min(), audio_orig.max()]).max().astype(np.float)
David Pollack's avatar
David Pollack committed
107
108
109
110
111
112
113
114
115

        tset = (transforms.Scale(factor=maxminmax),
                transforms.PadTrim(max_len=length_new))
        result = transforms.Compose(tset)(audio_orig)

        self.assertTrue(np.abs([result.min(), result.max()]).max() == 1.)

        self.assertTrue(result.size(0) == length_new)

116
117
118
        repr_test = transforms.Compose(tset)
        repr_test.__repr__()

David Pollack's avatar
David Pollack committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    def test_mu_law_companding(self):

        sig = self.sig.clone()

        quantization_channels = 256
        sig = self.sig.numpy()
        sig = sig / np.abs(sig).max()
        self.assertTrue(sig.min() >= -1. and sig.max() <= 1.)

        sig_mu = transforms.MuLawEncoding(quantization_channels)(sig)
        self.assertTrue(sig_mu.min() >= 0. and sig.max() <= quantization_channels)

        sig_exp = transforms.MuLawExpanding(quantization_channels)(sig_mu)
        self.assertTrue(sig_exp.min() >= -1. and sig_exp.max() <= 1.)

        sig = self.sig.clone()
        sig = sig / torch.abs(sig).max()
        self.assertTrue(sig.min() >= -1. and sig.max() <= 1.)

        sig_mu = transforms.MuLawEncoding(quantization_channels)(sig)
        self.assertTrue(sig_mu.min() >= 0. and sig.max() <= quantization_channels)

        sig_exp = transforms.MuLawExpanding(quantization_channels)(sig_mu)
        self.assertTrue(sig_exp.min() >= -1. and sig_exp.max() <= 1.)
David Pollack's avatar
David Pollack committed
143

144
145
146
147
148
        repr_test = transforms.MuLawEncoding(quantization_channels)
        repr_test.__repr__()
        repr_test = transforms.MuLawExpanding(quantization_channels)
        repr_test.__repr__()

Soumith Chintala's avatar
Soumith Chintala committed
149

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