1 | n/a | import audioop |
---|
2 | n/a | import sys |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | def pack(width, data): |
---|
6 | n/a | return b''.join(v.to_bytes(width, sys.byteorder, signed=True) for v in data) |
---|
7 | n/a | |
---|
8 | n/a | def unpack(width, data): |
---|
9 | n/a | return [int.from_bytes(data[i: i + width], sys.byteorder, signed=True) |
---|
10 | n/a | for i in range(0, len(data), width)] |
---|
11 | n/a | |
---|
12 | n/a | packs = {w: (lambda *data, width=w: pack(width, data)) for w in (1, 2, 3, 4)} |
---|
13 | n/a | maxvalues = {w: (1 << (8 * w - 1)) - 1 for w in (1, 2, 3, 4)} |
---|
14 | n/a | minvalues = {w: -1 << (8 * w - 1) for w in (1, 2, 3, 4)} |
---|
15 | n/a | |
---|
16 | n/a | datas = { |
---|
17 | n/a | 1: b'\x00\x12\x45\xbb\x7f\x80\xff', |
---|
18 | n/a | 2: packs[2](0, 0x1234, 0x4567, -0x4567, 0x7fff, -0x8000, -1), |
---|
19 | n/a | 3: packs[3](0, 0x123456, 0x456789, -0x456789, 0x7fffff, -0x800000, -1), |
---|
20 | n/a | 4: packs[4](0, 0x12345678, 0x456789ab, -0x456789ab, |
---|
21 | n/a | 0x7fffffff, -0x80000000, -1), |
---|
22 | n/a | } |
---|
23 | n/a | |
---|
24 | n/a | INVALID_DATA = [ |
---|
25 | n/a | (b'abc', 0), |
---|
26 | n/a | (b'abc', 2), |
---|
27 | n/a | (b'ab', 3), |
---|
28 | n/a | (b'abc', 4), |
---|
29 | n/a | ] |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | class TestAudioop(unittest.TestCase): |
---|
33 | n/a | |
---|
34 | n/a | def test_max(self): |
---|
35 | n/a | for w in 1, 2, 3, 4: |
---|
36 | n/a | self.assertEqual(audioop.max(b'', w), 0) |
---|
37 | n/a | self.assertEqual(audioop.max(bytearray(), w), 0) |
---|
38 | n/a | self.assertEqual(audioop.max(memoryview(b''), w), 0) |
---|
39 | n/a | p = packs[w] |
---|
40 | n/a | self.assertEqual(audioop.max(p(5), w), 5) |
---|
41 | n/a | self.assertEqual(audioop.max(p(5, -8, -1), w), 8) |
---|
42 | n/a | self.assertEqual(audioop.max(p(maxvalues[w]), w), maxvalues[w]) |
---|
43 | n/a | self.assertEqual(audioop.max(p(minvalues[w]), w), -minvalues[w]) |
---|
44 | n/a | self.assertEqual(audioop.max(datas[w], w), -minvalues[w]) |
---|
45 | n/a | |
---|
46 | n/a | def test_minmax(self): |
---|
47 | n/a | for w in 1, 2, 3, 4: |
---|
48 | n/a | self.assertEqual(audioop.minmax(b'', w), |
---|
49 | n/a | (0x7fffffff, -0x80000000)) |
---|
50 | n/a | self.assertEqual(audioop.minmax(bytearray(), w), |
---|
51 | n/a | (0x7fffffff, -0x80000000)) |
---|
52 | n/a | self.assertEqual(audioop.minmax(memoryview(b''), w), |
---|
53 | n/a | (0x7fffffff, -0x80000000)) |
---|
54 | n/a | p = packs[w] |
---|
55 | n/a | self.assertEqual(audioop.minmax(p(5), w), (5, 5)) |
---|
56 | n/a | self.assertEqual(audioop.minmax(p(5, -8, -1), w), (-8, 5)) |
---|
57 | n/a | self.assertEqual(audioop.minmax(p(maxvalues[w]), w), |
---|
58 | n/a | (maxvalues[w], maxvalues[w])) |
---|
59 | n/a | self.assertEqual(audioop.minmax(p(minvalues[w]), w), |
---|
60 | n/a | (minvalues[w], minvalues[w])) |
---|
61 | n/a | self.assertEqual(audioop.minmax(datas[w], w), |
---|
62 | n/a | (minvalues[w], maxvalues[w])) |
---|
63 | n/a | |
---|
64 | n/a | def test_maxpp(self): |
---|
65 | n/a | for w in 1, 2, 3, 4: |
---|
66 | n/a | self.assertEqual(audioop.maxpp(b'', w), 0) |
---|
67 | n/a | self.assertEqual(audioop.maxpp(bytearray(), w), 0) |
---|
68 | n/a | self.assertEqual(audioop.maxpp(memoryview(b''), w), 0) |
---|
69 | n/a | self.assertEqual(audioop.maxpp(packs[w](*range(100)), w), 0) |
---|
70 | n/a | self.assertEqual(audioop.maxpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) |
---|
71 | n/a | self.assertEqual(audioop.maxpp(datas[w], w), |
---|
72 | n/a | maxvalues[w] - minvalues[w]) |
---|
73 | n/a | |
---|
74 | n/a | def test_avg(self): |
---|
75 | n/a | for w in 1, 2, 3, 4: |
---|
76 | n/a | self.assertEqual(audioop.avg(b'', w), 0) |
---|
77 | n/a | self.assertEqual(audioop.avg(bytearray(), w), 0) |
---|
78 | n/a | self.assertEqual(audioop.avg(memoryview(b''), w), 0) |
---|
79 | n/a | p = packs[w] |
---|
80 | n/a | self.assertEqual(audioop.avg(p(5), w), 5) |
---|
81 | n/a | self .assertEqual(audioop.avg(p(5, 8), w), 6) |
---|
82 | n/a | self.assertEqual(audioop.avg(p(5, -8), w), -2) |
---|
83 | n/a | self.assertEqual(audioop.avg(p(maxvalues[w], maxvalues[w]), w), |
---|
84 | n/a | maxvalues[w]) |
---|
85 | n/a | self.assertEqual(audioop.avg(p(minvalues[w], minvalues[w]), w), |
---|
86 | n/a | minvalues[w]) |
---|
87 | n/a | self.assertEqual(audioop.avg(packs[4](0x50000000, 0x70000000), 4), |
---|
88 | n/a | 0x60000000) |
---|
89 | n/a | self.assertEqual(audioop.avg(packs[4](-0x50000000, -0x70000000), 4), |
---|
90 | n/a | -0x60000000) |
---|
91 | n/a | |
---|
92 | n/a | def test_avgpp(self): |
---|
93 | n/a | for w in 1, 2, 3, 4: |
---|
94 | n/a | self.assertEqual(audioop.avgpp(b'', w), 0) |
---|
95 | n/a | self.assertEqual(audioop.avgpp(bytearray(), w), 0) |
---|
96 | n/a | self.assertEqual(audioop.avgpp(memoryview(b''), w), 0) |
---|
97 | n/a | self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) |
---|
98 | n/a | self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) |
---|
99 | n/a | self.assertEqual(audioop.avgpp(datas[1], 1), 196) |
---|
100 | n/a | self.assertEqual(audioop.avgpp(datas[2], 2), 50534) |
---|
101 | n/a | self.assertEqual(audioop.avgpp(datas[3], 3), 12937096) |
---|
102 | n/a | self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002) |
---|
103 | n/a | |
---|
104 | n/a | def test_rms(self): |
---|
105 | n/a | for w in 1, 2, 3, 4: |
---|
106 | n/a | self.assertEqual(audioop.rms(b'', w), 0) |
---|
107 | n/a | self.assertEqual(audioop.rms(bytearray(), w), 0) |
---|
108 | n/a | self.assertEqual(audioop.rms(memoryview(b''), w), 0) |
---|
109 | n/a | p = packs[w] |
---|
110 | n/a | self.assertEqual(audioop.rms(p(*range(100)), w), 57) |
---|
111 | n/a | self.assertAlmostEqual(audioop.rms(p(maxvalues[w]) * 5, w), |
---|
112 | n/a | maxvalues[w], delta=1) |
---|
113 | n/a | self.assertAlmostEqual(audioop.rms(p(minvalues[w]) * 5, w), |
---|
114 | n/a | -minvalues[w], delta=1) |
---|
115 | n/a | self.assertEqual(audioop.rms(datas[1], 1), 77) |
---|
116 | n/a | self.assertEqual(audioop.rms(datas[2], 2), 20001) |
---|
117 | n/a | self.assertEqual(audioop.rms(datas[3], 3), 5120523) |
---|
118 | n/a | self.assertEqual(audioop.rms(datas[4], 4), 1310854152) |
---|
119 | n/a | |
---|
120 | n/a | def test_cross(self): |
---|
121 | n/a | for w in 1, 2, 3, 4: |
---|
122 | n/a | self.assertEqual(audioop.cross(b'', w), -1) |
---|
123 | n/a | self.assertEqual(audioop.cross(bytearray(), w), -1) |
---|
124 | n/a | self.assertEqual(audioop.cross(memoryview(b''), w), -1) |
---|
125 | n/a | p = packs[w] |
---|
126 | n/a | self.assertEqual(audioop.cross(p(0, 1, 2), w), 0) |
---|
127 | n/a | self.assertEqual(audioop.cross(p(1, 2, -3, -4), w), 1) |
---|
128 | n/a | self.assertEqual(audioop.cross(p(-1, -2, 3, 4), w), 1) |
---|
129 | n/a | self.assertEqual(audioop.cross(p(0, minvalues[w]), w), 1) |
---|
130 | n/a | self.assertEqual(audioop.cross(p(minvalues[w], maxvalues[w]), w), 1) |
---|
131 | n/a | |
---|
132 | n/a | def test_add(self): |
---|
133 | n/a | for w in 1, 2, 3, 4: |
---|
134 | n/a | self.assertEqual(audioop.add(b'', b'', w), b'') |
---|
135 | n/a | self.assertEqual(audioop.add(bytearray(), bytearray(), w), b'') |
---|
136 | n/a | self.assertEqual(audioop.add(memoryview(b''), memoryview(b''), w), b'') |
---|
137 | n/a | self.assertEqual(audioop.add(datas[w], b'\0' * len(datas[w]), w), |
---|
138 | n/a | datas[w]) |
---|
139 | n/a | self.assertEqual(audioop.add(datas[1], datas[1], 1), |
---|
140 | n/a | b'\x00\x24\x7f\x80\x7f\x80\xfe') |
---|
141 | n/a | self.assertEqual(audioop.add(datas[2], datas[2], 2), |
---|
142 | n/a | packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) |
---|
143 | n/a | self.assertEqual(audioop.add(datas[3], datas[3], 3), |
---|
144 | n/a | packs[3](0, 0x2468ac, 0x7fffff, -0x800000, |
---|
145 | n/a | 0x7fffff, -0x800000, -2)) |
---|
146 | n/a | self.assertEqual(audioop.add(datas[4], datas[4], 4), |
---|
147 | n/a | packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, |
---|
148 | n/a | 0x7fffffff, -0x80000000, -2)) |
---|
149 | n/a | |
---|
150 | n/a | def test_bias(self): |
---|
151 | n/a | for w in 1, 2, 3, 4: |
---|
152 | n/a | for bias in 0, 1, -1, 127, -128, 0x7fffffff, -0x80000000: |
---|
153 | n/a | self.assertEqual(audioop.bias(b'', w, bias), b'') |
---|
154 | n/a | self.assertEqual(audioop.bias(bytearray(), w, bias), b'') |
---|
155 | n/a | self.assertEqual(audioop.bias(memoryview(b''), w, bias), b'') |
---|
156 | n/a | self.assertEqual(audioop.bias(datas[1], 1, 1), |
---|
157 | n/a | b'\x01\x13\x46\xbc\x80\x81\x00') |
---|
158 | n/a | self.assertEqual(audioop.bias(datas[1], 1, -1), |
---|
159 | n/a | b'\xff\x11\x44\xba\x7e\x7f\xfe') |
---|
160 | n/a | self.assertEqual(audioop.bias(datas[1], 1, 0x7fffffff), |
---|
161 | n/a | b'\xff\x11\x44\xba\x7e\x7f\xfe') |
---|
162 | n/a | self.assertEqual(audioop.bias(datas[1], 1, -0x80000000), |
---|
163 | n/a | datas[1]) |
---|
164 | n/a | self.assertEqual(audioop.bias(datas[2], 2, 1), |
---|
165 | n/a | packs[2](1, 0x1235, 0x4568, -0x4566, -0x8000, -0x7fff, 0)) |
---|
166 | n/a | self.assertEqual(audioop.bias(datas[2], 2, -1), |
---|
167 | n/a | packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) |
---|
168 | n/a | self.assertEqual(audioop.bias(datas[2], 2, 0x7fffffff), |
---|
169 | n/a | packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) |
---|
170 | n/a | self.assertEqual(audioop.bias(datas[2], 2, -0x80000000), |
---|
171 | n/a | datas[2]) |
---|
172 | n/a | self.assertEqual(audioop.bias(datas[3], 3, 1), |
---|
173 | n/a | packs[3](1, 0x123457, 0x45678a, -0x456788, |
---|
174 | n/a | -0x800000, -0x7fffff, 0)) |
---|
175 | n/a | self.assertEqual(audioop.bias(datas[3], 3, -1), |
---|
176 | n/a | packs[3](-1, 0x123455, 0x456788, -0x45678a, |
---|
177 | n/a | 0x7ffffe, 0x7fffff, -2)) |
---|
178 | n/a | self.assertEqual(audioop.bias(datas[3], 3, 0x7fffffff), |
---|
179 | n/a | packs[3](-1, 0x123455, 0x456788, -0x45678a, |
---|
180 | n/a | 0x7ffffe, 0x7fffff, -2)) |
---|
181 | n/a | self.assertEqual(audioop.bias(datas[3], 3, -0x80000000), |
---|
182 | n/a | datas[3]) |
---|
183 | n/a | self.assertEqual(audioop.bias(datas[4], 4, 1), |
---|
184 | n/a | packs[4](1, 0x12345679, 0x456789ac, -0x456789aa, |
---|
185 | n/a | -0x80000000, -0x7fffffff, 0)) |
---|
186 | n/a | self.assertEqual(audioop.bias(datas[4], 4, -1), |
---|
187 | n/a | packs[4](-1, 0x12345677, 0x456789aa, -0x456789ac, |
---|
188 | n/a | 0x7ffffffe, 0x7fffffff, -2)) |
---|
189 | n/a | self.assertEqual(audioop.bias(datas[4], 4, 0x7fffffff), |
---|
190 | n/a | packs[4](0x7fffffff, -0x6dcba989, -0x3a987656, 0x3a987654, |
---|
191 | n/a | -2, -1, 0x7ffffffe)) |
---|
192 | n/a | self.assertEqual(audioop.bias(datas[4], 4, -0x80000000), |
---|
193 | n/a | packs[4](-0x80000000, -0x6dcba988, -0x3a987655, 0x3a987655, |
---|
194 | n/a | -1, 0, 0x7fffffff)) |
---|
195 | n/a | |
---|
196 | n/a | def test_lin2lin(self): |
---|
197 | n/a | for w in 1, 2, 3, 4: |
---|
198 | n/a | self.assertEqual(audioop.lin2lin(datas[w], w, w), datas[w]) |
---|
199 | n/a | self.assertEqual(audioop.lin2lin(bytearray(datas[w]), w, w), |
---|
200 | n/a | datas[w]) |
---|
201 | n/a | self.assertEqual(audioop.lin2lin(memoryview(datas[w]), w, w), |
---|
202 | n/a | datas[w]) |
---|
203 | n/a | |
---|
204 | n/a | self.assertEqual(audioop.lin2lin(datas[1], 1, 2), |
---|
205 | n/a | packs[2](0, 0x1200, 0x4500, -0x4500, 0x7f00, -0x8000, -0x100)) |
---|
206 | n/a | self.assertEqual(audioop.lin2lin(datas[1], 1, 3), |
---|
207 | n/a | packs[3](0, 0x120000, 0x450000, -0x450000, |
---|
208 | n/a | 0x7f0000, -0x800000, -0x10000)) |
---|
209 | n/a | self.assertEqual(audioop.lin2lin(datas[1], 1, 4), |
---|
210 | n/a | packs[4](0, 0x12000000, 0x45000000, -0x45000000, |
---|
211 | n/a | 0x7f000000, -0x80000000, -0x1000000)) |
---|
212 | n/a | self.assertEqual(audioop.lin2lin(datas[2], 2, 1), |
---|
213 | n/a | b'\x00\x12\x45\xba\x7f\x80\xff') |
---|
214 | n/a | self.assertEqual(audioop.lin2lin(datas[2], 2, 3), |
---|
215 | n/a | packs[3](0, 0x123400, 0x456700, -0x456700, |
---|
216 | n/a | 0x7fff00, -0x800000, -0x100)) |
---|
217 | n/a | self.assertEqual(audioop.lin2lin(datas[2], 2, 4), |
---|
218 | n/a | packs[4](0, 0x12340000, 0x45670000, -0x45670000, |
---|
219 | n/a | 0x7fff0000, -0x80000000, -0x10000)) |
---|
220 | n/a | self.assertEqual(audioop.lin2lin(datas[3], 3, 1), |
---|
221 | n/a | b'\x00\x12\x45\xba\x7f\x80\xff') |
---|
222 | n/a | self.assertEqual(audioop.lin2lin(datas[3], 3, 2), |
---|
223 | n/a | packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1)) |
---|
224 | n/a | self.assertEqual(audioop.lin2lin(datas[3], 3, 4), |
---|
225 | n/a | packs[4](0, 0x12345600, 0x45678900, -0x45678900, |
---|
226 | n/a | 0x7fffff00, -0x80000000, -0x100)) |
---|
227 | n/a | self.assertEqual(audioop.lin2lin(datas[4], 4, 1), |
---|
228 | n/a | b'\x00\x12\x45\xba\x7f\x80\xff') |
---|
229 | n/a | self.assertEqual(audioop.lin2lin(datas[4], 4, 2), |
---|
230 | n/a | packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1)) |
---|
231 | n/a | self.assertEqual(audioop.lin2lin(datas[4], 4, 3), |
---|
232 | n/a | packs[3](0, 0x123456, 0x456789, -0x45678a, |
---|
233 | n/a | 0x7fffff, -0x800000, -1)) |
---|
234 | n/a | |
---|
235 | n/a | def test_adpcm2lin(self): |
---|
236 | n/a | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 1, None), |
---|
237 | n/a | (b'\x00\x00\x00\xff\x00\xff', (-179, 40))) |
---|
238 | n/a | self.assertEqual(audioop.adpcm2lin(bytearray(b'\x07\x7f\x7f'), 1, None), |
---|
239 | n/a | (b'\x00\x00\x00\xff\x00\xff', (-179, 40))) |
---|
240 | n/a | self.assertEqual(audioop.adpcm2lin(memoryview(b'\x07\x7f\x7f'), 1, None), |
---|
241 | n/a | (b'\x00\x00\x00\xff\x00\xff', (-179, 40))) |
---|
242 | n/a | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 2, None), |
---|
243 | n/a | (packs[2](0, 0xb, 0x29, -0x16, 0x72, -0xb3), (-179, 40))) |
---|
244 | n/a | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 3, None), |
---|
245 | n/a | (packs[3](0, 0xb00, 0x2900, -0x1600, 0x7200, |
---|
246 | n/a | -0xb300), (-179, 40))) |
---|
247 | n/a | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 4, None), |
---|
248 | n/a | (packs[4](0, 0xb0000, 0x290000, -0x160000, 0x720000, |
---|
249 | n/a | -0xb30000), (-179, 40))) |
---|
250 | n/a | |
---|
251 | n/a | # Very cursory test |
---|
252 | n/a | for w in 1, 2, 3, 4: |
---|
253 | n/a | self.assertEqual(audioop.adpcm2lin(b'\0' * 5, w, None), |
---|
254 | n/a | (b'\0' * w * 10, (0, 0))) |
---|
255 | n/a | |
---|
256 | n/a | def test_lin2adpcm(self): |
---|
257 | n/a | self.assertEqual(audioop.lin2adpcm(datas[1], 1, None), |
---|
258 | n/a | (b'\x07\x7f\x7f', (-221, 39))) |
---|
259 | n/a | self.assertEqual(audioop.lin2adpcm(bytearray(datas[1]), 1, None), |
---|
260 | n/a | (b'\x07\x7f\x7f', (-221, 39))) |
---|
261 | n/a | self.assertEqual(audioop.lin2adpcm(memoryview(datas[1]), 1, None), |
---|
262 | n/a | (b'\x07\x7f\x7f', (-221, 39))) |
---|
263 | n/a | for w in 2, 3, 4: |
---|
264 | n/a | self.assertEqual(audioop.lin2adpcm(datas[w], w, None), |
---|
265 | n/a | (b'\x07\x7f\x7f', (31, 39))) |
---|
266 | n/a | |
---|
267 | n/a | # Very cursory test |
---|
268 | n/a | for w in 1, 2, 3, 4: |
---|
269 | n/a | self.assertEqual(audioop.lin2adpcm(b'\0' * w * 10, w, None), |
---|
270 | n/a | (b'\0' * 5, (0, 0))) |
---|
271 | n/a | |
---|
272 | n/a | def test_invalid_adpcm_state(self): |
---|
273 | n/a | # state must be a tuple or None, not an integer |
---|
274 | n/a | self.assertRaises(TypeError, audioop.adpcm2lin, b'\0', 1, 555) |
---|
275 | n/a | self.assertRaises(TypeError, audioop.lin2adpcm, b'\0', 1, 555) |
---|
276 | n/a | # Issues #24456, #24457: index out of range |
---|
277 | n/a | self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0, -1)) |
---|
278 | n/a | self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0, 89)) |
---|
279 | n/a | self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0, -1)) |
---|
280 | n/a | self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0, 89)) |
---|
281 | n/a | # value out of range |
---|
282 | n/a | self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (-0x8001, 0)) |
---|
283 | n/a | self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0x8000, 0)) |
---|
284 | n/a | self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (-0x8001, 0)) |
---|
285 | n/a | self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0x8000, 0)) |
---|
286 | n/a | |
---|
287 | n/a | def test_lin2alaw(self): |
---|
288 | n/a | self.assertEqual(audioop.lin2alaw(datas[1], 1), |
---|
289 | n/a | b'\xd5\x87\xa4\x24\xaa\x2a\x5a') |
---|
290 | n/a | self.assertEqual(audioop.lin2alaw(bytearray(datas[1]), 1), |
---|
291 | n/a | b'\xd5\x87\xa4\x24\xaa\x2a\x5a') |
---|
292 | n/a | self.assertEqual(audioop.lin2alaw(memoryview(datas[1]), 1), |
---|
293 | n/a | b'\xd5\x87\xa4\x24\xaa\x2a\x5a') |
---|
294 | n/a | for w in 2, 3, 4: |
---|
295 | n/a | self.assertEqual(audioop.lin2alaw(datas[w], w), |
---|
296 | n/a | b'\xd5\x87\xa4\x24\xaa\x2a\x55') |
---|
297 | n/a | |
---|
298 | n/a | def test_alaw2lin(self): |
---|
299 | n/a | encoded = b'\x00\x03\x24\x2a\x51\x54\x55\x58\x6b\x71\x7f'\ |
---|
300 | n/a | b'\x80\x83\xa4\xaa\xd1\xd4\xd5\xd8\xeb\xf1\xff' |
---|
301 | n/a | src = [-688, -720, -2240, -4032, -9, -3, -1, -27, -244, -82, -106, |
---|
302 | n/a | 688, 720, 2240, 4032, 9, 3, 1, 27, 244, 82, 106] |
---|
303 | n/a | for w in 1, 2, 3, 4: |
---|
304 | n/a | decoded = packs[w](*(x << (w * 8) >> 13 for x in src)) |
---|
305 | n/a | self.assertEqual(audioop.alaw2lin(encoded, w), decoded) |
---|
306 | n/a | self.assertEqual(audioop.alaw2lin(bytearray(encoded), w), decoded) |
---|
307 | n/a | self.assertEqual(audioop.alaw2lin(memoryview(encoded), w), decoded) |
---|
308 | n/a | |
---|
309 | n/a | encoded = bytes(range(256)) |
---|
310 | n/a | for w in 2, 3, 4: |
---|
311 | n/a | decoded = audioop.alaw2lin(encoded, w) |
---|
312 | n/a | self.assertEqual(audioop.lin2alaw(decoded, w), encoded) |
---|
313 | n/a | |
---|
314 | n/a | def test_lin2ulaw(self): |
---|
315 | n/a | self.assertEqual(audioop.lin2ulaw(datas[1], 1), |
---|
316 | n/a | b'\xff\xad\x8e\x0e\x80\x00\x67') |
---|
317 | n/a | self.assertEqual(audioop.lin2ulaw(bytearray(datas[1]), 1), |
---|
318 | n/a | b'\xff\xad\x8e\x0e\x80\x00\x67') |
---|
319 | n/a | self.assertEqual(audioop.lin2ulaw(memoryview(datas[1]), 1), |
---|
320 | n/a | b'\xff\xad\x8e\x0e\x80\x00\x67') |
---|
321 | n/a | for w in 2, 3, 4: |
---|
322 | n/a | self.assertEqual(audioop.lin2ulaw(datas[w], w), |
---|
323 | n/a | b'\xff\xad\x8e\x0e\x80\x00\x7e') |
---|
324 | n/a | |
---|
325 | n/a | def test_ulaw2lin(self): |
---|
326 | n/a | encoded = b'\x00\x0e\x28\x3f\x57\x6a\x76\x7c\x7e\x7f'\ |
---|
327 | n/a | b'\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff' |
---|
328 | n/a | src = [-8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0, |
---|
329 | n/a | 8031, 4447, 1471, 495, 163, 53, 18, 6, 2, 0] |
---|
330 | n/a | for w in 1, 2, 3, 4: |
---|
331 | n/a | decoded = packs[w](*(x << (w * 8) >> 14 for x in src)) |
---|
332 | n/a | self.assertEqual(audioop.ulaw2lin(encoded, w), decoded) |
---|
333 | n/a | self.assertEqual(audioop.ulaw2lin(bytearray(encoded), w), decoded) |
---|
334 | n/a | self.assertEqual(audioop.ulaw2lin(memoryview(encoded), w), decoded) |
---|
335 | n/a | |
---|
336 | n/a | # Current u-law implementation has two codes fo 0: 0x7f and 0xff. |
---|
337 | n/a | encoded = bytes(range(127)) + bytes(range(128, 256)) |
---|
338 | n/a | for w in 2, 3, 4: |
---|
339 | n/a | decoded = audioop.ulaw2lin(encoded, w) |
---|
340 | n/a | self.assertEqual(audioop.lin2ulaw(decoded, w), encoded) |
---|
341 | n/a | |
---|
342 | n/a | def test_mul(self): |
---|
343 | n/a | for w in 1, 2, 3, 4: |
---|
344 | n/a | self.assertEqual(audioop.mul(b'', w, 2), b'') |
---|
345 | n/a | self.assertEqual(audioop.mul(bytearray(), w, 2), b'') |
---|
346 | n/a | self.assertEqual(audioop.mul(memoryview(b''), w, 2), b'') |
---|
347 | n/a | self.assertEqual(audioop.mul(datas[w], w, 0), |
---|
348 | n/a | b'\0' * len(datas[w])) |
---|
349 | n/a | self.assertEqual(audioop.mul(datas[w], w, 1), |
---|
350 | n/a | datas[w]) |
---|
351 | n/a | self.assertEqual(audioop.mul(datas[1], 1, 2), |
---|
352 | n/a | b'\x00\x24\x7f\x80\x7f\x80\xfe') |
---|
353 | n/a | self.assertEqual(audioop.mul(datas[2], 2, 2), |
---|
354 | n/a | packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) |
---|
355 | n/a | self.assertEqual(audioop.mul(datas[3], 3, 2), |
---|
356 | n/a | packs[3](0, 0x2468ac, 0x7fffff, -0x800000, |
---|
357 | n/a | 0x7fffff, -0x800000, -2)) |
---|
358 | n/a | self.assertEqual(audioop.mul(datas[4], 4, 2), |
---|
359 | n/a | packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, |
---|
360 | n/a | 0x7fffffff, -0x80000000, -2)) |
---|
361 | n/a | |
---|
362 | n/a | def test_ratecv(self): |
---|
363 | n/a | for w in 1, 2, 3, 4: |
---|
364 | n/a | self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), |
---|
365 | n/a | (b'', (-1, ((0, 0),)))) |
---|
366 | n/a | self.assertEqual(audioop.ratecv(bytearray(), w, 1, 8000, 8000, None), |
---|
367 | n/a | (b'', (-1, ((0, 0),)))) |
---|
368 | n/a | self.assertEqual(audioop.ratecv(memoryview(b''), w, 1, 8000, 8000, None), |
---|
369 | n/a | (b'', (-1, ((0, 0),)))) |
---|
370 | n/a | self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), |
---|
371 | n/a | (b'', (-1, ((0, 0),) * 5))) |
---|
372 | n/a | self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), |
---|
373 | n/a | (b'', (-2, ((0, 0),)))) |
---|
374 | n/a | self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], |
---|
375 | n/a | datas[w]) |
---|
376 | n/a | self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 1, 0)[0], |
---|
377 | n/a | datas[w]) |
---|
378 | n/a | |
---|
379 | n/a | state = None |
---|
380 | n/a | d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) |
---|
381 | n/a | d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) |
---|
382 | n/a | self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') |
---|
383 | n/a | |
---|
384 | n/a | for w in 1, 2, 3, 4: |
---|
385 | n/a | d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) |
---|
386 | n/a | d, state = b'', None |
---|
387 | n/a | for i in range(0, len(datas[w]), w): |
---|
388 | n/a | d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, |
---|
389 | n/a | 8000, 16000, state) |
---|
390 | n/a | d += d1 |
---|
391 | n/a | self.assertEqual(d, d0) |
---|
392 | n/a | self.assertEqual(state, state0) |
---|
393 | n/a | |
---|
394 | n/a | expected = { |
---|
395 | n/a | 1: packs[1](0, 0x0d, 0x37, -0x26, 0x55, -0x4b, -0x14), |
---|
396 | n/a | 2: packs[2](0, 0x0da7, 0x3777, -0x2630, 0x5673, -0x4a64, -0x129a), |
---|
397 | n/a | 3: packs[3](0, 0x0da740, 0x377776, -0x262fca, |
---|
398 | n/a | 0x56740c, -0x4a62fd, -0x1298c0), |
---|
399 | n/a | 4: packs[4](0, 0x0da740da, 0x37777776, -0x262fc962, |
---|
400 | n/a | 0x56740da6, -0x4a62fc96, -0x1298bf26), |
---|
401 | n/a | } |
---|
402 | n/a | for w in 1, 2, 3, 4: |
---|
403 | n/a | self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 3, 1)[0], |
---|
404 | n/a | expected[w]) |
---|
405 | n/a | self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0], |
---|
406 | n/a | expected[w]) |
---|
407 | n/a | |
---|
408 | n/a | def test_reverse(self): |
---|
409 | n/a | for w in 1, 2, 3, 4: |
---|
410 | n/a | self.assertEqual(audioop.reverse(b'', w), b'') |
---|
411 | n/a | self.assertEqual(audioop.reverse(bytearray(), w), b'') |
---|
412 | n/a | self.assertEqual(audioop.reverse(memoryview(b''), w), b'') |
---|
413 | n/a | self.assertEqual(audioop.reverse(packs[w](0, 1, 2), w), |
---|
414 | n/a | packs[w](2, 1, 0)) |
---|
415 | n/a | |
---|
416 | n/a | def test_tomono(self): |
---|
417 | n/a | for w in 1, 2, 3, 4: |
---|
418 | n/a | data1 = datas[w] |
---|
419 | n/a | data2 = bytearray(2 * len(data1)) |
---|
420 | n/a | for k in range(w): |
---|
421 | n/a | data2[k::2*w] = data1[k::w] |
---|
422 | n/a | self.assertEqual(audioop.tomono(data2, w, 1, 0), data1) |
---|
423 | n/a | self.assertEqual(audioop.tomono(data2, w, 0, 1), b'\0' * len(data1)) |
---|
424 | n/a | for k in range(w): |
---|
425 | n/a | data2[k+w::2*w] = data1[k::w] |
---|
426 | n/a | self.assertEqual(audioop.tomono(data2, w, 0.5, 0.5), data1) |
---|
427 | n/a | self.assertEqual(audioop.tomono(bytearray(data2), w, 0.5, 0.5), |
---|
428 | n/a | data1) |
---|
429 | n/a | self.assertEqual(audioop.tomono(memoryview(data2), w, 0.5, 0.5), |
---|
430 | n/a | data1) |
---|
431 | n/a | |
---|
432 | n/a | def test_tostereo(self): |
---|
433 | n/a | for w in 1, 2, 3, 4: |
---|
434 | n/a | data1 = datas[w] |
---|
435 | n/a | data2 = bytearray(2 * len(data1)) |
---|
436 | n/a | for k in range(w): |
---|
437 | n/a | data2[k::2*w] = data1[k::w] |
---|
438 | n/a | self.assertEqual(audioop.tostereo(data1, w, 1, 0), data2) |
---|
439 | n/a | self.assertEqual(audioop.tostereo(data1, w, 0, 0), b'\0' * len(data2)) |
---|
440 | n/a | for k in range(w): |
---|
441 | n/a | data2[k+w::2*w] = data1[k::w] |
---|
442 | n/a | self.assertEqual(audioop.tostereo(data1, w, 1, 1), data2) |
---|
443 | n/a | self.assertEqual(audioop.tostereo(bytearray(data1), w, 1, 1), data2) |
---|
444 | n/a | self.assertEqual(audioop.tostereo(memoryview(data1), w, 1, 1), |
---|
445 | n/a | data2) |
---|
446 | n/a | |
---|
447 | n/a | def test_findfactor(self): |
---|
448 | n/a | self.assertEqual(audioop.findfactor(datas[2], datas[2]), 1.0) |
---|
449 | n/a | self.assertEqual(audioop.findfactor(bytearray(datas[2]), |
---|
450 | n/a | bytearray(datas[2])), 1.0) |
---|
451 | n/a | self.assertEqual(audioop.findfactor(memoryview(datas[2]), |
---|
452 | n/a | memoryview(datas[2])), 1.0) |
---|
453 | n/a | self.assertEqual(audioop.findfactor(b'\0' * len(datas[2]), datas[2]), |
---|
454 | n/a | 0.0) |
---|
455 | n/a | |
---|
456 | n/a | def test_findfit(self): |
---|
457 | n/a | self.assertEqual(audioop.findfit(datas[2], datas[2]), (0, 1.0)) |
---|
458 | n/a | self.assertEqual(audioop.findfit(bytearray(datas[2]), |
---|
459 | n/a | bytearray(datas[2])), (0, 1.0)) |
---|
460 | n/a | self.assertEqual(audioop.findfit(memoryview(datas[2]), |
---|
461 | n/a | memoryview(datas[2])), (0, 1.0)) |
---|
462 | n/a | self.assertEqual(audioop.findfit(datas[2], packs[2](1, 2, 0)), |
---|
463 | n/a | (1, 8038.8)) |
---|
464 | n/a | self.assertEqual(audioop.findfit(datas[2][:-2] * 5 + datas[2], datas[2]), |
---|
465 | n/a | (30, 1.0)) |
---|
466 | n/a | |
---|
467 | n/a | def test_findmax(self): |
---|
468 | n/a | self.assertEqual(audioop.findmax(datas[2], 1), 5) |
---|
469 | n/a | self.assertEqual(audioop.findmax(bytearray(datas[2]), 1), 5) |
---|
470 | n/a | self.assertEqual(audioop.findmax(memoryview(datas[2]), 1), 5) |
---|
471 | n/a | |
---|
472 | n/a | def test_getsample(self): |
---|
473 | n/a | for w in 1, 2, 3, 4: |
---|
474 | n/a | data = packs[w](0, 1, -1, maxvalues[w], minvalues[w]) |
---|
475 | n/a | self.assertEqual(audioop.getsample(data, w, 0), 0) |
---|
476 | n/a | self.assertEqual(audioop.getsample(bytearray(data), w, 0), 0) |
---|
477 | n/a | self.assertEqual(audioop.getsample(memoryview(data), w, 0), 0) |
---|
478 | n/a | self.assertEqual(audioop.getsample(data, w, 1), 1) |
---|
479 | n/a | self.assertEqual(audioop.getsample(data, w, 2), -1) |
---|
480 | n/a | self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w]) |
---|
481 | n/a | self.assertEqual(audioop.getsample(data, w, 4), minvalues[w]) |
---|
482 | n/a | |
---|
483 | n/a | def test_byteswap(self): |
---|
484 | n/a | swapped_datas = { |
---|
485 | n/a | 1: datas[1], |
---|
486 | n/a | 2: packs[2](0, 0x3412, 0x6745, -0x6646, -0x81, 0x80, -1), |
---|
487 | n/a | 3: packs[3](0, 0x563412, -0x7698bb, 0x7798ba, -0x81, 0x80, -1), |
---|
488 | n/a | 4: packs[4](0, 0x78563412, -0x547698bb, 0x557698ba, |
---|
489 | n/a | -0x81, 0x80, -1), |
---|
490 | n/a | } |
---|
491 | n/a | for w in 1, 2, 3, 4: |
---|
492 | n/a | self.assertEqual(audioop.byteswap(b'', w), b'') |
---|
493 | n/a | self.assertEqual(audioop.byteswap(datas[w], w), swapped_datas[w]) |
---|
494 | n/a | self.assertEqual(audioop.byteswap(swapped_datas[w], w), datas[w]) |
---|
495 | n/a | self.assertEqual(audioop.byteswap(bytearray(datas[w]), w), |
---|
496 | n/a | swapped_datas[w]) |
---|
497 | n/a | self.assertEqual(audioop.byteswap(memoryview(datas[w]), w), |
---|
498 | n/a | swapped_datas[w]) |
---|
499 | n/a | |
---|
500 | n/a | def test_negativelen(self): |
---|
501 | n/a | # from issue 3306, previously it segfaulted |
---|
502 | n/a | self.assertRaises(audioop.error, |
---|
503 | n/a | audioop.findmax, bytes(range(256)), -2392392) |
---|
504 | n/a | |
---|
505 | n/a | def test_issue7673(self): |
---|
506 | n/a | state = None |
---|
507 | n/a | for data, size in INVALID_DATA: |
---|
508 | n/a | size2 = size |
---|
509 | n/a | self.assertRaises(audioop.error, audioop.getsample, data, size, 0) |
---|
510 | n/a | self.assertRaises(audioop.error, audioop.max, data, size) |
---|
511 | n/a | self.assertRaises(audioop.error, audioop.minmax, data, size) |
---|
512 | n/a | self.assertRaises(audioop.error, audioop.avg, data, size) |
---|
513 | n/a | self.assertRaises(audioop.error, audioop.rms, data, size) |
---|
514 | n/a | self.assertRaises(audioop.error, audioop.avgpp, data, size) |
---|
515 | n/a | self.assertRaises(audioop.error, audioop.maxpp, data, size) |
---|
516 | n/a | self.assertRaises(audioop.error, audioop.cross, data, size) |
---|
517 | n/a | self.assertRaises(audioop.error, audioop.mul, data, size, 1.0) |
---|
518 | n/a | self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5) |
---|
519 | n/a | self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5) |
---|
520 | n/a | self.assertRaises(audioop.error, audioop.add, data, data, size) |
---|
521 | n/a | self.assertRaises(audioop.error, audioop.bias, data, size, 0) |
---|
522 | n/a | self.assertRaises(audioop.error, audioop.reverse, data, size) |
---|
523 | n/a | self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2) |
---|
524 | n/a | self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state) |
---|
525 | n/a | self.assertRaises(audioop.error, audioop.lin2ulaw, data, size) |
---|
526 | n/a | self.assertRaises(audioop.error, audioop.lin2alaw, data, size) |
---|
527 | n/a | self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state) |
---|
528 | n/a | |
---|
529 | n/a | def test_string(self): |
---|
530 | n/a | data = 'abcd' |
---|
531 | n/a | size = 2 |
---|
532 | n/a | self.assertRaises(TypeError, audioop.getsample, data, size, 0) |
---|
533 | n/a | self.assertRaises(TypeError, audioop.max, data, size) |
---|
534 | n/a | self.assertRaises(TypeError, audioop.minmax, data, size) |
---|
535 | n/a | self.assertRaises(TypeError, audioop.avg, data, size) |
---|
536 | n/a | self.assertRaises(TypeError, audioop.rms, data, size) |
---|
537 | n/a | self.assertRaises(TypeError, audioop.avgpp, data, size) |
---|
538 | n/a | self.assertRaises(TypeError, audioop.maxpp, data, size) |
---|
539 | n/a | self.assertRaises(TypeError, audioop.cross, data, size) |
---|
540 | n/a | self.assertRaises(TypeError, audioop.mul, data, size, 1.0) |
---|
541 | n/a | self.assertRaises(TypeError, audioop.tomono, data, size, 0.5, 0.5) |
---|
542 | n/a | self.assertRaises(TypeError, audioop.tostereo, data, size, 0.5, 0.5) |
---|
543 | n/a | self.assertRaises(TypeError, audioop.add, data, data, size) |
---|
544 | n/a | self.assertRaises(TypeError, audioop.bias, data, size, 0) |
---|
545 | n/a | self.assertRaises(TypeError, audioop.reverse, data, size) |
---|
546 | n/a | self.assertRaises(TypeError, audioop.lin2lin, data, size, size) |
---|
547 | n/a | self.assertRaises(TypeError, audioop.ratecv, data, size, 1, 1, 1, None) |
---|
548 | n/a | self.assertRaises(TypeError, audioop.lin2ulaw, data, size) |
---|
549 | n/a | self.assertRaises(TypeError, audioop.lin2alaw, data, size) |
---|
550 | n/a | self.assertRaises(TypeError, audioop.lin2adpcm, data, size, None) |
---|
551 | n/a | |
---|
552 | n/a | def test_wrongsize(self): |
---|
553 | n/a | data = b'abcdefgh' |
---|
554 | n/a | state = None |
---|
555 | n/a | for size in (-1, 0, 5, 1024): |
---|
556 | n/a | self.assertRaises(audioop.error, audioop.ulaw2lin, data, size) |
---|
557 | n/a | self.assertRaises(audioop.error, audioop.alaw2lin, data, size) |
---|
558 | n/a | self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state) |
---|
559 | n/a | |
---|
560 | n/a | if __name__ == '__main__': |
---|
561 | n/a | unittest.main() |
---|