blob: 440adabd57f4708f2d6b951845a54af878aa4a16 [file] [log] [blame]
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00001# Test audioop.
2import audioop
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test.test_support import verbose
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00004
5def gendata1():
Roger E. Masse7a61f871997-01-03 19:20:17 +00006 return '\0\1\2'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00007
8def gendata2():
Roger E. Masse7a61f871997-01-03 19:20:17 +00009 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000010 print 'getsample'
Roger E. Masse7a61f871997-01-03 19:20:17 +000011 if audioop.getsample('\0\1', 2, 0) == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +000012 return '\0\0\0\1\0\2'
Roger E. Masse7a61f871997-01-03 19:20:17 +000013 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000014 return '\0\0\1\0\2\0'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000015
16def gendata4():
Roger E. Masse7a61f871997-01-03 19:20:17 +000017 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000018 print 'getsample'
Roger E. Masse7a61f871997-01-03 19:20:17 +000019 if audioop.getsample('\0\0\0\1', 4, 0) == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +000020 return '\0\0\0\0\0\0\0\1\0\0\0\2'
Roger E. Masse7a61f871997-01-03 19:20:17 +000021 else:
Guido van Rossum41360a41998-03-26 19:42:58 +000022 return '\0\0\0\0\1\0\0\0\2\0\0\0'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000023
24def testmax(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000025 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000026 print 'max'
Fred Drake132dce22000-12-12 23:11:42 +000027 if audioop.max(data[0], 1) != 2 or \
28 audioop.max(data[1], 2) != 2 or \
29 audioop.max(data[2], 4) != 2:
Guido van Rossum41360a41998-03-26 19:42:58 +000030 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000031 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000032
Guido van Rossum4fdb7441996-12-20 03:13:06 +000033def testminmax(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000034 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000035 print 'minmax'
Fred Drake132dce22000-12-12 23:11:42 +000036 if audioop.minmax(data[0], 1) != (0, 2) or \
37 audioop.minmax(data[1], 2) != (0, 2) or \
38 audioop.minmax(data[2], 4) != (0, 2):
Guido van Rossum41360a41998-03-26 19:42:58 +000039 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000040 return 1
Guido van Rossum4fdb7441996-12-20 03:13:06 +000041
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000042def testmaxpp(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000043 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000044 print 'maxpp'
Fred Drake132dce22000-12-12 23:11:42 +000045 if audioop.maxpp(data[0], 1) != 0 or \
46 audioop.maxpp(data[1], 2) != 0 or \
47 audioop.maxpp(data[2], 4) != 0:
Guido van Rossum41360a41998-03-26 19:42:58 +000048 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000049 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000050
51def testavg(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000052 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000053 print 'avg'
Fred Drake132dce22000-12-12 23:11:42 +000054 if audioop.avg(data[0], 1) != 1 or \
55 audioop.avg(data[1], 2) != 1 or \
56 audioop.avg(data[2], 4) != 1:
Guido van Rossum41360a41998-03-26 19:42:58 +000057 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000058 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000059
60def testavgpp(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000061 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000062 print 'avgpp'
Fred Drake132dce22000-12-12 23:11:42 +000063 if audioop.avgpp(data[0], 1) != 0 or \
64 audioop.avgpp(data[1], 2) != 0 or \
65 audioop.avgpp(data[2], 4) != 0:
Guido van Rossum41360a41998-03-26 19:42:58 +000066 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000067 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000068
69def testrms(data):
Fred Drake132dce22000-12-12 23:11:42 +000070 if audioop.rms(data[0], 1) != 1 or \
71 audioop.rms(data[1], 2) != 1 or \
72 audioop.rms(data[2], 4) != 1:
Guido van Rossum41360a41998-03-26 19:42:58 +000073 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000074 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000075
76def testcross(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000077 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000078 print 'cross'
Fred Drake132dce22000-12-12 23:11:42 +000079 if audioop.cross(data[0], 1) != 0 or \
80 audioop.cross(data[1], 2) != 0 or \
81 audioop.cross(data[2], 4) != 0:
Guido van Rossum41360a41998-03-26 19:42:58 +000082 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000083 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000084
85def testadd(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +000086 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000087 print 'add'
Roger E. Masse7a61f871997-01-03 19:20:17 +000088 data2 = []
89 for d in data:
Guido van Rossum41360a41998-03-26 19:42:58 +000090 str = ''
91 for s in d:
92 str = str + chr(ord(s)*2)
93 data2.append(str)
Fred Drake132dce22000-12-12 23:11:42 +000094 if audioop.add(data[0], data[0], 1) != data2[0] or \
95 audioop.add(data[1], data[1], 2) != data2[1] or \
96 audioop.add(data[2], data[2], 4) != data2[2]:
Guido van Rossum41360a41998-03-26 19:42:58 +000097 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +000098 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000099
100def testbias(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000101 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000102 print 'bias'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000103 # Note: this test assumes that avg() works
104 d1 = audioop.bias(data[0], 1, 100)
105 d2 = audioop.bias(data[1], 2, 100)
106 d4 = audioop.bias(data[2], 4, 100)
Fred Drake132dce22000-12-12 23:11:42 +0000107 if audioop.avg(d1, 1) != 101 or \
108 audioop.avg(d2, 2) != 101 or \
109 audioop.avg(d4, 4) != 101:
Guido van Rossum41360a41998-03-26 19:42:58 +0000110 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000111 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000112
113def testlin2lin(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000114 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000115 print 'lin2lin'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000116 # too simple: we test only the size
117 for d1 in data:
Guido van Rossum41360a41998-03-26 19:42:58 +0000118 for d2 in data:
Guido van Rossum54e54c62001-09-04 19:14:14 +0000119 got = len(d1)//3
120 wtd = len(d2)//3
Fred Drake132dce22000-12-12 23:11:42 +0000121 if len(audioop.lin2lin(d1, got, wtd)) != len(d2):
Guido van Rossum41360a41998-03-26 19:42:58 +0000122 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000123 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000124
125def testadpcm2lin(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000126 # Very cursory test
Fred Drake132dce22000-12-12 23:11:42 +0000127 if audioop.adpcm2lin('\0\0', 1, None) != ('\0\0\0\0', (0,0)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000128 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000129 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000130
131def testlin2adpcm(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000132 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000133 print 'lin2adpcm'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000134 # Very cursory test
Fred Drake132dce22000-12-12 23:11:42 +0000135 if audioop.lin2adpcm('\0\0\0\0', 1, None) != ('\0\0', (0,0)):
Guido van Rossum41360a41998-03-26 19:42:58 +0000136 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000137 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000138
139def testlin2ulaw(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000140 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000141 print 'lin2ulaw'
Fred Drake132dce22000-12-12 23:11:42 +0000142 if audioop.lin2ulaw(data[0], 1) != '\377\347\333' or \
143 audioop.lin2ulaw(data[1], 2) != '\377\377\377' or \
144 audioop.lin2ulaw(data[2], 4) != '\377\377\377':
Guido van Rossum41360a41998-03-26 19:42:58 +0000145 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000146 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000147
148def testulaw2lin(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000149 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000150 print 'ulaw2lin'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000151 # Cursory
152 d = audioop.lin2ulaw(data[0], 1)
Fred Drake132dce22000-12-12 23:11:42 +0000153 if audioop.ulaw2lin(d, 1) != data[0]:
Guido van Rossum41360a41998-03-26 19:42:58 +0000154 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000155 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000156
157def testmul(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000158 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000159 print 'mul'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000160 data2 = []
161 for d in data:
Guido van Rossum41360a41998-03-26 19:42:58 +0000162 str = ''
163 for s in d:
164 str = str + chr(ord(s)*2)
165 data2.append(str)
Fred Drake132dce22000-12-12 23:11:42 +0000166 if audioop.mul(data[0], 1, 2) != data2[0] or \
167 audioop.mul(data[1],2, 2) != data2[1] or \
168 audioop.mul(data[2], 4, 2) != data2[2]:
Guido van Rossum41360a41998-03-26 19:42:58 +0000169 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000170 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000171
Guido van Rossumc8504e21997-02-14 15:58:00 +0000172def testratecv(data):
173 if verbose:
Guido van Rossum05ba2ab1997-05-21 14:27:20 +0000174 print 'ratecv'
175 state = None
176 d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
177 d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
178 if d1 + d2 != '\000\000\001\001\002\001\000\000\001\001\002':
179 return 0
Guido van Rossumc8504e21997-02-14 15:58:00 +0000180 return 1
181
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000182def testreverse(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000183 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000184 print 'reverse'
Fred Drake132dce22000-12-12 23:11:42 +0000185 if audioop.reverse(data[0], 1) != '\2\1\0':
Guido van Rossum41360a41998-03-26 19:42:58 +0000186 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000187 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000188
189def testtomono(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000190 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000191 print 'tomono'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000192 data2 = ''
193 for d in data[0]:
Guido van Rossum41360a41998-03-26 19:42:58 +0000194 data2 = data2 + d + d
Fred Drake132dce22000-12-12 23:11:42 +0000195 if audioop.tomono(data2, 1, 0.5, 0.5) != data[0]:
Guido van Rossum41360a41998-03-26 19:42:58 +0000196 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000197 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000198
199def testtostereo(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000200 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000201 print 'tostereo'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000202 data2 = ''
203 for d in data[0]:
Guido van Rossum41360a41998-03-26 19:42:58 +0000204 data2 = data2 + d + d
Fred Drake132dce22000-12-12 23:11:42 +0000205 if audioop.tostereo(data[0], 1, 1, 1) != data2:
Guido van Rossum41360a41998-03-26 19:42:58 +0000206 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000207 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000208
209def testfindfactor(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000210 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000211 print 'findfactor'
Fred Drake132dce22000-12-12 23:11:42 +0000212 if audioop.findfactor(data[1], data[1]) != 1.0:
Guido van Rossum41360a41998-03-26 19:42:58 +0000213 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000214 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000215
216def testfindfit(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000217 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000218 print 'findfit'
Fred Drake132dce22000-12-12 23:11:42 +0000219 if audioop.findfit(data[1], data[1]) != (0, 1.0):
Guido van Rossum41360a41998-03-26 19:42:58 +0000220 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000221 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000222
223def testfindmax(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000224 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000225 print 'findmax'
Fred Drake132dce22000-12-12 23:11:42 +0000226 if audioop.findmax(data[1], 1) != 2:
Guido van Rossum41360a41998-03-26 19:42:58 +0000227 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000228 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000229
230def testgetsample(data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000231 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000232 print 'getsample'
Roger E. Masse7a61f871997-01-03 19:20:17 +0000233 for i in range(3):
Fred Drake132dce22000-12-12 23:11:42 +0000234 if audioop.getsample(data[0], 1, i) != i or \
235 audioop.getsample(data[1], 2, i) != i or \
236 audioop.getsample(data[2], 4, i) != i:
Guido van Rossum41360a41998-03-26 19:42:58 +0000237 return 0
Roger E. Masse7a61f871997-01-03 19:20:17 +0000238 return 1
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000239
240def testone(name, data):
Roger E. Masse7a61f871997-01-03 19:20:17 +0000241 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000242 func = eval('test'+name)
Roger E. Masse7a61f871997-01-03 19:20:17 +0000243 except NameError:
Guido van Rossum41360a41998-03-26 19:42:58 +0000244 print 'No test found for audioop.'+name+'()'
245 return
Roger E. Masse7a61f871997-01-03 19:20:17 +0000246 try:
Guido van Rossum41360a41998-03-26 19:42:58 +0000247 rv = func(data)
Roger E. Masse7a61f871997-01-03 19:20:17 +0000248 except 'xx':
Guido van Rossum41360a41998-03-26 19:42:58 +0000249 print 'Test FAILED for audioop.'+name+'() (with an exception)'
250 return
Roger E. Masse7a61f871997-01-03 19:20:17 +0000251 if not rv:
Guido van Rossum41360a41998-03-26 19:42:58 +0000252 print 'Test FAILED for audioop.'+name+'()'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000253
254def testall():
Roger E. Masse7a61f871997-01-03 19:20:17 +0000255 data = [gendata1(), gendata2(), gendata4()]
256 names = dir(audioop)
257 # We know there is a routine 'add'
258 routines = []
259 for n in names:
Guido van Rossum41360a41998-03-26 19:42:58 +0000260 if type(eval('audioop.'+n)) == type(audioop.add):
261 routines.append(n)
Roger E. Masse7a61f871997-01-03 19:20:17 +0000262 for n in routines:
Guido van Rossum41360a41998-03-26 19:42:58 +0000263 testone(n, data)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000264testall()