blob: 38e3e7ace2fb902cc8c373058c1d571e7486a496 [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
Mark Dickinson81fece22010-05-11 13:34:35 +00004#define PY_SSIZE_T_CLEAN
5
Roger E. Masseeaa6e111997-01-03 19:26:27 +00006#include "Python.h"
Guido van Rossumb66efa01992-06-01 16:01:24 +00007
Guido van Rossum69011961998-04-23 20:23:00 +00008#if SIZEOF_INT == 4
9typedef int Py_Int32;
10typedef unsigned int Py_UInt32;
11#else
12#if SIZEOF_LONG == 4
13typedef long Py_Int32;
14typedef unsigned long Py_UInt32;
15#else
16#error "No 4-byte integral type"
17#endif
18#endif
19
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000020typedef short PyInt16;
21
Guido van Rossum7b1e9741994-08-29 10:46:42 +000022#if defined(__CHAR_UNSIGNED__)
23#if defined(signed)
Guido van Rossumb6775db1994-08-01 11:34:53 +000024/* This module currently does not work on systems where only unsigned
25 characters are available. Take it out of Setup. Sorry. */
26#endif
Guido van Rossum7b1e9741994-08-29 10:46:42 +000027#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000028
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029/* Code shamelessly stolen from sox, 12.17.7, g711.c
Guido van Rossumb66efa01992-06-01 16:01:24 +000030** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
31
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032/* From g711.c:
33 *
34 * December 30, 1994:
35 * Functions linear2alaw, linear2ulaw have been updated to correctly
36 * convert unquantized 16 bit values.
37 * Tables for direct u- to A-law and A- to u-law conversions have been
38 * corrected.
39 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
40 * bli@cpk.auc.dk
41 *
42 */
Guido van Rossumb66efa01992-06-01 16:01:24 +000043#define BIAS 0x84 /* define the add-in bias for 16 bit samples */
44#define CLIP 32635
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
46#define QUANT_MASK (0xf) /* Quantization field mask. */
47#define SEG_SHIFT (4) /* Left shift for segment number. */
48#define SEG_MASK (0x70) /* Segment field mask. */
Guido van Rossumb66efa01992-06-01 16:01:24 +000049
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
51 0x1FF, 0x3FF, 0x7FF, 0xFFF};
52static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
53 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
54
55static PyInt16
56search(PyInt16 val, PyInt16 *table, int size)
Roger E. Masseeaa6e111997-01-03 19:26:27 +000057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 int i;
Guido van Rossumb66efa01992-06-01 16:01:24 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 for (i = 0; i < size; i++) {
61 if (val <= *table++)
62 return (i);
63 }
64 return (size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065}
66#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
67#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
Guido van Rossumb66efa01992-06-01 16:01:24 +000068
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069static PyInt16 _st_ulaw2linear16[256] = {
70 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
71 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
72 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
73 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
74 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
75 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
76 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
77 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
78 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
79 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
80 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
81 -1052, -988, -924, -876, -844, -812, -780,
82 -748, -716, -684, -652, -620, -588, -556,
83 -524, -492, -460, -428, -396, -372, -356,
84 -340, -324, -308, -292, -276, -260, -244,
85 -228, -212, -196, -180, -164, -148, -132,
86 -120, -112, -104, -96, -88, -80, -72,
87 -64, -56, -48, -40, -32, -24, -16,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 -8, 0, 32124, 31100, 30076, 29052, 28028,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000089 27004, 25980, 24956, 23932, 22908, 21884, 20860,
90 19836, 18812, 17788, 16764, 15996, 15484, 14972,
91 14460, 13948, 13436, 12924, 12412, 11900, 11388,
92 10876, 10364, 9852, 9340, 8828, 8316, 7932,
93 7676, 7420, 7164, 6908, 6652, 6396, 6140,
94 5884, 5628, 5372, 5116, 4860, 4604, 4348,
95 4092, 3900, 3772, 3644, 3516, 3388, 3260,
96 3132, 3004, 2876, 2748, 2620, 2492, 2364,
97 2236, 2108, 1980, 1884, 1820, 1756, 1692,
98 1628, 1564, 1500, 1436, 1372, 1308, 1244,
99 1180, 1116, 1052, 988, 924, 876, 844,
100 812, 780, 748, 716, 684, 652, 620,
101 588, 556, 524, 492, 460, 428, 396,
102 372, 356, 340, 324, 308, 292, 276,
103 260, 244, 228, 212, 196, 180, 164,
104 148, 132, 120, 112, 104, 96, 88,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 80, 72, 64, 56, 48, 40, 32,
106 24, 16, 8, 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107};
Guido van Rossumb66efa01992-06-01 16:01:24 +0000108
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109/*
110 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
111 * stored in a unsigned char. This function should only be called with
112 * the data shifted such that it only contains information in the lower
113 * 14-bits.
114 *
115 * In order to simplify the encoding process, the original linear magnitude
116 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
117 * (33 - 8191). The result can be seen in the following encoding table:
118 *
119 * Biased Linear Input Code Compressed Code
120 * ------------------------ ---------------
121 * 00000001wxyza 000wxyz
122 * 0000001wxyzab 001wxyz
123 * 000001wxyzabc 010wxyz
124 * 00001wxyzabcd 011wxyz
125 * 0001wxyzabcde 100wxyz
126 * 001wxyzabcdef 101wxyz
127 * 01wxyzabcdefg 110wxyz
128 * 1wxyzabcdefgh 111wxyz
129 *
130 * Each biased linear code has a leading 1 which identifies the segment
131 * number. The value of the segment number is equal to 7 minus the number
132 * of leading 0's. The quantization interval is directly available as the
133 * four bits wxyz. * The trailing bits (a - h) are ignored.
134 *
135 * Ordinarily the complement of the resulting code word is used for
136 * transmission, and so the code word is complemented before it is returned.
137 *
138 * For further information see John C. Bellamy's Digital Telephony, 1982,
139 * John Wiley & Sons, pps 98-111 and 472-476.
140 */
141static unsigned char
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 PyInt16 mask;
145 PyInt16 seg;
146 unsigned char uval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 /* The original sox code does this in the calling function, not here */
149 pcm_val = pcm_val >> 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 /* u-law inverts all bits */
152 /* Get the sign and the magnitude of the value. */
153 if (pcm_val < 0) {
154 pcm_val = -pcm_val;
155 mask = 0x7F;
156 } else {
157 mask = 0xFF;
158 }
159 if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
160 pcm_val += (BIAS >> 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 /* Convert the scaled magnitude to segment number. */
163 seg = search(pcm_val, seg_uend, 8);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 /*
166 * Combine the sign, segment, quantization bits;
167 * and complement the code word.
168 */
169 if (seg >= 8) /* out of range, return maximum value. */
170 return (unsigned char) (0x7F ^ mask);
171 else {
172 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
173 return (uval ^ mask);
174 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
176}
177
178static PyInt16 _st_alaw2linear16[256] = {
179 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
180 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
181 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
182 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
183 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
184 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
185 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
186 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
187 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
188 -13568, -344, -328, -376, -360, -280, -264,
189 -312, -296, -472, -456, -504, -488, -408,
190 -392, -440, -424, -88, -72, -120, -104,
191 -24, -8, -56, -40, -216, -200, -248,
192 -232, -152, -136, -184, -168, -1376, -1312,
193 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
194 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
195 -688, -656, -752, -720, -560, -528, -624,
196 -592, -944, -912, -1008, -976, -816, -784,
197 -880, -848, 5504, 5248, 6016, 5760, 4480,
198 4224, 4992, 4736, 7552, 7296, 8064, 7808,
199 6528, 6272, 7040, 6784, 2752, 2624, 3008,
200 2880, 2240, 2112, 2496, 2368, 3776, 3648,
201 4032, 3904, 3264, 3136, 3520, 3392, 22016,
202 20992, 24064, 23040, 17920, 16896, 19968, 18944,
203 30208, 29184, 32256, 31232, 26112, 25088, 28160,
204 27136, 11008, 10496, 12032, 11520, 8960, 8448,
205 9984, 9472, 15104, 14592, 16128, 15616, 13056,
206 12544, 14080, 13568, 344, 328, 376, 360,
207 280, 264, 312, 296, 472, 456, 504,
208 488, 408, 392, 440, 424, 88, 72,
209 120, 104, 24, 8, 56, 40, 216,
210 200, 248, 232, 152, 136, 184, 168,
211 1376, 1312, 1504, 1440, 1120, 1056, 1248,
212 1184, 1888, 1824, 2016, 1952, 1632, 1568,
213 1760, 1696, 688, 656, 752, 720, 560,
214 528, 624, 592, 944, 912, 1008, 976,
215 816, 784, 880, 848
216};
217
218/*
219 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
220 * stored in a unsigned char. This function should only be called with
221 * the data shifted such that it only contains information in the lower
222 * 13-bits.
223 *
224 * Linear Input Code Compressed Code
225 * ------------------------ ---------------
226 * 0000000wxyza 000wxyz
227 * 0000001wxyza 001wxyz
228 * 000001wxyzab 010wxyz
229 * 00001wxyzabc 011wxyz
230 * 0001wxyzabcd 100wxyz
231 * 001wxyzabcde 101wxyz
232 * 01wxyzabcdef 110wxyz
233 * 1wxyzabcdefg 111wxyz
234 *
235 * For further information see John C. Bellamy's Digital Telephony, 1982,
236 * John Wiley & Sons, pps 98-111 and 472-476.
237 */
238static unsigned char
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 PyInt16 mask;
242 short seg;
243 unsigned char aval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 /* The original sox code does this in the calling function, not here */
246 pcm_val = pcm_val >> 3;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 /* A-law using even bit inversion */
249 if (pcm_val >= 0) {
250 mask = 0xD5; /* sign (7th) bit = 1 */
251 } else {
252 mask = 0x55; /* sign bit = 0 */
253 pcm_val = -pcm_val - 1;
254 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* Convert the scaled magnitude to segment number. */
257 seg = search(pcm_val, seg_aend, 8);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 /* Combine the sign, segment, and quantization bits. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (seg >= 8) /* out of range, return maximum value. */
262 return (unsigned char) (0x7F ^ mask);
263 else {
264 aval = (unsigned char) seg << SEG_SHIFT;
265 if (seg < 2)
266 aval |= (pcm_val >> 1) & QUANT_MASK;
267 else
268 aval |= (pcm_val >> seg) & QUANT_MASK;
269 return (aval ^ mask);
270 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000271}
Guido van Rossumb66efa01992-06-01 16:01:24 +0000272/* End of code taken from sox */
273
Guido van Rossumb64e6351992-07-06 14:21:56 +0000274/* Intel ADPCM step variation table */
275static int indexTable[16] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 -1, -1, -1, -1, 2, 4, 6, 8,
277 -1, -1, -1, -1, 2, 4, 6, 8,
Guido van Rossumb64e6351992-07-06 14:21:56 +0000278};
279
280static int stepsizeTable[89] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
282 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
283 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
284 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
285 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
286 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
287 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
288 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
289 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
Guido van Rossumb64e6351992-07-06 14:21:56 +0000290};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291
Guido van Rossumb66efa01992-06-01 16:01:24 +0000292#define CHARP(cp, i) ((signed char *)(cp+i))
293#define SHORTP(cp, i) ((short *)(cp+i))
Guido van Rossum69011961998-04-23 20:23:00 +0000294#define LONGP(cp, i) ((Py_Int32 *)(cp+i))
Guido van Rossumb66efa01992-06-01 16:01:24 +0000295
296
297
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000298static PyObject *AudioopError;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000299
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000300static int
301audioop_check_size(int size)
302{
303 if (size != 1 && size != 2 && size != 4) {
304 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
305 return 0;
306 }
307 else
308 return 1;
309}
310
311static int
312audioop_check_parameters(int len, int size)
313{
314 if (!audioop_check_size(size))
315 return 0;
316 if (len % size != 0) {
317 PyErr_SetString(AudioopError, "not a whole number of frames");
318 return 0;
319 }
320 return 1;
321}
322
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000323static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000324audioop_getsample(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000327 Py_ssize_t len, i;
328 int size, val = 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000329
Mark Dickinson81fece22010-05-11 13:34:35 +0000330 if ( !PyArg_ParseTuple(args, "s#in:getsample", &cp, &len, &size, &i) )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000332 if (!audioop_check_parameters(len, size))
333 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if ( i < 0 || i >= len/size ) {
335 PyErr_SetString(AudioopError, "Index out of range");
336 return 0;
337 }
338 if ( size == 1 ) val = (int)*CHARP(cp, i);
339 else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
340 else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
341 return PyLong_FromLong(val);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000342}
343
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000344static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000345audioop_max(PyObject *self, PyObject *args)
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000348 Py_ssize_t len, i;
349 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 int max = 0;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
353 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000354 if (!audioop_check_parameters(len, size))
355 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 for ( i=0; i<len; i+= size) {
357 if ( size == 1 ) val = (int)*CHARP(cp, i);
358 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
359 else if ( size == 4 ) val = (int)*LONGP(cp, i);
360 if ( val < 0 ) val = (-val);
361 if ( val > max ) max = val;
362 }
363 return PyLong_FromLong(max);
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000364}
365
366static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000367audioop_minmax(PyObject *self, PyObject *args)
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000370 Py_ssize_t len, i;
371 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 int min = 0x7fffffff, max = -0x7fffffff;
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
375 return NULL;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000376 if (!audioop_check_parameters(len, size))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 for (i = 0; i < len; i += size) {
379 if (size == 1) val = (int) *CHARP(cp, i);
380 else if (size == 2) val = (int) *SHORTP(cp, i);
381 else if (size == 4) val = (int) *LONGP(cp, i);
382 if (val > max) max = val;
383 if (val < min) min = val;
384 }
385 return Py_BuildValue("(ii)", min, max);
Sjoerd Mullendera1426131994-09-06 16:19:33 +0000386}
387
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000388static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000389audioop_avg(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000392 Py_ssize_t len, i;
393 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 double avg = 0.0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
397 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000398 if (!audioop_check_parameters(len, size))
399 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 for ( i=0; i<len; i+= size) {
401 if ( size == 1 ) val = (int)*CHARP(cp, i);
402 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
403 else if ( size == 4 ) val = (int)*LONGP(cp, i);
404 avg += val;
405 }
406 if ( len == 0 )
407 val = 0;
408 else
409 val = (int)(avg / (double)(len/size));
410 return PyLong_FromLong(val);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000411}
412
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000413static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000414audioop_rms(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000417 Py_ssize_t len, i;
418 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 double sum_squares = 0.0;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
422 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000423 if (!audioop_check_parameters(len, size))
424 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 for ( i=0; i<len; i+= size) {
426 if ( size == 1 ) val = (int)*CHARP(cp, i);
427 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
428 else if ( size == 4 ) val = (int)*LONGP(cp, i);
429 sum_squares += (double)val*(double)val;
430 }
431 if ( len == 0 )
432 val = 0;
433 else
434 val = (int)sqrt(sum_squares / (double)(len/size));
435 return PyLong_FromLong(val);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000436}
437
Mark Dickinson81fece22010-05-11 13:34:35 +0000438static double _sum2(short *a, short *b, Py_ssize_t len)
Jack Jansena90805f1993-02-17 14:29:28 +0000439{
Mark Dickinson81fece22010-05-11 13:34:35 +0000440 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 double sum = 0.0;
Jack Jansena90805f1993-02-17 14:29:28 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 for( i=0; i<len; i++) {
444 sum = sum + (double)a[i]*(double)b[i];
445 }
446 return sum;
Jack Jansena90805f1993-02-17 14:29:28 +0000447}
448
449/*
450** Findfit tries to locate a sample within another sample. Its main use
451** is in echo-cancellation (to find the feedback of the output signal in
452** the input signal).
453** The method used is as follows:
454**
455** let R be the reference signal (length n) and A the input signal (length N)
456** with N > n, and let all sums be over i from 0 to n-1.
457**
458** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
459** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
460** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
461**
462** Next, we compute the relative distance between the original signal and
463** the modified signal and minimize that over j:
464** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) =>
465** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
466**
467** In the code variables correspond as follows:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000468** cp1 A
469** cp2 R
470** len1 N
471** len2 n
472** aj_m1 A[j-1]
473** aj_lm1 A[j+n-1]
474** sum_ri_2 sum(R[i]^2)
475** sum_aij_2 sum(A[i+j]^2)
476** sum_aij_ri sum(A[i+j]R[i])
Jack Jansena90805f1993-02-17 14:29:28 +0000477**
478** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
479** is completely recalculated each step.
480*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000481static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000482audioop_findfit(PyObject *self, PyObject *args)
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 short *cp1, *cp2;
Mark Dickinson81fece22010-05-11 13:34:35 +0000485 Py_ssize_t len1, len2;
486 Py_ssize_t j, best_j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 double aj_m1, aj_lm1;
488 double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 /* Passing a short** for an 's' argument is correct only
491 if the string contents is aligned for interpretation
492 as short[]. Due to the definition of PyBytesObject,
493 this is currently (Python 2.6) the case. */
494 if ( !PyArg_ParseTuple(args, "s#s#:findfit",
495 (char**)&cp1, &len1, (char**)&cp2, &len2) )
496 return 0;
497 if ( len1 & 1 || len2 & 1 ) {
498 PyErr_SetString(AudioopError, "Strings should be even-sized");
499 return 0;
500 }
501 len1 >>= 1;
502 len2 >>= 1;
Jack Jansena90805f1993-02-17 14:29:28 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if ( len1 < len2 ) {
505 PyErr_SetString(AudioopError, "First sample should be longer");
506 return 0;
507 }
508 sum_ri_2 = _sum2(cp2, cp2, len2);
509 sum_aij_2 = _sum2(cp1, cp1, len2);
510 sum_aij_ri = _sum2(cp1, cp2, len2);
Jack Jansena90805f1993-02-17 14:29:28 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
Jack Jansena90805f1993-02-17 14:29:28 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 best_result = result;
515 best_j = 0;
516 j = 0;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 for ( j=1; j<=len1-len2; j++) {
519 aj_m1 = (double)cp1[j-1];
520 aj_lm1 = (double)cp1[j+len2-1];
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
523 sum_aij_ri = _sum2(cp1+j, cp2, len2);
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
526 / sum_aij_2;
527
528 if ( result < best_result ) {
529 best_result = result;
530 best_j = j;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000531 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 }
534
535 factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
536
Mark Dickinson81fece22010-05-11 13:34:35 +0000537 return Py_BuildValue("(nf)", best_j, factor);
Jack Jansena90805f1993-02-17 14:29:28 +0000538}
539
540/*
541** findfactor finds a factor f so that the energy in A-fB is minimal.
542** See the comment for findfit for details.
543*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000544static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000545audioop_findfactor(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +0000546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 short *cp1, *cp2;
Mark Dickinson81fece22010-05-11 13:34:35 +0000548 Py_ssize_t len1, len2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 double sum_ri_2, sum_aij_ri, result;
Jack Jansena90805f1993-02-17 14:29:28 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
552 (char**)&cp1, &len1, (char**)&cp2, &len2) )
553 return 0;
554 if ( len1 & 1 || len2 & 1 ) {
555 PyErr_SetString(AudioopError, "Strings should be even-sized");
556 return 0;
557 }
558 if ( len1 != len2 ) {
559 PyErr_SetString(AudioopError, "Samples should be same size");
560 return 0;
561 }
562 len2 >>= 1;
563 sum_ri_2 = _sum2(cp2, cp2, len2);
564 sum_aij_ri = _sum2(cp1, cp2, len2);
Jack Jansena90805f1993-02-17 14:29:28 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 result = sum_aij_ri / sum_ri_2;
Jack Jansena90805f1993-02-17 14:29:28 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return PyFloat_FromDouble(result);
Jack Jansena90805f1993-02-17 14:29:28 +0000569}
570
571/*
572** findmax returns the index of the n-sized segment of the input sample
573** that contains the most energy.
574*/
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000575static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000576audioop_findmax(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 short *cp1;
Mark Dickinson81fece22010-05-11 13:34:35 +0000579 Py_ssize_t len1, len2;
580 Py_ssize_t j, best_j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 double aj_m1, aj_lm1;
582 double result, best_result;
Jack Jansena90805f1993-02-17 14:29:28 +0000583
Mark Dickinson81fece22010-05-11 13:34:35 +0000584 if ( !PyArg_ParseTuple(args, "s#n:findmax",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 (char**)&cp1, &len1, &len2) )
586 return 0;
587 if ( len1 & 1 ) {
588 PyErr_SetString(AudioopError, "Strings should be even-sized");
589 return 0;
590 }
591 len1 >>= 1;
592
593 if ( len2 < 0 || len1 < len2 ) {
594 PyErr_SetString(AudioopError, "Input sample should be longer");
595 return 0;
596 }
597
598 result = _sum2(cp1, cp1, len2);
599
600 best_result = result;
601 best_j = 0;
602 j = 0;
603
604 for ( j=1; j<=len1-len2; j++) {
605 aj_m1 = (double)cp1[j-1];
606 aj_lm1 = (double)cp1[j+len2-1];
607
608 result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
609
610 if ( result > best_result ) {
611 best_result = result;
612 best_j = j;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000613 }
Jack Jansena90805f1993-02-17 14:29:28 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000616
Mark Dickinson81fece22010-05-11 13:34:35 +0000617 return PyLong_FromSsize_t(best_j);
Jack Jansendd8a6ea1993-02-17 14:21:09 +0000618}
619
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000620static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000621audioop_avgpp(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000624 Py_ssize_t len, i;
625 int size, val = 0, prevval = 0, prevextremevalid = 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 prevextreme = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 double avg = 0.0;
628 int diff, prevdiff, extremediff, nextreme = 0;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
631 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000632 if (!audioop_check_parameters(len, size))
633 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 /* Compute first delta value ahead. Also automatically makes us
635 ** skip the first extreme value
636 */
637 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
638 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
639 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
640 if ( size == 1 ) val = (int)*CHARP(cp, size);
641 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
642 else if ( size == 4 ) val = (int)*LONGP(cp, size);
643 prevdiff = val - prevval;
644
645 for ( i=size; i<len; i+= size) {
646 if ( size == 1 ) val = (int)*CHARP(cp, i);
647 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
648 else if ( size == 4 ) val = (int)*LONGP(cp, i);
649 diff = val - prevval;
650 if ( diff*prevdiff < 0 ) {
651 /* Derivative changed sign. Compute difference to last
652 ** extreme value and remember.
653 */
654 if ( prevextremevalid ) {
655 extremediff = prevval - prevextreme;
656 if ( extremediff < 0 )
657 extremediff = -extremediff;
658 avg += extremediff;
659 nextreme++;
660 }
661 prevextremevalid = 1;
662 prevextreme = prevval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000663 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 prevval = val;
665 if ( diff != 0 )
666 prevdiff = diff;
667 }
668 if ( nextreme == 0 )
669 val = 0;
670 else
671 val = (int)(avg / (double)nextreme);
672 return PyLong_FromLong(val);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000673}
674
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000675static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000676audioop_maxpp(PyObject *self, PyObject *args)
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000679 Py_ssize_t len, i;
680 int size, val = 0, prevval = 0, prevextremevalid = 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 prevextreme = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 int max = 0;
683 int diff, prevdiff, extremediff;
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
686 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000687 if (!audioop_check_parameters(len, size))
688 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* Compute first delta value ahead. Also automatically makes us
690 ** skip the first extreme value
691 */
692 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
693 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
694 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
695 if ( size == 1 ) val = (int)*CHARP(cp, size);
696 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
697 else if ( size == 4 ) val = (int)*LONGP(cp, size);
698 prevdiff = val - prevval;
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 for ( i=size; i<len; i+= size) {
701 if ( size == 1 ) val = (int)*CHARP(cp, i);
702 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
703 else if ( size == 4 ) val = (int)*LONGP(cp, i);
704 diff = val - prevval;
705 if ( diff*prevdiff < 0 ) {
706 /* Derivative changed sign. Compute difference to
707 ** last extreme value and remember.
708 */
709 if ( prevextremevalid ) {
710 extremediff = prevval - prevextreme;
711 if ( extremediff < 0 )
712 extremediff = -extremediff;
713 if ( extremediff > max )
714 max = extremediff;
715 }
716 prevextremevalid = 1;
717 prevextreme = prevval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 prevval = val;
720 if ( diff != 0 )
721 prevdiff = diff;
722 }
723 return PyLong_FromLong(max);
Jack Jansene1b4d7c1992-08-24 14:36:31 +0000724}
725
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000726static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000727audioop_cross(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 signed char *cp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000730 Py_ssize_t len, i;
731 int size, val = 0;
732 int prevval;
733 Py_ssize_t ncross;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
736 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000737 if (!audioop_check_parameters(len, size))
738 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 ncross = -1;
740 prevval = 17; /* Anything <> 0,1 */
741 for ( i=0; i<len; i+= size) {
742 if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
743 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
744 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
745 val = val & 1;
746 if ( val != prevval ) ncross++;
747 prevval = val;
748 }
Mark Dickinson81fece22010-05-11 13:34:35 +0000749 return PyLong_FromSsize_t(ncross);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000750}
751
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000752static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000753audioop_mul(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 signed char *cp, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000756 Py_ssize_t len, i;
757 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 double factor, fval, maxval;
759 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
762 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000763 if (!audioop_check_parameters(len, size))
764 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765
766 if ( size == 1 ) maxval = (double) 0x7f;
767 else if ( size == 2 ) maxval = (double) 0x7fff;
768 else if ( size == 4 ) maxval = (double) 0x7fffffff;
769 else {
770 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
771 return 0;
772 }
773
774 rv = PyBytes_FromStringAndSize(NULL, len);
775 if ( rv == 0 )
776 return 0;
777 ncp = (signed char *)PyBytes_AsString(rv);
778
779
780 for ( i=0; i < len; i += size ) {
781 if ( size == 1 ) val = (int)*CHARP(cp, i);
782 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
783 else if ( size == 4 ) val = (int)*LONGP(cp, i);
784 fval = (double)val*factor;
785 if ( fval > maxval ) fval = maxval;
786 else if ( fval < -maxval ) fval = -maxval;
787 val = (int)fval;
788 if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
789 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
790 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
791 }
792 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000793}
794
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000795static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000796audioop_tomono(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Py_buffer pcp;
799 signed char *cp, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000800 Py_ssize_t len, i;
801 int size, val1 = 0, val2 = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 double fac1, fac2, fval, maxval;
803 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 if ( !PyArg_ParseTuple(args, "s*idd:tomono",
806 &pcp, &size, &fac1, &fac2 ) )
807 return 0;
808 cp = pcp.buf;
809 len = pcp.len;
Mark Dickinsoncc588c12010-07-04 10:15:11 +0000810 if (!audioop_check_parameters(len, size)) {
811 PyBuffer_Release(&pcp);
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000812 return NULL;
Mark Dickinsoncc588c12010-07-04 10:15:11 +0000813 }
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000814 if (((len / size) & 1) != 0) {
815 PyErr_SetString(AudioopError, "not a whole number of frames");
Mark Dickinsoncc588c12010-07-04 10:15:11 +0000816 PyBuffer_Release(&pcp);
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000817 return NULL;
818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819
820 if ( size == 1 ) maxval = (double) 0x7f;
821 else if ( size == 2 ) maxval = (double) 0x7fff;
822 else if ( size == 4 ) maxval = (double) 0x7fffffff;
823 else {
824 PyBuffer_Release(&pcp);
825 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
826 return 0;
827 }
828
829 rv = PyBytes_FromStringAndSize(NULL, len/2);
Mark Dickinsoncc588c12010-07-04 10:15:11 +0000830 if ( rv == 0 ) {
831 PyBuffer_Release(&pcp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 return 0;
Mark Dickinsoncc588c12010-07-04 10:15:11 +0000833 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 ncp = (signed char *)PyBytes_AsString(rv);
835
836
837 for ( i=0; i < len; i += size*2 ) {
838 if ( size == 1 ) val1 = (int)*CHARP(cp, i);
839 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
840 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
841 if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
842 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
843 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
844 fval = (double)val1*fac1 + (double)val2*fac2;
845 if ( fval > maxval ) fval = maxval;
846 else if ( fval < -maxval ) fval = -maxval;
847 val1 = (int)fval;
848 if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
849 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
850 else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
851 }
852 PyBuffer_Release(&pcp);
853 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000854}
855
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000856static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000857audioop_tostereo(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 signed char *cp, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000860 Py_ssize_t len, i;
861 int size, val1, val2, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 double fac1, fac2, fval, maxval;
863 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
866 &cp, &len, &size, &fac1, &fac2 ) )
867 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000868 if (!audioop_check_parameters(len, size))
869 return NULL;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if ( size == 1 ) maxval = (double) 0x7f;
872 else if ( size == 2 ) maxval = (double) 0x7fff;
873 else if ( size == 4 ) maxval = (double) 0x7fffffff;
874 else {
875 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
876 return 0;
877 }
Guido van Rossumb66efa01992-06-01 16:01:24 +0000878
Mark Dickinson81fece22010-05-11 13:34:35 +0000879 if (len > PY_SSIZE_T_MAX/2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyErr_SetString(PyExc_MemoryError,
881 "not enough memory for output buffer");
882 return 0;
883 }
Guido van Rossumb66efa01992-06-01 16:01:24 +0000884
Mark Dickinson85eacea2010-05-10 16:27:45 +0000885 rv = PyBytes_FromStringAndSize(NULL, len*2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if ( rv == 0 )
887 return 0;
888 ncp = (signed char *)PyBytes_AsString(rv);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000889
Guido van Rossumb66efa01992-06-01 16:01:24 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 for ( i=0; i < len; i += size ) {
892 if ( size == 1 ) val = (int)*CHARP(cp, i);
893 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
894 else if ( size == 4 ) val = (int)*LONGP(cp, i);
895
896 fval = (double)val*fac1;
897 if ( fval > maxval ) fval = maxval;
898 else if ( fval < -maxval ) fval = -maxval;
899 val1 = (int)fval;
900
901 fval = (double)val*fac2;
902 if ( fval > maxval ) fval = maxval;
903 else if ( fval < -maxval ) fval = -maxval;
904 val2 = (int)fval;
905
906 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
907 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
908 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
909
910 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
911 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
912 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
913 }
914 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000915}
916
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000917static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000918audioop_add(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 signed char *cp1, *cp2, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000921 Py_ssize_t len1, len2, i;
922 int size, val1 = 0, val2 = 0, maxval, newval;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if ( !PyArg_ParseTuple(args, "s#s#i:add",
926 &cp1, &len1, &cp2, &len2, &size ) )
927 return 0;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000928 if (!audioop_check_parameters(len1, size))
929 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if ( len1 != len2 ) {
931 PyErr_SetString(AudioopError, "Lengths should be the same");
932 return 0;
933 }
Guido van Rossum1851a671997-02-14 16:14:03 +0000934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if ( size == 1 ) maxval = 0x7f;
936 else if ( size == 2 ) maxval = 0x7fff;
937 else if ( size == 4 ) maxval = 0x7fffffff;
938 else {
939 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
940 return 0;
941 }
Guido van Rossum1851a671997-02-14 16:14:03 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 rv = PyBytes_FromStringAndSize(NULL, len1);
944 if ( rv == 0 )
945 return 0;
946 ncp = (signed char *)PyBytes_AsString(rv);
Guido van Rossumb66efa01992-06-01 16:01:24 +0000947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 for ( i=0; i < len1; i += size ) {
949 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
950 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
951 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
Guido van Rossum1851a671997-02-14 16:14:03 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
954 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
955 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
956
957 newval = val1 + val2;
958 /* truncate in case of overflow */
959 if (newval > maxval) newval = maxval;
960 else if (newval < -maxval) newval = -maxval;
961 else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
962 newval = val1 > 0 ? maxval : - maxval;
963
964 if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
965 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
966 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
967 }
968 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000969}
970
Roger E. Masseeaa6e111997-01-03 19:26:27 +0000971static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +0000972audioop_bias(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 signed char *cp, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +0000975 Py_ssize_t len, i;
976 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyObject *rv;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 int bias;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if ( !PyArg_ParseTuple(args, "s#ii:bias",
981 &cp, &len, &size , &bias) )
982 return 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +0000983
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000984 if (!audioop_check_parameters(len, size))
985 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986
987 rv = PyBytes_FromStringAndSize(NULL, len);
988 if ( rv == 0 )
989 return 0;
990 ncp = (signed char *)PyBytes_AsString(rv);
991
992
993 for ( i=0; i < len; i += size ) {
994 if ( size == 1 ) val = (int)*CHARP(cp, i);
995 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
996 else if ( size == 4 ) val = (int)*LONGP(cp, i);
997
998 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
999 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
1000 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
1001 }
1002 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001003}
1004
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001005static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001006audioop_reverse(PyObject *self, PyObject *args)
Jack Jansen337b20e1993-02-23 13:39:57 +00001007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 signed char *cp;
1009 unsigned char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001010 Py_ssize_t len, i, j;
1011 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 PyObject *rv;
Jack Jansen337b20e1993-02-23 13:39:57 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if ( !PyArg_ParseTuple(args, "s#i:reverse",
1015 &cp, &len, &size) )
1016 return 0;
Jack Jansen337b20e1993-02-23 13:39:57 +00001017
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001018 if (!audioop_check_parameters(len, size))
1019 return NULL;
Jack Jansen337b20e1993-02-23 13:39:57 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 rv = PyBytes_FromStringAndSize(NULL, len);
1022 if ( rv == 0 )
1023 return 0;
1024 ncp = (unsigned char *)PyBytes_AsString(rv);
1025
1026 for ( i=0; i < len; i += size ) {
1027 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1028 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1029 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1030
1031 j = len - i - size;
1032
1033 if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1034 else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
1035 else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1036 }
1037 return rv;
Jack Jansen337b20e1993-02-23 13:39:57 +00001038}
1039
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001040static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001041audioop_lin2lin(PyObject *self, PyObject *args)
Jack Jansena90805f1993-02-17 14:29:28 +00001042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 signed char *cp;
1044 unsigned char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001045 Py_ssize_t len, i, j;
1046 int size, size2, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyObject *rv;
Jack Jansena90805f1993-02-17 14:29:28 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
1050 &cp, &len, &size, &size2) )
1051 return 0;
Jack Jansena90805f1993-02-17 14:29:28 +00001052
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001053 if (!audioop_check_parameters(len, size))
1054 return NULL;
1055 if (!audioop_check_size(size2))
1056 return NULL;
Jack Jansena90805f1993-02-17 14:29:28 +00001057
Mark Dickinson81fece22010-05-11 13:34:35 +00001058 if (len/size > PY_SSIZE_T_MAX/size2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyErr_SetString(PyExc_MemoryError,
1060 "not enough memory for output buffer");
1061 return 0;
1062 }
Mark Dickinson85eacea2010-05-10 16:27:45 +00001063 rv = PyBytes_FromStringAndSize(NULL, (len/size)*size2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 if ( rv == 0 )
1065 return 0;
1066 ncp = (unsigned char *)PyBytes_AsString(rv);
1067
1068 for ( i=0, j=0; i < len; i += size, j += size2 ) {
1069 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1070 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1071 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1072
1073 if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
1074 else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
1075 else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
1076 }
1077 return rv;
Jack Jansena90805f1993-02-17 14:29:28 +00001078}
1079
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001080static int
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001081gcd(int a, int b)
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 while (b > 0) {
1084 int tmp = a % b;
1085 a = b;
1086 b = tmp;
1087 }
1088 return a;
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001089}
1090
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001091static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001092audioop_ratecv(PyObject *self, PyObject *args)
Roger E. Massec905fff1997-01-17 18:12:04 +00001093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 char *cp, *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001095 Py_ssize_t len;
1096 int size, nchannels, inrate, outrate, weightA, weightB;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 int chan, d, *prev_i, *cur_i, cur_o;
1098 PyObject *state, *samps, *str, *rv = NULL;
1099 int bytes_per_frame;
Guido van Rossum1851a671997-02-14 16:14:03 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 weightA = 1;
1102 weightB = 0;
1103 if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
1104 &nchannels, &inrate, &outrate, &state,
1105 &weightA, &weightB))
1106 return NULL;
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001107 if (!audioop_check_size(size))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (nchannels < 1) {
1110 PyErr_SetString(AudioopError, "# of channels should be >= 1");
1111 return NULL;
1112 }
1113 bytes_per_frame = size * nchannels;
1114 if (bytes_per_frame / nchannels != size) {
1115 /* This overflow test is rigorously correct because
1116 both multiplicands are >= 1. Use the argument names
1117 from the docs for the error msg. */
1118 PyErr_SetString(PyExc_OverflowError,
1119 "width * nchannels too big for a C int");
1120 return NULL;
1121 }
1122 if (weightA < 1 || weightB < 0) {
1123 PyErr_SetString(AudioopError,
1124 "weightA should be >= 1, weightB should be >= 0");
1125 return NULL;
1126 }
1127 if (len % bytes_per_frame != 0) {
1128 PyErr_SetString(AudioopError, "not a whole number of frames");
1129 return NULL;
1130 }
1131 if (inrate <= 0 || outrate <= 0) {
1132 PyErr_SetString(AudioopError, "sampling rate not > 0");
1133 return NULL;
1134 }
1135 /* divide inrate and outrate by their greatest common divisor */
1136 d = gcd(inrate, outrate);
1137 inrate /= d;
1138 outrate /= d;
Guido van Rossumb24c9ea1997-05-20 15:59:35 +00001139
Mark Dickinson85eacea2010-05-10 16:27:45 +00001140 if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 PyErr_SetString(PyExc_MemoryError,
1142 "not enough memory for output buffer");
1143 return 0;
1144 }
Mark Dickinson85eacea2010-05-10 16:27:45 +00001145 prev_i = (int *) malloc(nchannels * sizeof(int));
1146 cur_i = (int *) malloc(nchannels * sizeof(int));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (prev_i == NULL || cur_i == NULL) {
1148 (void) PyErr_NoMemory();
1149 goto exit;
1150 }
1151
1152 len /= bytes_per_frame; /* # of frames */
1153
1154 if (state == Py_None) {
1155 d = -outrate;
1156 for (chan = 0; chan < nchannels; chan++)
1157 prev_i[chan] = cur_i[chan] = 0;
1158 }
1159 else {
1160 if (!PyArg_ParseTuple(state,
1161 "iO!;audioop.ratecv: illegal state argument",
1162 &d, &PyTuple_Type, &samps))
1163 goto exit;
1164 if (PyTuple_Size(samps) != nchannels) {
1165 PyErr_SetString(AudioopError,
1166 "illegal state argument");
1167 goto exit;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00001168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 for (chan = 0; chan < nchannels; chan++) {
1170 if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
1171 "ii:ratecv", &prev_i[chan],
1172 &cur_i[chan]))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 goto exit;
1174 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 }
Guido van Rossum1851a671997-02-14 16:14:03 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* str <- Space for the output buffer. */
1178 {
1179 /* There are len input frames, so we need (mathematically)
1180 ceiling(len*outrate/inrate) output frames, and each frame
1181 requires bytes_per_frame bytes. Computing this
1182 without spurious overflow is the challenge; we can
Mark Dickinson393b97a2010-05-11 13:09:58 +00001183 settle for a reasonable upper bound, though, in this
1184 case ceiling(len/inrate) * outrate. */
1185
1186 /* compute ceiling(len/inrate) without overflow */
Mark Dickinson81fece22010-05-11 13:34:35 +00001187 Py_ssize_t q = len > 0 ? 1 + (len - 1) / inrate : 0;
1188 if (outrate > PY_SSIZE_T_MAX / q / bytes_per_frame)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 str = NULL;
1190 else
Mark Dickinson393b97a2010-05-11 13:09:58 +00001191 str = PyBytes_FromStringAndSize(NULL,
1192 q * outrate * bytes_per_frame);
Tim Peters1691bd92001-12-05 06:05:07 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (str == NULL) {
1195 PyErr_SetString(PyExc_MemoryError,
1196 "not enough memory for output buffer");
1197 goto exit;
1198 }
1199 }
1200 ncp = PyBytes_AsString(str);
1201
1202 for (;;) {
1203 while (d < 0) {
1204 if (len == 0) {
1205 samps = PyTuple_New(nchannels);
1206 if (samps == NULL)
1207 goto exit;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208 for (chan = 0; chan < nchannels; chan++)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 PyTuple_SetItem(samps, chan,
1210 Py_BuildValue("(ii)",
1211 prev_i[chan],
1212 cur_i[chan]));
1213 if (PyErr_Occurred())
1214 goto exit;
1215 /* We have checked before that the length
1216 * of the string fits into int. */
Mark Dickinson81fece22010-05-11 13:34:35 +00001217 len = (Py_ssize_t)(ncp - PyBytes_AsString(str));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 rv = PyBytes_FromStringAndSize
1219 (PyBytes_AsString(str), len);
1220 Py_DECREF(str);
1221 str = rv;
1222 if (str == NULL)
1223 goto exit;
1224 rv = Py_BuildValue("(O(iO))", str, d, samps);
1225 Py_DECREF(samps);
1226 Py_DECREF(str);
1227 goto exit; /* return rv */
1228 }
1229 for (chan = 0; chan < nchannels; chan++) {
1230 prev_i[chan] = cur_i[chan];
1231 if (size == 1)
1232 cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
1233 else if (size == 2)
1234 cur_i[chan] = (int)*SHORTP(cp, 0);
1235 else if (size == 4)
1236 cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
1237 cp += size;
1238 /* implements a simple digital filter */
1239 cur_i[chan] =
1240 (weightA * cur_i[chan] +
1241 weightB * prev_i[chan]) /
1242 (weightA + weightB);
1243 }
1244 len--;
1245 d += outrate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 while (d >= 0) {
1248 for (chan = 0; chan < nchannels; chan++) {
1249 cur_o = (prev_i[chan] * d +
1250 cur_i[chan] * (outrate - d)) /
1251 outrate;
1252 if (size == 1)
1253 *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
1254 else if (size == 2)
1255 *SHORTP(ncp, 0) = (short)(cur_o);
1256 else if (size == 4)
1257 *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
1258 ncp += size;
1259 }
1260 d -= inrate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 }
Guido van Rossum65bb3281999-09-07 14:24:05 +00001263 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (prev_i != NULL)
1265 free(prev_i);
1266 if (cur_i != NULL)
1267 free(cur_i);
1268 return rv;
Roger E. Massec905fff1997-01-17 18:12:04 +00001269}
Guido van Rossum1851a671997-02-14 16:14:03 +00001270
Roger E. Massec905fff1997-01-17 18:12:04 +00001271static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001272audioop_lin2ulaw(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001273{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 signed char *cp;
1275 unsigned char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001276 Py_ssize_t len, i;
1277 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
1281 &cp, &len, &size) )
1282 return 0 ;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001283
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001284 if (!audioop_check_parameters(len, size))
1285 return NULL;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 rv = PyBytes_FromStringAndSize(NULL, len/size);
1288 if ( rv == 0 )
1289 return 0;
1290 ncp = (unsigned char *)PyBytes_AsString(rv);
1291
1292 for ( i=0; i < len; i += size ) {
1293 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1294 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1295 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1296
1297 *ncp++ = st_14linear2ulaw(val);
1298 }
1299 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001300}
1301
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001302static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001303audioop_ulaw2lin(PyObject *self, PyObject *args)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 unsigned char *cp;
1306 unsigned char cval;
1307 signed char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001308 Py_ssize_t len, i;
1309 int size, val;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 PyObject *rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
1313 &cp, &len, &size) )
1314 return 0;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001315
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001316 if (!audioop_check_parameters(len, size))
1317 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318
Mark Dickinson81fece22010-05-11 13:34:35 +00001319 if (len > PY_SSIZE_T_MAX/size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyErr_SetString(PyExc_MemoryError,
1321 "not enough memory for output buffer");
1322 return 0;
1323 }
Mark Dickinson85eacea2010-05-10 16:27:45 +00001324 rv = PyBytes_FromStringAndSize(NULL, len*size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 if ( rv == 0 )
1326 return 0;
1327 ncp = (signed char *)PyBytes_AsString(rv);
1328
Mark Dickinson85eacea2010-05-10 16:27:45 +00001329 for ( i=0; i < len*size; i += size ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 cval = *cp++;
1331 val = st_ulaw2linear16(cval);
1332
1333 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1334 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1335 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1336 }
1337 return rv;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001338}
1339
1340static PyObject *
1341audioop_lin2alaw(PyObject *self, PyObject *args)
1342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 signed char *cp;
1344 unsigned char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001345 Py_ssize_t len, i;
1346 int size, val = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyObject *rv;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
1350 &cp, &len, &size) )
1351 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001353 if (!audioop_check_parameters(len, size))
1354 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 rv = PyBytes_FromStringAndSize(NULL, len/size);
1357 if ( rv == 0 )
1358 return 0;
1359 ncp = (unsigned char *)PyBytes_AsString(rv);
1360
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;
1365
1366 *ncp++ = st_linear2alaw(val);
1367 }
1368 return rv;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369}
1370
1371static PyObject *
1372audioop_alaw2lin(PyObject *self, PyObject *args)
1373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 unsigned char *cp;
1375 unsigned char cval;
1376 signed char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001377 Py_ssize_t len, i;
1378 int size, val;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 PyObject *rv;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
1382 &cp, &len, &size) )
1383 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001385 if (!audioop_check_parameters(len, size))
1386 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387
Mark Dickinson81fece22010-05-11 13:34:35 +00001388 if (len > PY_SSIZE_T_MAX/size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyErr_SetString(PyExc_MemoryError,
1390 "not enough memory for output buffer");
1391 return 0;
1392 }
Mark Dickinson85eacea2010-05-10 16:27:45 +00001393 rv = PyBytes_FromStringAndSize(NULL, len*size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if ( rv == 0 )
1395 return 0;
1396 ncp = (signed char *)PyBytes_AsString(rv);
1397
Mark Dickinson85eacea2010-05-10 16:27:45 +00001398 for ( i=0; i < len*size; i += size ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 cval = *cp++;
1400 val = st_alaw2linear16(cval);
1401
1402 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
1403 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
1404 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
1405 }
1406 return rv;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001407}
1408
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001409static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001410audioop_lin2adpcm(PyObject *self, PyObject *args)
Guido van Rossumb64e6351992-07-06 14:21:56 +00001411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 signed char *cp;
1413 signed char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001414 Py_ssize_t len, i;
1415 int size, val = 0, step, valpred, delta,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 index, sign, vpdiff, diff;
1417 PyObject *rv, *state, *str;
Mark Dickinson81fece22010-05-11 13:34:35 +00001418 int outputbuffer = 0, bufferstep;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
1421 &cp, &len, &size, &state) )
1422 return 0;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001423
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001424 if (!audioop_check_parameters(len, size))
1425 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426
1427 str = PyBytes_FromStringAndSize(NULL, len/(size*2));
1428 if ( str == 0 )
1429 return 0;
1430 ncp = (signed char *)PyBytes_AsString(str);
1431
1432 /* Decode state, should have (value, step) */
1433 if ( state == Py_None ) {
1434 /* First time, it seems. Set defaults */
1435 valpred = 0;
1436 step = 7;
1437 index = 0;
1438 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
1439 return 0;
1440
1441 step = stepsizeTable[index];
1442 bufferstep = 1;
1443
1444 for ( i=0; i < len; i += size ) {
1445 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
1446 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
1447 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1448
1449 /* Step 1 - compute difference with previous value */
1450 diff = val - valpred;
1451 sign = (diff < 0) ? 8 : 0;
1452 if ( sign ) diff = (-diff);
1453
1454 /* Step 2 - Divide and clamp */
1455 /* Note:
1456 ** This code *approximately* computes:
1457 ** delta = diff*4/step;
1458 ** vpdiff = (delta+0.5)*step/4;
1459 ** but in shift step bits are dropped. The net result of this
1460 ** is that even if you have fast mul/div hardware you cannot
1461 ** put it to good use since the fixup would be too expensive.
1462 */
1463 delta = 0;
1464 vpdiff = (step >> 3);
1465
1466 if ( diff >= step ) {
1467 delta = 4;
1468 diff -= step;
1469 vpdiff += step;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 step >>= 1;
1472 if ( diff >= step ) {
1473 delta |= 2;
1474 diff -= step;
1475 vpdiff += step;
1476 }
1477 step >>= 1;
1478 if ( diff >= step ) {
1479 delta |= 1;
1480 vpdiff += step;
1481 }
Jack Jansendd8a6ea1993-02-17 14:21:09 +00001482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 /* Step 3 - Update previous value */
1484 if ( sign )
1485 valpred -= vpdiff;
1486 else
1487 valpred += vpdiff;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 /* Step 4 - Clamp previous value to 16 bits */
1490 if ( valpred > 32767 )
1491 valpred = 32767;
1492 else if ( valpred < -32768 )
1493 valpred = -32768;
1494
1495 /* Step 5 - Assemble value, update index and step values */
1496 delta |= sign;
1497
1498 index += indexTable[delta];
1499 if ( index < 0 ) index = 0;
1500 if ( index > 88 ) index = 88;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501 step = stepsizeTable[index];
Guido van Rossumb64e6351992-07-06 14:21:56 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 /* Step 6 - Output value */
1504 if ( bufferstep ) {
1505 outputbuffer = (delta << 4) & 0xf0;
1506 } else {
1507 *ncp++ = (delta & 0x0f) | outputbuffer;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 bufferstep = !bufferstep;
1510 }
1511 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1512 Py_DECREF(str);
1513 return rv;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001514}
1515
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001516static PyObject *
Peter Schneider-Kamp8bc8f0d2000-07-10 17:15:07 +00001517audioop_adpcm2lin(PyObject *self, PyObject *args)
Guido van Rossumb64e6351992-07-06 14:21:56 +00001518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 signed char *cp;
1520 signed char *ncp;
Mark Dickinson81fece22010-05-11 13:34:35 +00001521 Py_ssize_t len, i;
1522 int size, valpred, step, delta, index, sign, vpdiff;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 PyObject *rv, *str, *state;
Mark Dickinson81fece22010-05-11 13:34:35 +00001524 int inputbuffer = 0, bufferstep;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
1527 &cp, &len, &size, &state) )
1528 return 0;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001529
Victor Stinnerbc5c54b2010-07-03 13:44:22 +00001530 if (!audioop_check_parameters(len, size))
1531 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532
1533 /* Decode state, should have (value, step) */
1534 if ( state == Py_None ) {
1535 /* First time, it seems. Set defaults */
1536 valpred = 0;
1537 step = 7;
1538 index = 0;
1539 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
1540 return 0;
1541
Mark Dickinson81fece22010-05-11 13:34:35 +00001542 if (len > (PY_SSIZE_T_MAX/2)/size) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 PyErr_SetString(PyExc_MemoryError,
1544 "not enough memory for output buffer");
1545 return 0;
1546 }
Mark Dickinson85eacea2010-05-10 16:27:45 +00001547 str = PyBytes_FromStringAndSize(NULL, len*size*2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if ( str == 0 )
1549 return 0;
1550 ncp = (signed char *)PyBytes_AsString(str);
1551
1552 step = stepsizeTable[index];
1553 bufferstep = 0;
1554
Mark Dickinson85eacea2010-05-10 16:27:45 +00001555 for ( i=0; i < len*size*2; i += size ) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 /* Step 1 - get the delta value and compute next index */
1557 if ( bufferstep ) {
1558 delta = inputbuffer & 0xf;
1559 } else {
1560 inputbuffer = *cp++;
1561 delta = (inputbuffer >> 4) & 0xf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562 }
Guido van Rossumb64e6351992-07-06 14:21:56 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 bufferstep = !bufferstep;
1565
1566 /* Step 2 - Find new index value (for later) */
1567 index += indexTable[delta];
1568 if ( index < 0 ) index = 0;
1569 if ( index > 88 ) index = 88;
1570
1571 /* Step 3 - Separate sign and magnitude */
1572 sign = delta & 8;
1573 delta = delta & 7;
1574
1575 /* Step 4 - Compute difference and new predicted value */
1576 /*
1577 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
1578 ** in adpcm_coder.
1579 */
1580 vpdiff = step >> 3;
1581 if ( delta & 4 ) vpdiff += step;
1582 if ( delta & 2 ) vpdiff += step>>1;
1583 if ( delta & 1 ) vpdiff += step>>2;
1584
1585 if ( sign )
1586 valpred -= vpdiff;
1587 else
1588 valpred += vpdiff;
1589
1590 /* Step 5 - clamp output value */
1591 if ( valpred > 32767 )
1592 valpred = 32767;
1593 else if ( valpred < -32768 )
1594 valpred = -32768;
1595
1596 /* Step 6 - Update step value */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001597 step = stepsizeTable[index];
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 /* Step 6 - Output value */
1600 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
1601 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
1602 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
1603 }
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 rv = Py_BuildValue("(O(ii))", str, valpred, index);
1606 Py_DECREF(str);
1607 return rv;
Guido van Rossumb64e6351992-07-06 14:21:56 +00001608}
1609
Roger E. Masseeaa6e111997-01-03 19:26:27 +00001610static PyMethodDef audioop_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 { "max", audioop_max, METH_VARARGS },
1612 { "minmax", audioop_minmax, METH_VARARGS },
1613 { "avg", audioop_avg, METH_VARARGS },
1614 { "maxpp", audioop_maxpp, METH_VARARGS },
1615 { "avgpp", audioop_avgpp, METH_VARARGS },
1616 { "rms", audioop_rms, METH_VARARGS },
1617 { "findfit", audioop_findfit, METH_VARARGS },
1618 { "findmax", audioop_findmax, METH_VARARGS },
1619 { "findfactor", audioop_findfactor, METH_VARARGS },
1620 { "cross", audioop_cross, METH_VARARGS },
1621 { "mul", audioop_mul, METH_VARARGS },
1622 { "add", audioop_add, METH_VARARGS },
1623 { "bias", audioop_bias, METH_VARARGS },
1624 { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
1625 { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
1626 { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
1627 { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
1628 { "lin2lin", audioop_lin2lin, METH_VARARGS },
1629 { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
1630 { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
1631 { "tomono", audioop_tomono, METH_VARARGS },
1632 { "tostereo", audioop_tostereo, METH_VARARGS },
1633 { "getsample", audioop_getsample, METH_VARARGS },
1634 { "reverse", audioop_reverse, METH_VARARGS },
1635 { "ratecv", audioop_ratecv, METH_VARARGS },
1636 { 0, 0 }
Guido van Rossumb66efa01992-06-01 16:01:24 +00001637};
1638
Martin v. Löwis1a214512008-06-11 05:26:20 +00001639
1640static struct PyModuleDef audioopmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 PyModuleDef_HEAD_INIT,
1642 "audioop",
1643 NULL,
1644 -1,
1645 audioop_methods,
1646 NULL,
1647 NULL,
1648 NULL,
1649 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001650};
1651
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001652PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001653PyInit_audioop(void)
Guido van Rossumb66efa01992-06-01 16:01:24 +00001654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 PyObject *m, *d;
1656 m = PyModule_Create(&audioopmodule);
1657 if (m == NULL)
1658 return NULL;
1659 d = PyModule_GetDict(m);
1660 if (d == NULL)
1661 return NULL;
1662 AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
1663 if (AudioopError != NULL)
1664 PyDict_SetItemString(d,"error",AudioopError);
1665 return m;
Guido van Rossumb66efa01992-06-01 16:01:24 +00001666}