Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 1 | import audioop |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 2 | import sys |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | from test.support import run_unittest |
| 5 | |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 6 | def pack(width, data): |
| 7 | return b''.join(v.to_bytes(width, sys.byteorder, signed=True) for v in data) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 8 | |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 9 | packs = {w: (lambda *data, width=w: pack(width, data)) for w in (1, 2, 4)} |
| 10 | maxvalues = {w: (1 << (8 * w - 1)) - 1 for w in (1, 2, 4)} |
| 11 | minvalues = {w: -1 << (8 * w - 1) for w in (1, 2, 4)} |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 12 | |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 13 | datas = { |
| 14 | 1: b'\x00\x12\x45\xbb\x7f\x80\xff', |
| 15 | 2: packs[2](0, 0x1234, 0x4567, -0x4567, 0x7fff, -0x8000, -1), |
| 16 | 4: packs[4](0, 0x12345678, 0x456789ab, -0x456789ab, |
| 17 | 0x7fffffff, -0x80000000, -1), |
| 18 | } |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 19 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 20 | INVALID_DATA = [ |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 21 | (b'abc', 0), |
| 22 | (b'abc', 2), |
| 23 | (b'abc', 4), |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 24 | ] |
| 25 | |
Guido van Rossum | 4fdb744 | 1996-12-20 03:13:06 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 27 | class TestAudioop(unittest.TestCase): |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 28 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 29 | def test_max(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 30 | for w in 1, 2, 4: |
| 31 | self.assertEqual(audioop.max(b'', w), 0) |
| 32 | p = packs[w] |
| 33 | self.assertEqual(audioop.max(p(5), w), 5) |
| 34 | self.assertEqual(audioop.max(p(5, -8, -1), w), 8) |
| 35 | self.assertEqual(audioop.max(p(maxvalues[w]), w), maxvalues[w]) |
| 36 | self.assertEqual(audioop.max(p(minvalues[w]), w), -minvalues[w]) |
| 37 | self.assertEqual(audioop.max(datas[w], w), -minvalues[w]) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 38 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 39 | def test_minmax(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 40 | for w in 1, 2, 4: |
| 41 | self.assertEqual(audioop.minmax(b'', w), |
| 42 | (0x7fffffff, -0x80000000)) |
| 43 | p = packs[w] |
| 44 | self.assertEqual(audioop.minmax(p(5), w), (5, 5)) |
| 45 | self.assertEqual(audioop.minmax(p(5, -8, -1), w), (-8, 5)) |
| 46 | self.assertEqual(audioop.minmax(p(maxvalues[w]), w), |
| 47 | (maxvalues[w], maxvalues[w])) |
| 48 | self.assertEqual(audioop.minmax(p(minvalues[w]), w), |
| 49 | (minvalues[w], minvalues[w])) |
| 50 | self.assertEqual(audioop.minmax(datas[w], w), |
| 51 | (minvalues[w], maxvalues[w])) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 53 | def test_maxpp(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 54 | for w in 1, 2, 4: |
| 55 | self.assertEqual(audioop.maxpp(b'', w), 0) |
| 56 | self.assertEqual(audioop.maxpp(packs[w](*range(100)), w), 0) |
| 57 | self.assertEqual(audioop.maxpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) |
| 58 | self.assertEqual(audioop.maxpp(datas[w], w), |
| 59 | maxvalues[w] - minvalues[w]) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 60 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 61 | def test_avg(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 62 | for w in 1, 2, 4: |
| 63 | self.assertEqual(audioop.avg(b'', w), 0) |
| 64 | p = packs[w] |
| 65 | self.assertEqual(audioop.avg(p(5), w), 5) |
| 66 | self .assertEqual(audioop.avg(p(5, 8), w), 6) |
| 67 | self.assertEqual(audioop.avg(p(5, -8), w), -2) |
| 68 | self.assertEqual(audioop.avg(p(maxvalues[w], maxvalues[w]), w), |
| 69 | maxvalues[w]) |
| 70 | self.assertEqual(audioop.avg(p(minvalues[w], minvalues[w]), w), |
| 71 | minvalues[w]) |
| 72 | self.assertEqual(audioop.avg(packs[4](0x50000000, 0x70000000), 4), |
| 73 | 0x60000000) |
| 74 | self.assertEqual(audioop.avg(packs[4](-0x50000000, -0x70000000), 4), |
| 75 | -0x60000000) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 76 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 77 | def test_avgpp(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 78 | for w in 1, 2, 4: |
| 79 | self.assertEqual(audioop.avgpp(b'', w), 0) |
| 80 | self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0) |
| 81 | self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10) |
| 82 | self.assertEqual(audioop.avgpp(datas[1], 1), 196) |
| 83 | self.assertEqual(audioop.avgpp(datas[2], 2), 50534) |
| 84 | self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 86 | def test_rms(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 87 | for w in 1, 2, 4: |
| 88 | self.assertEqual(audioop.rms(b'', w), 0) |
| 89 | p = packs[w] |
| 90 | self.assertEqual(audioop.rms(p(*range(100)), w), 57) |
| 91 | self.assertAlmostEqual(audioop.rms(p(maxvalues[w]) * 5, w), |
| 92 | maxvalues[w], delta=1) |
| 93 | self.assertAlmostEqual(audioop.rms(p(minvalues[w]) * 5, w), |
| 94 | -minvalues[w], delta=1) |
| 95 | self.assertEqual(audioop.rms(datas[1], 1), 77) |
| 96 | self.assertEqual(audioop.rms(datas[2], 2), 20001) |
| 97 | self.assertEqual(audioop.rms(datas[4], 4), 1310854152) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 98 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 99 | def test_cross(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 100 | for w in 1, 2, 4: |
| 101 | self.assertEqual(audioop.cross(b'', w), -1) |
| 102 | p = packs[w] |
| 103 | self.assertEqual(audioop.cross(p(0, 1, 2), w), 0) |
| 104 | self.assertEqual(audioop.cross(p(1, 2, -3, -4), w), 1) |
| 105 | self.assertEqual(audioop.cross(p(-1, -2, 3, 4), w), 1) |
| 106 | self.assertEqual(audioop.cross(p(0, minvalues[w]), w), 1) |
| 107 | self.assertEqual(audioop.cross(p(minvalues[w], maxvalues[w]), w), 1) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 109 | def test_add(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 110 | for w in 1, 2, 4: |
| 111 | self.assertEqual(audioop.add(b'', b'', w), b'') |
| 112 | self.assertEqual(audioop.add(datas[w], b'\0' * len(datas[w]), w), |
| 113 | datas[w]) |
| 114 | self.assertEqual(audioop.add(datas[1], datas[1], 1), |
| 115 | b'\x00\x24\x7f\x80\x7f\x80\xfe') |
| 116 | self.assertEqual(audioop.add(datas[2], datas[2], 2), |
| 117 | packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) |
| 118 | self.assertEqual(audioop.add(datas[4], datas[4], 4), |
| 119 | packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, |
| 120 | 0x7fffffff, -0x80000000, -2)) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 121 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 122 | def test_bias(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 123 | for w in 1, 2, 4: |
| 124 | for bias in 0, 1, -1, 127, -128, 0x7fffffff, -0x80000000: |
| 125 | self.assertEqual(audioop.bias(b'', w, bias), b'') |
| 126 | self.assertEqual(audioop.bias(datas[1], 1, 1), |
| 127 | b'\x01\x13\x46\xbc\x80\x81\x00') |
| 128 | self.assertEqual(audioop.bias(datas[1], 1, -1), |
| 129 | b'\xff\x11\x44\xba\x7e\x7f\xfe') |
| 130 | self.assertEqual(audioop.bias(datas[1], 1, 0x7fffffff), |
| 131 | b'\xff\x11\x44\xba\x7e\x7f\xfe') |
| 132 | self.assertEqual(audioop.bias(datas[1], 1, -0x80000000), |
| 133 | datas[1]) |
| 134 | self.assertEqual(audioop.bias(datas[2], 2, 1), |
| 135 | packs[2](1, 0x1235, 0x4568, -0x4566, -0x8000, -0x7fff, 0)) |
| 136 | self.assertEqual(audioop.bias(datas[2], 2, -1), |
| 137 | packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) |
| 138 | self.assertEqual(audioop.bias(datas[2], 2, 0x7fffffff), |
| 139 | packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2)) |
| 140 | self.assertEqual(audioop.bias(datas[2], 2, -0x80000000), |
| 141 | datas[2]) |
| 142 | self.assertEqual(audioop.bias(datas[4], 4, 1), |
| 143 | packs[4](1, 0x12345679, 0x456789ac, -0x456789aa, |
| 144 | -0x80000000, -0x7fffffff, 0)) |
| 145 | self.assertEqual(audioop.bias(datas[4], 4, -1), |
| 146 | packs[4](-1, 0x12345677, 0x456789aa, -0x456789ac, |
| 147 | 0x7ffffffe, 0x7fffffff, -2)) |
| 148 | self.assertEqual(audioop.bias(datas[4], 4, 0x7fffffff), |
| 149 | packs[4](0x7fffffff, -0x6dcba989, -0x3a987656, 0x3a987654, |
| 150 | -2, -1, 0x7ffffffe)) |
| 151 | self.assertEqual(audioop.bias(datas[4], 4, -0x80000000), |
| 152 | packs[4](-0x80000000, -0x6dcba988, -0x3a987655, 0x3a987655, |
| 153 | -1, 0, 0x7fffffff)) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 154 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 155 | def test_lin2lin(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 156 | for w in 1, 2, 4: |
| 157 | self.assertEqual(audioop.lin2lin(datas[w], w, w), datas[w]) |
| 158 | |
| 159 | self.assertEqual(audioop.lin2lin(datas[1], 1, 2), |
| 160 | packs[2](0, 0x1200, 0x4500, -0x4500, 0x7f00, -0x8000, -0x100)) |
| 161 | self.assertEqual(audioop.lin2lin(datas[1], 1, 4), |
| 162 | packs[4](0, 0x12000000, 0x45000000, -0x45000000, |
| 163 | 0x7f000000, -0x80000000, -0x1000000)) |
| 164 | self.assertEqual(audioop.lin2lin(datas[2], 2, 1), |
| 165 | b'\x00\x12\x45\xba\x7f\x80\xff') |
| 166 | self.assertEqual(audioop.lin2lin(datas[2], 2, 4), |
| 167 | packs[4](0, 0x12340000, 0x45670000, -0x45670000, |
| 168 | 0x7fff0000, -0x80000000, -0x10000)) |
| 169 | self.assertEqual(audioop.lin2lin(datas[4], 4, 1), |
| 170 | b'\x00\x12\x45\xba\x7f\x80\xff') |
| 171 | self.assertEqual(audioop.lin2lin(datas[4], 4, 2), |
| 172 | packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 173 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 174 | def test_adpcm2lin(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 175 | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 1, None), |
| 176 | (b'\x00\x00\x00\xff\x00\xff', (-179, 40))) |
| 177 | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 2, None), |
| 178 | (packs[2](0, 0xb, 0x29, -0x16, 0x72, -0xb3), (-179, 40))) |
| 179 | self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 4, None), |
| 180 | (packs[4](0, 0xb0000, 0x290000, -0x160000, 0x720000, |
| 181 | -0xb30000), (-179, 40))) |
| 182 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 183 | # Very cursory test |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 184 | for w in 1, 2, 4: |
| 185 | self.assertEqual(audioop.adpcm2lin(b'\0' * 5, w, None), |
| 186 | (b'\0' * w * 10, (0, 0))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 187 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 188 | def test_lin2adpcm(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 189 | self.assertEqual(audioop.lin2adpcm(datas[1], 1, None), |
| 190 | (b'\x07\x7f\x7f', (-221, 39))) |
| 191 | self.assertEqual(audioop.lin2adpcm(datas[2], 2, None), |
| 192 | (b'\x07\x7f\x7f', (31, 39))) |
| 193 | self.assertEqual(audioop.lin2adpcm(datas[4], 4, None), |
| 194 | (b'\x07\x7f\x7f', (31, 39))) |
| 195 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 196 | # Very cursory test |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 197 | for w in 1, 2, 4: |
| 198 | self.assertEqual(audioop.lin2adpcm(b'\0' * w * 10, w, None), |
| 199 | (b'\0' * 5, (0, 0))) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 201 | def test_lin2alaw(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 202 | self.assertEqual(audioop.lin2alaw(datas[1], 1), |
| 203 | b'\xd5\x87\xa4\x24\xaa\x2a\x5a') |
| 204 | self.assertEqual(audioop.lin2alaw(datas[2], 2), |
| 205 | b'\xd5\x87\xa4\x24\xaa\x2a\x55') |
| 206 | self.assertEqual(audioop.lin2alaw(datas[4], 4), |
| 207 | b'\xd5\x87\xa4\x24\xaa\x2a\x55') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 209 | def test_alaw2lin(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 210 | encoded = b'\x00\x03\x24\x2a\x51\x54\x55\x58\x6b\x71\x7f'\ |
| 211 | b'\x80\x83\xa4\xaa\xd1\xd4\xd5\xd8\xeb\xf1\xff' |
| 212 | src = [-688, -720, -2240, -4032, -9, -3, -1, -27, -244, -82, -106, |
| 213 | 688, 720, 2240, 4032, 9, 3, 1, 27, 244, 82, 106] |
| 214 | for w in 1, 2, 4: |
| 215 | self.assertEqual(audioop.alaw2lin(encoded, w), |
| 216 | packs[w](*(x << (w * 8) >> 13 for x in src))) |
| 217 | |
| 218 | encoded = bytes(range(256)) |
| 219 | for w in 2, 4: |
| 220 | decoded = audioop.alaw2lin(encoded, w) |
| 221 | self.assertEqual(audioop.lin2alaw(decoded, w), encoded) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 222 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 223 | def test_lin2ulaw(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 224 | self.assertEqual(audioop.lin2ulaw(datas[1], 1), |
| 225 | b'\xff\xad\x8e\x0e\x80\x00\x67') |
| 226 | self.assertEqual(audioop.lin2ulaw(datas[2], 2), |
| 227 | b'\xff\xad\x8e\x0e\x80\x00\x7e') |
| 228 | self.assertEqual(audioop.lin2ulaw(datas[4], 4), |
| 229 | b'\xff\xad\x8e\x0e\x80\x00\x7e') |
Guido van Rossum | c8504e2 | 1997-02-14 15:58:00 +0000 | [diff] [blame] | 230 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 231 | def test_ulaw2lin(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 232 | encoded = b'\x00\x0e\x28\x3f\x57\x6a\x76\x7c\x7e\x7f'\ |
| 233 | b'\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff' |
| 234 | src = [-8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0, |
| 235 | 8031, 4447, 1471, 495, 163, 53, 18, 6, 2, 0] |
| 236 | for w in 1, 2, 4: |
| 237 | self.assertEqual(audioop.ulaw2lin(encoded, w), |
| 238 | packs[w](*(x << (w * 8) >> 14 for x in src))) |
| 239 | |
| 240 | # Current u-law implementation has two codes fo 0: 0x7f and 0xff. |
| 241 | encoded = bytes(range(127)) + bytes(range(128, 256)) |
| 242 | for w in 2, 4: |
| 243 | decoded = audioop.ulaw2lin(encoded, w) |
| 244 | self.assertEqual(audioop.lin2ulaw(decoded, w), encoded) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 245 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 246 | def test_mul(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 247 | for w in 1, 2, 4: |
| 248 | self.assertEqual(audioop.mul(b'', w, 2), b'') |
| 249 | self.assertEqual(audioop.mul(datas[w], w, 0), |
| 250 | b'\0' * len(datas[w])) |
| 251 | self.assertEqual(audioop.mul(datas[w], w, 1), |
| 252 | datas[w]) |
| 253 | self.assertEqual(audioop.mul(datas[1], 1, 2), |
| 254 | b'\x00\x24\x7f\x80\x7f\x80\xfe') |
| 255 | self.assertEqual(audioop.mul(datas[2], 2, 2), |
| 256 | packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2)) |
| 257 | self.assertEqual(audioop.mul(datas[4], 4, 2), |
| 258 | packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000, |
| 259 | 0x7fffffff, -0x80000000, -2)) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 260 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 261 | def test_ratecv(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 262 | for w in 1, 2, 4: |
| 263 | self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None), |
| 264 | (b'', (-1, ((0, 0),)))) |
| 265 | self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None), |
| 266 | (b'', (-1, ((0, 0),) * 5))) |
| 267 | self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None), |
| 268 | (b'', (-2, ((0, 0),)))) |
| 269 | self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0], |
| 270 | datas[w]) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 271 | state = None |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 272 | d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) |
| 273 | d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 274 | self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 275 | |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 276 | for w in 1, 2, 4: |
| 277 | d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None) |
| 278 | d, state = b'', None |
| 279 | for i in range(0, len(datas[w]), w): |
| 280 | d1, state = audioop.ratecv(datas[w][i:i + w], w, 1, |
| 281 | 8000, 16000, state) |
| 282 | d += d1 |
| 283 | self.assertEqual(d, d0) |
| 284 | self.assertEqual(state, state0) |
| 285 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 286 | def test_reverse(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 287 | for w in 1, 2, 4: |
| 288 | self.assertEqual(audioop.reverse(b'', w), b'') |
| 289 | self.assertEqual(audioop.reverse(packs[w](0, 1, 2), w), |
| 290 | packs[w](2, 1, 0)) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 291 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 292 | def test_tomono(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 293 | for w in 1, 2, 4: |
| 294 | data1 = datas[w] |
| 295 | data2 = bytearray(2 * len(data1)) |
| 296 | for k in range(w): |
| 297 | data2[k::2*w] = data1[k::w] |
| 298 | self.assertEqual(audioop.tomono(data2, w, 1, 0), data1) |
| 299 | self.assertEqual(audioop.tomono(data2, w, 0, 1), b'\0' * len(data1)) |
| 300 | for k in range(w): |
| 301 | data2[k+w::2*w] = data1[k::w] |
| 302 | self.assertEqual(audioop.tomono(data2, w, 0.5, 0.5), data1) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 303 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 304 | def test_tostereo(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 305 | for w in 1, 2, 4: |
| 306 | data1 = datas[w] |
| 307 | data2 = bytearray(2 * len(data1)) |
| 308 | for k in range(w): |
| 309 | data2[k::2*w] = data1[k::w] |
| 310 | self.assertEqual(audioop.tostereo(data1, w, 1, 0), data2) |
| 311 | self.assertEqual(audioop.tostereo(data1, w, 0, 0), b'\0' * len(data2)) |
| 312 | for k in range(w): |
| 313 | data2[k+w::2*w] = data1[k::w] |
| 314 | self.assertEqual(audioop.tostereo(data1, w, 1, 1), data2) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 315 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 316 | def test_findfactor(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 317 | self.assertEqual(audioop.findfactor(datas[2], datas[2]), 1.0) |
| 318 | self.assertEqual(audioop.findfactor(b'\0' * len(datas[2]), datas[2]), |
| 319 | 0.0) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 320 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 321 | def test_findfit(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 322 | self.assertEqual(audioop.findfit(datas[2], datas[2]), (0, 1.0)) |
| 323 | self.assertEqual(audioop.findfit(datas[2], packs[2](1, 2, 0)), |
| 324 | (1, 8038.8)) |
| 325 | self.assertEqual(audioop.findfit(datas[2][:-2] * 5 + datas[2], datas[2]), |
| 326 | (30, 1.0)) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 327 | |
| 328 | def test_findmax(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 329 | self.assertEqual(audioop.findmax(datas[2], 1), 5) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 330 | |
| 331 | def test_getsample(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 332 | for w in 1, 2, 4: |
| 333 | data = packs[w](0, 1, -1, maxvalues[w], minvalues[w]) |
| 334 | self.assertEqual(audioop.getsample(data, w, 0), 0) |
| 335 | self.assertEqual(audioop.getsample(data, w, 1), 1) |
| 336 | self.assertEqual(audioop.getsample(data, w, 2), -1) |
| 337 | self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w]) |
| 338 | self.assertEqual(audioop.getsample(data, w, 4), minvalues[w]) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 339 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 340 | def test_negativelen(self): |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 341 | # from issue 3306, previously it segfaulted |
| 342 | self.assertRaises(audioop.error, |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 343 | audioop.findmax, bytes(range(256)), -2392392) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 344 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 345 | def test_issue7673(self): |
| 346 | state = None |
| 347 | for data, size in INVALID_DATA: |
| 348 | size2 = size |
| 349 | self.assertRaises(audioop.error, audioop.getsample, data, size, 0) |
| 350 | self.assertRaises(audioop.error, audioop.max, data, size) |
| 351 | self.assertRaises(audioop.error, audioop.minmax, data, size) |
| 352 | self.assertRaises(audioop.error, audioop.avg, data, size) |
| 353 | self.assertRaises(audioop.error, audioop.rms, data, size) |
| 354 | self.assertRaises(audioop.error, audioop.avgpp, data, size) |
| 355 | self.assertRaises(audioop.error, audioop.maxpp, data, size) |
| 356 | self.assertRaises(audioop.error, audioop.cross, data, size) |
| 357 | self.assertRaises(audioop.error, audioop.mul, data, size, 1.0) |
| 358 | self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5) |
| 359 | self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5) |
| 360 | self.assertRaises(audioop.error, audioop.add, data, data, size) |
| 361 | self.assertRaises(audioop.error, audioop.bias, data, size, 0) |
| 362 | self.assertRaises(audioop.error, audioop.reverse, data, size) |
| 363 | self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2) |
| 364 | self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state) |
| 365 | self.assertRaises(audioop.error, audioop.lin2ulaw, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 366 | self.assertRaises(audioop.error, audioop.lin2alaw, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 367 | self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state) |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 368 | |
| 369 | def test_wrongsize(self): |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 370 | data = b'abcdefgh' |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 371 | state = None |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 372 | for size in (-1, 0, 3, 5, 1024): |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 373 | self.assertRaises(audioop.error, audioop.ulaw2lin, data, size) |
| 374 | self.assertRaises(audioop.error, audioop.alaw2lin, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 375 | self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state) |
| 376 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 377 | def test_main(): |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 378 | run_unittest(TestAudioop) |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 379 | |
| 380 | if __name__ == '__main__': |
| 381 | test_main() |