Location | Tool | Test ID | Function | Issue |
---|---|---|---|---|
/builddir/build/BUILD/Python-2.7.3/Modules/audioop.c:1172:31 | clang-analyzer | Division by zero | ||
/builddir/build/BUILD/Python-2.7.3/Modules/audioop.c:1172:31 | clang-analyzer | Division by zero |
1 /* audioopmodule - Module to detect peak values in arrays */
2
3 #include "Python.h"
4
5 #if SIZEOF_INT == 4
6 typedef int Py_Int32;
7 typedef unsigned int Py_UInt32;
8 #else
9 #if SIZEOF_LONG == 4
10 typedef long Py_Int32;
11 typedef unsigned long Py_UInt32;
12 #else
13 #error "No 4-byte integral type"
14 #endif
15 #endif
16
17 typedef short PyInt16;
18
19 #if defined(__CHAR_UNSIGNED__)
20 #if defined(signed)
21 /* This module currently does not work on systems where only unsigned
22 characters are available. Take it out of Setup. Sorry. */
23 #endif
24 #endif
25
26 /* Code shamelessly stolen from sox, 12.17.7, g711.c
27 ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
28
29 /* From g711.c:
30 *
31 * December 30, 1994:
32 * Functions linear2alaw, linear2ulaw have been updated to correctly
33 * convert unquantized 16 bit values.
34 * Tables for direct u- to A-law and A- to u-law conversions have been
35 * corrected.
36 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
37 * bli@cpk.auc.dk
38 *
39 */
40 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
41 #define CLIP 32635
42 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
43 #define QUANT_MASK (0xf) /* Quantization field mask. */
44 #define SEG_SHIFT (4) /* Left shift for segment number. */
45 #define SEG_MASK (0x70) /* Segment field mask. */
46
47 static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
48 0x1FF, 0x3FF, 0x7FF, 0xFFF};
49 static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
50 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
51
52 static PyInt16
53 search(PyInt16 val, PyInt16 *table, int size)
54 {
55 int i;
56
57 for (i = 0; i < size; i++) {
58 if (val <= *table++)
59 return (i);
60 }
61 return (size);
62 }
63 #define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
64 #define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
65
66 static PyInt16 _st_ulaw2linear16[256] = {
67 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
68 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
69 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
70 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
71 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
72 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
73 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
74 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
75 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
76 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
77 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
78 -1052, -988, -924, -876, -844, -812, -780,
79 -748, -716, -684, -652, -620, -588, -556,
80 -524, -492, -460, -428, -396, -372, -356,
81 -340, -324, -308, -292, -276, -260, -244,
82 -228, -212, -196, -180, -164, -148, -132,
83 -120, -112, -104, -96, -88, -80, -72,
84 -64, -56, -48, -40, -32, -24, -16,
85 -8, 0, 32124, 31100, 30076, 29052, 28028,
86 27004, 25980, 24956, 23932, 22908, 21884, 20860,
87 19836, 18812, 17788, 16764, 15996, 15484, 14972,
88 14460, 13948, 13436, 12924, 12412, 11900, 11388,
89 10876, 10364, 9852, 9340, 8828, 8316, 7932,
90 7676, 7420, 7164, 6908, 6652, 6396, 6140,
91 5884, 5628, 5372, 5116, 4860, 4604, 4348,
92 4092, 3900, 3772, 3644, 3516, 3388, 3260,
93 3132, 3004, 2876, 2748, 2620, 2492, 2364,
94 2236, 2108, 1980, 1884, 1820, 1756, 1692,
95 1628, 1564, 1500, 1436, 1372, 1308, 1244,
96 1180, 1116, 1052, 988, 924, 876, 844,
97 812, 780, 748, 716, 684, 652, 620,
98 588, 556, 524, 492, 460, 428, 396,
99 372, 356, 340, 324, 308, 292, 276,
100 260, 244, 228, 212, 196, 180, 164,
101 148, 132, 120, 112, 104, 96, 88,
102 80, 72, 64, 56, 48, 40, 32,
103 24, 16, 8, 0
104 };
105
106 /*
107 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
108 * stored in a unsigned char. This function should only be called with
109 * the data shifted such that it only contains information in the lower
110 * 14-bits.
111 *
112 * In order to simplify the encoding process, the original linear magnitude
113 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
114 * (33 - 8191). The result can be seen in the following encoding table:
115 *
116 * Biased Linear Input Code Compressed Code
117 * ------------------------ ---------------
118 * 00000001wxyza 000wxyz
119 * 0000001wxyzab 001wxyz
120 * 000001wxyzabc 010wxyz
121 * 00001wxyzabcd 011wxyz
122 * 0001wxyzabcde 100wxyz
123 * 001wxyzabcdef 101wxyz
124 * 01wxyzabcdefg 110wxyz
125 * 1wxyzabcdefgh 111wxyz
126 *
127 * Each biased linear code has a leading 1 which identifies the segment
128 * number. The value of the segment number is equal to 7 minus the number
129 * of leading 0's. The quantization interval is directly available as the
130 * four bits wxyz. * The trailing bits (a - h) are ignored.
131 *
132 * Ordinarily the complement of the resulting code word is used for
133 * transmission, and so the code word is complemented before it is returned.
134 *
135 * For further information see John C. Bellamy's Digital Telephony, 1982,
136 * John Wiley & Sons, pps 98-111 and 472-476.
137 */
138 static unsigned char
139 st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
140 {
141 PyInt16 mask;
142 PyInt16 seg;
143 unsigned char uval;
144
145 /* The original sox code does this in the calling function, not here */
146 pcm_val = pcm_val >> 2;
147
148 /* u-law inverts all bits */
149 /* Get the sign and the magnitude of the value. */
150 if (pcm_val < 0) {
151 pcm_val = -pcm_val;
152 mask = 0x7F;
153 } else {
154 mask = 0xFF;
155 }
156 if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
157 pcm_val += (BIAS >> 2);
158
159 /* Convert the scaled magnitude to segment number. */
160 seg = search(pcm_val, seg_uend, 8);
161
162 /*
163 * Combine the sign, segment, quantization bits;
164 * and complement the code word.
165 */
166 if (seg >= 8) /* out of range, return maximum value. */
167 return (unsigned char) (0x7F ^ mask);
168 else {
169 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
170 return (uval ^ mask);
171 }
172
173 }
174
175 static PyInt16 _st_alaw2linear16[256] = {
176 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
177 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
178 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
179 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
180 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
181 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
182 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
183 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
184 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
185 -13568, -344, -328, -376, -360, -280, -264,
186 -312, -296, -472, -456, -504, -488, -408,
187 -392, -440, -424, -88, -72, -120, -104,
188 -24, -8, -56, -40, -216, -200, -248,
189 -232, -152, -136, -184, -168, -1376, -1312,
190 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
191 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
192 -688, -656, -752, -720, -560, -528, -624,
193 -592, -944, -912, -1008, -976, -816, -784,
194 -880, -848, 5504, 5248, 6016, 5760, 4480,
195 4224, 4992, 4736, 7552, 7296, 8064, 7808,
196 6528, 6272, 7040, 6784, 2752, 2624, 3008,
197 2880, 2240, 2112, 2496, 2368, 3776, 3648,
198 4032, 3904, 3264, 3136, 3520, 3392, 22016,
199 20992, 24064, 23040, 17920, 16896, 19968, 18944,
200 30208, 29184, 32256, 31232, 26112, 25088, 28160,
201 27136, 11008, 10496, 12032, 11520, 8960, 8448,
202 9984, 9472, 15104, 14592, 16128, 15616, 13056,
203 12544, 14080, 13568, 344, 328, 376, 360,
204 280, 264, 312, 296, 472, 456, 504,
205 488, 408, 392, 440, 424, 88, 72,
206 120, 104, 24, 8, 56, 40, 216,
207 200, 248, 232, 152, 136, 184, 168,
208 1376, 1312, 1504, 1440, 1120, 1056, 1248,
209 1184, 1888, 1824, 2016, 1952, 1632, 1568,
210 1760, 1696, 688, 656, 752, 720, 560,
211 528, 624, 592, 944, 912, 1008, 976,
212 816, 784, 880, 848
213 };
214
215 /*
216 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
217 * stored in a unsigned char. This function should only be called with
218 * the data shifted such that it only contains information in the lower
219 * 13-bits.
220 *
221 * Linear Input Code Compressed Code
222 * ------------------------ ---------------
223 * 0000000wxyza 000wxyz
224 * 0000001wxyza 001wxyz
225 * 000001wxyzab 010wxyz
226 * 00001wxyzabc 011wxyz
227 * 0001wxyzabcd 100wxyz
228 * 001wxyzabcde 101wxyz
229 * 01wxyzabcdef 110wxyz
230 * 1wxyzabcdefg 111wxyz
231 *
232 * For further information see John C. Bellamy's Digital Telephony, 1982,
233 * John Wiley & Sons, pps 98-111 and 472-476.
234 */
235 static unsigned char
236 st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
237 {
238 PyInt16 mask;
239 short seg;
240 unsigned char aval;
241
242 /* The original sox code does this in the calling function, not here */
243 pcm_val = pcm_val >> 3;
244
245 /* A-law using even bit inversion */
246 if (pcm_val >= 0) {
247 mask = 0xD5; /* sign (7th) bit = 1 */
248 } else {
249 mask = 0x55; /* sign bit = 0 */
250 pcm_val = -pcm_val - 1;
251 }
252
253 /* Convert the scaled magnitude to segment number. */
254 seg = search(pcm_val, seg_aend, 8);
255
256 /* Combine the sign, segment, and quantization bits. */
257
258 if (seg >= 8) /* out of range, return maximum value. */
259 return (unsigned char) (0x7F ^ mask);
260 else {
261 aval = (unsigned char) seg << SEG_SHIFT;
262 if (seg < 2)
263 aval |= (pcm_val >> 1) & QUANT_MASK;
264 else
265 aval |= (pcm_val >> seg) & QUANT_MASK;
266 return (aval ^ mask);
267 }
268 }
269 /* End of code taken from sox */
270
271 /* Intel ADPCM step variation table */
272 static int indexTable[16] = {
273 -1, -1, -1, -1, 2, 4, 6, 8,
274 -1, -1, -1, -1, 2, 4, 6, 8,
275 };
276
277 static int stepsizeTable[89] = {
278 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
279 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
280 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
281 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
282 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
283 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
284 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
285 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
286 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
287 };
288
289 #define CHARP(cp, i) ((signed char *)(cp+i))
290 #define SHORTP(cp, i) ((short *)(cp+i))
291 #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
292
293
294
295 static PyObject *AudioopError;
296
297 static int
298 audioop_check_size(int size)
299 {
300 if (size != 1 && size != 2 && size != 4) {
301 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
302 return 0;
303 }
304 else
305 return 1;
306 }
307
308 static int
309 audioop_check_parameters(int len, int size)
310 {
311 if (!audioop_check_size(size))
312 return 0;
313 if (len % size != 0) {
314 PyErr_SetString(AudioopError, "not a whole number of frames");
315 return 0;
316 }
317 return 1;
318 }
319
320 static PyObject *
321 audioop_getsample(PyObject *self, PyObject *args)
322 {
323 signed char *cp;
324 int len, size, val = 0;
325 int i;
326
327 if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
328 return 0;
329 if (!audioop_check_parameters(len, size))
330 return NULL;
331 if ( i < 0 || i >= len/size ) {
332 PyErr_SetString(AudioopError, "Index out of range");
333 return 0;
334 }
335 if ( size == 1 ) val = (int)*CHARP(cp, i);
336 else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
337 else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
338 return PyInt_FromLong(val);
339 }
340
341 static PyObject *
342 audioop_max(PyObject *self, PyObject *args)
343 {
344 signed char *cp;
345 int len, size, val = 0;
346 int i;
347 int max = 0;
348
349 if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
350 return 0;
351 if (!audioop_check_parameters(len, size))
352 return NULL;
353 for ( i=0; i<len; i+= size) {
354 if ( size == 1 ) val = (int)*CHARP(cp, i);
355 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
356 else if ( size == 4 ) val = (int)*LONGP(cp, i);
357 if ( val < 0 ) val = (-val);
358 if ( val > max ) max = val;
359 }
360 return PyInt_FromLong(max);
361 }
362
363 static PyObject *
364 audioop_minmax(PyObject *self, PyObject *args)
365 {
366 signed char *cp;
367 int len, size, val = 0;
368 int i;
369 int min = 0x7fffffff, max = -0x7fffffff;
370
371 if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
372 return NULL;
373 if (!audioop_check_parameters(len, size))
374 return NULL;
375 for (i = 0; i < len; i += size) {
376 if (size == 1) val = (int) *CHARP(cp, i);
377 else if (size == 2) val = (int) *SHORTP(cp, i);
378 else if (size == 4) val = (int) *LONGP(cp, i);
379 if (val > max) max = val;
380 if (val < min) min = val;
381 }
382 return Py_BuildValue("(ii)", min, max);
383 }
384
385 static PyObject *
386 audioop_avg(PyObject *self, PyObject *args)
387 {
388 signed char *cp;
389 int len, size, val = 0;
390 int i;
391 double avg = 0.0;
392
393 if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
394 return 0;
395 if (!audioop_check_parameters(len, size))
396 return NULL;
397 for ( i=0; i<len; i+= size) {
398 if ( size == 1 ) val = (int)*CHARP(cp, i);
399 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
400 else if ( size == 4 ) val = (int)*LONGP(cp, i);
401 avg += val;
402 }
403 if ( len == 0 )
404 val = 0;
405 else
406 val = (int)(avg / (double)(len/size));
407 return PyInt_FromLong(val);
408 }
409
410 static PyObject *
411 audioop_rms(PyObject *self, PyObject *args)
412 {
413 signed char *cp;
414 int len, size, val = 0;
415 int i;
416 double sum_squares = 0.0;
417
418 if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
419 return 0;
420 if (!audioop_check_parameters(len, size))
421 return NULL;
422 for ( i=0; i<len; i+= size) {
423 if ( size == 1 ) val = (int)*CHARP(cp, i);
424 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
425 else if ( size == 4 ) val = (int)*LONGP(cp, i);
426 sum_squares += (double)val*(double)val;
427 }
428 if ( len == 0 )
429 val = 0;
430 else
431 val = (int)sqrt(sum_squares / (double)(len/size));
432 return PyInt_FromLong(val);
433 }
434
435 static double _sum2(short *a, short *b, int len)
436 {
437 int i;
438 double sum = 0.0;
439
440 for( i=0; i<len; i++) {
441 sum = sum + (double)a[i]*(double)b[i];
442 }
443 return sum;
444 }
445
446 /*
447 ** Findfit tries to locate a sample within another sample. Its main use
448 ** is in echo-cancellation (to find the feedback of the output signal in
449 ** the input signal).
450 ** The method used is as follows:
451 **
452 ** let R be the reference signal (length n) and A the input signal (length N)
453 ** with N > n, and let all sums be over i from 0 to n-1.
454 **
455 ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
456 ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
457 ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
458 **
459 ** Next, we compute the relative distance between the original signal and
460 ** the modified signal and minimize that over j:
461 ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) =>
462 ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
463 **
464 ** In the code variables correspond as follows:
465 ** cp1 A
466 ** cp2 R
467 ** len1 N
468 ** len2 n
469 ** aj_m1 A[j-1]
470 ** aj_lm1 A[j+n-1]
471 ** sum_ri_2 sum(R[i]^2)
472 ** sum_aij_2 sum(A[i+j]^2)
473 ** sum_aij_ri sum(A[i+j]R[i])
474 **
475 ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
476 ** is completely recalculated each step.
477 */
478 static PyObject *
479 audioop_findfit(PyObject *self, PyObject *args)
480 {
481 short *cp1, *cp2;
482 int len1, len2;
483 int j, best_j;
484 double aj_m1, aj_lm1;
485 double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
486
487 /* Passing a short** for an 's' argument is correct only
488 if the string contents is aligned for interpretation
489 as short[]. Due to the definition of PyStringObject,
490 this is currently (Python 2.6) the case. */
491 if ( !PyArg_ParseTuple(args, "s#s#:findfit",
492 (char**)&cp1, &len1, (char**)&cp2, &len2) )
493 return 0;
494 if ( len1 & 1 || len2 & 1 ) {
495 PyErr_SetString(AudioopError, "Strings should be even-sized");
496 return 0;
497 }
498 len1 >>= 1;
499 len2 >>= 1;
500
501 if ( len1 < len2 ) {
502 PyErr_SetString(AudioopError, "First sample should be longer");
503 return 0;
504 }
505 sum_ri_2 = _sum2(cp2, cp2, len2);
506 sum_aij_2 = _sum2(cp1, cp1, len2);
507 sum_aij_ri = _sum2(cp1, cp2, len2);
508
509 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
510
511 best_result = result;
512 best_j = 0;
513
514 for (j=1; j<=len1-len2; j++) {
515 aj_m1 = (double)cp1[j-1];
516 aj_lm1 = (double)cp1[j+len2-1];
517
518 sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
519 sum_aij_ri = _sum2(cp1+j, cp2, len2);
520
521 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
522 / sum_aij_2;
523
524 if ( result < best_result ) {
525 best_result = result;
526 best_j = j;
527 }
528
529 }
530
531 factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
532
533 return Py_BuildValue("(if)", best_j, factor);
534 }
535
536 /*
537 ** findfactor finds a factor f so that the energy in A-fB is minimal.
538 ** See the comment for findfit for details.
539 */
540 static PyObject *
541 audioop_findfactor(PyObject *self, PyObject *args)
542 {
543 short *cp1, *cp2;
544 int len1, len2;
545 double sum_ri_2, sum_aij_ri, result;
546
547 if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
548 (char**)&cp1, &len1, (char**)&cp2, &len2) )
549 return 0;
550 if ( len1 & 1 || len2 & 1 ) {
551 PyErr_SetString(AudioopError, "Strings should be even-sized");
552 return 0;
553 }
554 if ( len1 != len2 ) {
555 PyErr_SetString(AudioopError, "Samples should be same size");
556 return 0;
557 }
558 len2 >>= 1;
559 sum_ri_2 = _sum2(cp2, cp2, len2);
560 sum_aij_ri = _sum2(cp1, cp2, len2);
561
562 result = sum_aij_ri / sum_ri_2;
563
564 return PyFloat_FromDouble(result);
565 }
566
567 /*
568 ** findmax returns the index of the n-sized segment of the input sample
569 ** that contains the most energy.
570 */
571 static PyObject *
572 audioop_findmax(PyObject *self, PyObject *args)
573 {
574 short *cp1;
575 int len1, len2;
576 int j, best_j;
577 double aj_m1, aj_lm1;
578 double result, best_result;
579
580 if ( !PyArg_ParseTuple(args, "s#i:findmax",
581 (char**)&cp1, &len1, &len2) )
582 return 0;
583 if ( len1 & 1 ) {
584 PyErr_SetString(AudioopError, "Strings should be even-sized");
585 return 0;
586 }
587 len1 >>= 1;
588
589 if ( len2 < 0 || len1 < len2 ) {
590 PyErr_SetString(AudioopError, "Input sample should be longer");
591 return 0;
592 }
593
594 result = _sum2(cp1, cp1, len2);
595
596 best_result = result;
597 best_j = 0;
598
599 for (j=1; j<=len1-len2; j++) {
600 aj_m1 = (double)cp1[j-1];
601 aj_lm1 = (double)cp1[j+len2-1];
602
603 result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
604
605 if ( result > best_result ) {
606 best_result = result;
607 best_j = j;
608 }
609
610 }
611
612 return PyInt_FromLong(best_j);
613 }
614
615 static PyObject *
616 audioop_avgpp(PyObject *self, PyObject *args)
617 {
618 signed char *cp;
619 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
620 prevextreme = 0;
621 int i;
622 double avg = 0.0;
623 int diff, prevdiff, extremediff, nextreme = 0;
624
625 if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
626 return 0;
627 if (!audioop_check_parameters(len, size))
628 return NULL;
629 /* Compute first delta value ahead. Also automatically makes us
630 ** skip the first extreme value
631 */
632 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
633 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
634 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
635 if ( size == 1 ) val = (int)*CHARP(cp, size);
636 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
637 else if ( size == 4 ) val = (int)*LONGP(cp, size);
638 prevdiff = val - prevval;
639
640 for ( i=size; i<len; i+= size) {
641 if ( size == 1 ) val = (int)*CHARP(cp, i);
642 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
643 else if ( size == 4 ) val = (int)*LONGP(cp, i);
644 diff = val - prevval;
645 if ( diff*prevdiff < 0 ) {
646 /* Derivative changed sign. Compute difference to last
647 ** extreme value and remember.
648 */
649 if ( prevextremevalid ) {
650 extremediff = prevval - prevextreme;
651 if ( extremediff < 0 )
652 extremediff = -extremediff;
653 avg += extremediff;
654 nextreme++;
655 }
656 prevextremevalid = 1;
657 prevextreme = prevval;
658 }
659 prevval = val;
660 if ( diff != 0 )
661 prevdiff = diff;
662 }
663 if ( nextreme == 0 )
664 val = 0;
665 else
666 val = (int)(avg / (double)nextreme);
667 return PyInt_FromLong(val);
668 }
669
670 static PyObject *
671 audioop_maxpp(PyObject *self, PyObject *args)
672 {
673 signed char *cp;
674 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
675 prevextreme = 0;
676 int i;
677 int max = 0;
678 int diff, prevdiff, extremediff;
679
680 if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
681 return 0;
682 if (!audioop_check_parameters(len, size))
683 return NULL;
684 /* Compute first delta value ahead. Also automatically makes us
685 ** skip the first extreme value
686 */
687 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
688 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
689 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
690 if ( size == 1 ) val = (int)*CHARP(cp, size);
691 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
692 else if ( size == 4 ) val = (int)*LONGP(cp, size);
693 prevdiff = val - prevval;
694
695 for ( i=size; i<len; i+= size) {
696 if ( size == 1 ) val = (int)*CHARP(cp, i);
697 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
698 else if ( size == 4 ) val = (int)*LONGP(cp, i);
699 diff = val - prevval;
700 if ( diff*prevdiff < 0 ) {
701 /* Derivative changed sign. Compute difference to
702 ** last extreme value and remember.
703 */
704 if ( prevextremevalid ) {
705 extremediff = prevval - prevextreme;
706 if ( extremediff < 0 )
707 extremediff = -extremediff;
708 if ( extremediff > max )
709 max = extremediff;
710 }
711 prevextremevalid = 1;
712 prevextreme = prevval;
713 }
714 prevval = val;
715 if ( diff != 0 )
716 prevdiff = diff;
717 }
718 return PyInt_FromLong(max);
719 }
720
721 static PyObject *
722 audioop_cross(PyObject *self, PyObject *args)
723 {
724 signed char *cp;
725 int len, size, val = 0;
726 int i;
727 int prevval, ncross;
728
729 if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
730 return 0;
731 if (!audioop_check_parameters(len, size))
732 return NULL;
733 ncross = -1;
734 prevval = 17; /* Anything <> 0,1 */
735 for ( i=0; i<len; i+= size) {
736 if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
737 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
738 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
739 val = val & 1;
740 if ( val != prevval ) ncross++;
741 prevval = val;
742 }
743 return PyInt_FromLong(ncross);
744 }
745
746 static PyObject *
747 audioop_mul(PyObject *self, PyObject *args)
748 {
749 signed char *cp, *ncp;
750 int len, size, val = 0;
751 double factor, fval, maxval;
752 PyObject *rv;
753 int i;
754
755 if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
756 return 0;
757 if (!audioop_check_parameters(len, size))
758 return NULL;
759
760 if ( size == 1 ) maxval = (double) 0x7f;
761 else if ( size == 2 ) maxval = (double) 0x7fff;
762 else if ( size == 4 ) maxval = (double) 0x7fffffff;
763 else {
764 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
765 return 0;
766 }
767
768 rv = PyString_FromStringAndSize(NULL, len);
769 if ( rv == 0 )
770 return 0;
771 ncp = (signed char *)PyString_AsString(rv);
772
773
774 for ( i=0; i < len; i += size ) {
775 if ( size == 1 ) val = (int)*CHARP(cp, i);
776 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
777 else if ( size == 4 ) val = (int)*LONGP(cp, i);
778 fval = (double)val*factor;
779 if ( fval > maxval ) fval = maxval;
780 else if ( fval < -maxval ) fval = -maxval;
781 val = (int)fval;
782 if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
783 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
784 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
785 }
786 return rv;
787 }
788
789 static PyObject *
790 audioop_tomono(PyObject *self, PyObject *args)
791 {
792 signed char *cp, *ncp;
793 int len, size, val1 = 0, val2 = 0;
794 double fac1, fac2, fval, maxval;
795 PyObject *rv;
796 int i;
797
798 if ( !PyArg_ParseTuple(args, "s#idd:tomono",
799 &cp, &len, &size, &fac1, &fac2 ) )
800 return 0;
801 if (!audioop_check_parameters(len, size))
802 return NULL;
803 if (((len / size) & 1) != 0) {
804 PyErr_SetString(AudioopError, "not a whole number of frames");
805 return NULL;
806 }
807
808 if ( size == 1 ) maxval = (double) 0x7f;
809 else if ( size == 2 ) maxval = (double) 0x7fff;
810 else if ( size == 4 ) maxval = (double) 0x7fffffff;
811 else {
812 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
813 return 0;
814 }
815
816 rv = PyString_FromStringAndSize(NULL, len/2);
817 if ( rv == 0 )
818 return 0;
819 ncp = (signed char *)PyString_AsString(rv);
820
821
822 for ( i=0; i < len; i += size*2 ) {
823 if ( size == 1 ) val1 = (int)*CHARP(cp, i);
824 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
825 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
826 if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
827 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
828 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
829 fval = (double)val1*fac1 + (double)val2*fac2;
830 if ( fval > maxval ) fval = maxval;
831 else if ( fval < -maxval ) fval = -maxval;
832 val1 = (int)fval;
833 if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
834 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
835 else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
836 }
837 return rv;
838 }
839
840 static PyObject *
841 audioop_tostereo(PyObject *self, PyObject *args)
842 {
843 signed char *cp, *ncp;
844 int len, size, val1, val2, val = 0;
845 double fac1, fac2, fval, maxval;
846 PyObject *rv;
847 int i;
848
849 if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
850 &cp, &len, &size, &fac1, &fac2 ) )
851 return 0;
852 if (!audioop_check_parameters(len, size))
853 return NULL;
854
855 if ( size == 1 ) maxval = (double) 0x7f;
856 else if ( size == 2 ) maxval = (double) 0x7fff;
857 else if ( size == 4 ) maxval = (double) 0x7fffffff;
858 else {
859 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
860 return 0;
861 }
862
863 if (len > INT_MAX/2) {
864 PyErr_SetString(PyExc_MemoryError,
865 "not enough memory for output buffer");
866 return 0;
867 }
868
869 rv = PyString_FromStringAndSize(NULL, len*2);
870 if ( rv == 0 )
871 return 0;
872 ncp = (signed char *)PyString_AsString(rv);
873
874
875 for ( i=0; i < len; i += size ) {
876 if ( size == 1 ) val = (int)*CHARP(cp, i);
877 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
878 else if ( size == 4 ) val = (int)*LONGP(cp, i);
879
880 fval = (double)val*fac1;
881 if ( fval > maxval ) fval = maxval;
882 else if ( fval < -maxval ) fval = -maxval;
883 val1 = (int)fval;
884
885 fval = (double)val*fac2;
886 if ( fval > maxval ) fval = maxval;
887 else if ( fval < -maxval ) fval = -maxval;
888 val2 = (int)fval;
889
890 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
891 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
892 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
893
894 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
895 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
896 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
897 }
898 return rv;
899 }
900
901 static PyObject *
902 audioop_add(PyObject *self, PyObject *args)
903 {
904 signed char *cp1, *cp2, *ncp;
905 int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
906 PyObject *rv;
907 int i;
908
909 if ( !PyArg_ParseTuple(args, "s#s#i:add",
910 &cp1, &len1, &cp2, &len2, &size ) )
911 return 0;
912 if (!audioop_check_parameters(len1, size))
913 return NULL;
914 if ( len1 != len2 ) {
915 PyErr_SetString(AudioopError, "Lengths should be the same");
916 return 0;
917 }
918
919 if ( size == 1 ) maxval = 0x7f;
920 else if ( size == 2 ) maxval = 0x7fff;
921 else if ( size == 4 ) maxval = 0x7fffffff;
922 else {
923 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
924 return 0;
925 }
926
927 rv = PyString_FromStringAndSize(NULL, len1);
928 if ( rv == 0 )
929 return 0;
930 ncp = (signed char *)PyString_AsString(rv);
931
932 for ( i=0; i < len1; i += size ) {
933 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
934 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
935 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
936
937 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
938 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
939 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
940
941 newval = val1 + val2;
942 /* truncate in case of overflow */
943 if (newval > maxval) newval = maxval;
944 else if (newval < -maxval) newval = -maxval;
945 else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
946 newval = val1 > 0 ? maxval : - maxval;
947
948 if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
949 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
950 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
951 }
952 return rv;
953 }
954
955 static PyObject *
956 audioop_bias(PyObject *self, PyObject *args)
957 {
958 signed char *cp, *ncp;
959 int len, size, val = 0;
960 PyObject *rv;
961 int i;
962 int bias;
963
964 if ( !PyArg_ParseTuple(args, "s#ii:bias",
965 &cp, &len, &size , &bias) )
966 return 0;
967
968 if (!audioop_check_parameters(len, size))
969 return NULL;
970
971 rv = PyString_FromStringAndSize(NULL, len);
972 if ( rv == 0 )
973 return 0;
974 ncp = (signed char *)PyString_AsString(rv);
975
976
977 for ( i=0; i < len; i += size ) {
978 if ( size == 1 ) val = (int)*CHARP(cp, i);
979 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
980 else if ( size == 4 ) val = (int)*LONGP(cp, i);
981
982 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
983 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
984 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
985 }
986 return rv;
987 }
988
989 static PyObject *
990 audioop_reverse(PyObject *self, PyObject *args)
991 {
992 signed char *cp;
993 unsigned char *ncp;
994 int len, size, val = 0;
995 PyObject *rv;
996 int i, j;
997
998 if ( !PyArg_ParseTuple(args, "s#i:reverse",
999 &cp, &len, &size) )
1000 return 0;
1001
1002 if (!audioop_check_parameters(len, size))
1003 return NULL;
1004
1005 rv = PyString_FromStringAndSize(NULL, len);
1006 if ( rv == 0 )
1007 return 0;
1008 ncp = (unsigned char *)PyString_AsString(rv);
1009
1010 for ( i=0; i < len; i += size ) {
1011 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1012 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1013 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1014
1015 j = len - i - size;
1016
1017 if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1018 else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
1019 else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1020 }
1021 return rv;
1022 }
1023
1024 static PyObject *
1025 audioop_lin2lin(PyObject *self, PyObject *args)
1026 {
1027 signed char *cp;
1028 unsigned char *ncp;
1029 int len, size, size2, val = 0;
1030 PyObject *rv;
1031 int i, j;
1032
1033 if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
1034 &cp, &len, &size, &size2) )
1035 return 0;
1036
1037 if (!audioop_check_parameters(len, size))
1038 return NULL;
1039 if (!audioop_check_size(size2))
1040 return NULL;
1041
1042 if (len/size > INT_MAX/size2) {
1043 PyErr_SetString(PyExc_MemoryError,
1044 "not enough memory for output buffer");
1045 return 0;
1046 }
1047 rv = PyString_FromStringAndSize(NULL, (len/size)*size2);
1048 if ( rv == 0 )
1049 return 0;
1050 ncp = (unsigned char *)PyString_AsString(rv);
1051
1052 for ( i=0, j=0; i < len; i += size, j += size2 ) {
1053 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1054 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1055 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1056
1057 if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1058 else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
1059 else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1060 }
1061 return rv;
1062 }
1063
1064 static int
1065 gcd(int a, int b)
1066 {
1067 while (b > 0) {
1068 int tmp = a % b;
1069 a = b;
1070 b = tmp;
1071 }
1072 return a;
1073 }
1074
1075 static PyObject *
1076 audioop_ratecv(PyObject *self, PyObject *args)
1077 {
1078 char *cp, *ncp;
1079 int len, size, nchannels, inrate, outrate, weightA, weightB;
1080 int chan, d, *prev_i, *cur_i, cur_o;
1081 PyObject *state, *samps, *str, *rv = NULL;
1082 int bytes_per_frame;
1083
1084 weightA = 1;
1085 weightB = 0;
1086 if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
1087 &nchannels, &inrate, &outrate, &state,
1088 &weightA, &weightB))
1089 return NULL;
1090 if (!audioop_check_size(size))
1091 return NULL;
1092 if (nchannels < 1) {
1093 PyErr_SetString(AudioopError, "# of channels should be >= 1");
1094 return NULL;
1095 }
1096 bytes_per_frame = size * nchannels;
1097 if (bytes_per_frame / nchannels != size) {
1098 /* This overflow test is rigorously correct because
1099 both multiplicands are >= 1. Use the argument names
1100 from the docs for the error msg. */
1101 PyErr_SetString(PyExc_OverflowError,
1102 "width * nchannels too big for a C int");
1103 return NULL;
1104 }
1105 if (weightA < 1 || weightB < 0) {
1106 PyErr_SetString(AudioopError,
1107 "weightA should be >= 1, weightB should be >= 0");
1108 return NULL;
1109 }
1110 if (len % bytes_per_frame != 0) {
1111 PyErr_SetString(AudioopError, "not a whole number of frames");
1112 return NULL;
1113 }
1114 if (inrate <= 0 || outrate <= 0) {
1115 PyErr_SetString(AudioopError, "sampling rate not > 0");
1116 return NULL;
1117 }
1118 /* divide inrate and outrate by their greatest common divisor */
1119 d = gcd(inrate, outrate);
1120 inrate /= d;
1121 outrate /= d;
1122
1123 if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
1124 PyErr_SetString(PyExc_MemoryError,
1125 "not enough memory for output buffer");
1126 return 0;
1127 }
1128 prev_i = (int *) malloc(nchannels * sizeof(int));
1129 cur_i = (int *) malloc(nchannels * sizeof(int));
1130 if (prev_i == NULL || cur_i == NULL) {
1131 (void) PyErr_NoMemory();
1132 goto exit;
1133 }
1134
1135 len /= bytes_per_frame; /* # of frames */
1136
1137 if (state == Py_None) {
1138 d = -outrate;
1139 for (chan = 0; chan < nchannels; chan++)
1140 prev_i[chan] = cur_i[chan] = 0;
1141 }
1142 else {
1143 if (!PyArg_ParseTuple(state,
1144 "iO!;audioop.ratecv: illegal state argument",
1145 &d, &PyTuple_Type, &samps))
1146 goto exit;
1147 if (PyTuple_Size(samps) != nchannels) {
1148 PyErr_SetString(AudioopError,
1149 "illegal state argument");
1150 goto exit;
1151 }
1152 for (chan = 0; chan < nchannels; chan++) {
1153 if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
1154 "ii:ratecv", &prev_i[chan],
1155 &cur_i[chan]))
1156 goto exit;
1157 }
1158 }
1159
1160 /* str <- Space for the output buffer. */
1161 {
1162 /* There are len input frames, so we need (mathematically)
1163 ceiling(len*outrate/inrate) output frames, and each frame
1164 requires bytes_per_frame bytes. Computing this
1165 without spurious overflow is the challenge; we can
1166 settle for a reasonable upper bound, though, in this
1167 case ceiling(len/inrate) * outrate. */
1168
1169 /* compute ceiling(len/inrate) without overflow */
1170 int q = len > 0 ? 1 + (len - 1) / inrate : 0;
1171 if (outrate > INT_MAX / q / bytes_per_frame)
1172 str = NULL;
(emitted by clang-analyzer)TODO: a detailed trace is available in the data model (not yet rendered in this report)
(emitted by clang-analyzer)TODO: a detailed trace is available in the data model (not yet rendered in this report)
1173 else
1174 str = PyString_FromStringAndSize(NULL,
1175 q * outrate * bytes_per_frame);
1176
1177 if (str == NULL) {
1178 PyErr_SetString(PyExc_MemoryError,
1179 "not enough memory for output buffer");
1180 goto exit;
1181 }
1182 }
1183 ncp = PyString_AsString(str);
1184
1185 for (;;) {
1186 while (d < 0) {
1187 if (len == 0) {
1188 samps = PyTuple_New(nchannels);
1189 if (samps == NULL)
1190 goto exit;
1191 for (chan = 0; chan < nchannels; chan++)
1192 PyTuple_SetItem(samps, chan,
1193 Py_BuildValue("(ii)",
1194 prev_i[chan],
1195 cur_i[chan]));
1196 if (PyErr_Occurred())
1197 goto exit;
1198 /* We have checked before that the length
1199 * of the string fits into int. */
1200 len = (int)(ncp - PyString_AsString(str));
1201 if (len == 0) {
1202 /*don't want to resize to zero length*/
1203 rv = PyString_FromStringAndSize("", 0);
1204 Py_DECREF(str);
1205 str = rv;
1206 } else if (_PyString_Resize(&str, len) < 0)
1207 goto exit;
1208 rv = Py_BuildValue("(O(iO))", str, d, samps);
1209 Py_DECREF(samps);
1210 Py_DECREF(str);
1211 goto exit; /* return rv */
1212 }
1213 for (chan = 0; chan < nchannels; chan++) {
1214 prev_i[chan] = cur_i[chan];
1215 if (size == 1)
1216 cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
1217 else if (size == 2)
1218 cur_i[chan] = (int)*SHORTP(cp, 0);
1219 else if (size == 4)
1220 cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
1221 cp += size;
1222 /* implements a simple digital filter */
1223 cur_i[chan] =
1224 (weightA * cur_i[chan] +
1225 weightB * prev_i[chan]) /
1226 (weightA + weightB);
1227 }
1228 len--;
1229 d += outrate;
1230 }
1231 while (d >= 0) {
1232 for (chan = 0; chan < nchannels; chan++) {
1233 cur_o = (prev_i[chan] * d +
1234 cur_i[chan] * (outrate - d)) /
1235 outrate;
1236 if (size == 1)
1237 *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
1238 else if (size == 2)
1239 *SHORTP(ncp, 0) = (short)(cur_o);
1240 else if (size == 4)
1241 *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
1242 ncp += size;
1243 }
1244 d -= inrate;
1245 }
1246 }
1247 exit:
1248 if (prev_i != NULL)
1249 free(prev_i);
1250 if (cur_i != NULL)
1251 free(cur_i);
1252 return rv;
1253 }
1254
1255 static PyObject *
1256 audioop_lin2ulaw(PyObject *self, PyObject *args)
1257 {
1258 signed char *cp;
1259 unsigned char *ncp;
1260 int len, size, val = 0;
1261 PyObject *rv;
1262 int i;
1263
1264 if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
1265 &cp, &len, &size) )
1266 return 0 ;
1267
1268 if (!audioop_check_parameters(len, size))
1269 return NULL;
1270
1271 rv = PyString_FromStringAndSize(NULL, len/size);
1272 if ( rv == 0 )
1273 return 0;
1274 ncp = (unsigned char *)PyString_AsString(rv);
1275
1276 for ( i=0; i < len; i += size ) {
1277 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1278 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1279 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1280
1281 *ncp++ = st_14linear2ulaw(val);
1282 }
1283 return rv;
1284 }
1285
1286 static PyObject *
1287 audioop_ulaw2lin(PyObject *self, PyObject *args)
1288 {
1289 unsigned char *cp;
1290 unsigned char cval;
1291 signed char *ncp;
1292 int len, size, val;
1293 PyObject *rv;
1294 int i;
1295
1296 if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
1297 &cp, &len, &size) )
1298 return 0;
1299
1300 if (!audioop_check_size(size))
1301 return NULL;
1302
1303 if (len > INT_MAX/size) {
1304 PyErr_SetString(PyExc_MemoryError,
1305 "not enough memory for output buffer");
1306 return 0;
1307 }
1308 rv = PyString_FromStringAndSize(NULL, len*size);
1309 if ( rv == 0 )
1310 return 0;
1311 ncp = (signed char *)PyString_AsString(rv);
1312
1313 for ( i=0; i < len*size; i += size ) {
1314 cval = *cp++;
1315 val = st_ulaw2linear16(cval);
1316
1317 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1318 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1319 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1320 }
1321 return rv;
1322 }
1323
1324 static PyObject *
1325 audioop_lin2alaw(PyObject *self, PyObject *args)
1326 {
1327 signed char *cp;
1328 unsigned char *ncp;
1329 int len, size, val = 0;
1330 PyObject *rv;
1331 int i;
1332
1333 if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
1334 &cp, &len, &size) )
1335 return 0;
1336
1337 if (!audioop_check_parameters(len, size))
1338 return NULL;
1339
1340 rv = PyString_FromStringAndSize(NULL, len/size);
1341 if ( rv == 0 )
1342 return 0;
1343 ncp = (unsigned char *)PyString_AsString(rv);
1344
1345 for ( i=0; i < len; i += size ) {
1346 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1347 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1348 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1349
1350 *ncp++ = st_linear2alaw(val);
1351 }
1352 return rv;
1353 }
1354
1355 static PyObject *
1356 audioop_alaw2lin(PyObject *self, PyObject *args)
1357 {
1358 unsigned char *cp;
1359 unsigned char cval;
1360 signed char *ncp;
1361 int len, size, val;
1362 PyObject *rv;
1363 int i;
1364
1365 if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
1366 &cp, &len, &size) )
1367 return 0;
1368
1369 if (!audioop_check_size(size))
1370 return NULL;
1371
1372 if (len > INT_MAX/size) {
1373 PyErr_SetString(PyExc_MemoryError,
1374 "not enough memory for output buffer");
1375 return 0;
1376 }
1377 rv = PyString_FromStringAndSize(NULL, len*size);
1378 if ( rv == 0 )
1379 return 0;
1380 ncp = (signed char *)PyString_AsString(rv);
1381
1382 for ( i=0; i < len*size; i += size ) {
1383 cval = *cp++;
1384 val = st_alaw2linear16(cval);
1385
1386 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1387 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1388 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1389 }
1390 return rv;
1391 }
1392
1393 static PyObject *
1394 audioop_lin2adpcm(PyObject *self, PyObject *args)
1395 {
1396 signed char *cp;
1397 signed char *ncp;
1398 int len, size, val = 0, step, valpred, delta,
1399 index, sign, vpdiff, diff;
1400 PyObject *rv, *state, *str;
1401 int i, outputbuffer = 0, bufferstep;
1402
1403 if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
1404 &cp, &len, &size, &state) )
1405 return 0;
1406
1407 if (!audioop_check_parameters(len, size))
1408 return NULL;
1409
1410 str = PyString_FromStringAndSize(NULL, len/(size*2));
1411 if ( str == 0 )
1412 return 0;
1413 ncp = (signed char *)PyString_AsString(str);
1414
1415 /* Decode state, should have (value, step) */
1416 if ( state == Py_None ) {
1417 /* First time, it seems. Set defaults */
1418 valpred = 0;
1419 index = 0;
1420 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
1421 return 0;
1422
1423 step = stepsizeTable[index];
1424 bufferstep = 1;
1425
1426 for ( i=0; i < len; i += size ) {
1427 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1428 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1429 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1430
1431 /* Step 1 - compute difference with previous value */
1432 diff = val - valpred;
1433 sign = (diff < 0) ? 8 : 0;
1434 if ( sign ) diff = (-diff);
1435
1436 /* Step 2 - Divide and clamp */
1437 /* Note:
1438 ** This code *approximately* computes:
1439 ** delta = diff*4/step;
1440 ** vpdiff = (delta+0.5)*step/4;
1441 ** but in shift step bits are dropped. The net result of this
1442 ** is that even if you have fast mul/div hardware you cannot
1443 ** put it to good use since the fixup would be too expensive.
1444 */
1445 delta = 0;
1446 vpdiff = (step >> 3);
1447
1448 if ( diff >= step ) {
1449 delta = 4;
1450 diff -= step;
1451 vpdiff += step;
1452 }
1453 step >>= 1;
1454 if ( diff >= step ) {
1455 delta |= 2;
1456 diff -= step;
1457 vpdiff += step;
1458 }
1459 step >>= 1;
1460 if ( diff >= step ) {
1461 delta |= 1;
1462 vpdiff += step;
1463 }
1464
1465 /* Step 3 - Update previous value */
1466 if ( sign )
1467 valpred -= vpdiff;
1468 else
1469 valpred += vpdiff;
1470
1471 /* Step 4 - Clamp previous value to 16 bits */
1472 if ( valpred > 32767 )
1473 valpred = 32767;
1474 else if ( valpred < -32768 )
1475 valpred = -32768;
1476
1477 /* Step 5 - Assemble value, update index and step values */
1478 delta |= sign;
1479
1480 index += indexTable[delta];
1481 if ( index < 0 ) index = 0;
1482 if ( index > 88 ) index = 88;
1483 step = stepsizeTable[index];
1484
1485 /* Step 6 - Output value */
1486 if ( bufferstep ) {
1487 outputbuffer = (delta << 4) & 0xf0;
1488 } else {
1489 *ncp++ = (delta & 0x0f) | outputbuffer;
1490 }
1491 bufferstep = !bufferstep;
1492 }
1493 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1494 Py_DECREF(str);
1495 return rv;
1496 }
1497
1498 static PyObject *
1499 audioop_adpcm2lin(PyObject *self, PyObject *args)
1500 {
1501 signed char *cp;
1502 signed char *ncp;
1503 int len, size, valpred, step, delta, index, sign, vpdiff;
1504 PyObject *rv, *str, *state;
1505 int i, inputbuffer = 0, bufferstep;
1506
1507 if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
1508 &cp, &len, &size, &state) )
1509 return 0;
1510
1511 if (!audioop_check_size(size))
1512 return NULL;
1513
1514 /* Decode state, should have (value, step) */
1515 if ( state == Py_None ) {
1516 /* First time, it seems. Set defaults */
1517 valpred = 0;
1518 index = 0;
1519 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
1520 return 0;
1521
1522 if (len > (INT_MAX/2)/size) {
1523 PyErr_SetString(PyExc_MemoryError,
1524 "not enough memory for output buffer");
1525 return 0;
1526 }
1527 str = PyString_FromStringAndSize(NULL, len*size*2);
1528 if ( str == 0 )
1529 return 0;
1530 ncp = (signed char *)PyString_AsString(str);
1531
1532 step = stepsizeTable[index];
1533 bufferstep = 0;
1534
1535 for ( i=0; i < len*size*2; i += size ) {
1536 /* Step 1 - get the delta value and compute next index */
1537 if ( bufferstep ) {
1538 delta = inputbuffer & 0xf;
1539 } else {
1540 inputbuffer = *cp++;
1541 delta = (inputbuffer >> 4) & 0xf;
1542 }
1543
1544 bufferstep = !bufferstep;
1545
1546 /* Step 2 - Find new index value (for later) */
1547 index += indexTable[delta];
1548 if ( index < 0 ) index = 0;
1549 if ( index > 88 ) index = 88;
1550
1551 /* Step 3 - Separate sign and magnitude */
1552 sign = delta & 8;
1553 delta = delta & 7;
1554
1555 /* Step 4 - Compute difference and new predicted value */
1556 /*
1557 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
1558 ** in adpcm_coder.
1559 */
1560 vpdiff = step >> 3;
1561 if ( delta & 4 ) vpdiff += step;
1562 if ( delta & 2 ) vpdiff += step>>1;
1563 if ( delta & 1 ) vpdiff += step>>2;
1564
1565 if ( sign )
1566 valpred -= vpdiff;
1567 else
1568 valpred += vpdiff;
1569
1570 /* Step 5 - clamp output value */
1571 if ( valpred > 32767 )
1572 valpred = 32767;
1573 else if ( valpred < -32768 )
1574 valpred = -32768;
1575
1576 /* Step 6 - Update step value */
1577 step = stepsizeTable[index];
1578
1579 /* Step 6 - Output value */
1580 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
1581 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
1582 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
1583 }
1584
1585 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1586 Py_DECREF(str);
1587 return rv;
1588 }
1589
1590 static PyMethodDef audioop_methods[] = {
1591 { "max", audioop_max, METH_VARARGS },
1592 { "minmax", audioop_minmax, METH_VARARGS },
1593 { "avg", audioop_avg, METH_VARARGS },
1594 { "maxpp", audioop_maxpp, METH_VARARGS },
1595 { "avgpp", audioop_avgpp, METH_VARARGS },
1596 { "rms", audioop_rms, METH_VARARGS },
1597 { "findfit", audioop_findfit, METH_VARARGS },
1598 { "findmax", audioop_findmax, METH_VARARGS },
1599 { "findfactor", audioop_findfactor, METH_VARARGS },
1600 { "cross", audioop_cross, METH_VARARGS },
1601 { "mul", audioop_mul, METH_VARARGS },
1602 { "add", audioop_add, METH_VARARGS },
1603 { "bias", audioop_bias, METH_VARARGS },
1604 { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
1605 { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
1606 { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
1607 { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
1608 { "lin2lin", audioop_lin2lin, METH_VARARGS },
1609 { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
1610 { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
1611 { "tomono", audioop_tomono, METH_VARARGS },
1612 { "tostereo", audioop_tostereo, METH_VARARGS },
1613 { "getsample", audioop_getsample, METH_VARARGS },
1614 { "reverse", audioop_reverse, METH_VARARGS },
1615 { "ratecv", audioop_ratecv, METH_VARARGS },
1616 { 0, 0 }
1617 };
1618
1619 PyMODINIT_FUNC
1620 initaudioop(void)
1621 {
1622 PyObject *m, *d;
1623 m = Py_InitModule("audioop", audioop_methods);
1624 if (m == NULL)
1625 return;
1626 d = PyModule_GetDict(m);
1627 if (d == NULL)
1628 return;
1629 AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
1630 if (AudioopError != NULL)
1631 PyDict_SetItemString(d,"error",AudioopError);
1632 }