_audioop_build.py 23 KB

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