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