test_transforms.py 5.34 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 = .3
Soumith Chintala's avatar
Soumith Chintala committed
14
    sig = (torch.cos(2 * np.pi * torch.arange(0, 4 * sr) * freq / sr)).float()
15
    # sig = (torch.cos((1+torch.arange(0, 4 * sr) * 2) / sr * 2 * np.pi * torch.arange(0, 4 * sr) * freq / sr)).float()
David Pollack's avatar
David Pollack committed
16
    sig.unsqueeze_(1)
Soumith Chintala's avatar
Soumith Chintala committed
17
    sig = (sig * volume * 2**31).long()
David Pollack's avatar
David Pollack committed
18
19
20
21
22
23

    def test_scale(self):

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

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

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

David Pollack's avatar
David Pollack committed
36
37
38
39
40
41
42
43
44
    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,
45
                        print("old size: {}, new size: {}".format(audio_orig.size(0), result.size(0))))
David Pollack's avatar
David Pollack committed
46
47
48
49
50
51
52
53

        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,
54
                        print("old size: {}, new size: {}".format(audio_orig.size(0), result.size(0))))
David Pollack's avatar
David Pollack committed
55

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

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

David Pollack's avatar
David Pollack committed
61
62
63
64
65
66
67
68
69
70
71
72
73
        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)

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

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

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

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

86
87
88
89
    def test_mel(self):

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

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

David Pollack's avatar
David Pollack committed
101
102
103
104
105
    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
106
107
        maxminmax = np.abs(
            [audio_orig.min(), audio_orig.max()]).max().astype(np.float)
David Pollack's avatar
David Pollack committed
108
109
110
111
112
113
114
115
116

        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)

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

David Pollack's avatar
David Pollack committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    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
144

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

150
151
152
153
154
155
156
    def test_mel2(self):
        audio_orig = self.sig.clone()  # (16000, 1)
        audio_scaled = transforms.Scale()(audio_orig)  # (16000, 1)
        audio_scaled = transforms.LC2CL()(audio_scaled)  # (1, 16000)
        spectrogram_torch = transforms.MEL2()(audio_scaled)  # (1, 319, 40)
        self.assertTrue(spectrogram_torch.dim() == 3)
        self.assertTrue(spectrogram_torch.max() <= 0.)
Soumith Chintala's avatar
Soumith Chintala committed
157

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