Debian Codegraph @

lib_pypy/_audioop_build.py

raw symbols SCIP
1 from cffi import FFI
2
3 ffi = FFI()
4 ffi.cdef("""
5 typedef short PyInt16;
6
7 int ratecv(char* rv, char* cp, size_t len, int size,
8 int nchannels, int inrate, int outrate,
9 int* state_d, int* prev_i, int* cur_i,
10 int weightA, int weightB);
11
12 void tostereo(char* rv, char* cp, size_t len, int size,
13 double fac1, double fac2);
14 void add(char* rv, char* cp1, char* cp2, size_t len1, int size);
15
16 /* 2's complement (14-bit range) */
17 unsigned char
18 st_14linear2ulaw(PyInt16 pcm_val);
19 PyInt16 st_ulaw2linear16(unsigned char);
20
21 /* 2's complement (13-bit range) */
22 unsigned char
23 st_linear2alaw(PyInt16 pcm_val);
24 PyInt16 st_alaw2linear16(unsigned char);
25
26
27 void lin2adcpm(unsigned char* rv, unsigned char* cp, size_t len,
28 size_t size, int* state);
29 void adcpm2lin(unsigned char* rv, unsigned char* cp, size_t len,
30 size_t size, int* state);
31 """)
32
33 # This code is directly copied from CPython file: Modules/audioop.c
34 _AUDIOOP_C_MODULE = r"""
35 typedef short PyInt16;
36 typedef int Py_Int32;
37
38 /* Code shamelessly stolen from sox, 12.17.7, g711.c
39 ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
40
41 /* From g711.c:
42 *
43 * December 30, 1994:
44 * Functions linear2alaw, linear2ulaw have been updated to correctly
45 * convert unquantized 16 bit values.
46 * Tables for direct u- to A-law and A- to u-law conversions have been
47 * corrected.
48 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
49 * [email protected]
50 *
51 */
52 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
53 #define CLIP 32635
54 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
55 #define QUANT_MASK (0xf) /* Quantization field mask. */
56 #define SEG_SHIFT (4) /* Left shift for segment number. */
57 #define SEG_MASK (0x70) /* Segment field mask. */
58
59 static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
60 0x1FF, 0x3FF, 0x7FF, 0xFFF};
61 static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
62 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
63
64 static PyInt16
65 search(PyInt16 val, PyInt16 *table, int size)
66 {
67 int i;
68
69 for (i = 0; i < size; i++) {
70 if (val <= *table++)
71 return (i);
72 }
73 return (size);
74 }
75 #define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
76 #define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
77
78 static PyInt16 _st_ulaw2linear16[256] = {
79 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
80 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
81 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
82 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
83 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
84 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
85 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
86 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
87 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
88 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
89 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
90 -1052, -988, -924, -876, -844, -812, -780,
91 -748, -716, -684, -652, -620, -588, -556,
92 -524, -492, -460, -428, -396, -372, -356,
93 -340, -324, -308, -292, -276, -260, -244,
94 -228, -212, -196, -180, -164, -148, -132,
95 -120, -112, -104, -96, -88, -80, -72,
96 -64, -56, -48, -40, -32, -24, -16,
97 -8, 0, 32124, 31100, 30076, 29052, 28028,
98 27004, 25980, 24956, 23932, 22908, 21884, 20860,
99 19836, 18812, 17788, 16764, 15996, 15484, 14972,
100 14460, 13948, 13436, 12924, 12412, 11900, 11388,
101 10876, 10364, 9852, 9340, 8828, 8316, 7932,
102 7676, 7420, 7164, 6908, 6652, 6396, 6140,
103 5884, 5628, 5372, 5116, 4860, 4604, 4348,
104 4092, 3900, 3772, 3644, 3516, 3388, 3260,
105 3132, 3004, 2876, 2748, 2620, 2492, 2364,
106 2236, 2108, 1980, 1884, 1820, 1756, 1692,
107 1628, 1564, 1500, 1436, 1372, 1308, 1244,
108 1180, 1116, 1052, 988, 924, 876, 844,
109 812, 780, 748, 716, 684, 652, 620,
110 588, 556, 524, 492, 460, 428, 396,
111 372, 356, 340, 324, 308, 292, 276,
112 260, 244, 228, 212, 196, 180, 164,
113 148, 132, 120, 112, 104, 96, 88,
114 80, 72, 64, 56, 48, 40, 32,
115 24, 16, 8, 0
116 };
117
118 /*
119 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
120 * stored in a unsigned char. This function should only be called with
121 * the data shifted such that it only contains information in the lower
122 * 14-bits.
123 *
124 * In order to simplify the encoding process, the original linear magnitude
125 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
126 * (33 - 8191). The result can be seen in the following encoding table:
127 *
128 * Biased Linear Input Code Compressed Code
129 * ------------------------ ---------------
130 * 00000001wxyza 000wxyz
131 * 0000001wxyzab 001wxyz
132 * 000001wxyzabc 010wxyz
133 * 00001wxyzabcd 011wxyz
134 * 0001wxyzabcde 100wxyz
135 * 001wxyzabcdef 101wxyz
136 * 01wxyzabcdefg 110wxyz
137 * 1wxyzabcdefgh 111wxyz
138 *
139 * Each biased linear code has a leading 1 which identifies the segment
140 * number. The value of the segment number is equal to 7 minus the number
141 * of leading 0's. The quantization interval is directly available as the
142 * four bits wxyz. * The trailing bits (a - h) are ignored.
143 *
144 * Ordinarily the complement of the resulting code word is used for
145 * transmission, and so the code word is complemented before it is returned.
146 *
147 * For further information see John C. Bellamy's Digital Telephony, 1982,
148 * John Wiley & Sons, pps 98-111 and 472-476.
149 */
150 static unsigned char
151 st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
152 {
153 PyInt16 mask;
154 PyInt16 seg;
155 unsigned char uval;
156
157 /* The original sox code does this in the calling function, not here */
158 pcm_val = pcm_val >> 2;
159
160 /* u-law inverts all bits */
161 /* Get the sign and the magnitude of the value. */
162 if (pcm_val < 0) {
163 pcm_val = -pcm_val;
164 mask = 0x7F;
165 } else {
166 mask = 0xFF;
167 }
168 if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
169 pcm_val += (BIAS >> 2);
170
171 /* Convert the scaled magnitude to segment number. */
172 seg = search(pcm_val, seg_uend, 8);
173
174 /*
175 * Combine the sign, segment, quantization bits;
176 * and complement the code word.
177 */
178 if (seg >= 8) /* out of range, return maximum value. */
179 return (unsigned char) (0x7F ^ mask);
180 else {
181 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
182 return (uval ^ mask);
183 }
184
185 }
186
187 static PyInt16 _st_alaw2linear16[256] = {
188 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
189 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
190 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
191 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
192 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
193 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
194 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
195 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
196 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
197 -13568, -344, -328, -376, -360, -280, -264,
198 -312, -296, -472, -456, -504, -488, -408,
199 -392, -440, -424, -88, -72, -120, -104,
200 -24, -8, -56, -40, -216, -200, -248,
201 -232, -152, -136, -184, -168, -1376, -1312,
202 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
203 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
204 -688, -656, -752, -720, -560, -528, -624,
205 -592, -944, -912, -1008, -976, -816, -784,
206 -880, -848, 5504, 5248, 6016, 5760, 4480,
207 4224, 4992, 4736, 7552, 7296, 8064, 7808,
208 6528, 6272, 7040, 6784, 2752, 2624, 3008,
209 2880, 2240, 2112, 2496, 2368, 3776, 3648,
210 4032, 3904, 3264, 3136, 3520, 3392, 22016,
211 20992, 24064, 23040, 17920, 16896, 19968, 18944,
212 30208, 29184, 32256, 31232, 26112, 25088, 28160,
213 27136, 11008, 10496, 12032, 11520, 8960, 8448,
214 9984, 9472, 15104, 14592, 16128, 15616, 13056,
215 12544, 14080, 13568, 344, 328, 376, 360,
216 280, 264, 312, 296, 472, 456, 504,
217 488, 408, 392, 440, 424, 88, 72,
218 120, 104, 24, 8, 56, 40, 216,
219 200, 248, 232, 152, 136, 184, 168,
220 1376, 1312, 1504, 1440, 1120, 1056, 1248,
221 1184, 1888, 1824, 2016, 1952, 1632, 1568,
222 1760, 1696, 688, 656, 752, 720, 560,
223 528, 624, 592, 944, 912, 1008, 976,
224 816, 784, 880, 848
225 };
226
227 /*
228 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
229 * stored in a unsigned char. This function should only be called with
230 * the data shifted such that it only contains information in the lower
231 * 13-bits.
232 *
233 * Linear Input Code Compressed Code
234 * ------------------------ ---------------
235 * 0000000wxyza 000wxyz
236 * 0000001wxyza 001wxyz
237 * 000001wxyzab 010wxyz
238 * 00001wxyzabc 011wxyz
239 * 0001wxyzabcd 100wxyz
240 * 001wxyzabcde 101wxyz
241 * 01wxyzabcdef 110wxyz
242 * 1wxyzabcdefg 111wxyz
243 *
244 * For further information see John C. Bellamy's Digital Telephony, 1982,
245 * John Wiley & Sons, pps 98-111 and 472-476.
246 */
247 static unsigned char
248 st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
249 {
250 PyInt16 mask;
251 short seg;
252 unsigned char aval;
253
254 /* The original sox code does this in the calling function, not here */
255 pcm_val = pcm_val >> 3;
256
257 /* A-law using even bit inversion */
258 if (pcm_val >= 0) {
259 mask = 0xD5; /* sign (7th) bit = 1 */
260 } else {
261 mask = 0x55; /* sign bit = 0 */
262 pcm_val = -pcm_val - 1;
263 }
264
265 /* Convert the scaled magnitude to segment number. */
266 seg = search(pcm_val, seg_aend, 8);
267
268 /* Combine the sign, segment, and quantization bits. */
269
270 if (seg >= 8) /* out of range, return maximum value. */
271 return (unsigned char) (0x7F ^ mask);
272 else {
273 aval = (unsigned char) seg << SEG_SHIFT;
274 if (seg < 2)
275 aval |= (pcm_val >> 1) & QUANT_MASK;
276 else
277 aval |= (pcm_val >> seg) & QUANT_MASK;
278 return (aval ^ mask);
279 }
280 }
281 /* End of code taken from sox */
282
283 /* Intel ADPCM step variation table */
284 static int indexTable[16] = {
285 -1, -1, -1, -1, 2, 4, 6, 8,
286 -1, -1, -1, -1, 2, 4, 6, 8,
287 };
288
289 static int stepsizeTable[89] = {
290 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
291 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
292 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
293 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
294 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
295 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
296 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
297 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
298 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
299 };
300
301 #define CHARP(cp, i) ((signed char *)(cp+i))
302 #define SHORTP(cp, i) ((short *)(cp+i))
303 #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
304
305 #if WORDS_BIGENDIAN
306 #define GETINT24(cp, i) ( \
307 ((unsigned char *)(cp) + (i))[2] + \
308 (((unsigned char *)(cp) + (i))[1] << 8) + \
309 (((signed char *)(cp) + (i))[0] << 16) )
310 #else
311 #define GETINT24(cp, i) ( \
312 ((unsigned char *)(cp) + (i))[0] + \
313 (((unsigned char *)(cp) + (i))[1] << 8) + \
314 (((signed char *)(cp) + (i))[2] << 16) )
315 #endif
316
317 #if WORDS_BIGENDIAN
318 #define SETINT24(cp, i, val) do { \
319 ((unsigned char *)(cp) + (i))[2] = (int)(val); \
320 ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8; \
321 ((signed char *)(cp) + (i))[0] = (int)(val) >> 16; \
322 } while (0)
323 #else
324 #define SETINT24(cp, i, val) do { \
325 ((unsigned char *)(cp) + (i))[0] = (int)(val); \
326 ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8; \
327 ((signed char *)(cp) + (i))[2] = (int)(val) >> 16; \
328 } while (0)
329 #endif
330 """
331
332 C_SOURCE = _AUDIOOP_C_MODULE + r"""
333 #include <math.h>
334
335 static const int maxvals[] = {0, 0x7F, 0x7FFF, 0x7FFFFF, 0x7FFFFFFF};
336 /* -1 trick is needed on Windows to support -0x80000000 without a warning */
337 static const int minvals[] = {0, -0x80, -0x8000, -0x800000, -0x7FFFFFFF-1};
338
339 static int
340 fbound(double val, double minval, double maxval)
341 {
342 if (val > maxval) {
343 val = maxval;
344 }
345 else if (val < minval + 1.0) {
346 val = minval;
347 }
348
349 /* Round towards minus infinity (-inf) */
350 val = floor(val);
351
352 /* Cast double to integer: round towards zero */
353 return (int)val;
354 }
355
356 static int
357 gcd(int a, int b)
358 {
359 while (b > 0) {
360 int tmp = a % b;
361 a = b;
362 b = tmp;
363 }
364 return a;
365 }
366
367 static
368 int ratecv(char* rv, char* cp, size_t len, int size,
369 int nchannels, int inrate, int outrate,
370 int* state_d, int* prev_i, int* cur_i,
371 int weightA, int weightB)
372 {
373 char *ncp = rv;
374 int d, chan;
375
376 /* divide inrate and outrate by their greatest common divisor */
377 d = gcd(inrate, outrate);
378 inrate /= d;
379 outrate /= d;
380 /* divide weightA and weightB by their greatest common divisor */
381 d = gcd(weightA, weightB);
382 weightA /= d;
383 weightA /= d;
384
385 d = *state_d;
386
387 for (;;) {
388 while (d < 0) {
389 if (len == 0) {
390 *state_d = d;
391 return ncp - rv;
392 }
393 for (chan = 0; chan < nchannels; chan++) {
394 prev_i[chan] = cur_i[chan];
395 if (size == 1)
396 cur_i[chan] = ((int)*CHARP(cp, 0)) << 24;
397 else if (size == 2)
398 cur_i[chan] = ((int)*SHORTP(cp, 0)) << 16;
399 else if (size == 3)
400 cur_i[chan] = ((int)GETINT24(cp, 0)) << 8;
401 else if (size == 4)
402 cur_i[chan] = (int)*LONGP(cp, 0);
403 cp += size;
404 /* implements a simple digital filter */
405 cur_i[chan] = (int)(
406 ((double)weightA * (double)cur_i[chan] +
407 (double)weightB * (double)prev_i[chan]) /
408 ((double)weightA + (double)weightB));
409 }
410 len--;
411 d += outrate;
412 }
413 while (d >= 0) {
414 for (chan = 0; chan < nchannels; chan++) {
415 int cur_o;
416 cur_o = (int)(((double)prev_i[chan] * (double)d +
417 (double)cur_i[chan] * (double)(outrate - d)) /
418 (double)outrate);
419 if (size == 1)
420 *CHARP(ncp, 0) = (signed char)(cur_o >> 24);
421 else if (size == 2)
422 *SHORTP(ncp, 0) = (short)(cur_o >> 16);
423 else if (size == 3)
424 SETINT24(ncp, 0, cur_o >> 8);
425 else if (size == 4)
426 *LONGP(ncp, 0) = (Py_Int32)(cur_o);
427 ncp += size;
428 }
429 d -= inrate;
430 }
431 }
432 }
433
434 static
435 void tostereo(char* rv, char* cp, size_t len, int size,
436 double fac1, double fac2)
437 {
438 int val1, val2, val = 0;
439 double fval, maxval, minval;
440 char *ncp = rv;
441 int i;
442
443 maxval = (double) maxvals[size];
444 minval = (double) minvals[size];
445
446 for ( i=0; i < len; i += size ) {
447 if ( size == 1 ) val = (int)*CHARP(cp, i);
448 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
449 else if ( size == 3 ) val = (int)GETINT24(cp, i);
450 else if ( size == 4 ) val = (int)*LONGP(cp, i);
451
452 fval = (double)val * fac1;
453 val1 = fbound(fval, minval, maxval);
454
455 fval = (double)val * fac2;
456 val2 = fbound(fval, minval, maxval);
457
458 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
459 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
460 else if ( size == 3 ) SETINT24(ncp, i*2, val1);
461 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
462
463 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
464 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
465 else if ( size == 3 ) SETINT24(ncp, i*2+3, val2);
466 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
467 }
468 }
469
470 static
471 void add(char* rv, char* cp1, char* cp2, size_t len1, int size)
472 {
473 int i;
474 int val1 = 0, val2 = 0, minval, maxval, newval;
475 char* ncp = rv;
476
477 maxval = maxvals[size];
478 minval = minvals[size];
479
480 for ( i=0; i < len1; i += size ) {
481 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
482 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
483 else if ( size == 3 ) val1 = (int)GETINT24(cp1, i);
484 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
485
486 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
487 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
488 else if ( size == 3 ) val2 = (int)GETINT24(cp2, i);
489 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
490
491 if (size < 4) {
492 newval = val1 + val2;
493 /* truncate in case of overflow */
494 if (newval > maxval)
495 newval = maxval;
496 else if (newval < minval)
497 newval = minval;
498 }
499 else {
500 double fval = (double)val1 + (double)val2;
501 /* truncate in case of overflow */
502 newval = fbound(fval, minval, maxval);
503 }
504
505 if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
506 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
507 else if ( size == 3 ) SETINT24(ncp, i, newval);
508 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
509 }
510 }
511
512 static
513 void lin2adcpm(unsigned char* ncp, unsigned char* cp, size_t len,
514 size_t size, int* state)
515 {
516 int step, outputbuffer = 0, bufferstep;
517 int val = 0;
518 int diff, vpdiff, sign, delta;
519 size_t i;
520 int valpred = state[0];
521 int index = state[1];
522
523 step = stepsizeTable[index];
524 bufferstep = 1;
525
526 for ( i=0; i < len; i += size ) {
527 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
528 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
529 else if ( size == 3 ) val = ((int)GETINT24(cp, i)) >> 8;
530 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
531
532 /* Step 1 - compute difference with previous value */
533 diff = val - valpred;
534 sign = (diff < 0) ? 8 : 0;
535 if ( sign ) diff = (-diff);
536
537 /* Step 2 - Divide and clamp */
538 /* Note:
539 ** This code *approximately* computes:
540 ** delta = diff*4/step;
541 ** vpdiff = (delta+0.5)*step/4;
542 ** but in shift step bits are dropped. The net result of this
543 ** is that even if you have fast mul/div hardware you cannot
544 ** put it to good use since the fixup would be too expensive.
545 */
546 delta = 0;
547 vpdiff = (step >> 3);
548
549 if ( diff >= step ) {
550 delta = 4;
551 diff -= step;
552 vpdiff += step;
553 }
554 step >>= 1;
555 if ( diff >= step ) {
556 delta |= 2;
557 diff -= step;
558 vpdiff += step;
559 }
560 step >>= 1;
561 if ( diff >= step ) {
562 delta |= 1;
563 vpdiff += step;
564 }
565
566 /* Step 3 - Update previous value */
567 if ( sign )
568 valpred -= vpdiff;
569 else
570 valpred += vpdiff;
571
572 /* Step 4 - Clamp previous value to 16 bits */
573 if ( valpred > 32767 )
574 valpred = 32767;
575 else if ( valpred < -32768 )
576 valpred = -32768;
577
578 /* Step 5 - Assemble value, update index and step values */
579 delta |= sign;
580
581 index += indexTable[delta];
582 if ( index < 0 ) index = 0;
583 if ( index > 88 ) index = 88;
584 step = stepsizeTable[index];
585
586 /* Step 6 - Output value */
587 if ( bufferstep ) {
588 outputbuffer = (delta << 4) & 0xf0;
589 } else {
590 *ncp++ = (delta & 0x0f) | outputbuffer;
591 }
592 bufferstep = !bufferstep;
593 }
594 state[0] = valpred;
595 state[1] = index;
596 }
597
598
599 static
600 void adcpm2lin(unsigned char* ncp, unsigned char* cp, size_t len,
601 size_t size, int* state)
602 {
603 int step, inputbuffer = 0, bufferstep;
604 int val = 0;
605 int diff, vpdiff, sign, delta;
606 size_t i;
607 int valpred = state[0];
608 int index = state[1];
609
610 step = stepsizeTable[index];
611 bufferstep = 0;
612
613 for ( i=0; i < len*size*2; i += size ) {
614 /* Step 1 - get the delta value and compute next index */
615 if ( bufferstep ) {
616 delta = inputbuffer & 0xf;
617 } else {
618 inputbuffer = *cp++;
619 delta = (inputbuffer >> 4) & 0xf;
620 }
621
622 bufferstep = !bufferstep;
623
624 /* Step 2 - Find new index value (for later) */
625 index += indexTable[delta];
626 if ( index < 0 ) index = 0;
627 if ( index > 88 ) index = 88;
628
629 /* Step 3 - Separate sign and magnitude */
630 sign = delta & 8;
631 delta = delta & 7;
632
633 /* Step 4 - Compute difference and new predicted value */
634 /*
635 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
636 ** in adpcm_coder.
637 */
638 vpdiff = step >> 3;
639 if ( delta & 4 ) vpdiff += step;
640 if ( delta & 2 ) vpdiff += step>>1;
641 if ( delta & 1 ) vpdiff += step>>2;
642
643 if ( sign )
644 valpred -= vpdiff;
645 else
646 valpred += vpdiff;
647
648 /* Step 5 - clamp output value */
649 if ( valpred > 32767 )
650 valpred = 32767;
651 else if ( valpred < -32768 )
652 valpred = -32768;
653
654 /* Step 6 - Update step value */
655 step = stepsizeTable[index];
656
657 /* Step 6 - Output value */
658 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
659 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
660 else if ( size == 3 ) SETINT24(ncp, i, valpred << 8);
661 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
662 }
663 state[0] = valpred;
664 state[1] = index;
665 }
666 """
667
668 ffi.set_source("_audioop_cffi", C_SOURCE)
669
670 if __name__ == "__main__":
671 import sys
672 print('using python from', sys.executable)
673 ffi.compile(verbose=2)