test_transforms.py 5.1 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
        result = transforms.PadTrim(max_len=length_new, channels_first=False)(audio_orig)
David Pollack's avatar
David Pollack committed
41

42
        self.assertEqual(result.size(0), length_new)
David Pollack's avatar
David Pollack committed
43
44
45
46
47

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

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

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

52
        repr_test = transforms.PadTrim(max_len=length_new, channels_first=False)
53
54
        repr_test.__repr__()

David Pollack's avatar
David Pollack committed
55
    def test_downmix_mono(self):
David Pollack's avatar
David Pollack committed
56

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

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

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

70
        repr_test = transforms.DownmixMono(channels_first=False)
71
72
        repr_test.__repr__()

73
74
75
76
77
78
    def test_lc2cl(self):

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

79
80
81
        repr_test = transforms.LC2CL()
        repr_test.__repr__()

82
83
84
85
    def test_mel(self):

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

92
93
94
95
96
        repr_test = transforms.MEL()
        repr_test.__repr__()
        repr_test = transforms.BLC2CBL()
        repr_test.__repr__()

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

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

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

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

113
114
115
        repr_test = transforms.Compose(tset)
        repr_test.__repr__()

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

141
142
143
144
145
        repr_test = transforms.MuLawEncoding(quantization_channels)
        repr_test.__repr__()
        repr_test = transforms.MuLawExpanding(quantization_channels)
        repr_test.__repr__()

146
147
148
149
150
151
152
    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
153

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