2 /* audioopmodule - Module to detect peak values in arrays */
8 typedef unsigned int Py_UInt32
;
11 typedef long Py_Int32
;
12 typedef unsigned long Py_UInt32
;
14 #error "No 4-byte integral type"
18 typedef short PyInt16
;
20 #if defined(__CHAR_UNSIGNED__)
22 /* This module currently does not work on systems where only unsigned
23 characters are available. Take it out of Setup. Sorry. */
27 /* Code shamelessly stolen from sox, 12.17.7, g711.c
28 ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
33 * Functions linear2alaw, linear2ulaw have been updated to correctly
34 * convert unquantized 16 bit values.
35 * Tables for direct u- to A-law and A- to u-law conversions have been
37 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
41 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
43 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
44 #define QUANT_MASK (0xf) /* Quantization field mask. */
45 #define SEG_SHIFT (4) /* Left shift for segment number. */
46 #define SEG_MASK (0x70) /* Segment field mask. */
48 static PyInt16 seg_aend
[8] = {0x1F, 0x3F, 0x7F, 0xFF,
49 0x1FF, 0x3FF, 0x7FF, 0xFFF};
50 static PyInt16 seg_uend
[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
51 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
54 search(PyInt16 val
, PyInt16
*table
, int size
)
58 for (i
= 0; i
< size
; i
++) {
64 #define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
65 #define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
67 static PyInt16 _st_ulaw2linear16
[256] = {
68 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
69 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
70 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
71 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
72 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
73 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
74 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
75 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
76 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
77 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
78 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
79 -1052, -988, -924, -876, -844, -812, -780,
80 -748, -716, -684, -652, -620, -588, -556,
81 -524, -492, -460, -428, -396, -372, -356,
82 -340, -324, -308, -292, -276, -260, -244,
83 -228, -212, -196, -180, -164, -148, -132,
84 -120, -112, -104, -96, -88, -80, -72,
85 -64, -56, -48, -40, -32, -24, -16,
86 -8, 0, 32124, 31100, 30076, 29052, 28028,
87 27004, 25980, 24956, 23932, 22908, 21884, 20860,
88 19836, 18812, 17788, 16764, 15996, 15484, 14972,
89 14460, 13948, 13436, 12924, 12412, 11900, 11388,
90 10876, 10364, 9852, 9340, 8828, 8316, 7932,
91 7676, 7420, 7164, 6908, 6652, 6396, 6140,
92 5884, 5628, 5372, 5116, 4860, 4604, 4348,
93 4092, 3900, 3772, 3644, 3516, 3388, 3260,
94 3132, 3004, 2876, 2748, 2620, 2492, 2364,
95 2236, 2108, 1980, 1884, 1820, 1756, 1692,
96 1628, 1564, 1500, 1436, 1372, 1308, 1244,
97 1180, 1116, 1052, 988, 924, 876, 844,
98 812, 780, 748, 716, 684, 652, 620,
99 588, 556, 524, 492, 460, 428, 396,
100 372, 356, 340, 324, 308, 292, 276,
101 260, 244, 228, 212, 196, 180, 164,
102 148, 132, 120, 112, 104, 96, 88,
103 80, 72, 64, 56, 48, 40, 32,
108 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
109 * stored in a unsigned char. This function should only be called with
110 * the data shifted such that it only contains information in the lower
113 * In order to simplify the encoding process, the original linear magnitude
114 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
115 * (33 - 8191). The result can be seen in the following encoding table:
117 * Biased Linear Input Code Compressed Code
118 * ------------------------ ---------------
119 * 00000001wxyza 000wxyz
120 * 0000001wxyzab 001wxyz
121 * 000001wxyzabc 010wxyz
122 * 00001wxyzabcd 011wxyz
123 * 0001wxyzabcde 100wxyz
124 * 001wxyzabcdef 101wxyz
125 * 01wxyzabcdefg 110wxyz
126 * 1wxyzabcdefgh 111wxyz
128 * Each biased linear code has a leading 1 which identifies the segment
129 * number. The value of the segment number is equal to 7 minus the number
130 * of leading 0's. The quantization interval is directly available as the
131 * four bits wxyz. * The trailing bits (a - h) are ignored.
133 * Ordinarily the complement of the resulting code word is used for
134 * transmission, and so the code word is complemented before it is returned.
136 * For further information see John C. Bellamy's Digital Telephony, 1982,
137 * John Wiley & Sons, pps 98-111 and 472-476.
140 st_14linear2ulaw(PyInt16 pcm_val
) /* 2's complement (14-bit range) */
146 /* The original sox code does this in the calling function, not here */
147 pcm_val
= pcm_val
>> 2;
149 /* u-law inverts all bits */
150 /* Get the sign and the magnitude of the value. */
157 if ( pcm_val
> CLIP
) pcm_val
= CLIP
; /* clip the magnitude */
158 pcm_val
+= (BIAS
>> 2);
160 /* Convert the scaled magnitude to segment number. */
161 seg
= search(pcm_val
, seg_uend
, 8);
164 * Combine the sign, segment, quantization bits;
165 * and complement the code word.
167 if (seg
>= 8) /* out of range, return maximum value. */
168 return (unsigned char) (0x7F ^ mask
);
170 uval
= (unsigned char) (seg
<< 4) | ((pcm_val
>> (seg
+ 1)) & 0xF);
171 return (uval
^ mask
);
176 static PyInt16 _st_alaw2linear16
[256] = {
177 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
178 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
179 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
180 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
181 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
182 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
183 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
184 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
185 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
186 -13568, -344, -328, -376, -360, -280, -264,
187 -312, -296, -472, -456, -504, -488, -408,
188 -392, -440, -424, -88, -72, -120, -104,
189 -24, -8, -56, -40, -216, -200, -248,
190 -232, -152, -136, -184, -168, -1376, -1312,
191 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
192 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
193 -688, -656, -752, -720, -560, -528, -624,
194 -592, -944, -912, -1008, -976, -816, -784,
195 -880, -848, 5504, 5248, 6016, 5760, 4480,
196 4224, 4992, 4736, 7552, 7296, 8064, 7808,
197 6528, 6272, 7040, 6784, 2752, 2624, 3008,
198 2880, 2240, 2112, 2496, 2368, 3776, 3648,
199 4032, 3904, 3264, 3136, 3520, 3392, 22016,
200 20992, 24064, 23040, 17920, 16896, 19968, 18944,
201 30208, 29184, 32256, 31232, 26112, 25088, 28160,
202 27136, 11008, 10496, 12032, 11520, 8960, 8448,
203 9984, 9472, 15104, 14592, 16128, 15616, 13056,
204 12544, 14080, 13568, 344, 328, 376, 360,
205 280, 264, 312, 296, 472, 456, 504,
206 488, 408, 392, 440, 424, 88, 72,
207 120, 104, 24, 8, 56, 40, 216,
208 200, 248, 232, 152, 136, 184, 168,
209 1376, 1312, 1504, 1440, 1120, 1056, 1248,
210 1184, 1888, 1824, 2016, 1952, 1632, 1568,
211 1760, 1696, 688, 656, 752, 720, 560,
212 528, 624, 592, 944, 912, 1008, 976,
217 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
218 * stored in a unsigned char. This function should only be called with
219 * the data shifted such that it only contains information in the lower
222 * Linear Input Code Compressed Code
223 * ------------------------ ---------------
224 * 0000000wxyza 000wxyz
225 * 0000001wxyza 001wxyz
226 * 000001wxyzab 010wxyz
227 * 00001wxyzabc 011wxyz
228 * 0001wxyzabcd 100wxyz
229 * 001wxyzabcde 101wxyz
230 * 01wxyzabcdef 110wxyz
231 * 1wxyzabcdefg 111wxyz
233 * For further information see John C. Bellamy's Digital Telephony, 1982,
234 * John Wiley & Sons, pps 98-111 and 472-476.
237 st_linear2alaw(PyInt16 pcm_val
) /* 2's complement (13-bit range) */
243 /* The original sox code does this in the calling function, not here */
244 pcm_val
= pcm_val
>> 3;
246 /* A-law using even bit inversion */
248 mask
= 0xD5; /* sign (7th) bit = 1 */
250 mask
= 0x55; /* sign bit = 0 */
251 pcm_val
= -pcm_val
- 1;
254 /* Convert the scaled magnitude to segment number. */
255 seg
= search(pcm_val
, seg_aend
, 8);
257 /* Combine the sign, segment, and quantization bits. */
259 if (seg
>= 8) /* out of range, return maximum value. */
260 return (unsigned char) (0x7F ^ mask
);
262 aval
= (unsigned char) seg
<< SEG_SHIFT
;
264 aval
|= (pcm_val
>> 1) & QUANT_MASK
;
266 aval
|= (pcm_val
>> seg
) & QUANT_MASK
;
267 return (aval
^ mask
);
270 /* End of code taken from sox */
272 /* Intel ADPCM step variation table */
273 static int indexTable
[16] = {
274 -1, -1, -1, -1, 2, 4, 6, 8,
275 -1, -1, -1, -1, 2, 4, 6, 8,
278 static int stepsizeTable
[89] = {
279 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
280 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
281 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
282 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
283 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
284 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
285 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
286 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
287 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
290 #define CHARP(cp, i) ((signed char *)(cp+i))
291 #define SHORTP(cp, i) ((short *)(cp+i))
292 #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
296 static PyObject
*AudioopError
;
299 audioop_getsample(PyObject
*self
, PyObject
*args
)
302 int len
, size
, val
= 0;
305 if ( !PyArg_ParseTuple(args
, "s#ii:getsample", &cp
, &len
, &size
, &i
) )
307 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
308 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
311 if ( i
< 0 || i
>= len
/size
) {
312 PyErr_SetString(AudioopError
, "Index out of range");
315 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
316 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
*2);
317 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
*4);
318 return PyLong_FromLong(val
);
322 audioop_max(PyObject
*self
, PyObject
*args
)
325 int len
, size
, val
= 0;
329 if ( !PyArg_ParseTuple(args
, "s#i:max", &cp
, &len
, &size
) )
331 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
332 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
335 for ( i
=0; i
<len
; i
+= size
) {
336 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
337 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
338 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
339 if ( val
< 0 ) val
= (-val
);
340 if ( val
> max
) max
= val
;
342 return PyLong_FromLong(max
);
346 audioop_minmax(PyObject
*self
, PyObject
*args
)
349 int len
, size
, val
= 0;
351 int min
= 0x7fffffff, max
= -0x7fffffff;
353 if (!PyArg_ParseTuple(args
, "s#i:minmax", &cp
, &len
, &size
))
355 if (size
!= 1 && size
!= 2 && size
!= 4) {
356 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
359 for (i
= 0; i
< len
; i
+= size
) {
360 if (size
== 1) val
= (int) *CHARP(cp
, i
);
361 else if (size
== 2) val
= (int) *SHORTP(cp
, i
);
362 else if (size
== 4) val
= (int) *LONGP(cp
, i
);
363 if (val
> max
) max
= val
;
364 if (val
< min
) min
= val
;
366 return Py_BuildValue("(ii)", min
, max
);
370 audioop_avg(PyObject
*self
, PyObject
*args
)
373 int len
, size
, val
= 0;
377 if ( !PyArg_ParseTuple(args
, "s#i:avg", &cp
, &len
, &size
) )
379 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
380 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
383 for ( i
=0; i
<len
; i
+= size
) {
384 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
385 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
386 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
392 val
= (int)(avg
/ (double)(len
/size
));
393 return PyLong_FromLong(val
);
397 audioop_rms(PyObject
*self
, PyObject
*args
)
400 int len
, size
, val
= 0;
402 double sum_squares
= 0.0;
404 if ( !PyArg_ParseTuple(args
, "s#i:rms", &cp
, &len
, &size
) )
406 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
407 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
410 for ( i
=0; i
<len
; i
+= size
) {
411 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
412 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
413 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
414 sum_squares
+= (double)val
*(double)val
;
419 val
= (int)sqrt(sum_squares
/ (double)(len
/size
));
420 return PyLong_FromLong(val
);
423 static double _sum2(short *a
, short *b
, int len
)
428 for( i
=0; i
<len
; i
++) {
429 sum
= sum
+ (double)a
[i
]*(double)b
[i
];
435 ** Findfit tries to locate a sample within another sample. Its main use
436 ** is in echo-cancellation (to find the feedback of the output signal in
437 ** the input signal).
438 ** The method used is as follows:
440 ** let R be the reference signal (length n) and A the input signal (length N)
441 ** with N > n, and let all sums be over i from 0 to n-1.
443 ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
444 ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
445 ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
447 ** Next, we compute the relative distance between the original signal and
448 ** the modified signal and minimize that over j:
449 ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) =>
450 ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
452 ** In the code variables correspond as follows:
459 ** sum_ri_2 sum(R[i]^2)
460 ** sum_aij_2 sum(A[i+j]^2)
461 ** sum_aij_ri sum(A[i+j]R[i])
463 ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
464 ** is completely recalculated each step.
467 audioop_findfit(PyObject
*self
, PyObject
*args
)
472 double aj_m1
, aj_lm1
;
473 double sum_ri_2
, sum_aij_2
, sum_aij_ri
, result
, best_result
, factor
;
475 /* Passing a short** for an 's' argument is correct only
476 if the string contents is aligned for interpretation
477 as short[]. Due to the definition of PyBytesObject,
478 this is currently (Python 2.6) the case. */
479 if ( !PyArg_ParseTuple(args
, "s#s#:findfit",
480 (char**)&cp1
, &len1
, (char**)&cp2
, &len2
) )
482 if ( len1
& 1 || len2
& 1 ) {
483 PyErr_SetString(AudioopError
, "Strings should be even-sized");
490 PyErr_SetString(AudioopError
, "First sample should be longer");
493 sum_ri_2
= _sum2(cp2
, cp2
, len2
);
494 sum_aij_2
= _sum2(cp1
, cp1
, len2
);
495 sum_aij_ri
= _sum2(cp1
, cp2
, len2
);
497 result
= (sum_ri_2
*sum_aij_2
- sum_aij_ri
*sum_aij_ri
) / sum_aij_2
;
499 best_result
= result
;
503 for ( j
=1; j
<=len1
-len2
; j
++) {
504 aj_m1
= (double)cp1
[j
-1];
505 aj_lm1
= (double)cp1
[j
+len2
-1];
507 sum_aij_2
= sum_aij_2
+ aj_lm1
*aj_lm1
- aj_m1
*aj_m1
;
508 sum_aij_ri
= _sum2(cp1
+j
, cp2
, len2
);
510 result
= (sum_ri_2
*sum_aij_2
- sum_aij_ri
*sum_aij_ri
)
513 if ( result
< best_result
) {
514 best_result
= result
;
520 factor
= _sum2(cp1
+best_j
, cp2
, len2
) / sum_ri_2
;
522 return Py_BuildValue("(if)", best_j
, factor
);
526 ** findfactor finds a factor f so that the energy in A-fB is minimal.
527 ** See the comment for findfit for details.
530 audioop_findfactor(PyObject
*self
, PyObject
*args
)
534 double sum_ri_2
, sum_aij_ri
, result
;
536 if ( !PyArg_ParseTuple(args
, "s#s#:findfactor",
537 (char**)&cp1
, &len1
, (char**)&cp2
, &len2
) )
539 if ( len1
& 1 || len2
& 1 ) {
540 PyErr_SetString(AudioopError
, "Strings should be even-sized");
543 if ( len1
!= len2
) {
544 PyErr_SetString(AudioopError
, "Samples should be same size");
548 sum_ri_2
= _sum2(cp2
, cp2
, len2
);
549 sum_aij_ri
= _sum2(cp1
, cp2
, len2
);
551 result
= sum_aij_ri
/ sum_ri_2
;
553 return PyFloat_FromDouble(result
);
557 ** findmax returns the index of the n-sized segment of the input sample
558 ** that contains the most energy.
561 audioop_findmax(PyObject
*self
, PyObject
*args
)
566 double aj_m1
, aj_lm1
;
567 double result
, best_result
;
569 if ( !PyArg_ParseTuple(args
, "s#i:findmax",
570 (char**)&cp1
, &len1
, &len2
) )
573 PyErr_SetString(AudioopError
, "Strings should be even-sized");
578 if ( len2
< 0 || len1
< len2
) {
579 PyErr_SetString(AudioopError
, "Input sample should be longer");
583 result
= _sum2(cp1
, cp1
, len2
);
585 best_result
= result
;
589 for ( j
=1; j
<=len1
-len2
; j
++) {
590 aj_m1
= (double)cp1
[j
-1];
591 aj_lm1
= (double)cp1
[j
+len2
-1];
593 result
= result
+ aj_lm1
*aj_lm1
- aj_m1
*aj_m1
;
595 if ( result
> best_result
) {
596 best_result
= result
;
602 return PyLong_FromLong(best_j
);
606 audioop_avgpp(PyObject
*self
, PyObject
*args
)
609 int len
, size
, val
= 0, prevval
= 0, prevextremevalid
= 0,
613 int diff
, prevdiff
, extremediff
, nextreme
= 0;
615 if ( !PyArg_ParseTuple(args
, "s#i:avgpp", &cp
, &len
, &size
) )
617 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
618 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
621 /* Compute first delta value ahead. Also automatically makes us
622 ** skip the first extreme value
624 if ( size
== 1 ) prevval
= (int)*CHARP(cp
, 0);
625 else if ( size
== 2 ) prevval
= (int)*SHORTP(cp
, 0);
626 else if ( size
== 4 ) prevval
= (int)*LONGP(cp
, 0);
627 if ( size
== 1 ) val
= (int)*CHARP(cp
, size
);
628 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, size
);
629 else if ( size
== 4 ) val
= (int)*LONGP(cp
, size
);
630 prevdiff
= val
- prevval
;
632 for ( i
=size
; i
<len
; i
+= size
) {
633 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
634 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
635 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
636 diff
= val
- prevval
;
637 if ( diff
*prevdiff
< 0 ) {
638 /* Derivative changed sign. Compute difference to last
639 ** extreme value and remember.
641 if ( prevextremevalid
) {
642 extremediff
= prevval
- prevextreme
;
643 if ( extremediff
< 0 )
644 extremediff
= -extremediff
;
648 prevextremevalid
= 1;
649 prevextreme
= prevval
;
658 val
= (int)(avg
/ (double)nextreme
);
659 return PyLong_FromLong(val
);
663 audioop_maxpp(PyObject
*self
, PyObject
*args
)
666 int len
, size
, val
= 0, prevval
= 0, prevextremevalid
= 0,
670 int diff
, prevdiff
, extremediff
;
672 if ( !PyArg_ParseTuple(args
, "s#i:maxpp", &cp
, &len
, &size
) )
674 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
675 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
678 /* Compute first delta value ahead. Also automatically makes us
679 ** skip the first extreme value
681 if ( size
== 1 ) prevval
= (int)*CHARP(cp
, 0);
682 else if ( size
== 2 ) prevval
= (int)*SHORTP(cp
, 0);
683 else if ( size
== 4 ) prevval
= (int)*LONGP(cp
, 0);
684 if ( size
== 1 ) val
= (int)*CHARP(cp
, size
);
685 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, size
);
686 else if ( size
== 4 ) val
= (int)*LONGP(cp
, size
);
687 prevdiff
= val
- prevval
;
689 for ( i
=size
; i
<len
; i
+= size
) {
690 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
691 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
692 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
693 diff
= val
- prevval
;
694 if ( diff
*prevdiff
< 0 ) {
695 /* Derivative changed sign. Compute difference to
696 ** last extreme value and remember.
698 if ( prevextremevalid
) {
699 extremediff
= prevval
- prevextreme
;
700 if ( extremediff
< 0 )
701 extremediff
= -extremediff
;
702 if ( extremediff
> max
)
705 prevextremevalid
= 1;
706 prevextreme
= prevval
;
712 return PyLong_FromLong(max
);
716 audioop_cross(PyObject
*self
, PyObject
*args
)
719 int len
, size
, val
= 0;
723 if ( !PyArg_ParseTuple(args
, "s#i:cross", &cp
, &len
, &size
) )
725 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
726 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
730 prevval
= 17; /* Anything <> 0,1 */
731 for ( i
=0; i
<len
; i
+= size
) {
732 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) >> 7;
733 else if ( size
== 2 ) val
= ((int)*SHORTP(cp
, i
)) >> 15;
734 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 31;
736 if ( val
!= prevval
) ncross
++;
739 return PyLong_FromLong(ncross
);
743 audioop_mul(PyObject
*self
, PyObject
*args
)
745 signed char *cp
, *ncp
;
746 int len
, size
, val
= 0;
747 double factor
, fval
, maxval
;
751 if ( !PyArg_ParseTuple(args
, "s#id:mul", &cp
, &len
, &size
, &factor
) )
754 if ( size
== 1 ) maxval
= (double) 0x7f;
755 else if ( size
== 2 ) maxval
= (double) 0x7fff;
756 else if ( size
== 4 ) maxval
= (double) 0x7fffffff;
758 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
762 rv
= PyBytes_FromStringAndSize(NULL
, len
);
765 ncp
= (signed char *)PyBytes_AsString(rv
);
768 for ( i
=0; i
< len
; i
+= size
) {
769 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
770 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
771 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
772 fval
= (double)val
*factor
;
773 if ( fval
> maxval
) fval
= maxval
;
774 else if ( fval
< -maxval
) fval
= -maxval
;
776 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)val
;
777 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)val
;
778 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)val
;
784 audioop_tomono(PyObject
*self
, PyObject
*args
)
787 signed char *cp
, *ncp
;
788 int len
, size
, val1
= 0, val2
= 0;
789 double fac1
, fac2
, fval
, maxval
;
793 if ( !PyArg_ParseTuple(args
, "s*idd:tomono",
794 &pcp
, &size
, &fac1
, &fac2
) )
799 if ( size
== 1 ) maxval
= (double) 0x7f;
800 else if ( size
== 2 ) maxval
= (double) 0x7fff;
801 else if ( size
== 4 ) maxval
= (double) 0x7fffffff;
803 PyBuffer_Release(&pcp
);
804 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
808 rv
= PyBytes_FromStringAndSize(NULL
, len
/2);
811 ncp
= (signed char *)PyBytes_AsString(rv
);
814 for ( i
=0; i
< len
; i
+= size
*2 ) {
815 if ( size
== 1 ) val1
= (int)*CHARP(cp
, i
);
816 else if ( size
== 2 ) val1
= (int)*SHORTP(cp
, i
);
817 else if ( size
== 4 ) val1
= (int)*LONGP(cp
, i
);
818 if ( size
== 1 ) val2
= (int)*CHARP(cp
, i
+1);
819 else if ( size
== 2 ) val2
= (int)*SHORTP(cp
, i
+2);
820 else if ( size
== 4 ) val2
= (int)*LONGP(cp
, i
+4);
821 fval
= (double)val1
*fac1
+ (double)val2
*fac2
;
822 if ( fval
> maxval
) fval
= maxval
;
823 else if ( fval
< -maxval
) fval
= -maxval
;
825 if ( size
== 1 ) *CHARP(ncp
, i
/2) = (signed char)val1
;
826 else if ( size
== 2 ) *SHORTP(ncp
, i
/2) = (short)val1
;
827 else if ( size
== 4 ) *LONGP(ncp
, i
/2)= (Py_Int32
)val1
;
829 PyBuffer_Release(&pcp
);
834 audioop_tostereo(PyObject
*self
, PyObject
*args
)
836 signed char *cp
, *ncp
;
837 int len
, new_len
, size
, val1
, val2
, val
= 0;
838 double fac1
, fac2
, fval
, maxval
;
842 if ( !PyArg_ParseTuple(args
, "s#idd:tostereo",
843 &cp
, &len
, &size
, &fac1
, &fac2
) )
846 if ( size
== 1 ) maxval
= (double) 0x7f;
847 else if ( size
== 2 ) maxval
= (double) 0x7fff;
848 else if ( size
== 4 ) maxval
= (double) 0x7fffffff;
850 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
856 PyErr_SetString(PyExc_MemoryError
,
857 "not enough memory for output buffer");
861 rv
= PyBytes_FromStringAndSize(NULL
, new_len
);
864 ncp
= (signed char *)PyBytes_AsString(rv
);
867 for ( i
=0; i
< len
; i
+= size
) {
868 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
869 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
870 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
872 fval
= (double)val
*fac1
;
873 if ( fval
> maxval
) fval
= maxval
;
874 else if ( fval
< -maxval
) fval
= -maxval
;
877 fval
= (double)val
*fac2
;
878 if ( fval
> maxval
) fval
= maxval
;
879 else if ( fval
< -maxval
) fval
= -maxval
;
882 if ( size
== 1 ) *CHARP(ncp
, i
*2) = (signed char)val1
;
883 else if ( size
== 2 ) *SHORTP(ncp
, i
*2) = (short)val1
;
884 else if ( size
== 4 ) *LONGP(ncp
, i
*2) = (Py_Int32
)val1
;
886 if ( size
== 1 ) *CHARP(ncp
, i
*2+1) = (signed char)val2
;
887 else if ( size
== 2 ) *SHORTP(ncp
, i
*2+2) = (short)val2
;
888 else if ( size
== 4 ) *LONGP(ncp
, i
*2+4) = (Py_Int32
)val2
;
894 audioop_add(PyObject
*self
, PyObject
*args
)
896 signed char *cp1
, *cp2
, *ncp
;
897 int len1
, len2
, size
, val1
= 0, val2
= 0, maxval
, newval
;
901 if ( !PyArg_ParseTuple(args
, "s#s#i:add",
902 &cp1
, &len1
, &cp2
, &len2
, &size
) )
905 if ( len1
!= len2
) {
906 PyErr_SetString(AudioopError
, "Lengths should be the same");
910 if ( size
== 1 ) maxval
= 0x7f;
911 else if ( size
== 2 ) maxval
= 0x7fff;
912 else if ( size
== 4 ) maxval
= 0x7fffffff;
914 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
918 rv
= PyBytes_FromStringAndSize(NULL
, len1
);
921 ncp
= (signed char *)PyBytes_AsString(rv
);
923 for ( i
=0; i
< len1
; i
+= size
) {
924 if ( size
== 1 ) val1
= (int)*CHARP(cp1
, i
);
925 else if ( size
== 2 ) val1
= (int)*SHORTP(cp1
, i
);
926 else if ( size
== 4 ) val1
= (int)*LONGP(cp1
, i
);
928 if ( size
== 1 ) val2
= (int)*CHARP(cp2
, i
);
929 else if ( size
== 2 ) val2
= (int)*SHORTP(cp2
, i
);
930 else if ( size
== 4 ) val2
= (int)*LONGP(cp2
, i
);
932 newval
= val1
+ val2
;
933 /* truncate in case of overflow */
934 if (newval
> maxval
) newval
= maxval
;
935 else if (newval
< -maxval
) newval
= -maxval
;
936 else if (size
== 4 && (newval
^val1
) < 0 && (newval
^val2
) < 0)
937 newval
= val1
> 0 ? maxval
: - maxval
;
939 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)newval
;
940 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)newval
;
941 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)newval
;
947 audioop_bias(PyObject
*self
, PyObject
*args
)
949 signed char *cp
, *ncp
;
950 int len
, size
, val
= 0;
955 if ( !PyArg_ParseTuple(args
, "s#ii:bias",
956 &cp
, &len
, &size
, &bias
) )
959 if ( size
!= 1 && size
!= 2 && size
!= 4) {
960 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
964 rv
= PyBytes_FromStringAndSize(NULL
, len
);
967 ncp
= (signed char *)PyBytes_AsString(rv
);
970 for ( i
=0; i
< len
; i
+= size
) {
971 if ( size
== 1 ) val
= (int)*CHARP(cp
, i
);
972 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
973 else if ( size
== 4 ) val
= (int)*LONGP(cp
, i
);
975 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)(val
+bias
);
976 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)(val
+bias
);
977 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)(val
+bias
);
983 audioop_reverse(PyObject
*self
, PyObject
*args
)
987 int len
, size
, val
= 0;
991 if ( !PyArg_ParseTuple(args
, "s#i:reverse",
995 if ( size
!= 1 && size
!= 2 && size
!= 4 ) {
996 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1000 rv
= PyBytes_FromStringAndSize(NULL
, len
);
1003 ncp
= (unsigned char *)PyBytes_AsString(rv
);
1005 for ( i
=0; i
< len
; i
+= size
) {
1006 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) << 8;
1007 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
1008 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 16;
1012 if ( size
== 1 ) *CHARP(ncp
, j
) = (signed char)(val
>> 8);
1013 else if ( size
== 2 ) *SHORTP(ncp
, j
) = (short)(val
);
1014 else if ( size
== 4 ) *LONGP(ncp
, j
) = (Py_Int32
)(val
<<16);
1020 audioop_lin2lin(PyObject
*self
, PyObject
*args
)
1024 int len
, new_len
, size
, size2
, val
= 0;
1028 if ( !PyArg_ParseTuple(args
, "s#ii:lin2lin",
1029 &cp
, &len
, &size
, &size2
) )
1032 if ( (size
!= 1 && size
!= 2 && size
!= 4) ||
1033 (size2
!= 1 && size2
!= 2 && size2
!= 4)) {
1034 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1038 new_len
= (len
/size
)*size2
;
1040 PyErr_SetString(PyExc_MemoryError
,
1041 "not enough memory for output buffer");
1044 rv
= PyBytes_FromStringAndSize(NULL
, new_len
);
1047 ncp
= (unsigned char *)PyBytes_AsString(rv
);
1049 for ( i
=0, j
=0; i
< len
; i
+= size
, j
+= size2
) {
1050 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) << 8;
1051 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
1052 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 16;
1054 if ( size2
== 1 ) *CHARP(ncp
, j
) = (signed char)(val
>> 8);
1055 else if ( size2
== 2 ) *SHORTP(ncp
, j
) = (short)(val
);
1056 else if ( size2
== 4 ) *LONGP(ncp
, j
) = (Py_Int32
)(val
<<16);
1073 audioop_ratecv(PyObject
*self
, PyObject
*args
)
1076 int len
, size
, nchannels
, inrate
, outrate
, weightA
, weightB
;
1077 int chan
, d
, *prev_i
, *cur_i
, cur_o
;
1078 PyObject
*state
, *samps
, *str
, *rv
= NULL
;
1079 int bytes_per_frame
;
1084 if (!PyArg_ParseTuple(args
, "s#iiiiO|ii:ratecv", &cp
, &len
, &size
,
1085 &nchannels
, &inrate
, &outrate
, &state
,
1086 &weightA
, &weightB
))
1088 if (size
!= 1 && size
!= 2 && size
!= 4) {
1089 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1092 if (nchannels
< 1) {
1093 PyErr_SetString(AudioopError
, "# of channels should be >= 1");
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");
1105 if (weightA
< 1 || weightB
< 0) {
1106 PyErr_SetString(AudioopError
,
1107 "weightA should be >= 1, weightB should be >= 0");
1110 if (len
% bytes_per_frame
!= 0) {
1111 PyErr_SetString(AudioopError
, "not a whole number of frames");
1114 if (inrate
<= 0 || outrate
<= 0) {
1115 PyErr_SetString(AudioopError
, "sampling rate not > 0");
1118 /* divide inrate and outrate by their greatest common divisor */
1119 d
= gcd(inrate
, outrate
);
1123 alloc_size
= sizeof(int) * (unsigned)nchannels
;
1124 if (alloc_size
< (unsigned)nchannels
) {
1125 PyErr_SetString(PyExc_MemoryError
,
1126 "not enough memory for output buffer");
1129 prev_i
= (int *) malloc(alloc_size
);
1130 cur_i
= (int *) malloc(alloc_size
);
1131 if (prev_i
== NULL
|| cur_i
== NULL
) {
1132 (void) PyErr_NoMemory();
1136 len
/= bytes_per_frame
; /* # of frames */
1138 if (state
== Py_None
) {
1140 for (chan
= 0; chan
< nchannels
; chan
++)
1141 prev_i
[chan
] = cur_i
[chan
] = 0;
1144 if (!PyArg_ParseTuple(state
,
1145 "iO!;audioop.ratecv: illegal state argument",
1146 &d
, &PyTuple_Type
, &samps
))
1148 if (PyTuple_Size(samps
) != nchannels
) {
1149 PyErr_SetString(AudioopError
,
1150 "illegal state argument");
1153 for (chan
= 0; chan
< nchannels
; chan
++) {
1154 if (!PyArg_ParseTuple(PyTuple_GetItem(samps
, chan
),
1155 "ii:ratecv", &prev_i
[chan
],
1161 /* str <- Space for the output buffer. */
1163 /* There are len input frames, so we need (mathematically)
1164 ceiling(len*outrate/inrate) output frames, and each frame
1165 requires bytes_per_frame bytes. Computing this
1166 without spurious overflow is the challenge; we can
1167 settle for a reasonable upper bound, though. */
1168 int ceiling
; /* the number of output frames */
1169 int nbytes
; /* the number of output bytes needed */
1170 int q
= len
/ inrate
;
1171 /* Now len = q * inrate + r exactly (with r = len % inrate),
1172 and this is less than q * inrate + inrate = (q+1)*inrate.
1173 So a reasonable upper bound on len*outrate/inrate is
1174 ((q+1)*inrate)*outrate/inrate =
1177 ceiling
= (q
+1) * outrate
;
1178 nbytes
= ceiling
* bytes_per_frame
;
1179 /* See whether anything overflowed; if not, get the space. */
1181 ceiling
/ outrate
!= q
+1 ||
1182 nbytes
/ bytes_per_frame
!= ceiling
)
1185 str
= PyBytes_FromStringAndSize(NULL
, nbytes
);
1188 PyErr_SetString(PyExc_MemoryError
,
1189 "not enough memory for output buffer");
1193 ncp
= PyBytes_AsString(str
);
1198 samps
= PyTuple_New(nchannels
);
1201 for (chan
= 0; chan
< nchannels
; chan
++)
1202 PyTuple_SetItem(samps
, chan
,
1203 Py_BuildValue("(ii)",
1206 if (PyErr_Occurred())
1208 /* We have checked before that the length
1209 * of the string fits into int. */
1210 len
= (int)(ncp
- PyBytes_AsString(str
));
1211 rv
= PyBytes_FromStringAndSize
1212 (PyBytes_AsString(str
), len
);
1217 rv
= Py_BuildValue("(O(iO))", str
, d
, samps
);
1220 goto exit
; /* return rv */
1222 for (chan
= 0; chan
< nchannels
; chan
++) {
1223 prev_i
[chan
] = cur_i
[chan
];
1225 cur_i
[chan
] = ((int)*CHARP(cp
, 0)) << 8;
1227 cur_i
[chan
] = (int)*SHORTP(cp
, 0);
1229 cur_i
[chan
] = ((int)*LONGP(cp
, 0)) >> 16;
1231 /* implements a simple digital filter */
1233 (weightA
* cur_i
[chan
] +
1234 weightB
* prev_i
[chan
]) /
1235 (weightA
+ weightB
);
1241 for (chan
= 0; chan
< nchannels
; chan
++) {
1242 cur_o
= (prev_i
[chan
] * d
+
1243 cur_i
[chan
] * (outrate
- d
)) /
1246 *CHARP(ncp
, 0) = (signed char)(cur_o
>> 8);
1248 *SHORTP(ncp
, 0) = (short)(cur_o
);
1250 *LONGP(ncp
, 0) = (Py_Int32
)(cur_o
<<16);
1265 audioop_lin2ulaw(PyObject
*self
, PyObject
*args
)
1269 int len
, size
, val
= 0;
1273 if ( !PyArg_ParseTuple(args
, "s#i:lin2ulaw",
1277 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1278 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1282 rv
= PyBytes_FromStringAndSize(NULL
, len
/size
);
1285 ncp
= (unsigned char *)PyBytes_AsString(rv
);
1287 for ( i
=0; i
< len
; i
+= size
) {
1288 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) << 8;
1289 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
1290 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 16;
1292 *ncp
++ = st_14linear2ulaw(val
);
1298 audioop_ulaw2lin(PyObject
*self
, PyObject
*args
)
1303 int len
, new_len
, size
, val
;
1307 if ( !PyArg_ParseTuple(args
, "s#i:ulaw2lin",
1311 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1312 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1318 PyErr_SetString(PyExc_MemoryError
,
1319 "not enough memory for output buffer");
1322 rv
= PyBytes_FromStringAndSize(NULL
, new_len
);
1325 ncp
= (signed char *)PyBytes_AsString(rv
);
1327 for ( i
=0; i
< new_len
; i
+= size
) {
1329 val
= st_ulaw2linear16(cval
);
1331 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)(val
>> 8);
1332 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)(val
);
1333 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)(val
<<16);
1339 audioop_lin2alaw(PyObject
*self
, PyObject
*args
)
1343 int len
, size
, val
= 0;
1347 if ( !PyArg_ParseTuple(args
, "s#i:lin2alaw",
1351 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1352 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1356 rv
= PyBytes_FromStringAndSize(NULL
, len
/size
);
1359 ncp
= (unsigned char *)PyBytes_AsString(rv
);
1361 for ( i
=0; i
< len
; i
+= size
) {
1362 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) << 8;
1363 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
1364 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 16;
1366 *ncp
++ = st_linear2alaw(val
);
1372 audioop_alaw2lin(PyObject
*self
, PyObject
*args
)
1377 int len
, new_len
, size
, val
;
1381 if ( !PyArg_ParseTuple(args
, "s#i:alaw2lin",
1385 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1386 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1392 PyErr_SetString(PyExc_MemoryError
,
1393 "not enough memory for output buffer");
1396 rv
= PyBytes_FromStringAndSize(NULL
, new_len
);
1399 ncp
= (signed char *)PyBytes_AsString(rv
);
1401 for ( i
=0; i
< new_len
; i
+= size
) {
1403 val
= st_alaw2linear16(cval
);
1405 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)(val
>> 8);
1406 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)(val
);
1407 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)(val
<<16);
1413 audioop_lin2adpcm(PyObject
*self
, PyObject
*args
)
1417 int len
, size
, val
= 0, step
, valpred
, delta
,
1418 index
, sign
, vpdiff
, diff
;
1419 PyObject
*rv
, *state
, *str
;
1420 int i
, outputbuffer
= 0, bufferstep
;
1422 if ( !PyArg_ParseTuple(args
, "s#iO:lin2adpcm",
1423 &cp
, &len
, &size
, &state
) )
1427 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1428 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1432 str
= PyBytes_FromStringAndSize(NULL
, len
/(size
*2));
1435 ncp
= (signed char *)PyBytes_AsString(str
);
1437 /* Decode state, should have (value, step) */
1438 if ( state
== Py_None
) {
1439 /* First time, it seems. Set defaults */
1443 } else if ( !PyArg_ParseTuple(state
, "ii", &valpred
, &index
) )
1446 step
= stepsizeTable
[index
];
1449 for ( i
=0; i
< len
; i
+= size
) {
1450 if ( size
== 1 ) val
= ((int)*CHARP(cp
, i
)) << 8;
1451 else if ( size
== 2 ) val
= (int)*SHORTP(cp
, i
);
1452 else if ( size
== 4 ) val
= ((int)*LONGP(cp
, i
)) >> 16;
1454 /* Step 1 - compute difference with previous value */
1455 diff
= val
- valpred
;
1456 sign
= (diff
< 0) ? 8 : 0;
1457 if ( sign
) diff
= (-diff
);
1459 /* Step 2 - Divide and clamp */
1461 ** This code *approximately* computes:
1462 ** delta = diff*4/step;
1463 ** vpdiff = (delta+0.5)*step/4;
1464 ** but in shift step bits are dropped. The net result of this
1465 ** is that even if you have fast mul/div hardware you cannot
1466 ** put it to good use since the fixup would be too expensive.
1469 vpdiff
= (step
>> 3);
1471 if ( diff
>= step
) {
1477 if ( diff
>= step
) {
1483 if ( diff
>= step
) {
1488 /* Step 3 - Update previous value */
1494 /* Step 4 - Clamp previous value to 16 bits */
1495 if ( valpred
> 32767 )
1497 else if ( valpred
< -32768 )
1500 /* Step 5 - Assemble value, update index and step values */
1503 index
+= indexTable
[delta
];
1504 if ( index
< 0 ) index
= 0;
1505 if ( index
> 88 ) index
= 88;
1506 step
= stepsizeTable
[index
];
1508 /* Step 6 - Output value */
1510 outputbuffer
= (delta
<< 4) & 0xf0;
1512 *ncp
++ = (delta
& 0x0f) | outputbuffer
;
1514 bufferstep
= !bufferstep
;
1516 rv
= Py_BuildValue("(O(ii))", str
, valpred
, index
);
1522 audioop_adpcm2lin(PyObject
*self
, PyObject
*args
)
1526 int len
, new_len
, size
, valpred
, step
, delta
, index
, sign
, vpdiff
;
1527 PyObject
*rv
, *str
, *state
;
1528 int i
, inputbuffer
= 0, bufferstep
;
1530 if ( !PyArg_ParseTuple(args
, "s#iO:adpcm2lin",
1531 &cp
, &len
, &size
, &state
) )
1534 if ( size
!= 1 && size
!= 2 && size
!= 4) {
1535 PyErr_SetString(AudioopError
, "Size should be 1, 2 or 4");
1539 /* Decode state, should have (value, step) */
1540 if ( state
== Py_None
) {
1541 /* First time, it seems. Set defaults */
1545 } else if ( !PyArg_ParseTuple(state
, "ii", &valpred
, &index
) )
1548 new_len
= len
*size
*2;
1550 PyErr_SetString(PyExc_MemoryError
,
1551 "not enough memory for output buffer");
1554 str
= PyBytes_FromStringAndSize(NULL
, new_len
);
1557 ncp
= (signed char *)PyBytes_AsString(str
);
1559 step
= stepsizeTable
[index
];
1562 for ( i
=0; i
< new_len
; i
+= size
) {
1563 /* Step 1 - get the delta value and compute next index */
1565 delta
= inputbuffer
& 0xf;
1567 inputbuffer
= *cp
++;
1568 delta
= (inputbuffer
>> 4) & 0xf;
1571 bufferstep
= !bufferstep
;
1573 /* Step 2 - Find new index value (for later) */
1574 index
+= indexTable
[delta
];
1575 if ( index
< 0 ) index
= 0;
1576 if ( index
> 88 ) index
= 88;
1578 /* Step 3 - Separate sign and magnitude */
1582 /* Step 4 - Compute difference and new predicted value */
1584 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
1588 if ( delta
& 4 ) vpdiff
+= step
;
1589 if ( delta
& 2 ) vpdiff
+= step
>>1;
1590 if ( delta
& 1 ) vpdiff
+= step
>>2;
1597 /* Step 5 - clamp output value */
1598 if ( valpred
> 32767 )
1600 else if ( valpred
< -32768 )
1603 /* Step 6 - Update step value */
1604 step
= stepsizeTable
[index
];
1606 /* Step 6 - Output value */
1607 if ( size
== 1 ) *CHARP(ncp
, i
) = (signed char)(valpred
>> 8);
1608 else if ( size
== 2 ) *SHORTP(ncp
, i
) = (short)(valpred
);
1609 else if ( size
== 4 ) *LONGP(ncp
, i
) = (Py_Int32
)(valpred
<<16);
1612 rv
= Py_BuildValue("(O(ii))", str
, valpred
, index
);
1617 static PyMethodDef audioop_methods
[] = {
1618 { "max", audioop_max
, METH_VARARGS
},
1619 { "minmax", audioop_minmax
, METH_VARARGS
},
1620 { "avg", audioop_avg
, METH_VARARGS
},
1621 { "maxpp", audioop_maxpp
, METH_VARARGS
},
1622 { "avgpp", audioop_avgpp
, METH_VARARGS
},
1623 { "rms", audioop_rms
, METH_VARARGS
},
1624 { "findfit", audioop_findfit
, METH_VARARGS
},
1625 { "findmax", audioop_findmax
, METH_VARARGS
},
1626 { "findfactor", audioop_findfactor
, METH_VARARGS
},
1627 { "cross", audioop_cross
, METH_VARARGS
},
1628 { "mul", audioop_mul
, METH_VARARGS
},
1629 { "add", audioop_add
, METH_VARARGS
},
1630 { "bias", audioop_bias
, METH_VARARGS
},
1631 { "ulaw2lin", audioop_ulaw2lin
, METH_VARARGS
},
1632 { "lin2ulaw", audioop_lin2ulaw
, METH_VARARGS
},
1633 { "alaw2lin", audioop_alaw2lin
, METH_VARARGS
},
1634 { "lin2alaw", audioop_lin2alaw
, METH_VARARGS
},
1635 { "lin2lin", audioop_lin2lin
, METH_VARARGS
},
1636 { "adpcm2lin", audioop_adpcm2lin
, METH_VARARGS
},
1637 { "lin2adpcm", audioop_lin2adpcm
, METH_VARARGS
},
1638 { "tomono", audioop_tomono
, METH_VARARGS
},
1639 { "tostereo", audioop_tostereo
, METH_VARARGS
},
1640 { "getsample", audioop_getsample
, METH_VARARGS
},
1641 { "reverse", audioop_reverse
, METH_VARARGS
},
1642 { "ratecv", audioop_ratecv
, METH_VARARGS
},
1647 static struct PyModuleDef audioopmodule
= {
1648 PyModuleDef_HEAD_INIT
,
1660 PyInit_audioop(void)
1663 m
= PyModule_Create(&audioopmodule
);
1666 d
= PyModule_GetDict(m
);
1669 AudioopError
= PyErr_NewException("audioop.error", NULL
, NULL
);
1670 if (AudioopError
!= NULL
)
1671 PyDict_SetItemString(d
,"error",AudioopError
);