test_transforms.py 5.29 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
14
    sig = (torch.cos(2 * np.pi * torch.arange(0, 4 * sr).float() * freq / sr))
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

    def test_scale(self):

        audio_orig = self.sig.clone()
        result = transforms.Scale()(audio_orig)
23
        self.assertTrue(result.min() >= -1. and result.max() <= 1.)
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
        result = transforms.Scale(factor=maxminmax)(audio_orig)
        self.assertTrue((result.min() == -1. or result.max() == 1.) and
29
                        result.min() >= -1. and result.max() <= 1.)
David Pollack's avatar
David Pollack committed
30

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

David Pollack's avatar
David Pollack committed
34
35
36
37
38
39
    def test_pad_trim(self):

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

40
41
        result = transforms.PadTrim(max_len=length_new, channels_first=False)(audio_orig)
        self.assertEqual(result.size(0), length_new)
David Pollack's avatar
David Pollack committed
42

43
44
45
        result = transforms.PadTrim(max_len=length_new, channels_first=True)(audio_orig.transpose(0, 1))
        self.assertEqual(result.size(1), length_new)

David Pollack's avatar
David Pollack committed
46
47
48
49
        audio_orig = self.sig.clone()
        length_orig = audio_orig.size(0)
        length_new = int(length_orig * 0.8)

50
        result = transforms.PadTrim(max_len=length_new, channels_first=False)(audio_orig)
David Pollack's avatar
David Pollack committed
51

52
        self.assertEqual(result.size(0), length_new)
David Pollack's avatar
David Pollack committed
53

54
        repr_test = transforms.PadTrim(max_len=length_new, channels_first=False)
55
56
        repr_test.__repr__()

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

David Pollack's avatar
David Pollack committed
59
60
61
62
63
64
65
66
67
        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)

68
        result = transforms.DownmixMono(channels_first=False)(audio_Stereo)
David Pollack's avatar
David Pollack committed
69
70
71

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

72
        repr_test = transforms.DownmixMono(channels_first=False)
73
74
        repr_test.__repr__()

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

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

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

84
85
86
87
    def test_mel(self):

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

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

David Pollack's avatar
David Pollack committed
99
100
101
102
103
    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
104
105
        maxminmax = np.abs(
            [audio_orig.min(), audio_orig.max()]).max().astype(np.float)
David Pollack's avatar
David Pollack committed
106
107

        tset = (transforms.Scale(factor=maxminmax),
108
                transforms.PadTrim(max_len=length_new, channels_first=False))
David Pollack's avatar
David Pollack committed
109
110
111
112
113
114
        result = transforms.Compose(tset)(audio_orig)

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

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

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

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

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

148
149
150
151
    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)
152
        spectrogram_torch = transforms.MEL2(window_fn=torch.hamming_window, pad=10)(audio_scaled)  # (1, 319, 40)
153
154
        self.assertTrue(spectrogram_torch.dim() == 3)
        self.assertTrue(spectrogram_torch.max() <= 0.)
Soumith Chintala's avatar
Soumith Chintala committed
155

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