blob: c1f4463ead54dd8c8651d7973123d5bd19284f7a [file] [log] [blame]
Guido van Rossumb66efa01992-06-01 16:01:24 +00001
Guido van Rossumb6775db1994-08-01 11:34:53 +00002/* audioopmodule - Module to detect peak values in arrays */
Jack Jansene1b4d7c1992-08-24 14:36:31 +00003
Roger E. Masseeaa6e111997-01-03 19:26:27 +00004#include "Python.h"
Guido van Rossumb66efa01992-06-01 16:01:24 +00005
Guido van Rossum69011961998-04-23 20:23:00 +00006#if SIZEOF_INT == 4
7typedef int Py_Int32;
8typedef unsigned int Py_UInt32;
9#else
10#if SIZEOF_LONG == 4
11typedef long Py_Int32;
12typedef unsigned long Py_UInt32;
13#else
14#error "No 4-byte integral type"
15#endif
16#endif
17
Anthony Baxter17471432006-03-20 05:58:21 +000018typedef short PyInt16;
19
Guido van Rossum7b1e9741994-08-29 10:46:42 +000020#if defined(__CHAR_UNSIGNED__)
21#if defined(signed)
Guido van Rossumb6775db1994-08-01 11:34:53 +000022/* This module currently does not work on systems where only unsigned
23 characters are available. Take it out of Setup. Sorry. */
24#endif
Guido van Rossum7b1e9741994-08-29 10:46:42 +000025#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000026
Anthony Baxterfa869072006-03-20 05:21:58 +000027/* Code shamelessly stolen from sox, 12.17.7, g711.c
Guido van Rossumb66efa01992-06-01 16:01:24 +000028** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
29
Anthony Baxterfa869072006-03-20 05:21:58 +000030/* From g711.c:
31 *
32 * December 30, 1994:
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
36 * corrected.
37 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
38 * bli@cpk.auc.dk
39 *
40 */
Guido van Rossumb66efa01992-06-01 16:01:24 +000041#define BIAS 0x84 /* define the add-in bias for 16 bit samples */
42#define CLIP 32635
Anthony Baxter17471432006-03-20 05:58:21 +000043#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. */
Guido van Rossumb66efa01992-06-01 16:01:24 +000047
Anthony Baxter17471432006-03-20 05:58:21 +000048static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
49 0x1FF, 0x3FF, 0x7FF, 0xFFF};
50static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
51 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
Anthony Baxterfa869072006-03-20 05:21:58 +000052
Neal Norwitz49c65d02006-03-20 06:34:06 +000053static PyInt16
54search(PyInt16 val, PyInt16 *table, int size)
Roger E. Masseeaa6e111997-01-03 19:26:27 +000055{
Anthony Baxter17471432006-03-20 05:58:21 +000056 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +000057
Anthony Baxter17471432006-03-20 05:58:21 +000058 for (i = 0; i < size; i++) {
59 if (val <= *table++)
60 return (i);
61 }
62 return (size);
Anthony Baxterfa869072006-03-20 05:21:58 +000063}
64#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
65#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
Guido van Rossumb66efa01992-06-01 16:01:24 +000066
Neal Norwitz49c65d02006-03-20 06:34:06 +000067static PyInt16 _st_ulaw2linear16[256] = {
Anthony Baxterfa869072006-03-20 05:21:58 +000068 -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,
104 24, 16, 8, 0
105};
Guido van Rossumb66efa01992-06-01 16:01:24 +0000106
Anthony Baxterfa869072006-03-20 05:21:58 +0000107/*
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
111 * 14-bits.
112 *
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:
116 *
Anthony Baxter17471432006-03-20 05:58:21 +0000117 * 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
Anthony Baxterfa869072006-03-20 05:21:58 +0000127 *
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.
132 *
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.
135 *
136 * For further information see John C. Bellamy's Digital Telephony, 1982,
137 * John Wiley & Sons, pps 98-111 and 472-476.
138 */
Neal Norwitz49c65d02006-03-20 06:34:06 +0000139static unsigned char
140st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
Anthony Baxterfa869072006-03-20 05:21:58 +0000141{
Anthony Baxter17471432006-03-20 05:58:21 +0000142 PyInt16 mask;
143 PyInt16 seg;
144 unsigned char uval;
Anthony Baxterfa869072006-03-20 05:21:58 +0000145
Anthony Baxter17471432006-03-20 05:58:21 +0000146 /* The original sox code does this in the calling function, not here */
147 pcm_val = pcm_val >> 2;
Anthony Baxterfa869072006-03-20 05:21:58 +0000148
Anthony Baxter17471432006-03-20 05:58:21 +0000149 /* u-law inverts all bits */
150 /* Get the sign and the magnitude of the value. */
151 if (pcm_val < 0) {
152 pcm_val = -pcm_val;
153 mask = 0x7F;
154 } else {
155 mask = 0xFF;
156 }
157 if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
158 pcm_val += (BIAS >> 2);
Anthony Baxterfa869072006-03-20 05:21:58 +0000159
Anthony Baxter17471432006-03-20 05:58:21 +0000160 /* Convert the scaled magnitude to segment number. */
161 seg = search(pcm_val, seg_uend, 8);
Anthony Baxterfa869072006-03-20 05:21:58 +0000162
Anthony Baxter17471432006-03-20 05:58:21 +0000163 /*
164 * Combine the sign, segment, quantization bits;
165 * and complement the code word.
166 */
167 if (seg >= 8) /* out of range, return maximum value. */
168 return (unsigned char) (0x7F ^ mask);
169 else {
170 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
171 return (uval ^ mask);
172 }
Anthony Baxterfa869072006-03-20 05:21:58 +0000173
174}
175
Neal Norwitz49c65d02006-03-20 06:34:06 +0000176static PyInt16 _st_alaw2linear16[256] = {
Anthony Baxterfa869072006-03-20 05:21:58 +0000177 -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,
213 816, 784, 880, 848
214};
215
216/*
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
220 * 13-bits.
221 *
Anthony Baxter17471432006-03-20 05:58:21 +0000222 * 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
Anthony Baxterfa869072006-03-20 05:21:58 +0000232 *
233 * For further information see John C. Bellamy's Digital Telephony, 1982,
234 * John Wiley & Sons, pps 98-111 and 472-476.
235 */
Neal Norwitz49c65d02006-03-20 06:34:06 +0000236static unsigned char
237st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
Anthony Baxterfa869072006-03-20 05:21:58 +0000238{
Anthony Baxter17471432006-03-20 05:58:21 +0000239 PyInt16 mask;
240 short seg;
241 unsigned char aval;
Anthony Baxterfa869072006-03-20 05:21:58 +0000242
Anthony Baxter17471432006-03-20 05:58:21 +0000243 /* The original sox code does this in the calling function, not here */
244 pcm_val = pcm_val >> 3;
Anthony Baxterfa869072006-03-20 05:21:58 +0000245
Anthony Baxter17471432006-03-20 05:58:21 +0000246 /* A-law using even bit inversion */
247 if (pcm_val >= 0) {
248 mask = 0xD5; /* sign (7th) bit = 1 */
249 } else {
250 mask = 0x55; /* sign bit = 0 */
251 pcm_val = -pcm_val - 1;
252 }
Anthony Baxterfa869072006-03-20 05:21:58 +0000253
Anthony Baxter17471432006-03-20 05:58:21 +0000254 /* Convert the scaled magnitude to segment number. */
255 seg = search(pcm_val, seg_aend, 8);
Anthony Baxterfa869072006-03-20 05:21:58 +0000256
Anthony Baxter17471432006-03-20 05:58:21 +0000257 /* Combine the sign, segment, and quantization bits. */
Anthony Baxterfa869072006-03-20 05:21:58 +0000258
Anthony Baxter17471432006-03-20 05:58:21 +0000259 if (seg >= 8) /* out of range, return maximum value. */
260 return (unsigned char) (0x7F ^ mask);
261 else {
262 aval = (unsigned char) seg << SEG_SHIFT;
263 if (seg < 2)
264 aval |= (pcm_val >> 1) & QUANT_MASK;
265 else
266 aval |= (pcm_val >> seg) & QUANT_MASK;
267 return (aval ^ mask);
268 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000269}
Guido van Rossumb66efa01992-06-01 16:01:24 +0000270/* End of code taken from sox */
271
Guido van Rossumb64e6351992-07-06 14:21:56 +0000272/* Intel ADPCM step variation table */
273static int indexTable[16] = {
Anthony Baxter17471432006-03-20 05:58:21 +0000274 -1, -1, -1, -1, 2, 4, 6, 8,
275 -1, -1, -1, -1, 2, 4, 6, 8,
Guido van Rossumb64e6351992-07-06 14:21:56 +0000276};
277
278static int stepsizeTable[89] = {
Anthony Baxter17471432006-03-20 05:58:21 +0000279 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
Guido van Rossumb64e6351992-07-06 14:21:56 +0000288};
Brett Cannon9824e7f2010-05-03 23:42:40 +0000289
Guido van Rossumb66efa01992-06-01 16:01:24 +0000290#define CHARP(cp, i) ((signed char *)(cp+i))
291#define SHORTP(cp, i) ((short *)(cp+i))
Guido van Rossum69011961998-04-23 20:23:00 +0000292#define LONGP(cp, i) ((Py_Int32 *)(cp+i))
Guido van Rossumb66efa01992-06-01 16:01:24 +0000293
294
295
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000296static PyObject *AudioopError;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000297
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000298static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000299audioop_getsample(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000300{
Anthony Baxter17471432006-03-20 05:58:21 +0000301 signed char *cp;
302 int len, size, val = 0;
303 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000304
Georg Brandl660222f2006-05-28 22:34:51 +0000305 if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
Anthony Baxter17471432006-03-20 05:58:21 +0000306 return 0;
307 if ( size != 1 && size != 2 && size != 4 ) {
308 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
309 return 0;
310 }
311 if ( i < 0 || i >= len/size ) {
312 PyErr_SetString(AudioopError, "Index out of range");
313 return 0;
314 }
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 PyInt_FromLong(val);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000319}
320
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000321static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000322audioop_max(PyObject *self, PyObject *args)
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000323{
Anthony Baxter17471432006-03-20 05:58:21 +0000324 signed char *cp;
325 int len, size, val = 0;
326 int i;
327 int max = 0;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000328
Georg Brandl660222f2006-05-28 22:34:51 +0000329 if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000330 return 0;
331 if ( size != 1 && size != 2 && size != 4 ) {
332 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
333 return 0;
334 }
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;
341 }
342 return PyInt_FromLong(max);
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000343}
344
345static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000346audioop_minmax(PyObject *self, PyObject *args)
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000347{
Anthony Baxter17471432006-03-20 05:58:21 +0000348 signed char *cp;
349 int len, size, val = 0;
350 int i;
351 int min = 0x7fffffff, max = -0x7fffffff;
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000352
Georg Brandl660222f2006-05-28 22:34:51 +0000353 if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
Anthony Baxter17471432006-03-20 05:58:21 +0000354 return NULL;
355 if (size != 1 && size != 2 && size != 4) {
356 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
357 return NULL;
358 }
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;
365 }
366 return Py_BuildValue("(ii)", min, max);
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000367}
368
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000369static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000370audioop_avg(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000371{
Anthony Baxter17471432006-03-20 05:58:21 +0000372 signed char *cp;
373 int len, size, val = 0;
374 int i;
375 double avg = 0.0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000376
Georg Brandl660222f2006-05-28 22:34:51 +0000377 if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000378 return 0;
379 if ( size != 1 && size != 2 && size != 4 ) {
380 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
381 return 0;
382 }
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);
387 avg += val;
388 }
389 if ( len == 0 )
390 val = 0;
391 else
392 val = (int)(avg / (double)(len/size));
393 return PyInt_FromLong(val);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000394}
395
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000396static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000397audioop_rms(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000398{
Anthony Baxter17471432006-03-20 05:58:21 +0000399 signed char *cp;
400 int len, size, val = 0;
401 int i;
402 double sum_squares = 0.0;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000403
Georg Brandl660222f2006-05-28 22:34:51 +0000404 if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000405 return 0;
406 if ( size != 1 && size != 2 && size != 4 ) {
407 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
408 return 0;
409 }
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;
415 }
416 if ( len == 0 )
417 val = 0;
418 else
419 val = (int)sqrt(sum_squares / (double)(len/size));
420 return PyInt_FromLong(val);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000421}
422
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000423static double _sum2(short *a, short *b, int len)
Jack Jansena90805f1993-02-17 14:29:28 +0000424{
Anthony Baxter17471432006-03-20 05:58:21 +0000425 int i;
426 double sum = 0.0;
Jack Jansena90805f1993-02-17 14:29:28 +0000427
Anthony Baxter17471432006-03-20 05:58:21 +0000428 for( i=0; i<len; i++) {
429 sum = sum + (double)a[i]*(double)b[i];
430 }
431 return sum;
Jack Jansena90805f1993-02-17 14:29:28 +0000432}
433
434/*
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:
439**
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.
442**
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).
446**
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 )
451**
452** In the code variables correspond as follows:
Anthony Baxter17471432006-03-20 05:58:21 +0000453** cp1 A
454** cp2 R
455** len1 N
456** len2 n
457** aj_m1 A[j-1]
458** aj_lm1 A[j+n-1]
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])
Jack Jansena90805f1993-02-17 14:29:28 +0000462**
463** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
464** is completely recalculated each step.
465*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000466static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000467audioop_findfit(PyObject *self, PyObject *args)
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000468{
Anthony Baxter17471432006-03-20 05:58:21 +0000469 short *cp1, *cp2;
470 int len1, len2;
471 int j, best_j;
472 double aj_m1, aj_lm1;
473 double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000474
Martin v. Löwisa811c382006-10-19 11:00:37 +0000475 /* Passing a short** for an 's' argument is correct only
476 if the string contents is aligned for interpretation
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000477 as short[]. Due to the definition of PyStringObject,
Martin v. Löwisa811c382006-10-19 11:00:37 +0000478 this is currently (Python 2.6) the case. */
Georg Brandl660222f2006-05-28 22:34:51 +0000479 if ( !PyArg_ParseTuple(args, "s#s#:findfit",
Martin v. Löwisa811c382006-10-19 11:00:37 +0000480 (char**)&cp1, &len1, (char**)&cp2, &len2) )
Anthony Baxter17471432006-03-20 05:58:21 +0000481 return 0;
482 if ( len1 & 1 || len2 & 1 ) {
483 PyErr_SetString(AudioopError, "Strings should be even-sized");
484 return 0;
485 }
486 len1 >>= 1;
487 len2 >>= 1;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000488
Anthony Baxter17471432006-03-20 05:58:21 +0000489 if ( len1 < len2 ) {
490 PyErr_SetString(AudioopError, "First sample should be longer");
491 return 0;
492 }
493 sum_ri_2 = _sum2(cp2, cp2, len2);
494 sum_aij_2 = _sum2(cp1, cp1, len2);
495 sum_aij_ri = _sum2(cp1, cp2, len2);
Jack Jansena90805f1993-02-17 14:29:28 +0000496
Anthony Baxter17471432006-03-20 05:58:21 +0000497 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
Jack Jansena90805f1993-02-17 14:29:28 +0000498
Anthony Baxter17471432006-03-20 05:58:21 +0000499 best_result = result;
500 best_j = 0;
Jack Jansena90805f1993-02-17 14:29:28 +0000501
Brett Cannon9824e7f2010-05-03 23:42:40 +0000502 for (j=1; j<=len1-len2; j++) {
Anthony Baxter17471432006-03-20 05:58:21 +0000503 aj_m1 = (double)cp1[j-1];
504 aj_lm1 = (double)cp1[j+len2-1];
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000505
Anthony Baxter17471432006-03-20 05:58:21 +0000506 sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
507 sum_aij_ri = _sum2(cp1+j, cp2, len2);
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000508
Anthony Baxter17471432006-03-20 05:58:21 +0000509 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
510 / sum_aij_2;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000511
Anthony Baxter17471432006-03-20 05:58:21 +0000512 if ( result < best_result ) {
513 best_result = result;
514 best_j = j;
515 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000516
Anthony Baxter17471432006-03-20 05:58:21 +0000517 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000518
Anthony Baxter17471432006-03-20 05:58:21 +0000519 factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000520
Anthony Baxter17471432006-03-20 05:58:21 +0000521 return Py_BuildValue("(if)", best_j, factor);
Jack Jansena90805f1993-02-17 14:29:28 +0000522}
523
524/*
525** findfactor finds a factor f so that the energy in A-fB is minimal.
526** See the comment for findfit for details.
527*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000528static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000529audioop_findfactor(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +0000530{
Anthony Baxter17471432006-03-20 05:58:21 +0000531 short *cp1, *cp2;
532 int len1, len2;
533 double sum_ri_2, sum_aij_ri, result;
Jack Jansena90805f1993-02-17 14:29:28 +0000534
Georg Brandl660222f2006-05-28 22:34:51 +0000535 if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
Martin v. Löwisa811c382006-10-19 11:00:37 +0000536 (char**)&cp1, &len1, (char**)&cp2, &len2) )
Anthony Baxter17471432006-03-20 05:58:21 +0000537 return 0;
538 if ( len1 & 1 || len2 & 1 ) {
539 PyErr_SetString(AudioopError, "Strings should be even-sized");
540 return 0;
541 }
542 if ( len1 != len2 ) {
543 PyErr_SetString(AudioopError, "Samples should be same size");
544 return 0;
545 }
546 len2 >>= 1;
547 sum_ri_2 = _sum2(cp2, cp2, len2);
548 sum_aij_ri = _sum2(cp1, cp2, len2);
Jack Jansena90805f1993-02-17 14:29:28 +0000549
Anthony Baxter17471432006-03-20 05:58:21 +0000550 result = sum_aij_ri / sum_ri_2;
Jack Jansena90805f1993-02-17 14:29:28 +0000551
Anthony Baxter17471432006-03-20 05:58:21 +0000552 return PyFloat_FromDouble(result);
Jack Jansena90805f1993-02-17 14:29:28 +0000553}
554
555/*
556** findmax returns the index of the n-sized segment of the input sample
557** that contains the most energy.
558*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000559static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000560audioop_findmax(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +0000561{
Anthony Baxter17471432006-03-20 05:58:21 +0000562 short *cp1;
563 int len1, len2;
564 int j, best_j;
565 double aj_m1, aj_lm1;
566 double result, best_result;
Jack Jansena90805f1993-02-17 14:29:28 +0000567
Brett Cannon9824e7f2010-05-03 23:42:40 +0000568 if ( !PyArg_ParseTuple(args, "s#i:findmax",
Martin v. Löwisa811c382006-10-19 11:00:37 +0000569 (char**)&cp1, &len1, &len2) )
Anthony Baxter17471432006-03-20 05:58:21 +0000570 return 0;
571 if ( len1 & 1 ) {
572 PyErr_SetString(AudioopError, "Strings should be even-sized");
573 return 0;
574 }
575 len1 >>= 1;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000576
Facundo Batista1a664412008-07-07 17:02:59 +0000577 if ( len2 < 0 || len1 < len2 ) {
Anthony Baxter17471432006-03-20 05:58:21 +0000578 PyErr_SetString(AudioopError, "Input sample should be longer");
579 return 0;
580 }
Jack Jansena90805f1993-02-17 14:29:28 +0000581
Anthony Baxter17471432006-03-20 05:58:21 +0000582 result = _sum2(cp1, cp1, len2);
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000583
Anthony Baxter17471432006-03-20 05:58:21 +0000584 best_result = result;
585 best_j = 0;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000586
Brett Cannon9824e7f2010-05-03 23:42:40 +0000587 for (j=1; j<=len1-len2; j++) {
Anthony Baxter17471432006-03-20 05:58:21 +0000588 aj_m1 = (double)cp1[j-1];
589 aj_lm1 = (double)cp1[j+len2-1];
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000590
Anthony Baxter17471432006-03-20 05:58:21 +0000591 result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000592
Anthony Baxter17471432006-03-20 05:58:21 +0000593 if ( result > best_result ) {
594 best_result = result;
595 best_j = j;
596 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000597
Anthony Baxter17471432006-03-20 05:58:21 +0000598 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000599
Anthony Baxter17471432006-03-20 05:58:21 +0000600 return PyInt_FromLong(best_j);
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000601}
602
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000603static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000604audioop_avgpp(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000605{
Anthony Baxter17471432006-03-20 05:58:21 +0000606 signed char *cp;
607 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
608 prevextreme = 0;
609 int i;
610 double avg = 0.0;
611 int diff, prevdiff, extremediff, nextreme = 0;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000612
Georg Brandl660222f2006-05-28 22:34:51 +0000613 if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000614 return 0;
615 if ( size != 1 && size != 2 && size != 4 ) {
616 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
617 return 0;
618 }
619 /* Compute first delta value ahead. Also automatically makes us
620 ** skip the first extreme value
621 */
622 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
623 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
624 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
625 if ( size == 1 ) val = (int)*CHARP(cp, size);
626 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
627 else if ( size == 4 ) val = (int)*LONGP(cp, size);
628 prevdiff = val - prevval;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000629
Anthony Baxter17471432006-03-20 05:58:21 +0000630 for ( i=size; i<len; i+= size) {
631 if ( size == 1 ) val = (int)*CHARP(cp, i);
632 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
633 else if ( size == 4 ) val = (int)*LONGP(cp, i);
634 diff = val - prevval;
635 if ( diff*prevdiff < 0 ) {
636 /* Derivative changed sign. Compute difference to last
637 ** extreme value and remember.
638 */
639 if ( prevextremevalid ) {
640 extremediff = prevval - prevextreme;
641 if ( extremediff < 0 )
642 extremediff = -extremediff;
643 avg += extremediff;
644 nextreme++;
645 }
646 prevextremevalid = 1;
647 prevextreme = prevval;
648 }
649 prevval = val;
650 if ( diff != 0 )
Brett Cannon9824e7f2010-05-03 23:42:40 +0000651 prevdiff = diff;
Anthony Baxter17471432006-03-20 05:58:21 +0000652 }
653 if ( nextreme == 0 )
654 val = 0;
655 else
656 val = (int)(avg / (double)nextreme);
657 return PyInt_FromLong(val);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000658}
659
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000660static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000661audioop_maxpp(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000662{
Anthony Baxter17471432006-03-20 05:58:21 +0000663 signed char *cp;
664 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
665 prevextreme = 0;
666 int i;
667 int max = 0;
668 int diff, prevdiff, extremediff;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000669
Georg Brandl660222f2006-05-28 22:34:51 +0000670 if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000671 return 0;
672 if ( size != 1 && size != 2 && size != 4 ) {
673 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
674 return 0;
675 }
676 /* Compute first delta value ahead. Also automatically makes us
677 ** skip the first extreme value
678 */
679 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
680 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
681 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
682 if ( size == 1 ) val = (int)*CHARP(cp, size);
683 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
684 else if ( size == 4 ) val = (int)*LONGP(cp, size);
685 prevdiff = val - prevval;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000686
Anthony Baxter17471432006-03-20 05:58:21 +0000687 for ( i=size; i<len; i+= size) {
688 if ( size == 1 ) val = (int)*CHARP(cp, i);
689 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
690 else if ( size == 4 ) val = (int)*LONGP(cp, i);
691 diff = val - prevval;
692 if ( diff*prevdiff < 0 ) {
693 /* Derivative changed sign. Compute difference to
694 ** last extreme value and remember.
695 */
696 if ( prevextremevalid ) {
697 extremediff = prevval - prevextreme;
698 if ( extremediff < 0 )
699 extremediff = -extremediff;
700 if ( extremediff > max )
701 max = extremediff;
702 }
703 prevextremevalid = 1;
704 prevextreme = prevval;
705 }
706 prevval = val;
707 if ( diff != 0 )
708 prevdiff = diff;
709 }
710 return PyInt_FromLong(max);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000711}
712
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000713static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000714audioop_cross(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000715{
Anthony Baxter17471432006-03-20 05:58:21 +0000716 signed char *cp;
717 int len, size, val = 0;
718 int i;
719 int prevval, ncross;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000720
Georg Brandl660222f2006-05-28 22:34:51 +0000721 if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +0000722 return 0;
723 if ( size != 1 && size != 2 && size != 4 ) {
724 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
725 return 0;
726 }
727 ncross = -1;
728 prevval = 17; /* Anything <> 0,1 */
729 for ( i=0; i<len; i+= size) {
730 if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
731 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
732 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
733 val = val & 1;
734 if ( val != prevval ) ncross++;
735 prevval = val;
736 }
737 return PyInt_FromLong(ncross);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000738}
739
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000740static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000741audioop_mul(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000742{
Anthony Baxter17471432006-03-20 05:58:21 +0000743 signed char *cp, *ncp;
744 int len, size, val = 0;
745 double factor, fval, maxval;
746 PyObject *rv;
747 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000748
Georg Brandl660222f2006-05-28 22:34:51 +0000749 if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
Anthony Baxter17471432006-03-20 05:58:21 +0000750 return 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000751
Anthony Baxter17471432006-03-20 05:58:21 +0000752 if ( size == 1 ) maxval = (double) 0x7f;
753 else if ( size == 2 ) maxval = (double) 0x7fff;
754 else if ( size == 4 ) maxval = (double) 0x7fffffff;
755 else {
756 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
757 return 0;
758 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000759
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000760 rv = PyString_FromStringAndSize(NULL, len);
Anthony Baxter17471432006-03-20 05:58:21 +0000761 if ( rv == 0 )
762 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000763 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000764
765
Anthony Baxter17471432006-03-20 05:58:21 +0000766 for ( i=0; i < len; i += size ) {
767 if ( size == 1 ) val = (int)*CHARP(cp, i);
768 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
769 else if ( size == 4 ) val = (int)*LONGP(cp, i);
770 fval = (double)val*factor;
771 if ( fval > maxval ) fval = maxval;
772 else if ( fval < -maxval ) fval = -maxval;
773 val = (int)fval;
774 if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
775 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
776 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
777 }
778 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000779}
780
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000781static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000782audioop_tomono(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000783{
Anthony Baxter17471432006-03-20 05:58:21 +0000784 signed char *cp, *ncp;
785 int len, size, val1 = 0, val2 = 0;
786 double fac1, fac2, fval, maxval;
787 PyObject *rv;
788 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000789
Georg Brandl660222f2006-05-28 22:34:51 +0000790 if ( !PyArg_ParseTuple(args, "s#idd:tomono",
791 &cp, &len, &size, &fac1, &fac2 ) )
Anthony Baxter17471432006-03-20 05:58:21 +0000792 return 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000793
Anthony Baxter17471432006-03-20 05:58:21 +0000794 if ( size == 1 ) maxval = (double) 0x7f;
795 else if ( size == 2 ) maxval = (double) 0x7fff;
796 else if ( size == 4 ) maxval = (double) 0x7fffffff;
797 else {
798 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
799 return 0;
800 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000801
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000802 rv = PyString_FromStringAndSize(NULL, len/2);
Anthony Baxter17471432006-03-20 05:58:21 +0000803 if ( rv == 0 )
804 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000805 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000806
807
Anthony Baxter17471432006-03-20 05:58:21 +0000808 for ( i=0; i < len; i += size*2 ) {
809 if ( size == 1 ) val1 = (int)*CHARP(cp, i);
810 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
811 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
812 if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
813 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
814 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
815 fval = (double)val1*fac1 + (double)val2*fac2;
816 if ( fval > maxval ) fval = maxval;
817 else if ( fval < -maxval ) fval = -maxval;
818 val1 = (int)fval;
819 if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
820 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
821 else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
822 }
823 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000824}
825
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000826static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000827audioop_tostereo(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000828{
Anthony Baxter17471432006-03-20 05:58:21 +0000829 signed char *cp, *ncp;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000830 int len, new_len, size, val1, val2, val = 0;
Anthony Baxter17471432006-03-20 05:58:21 +0000831 double fac1, fac2, fval, maxval;
832 PyObject *rv;
833 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000834
Georg Brandl660222f2006-05-28 22:34:51 +0000835 if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
836 &cp, &len, &size, &fac1, &fac2 ) )
Anthony Baxter17471432006-03-20 05:58:21 +0000837 return 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +0000838
Anthony Baxter17471432006-03-20 05:58:21 +0000839 if ( size == 1 ) maxval = (double) 0x7f;
840 else if ( size == 2 ) maxval = (double) 0x7fff;
841 else if ( size == 4 ) maxval = (double) 0x7fffffff;
842 else {
843 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
844 return 0;
845 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000846
Gregory P. Smith9d534572008-06-11 07:41:16 +0000847 new_len = len*2;
848 if (new_len < 0) {
849 PyErr_SetString(PyExc_MemoryError,
850 "not enough memory for output buffer");
851 return 0;
852 }
853
854 rv = PyString_FromStringAndSize(NULL, new_len);
Anthony Baxter17471432006-03-20 05:58:21 +0000855 if ( rv == 0 )
856 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000857 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000858
859
Anthony Baxter17471432006-03-20 05:58:21 +0000860 for ( i=0; i < len; i += size ) {
861 if ( size == 1 ) val = (int)*CHARP(cp, i);
862 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
863 else if ( size == 4 ) val = (int)*LONGP(cp, i);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000864
Anthony Baxter17471432006-03-20 05:58:21 +0000865 fval = (double)val*fac1;
866 if ( fval > maxval ) fval = maxval;
867 else if ( fval < -maxval ) fval = -maxval;
868 val1 = (int)fval;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000869
Anthony Baxter17471432006-03-20 05:58:21 +0000870 fval = (double)val*fac2;
871 if ( fval > maxval ) fval = maxval;
872 else if ( fval < -maxval ) fval = -maxval;
873 val2 = (int)fval;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000874
Anthony Baxter17471432006-03-20 05:58:21 +0000875 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
876 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
877 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000878
Anthony Baxter17471432006-03-20 05:58:21 +0000879 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
880 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
881 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
882 }
883 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000884}
885
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000886static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000887audioop_add(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000888{
Anthony Baxter17471432006-03-20 05:58:21 +0000889 signed char *cp1, *cp2, *ncp;
890 int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
891 PyObject *rv;
892 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000893
Georg Brandl660222f2006-05-28 22:34:51 +0000894 if ( !PyArg_ParseTuple(args, "s#s#i:add",
Anthony Baxter17471432006-03-20 05:58:21 +0000895 &cp1, &len1, &cp2, &len2, &size ) )
896 return 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000897
Anthony Baxter17471432006-03-20 05:58:21 +0000898 if ( len1 != len2 ) {
899 PyErr_SetString(AudioopError, "Lengths should be the same");
900 return 0;
901 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000902
Anthony Baxter17471432006-03-20 05:58:21 +0000903 if ( size == 1 ) maxval = 0x7f;
904 else if ( size == 2 ) maxval = 0x7fff;
905 else if ( size == 4 ) maxval = 0x7fffffff;
906 else {
907 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
908 return 0;
909 }
Guido van Rossum1851a671997-02-14 16:14:03 +0000910
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000911 rv = PyString_FromStringAndSize(NULL, len1);
Anthony Baxter17471432006-03-20 05:58:21 +0000912 if ( rv == 0 )
913 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000914 ncp = (signed char *)PyString_AsString(rv);
Guido van Rossum1851a671997-02-14 16:14:03 +0000915
Anthony Baxter17471432006-03-20 05:58:21 +0000916 for ( i=0; i < len1; i += size ) {
917 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
918 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
919 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000920
Anthony Baxter17471432006-03-20 05:58:21 +0000921 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
922 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
923 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000924
Anthony Baxter17471432006-03-20 05:58:21 +0000925 newval = val1 + val2;
926 /* truncate in case of overflow */
927 if (newval > maxval) newval = maxval;
928 else if (newval < -maxval) newval = -maxval;
929 else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
930 newval = val1 > 0 ? maxval : - maxval;
Guido van Rossum1851a671997-02-14 16:14:03 +0000931
Anthony Baxter17471432006-03-20 05:58:21 +0000932 if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
933 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
934 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
935 }
936 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000937}
938
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000939static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000940audioop_bias(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000941{
Anthony Baxter17471432006-03-20 05:58:21 +0000942 signed char *cp, *ncp;
943 int len, size, val = 0;
944 PyObject *rv;
945 int i;
946 int bias;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000947
Georg Brandl660222f2006-05-28 22:34:51 +0000948 if ( !PyArg_ParseTuple(args, "s#ii:bias",
Anthony Baxter17471432006-03-20 05:58:21 +0000949 &cp, &len, &size , &bias) )
950 return 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000951
Anthony Baxter17471432006-03-20 05:58:21 +0000952 if ( size != 1 && size != 2 && size != 4) {
953 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
954 return 0;
955 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000956
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000957 rv = PyString_FromStringAndSize(NULL, len);
Anthony Baxter17471432006-03-20 05:58:21 +0000958 if ( rv == 0 )
959 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000960 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000961
962
Anthony Baxter17471432006-03-20 05:58:21 +0000963 for ( i=0; i < len; i += size ) {
964 if ( size == 1 ) val = (int)*CHARP(cp, i);
965 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
966 else if ( size == 4 ) val = (int)*LONGP(cp, i);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000967
Anthony Baxter17471432006-03-20 05:58:21 +0000968 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
969 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
970 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
971 }
972 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000973}
974
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000975static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000976audioop_reverse(PyObject *self, PyObject *args)
Jack Jansen337b20e1993-02-23 13:39:57 +0000977{
Anthony Baxter17471432006-03-20 05:58:21 +0000978 signed char *cp;
979 unsigned char *ncp;
980 int len, size, val = 0;
981 PyObject *rv;
982 int i, j;
Jack Jansen337b20e1993-02-23 13:39:57 +0000983
Georg Brandl660222f2006-05-28 22:34:51 +0000984 if ( !PyArg_ParseTuple(args, "s#i:reverse",
Anthony Baxter17471432006-03-20 05:58:21 +0000985 &cp, &len, &size) )
986 return 0;
Jack Jansen337b20e1993-02-23 13:39:57 +0000987
Anthony Baxter17471432006-03-20 05:58:21 +0000988 if ( size != 1 && size != 2 && size != 4 ) {
989 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
990 return 0;
991 }
Brett Cannon9824e7f2010-05-03 23:42:40 +0000992
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000993 rv = PyString_FromStringAndSize(NULL, len);
Anthony Baxter17471432006-03-20 05:58:21 +0000994 if ( rv == 0 )
995 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000996 ncp = (unsigned char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +0000997
Anthony Baxter17471432006-03-20 05:58:21 +0000998 for ( i=0; i < len; i += size ) {
999 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1000 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1001 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
Jack Jansen337b20e1993-02-23 13:39:57 +00001002
Anthony Baxter17471432006-03-20 05:58:21 +00001003 j = len - i - size;
Brett Cannon9824e7f2010-05-03 23:42:40 +00001004
Anthony Baxter17471432006-03-20 05:58:21 +00001005 if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1006 else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
1007 else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1008 }
1009 return rv;
Jack Jansen337b20e1993-02-23 13:39:57 +00001010}
1011
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001012static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001013audioop_lin2lin(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +00001014{
Anthony Baxter17471432006-03-20 05:58:21 +00001015 signed char *cp;
1016 unsigned char *ncp;
Gregory P. Smith9d534572008-06-11 07:41:16 +00001017 int len, new_len, size, size2, val = 0;
Anthony Baxter17471432006-03-20 05:58:21 +00001018 PyObject *rv;
1019 int i, j;
Jack Jansena90805f1993-02-17 14:29:28 +00001020
Georg Brandl660222f2006-05-28 22:34:51 +00001021 if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
Anthony Baxter17471432006-03-20 05:58:21 +00001022 &cp, &len, &size, &size2) )
1023 return 0;
Jack Jansena90805f1993-02-17 14:29:28 +00001024
Anthony Baxter17471432006-03-20 05:58:21 +00001025 if ( (size != 1 && size != 2 && size != 4) ||
1026 (size2 != 1 && size2 != 2 && size2 != 4)) {
1027 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1028 return 0;
1029 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001030
Gregory P. Smith9d534572008-06-11 07:41:16 +00001031 new_len = (len/size)*size2;
1032 if (new_len < 0) {
1033 PyErr_SetString(PyExc_MemoryError,
1034 "not enough memory for output buffer");
1035 return 0;
1036 }
1037 rv = PyString_FromStringAndSize(NULL, new_len);
Anthony Baxter17471432006-03-20 05:58:21 +00001038 if ( rv == 0 )
1039 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001040 ncp = (unsigned char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001041
Anthony Baxter17471432006-03-20 05:58:21 +00001042 for ( i=0, j=0; i < len; i += size, j += size2 ) {
1043 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1044 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1045 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
Jack Jansena90805f1993-02-17 14:29:28 +00001046
Anthony Baxter17471432006-03-20 05:58:21 +00001047 if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1048 else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
1049 else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1050 }
1051 return rv;
Jack Jansena90805f1993-02-17 14:29:28 +00001052}
1053
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001054static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001055gcd(int a, int b)
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001056{
Anthony Baxter17471432006-03-20 05:58:21 +00001057 while (b > 0) {
1058 int tmp = a % b;
1059 a = b;
1060 b = tmp;
1061 }
1062 return a;
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001063}
1064
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001065static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001066audioop_ratecv(PyObject *self, PyObject *args)
Roger E. Massec905fff1997-01-17 18:12:04 +00001067{
Anthony Baxter17471432006-03-20 05:58:21 +00001068 char *cp, *ncp;
1069 int len, size, nchannels, inrate, outrate, weightA, weightB;
1070 int chan, d, *prev_i, *cur_i, cur_o;
1071 PyObject *state, *samps, *str, *rv = NULL;
1072 int bytes_per_frame;
Gregory P. Smith9d534572008-06-11 07:41:16 +00001073 size_t alloc_size;
Guido van Rossum1851a671997-02-14 16:14:03 +00001074
Anthony Baxter17471432006-03-20 05:58:21 +00001075 weightA = 1;
1076 weightB = 0;
Georg Brandl660222f2006-05-28 22:34:51 +00001077 if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
1078 &nchannels, &inrate, &outrate, &state,
1079 &weightA, &weightB))
Anthony Baxter17471432006-03-20 05:58:21 +00001080 return NULL;
1081 if (size != 1 && size != 2 && size != 4) {
1082 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1083 return NULL;
1084 }
1085 if (nchannels < 1) {
1086 PyErr_SetString(AudioopError, "# of channels should be >= 1");
1087 return NULL;
1088 }
1089 bytes_per_frame = size * nchannels;
1090 if (bytes_per_frame / nchannels != size) {
1091 /* This overflow test is rigorously correct because
1092 both multiplicands are >= 1. Use the argument names
1093 from the docs for the error msg. */
1094 PyErr_SetString(PyExc_OverflowError,
1095 "width * nchannels too big for a C int");
1096 return NULL;
1097 }
1098 if (weightA < 1 || weightB < 0) {
1099 PyErr_SetString(AudioopError,
1100 "weightA should be >= 1, weightB should be >= 0");
1101 return NULL;
1102 }
1103 if (len % bytes_per_frame != 0) {
1104 PyErr_SetString(AudioopError, "not a whole number of frames");
1105 return NULL;
1106 }
1107 if (inrate <= 0 || outrate <= 0) {
1108 PyErr_SetString(AudioopError, "sampling rate not > 0");
1109 return NULL;
1110 }
1111 /* divide inrate and outrate by their greatest common divisor */
1112 d = gcd(inrate, outrate);
1113 inrate /= d;
1114 outrate /= d;
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001115
Gregory P. Smith9d534572008-06-11 07:41:16 +00001116 alloc_size = sizeof(int) * (unsigned)nchannels;
Raymond Hettinger1672dc62009-06-10 16:15:40 +00001117 if (alloc_size < (unsigned)nchannels) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00001118 PyErr_SetString(PyExc_MemoryError,
1119 "not enough memory for output buffer");
1120 return 0;
1121 }
1122 prev_i = (int *) malloc(alloc_size);
1123 cur_i = (int *) malloc(alloc_size);
Anthony Baxter17471432006-03-20 05:58:21 +00001124 if (prev_i == NULL || cur_i == NULL) {
1125 (void) PyErr_NoMemory();
1126 goto exit;
1127 }
Guido van Rossum1851a671997-02-14 16:14:03 +00001128
Anthony Baxter17471432006-03-20 05:58:21 +00001129 len /= bytes_per_frame; /* # of frames */
Tim Peters1691bd92001-12-05 06:05:07 +00001130
Anthony Baxter17471432006-03-20 05:58:21 +00001131 if (state == Py_None) {
1132 d = -outrate;
1133 for (chan = 0; chan < nchannels; chan++)
1134 prev_i[chan] = cur_i[chan] = 0;
1135 }
1136 else {
1137 if (!PyArg_ParseTuple(state,
1138 "iO!;audioop.ratecv: illegal state argument",
1139 &d, &PyTuple_Type, &samps))
1140 goto exit;
1141 if (PyTuple_Size(samps) != nchannels) {
1142 PyErr_SetString(AudioopError,
1143 "illegal state argument");
1144 goto exit;
1145 }
1146 for (chan = 0; chan < nchannels; chan++) {
1147 if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
Brett Cannon9824e7f2010-05-03 23:42:40 +00001148 "ii:ratecv", &prev_i[chan],
Georg Brandl660222f2006-05-28 22:34:51 +00001149 &cur_i[chan]))
Anthony Baxter17471432006-03-20 05:58:21 +00001150 goto exit;
1151 }
1152 }
Tim Peters1691bd92001-12-05 06:05:07 +00001153
Anthony Baxter17471432006-03-20 05:58:21 +00001154 /* str <- Space for the output buffer. */
1155 {
1156 /* There are len input frames, so we need (mathematically)
1157 ceiling(len*outrate/inrate) output frames, and each frame
1158 requires bytes_per_frame bytes. Computing this
1159 without spurious overflow is the challenge; we can
1160 settle for a reasonable upper bound, though. */
1161 int ceiling; /* the number of output frames */
1162 int nbytes; /* the number of output bytes needed */
1163 int q = len / inrate;
1164 /* Now len = q * inrate + r exactly (with r = len % inrate),
1165 and this is less than q * inrate + inrate = (q+1)*inrate.
1166 So a reasonable upper bound on len*outrate/inrate is
1167 ((q+1)*inrate)*outrate/inrate =
1168 (q+1)*outrate.
1169 */
1170 ceiling = (q+1) * outrate;
1171 nbytes = ceiling * bytes_per_frame;
1172 /* See whether anything overflowed; if not, get the space. */
1173 if (q+1 < 0 ||
1174 ceiling / outrate != q+1 ||
1175 nbytes / bytes_per_frame != ceiling)
1176 str = NULL;
1177 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001178 str = PyString_FromStringAndSize(NULL, nbytes);
Tim Peterseb4b7ba2001-12-07 00:37:39 +00001179
Anthony Baxter17471432006-03-20 05:58:21 +00001180 if (str == NULL) {
1181 PyErr_SetString(PyExc_MemoryError,
1182 "not enough memory for output buffer");
1183 goto exit;
1184 }
1185 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001186 ncp = PyString_AsString(str);
Guido van Rossum1851a671997-02-14 16:14:03 +00001187
Anthony Baxter17471432006-03-20 05:58:21 +00001188 for (;;) {
1189 while (d < 0) {
1190 if (len == 0) {
1191 samps = PyTuple_New(nchannels);
1192 if (samps == NULL)
1193 goto exit;
1194 for (chan = 0; chan < nchannels; chan++)
1195 PyTuple_SetItem(samps, chan,
1196 Py_BuildValue("(ii)",
1197 prev_i[chan],
1198 cur_i[chan]));
1199 if (PyErr_Occurred())
1200 goto exit;
1201 /* We have checked before that the length
1202 * of the string fits into int. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001203 len = (int)(ncp - PyString_AsString(str));
Anthony Baxter17471432006-03-20 05:58:21 +00001204 if (len == 0) {
1205 /*don't want to resize to zero length*/
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001206 rv = PyString_FromStringAndSize("", 0);
Anthony Baxter17471432006-03-20 05:58:21 +00001207 Py_DECREF(str);
1208 str = rv;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 } else if (_PyString_Resize(&str, len) < 0)
Anthony Baxter17471432006-03-20 05:58:21 +00001210 goto exit;
1211 rv = Py_BuildValue("(O(iO))", str, d, samps);
1212 Py_DECREF(samps);
1213 Py_DECREF(str);
1214 goto exit; /* return rv */
1215 }
1216 for (chan = 0; chan < nchannels; chan++) {
1217 prev_i[chan] = cur_i[chan];
1218 if (size == 1)
1219 cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
1220 else if (size == 2)
1221 cur_i[chan] = (int)*SHORTP(cp, 0);
1222 else if (size == 4)
1223 cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
1224 cp += size;
1225 /* implements a simple digital filter */
1226 cur_i[chan] =
1227 (weightA * cur_i[chan] +
1228 weightB * prev_i[chan]) /
1229 (weightA + weightB);
1230 }
1231 len--;
1232 d += outrate;
1233 }
1234 while (d >= 0) {
1235 for (chan = 0; chan < nchannels; chan++) {
1236 cur_o = (prev_i[chan] * d +
1237 cur_i[chan] * (outrate - d)) /
1238 outrate;
1239 if (size == 1)
1240 *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
1241 else if (size == 2)
1242 *SHORTP(ncp, 0) = (short)(cur_o);
1243 else if (size == 4)
1244 *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
1245 ncp += size;
1246 }
1247 d -= inrate;
1248 }
1249 }
Guido van Rossum65bb3281999-09-07 14:24:05 +00001250 exit:
Anthony Baxter17471432006-03-20 05:58:21 +00001251 if (prev_i != NULL)
1252 free(prev_i);
1253 if (cur_i != NULL)
1254 free(cur_i);
1255 return rv;
Roger E. Massec905fff1997-01-17 18:12:04 +00001256}
Guido van Rossum1851a671997-02-14 16:14:03 +00001257
Roger E. Massec905fff1997-01-17 18:12:04 +00001258static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001259audioop_lin2ulaw(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001260{
Anthony Baxter17471432006-03-20 05:58:21 +00001261 signed char *cp;
1262 unsigned char *ncp;
1263 int len, size, val = 0;
1264 PyObject *rv;
1265 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001266
Georg Brandl660222f2006-05-28 22:34:51 +00001267 if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
1268 &cp, &len, &size) )
1269 return 0 ;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001270
Anthony Baxter17471432006-03-20 05:58:21 +00001271 if ( size != 1 && size != 2 && size != 4) {
1272 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1273 return 0;
1274 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001275
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001276 rv = PyString_FromStringAndSize(NULL, len/size);
Anthony Baxter17471432006-03-20 05:58:21 +00001277 if ( rv == 0 )
1278 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001279 ncp = (unsigned char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001280
Anthony Baxter17471432006-03-20 05:58:21 +00001281 for ( i=0; i < len; i += size ) {
1282 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1283 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1284 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001285
Anthony Baxter17471432006-03-20 05:58:21 +00001286 *ncp++ = st_14linear2ulaw(val);
1287 }
1288 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001289}
1290
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001291static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001292audioop_ulaw2lin(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001293{
Anthony Baxter17471432006-03-20 05:58:21 +00001294 unsigned char *cp;
1295 unsigned char cval;
1296 signed char *ncp;
Gregory P. Smith9d534572008-06-11 07:41:16 +00001297 int len, new_len, size, val;
Anthony Baxter17471432006-03-20 05:58:21 +00001298 PyObject *rv;
1299 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001300
Georg Brandl660222f2006-05-28 22:34:51 +00001301 if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
1302 &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +00001303 return 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001304
Anthony Baxter17471432006-03-20 05:58:21 +00001305 if ( size != 1 && size != 2 && size != 4) {
1306 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1307 return 0;
1308 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001309
Gregory P. Smith9d534572008-06-11 07:41:16 +00001310 new_len = len*size;
1311 if (new_len < 0) {
1312 PyErr_SetString(PyExc_MemoryError,
1313 "not enough memory for output buffer");
1314 return 0;
1315 }
1316 rv = PyString_FromStringAndSize(NULL, new_len);
Anthony Baxter17471432006-03-20 05:58:21 +00001317 if ( rv == 0 )
1318 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001319 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001320
Gregory P. Smith9d534572008-06-11 07:41:16 +00001321 for ( i=0; i < new_len; i += size ) {
Anthony Baxter17471432006-03-20 05:58:21 +00001322 cval = *cp++;
1323 val = st_ulaw2linear16(cval);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001324
Anthony Baxter17471432006-03-20 05:58:21 +00001325 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1326 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1327 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1328 }
1329 return rv;
Anthony Baxterfa869072006-03-20 05:21:58 +00001330}
1331
1332static PyObject *
1333audioop_lin2alaw(PyObject *self, PyObject *args)
1334{
Anthony Baxter17471432006-03-20 05:58:21 +00001335 signed char *cp;
1336 unsigned char *ncp;
1337 int len, size, val = 0;
1338 PyObject *rv;
1339 int i;
Anthony Baxterfa869072006-03-20 05:21:58 +00001340
Georg Brandl660222f2006-05-28 22:34:51 +00001341 if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
1342 &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +00001343 return 0;
Anthony Baxterfa869072006-03-20 05:21:58 +00001344
Anthony Baxter17471432006-03-20 05:58:21 +00001345 if ( size != 1 && size != 2 && size != 4) {
1346 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1347 return 0;
1348 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001349
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001350 rv = PyString_FromStringAndSize(NULL, len/size);
Anthony Baxter17471432006-03-20 05:58:21 +00001351 if ( rv == 0 )
1352 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001353 ncp = (unsigned char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001354
Anthony Baxter17471432006-03-20 05:58:21 +00001355 for ( i=0; i < len; i += size ) {
1356 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1357 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1358 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
Anthony Baxterfa869072006-03-20 05:21:58 +00001359
Anthony Baxter17471432006-03-20 05:58:21 +00001360 *ncp++ = st_linear2alaw(val);
1361 }
1362 return rv;
Anthony Baxterfa869072006-03-20 05:21:58 +00001363}
1364
1365static PyObject *
1366audioop_alaw2lin(PyObject *self, PyObject *args)
1367{
Anthony Baxter17471432006-03-20 05:58:21 +00001368 unsigned char *cp;
1369 unsigned char cval;
1370 signed char *ncp;
Gregory P. Smith9d534572008-06-11 07:41:16 +00001371 int len, new_len, size, val;
Anthony Baxter17471432006-03-20 05:58:21 +00001372 PyObject *rv;
1373 int i;
Anthony Baxterfa869072006-03-20 05:21:58 +00001374
Georg Brandl660222f2006-05-28 22:34:51 +00001375 if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
1376 &cp, &len, &size) )
Anthony Baxter17471432006-03-20 05:58:21 +00001377 return 0;
Anthony Baxterfa869072006-03-20 05:21:58 +00001378
Anthony Baxter17471432006-03-20 05:58:21 +00001379 if ( size != 1 && size != 2 && size != 4) {
1380 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1381 return 0;
1382 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001383
Gregory P. Smith9d534572008-06-11 07:41:16 +00001384 new_len = len*size;
1385 if (new_len < 0) {
1386 PyErr_SetString(PyExc_MemoryError,
1387 "not enough memory for output buffer");
1388 return 0;
1389 }
1390 rv = PyString_FromStringAndSize(NULL, new_len);
Anthony Baxter17471432006-03-20 05:58:21 +00001391 if ( rv == 0 )
1392 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001393 ncp = (signed char *)PyString_AsString(rv);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001394
Gregory P. Smith9d534572008-06-11 07:41:16 +00001395 for ( i=0; i < new_len; i += size ) {
Anthony Baxter17471432006-03-20 05:58:21 +00001396 cval = *cp++;
1397 val = st_alaw2linear16(cval);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001398
Anthony Baxter17471432006-03-20 05:58:21 +00001399 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1400 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1401 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1402 }
1403 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001404}
1405
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001406static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001407audioop_lin2adpcm(PyObject *self, PyObject *args)
Guido van Rossumb64e6351992-07-06 14:21:56 +00001408{
Anthony Baxter17471432006-03-20 05:58:21 +00001409 signed char *cp;
1410 signed char *ncp;
1411 int len, size, val = 0, step, valpred, delta,
1412 index, sign, vpdiff, diff;
1413 PyObject *rv, *state, *str;
1414 int i, outputbuffer = 0, bufferstep;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001415
Georg Brandl660222f2006-05-28 22:34:51 +00001416 if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
1417 &cp, &len, &size, &state) )
Anthony Baxter17471432006-03-20 05:58:21 +00001418 return 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +00001419
Guido van Rossumb64e6351992-07-06 14:21:56 +00001420
Anthony Baxter17471432006-03-20 05:58:21 +00001421 if ( size != 1 && size != 2 && size != 4) {
1422 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1423 return 0;
1424 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001425
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001426 str = PyString_FromStringAndSize(NULL, len/(size*2));
Anthony Baxter17471432006-03-20 05:58:21 +00001427 if ( str == 0 )
1428 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001429 ncp = (signed char *)PyString_AsString(str);
Jack Jansendd8a6ea1993-02-17 14:21:09 +00001430
Anthony Baxter17471432006-03-20 05:58:21 +00001431 /* Decode state, should have (value, step) */
1432 if ( state == Py_None ) {
1433 /* First time, it seems. Set defaults */
1434 valpred = 0;
Anthony Baxter17471432006-03-20 05:58:21 +00001435 index = 0;
Georg Brandl660222f2006-05-28 22:34:51 +00001436 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
Anthony Baxter17471432006-03-20 05:58:21 +00001437 return 0;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001438
Anthony Baxter17471432006-03-20 05:58:21 +00001439 step = stepsizeTable[index];
1440 bufferstep = 1;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001441
Anthony Baxter17471432006-03-20 05:58:21 +00001442 for ( i=0; i < len; i += size ) {
1443 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1444 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1445 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001446
Anthony Baxter17471432006-03-20 05:58:21 +00001447 /* Step 1 - compute difference with previous value */
1448 diff = val - valpred;
1449 sign = (diff < 0) ? 8 : 0;
1450 if ( sign ) diff = (-diff);
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001451
Anthony Baxter17471432006-03-20 05:58:21 +00001452 /* Step 2 - Divide and clamp */
1453 /* Note:
1454 ** This code *approximately* computes:
1455 ** delta = diff*4/step;
1456 ** vpdiff = (delta+0.5)*step/4;
1457 ** but in shift step bits are dropped. The net result of this
1458 ** is that even if you have fast mul/div hardware you cannot
1459 ** put it to good use since the fixup would be too expensive.
1460 */
1461 delta = 0;
1462 vpdiff = (step >> 3);
Brett Cannon9824e7f2010-05-03 23:42:40 +00001463
Anthony Baxter17471432006-03-20 05:58:21 +00001464 if ( diff >= step ) {
1465 delta = 4;
1466 diff -= step;
1467 vpdiff += step;
1468 }
1469 step >>= 1;
1470 if ( diff >= step ) {
1471 delta |= 2;
1472 diff -= step;
1473 vpdiff += step;
1474 }
1475 step >>= 1;
1476 if ( diff >= step ) {
1477 delta |= 1;
1478 vpdiff += step;
1479 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001480
Anthony Baxter17471432006-03-20 05:58:21 +00001481 /* Step 3 - Update previous value */
1482 if ( sign )
1483 valpred -= vpdiff;
1484 else
1485 valpred += vpdiff;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001486
Anthony Baxter17471432006-03-20 05:58:21 +00001487 /* Step 4 - Clamp previous value to 16 bits */
1488 if ( valpred > 32767 )
1489 valpred = 32767;
1490 else if ( valpred < -32768 )
1491 valpred = -32768;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001492
Anthony Baxter17471432006-03-20 05:58:21 +00001493 /* Step 5 - Assemble value, update index and step values */
1494 delta |= sign;
Brett Cannon9824e7f2010-05-03 23:42:40 +00001495
Anthony Baxter17471432006-03-20 05:58:21 +00001496 index += indexTable[delta];
1497 if ( index < 0 ) index = 0;
1498 if ( index > 88 ) index = 88;
1499 step = stepsizeTable[index];
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001500
Anthony Baxter17471432006-03-20 05:58:21 +00001501 /* Step 6 - Output value */
1502 if ( bufferstep ) {
1503 outputbuffer = (delta << 4) & 0xf0;
1504 } else {
1505 *ncp++ = (delta & 0x0f) | outputbuffer;
1506 }
1507 bufferstep = !bufferstep;
1508 }
1509 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1510 Py_DECREF(str);
1511 return rv;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001512}
1513
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001514static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001515audioop_adpcm2lin(PyObject *self, PyObject *args)
Guido van Rossumb64e6351992-07-06 14:21:56 +00001516{
Anthony Baxter17471432006-03-20 05:58:21 +00001517 signed char *cp;
1518 signed char *ncp;
Gregory P. Smith9d534572008-06-11 07:41:16 +00001519 int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
Anthony Baxter17471432006-03-20 05:58:21 +00001520 PyObject *rv, *str, *state;
1521 int i, inputbuffer = 0, bufferstep;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001522
Georg Brandl660222f2006-05-28 22:34:51 +00001523 if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
1524 &cp, &len, &size, &state) )
Anthony Baxter17471432006-03-20 05:58:21 +00001525 return 0;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001526
Anthony Baxter17471432006-03-20 05:58:21 +00001527 if ( size != 1 && size != 2 && size != 4) {
1528 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1529 return 0;
1530 }
Brett Cannon9824e7f2010-05-03 23:42:40 +00001531
Anthony Baxter17471432006-03-20 05:58:21 +00001532 /* Decode state, should have (value, step) */
1533 if ( state == Py_None ) {
1534 /* First time, it seems. Set defaults */
1535 valpred = 0;
Anthony Baxter17471432006-03-20 05:58:21 +00001536 index = 0;
Georg Brandl660222f2006-05-28 22:34:51 +00001537 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
Anthony Baxter17471432006-03-20 05:58:21 +00001538 return 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +00001539
Gregory P. Smith9d534572008-06-11 07:41:16 +00001540 new_len = len*size*2;
1541 if (new_len < 0) {
1542 PyErr_SetString(PyExc_MemoryError,
1543 "not enough memory for output buffer");
1544 return 0;
1545 }
1546 str = PyString_FromStringAndSize(NULL, new_len);
Anthony Baxter17471432006-03-20 05:58:21 +00001547 if ( str == 0 )
1548 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001549 ncp = (signed char *)PyString_AsString(str);
Guido van Rossumb64e6351992-07-06 14:21:56 +00001550
Anthony Baxter17471432006-03-20 05:58:21 +00001551 step = stepsizeTable[index];
1552 bufferstep = 0;
Brett Cannon9824e7f2010-05-03 23:42:40 +00001553
Gregory P. Smith9d534572008-06-11 07:41:16 +00001554 for ( i=0; i < new_len; i += size ) {
Anthony Baxter17471432006-03-20 05:58:21 +00001555 /* Step 1 - get the delta value and compute next index */
1556 if ( bufferstep ) {
1557 delta = inputbuffer & 0xf;
1558 } else {
1559 inputbuffer = *cp++;
1560 delta = (inputbuffer >> 4) & 0xf;
1561 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001562
Anthony Baxter17471432006-03-20 05:58:21 +00001563 bufferstep = !bufferstep;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001564
Anthony Baxter17471432006-03-20 05:58:21 +00001565 /* Step 2 - Find new index value (for later) */
1566 index += indexTable[delta];
1567 if ( index < 0 ) index = 0;
1568 if ( index > 88 ) index = 88;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001569
Anthony Baxter17471432006-03-20 05:58:21 +00001570 /* Step 3 - Separate sign and magnitude */
1571 sign = delta & 8;
1572 delta = delta & 7;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001573
Anthony Baxter17471432006-03-20 05:58:21 +00001574 /* Step 4 - Compute difference and new predicted value */
1575 /*
1576 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
1577 ** in adpcm_coder.
1578 */
1579 vpdiff = step >> 3;
1580 if ( delta & 4 ) vpdiff += step;
1581 if ( delta & 2 ) vpdiff += step>>1;
1582 if ( delta & 1 ) vpdiff += step>>2;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001583
Anthony Baxter17471432006-03-20 05:58:21 +00001584 if ( sign )
1585 valpred -= vpdiff;
1586 else
1587 valpred += vpdiff;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001588
Anthony Baxter17471432006-03-20 05:58:21 +00001589 /* Step 5 - clamp output value */
1590 if ( valpred > 32767 )
1591 valpred = 32767;
1592 else if ( valpred < -32768 )
1593 valpred = -32768;
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001594
Anthony Baxter17471432006-03-20 05:58:21 +00001595 /* Step 6 - Update step value */
1596 step = stepsizeTable[index];
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001597
Anthony Baxter17471432006-03-20 05:58:21 +00001598 /* Step 6 - Output value */
1599 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
1600 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
1601 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
1602 }
Guido van Rossumb64e6351992-07-06 14:21:56 +00001603
Anthony Baxter17471432006-03-20 05:58:21 +00001604 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1605 Py_DECREF(str);
1606 return rv;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001607}
1608
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001609static PyMethodDef audioop_methods[] = {
Georg Brandl660222f2006-05-28 22:34:51 +00001610 { "max", audioop_max, METH_VARARGS },
1611 { "minmax", audioop_minmax, METH_VARARGS },
1612 { "avg", audioop_avg, METH_VARARGS },
1613 { "maxpp", audioop_maxpp, METH_VARARGS },
1614 { "avgpp", audioop_avgpp, METH_VARARGS },
1615 { "rms", audioop_rms, METH_VARARGS },
1616 { "findfit", audioop_findfit, METH_VARARGS },
1617 { "findmax", audioop_findmax, METH_VARARGS },
1618 { "findfactor", audioop_findfactor, METH_VARARGS },
1619 { "cross", audioop_cross, METH_VARARGS },
1620 { "mul", audioop_mul, METH_VARARGS },
1621 { "add", audioop_add, METH_VARARGS },
1622 { "bias", audioop_bias, METH_VARARGS },
1623 { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
1624 { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
1625 { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
1626 { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
1627 { "lin2lin", audioop_lin2lin, METH_VARARGS },
1628 { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
1629 { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
1630 { "tomono", audioop_tomono, METH_VARARGS },
1631 { "tostereo", audioop_tostereo, METH_VARARGS },
1632 { "getsample", audioop_getsample, METH_VARARGS },
1633 { "reverse", audioop_reverse, METH_VARARGS },
Anthony Baxter17471432006-03-20 05:58:21 +00001634 { "ratecv", audioop_ratecv, METH_VARARGS },
1635 { 0, 0 }
Guido van Rossumb66efa01992-06-01 16:01:24 +00001636};
1637
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001638PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001639initaudioop(void)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001640{
Anthony Baxter17471432006-03-20 05:58:21 +00001641 PyObject *m, *d;
1642 m = Py_InitModule("audioop", audioop_methods);
1643 if (m == NULL)
1644 return;
1645 d = PyModule_GetDict(m);
Neal Norwitz49c65d02006-03-20 06:34:06 +00001646 if (d == NULL)
1647 return;
Anthony Baxter17471432006-03-20 05:58:21 +00001648 AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
1649 if (AudioopError != NULL)
1650 PyDict_SetItemString(d,"error",AudioopError);
Guido van Rossumb66efa01992-06-01 16:01:24 +00001651}